Skip to content

Commit a9025c5

Browse files
authored
fixes an issue with ARM APSR register handling: (unicorn-engine#1317)
- Q flag / GE flag were not included in APSR register (read/write) - UC_ARM_REG_APSR_NZCV register constant was ignored completely. - regression test added
1 parent 3134f33 commit a9025c5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

qemu/target-arm/unicorn_arm.c

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ int arm_reg_read(struct uc_struct *uc, unsigned int *regs, void **vals, int coun
6969
else {
7070
switch(regid) {
7171
case UC_ARM_REG_APSR:
72+
*(int32_t *)value = cpsr_read(&ARM_CPU(uc, mycpu)->env) & (CPSR_NZCV | CPSR_Q | CPSR_GE);
73+
break;
74+
case UC_ARM_REG_APSR_NZCV:
7275
*(int32_t *)value = cpsr_read(&ARM_CPU(uc, mycpu)->env) & CPSR_NZCV;
7376
break;
7477
case UC_ARM_REG_CPSR:
@@ -132,6 +135,9 @@ int arm_reg_write(struct uc_struct *uc, unsigned int *regs, void* const* vals, i
132135
else {
133136
switch(regid) {
134137
case UC_ARM_REG_APSR:
138+
cpsr_write(&ARM_CPU(uc, mycpu)->env, *(uint32_t *)value, (CPSR_NZCV | CPSR_Q | CPSR_GE));
139+
break;
140+
case UC_ARM_REG_APSR_NZCV:
135141
cpsr_write(&ARM_CPU(uc, mycpu)->env, *(uint32_t *)value, CPSR_NZCV);
136142
break;
137143
case UC_ARM_REG_CPSR:

tests/regress/arm_apsr_access.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/python
2+
3+
from unicorn import *
4+
from unicorn.arm_const import *
5+
6+
import regress
7+
8+
class APSRAccess(regress.RegressTest):
9+
10+
def runTest(self):
11+
code = (
12+
b'\x00\x00\xa0\xe1' + # 0: mov r0, r0
13+
b'\x08\x10\x9f\xe5' + # 4: ldr r1, [pc, #8]
14+
b'\x01\xf0\x28\xe1' + # 8: 01 f0 28 e1 msr apsr_nzcvq, r1
15+
b'\x00\x00\xa0\xe1' + # c: mov r0, r0
16+
b'\x00\x00\xa0\xe1' + # 10: mov r0, r0
17+
b'\x00\x00\x00\xff') # 14: data for inst @4
18+
19+
uc = Uc(UC_ARCH_ARM, UC_MODE_ARM)
20+
uc.mem_map(0x1000, 0x1000)
21+
uc.mem_write(0x1000, code) # bxeq lr; mov r0, r0
22+
23+
uc.reg_write(UC_ARM_REG_APSR, 0)
24+
uc.emu_start(0x1000, 0x100c)
25+
26+
self.assertEqual(uc.reg_read(UC_ARM_REG_APSR), 0xf8000000)
27+
self.assertEqual(uc.reg_read(UC_ARM_REG_APSR_NZCV), 0xf0000000)
28+
29+
if __name__ == '__main__':
30+
regress.main()

0 commit comments

Comments
 (0)