radixmmu: fix segment_check function and its caller
[soc.git] / src / soc / decoder / isa / radixmmu.py
1 # SPDX-License-Identifier: LGPLv3+
2 # Copyright (C) 2020, 2021 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
3 # Copyright (C) 2021 Tobias Platen
4 # Funded by NLnet http://nlnet.nl
5 """core of the python-based POWER9 simulator
6
7 this is part of a cycle-accurate POWER9 simulator. its primary purpose is
8 not speed, it is for both learning and educational purposes, as well as
9 a method of verifying the HDL.
10
11 related bugs:
12
13 * https://bugs.libre-soc.org/show_bug.cgi?id=604
14 """
15
16 from nmigen.back.pysim import Settle
17 from copy import copy
18 from soc.decoder.selectable_int import (FieldSelectableInt, SelectableInt,
19 selectconcat)
20 from soc.decoder.helpers import exts, gtu, ltu, undefined
21 from soc.decoder.isa.mem import Mem
22 from soc.consts import MSRb # big-endian (PowerISA versions)
23
24 import math
25 import sys
26 import unittest
27
28 # very quick, TODO move to SelectableInt utils later
29 def genmask(shift, size):
30 res = SelectableInt(0, size)
31 for i in range(size):
32 if i < shift:
33 res[size-1-i] = SelectableInt(1, 1)
34 return res
35
36 # NOTE: POWER 3.0B annotation order! see p4 1.3.2
37 # MSB is indexed **LOWEST** (sigh)
38 # from gem5 radixwalk.hh
39 # Bitfield<63> valid; 64 - (63 + 1) = 0
40 # Bitfield<62> leaf; 64 - (62 + 1) = 1
41
42 def rpte_valid(r):
43 return bool(r[0])
44
45 def rpte_leaf(r):
46 return bool(r[1])
47
48 ## Shift address bits 61--12 right by 0--47 bits and
49 ## supply the least significant 16 bits of the result.
50 def addrshift(addr,shift):
51 x = addr.value >> shift.value
52 return SelectableInt(x,16)
53
54 def NLB(x):
55 """
56 Next Level Base
57 right shifted by 8
58 """
59 return x[4:55]
60
61 def NLS(x):
62 """
63 Next Level Size
64 NLS >= 5
65 """
66 return x[59:63]
67
68 """
69 Get Root Page
70
71 //Accessing 2nd double word of partition table (pate1)
72 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.1
73 // PTCR Layout
74 // ====================================================
75 // -----------------------------------------------
76 // | /// | PATB | /// | PATS |
77 // -----------------------------------------------
78 // 0 4 51 52 58 59 63
79 // PATB[4:51] holds the base address of the Partition Table,
80 // right shifted by 12 bits.
81 // This is because the address of the Partition base is
82 // 4k aligned. Hence, the lower 12bits, which are always
83 // 0 are ommitted from the PTCR.
84 //
85 // Thus, The Partition Table Base is obtained by (PATB << 12)
86 //
87 // PATS represents the partition table size right-shifted by 12 bits.
88 // The minimal size of the partition table is 4k.
89 // Thus partition table size = (1 << PATS + 12).
90 //
91 // Partition Table
92 // ====================================================
93 // 0 PATE0 63 PATE1 127
94 // |----------------------|----------------------|
95 // | | |
96 // |----------------------|----------------------|
97 // | | |
98 // |----------------------|----------------------|
99 // | | | <-- effLPID
100 // |----------------------|----------------------|
101 // .
102 // .
103 // .
104 // |----------------------|----------------------|
105 // | | |
106 // |----------------------|----------------------|
107 //
108 // The effective LPID forms the index into the Partition Table.
109 //
110 // Each entry in the partition table contains 2 double words, PATE0, PATE1,
111 // corresponding to that partition.
112 //
113 // In case of Radix, The structure of PATE0 and PATE1 is as follows.
114 //
115 // PATE0 Layout
116 // -----------------------------------------------
117 // |1|RTS1|/| RPDB | RTS2 | RPDS |
118 // -----------------------------------------------
119 // 0 1 2 3 4 55 56 58 59 63
120 //
121 // HR[0] : For Radix Page table, first bit should be 1.
122 // RTS1[1:2] : Gives one fragment of the Radix treesize
123 // RTS2[56:58] : Gives the second fragment of the Radix Tree size.
124 // RTS = (RTS1 << 3 + RTS2) + 31.
125 //
126 // RPDB[4:55] = Root Page Directory Base.
127 // RPDS = Logarithm of Root Page Directory Size right shifted by 3.
128 // Thus, Root page directory size = 1 << (RPDS + 3).
129 // Note: RPDS >= 5.
130 //
131 // PATE1 Layout
132 // -----------------------------------------------
133 // |///| PRTB | // | PRTS |
134 // -----------------------------------------------
135 // 0 3 4 51 52 58 59 63
136 //
137 // PRTB[4:51] = Process Table Base. This is aligned to size.
138 // PRTS[59: 63] = Process Table Size right shifted by 12.
139 // Minimal size of the process table is 4k.
140 // Process Table Size = (1 << PRTS + 12).
141 // Note: PRTS <= 24.
142 //
143 // Computing the size aligned Process Table Base:
144 // table_base = (PRTB & ~((1 << PRTS) - 1)) << 12
145 // Thus, the lower 12+PRTS bits of table_base will
146 // be zero.
147
148
149 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.2
150 //
151 // Process Table
152 // ==========================
153 // 0 PRTE0 63 PRTE1 127
154 // |----------------------|----------------------|
155 // | | |
156 // |----------------------|----------------------|
157 // | | |
158 // |----------------------|----------------------|
159 // | | | <-- effPID
160 // |----------------------|----------------------|
161 // .
162 // .
163 // .
164 // |----------------------|----------------------|
165 // | | |
166 // |----------------------|----------------------|
167 //
168 // The effective Process id (PID) forms the index into the Process Table.
169 //
170 // Each entry in the partition table contains 2 double words, PRTE0, PRTE1,
171 // corresponding to that process
172 //
173 // In case of Radix, The structure of PRTE0 and PRTE1 is as follows.
174 //
175 // PRTE0 Layout
176 // -----------------------------------------------
177 // |/|RTS1|/| RPDB | RTS2 | RPDS |
178 // -----------------------------------------------
179 // 0 1 2 3 4 55 56 58 59 63
180 //
181 // RTS1[1:2] : Gives one fragment of the Radix treesize
182 // RTS2[56:58] : Gives the second fragment of the Radix Tree size.
183 // RTS = (RTS1 << 3 + RTS2) << 31,
184 // since minimal Radix Tree size is 4G.
185 //
186 // RPDB = Root Page Directory Base.
187 // RPDS = Root Page Directory Size right shifted by 3.
188 // Thus, Root page directory size = RPDS << 3.
189 // Note: RPDS >= 5.
190 //
191 // PRTE1 Layout
192 // -----------------------------------------------
193 // | /// |
194 // -----------------------------------------------
195 // 0 63
196 // All bits are reserved.
197
198
199 """
200
201 testmem = {
202
203 0x10000: # PARTITION_TABLE_2 (not implemented yet)
204 # PATB_GR=1 PRTB=0x1000 PRTS=0xb
205 0x800000000100000b,
206
207 0x30000: # RADIX_ROOT_PTE
208 # V = 1 L = 0 NLB = 0x400 NLS = 9
209 0x8000000000040009,
210 0x40000: # RADIX_SECOND_LEVEL
211 # V = 1 L = 1 SW = 0 RPN = 0
212 # R = 1 C = 1 ATT = 0 EAA 0x7
213 0xc000000000000187,
214
215 0x1000000: # PROCESS_TABLE_3
216 # RTS1 = 0x2 RPDB = 0x300 RTS2 = 0x5 RPDS = 13
217 0x40000000000300ad,
218 }
219
220 # this one has a 2nd level RADIX with a RPN of 0x5000
221 testmem2 = {
222
223 0x10000: # PARTITION_TABLE_2 (not implemented yet)
224 # PATB_GR=1 PRTB=0x1000 PRTS=0xb
225 0x800000000100000b,
226
227 0x30000: # RADIX_ROOT_PTE
228 # V = 1 L = 0 NLB = 0x400 NLS = 9
229 0x8000000000040009,
230 0x40000: # RADIX_SECOND_LEVEL
231 # V = 1 L = 1 SW = 0 RPN = 0x5000
232 # R = 1 C = 1 ATT = 0 EAA 0x7
233 0xc000000005000187,
234
235 0x1000000: # PROCESS_TABLE_3
236 # RTS1 = 0x2 RPDB = 0x300 RTS2 = 0x5 RPDS = 13
237 0x40000000000300ad,
238 }
239
240
241 testresult = """
242 prtbl = 1000000
243 DCACHE GET 1000000 PROCESS_TABLE_3
244 DCACHE GET 30000 RADIX_ROOT_PTE V = 1 L = 0
245 DCACHE GET 40000 RADIX_SECOND_LEVEL V = 1 L = 1
246 DCACHE GET 10000 PARTITION_TABLE_2
247 translated done 1 err 0 badtree 0 addr 40000 pte 0
248 """
249
250 # see qemu/target/ppc/mmu-radix64.c for reference
251 class RADIX:
252 def __init__(self, mem, caller):
253 self.mem = mem
254 self.caller = caller
255 if caller is not None:
256 self.dsisr = self.caller.spr["DSISR"]
257 self.dar = self.caller.spr["DAR"]
258 self.pidr = self.caller.spr["PIDR"]
259 self.prtbl = self.caller.spr["PRTBL"]
260 self.msr = self.caller.msr
261
262 # cached page table stuff
263 self.pgtbl0 = 0
264 self.pt0_valid = False
265 self.pgtbl3 = 0
266 self.pt3_valid = False
267
268 def __call__(self, addr, sz):
269 val = self.ld(addr.value, sz, swap=False)
270 print("RADIX memread", addr, sz, val)
271 return SelectableInt(val, sz*8)
272
273 def ld(self, address, width=8, swap=True, check_in_mem=False,
274 instr_fetch=False):
275 print("RADIX: ld from addr 0x%x width %d" % (address, width))
276
277 priv = ~(self.msr(MSR_PR).value) # problem-state ==> privileged
278 if instr_fetch:
279 mode = 'EXECUTE'
280 else:
281 mode = 'LOAD'
282 addr = SelectableInt(address, 64)
283 (shift, mbits, pgbase) = self._decode_prte(addr)
284 #shift = SelectableInt(0, 32)
285
286 pte = self._walk_tree(addr, pgbase, mode, mbits, shift, priv)
287
288 # use pte to load from phys address
289 return self.mem.ld(pte.value, width, swap, check_in_mem)
290
291 # XXX set SPRs on error
292
293 # TODO implement
294 def st(self, address, v, width=8, swap=True):
295 print("RADIX: st to addr 0x%x width %d data %x" % (address, width, v))
296
297 priv = ~(self.msr(MSR_PR).value) # problem-state ==> privileged
298 mode = 'STORE'
299 addr = SelectableInt(address, 64)
300 (shift, mbits, pgbase) = self._decode_prte(addr)
301 pte = self._walk_tree(addr, pgbase, mode, mbits, shift, priv)
302
303 # use pte to store at phys address
304 return self.mem.st(pte.value, v, width, swap)
305
306 # XXX set SPRs on error
307
308 def memassign(self, addr, sz, val):
309 print("memassign", addr, sz, val)
310 self.st(addr.value, val.value, sz, swap=False)
311
312 def _next_level(self, addr, entry_width, swap, check_in_mem):
313 # implement read access to mmu mem here
314
315 # DO NOT perform byte-swapping: load 8 bytes (that's the entry size)
316 value = self.mem.ld(addr.value, 8, False, check_in_mem)
317 assert(value is not None, "address lookup %x not found" % addr.value)
318
319 print("addr", hex(addr.value))
320 data = SelectableInt(value, 64) # convert to SelectableInt
321 print("value", hex(value))
322 # index += 1
323 return data;
324
325 def _walk_tree(self, addr, pgbase, mode, mbits, shift, priv=1):
326 """walk tree
327
328 // vaddr 64 Bit
329 // vaddr |-----------------------------------------------------|
330 // | Unused | Used |
331 // |-----------|-----------------------------------------|
332 // | 0000000 | usefulBits = X bits (typically 52) |
333 // |-----------|-----------------------------------------|
334 // | |<--Cursize---->| |
335 // | | Index | |
336 // | | into Page | |
337 // | | Directory | |
338 // |-----------------------------------------------------|
339 // | |
340 // V |
341 // PDE |---------------------------| |
342 // |V|L|//| NLB |///|NLS| |
343 // |---------------------------| |
344 // PDE = Page Directory Entry |
345 // [0] = V = Valid Bit |
346 // [1] = L = Leaf bit. If 0, then |
347 // [4:55] = NLB = Next Level Base |
348 // right shifted by 8 |
349 // [59:63] = NLS = Next Level Size |
350 // | NLS >= 5 |
351 // | V
352 // | |--------------------------|
353 // | | usfulBits = X-Cursize |
354 // | |--------------------------|
355 // |---------------------><--NLS-->| |
356 // | Index | |
357 // | into | |
358 // | PDE | |
359 // |--------------------------|
360 // |
361 // If the next PDE obtained by |
362 // (NLB << 8 + 8 * index) is a |
363 // nonleaf, then repeat the above. |
364 // |
365 // If the next PDE is a leaf, |
366 // then Leaf PDE structure is as |
367 // follows |
368 // |
369 // |
370 // Leaf PDE |
371 // |------------------------------| |----------------|
372 // |V|L|sw|//|RPN|sw|R|C|/|ATT|EAA| | usefulBits |
373 // |------------------------------| |----------------|
374 // [0] = V = Valid Bit |
375 // [1] = L = Leaf Bit = 1 if leaf |
376 // PDE |
377 // [2] = Sw = Sw bit 0. |
378 // [7:51] = RPN = Real Page Number, V
379 // real_page = RPN << 12 -------------> Logical OR
380 // [52:54] = Sw Bits 1:3 |
381 // [55] = R = Reference |
382 // [56] = C = Change V
383 // [58:59] = Att = Physical Address
384 // 0b00 = Normal Memory
385 // 0b01 = SAO
386 // 0b10 = Non Idenmpotent
387 // 0b11 = Tolerant I/O
388 // [60:63] = Encoded Access
389 // Authority
390 //
391 """
392 # get sprs
393 print("_walk_tree")
394 pidr = self.caller.spr["PIDR"]
395 prtbl = self.caller.spr["PRTBL"]
396 print(pidr)
397 print(prtbl)
398 p = addr[55:63]
399 print("last 8 bits ----------")
400 print
401
402 # get address of root entry
403 shift = selectconcat(SelectableInt(0,1), prtbl[58:63]) # TODO verify
404 addr_next = self._get_prtable_addr(shift, prtbl, addr, pidr)
405 print("starting with prtable, addr_next",addr_next)
406
407 assert(addr_next.bits == 64)
408 assert(addr_next.value == 0x1000000) #TODO
409
410 # read an entry from prtable
411 swap = False
412 check_in_mem = False
413 entry_width = 8
414 data = self._next_level(addr_next, entry_width, swap, check_in_mem)
415 print("pr_table",data)
416
417 # rts = shift = unsigned('0' & data(62 downto 61) & data(7 downto 5));
418 shift = selectconcat(SelectableInt(0,1), data[1:3], data[55:58])
419 assert(shift.bits==6) # variable rts : unsigned(5 downto 0);
420 print("shift",shift)
421
422 # mbits := unsigned('0' & data(4 downto 0));
423 mbits = selectconcat(SelectableInt(0,1), data[58:63])
424 assert(mbits.bits==6) #variable mbits : unsigned(5 downto 0);
425
426 # WIP
427 if mbits==0:
428 return "invalid"
429
430 # mask_size := mbits(4 downto 0);
431 mask_size = mbits[0:5];
432 assert(mask_size.bits==5)
433 print("before segment check ==========")
434 print("mask_size:",bin(mask_size.value))
435 print("mbits:",bin(mbits.value))
436
437 print("calling segment_check")
438
439 mbits = selectconcat(SelectableInt(0,1), mask_size)
440 new_shift = self._segment_check(addr, mbits, shift)
441 print("new_shift",new_shift)
442
443 print("TODO: next")
444 return None
445
446 #addr_next = SelectableInt(0x30000,64) # radix root for testing
447 # this needs to be calculated using the code above
448
449 # walk tree starts on prtbl
450 while True:
451 print("nextlevel----------------------------")
452 # read an entry
453 swap = False
454 check_in_mem = False
455 entry_width = 8
456
457 data = self._next_level(addr_next, entry_width, swap, check_in_mem)
458 valid = rpte_valid(data)
459 leaf = rpte_leaf(data)
460
461 print(" valid, leaf", valid, leaf)
462 if not valid:
463 return "invalid" # TODO: return error
464 if leaf:
465 print ("is leaf, checking perms")
466 ok = self._check_perms(data, priv, mode)
467 if ok == True: # data was ok, found phys address, return it?
468 paddr = self._get_pte(addrsh, addr, data)
469 print (" phys addr", hex(paddr.value))
470 return paddr
471 return ok # return the error code
472 else:
473 newlookup = self._new_lookup(data, shift)
474 if newlookup == 'badtree':
475 return newlookup
476 shift, mask, pgbase = newlookup
477 print (" next level", shift, mask, pgbase)
478 shift = SelectableInt(shift.value,16) #THIS is wrong !!!
479 print("calling _get_pgtable_addr")
480 print(mask) #SelectableInt(value=0x9, bits=4)
481 print(pgbase) #SelectableInt(value=0x40000, bits=56)
482 print(shift) #SelectableInt(value=0x4, bits=16) #FIXME
483 pgbase = SelectableInt(pgbase.value, 64)
484 addrsh = addrshift(addr,shift)
485 addr_next = self._get_pgtable_addr(mask, pgbase, addrsh)
486 print("addr_next",addr_next)
487 print("addrsh",addrsh)
488
489 def _new_lookup(self, data, shift):
490 """
491 mbits := unsigned('0' & data(4 downto 0));
492 if mbits < 5 or mbits > 16 or mbits > r.shift then
493 v.state := RADIX_FINISH;
494 v.badtree := '1'; -- throw error
495 else
496 v.shift := v.shift - mbits;
497 v.mask_size := mbits(4 downto 0);
498 v.pgbase := data(55 downto 8) & x"00"; NLB?
499 v.state := RADIX_LOOKUP; --> next level
500 end if;
501 """
502 mbits = data[59:64]
503 print("mbits=", mbits)
504 if mbits < 5 or mbits > 16: #fixme compare with r.shift
505 print("badtree")
506 return "badtree"
507 # reduce shift (has to be done at same bitwidth)
508 shift = shift - selectconcat(SelectableInt(0, 1), mbits)
509 mask_size = mbits[1:5] # get 4 LSBs
510 pgbase = selectconcat(data[8:56], SelectableInt(0, 8)) # shift up 8
511 return shift, mask_size, pgbase
512
513 def _decode_prte(self, data):
514 """PRTE0 Layout
515 -----------------------------------------------
516 |/|RTS1|/| RPDB | RTS2 | RPDS |
517 -----------------------------------------------
518 0 1 2 3 4 55 56 58 59 63
519 """
520 # note that SelectableInt does big-endian! so the indices
521 # below *directly* match the spec, unlike microwatt which
522 # has to turn them around (to LE)
523 zero = SelectableInt(0, 1)
524 rts = selectconcat(zero,
525 data[56:59], # RTS2
526 data[1:3], # RTS1
527 )
528 masksize = data[59:64] # RPDS
529 mbits = selectconcat(zero, masksize)
530 pgbase = selectconcat(data[8:56], # part of RPDB
531 SelectableInt(0, 16),)
532
533 return (rts, mbits, pgbase)
534
535 def _segment_check(self, addr, mbits, shift):
536 """checks segment valid
537 mbits := '0' & r.mask_size;
538 v.shift := r.shift + (31 - 12) - mbits;
539 nonzero := or(r.addr(61 downto 31) and not finalmask(30 downto 0));
540 if r.addr(63) /= r.addr(62) or nonzero = '1' then
541 v.state := RADIX_FINISH;
542 v.segerror := '1';
543 elsif mbits < 5 or mbits > 16 or mbits > (r.shift + (31 - 12)) then
544 v.state := RADIX_FINISH;
545 v.badtree := '1';
546 else
547 v.state := RADIX_LOOKUP;
548 """
549 # note that SelectableInt does big-endian! so the indices
550 # below *directly* match the spec, unlike microwatt which
551 # has to turn them around (to LE)
552 mask = genmask(shift, 44)
553 nonzero = addr[2:33] & mask[13:44] # mask 31 LSBs (BE numbered 13:44)
554 print ("RADIX _segment_check nonzero", bin(nonzero.value))
555 print ("RADIX _segment_check addr[0-1]", addr[0].value, addr[1].value)
556 if addr[0] != addr[1] or nonzero != 0:
557 return "segerror"
558 limit = shift + (31 - 12)
559 if mbits.value < 5 or mbits.value > 16 or mbits.value > limit.value:
560 return "badtree mbits="+str(mbits.value)+" limit="+str(limit.value)
561 new_shift = shift + (31 - 12) - mbits
562 # TODO verify that returned result is correct
563 return new_shift
564
565 def _check_perms(self, data, priv, mode):
566 """check page permissions
567 // Leaf PDE |
568 // |------------------------------| |----------------|
569 // |V|L|sw|//|RPN|sw|R|C|/|ATT|EAA| | usefulBits |
570 // |------------------------------| |----------------|
571 // [0] = V = Valid Bit |
572 // [1] = L = Leaf Bit = 1 if leaf |
573 // PDE |
574 // [2] = Sw = Sw bit 0. |
575 // [7:51] = RPN = Real Page Number, V
576 // real_page = RPN << 12 -------------> Logical OR
577 // [52:54] = Sw Bits 1:3 |
578 // [55] = R = Reference |
579 // [56] = C = Change V
580 // [58:59] = Att = Physical Address
581 // 0b00 = Normal Memory
582 // 0b01 = SAO
583 // 0b10 = Non Idenmpotent
584 // 0b11 = Tolerant I/O
585 // [60:63] = Encoded Access
586 // Authority
587 //
588 -- test leaf bit
589 -- check permissions and RC bits
590 perm_ok := '0';
591 if r.priv = '1' or data(3) = '0' then
592 if r.iside = '0' then
593 perm_ok := data(1) or (data(2) and not r.store);
594 else
595 -- no IAMR, so no KUEP support for now
596 -- deny execute permission if cache inhibited
597 perm_ok := data(0) and not data(5);
598 end if;
599 end if;
600 rc_ok := data(8) and (data(7) or not r.store);
601 if perm_ok = '1' and rc_ok = '1' then
602 v.state := RADIX_LOAD_TLB;
603 else
604 v.state := RADIX_FINISH;
605 v.perm_err := not perm_ok;
606 -- permission error takes precedence over RC error
607 v.rc_error := perm_ok;
608 end if;
609 """
610 # decode mode into something that matches microwatt equivalent code
611 instr_fetch, store = 0, 0
612 if mode == 'STORE':
613 store = 1
614 if mode == 'EXECUTE':
615 inst_fetch = 1
616
617 # check permissions and RC bits
618 perm_ok = 0
619 if priv == 1 or data[60] == 0:
620 if instr_fetch == 0:
621 perm_ok = data[62] | (data[61] & (store == 0))
622 # no IAMR, so no KUEP support for now
623 # deny execute permission if cache inhibited
624 perm_ok = data[63] & ~data[58]
625 rc_ok = data[55] & (data[56] | (store == 0))
626 if perm_ok == 1 and rc_ok == 1:
627 return True
628
629 return "perm_err" if perm_ok == 0 else "rc_err"
630
631 def _get_prtable_addr(self, shift, prtbl, addr, pid):
632 """
633 if r.addr(63) = '1' then
634 effpid := x"00000000";
635 else
636 effpid := r.pid;
637 end if;
638 x"00" & r.prtbl(55 downto 36) &
639 ((r.prtbl(35 downto 12) and not finalmask(23 downto 0)) or
640 (effpid(31 downto 8) and finalmask(23 downto 0))) &
641 effpid(7 downto 0) & "0000";
642 """
643 print ("_get_prtable_addr", shift, prtbl, addr, pid)
644 finalmask = genmask(shift, 44)
645 finalmask24 = finalmask[20:44]
646 if addr[0].value == 1:
647 effpid = SelectableInt(0, 32)
648 else:
649 effpid = pid #self.pid # TODO, check on this
650 zero8 = SelectableInt(0, 8)
651 zero4 = SelectableInt(0, 4)
652 res = selectconcat(zero8,
653 prtbl[8:28], #
654 (prtbl[28:52] & ~finalmask24) | #
655 (effpid[0:24] & finalmask24), #
656 effpid[24:32],
657 zero4
658 )
659 return res
660
661 def _get_pgtable_addr(self, mask_size, pgbase, addrsh):
662 """
663 x"00" & r.pgbase(55 downto 19) &
664 ((r.pgbase(18 downto 3) and not mask) or (addrsh and mask)) &
665 "000";
666 """
667 mask16 = genmask(mask_size+5, 16)
668 zero8 = SelectableInt(0, 8)
669 zero3 = SelectableInt(0, 3)
670 res = selectconcat(zero8,
671 pgbase[8:45], #
672 (pgbase[45:61] & ~mask16) | #
673 (addrsh & mask16), #
674 zero3
675 )
676 return res
677
678 def _get_pte(self, shift, addr, pde):
679 """
680 x"00" &
681 ((r.pde(55 downto 12) and not finalmask) or
682 (r.addr(55 downto 12) and finalmask))
683 & r.pde(11 downto 0);
684 """
685 shift.value = 12
686 finalmask = genmask(shift, 44)
687 zero8 = SelectableInt(0, 8)
688 rpn = pde[8:52] # RPN = Real Page Number
689 abits = addr[8:52] # non-masked address bits
690 print(" get_pte RPN", hex(rpn.value))
691 print(" abits", hex(abits.value))
692 print(" shift", shift.value)
693 print(" finalmask", bin(finalmask.value))
694 res = selectconcat(zero8,
695 (rpn & ~finalmask) | #
696 (abits & finalmask), #
697 addr[52:64],
698 )
699 return res
700
701 class TestRadixMMU(unittest.TestCase):
702
703 def tst_genmask(self):
704 shift = SelectableInt(5, 6)
705 mask = genmask(shift, 43)
706 print (" mask", bin(mask.value))
707
708 self.assertEqual(mask.value, 0b11111, "mask should be 5 1s")
709
710 def tst_get_pgtable_addr(self):
711
712 mem = None
713 caller = None
714 dut = RADIX(mem, caller)
715
716 mask_size=4
717 pgbase = SelectableInt(0,64)
718 addrsh = SelectableInt(0,16)
719 ret = dut._get_pgtable_addr(mask_size, pgbase, addrsh)
720 print("ret=", ret)
721 self.assertEqual(ret, 0, "pgtbl_addr should be 0")
722
723 def test_walk_tree_1(self):
724
725 # test address as in
726 # https://github.com/power-gem5/gem5/blob/gem5-experimental/src/arch/power/radix_walk_example.txt#L65
727 testaddr = 0x1000
728 expected = 0x1000
729
730 # starting prtbl
731 prtbl = 0x1000000
732
733 # set up dummy minimal ISACaller
734 spr = {'DSISR': SelectableInt(0, 64),
735 'DAR': SelectableInt(0, 64),
736 'PIDR': SelectableInt(0, 64),
737 'PRTBL': SelectableInt(prtbl, 64)
738 }
739 # set problem state == 0 (other unit tests, set to 1)
740 msr = SelectableInt(0, 64)
741 msr[MSRb.PR] = 0
742 class ISACaller: pass
743 caller = ISACaller()
744 caller.spr = spr
745 caller.msr = msr
746
747 shift = SelectableInt(5, 6)
748 mask = genmask(shift, 43)
749 print (" mask", bin(mask.value))
750
751 mem = Mem(row_bytes=8, initial_mem=testmem)
752 mem = RADIX(mem, caller)
753 # -----------------------------------------------
754 # |/|RTS1|/| RPDB | RTS2 | RPDS |
755 # -----------------------------------------------
756 # |0|1 2|3|4 55|56 58|59 63|
757 data = SelectableInt(0, 64)
758 data[1:3] = 0b01
759 data[56:59] = 0b11
760 data[59:64] = 0b01101 # mask
761 data[55] = 1
762 (rts, mbits, pgbase) = mem._decode_prte(data)
763 print (" rts", bin(rts.value), rts.bits)
764 print (" mbits", bin(mbits.value), mbits.bits)
765 print (" pgbase", hex(pgbase.value), pgbase.bits)
766 addr = SelectableInt(0x1000, 64)
767 check = mem._segment_check(addr, mbits, shift)
768 print (" segment check", check)
769
770 print("walking tree")
771 addr = SelectableInt(testaddr,64)
772 # pgbase = None
773 mode = None
774 #mbits = None
775 shift = rts
776 result = mem._walk_tree(addr, pgbase, mode, mbits, shift)
777 print(" walking tree result", result)
778 print("should be", testresult)
779 self.assertEqual(result.value, expected,
780 "expected 0x%x got 0x%x" % (expected,
781 result.value))
782
783
784 def tst_walk_tree_2(self):
785
786 # test address slightly different
787 testaddr = 0x1101
788 expected = 0x5001101
789
790 # starting prtbl
791 prtbl = 0x1000000
792
793 # set up dummy minimal ISACaller
794 spr = {'DSISR': SelectableInt(0, 64),
795 'DAR': SelectableInt(0, 64),
796 'PIDR': SelectableInt(0, 64),
797 'PRTBL': SelectableInt(prtbl, 64)
798 }
799 # set problem state == 0 (other unit tests, set to 1)
800 msr = SelectableInt(0, 64)
801 msr[MSRb.PR] = 0
802 class ISACaller: pass
803 caller = ISACaller()
804 caller.spr = spr
805 caller.msr = msr
806
807 shift = SelectableInt(5, 6)
808 mask = genmask(shift, 43)
809 print (" mask", bin(mask.value))
810
811 mem = Mem(row_bytes=8, initial_mem=testmem2)
812 mem = RADIX(mem, caller)
813 # -----------------------------------------------
814 # |/|RTS1|/| RPDB | RTS2 | RPDS |
815 # -----------------------------------------------
816 # |0|1 2|3|4 55|56 58|59 63|
817 data = SelectableInt(0, 64)
818 data[1:3] = 0b01
819 data[56:59] = 0b11
820 data[59:64] = 0b01101 # mask
821 data[55] = 1
822 (rts, mbits, pgbase) = mem._decode_prte(data)
823 print (" rts", bin(rts.value), rts.bits)
824 print (" mbits", bin(mbits.value), mbits.bits)
825 print (" pgbase", hex(pgbase.value), pgbase.bits)
826 addr = SelectableInt(0x1000, 64)
827 check = mem._segment_check(addr, mbits, shift)
828 print (" segment check", check)
829
830 print("walking tree")
831 addr = SelectableInt(testaddr,64)
832 # pgbase = None
833 mode = None
834 #mbits = None
835 shift = rts
836 result = mem._walk_tree(addr, pgbase, mode, mbits, shift)
837 print(" walking tree result", result)
838 print("should be", testresult)
839 self.assertEqual(result.value, expected,
840 "expected 0x%x got 0x%x" % (expected,
841 result.value))
842
843
844 if __name__ == '__main__':
845 unittest.main()