Skip to content

Commit dd47c44

Browse files
authored
Merge pull request #1056 from cla7aye15I4nd/stm32f4
Some fix for ARM exception handler and mnist example
2 parents b1d9216 + ff0efbd commit dd47c44

30 files changed

+444
-35
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4+
#
5+
6+
import sys
7+
sys.path.append("../..")
8+
9+
from qiling.core import Qiling
10+
from qiling.const import QL_VERBOSE
11+
from qiling.extensions.mcu.stm32f4 import stm32f407
12+
from qiling.hw.external_device.oled.ssd1306 import PyGameSSD1306Spi
13+
14+
15+
ql = Qiling(["../rootfs/mcu/stm32f407/mnist.bin", 0x8000000],
16+
archtype="cortex_m", env=stm32f407, verbose=QL_VERBOSE.DEFAULT)
17+
18+
ql.hw.create('rcc')
19+
ql.hw.create('gpiod')
20+
ql.hw.create('spi1')
21+
ql.hw.create('crc')
22+
23+
oled = PyGameSSD1306Spi(dc=(ql.hw.gpiod, 5))
24+
ql.hw.spi1.connect(oled)
25+
26+
ql.hw.systick.ratio = 1000
27+
28+
## a temporary method
29+
def hook_smlabb(ql):
30+
ql.reg.r3 = ql.reg.r2 + ql.reg.r1 * ql.reg.r3
31+
ql.reg.pc = (ql.reg.pc + 4) | 1
32+
33+
ql.hook_address(hook_smlabb, 0x8007a12)
34+
ql.hook_address(hook_smlabb, 0x8007b60)
35+
36+
ql.run()

examples/rootfs

qiling/arch/cortex_m.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def step(self):
8585
self.ql.hw.step()
8686

8787
def stop(self):
88+
self.ql.emu_stop()
8889
self.runable = False
8990

9091
def run(self, count=-1, end=None):
@@ -112,13 +113,34 @@ def init_context(self):
112113
self.ql.reg.write('pc' , self.ql.mem.read_ptr(0x4))
113114

114115
def soft_interrupt_handler(self, ql, intno):
115-
if intno == EXCP.SWI:
116-
ql.hw.nvic.set_pending(IRQ.SVCALL)
117-
118-
elif intno == EXCP.EXCEPTION_EXIT:
119-
ql.emu_stop()
120-
121-
else:
116+
forward_mapper = {
117+
EXCP.UDEF : IRQ.HARD_FAULT, # undefined instruction
118+
EXCP.SWI : IRQ.SVCALL, # software interrupt
119+
EXCP.PREFETCH_ABORT : IRQ.HARD_FAULT,
120+
EXCP.DATA_ABORT : IRQ.HARD_FAULT,
121+
EXCP.EXCEPTION_EXIT : IRQ.NOTHING,
122+
# EXCP.KERNEL_TRAP : IRQ.NOTHING,
123+
# EXCP.HVC : IRQ.NOTHING,
124+
# EXCP.HYP_TRAP : IRQ.NOTHING,
125+
# EXCP.SMC : IRQ.NOTHING,
126+
# EXCP.VIRQ : IRQ.NOTHING,
127+
# EXCP.VFIQ : IRQ.NOTHING,
128+
# EXCP.SEMIHOST : IRQ.NOTHING,
129+
EXCP.NOCP : IRQ.USAGE_FAULT, # v7M NOCP UsageFault
130+
EXCP.INVSTATE : IRQ.USAGE_FAULT, # v7M INVSTATE UsageFault
131+
EXCP.STKOF : IRQ.USAGE_FAULT, # v8M STKOF UsageFault
132+
# EXCP.LAZYFP : IRQ.NOTHING,
133+
# EXCP.LSERR : IRQ.NOTHING,
134+
EXCP.UNALIGNED : IRQ.USAGE_FAULT, # v7M UNALIGNED UsageFault
135+
}
136+
137+
ql.emu_stop()
138+
139+
try:
140+
handle = forward_mapper.get(intno)
141+
if handle != IRQ.NOTHING:
142+
ql.hw.nvic.set_pending(handle)
143+
except IndexError:
122144
raise QlErrorNotImplemented(f'Unhandled interrupt number ({intno})')
123145

124146
def hard_interrupt_handler(self, ql, intno):

qiling/arch/cortex_m_const.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,37 @@ class IRQ(IntEnum):
4848
SVCALL = -5
4949
PENDSV = -2
5050
SYSTICK = -1
51+
NOTHING = 0
5152

5253
class CONTROL(IntEnum):
5354
FPCA = 0b100
5455
SPSEL = 0b010
5556
PRIV = 0b001
5657

5758
class EXC_RETURN(IntEnum):
58-
MASK = 0xfffffff0
59+
MASK = 0xfffffff0
5960
RETURN_SP = 0b0100
6061
RETURN_MODE = 0b1000
6162

