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
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.
13 * https://bugs.libre-soc.org/show_bug.cgi?id=604
16 from nmigen
.back
.pysim
import Settle
18 from soc
.decoder
.selectable_int
import (FieldSelectableInt
, SelectableInt
,
20 from soc
.decoder
.helpers
import exts
, gtu
, ltu
, undefined
21 from soc
.decoder
.isa
.mem
import Mem
26 # very quick, TODO move to SelectableInt utils later
27 def genmask(shift
, size
):
28 res
= SelectableInt(0, size
)
31 res
[size
-1-i
] = SelectableInt(1, 1)
34 # NOTE: POWER 3.0B annotation order! see p4 1.3.2
35 # MSB is indexed **LOWEST** (sigh)
36 # from gem5 radixwalk.hh
37 # Bitfield<63> valid; 64 - (63 + 1) = 0
38 # Bitfield<62> leaf; 64 - (62 + 1) = 1
63 //Accessing 2nd double word of partition table (pate1)
64 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.1
66 // ====================================================
67 // -----------------------------------------------
68 // | /// | PATB | /// | PATS |
69 // -----------------------------------------------
71 // PATB[4:51] holds the base address of the Partition Table,
72 // right shifted by 12 bits.
73 // This is because the address of the Partition base is
74 // 4k aligned. Hence, the lower 12bits, which are always
75 // 0 are ommitted from the PTCR.
77 // Thus, The Partition Table Base is obtained by (PATB << 12)
79 // PATS represents the partition table size right-shifted by 12 bits.
80 // The minimal size of the partition table is 4k.
81 // Thus partition table size = (1 << PATS + 12).
84 // ====================================================
85 // 0 PATE0 63 PATE1 127
86 // |----------------------|----------------------|
88 // |----------------------|----------------------|
90 // |----------------------|----------------------|
92 // |----------------------|----------------------|
96 // |----------------------|----------------------|
98 // |----------------------|----------------------|
100 // The effective LPID forms the index into the Partition Table.
102 // Each entry in the partition table contains 2 double words, PATE0, PATE1,
103 // corresponding to that partition.
105 // In case of Radix, The structure of PATE0 and PATE1 is as follows.
108 // -----------------------------------------------
109 // |1|RTS1|/| RPDB | RTS2 | RPDS |
110 // -----------------------------------------------
111 // 0 1 2 3 4 55 56 58 59 63
113 // HR[0] : For Radix Page table, first bit should be 1.
114 // RTS1[1:2] : Gives one fragment of the Radix treesize
115 // RTS2[56:58] : Gives the second fragment of the Radix Tree size.
116 // RTS = (RTS1 << 3 + RTS2) + 31.
118 // RPDB[4:55] = Root Page Directory Base.
119 // RPDS = Logarithm of Root Page Directory Size right shifted by 3.
120 // Thus, Root page directory size = 1 << (RPDS + 3).
124 // -----------------------------------------------
125 // |///| PRTB | // | PRTS |
126 // -----------------------------------------------
127 // 0 3 4 51 52 58 59 63
129 // PRTB[4:51] = Process Table Base. This is aligned to size.
130 // PRTS[59: 63] = Process Table Size right shifted by 12.
131 // Minimal size of the process table is 4k.
132 // Process Table Size = (1 << PRTS + 12).
135 // Computing the size aligned Process Table Base:
136 // table_base = (PRTB & ~((1 << PRTS) - 1)) << 12
137 // Thus, the lower 12+PRTS bits of table_base will
141 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.2
144 // ==========================
145 // 0 PRTE0 63 PRTE1 127
146 // |----------------------|----------------------|
148 // |----------------------|----------------------|
150 // |----------------------|----------------------|
152 // |----------------------|----------------------|
156 // |----------------------|----------------------|
158 // |----------------------|----------------------|
160 // The effective Process id (PID) forms the index into the Process Table.
162 // Each entry in the partition table contains 2 double words, PRTE0, PRTE1,
163 // corresponding to that process
165 // In case of Radix, The structure of PRTE0 and PRTE1 is as follows.
168 // -----------------------------------------------
169 // |/|RTS1|/| RPDB | RTS2 | RPDS |
170 // -----------------------------------------------
171 // 0 1 2 3 4 55 56 58 59 63
173 // RTS1[1:2] : Gives one fragment of the Radix treesize
174 // RTS2[56:58] : Gives the second fragment of the Radix Tree size.
175 // RTS = (RTS1 << 3 + RTS2) << 31,
176 // since minimal Radix Tree size is 4G.
178 // RPDB = Root Page Directory Base.
179 // RPDS = Root Page Directory Size right shifted by 3.
180 // Thus, Root page directory size = RPDS << 3.
184 // -----------------------------------------------
186 // -----------------------------------------------
188 // All bits are reserved.
193 # see qemu/target/ppc/mmu-radix64.c for reference
195 def __init__(self
, mem
, caller
):
199 self
.dsisr
= self
.caller
.spr
["DSISR"]
200 self
.dar
= self
.caller
.spr
["DAR"]
201 self
.pidr
= self
.caller
.spr
["PIDR"]
202 self
.prtbl
= self
.caller
.spr
["PRTBL"]
204 # cached page table stuff
206 self
.pt0_valid
= False
208 self
.pt3_valid
= False
210 def __call__(self
, addr
, sz
):
211 val
= self
.ld(addr
.value
, sz
, swap
=False)
212 print("RADIX memread", addr
, sz
, val
)
213 return SelectableInt(val
, sz
*8)
215 def ld(self
, address
, width
=8, swap
=True, check_in_mem
=False,
217 print("RADIX: ld from addr 0x%x width %d" % (address
, width
))
223 addr
= SelectableInt(address
, 64)
224 (shift
, mbits
, pgbase
) = self
._decode
_prte
(addr
)
225 #shift = SelectableInt(0, 32)
227 pte
= self
._walk
_tree
(addr
, pgbase
, mode
, mbits
, shift
)
228 # use pte to caclculate phys address
229 return self
.mem
.ld(address
, width
, swap
, check_in_mem
)
231 # XXX set SPRs on error
234 def st(self
, address
, v
, width
=8, swap
=True):
235 print("RADIX: st to addr 0x%x width %d data %x" % (address
, width
, v
))
238 addr
= SelectableInt(address
, 64)
239 (shift
, mbits
, pgbase
) = self
._decode
_prte
(addr
)
240 pte
= self
._walk
_tree
(addr
, pgbase
, mode
, mbits
, shift
)
242 # use pte to caclculate phys address (addr)
243 return self
.mem
.st(addr
.value
, v
, width
, swap
)
245 # XXX set SPRs on error
247 def memassign(self
, addr
, sz
, val
):
248 print("memassign", addr
, sz
, val
)
249 self
.st(addr
.value
, val
.value
, sz
, swap
=False)
251 def _next_level(self
,r
):
252 return rpte_valid(r
), rpte_leaf(r
)
256 ## Prepare for next iteration
258 def _walk_tree(self
, addr
, pgbase
, mode
, mbits
, shift
):
262 // vaddr |-----------------------------------------------------|
264 // |-----------|-----------------------------------------|
265 // | 0000000 | usefulBits = X bits (typically 52) |
266 // |-----------|-----------------------------------------|
267 // | |<--Cursize---->| |
271 // |-----------------------------------------------------|
274 // PDE |---------------------------| |
275 // |V|L|//| NLB |///|NLS| |
276 // |---------------------------| |
277 // PDE = Page Directory Entry |
278 // [0] = V = Valid Bit |
279 // [1] = L = Leaf bit. If 0, then |
280 // [4:55] = NLB = Next Level Base |
281 // right shifted by 8 |
282 // [59:63] = NLS = Next Level Size |
285 // | |--------------------------|
286 // | | usfulBits = X-Cursize |
287 // | |--------------------------|
288 // |---------------------><--NLS-->| |
292 // |--------------------------|
294 // If the next PDE obtained by |
295 // (NLB << 8 + 8 * index) is a |
296 // nonleaf, then repeat the above. |
298 // If the next PDE is a leaf, |
299 // then Leaf PDE structure is as |
304 // |------------------------------| |----------------|
305 // |V|L|sw|//|RPN|sw|R|C|/|ATT|EAA| | usefulBits |
306 // |------------------------------| |----------------|
307 // [0] = V = Valid Bit |
308 // [1] = L = Leaf Bit = 1 if leaf |
310 // [2] = Sw = Sw bit 0. |
311 // [7:51] = RPN = Real Page Number, V
312 // real_page = RPN << 12 -------------> Logical OR
313 // [52:54] = Sw Bits 1:3 |
314 // [55] = R = Reference |
315 // [56] = C = Change V
316 // [58:59] = Att = Physical Address
317 // 0b00 = Normal Memory
319 // 0b10 = Non Idenmpotent
320 // 0b11 = Tolerant I/O
321 // [60:63] = Encoded Access
327 pidr
= self
.caller
.spr
["PIDR"]
328 prtbl
= self
.caller
.spr
["PRTBL"]
332 print("last 8 bits ----------")
335 # get address of root entry
336 prtable_addr
= self
._get
_prtable
_addr
(shift
, prtbl
, addr
, pidr
)
337 print("prtable_addr",prtable_addr
)
339 # read root entry - imcomplete
343 value
= self
.mem
.ld(prtable_addr
.value
, entry_width
, swap
, check_in_mem
)
347 SelectableInt(0x8000000000000007, 64), #valid
348 SelectableInt(0xc000000000000000, 64) #exit
352 # walk tree starts on prtbl
354 print("nextlevel----------------------------")
355 l
= test_input
[index
]
357 valid
,leaf
= self
._next
_level
(l
)
362 if mbits
< 5 or mbits
> 16:
366 mbits := unsigned('0' & data(4 downto 0));
367 if mbits < 5 or mbits > 16 or mbits > r.shift then
368 v.state := RADIX_FINISH;
369 v.badtree := '1'; -- throw error
371 v.shift := v.shift - mbits;
372 v.mask_size := mbits(4 downto 0);
373 v.pgbase := data(55 downto 8) & x"00"; NLB?
374 v.state := RADIX_LOOKUP; --> next level
379 if not valid
: return None
382 def _decode_prte(self
, data
):
384 -----------------------------------------------
385 |/|RTS1|/| RPDB | RTS2 | RPDS |
386 -----------------------------------------------
387 0 1 2 3 4 55 56 58 59 63
389 # note that SelectableInt does big-endian! so the indices
390 # below *directly* match the spec, unlike microwatt which
391 # has to turn them around (to LE)
392 zero
= SelectableInt(0, 1)
393 rts
= selectconcat(zero
,
397 masksize
= data
[59:64] # RPDS
398 mbits
= selectconcat(zero
, masksize
)
399 pgbase
= selectconcat(data
[8:56], # part of RPDB
400 SelectableInt(0, 16),)
402 return (rts
, mbits
, pgbase
)
404 def _segment_check(self
, addr
, mbits
, shift
):
405 """checks segment valid
406 mbits := '0' & r.mask_size;
407 v.shift := r.shift + (31 - 12) - mbits;
408 nonzero := or(r.addr(61 downto 31) and not finalmask(30 downto 0));
409 if r.addr(63) /= r.addr(62) or nonzero = '1' then
410 v.state := RADIX_FINISH;
412 elsif mbits < 5 or mbits > 16 or mbits > (r.shift + (31 - 12)) then
413 v.state := RADIX_FINISH;
416 v.state := RADIX_LOOKUP;
418 # note that SelectableInt does big-endian! so the indices
419 # below *directly* match the spec, unlike microwatt which
420 # has to turn them around (to LE)
421 mask
= genmask(shift
, 44)
422 nonzero
= addr
[1:32] & mask
[13:44] # mask 31 LSBs (BE numbered 13:44)
423 print ("RADIX _segment_check nonzero", bin(nonzero
.value
))
424 print ("RADIX _segment_check addr[0-1]", addr
[0].value
, addr
[1].value
)
425 if addr
[0] != addr
[1] or nonzero
== 1:
427 limit
= shift
+ (31 - 12)
428 if mbits
< 5 or mbits
> 16 or mbits
> limit
:
430 new_shift
= shift
+ (31 - 12) - mbits
433 def _check_perms(self
, data
, priv
, instr_fetch
, store
):
434 """check page permissions
436 // |------------------------------| |----------------|
437 // |V|L|sw|//|RPN|sw|R|C|/|ATT|EAA| | usefulBits |
438 // |------------------------------| |----------------|
439 // [0] = V = Valid Bit |
440 // [1] = L = Leaf Bit = 1 if leaf |
442 // [2] = Sw = Sw bit 0. |
443 // [7:51] = RPN = Real Page Number, V
444 // real_page = RPN << 12 -------------> Logical OR
445 // [52:54] = Sw Bits 1:3 |
446 // [55] = R = Reference |
447 // [56] = C = Change V
448 // [58:59] = Att = Physical Address
449 // 0b00 = Normal Memory
451 // 0b10 = Non Idenmpotent
452 // 0b11 = Tolerant I/O
453 // [60:63] = Encoded Access
457 -- check permissions and RC bits
459 if r.priv = '1' or data(3) = '0' then
460 if r.iside = '0' then
461 perm_ok := data(1) or (data(2) and not r.store);
463 -- no IAMR, so no KUEP support for now
464 -- deny execute permission if cache inhibited
465 perm_ok := data(0) and not data(5);
468 rc_ok := data(8) and (data(7) or not r.store);
469 if perm_ok = '1' and rc_ok = '1' then
470 v.state := RADIX_LOAD_TLB;
472 v.state := RADIX_FINISH;
473 v.perm_err := not perm_ok;
474 -- permission error takes precedence over RC error
475 v.rc_error := perm_ok;
478 # check permissions and RC bits
480 if priv
== 1 or data
[60] == 0:
482 perm_ok
= data
[62] |
(data
[61] & (store
== 0))
483 # no IAMR, so no KUEP support for now
484 # deny execute permission if cache inhibited
485 perm_ok
= data
[63] & ~data
[58]
486 rc_ok
= data
[55] & (data
[56] |
(store
== 0))
487 if perm_ok
== 1 and rc_ok
== 1:
489 return "perm_err" if perm_ok
== 0 else "rc_err"
491 def _get_prtable_addr(self
, shift
, prtbl
, addr
, pid
):
493 if r.addr(63) = '1' then
494 effpid := x"00000000";
498 x"00" & r.prtbl(55 downto 36) &
499 ((r.prtbl(35 downto 12) and not finalmask(23 downto 0)) or
500 (effpid(31 downto 8) and finalmask(23 downto 0))) &
501 effpid(7 downto 0) & "0000";
503 print ("_get_prtable_addr_", shift
, prtbl
, addr
, pid
)
504 finalmask
= genmask(shift
, 44)
505 finalmask24
= finalmask
[20:44]
506 if addr
[0].value
== 1:
507 effpid
= SelectableInt(0, 32)
509 effpid
= pid
#self.pid # TODO, check on this
510 zero16
= SelectableInt(0, 16)
511 zero4
= SelectableInt(0, 4)
512 res
= selectconcat(zero16
,
514 (prtbl
[28:52] & ~finalmask24
) |
#
515 (effpid
[0:24] & finalmask24
), #
521 def _get_pgtable_addr(self
, mask_size
, pgbase
, addrsh
):
523 x"00" & r.pgbase(55 downto 19) &
524 ((r.pgbase(18 downto 3) and not mask) or (addrsh and mask)) &
527 mask16
= genmask(mask_size
+5, 16)
528 zero8
= SelectableInt(0, 8)
529 zero3
= SelectableInt(0, 3)
530 res
= selectconcat(zero8
,
532 (prtbl
[45:61] & ~mask16
) |
#
538 def _get_pte(self
, shift
, addr
, pde
):
541 ((r.pde(55 downto 12) and not finalmask) or
542 (r.addr(55 downto 12) and finalmask))
543 & r.pde(11 downto 0);
545 finalmask
= genmask(shift
, 44)
546 zero8
= SelectableInt(0, 8)
547 res
= selectconcat(zero8
,
548 (pde
[8:52] & ~finalmask
) |
#
549 (addr
[8:52] & finalmask
), #
555 # very quick test of maskgen function (TODO, move to util later)
556 if __name__
== '__main__':
557 # set up dummy minimal ISACaller
558 spr
= {'DSISR': SelectableInt(0, 64),
559 'DAR': SelectableInt(0, 64),
560 'PIDR': SelectableInt(0, 64),
561 'PRTBL': SelectableInt(0, 64)
563 class ISACaller
: pass
567 shift
= SelectableInt(5, 6)
568 mask
= genmask(shift
, 43)
569 print (" mask", bin(mask
.value
))
571 mem
= Mem(row_bytes
=8)
572 mem
= RADIX(mem
, caller
)
573 # -----------------------------------------------
574 # |/|RTS1|/| RPDB | RTS2 | RPDS |
575 # -----------------------------------------------
576 # |0|1 2|3|4 55|56 58|59 63|
577 data
= SelectableInt(0, 64)
580 data
[59:64] = 0b01101 # mask
582 (rts
, mbits
, pgbase
) = mem
._decode
_prte
(data
)
583 print (" rts", bin(rts
.value
), rts
.bits
)
584 print (" mbits", bin(mbits
.value
), mbits
.bits
)
585 print (" pgbase", hex(pgbase
.value
), pgbase
.bits
)
586 addr
= SelectableInt(0x1000, 64)
587 check
= mem
._segment
_check
(addr
, mbits
, shift
)
588 print (" segment check", check
)
590 print("walking tree")
596 result
= mem
._walk
_tree
(addr
, pgbase
, mode
, mbits
, shift
)