Debian Codegraph @

test/unit/test_rom_table.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2019-2020 Arm Limited
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import pytest
18 import logging
19 from intervaltree import (Interval, IntervalTree)
20
21 from .conftest import mock
22
23 from pyocd.coresight.component import CoreSightCoreComponent
24 from pyocd.core import memory_map
25 from pyocd.coresight.rom_table import (
26 CoreSightComponentID,
27 ROMTable,
28 )
29 from pyocd.core.memory_interface import MemoryInterface
30 from pyocd.debug.cache import MemoryCache
31 from pyocd.debug.context import DebugContext
32
33 class MockCoreForMemCache(CoreSightCoreComponent):
34 """@brief Just enough of a core to satisfy MemoryCache.
35
36 Most importantly, it defines a memory map with a single RAM region covering almost the
37 full 4 GB address space.
38 """
39 def __init__(self):
40 self.run_token = 1
41 self.memory_map = memory_map.MemoryMap(
42 memory_map.RamRegion(name="ram", start=0, length=0xf0000000, cacheable=True)
43 )
44
45 def is_running(self):
46 return False
47
48 MockDebugContext = mock.Mock(spec=DebugContext)
49
50 class RomMemory(MemoryCache, MemoryInterface):
51 """@brief Memory interface for reading constant values.
52
53 Uses the memory cache as readily-available component to store data at fixed addresses. We
54 just have to make sure the cache is never invalidated.
55 """
56 def __init__(self, ranges):
57 """@brief Constructor.
58
59 @param self
60 @param ranges Dict of start address -> list of word values.
61 """
62 super(RomMemory, self).__init__(MockDebugContext(), MockCoreForMemCache())
63
64 # Fill in cache with data from ranges.
65 for addr, data in ranges.items():
66 self.write_memory_block32(addr, data)
67
68 def _check_cache(self):
69 # Ensure the cache is always valid.
70 pass
71
72 class MockCoreSight(RomMemory):
73 """@brief RomMemory based on a list of MockCoreSightComponent objects."""
74 def __init__(self, components):
75 """@brief Constructor.
76
77 @param self
78 @param components List of component dicts, where each component dict consists of start
79 address -> list of word values.
80 """
81 ranges = {base: data for c in components for base, data in c.data.items()}
82 super(MockCoreSight, self).__init__(ranges)
83
84 @property
85 def short_description(self):
86 return "MockCoreSight"
87
88 class MockCoreSightComponent(object):
89 """@brief Generates a data dict from CoreSight component ID register values."""
90
91 # Start offset within the 4 kB CoreSight component memory window of the ID registers
92 # we care about, particularly those read by CoreSightComponentID.
93 CMPID_REGS_OFFSET = 0xfbc
94
95 def __init__(self, base, cidr, pidr, **kwargs):
96 """@brief Constructor.
97 @param self
98 @param base Base address of the component.
99 @param cidr 32-bit combined CIDR register value.
100 @param pidr 64-bit combined PIDR register value.
101 @param kwargs Optional 'devarch', 'devtype', 'devid', and 'extra' keyword arguments. The
102 first two are simple integers. 'devid' must be a list of 3 integers. 'extra' is a
103 data dictionary (address -> list of 32-bit word values) for any extra fixed data that
104 is contained in the component's 4 kB address space (such as a ROM table).
105 """
106 self._base = base
107 self._pidr4_7 = (pidr >> 32)
108 self._pidr0_3 = pidr & 0xffffffff
109 self._cidr = cidr
110 self._devarch = kwargs.get('devarch', 0)
111 self._devid = kwargs.get('devid', [0, 0, 0])
112 self._devtype = kwargs.get('devtype', 0)
113 self._extra = kwargs.get('extra', {})
114
115 @property
116 def data(self):
117 d = self._extra.copy()
118 d.update({
119 self._base + self.CMPID_REGS_OFFSET: [
120 self._devarch, # 0xfbc = DEVARCH
121 self._devid[2], # 0xfc0 = DEVID2
122 self._devid[1], # 0xfc4 = DEVID1
123 self._devid[0], # 0xfc8 = DEVID0
124 self._devtype, # 0xfcc = DEVTYPE
125 self._pidr4_7 & 0xff, # 0xfd0 = PID4
126 (self._pidr4_7 >> 8) & 0xff, # 0xfd4 = PID5
127 (self._pidr4_7 >> 16) & 0xff, # 0xfd8 = PID6
128 (self._pidr4_7 >> 24) & 0xff, # 0xfdc = PID7
129 self._pidr0_3 & 0xff, # 0xfe0 = PID0
130 (self._pidr0_3 >> 8) & 0xff, # 0xfe4 = PID1
131 (self._pidr0_3 >> 16) & 0xff, # 0xfe8 = PID3
132 (self._pidr0_3 >> 24) & 0xff, # 0xfec = PID3
133 self._cidr & 0xff, # 0xff0 = CID0
134 (self._cidr >> 8) & 0xff, # 0xff4 = CID1
135 (self._cidr >> 16) & 0xff, # 0xff8 = CID2
136 (self._cidr >> 24) & 0xff, # 0xffc = CID3
137 ]
138 })
139 return d
140
141 class MockM4Components:
142 """@ brief Namespace for mock Cortex-M4 Class 0x1 ROM table and core complex components."""
143
144 # ROM table #0 @ 0xe00ff000 (designer=244 part=00d)
145 M4_ROM_TABLE_BASE = 0xe00ff000
146 M4_ROM_TABLE = MockCoreSightComponent(M4_ROM_TABLE_BASE, cidr=0xb105100d, pidr=0x4000bb4c4,
147 extra={
148 # ROM table entries.
149 0xe00ff000: [ 0xfff0f003, # SCS
150 0xfff02003, # DWT
151 0xfff03003, # FPB
152 0xfff01003, # ITM
153 0xfff41003, # TPIU
154 0xfff42003, # ETM
155 0x00000000, 0x00000000, 0x00000000, 0x00000000, # (Terminator and extra)
156 ],
157 })
158
159 # [0]<e000e000:SCS-M4 class=14 designer=43b part=00c>
160 SCS_BASE = 0xe000e000
161 SCS = MockCoreSightComponent(SCS_BASE, cidr=0xb105e00d, pidr=0x4000bb00c)
162
163 # [1]<e0001000:DWT class=14 designer=43b part=002>
164 DWT_BASE = 0xe0001000
165 DWT = MockCoreSightComponent(DWT_BASE, cidr=0xb105e00d, pidr=0x4003bb002)
166
167 # [2]<e0002000:FPB class=14 designer=43b part=003>
168 FPB_BASE = 0xe0002000
169 FPB = MockCoreSightComponent(FPB_BASE, cidr=0xb105e00d, pidr=0x4002bb003)
170
171 # [3]<e0000000:ITM class=14 designer=43b part=001>
172 ITM_BASE = 0xe0000000
173 ITM = MockCoreSightComponent(ITM_BASE, cidr=0xb105e00d, pidr=0x4003bb001)
174
175 # [4]<e0040000:TPIU-M4 class=9 designer=43b part=9a1 devtype=11 archid=0000 devid=ca1:0:0>
176 TPIU_BASE = 0xe0040000
177 TPIU = MockCoreSightComponent(TPIU_BASE, cidr=0xb105900d, pidr=0x4000bb9a1, devtype=0x11, devid=[0xca1, 0, 0])
178
179 # [5]<e0041000:ETM-M4 class=9 designer=43b part=925 devtype=13 archid=0000 devid=0:0:0>
180 ETM_BASE = 0xe0041000
181 ETM = MockCoreSightComponent(ETM_BASE, cidr=0xb105900d, pidr=0x4000bb925, devtype=0x13)
182
183 class MockCSSOC600Components:
184 """@ brief Namespace for mock Class 0x9 ROM table and CoreSight SoC-600 components."""
185
186 C9_ROM_TABLE_BASE = 0x00000000
187 C9_ROM_TABLE = MockCoreSightComponent(C9_ROM_TABLE_BASE, cidr=0xb105900d, pidr=0x4000bb7d5,
188 devarch=0x47700af7, devid=[0x20, 0, 0],
189 extra={
190 # ROM table entries.
191 0x00000000: [ 0x00001003, # SDC-600
192 0x00002003, # SoC-600 AHB-AP
193 0x00000000, 0x00000000, 0x00000000, 0x00000000, # (Terminator and extra)
194 0x00000000, 0x00000000, 0x00000000, 0x00000000, # (extra)
195 ],
196 })
197
198 SDC600_BASE = 0x00001000
199 SDC600 = MockCoreSightComponent(SDC600_BASE, cidr=0xb105900d, pidr=0x4000bb9ef, devarch=0x47700a57)
200
201 C9_AHB_AP_BASE = 0x00002000
202 C9_AHB_AP = MockCoreSightComponent(C9_AHB_AP_BASE, cidr=0xb105900d, pidr=0x4002bb9e3, devarch=0x47700a17)
203
204
205 # Complete set of components for a Cortex-M4 subsystem.
206 @pytest.fixture(scope='function')
207 def m4_rom():
208 return MockCoreSight([
209 MockM4Components.M4_ROM_TABLE,
210 MockM4Components.SCS,
211 MockM4Components.DWT,
212 MockM4Components.FPB,
213 MockM4Components.ITM,
214 MockM4Components.TPIU,
215 MockM4Components.ETM,
216 ])
217
218 @pytest.fixture(scope='function')
219 def c9_top_rom():
220 return MockCoreSight([
221 MockCSSOC600Components.C9_ROM_TABLE,
222 MockCSSOC600Components.SDC600,
223 MockCSSOC600Components.C9_AHB_AP,
224 ])
225
226 @pytest.fixture(scope='function')
227 def testrom():
228 a = {
229 0x1000: [1, 2, 3, 4],
230 0x4000: [0xaa, 0xbeef],
231 0x4400: [0xcccccccc],
232 }
233 return RomMemory(a)
234
235 @pytest.fixture(scope='function')
236 def testcoresight():
237 return MockCoreSight([MockM4Components.M4_ROM_TABLE, MockM4Components.ETM])
238
239 class TestRomMemory:
240 def test_r32(self, testrom):
241 assert testrom.read32(0x1000) == 1
242 assert testrom.read32(0x1004) == 2
243 assert testrom.read32(0x4004) == 0xbeef
244 assert testrom.read32(0x4400) == 0xcccccccc
245
246 def test_r16(self, testrom):
247 assert testrom.read16(0x1000) == 1
248 assert testrom.read16(0x1002) == 0
249 assert testrom.read16(0x4000) == 0xaa
250 assert testrom.read16(0x4004) == 0xbeef
251 assert testrom.read16(0x4402) == 0xcccc
252
253 def test_r8(self, testrom):
254 assert testrom.read8(0x1001) == 0
255 assert testrom.read8(0x1003) == 0
256 assert testrom.read8(0x1004) == 2
257 assert testrom.read8(0x4001) == 0
258 assert testrom.read8(0x4004) == 0xef
259 assert testrom.read8(0x4005) == 0xbe
260 assert testrom.read8(0x4401) == 0xcc
261
262 def test_rb32(self, testrom):
263 assert testrom.read_memory_block32(0x1000, 4) == [1, 2, 3, 4]
264 assert testrom.read_memory_block32(0x4000, 2) == [0xaa, 0xbeef]
265
266 def test_rb8(self, testrom):
267 assert testrom.read_memory_block8(0x1008, 6) == [3, 0, 0, 0, 4, 0]
268 assert testrom.read_memory_block8(0x4001, 6) == [0, 0, 0, 0xef, 0xbe, 00]
269
270 class TestMockCoreSight:
271 def test_1(self, testcoresight):
272 assert testcoresight.read32(0xe00ff000) == 0xfff0f003
273 assert testcoresight.read32(0xe00ff000) == 0xfff0f003
274 assert testcoresight.read_memory_block32(0xe00ffff0, 4) == [0xd, 0x10, 0x5, 0xb1]
275
276 class TestCoreSightComponentID:
277 # Test parsing a non-CoreSight component in isolation.
278 def test_scs(self):
279 cmp = CoreSightComponentID(None, MockCoreSight([MockM4Components.SCS]),
280 MockM4Components.SCS_BASE)
281 cmp.read_id_registers()
282 assert cmp.component_class == 14
283 assert cmp.designer == 0x43b
284 assert cmp.part == 0xc
285 assert cmp.devarch == 0
286 assert cmp.devid == [0, 0, 0]
287
288 # Test parsing a CoreSight (class 9) component in isolation.
289 def test_etm(self):
290 cmp = CoreSightComponentID(None, MockCoreSight([MockM4Components.ETM]),
291 MockM4Components.ETM_BASE)
292 cmp.read_id_registers()
293 assert cmp.component_class == 9
294 assert cmp.designer == 0x43b
295 assert cmp.part == 0x925
296 assert cmp.devtype == 0x13
297 assert cmp.archid == 0
298 assert cmp.devarch == 0
299 assert cmp.devid == [0, 0, 0]
300
301 # Test parsing a CoreSight (class 9) component with a DEVID.
302 def test_tpiu(self):
303 cmp = CoreSightComponentID(None, MockCoreSight([MockM4Components.TPIU]),
304 MockM4Components.TPIU_BASE)
305 cmp.read_id_registers()
306 assert cmp.component_class == 9
307 assert cmp.designer == 0x43b
308 assert cmp.part == 0x9a1
309 assert cmp.devtype == 0x11
310 assert cmp.archid == 0
311 assert cmp.devarch == 0
312 assert cmp.devid == [0xca1, 0, 0]
313
314 # Test parsing a Class 0x9 ROM table.
315 def test_c9_rom(self):
316 cmp = CoreSightComponentID(None, MockCoreSight([MockCSSOC600Components.C9_ROM_TABLE]),
317 MockCSSOC600Components.C9_ROM_TABLE_BASE)
318 cmp.read_id_registers()
319 assert cmp.component_class == 9
320 assert cmp.is_rom_table
321
322 class TestRomTable:
323 # Test a Class 0x1 ROM table.
324 def test_m4_rom(self, m4_rom):
325 # Read ROM table component ID.
326 cmpid = CoreSightComponentID(None, m4_rom, MockM4Components.M4_ROM_TABLE_BASE)
327 cmpid.read_id_registers()
328
329 # Create the ROM table.
330 rom_table = ROMTable.create(m4_rom, cmpid)
331 rom_table.init()
332
333 # Verify all components were parsed.
334 assert len(rom_table.components) == 6
335
336 # Check SCS-M4.
337 scs = rom_table.components[0]
338 assert scs.component_class == 14
339 assert scs.designer == 0x43b
340 assert scs.part == 0xc
341
342 # Check TPIU.
343 tpiu = rom_table.components[4]
344 assert tpiu.component_class == 9
345 assert tpiu.part == 0x9a1
346 assert tpiu.devid == [0xca1, 0, 0]
347
348 # Test a Class 0x9 ROM table and CS-600 components.
349 def test_c9_rom(self, c9_top_rom):
350 # Read ROM table component ID.
351 cmpid = CoreSightComponentID(None, c9_top_rom, MockCSSOC600Components.C9_ROM_TABLE_BASE)
352 cmpid.read_id_registers()
353
354 # Create the ROM table.
355 rom_table = ROMTable.create(c9_top_rom, cmpid)
356 rom_table.init()
357
358 # Validate ROM table properties.
359 assert rom_table._width == 32
360 assert not rom_table.has_com_port
361 assert rom_table.has_prr
362 assert not rom_table.is_sysmem
363
364 # Validate components.
365 assert len(rom_table.components) == 2
366
367 # Validate SDC-600.
368 sdc = rom_table.components[0]
369 assert sdc.component_class == 9
370 assert sdc.designer == 0x43b
371 assert sdc.part == 0x9ef
372 assert sdc.archid == 0xa57
373
374 # Validate AHB-AP.
375 ahb = rom_table.components[1]
376 assert ahb.component_class == 9
377 assert ahb.designer == 0x43b
378 assert ahb.part == 0x9e3
379 assert ahb.archid == 0xa17
380