55
66from functools import cached_property
77from contextlib import ContextDecorator
8+ from typing import Optional
89
910from unicorn import UC_ARCH_ARM , UC_MODE_ARM , UC_MODE_MCLASS , UC_MODE_THUMB
1011from capstone import Cs , CS_ARCH_ARM , CS_MODE_ARM , CS_MODE_MCLASS , CS_MODE_THUMB
1314from qiling import Qiling
1415from qiling .arch .arm import QlArchARM
1516from qiling .arch import cortex_m_const
17+ from qiling .arch .models import ARM_CPU_MODEL
1618from qiling .arch .register import QlRegisterManager
1719from qiling .arch .cortex_m_const import IRQ , EXC_RETURN , CONTROL , EXCP
1820from qiling .const import QL_ARCH , QL_ENDIAN , QL_VERBOSE
@@ -66,12 +68,14 @@ class QlArchCORTEX_M(QlArchARM):
6668 type = QL_ARCH .CORTEX_M
6769 bits = 32
6870
69- def __init__ (self , ql : Qiling ):
70- super ().__init__ (ql , endian = QL_ENDIAN .EL , thumb = True )
71+ def __init__ (self , ql : Qiling , * , cputype : Optional [ ARM_CPU_MODEL ] = None ):
72+ super ().__init__ (ql , cputype = cputype , endian = QL_ENDIAN .EL , thumb = True )
7173
7274 @cached_property
7375 def uc (self ):
74- return MultiTaskUnicorn (UC_ARCH_ARM , UC_MODE_ARM + UC_MODE_MCLASS + UC_MODE_THUMB , 10 )
76+ cpu = self .cpu and self .cpu .value
77+
78+ return MultiTaskUnicorn (UC_ARCH_ARM , UC_MODE_ARM + UC_MODE_MCLASS + UC_MODE_THUMB , cpu , 10 )
7579
7680 @cached_property
7781 def regs (self ) -> QlRegisterManager :
@@ -97,18 +101,18 @@ def is_thumb(self) -> bool:
97101 def endian (self ) -> QL_ENDIAN :
98102 return QL_ENDIAN .EL
99103
100- def is_handler_mode (self ):
104+ def is_handler_mode (self ) -> bool :
101105 return self .regs .ipsr > 1
102106
103- def using_psp (self ):
107+ def using_psp (self ) -> bool :
104108 return not self .is_handler_mode () and (self .regs .control & CONTROL .SPSEL ) > 0
105109
106- def init_context (self ):
110+ def init_context (self ) -> None :
107111 self .regs .lr = 0xffffffff
108112 self .regs .msp = self .ql .mem .read_ptr (0x0 )
109113 self .regs .pc = self .ql .mem .read_ptr (0x4 )
110114
111- def unicorn_exception_handler (self , ql , intno ):
115+ def unicorn_exception_handler (self , ql : Qiling , intno : int ):
112116 forward_mapper = {
113117 EXCP .UDEF : IRQ .HARD_FAULT , # undefined instruction
114118 EXCP .SWI : IRQ .SVCALL , # software interrupt
@@ -139,8 +143,9 @@ def unicorn_exception_handler(self, ql, intno):
139143 except IndexError :
140144 raise QlErrorNotImplemented (f'Unhandled interrupt number ({ intno } )' )
141145
142- def interrupt_handler (self , ql , intno ):
146+ def interrupt_handler (self , ql : Qiling , intno : int ):
143147 basepri = self .regs .basepri & 0xf0
148+
144149 if basepri and basepri <= ql .hw .nvic .get_priority (intno ):
145150 return
146151
0 commit comments