|
3 | 3 | # Cross Platform and Multi Architecture Advanced Binary Emulation Framework |
4 | 4 | # |
5 | 5 |
|
6 | | -from typing import Mapping |
| 6 | +from typing import Dict, Optional |
7 | 7 |
|
8 | 8 | from .arch import Arch |
9 | 9 |
|
| 10 | + |
10 | 11 | class ArchARM(Arch): |
11 | | - def __init__(self): |
12 | | - super().__init__() |
13 | | - self._regs = ( |
14 | | - "r0", "r1", "r2", "r3", |
15 | | - "r4", "r5", "r6", "r7", |
16 | | - "r8", "r9", "r10", "r11", |
17 | | - "r12", "sp", "lr", "pc", |
18 | | - ) |
| 12 | + def __init__(self) -> None: |
| 13 | + regs = ( |
| 14 | + 'r0', 'r1', 'r2', 'r3', |
| 15 | + 'r4', 'r5', 'r6', 'r7', |
| 16 | + 'r8', 'r9', 'r10', 'r11', |
| 17 | + 'r12', 'sp', 'lr', 'pc' |
| 18 | + ) |
| 19 | + |
| 20 | + aliases = { |
| 21 | + 'r9' : 'sb', |
| 22 | + 'r10': 'sl', |
| 23 | + 'r12': 'ip', |
| 24 | + 'r11': 'fp' |
| 25 | + } |
| 26 | + |
| 27 | + asize = 4 |
| 28 | + isize = 4 |
| 29 | + |
| 30 | + super().__init__(regs, aliases, asize, isize) |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def get_flags(bits: int) -> Dict[str, bool]: |
| 34 | + return { |
| 35 | + 'thumb': bits & (0b1 << 5) != 0, |
| 36 | + 'fiq': bits & (0b1 << 6) != 0, |
| 37 | + 'irq': bits & (0b1 << 7) != 0, |
| 38 | + 'overflow': bits & (0b1 << 28) != 0, |
| 39 | + 'carry': bits & (0b1 << 29) != 0, |
| 40 | + 'zero': bits & (0b1 << 30) != 0, |
| 41 | + 'neg': bits & (0b1 << 31) != 0 |
| 42 | + } |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def get_mode(bits: int) -> str: |
| 46 | + modes = { |
| 47 | + 0b10000: 'User', |
| 48 | + 0b10001: 'FIQ', |
| 49 | + 0b10010: 'IRQ', |
| 50 | + 0b10011: 'Supervisor', |
| 51 | + 0b10110: 'Monitor', |
| 52 | + 0b10111: 'Abort', |
| 53 | + 0b11010: 'Hypervisor', |
| 54 | + 0b11011: 'Undefined', |
| 55 | + 0b11111: 'System' |
| 56 | + } |
| 57 | + |
| 58 | + return modes.get(bits & 0b11111, '?') |
19 | 59 |
|
20 | 60 | @property |
21 | | - def regs(self): |
22 | | - return self._regs |
| 61 | + def is_thumb(self) -> bool: |
| 62 | + """Query whether the processor is currently in thumb mode. |
| 63 | + """ |
23 | 64 |
|
24 | | - @regs.setter |
25 | | - def regs(self, regs): |
26 | | - self._regs += regs |
| 65 | + return self.ql.arch.is_thumb |
27 | 66 |
|
28 | 67 | @property |
29 | | - def regs_need_swapped(self): |
30 | | - return { |
31 | | - "sl": "r10", |
32 | | - "ip": "r12", |
33 | | - "fp": "r11", |
34 | | - } |
| 68 | + def isize(self) -> int: |
| 69 | + return 2 if self.is_thumb else self._isize |
35 | 70 |
|
36 | 71 | @staticmethod |
37 | | - def get_flags(bits: int) -> Mapping[str, bool]: |
38 | | - """ |
39 | | - get flags for ARM |
| 72 | + def __is_wide_insn(data: bytes) -> bool: |
| 73 | + """Determine whether a sequence of bytes respresents a wide thumb instruction. |
40 | 74 | """ |
41 | 75 |
|
42 | | - def get_mode(bits: int) -> int: |
43 | | - """ |
44 | | - get operating mode for ARM |
45 | | - """ |
46 | | - return { |
47 | | - 0b10000: "User", |
48 | | - 0b10001: "FIQ", |
49 | | - 0b10010: "IRQ", |
50 | | - 0b10011: "Supervisor", |
51 | | - 0b10110: "Monitor", |
52 | | - 0b10111: "Abort", |
53 | | - 0b11010: "Hypervisor", |
54 | | - 0b11011: "Undefined", |
55 | | - 0b11111: "System", |
56 | | - }.get(bits & 0x00001f) |
| 76 | + assert len(data) in (2, 4), f'unexpected instruction length: {len(data)}' |
57 | 77 |
|
58 | | - return { |
59 | | - "mode": get_mode(bits), |
60 | | - "thumb": bits & 0x00000020 != 0, |
61 | | - "fiq": bits & 0x00000040 != 0, |
62 | | - "irq": bits & 0x00000080 != 0, |
63 | | - "neg": bits & 0x80000000 != 0, |
64 | | - "zero": bits & 0x40000000 != 0, |
65 | | - "carry": bits & 0x20000000 != 0, |
66 | | - "overflow": bits & 0x10000000 != 0, |
67 | | - } |
| 78 | + # determine whether this is a wide instruction by inspecting the 5 most |
| 79 | + # significant bits in the first half-word |
| 80 | + return (data[1] >> 3) & 0b11111 in (0b11101, 0b11110, 0b11111) |
68 | 81 |
|
69 | | - @property |
70 | | - def thumb_mode(self) -> bool: |
71 | | - """ |
72 | | - helper function for checking thumb mode |
| 82 | + def __read_thumb_insn_fail(self, address: int) -> Optional[bytearray]: |
| 83 | + """A failsafe method for reading thumb instructions. This method is needed for |
| 84 | + rare cases in which a narrow instruction is on a page boundary where the next |
| 85 | + page is unavailable. |
73 | 86 | """ |
74 | 87 |
|
75 | | - return self.ql.arch.is_thumb |
| 88 | + lo_half = self.try_read_mem(address, 2) |
76 | 89 |
|
| 90 | + if lo_half is None: |
| 91 | + return None |
77 | 92 |
|
78 | | - def read_insn(self, address: int) -> bytes: |
79 | | - """ |
80 | | - read instruction depending on current operating mode |
| 93 | + data = lo_half |
| 94 | + |
| 95 | + if ArchARM.__is_wide_insn(data): |
| 96 | + hi_half = self.try_read_mem(address + 2, 2) |
| 97 | + |
| 98 | + # fail if higher half-word was required but could not be read |
| 99 | + if hi_half is None: |
| 100 | + return None |
| 101 | + |
| 102 | + data.extend(hi_half) |
| 103 | + |
| 104 | + return data |
| 105 | + |
| 106 | + def __read_thumb_insn(self, address: int) -> Optional[bytearray]: |
| 107 | + """Read one instruction in thumb mode. |
| 108 | +
|
| 109 | + Thumb instructions may be either 2 or 4 bytes long, depending on encoding of |
| 110 | + the first word. However, reading two chunks of two bytes each is slower. For |
| 111 | + most cases reading all four bytes in advance will be safe and quicker. |
81 | 112 | """ |
82 | 113 |
|
83 | | - def thumb_read(address: int) -> bytes: |
| 114 | + data = self.try_read_mem(address, 4) |
84 | 115 |
|
85 | | - first_two = self.ql.mem.read_ptr(address, 2) |
86 | | - result = self.ql.pack16(first_two) |
| 116 | + if data is None: |
| 117 | + # there is a slight chance we could not read 4 bytes because only 2 |
| 118 | + # are available. try the failsafe method to find out |
| 119 | + return self.__read_thumb_insn_fail(address) |
87 | 120 |
|
88 | | - # to judge it's thumb mode or not |
89 | | - if any([ |
90 | | - first_two & 0xf000 == 0xf000, |
91 | | - first_two & 0xf800 == 0xf800, |
92 | | - first_two & 0xe800 == 0xe800, |
93 | | - ]): |
| 121 | + if ArchARM.__is_wide_insn(data): |
| 122 | + return data |
94 | 123 |
|
95 | | - latter_two = self.ql.mem.read_ptr(address+2, 2) |
96 | | - result += self.ql.pack16(latter_two) |
| 124 | + return data[:2] |
97 | 125 |
|
98 | | - return result |
| 126 | + def read_insn(self, address: int) -> Optional[bytearray]: |
| 127 | + """Read one instruction worth of bytes. |
| 128 | + """ |
99 | 129 |
|
100 | | - return super().read_insn(address) if not self.thumb_mode else thumb_read(address) |
| 130 | + if self.is_thumb: |
| 131 | + return self.__read_thumb_insn(address) |
101 | 132 |
|
| 133 | + return super().read_insn(address) |
102 | 134 |
|
103 | 135 |
|
104 | 136 | class ArchCORTEX_M(ArchARM): |
105 | 137 | def __init__(self): |
106 | 138 | super().__init__() |
107 | | - self.regs += ("xpsr", "control", "primask", "basepri", "faultmask") |
| 139 | + |
| 140 | + self._regs += ( |
| 141 | + 'xpsr', 'control', 'primask', |
| 142 | + 'basepri', 'faultmask' |
| 143 | + ) |
0 commit comments