Skip to content

Commit 7315a7c

Browse files
marpomramosian-glider
authored andcommitted
executor: arm64: add SYZOS_API_MRS
Add support for the MRS instruction in a similar manner to MSR.
1 parent f310a27 commit 7315a7c

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

executor/common_kvm_arm64_syzos.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef enum {
2828
SYZOS_API_MEMWRITE,
2929
SYZOS_API_ITS_SETUP,
3030
SYZOS_API_ITS_SEND_CMD,
31+
SYZOS_API_MRS,
3132
SYZOS_API_STOP, // Must be the last one
3233
} syzos_api_id;
3334

@@ -41,6 +42,11 @@ struct api_call_uexit {
4142
uint64 exit_code;
4243
};
4344

45+
struct api_call_1 {
46+
struct api_call_header header;
47+
uint64 arg;
48+
};
49+
4450
struct api_call_2 {
4551
struct api_call_header header;
4652
uint64 args[2];
@@ -89,6 +95,7 @@ struct api_call_its_send_cmd {
8995

9096
static void guest_uexit(uint64 exit_code);
9197
static void guest_execute_code(uint32* insns, uint64 size);
98+
static void guest_handle_mrs(uint64 reg);
9299
static void guest_handle_msr(uint64 reg, uint64 val);
93100
static void guest_handle_smc(struct api_call_smccc* cmd);
94101
static void guest_handle_hvc(struct api_call_smccc* cmd);
@@ -128,6 +135,11 @@ guest_main(uint64 size, uint64 cpu)
128135
guest_execute_code(ccmd->insns, cmd->size - sizeof(struct api_call_header));
129136
break;
130137
}
138+
case SYZOS_API_MRS: {
139+
struct api_call_1* ccmd = (struct api_call_1*)cmd;
140+
guest_handle_mrs(ccmd->arg);
141+
break;
142+
}
131143
case SYZOS_API_MSR: {
132144
struct api_call_2* ccmd = (struct api_call_2*)cmd;
133145
guest_handle_msr(ccmd->args[0], ccmd->args[1]);
@@ -180,6 +192,7 @@ GUEST_CODE static noinline void guest_uexit(uint64 exit_code)
180192
}
181193

182194
#define MSR_REG_OPCODE 0xd5100000
195+
#define MRS_REG_OPCODE 0xd5300000
183196

184197
// Generate an `MSR register, x0` instruction based on the register ID.
185198
// Luckily for us, the five operands, Op0, Op1, CRn, CRm, and Op2 are laid out sequentially in
@@ -191,6 +204,12 @@ GUEST_CODE static uint32 reg_to_msr(uint64 reg)
191204
return MSR_REG_OPCODE | ((reg & 0xffff) << 5);
192205
}
193206