6263
class EXCP(IntEnum):
63-
SWI = 2 # software interrupt
64-
EXCEPTION_EXIT = 8 # Return from v7M exception
65-
64+
UDEF = 1 # undefined instruction
65+
SWI = 2 # software interrupt
66+
PREFETCH_ABORT = 3
67+
DATA_ABORT = 4
68+
IRQ = 5
69+
FIQ = 6
70+
BKPT = 7
71+
EXCEPTION_EXIT = 8 # Return from v7M exception.
72+
KERNEL_TRAP = 9 # Jumped to kernel code page.
73+
HVC = 11 # HyperVisor Call
74+
HYP_TRAP = 12
75+
SMC = 13 # Secure Monitor Call
76+
VIRQ = 14
77+
VFIQ = 15
78+
SEMIHOST = 16 # semihosting call
79+
NOCP = 17 # v7M NOCP UsageFault
80+
INVSTATE = 18 # v7M INVSTATE UsageFault
81+
STKOF = 19 # v8M STKOF UsageFault
82+
LAZYFP = 20 # v7M fault during lazy FP stacking
83+
LSERR = 21 # v8M LSERR SecureFault
84+
UNALIGNED = 22 # v7M UNALIGNED UsageFault
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4+
#
5+
6+
from .bes2300 import bes2300
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
4+
#
5+
6+
bes2300 = {
7+
"ROM": {
8+
"base":0x0,
9+
"size":0xc000,
10+
"type": "memory"
11+
},
12+
"RAM": {
13+
"base":0x200a0000,
14+
"size":0x20000,
15+
"type": "memory"
16+
},
17+
"FLASH": {
18+
"base": 0x3C000000,
19+
"size": 0x100000,
20+
"type": "memory"
21+
},
22+
"CMU": {
23+
"struct": "BES2300Cmu",
24+
"base":0x40000000,
25+
"type": "peripheral"
26+
},
27+
"I2C0": {
28+
"struct": "BES2300I2c",
29+
"base":0x40005000,
30+
"type": "peripheral"
31+
},
32+
"I2C1": {
33+
"struct": "BES2300I2c",
34+
"base":0x40006000,
35+
"type": "peripheral"
36+
},
37+
"SPI": {
38+
"struct": "BES2300Spi",
39+
"base":0x40007000,
40+
"type": "peripheral"
41+
},
42+
"SPILCD": {
43+
"struct": "BES2300Spi",
44+
"base":0x40008000,
45+
"type": "peripheral"
46+
},
47+
"SPIPHY": {
48+
"struct": "BES2300Spi",
49+
"base":0x4000a000,
50+
"type": "peripheral"
51+
},
52+
"UART0": {
53+
"struct": "BES2300Uart",
54+
"base":0x4000b000,
55+
"type": "peripheral"
56+
},
57+
"UART1": {
58+
"struct": "BES2300Uart",
59+
"base":0x4000c000,
60+
"type": "peripheral"
61+
},
62+
"UART2": {
63+
"struct": "BES2300Uart",
64+
"base":0x4000d000,
65+
"type": "peripheral"
66+
},
67+
"BTPCM": {
68+
"struct": "BES2300Btpcm",
69+
"base":0x4000e000,
70+
"type": "peripheral"
71+
},
72+
"I2S0": {
73+
"struct": "BES2300I2s",
74+
"base":0x4000f000,
75+
"type": "peripheral"
76+
},
77+
"SPDIF0": {
78+
"struct": "BES2300Spdif",
79+
"base":0x40010000,
80+
"type": "peripheral"
81+
},
82+
"SDMMC": {
83+
"struct": "BES2300Sdmmc",
84+
"base":0x40110000,
85+
"type": "peripheral"
86+
},
87+
"I2C_SLAVE": {
88+
"struct": "BES2300I2c",
89+
"base":0x40160000,
90+
"type": "peripheral"
91+
},
92+
"USB": {
93+
"struct": "BES2300Usb",
94+
"base":0x40180000,
95+
"type": "peripheral"
96+
},
97+
"CODEC": {
98+
"struct": "BES2300Codec",
99+
"base":0x40300000,
100+
"type": "peripheral"
101+
},
102+
"IOMUX": {
103+
"struct": "BES2300Iomux",
104+
"base":0x40086000,
105+
"type": "peripheral"
106+
},
107+
"GPIO": {
108+
"struct": "BES2300Gpio",
109+
"base":0x40081000,
110+
"type": "peripheral"
111+
},
112+
"PWM": {
113+
"struct": "BES2300Pwm",
114+
"base":0x40083000,
115+
"type": "peripheral"
116+
},
117+
"TIMER0": {
118+
"struct": "BES2300Timer",
119+
"base":0x40002000,
120+
"type": "peripheral"
121+
},
122+
"TIMER1": {
123+
"struct": "BES2300Timer",
124+
"base":0x40003000,
125+
"type": "peripheral"
126+
}
127+
}

qiling/extensions/mcu/stm32f4/stm32f401.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,20 @@
5757
"struct": "STM32F4xxExti",
5858
"type": "peripheral"
5959
},
60+
"CODE": {
61+
"base": 0x08000000,
62+
"size": 0x10000,
63+
"alias": 0x0,
64+
"type": "remap"
65+
},
66+
"CODE": {
67+
"base": 0x08000000,
68+
"size": 0x80000,
69+
"alias": 0x0,
70+
"type": "remap"
71+
},
6072
"FLASH": {
61-
"base": 0x8000000,
73+
"base": 0x08000000,
6274
"size": 0x80000,
6375
"type": "memory"
6476
},

qiling/extensions/mcu/stm32f4/stm32f405.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@
9494
"struct": "STM32F4xxExti",
9595
"type": "peripheral"
9696
},
97+
"CODE": {
98+
"base": 0x08000000,
99+
"size": 0x100000,
100+
"alias": 0x0,
101+
"type": "remap"
102+
},
97103
"FLASH": {
98-
"base": 0x8000000,
104+
"base": 0x08000000,
99105
"size": 0x100000,
100106
"type": "memory"
101107
},

qiling/extensions/mcu/stm32f4/stm32f407.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,14 @@
111111
"struct": "STM32F4xxExti",
112112
"type": "peripheral"
113113
},
114+
"CODE": {
115+
"base": 0x08000000,
116+
"size": 0x100000,
117+
"alias": 0x0,
118+
"type": "remap"
119+
},
114120
"FLASH": {
115-
"base": 0x8000000,
121+
"base": 0x08000000,
116122
"size": 0x100000,
117123
"type": "memory"
118124
},

0 commit comments

Comments
 (0)