207+
// Generate an `MRS register, x0` instruction based on the register ID.
208+
GUEST_CODE static uint32 reg_to_mrs(uint64 reg)
209+
{
210+
return MRS_REG_OPCODE | ((reg & 0xffff) << 5);
211+
}
212+
194213
// Host sets TPIDR_EL1 to contain the virtual CPU id.
195214
GUEST_CODE static uint32 get_cpu_id()
196215
{
@@ -203,6 +222,23 @@ GUEST_CODE static uint32 get_cpu_id()
203222
// Some ARM chips use 128-byte cache lines. Pick 256 to be on the safe side.
204223
#define MAX_CACHE_LINE_SIZE 256
205224

225+
// Read the value from a system register using an MRS instruction.
226+
GUEST_CODE static noinline void
227+
guest_handle_mrs(uint64 reg)
228+
{
229+
uint32 mrs = reg_to_mrs(reg);
230+
uint32 cpu_id = get_cpu_id();
231+
// Make sure CPUs use different cache lines for scratch code.
232+
uint32* insn = (uint32*)((uint64)ARM64_ADDR_SCRATCH_CODE + cpu_id * MAX_CACHE_LINE_SIZE);
233+
insn[0] = mrs;
234+
insn[1] = 0xd65f03c0; // RET
235+
// Make a call to the generated MSR instruction and clobber x0.
236+
asm("blr %[pc]\n"
237+
:
238+
: [pc] "r"(insn)
239+
: "x0", "x30");
240+
}
241+
206242
// Write value to a system register using an MSR instruction.
207243
// The word "MSR" here has nothing to do with the x86 MSR registers.
208244
GUEST_CODE static noinline void

sys/linux/dev_kvm_arm64.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ syzos_api_code {
100100
ret const[0xd65f03c0, int32]
101101
} [packed]
102102

103+
syzos_api_mrs {
104+
arg_reg flags[kvm_regs_arm64_sys, int64]
105+
}
106+
103107
syzos_api_msr {
104108
arg_reg flags[kvm_regs_arm64_sys, int64]
105109
arg_value int64
@@ -207,4 +211,5 @@ syzos_api_call [
207211
memwrite syzos_api[6, syzos_api_memwrite]
208212
its_setup syzos_api[7, syzos_api_its_setup]
209213
its_send_cmd syzos_api[8, syzos_api_its_send_cmd]
214+
mrs syzos_api[9, syzos_api_mrs]
210215
] [varlen]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# requires: arch=arm64 -threaded
3+
#
4+
r0 = openat$kvm(0, &AUTO='/dev/kvm\x00', 0x0, 0x0)
5+
r1 = ioctl$KVM_CREATE_VM(r0, AUTO, 0x0)
6+
r2 = syz_kvm_setup_syzos_vm(r1, &(0x7f0000c00000/0x400000)=nil)
7+
#
8+
# 0x603000000013df40 is PMEVCNTR0_EL0, write to it will trigger access_pmu_evcntr() in arch/arm64/kvm/sys_regs.c
9+
# This is done to illustrate that PMU is accessible.
10+
# 0x8 corresponds to the KVM_ARM_VCPU_PMU_V3 feature bit and is required to enable PMU.
11+
#
12+
r3 = syz_kvm_add_vcpu(r2, &AUTO={0x0, &AUTO=[@mrs={AUTO, AUTO, {0x603000000013df40}}], AUTO}, &AUTO=[@featur1={0x1, 0x8}], 0x1)
13+
#
14+
# Call ioctl(KVM_SET_DEVICE_ATTR) with group=KVM_ARM_VCPU_PMU_V3_CTRL and attr=KVM_ARM_VCPU_PMU_V3_INIT,
15+
# as per https://www.kernel.org/doc/Documentation/virt/kvm/devices/vcpu.rst.
16+
#
17+
ioctl$KVM_SET_DEVICE_ATTR_vcpu(r3, AUTO, &AUTO=@attr_pmu_init)
18+
19+
r4 = ioctl$KVM_GET_VCPU_MMAP_SIZE(r0, AUTO)
20+
r5 = mmap$KVM_VCPU(&(0x7f0000009000/0x1000)=nil, r4, 0x3, 0x1, r3, 0x0)
21+
#
22+
# Run till the end of guest_main(). 0xffffffffffffffff is UEXIT_END.
23+
#
24+
ioctl$KVM_RUN(r3, AUTO, 0x0)
25+
syz_kvm_assert_syzos_uexit(r5, 0xffffffffffffffff)

sys/linux/test/arm64-syz_kvm_setup_syzos_vm-enable-pmu renamed to sys/linux/test/arm64-syz_kvm_setup_syzos_vm-enable-pmu-msr

File renamed without changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# requires: arch=arm64 -threaded
3+
#
4+
r0 = openat$kvm(0, &AUTO='/dev/kvm\x00', 0x0, 0x0)
5+
r1 = ioctl$KVM_CREATE_VM(r0, AUTO, 0x0)
6+
r2 = syz_kvm_setup_syzos_vm(r1, &(0x7f0000c00000/0x400000)=nil)
7+
#
8+
# 0x6030000000138010 is MDCCINT_EL1.
9+
#
10+
r3 = syz_kvm_add_vcpu(r2, &AUTO={0x0, &AUTO=[@mrs={AUTO, AUTO, {0x6030000000138010}}], AUTO}, 0x0, 0x0)
11+
r4 = ioctl$KVM_GET_VCPU_MMAP_SIZE(r0, AUTO)
12+
r5 = mmap$KVM_VCPU(&(0x7f0000009000/0x1000)=nil, r4, 0x3, 0x1, r3, 0x0)
13+
14+
# Run till the end of guest_main(). 0xffffffffffffffff is UEXIT_END.
15+
#
16+
ioctl$KVM_RUN(r3, AUTO, 0x0)
17+
syz_kvm_assert_syzos_uexit(r5, 0xffffffffffffffff)

0 commit comments

Comments
 (0)