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
17 from functools
import wraps
19 from soc
.decoder
.orderedset
import OrderedSet
20 from soc
.decoder
.selectable_int
import (FieldSelectableInt
, SelectableInt
,
22 from soc
.decoder
.power_enums
import (spr_dict
, spr_byname
, XER_bits
,
23 insns
, MicrOp
, In1Sel
, In2Sel
, In3Sel
,
26 from soc
.decoder
.power_enums
import SPR
as DEC_SPR
28 from soc
.decoder
.helpers
import exts
, gtu
, ltu
, undefined
29 from soc
.consts
import PIb
, MSRb
# big-endian (PowerISA versions)
30 from soc
.decoder
.power_svp64
import SVP64RM
, decode_extra
32 from collections
import namedtuple
36 instruction_info
= namedtuple('instruction_info',
37 'func read_regs uninit_regs write_regs ' +
38 'special_regs op_fields form asmregs')
48 def swap_order(x
, nbytes
):
49 x
= x
.to_bytes(nbytes
, byteorder
='little')
50 x
= int.from_bytes(x
, byteorder
='big', signed
=False)
55 # TODO (lkcl): adjust other registers that should be in a particular order
56 # probably CA, CA32, and CR
73 # very quick, TODO move to SelectableInt utils later
74 def genmask(shift
, size
):
75 res
= SelectableInt(0, size
)
78 res
[size
-1-i
] = SelectableInt(1, 1)
84 //Accessing 2nd double word of partition table (pate1)
85 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.1
87 // ====================================================
88 // -----------------------------------------------
89 // | /// | PATB | /// | PATS |
90 // -----------------------------------------------
92 // PATB[4:51] holds the base address of the Partition Table,
93 // right shifted by 12 bits.
94 // This is because the address of the Partition base is
95 // 4k aligned. Hence, the lower 12bits, which are always
96 // 0 are ommitted from the PTCR.
98 // Thus, The Partition Table Base is obtained by (PATB << 12)
100 // PATS represents the partition table size right-shifted by 12 bits.
101 // The minimal size of the partition table is 4k.
102 // Thus partition table size = (1 << PATS + 12).
105 // ====================================================
106 // 0 PATE0 63 PATE1 127
107 // |----------------------|----------------------|
109 // |----------------------|----------------------|
111 // |----------------------|----------------------|
113 // |----------------------|----------------------|
117 // |----------------------|----------------------|
119 // |----------------------|----------------------|
121 // The effective LPID forms the index into the Partition Table.
123 // Each entry in the partition table contains 2 double words, PATE0, PATE1,
124 // corresponding to that partition.
126 // In case of Radix, The structure of PATE0 and PATE1 is as follows.
129 // -----------------------------------------------
130 // |1|RTS1|/| RPDB | RTS2 | RPDS |
131 // -----------------------------------------------
132 // 0 1 2 3 4 55 56 58 59 63
134 // HR[0] : For Radix Page table, first bit should be 1.
135 // RTS1[1:2] : Gives one fragment of the Radix treesize
136 // RTS2[56:58] : Gives the second fragment of the Radix Tree size.
137 // RTS = (RTS1 << 3 + RTS2) + 31.
139 // RPDB[4:55] = Root Page Directory Base.
140 // RPDS = Logarithm of Root Page Directory Size right shifted by 3.
141 // Thus, Root page directory size = 1 << (RPDS + 3).
145 // -----------------------------------------------
146 // |///| PRTB | // | PRTS |
147 // -----------------------------------------------
148 // 0 3 4 51 52 58 59 63
150 // PRTB[4:51] = Process Table Base. This is aligned to size.
151 // PRTS[59: 63] = Process Table Size right shifted by 12.
152 // Minimal size of the process table is 4k.
153 // Process Table Size = (1 << PRTS + 12).
156 // Computing the size aligned Process Table Base:
157 // table_base = (PRTB & ~((1 << PRTS) - 1)) << 12
158 // Thus, the lower 12+PRTS bits of table_base will
162 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.2
165 // ==========================
166 // 0 PRTE0 63 PRTE1 127
167 // |----------------------|----------------------|
169 // |----------------------|----------------------|
171 // |----------------------|----------------------|
173 // |----------------------|----------------------|
177 // |----------------------|----------------------|
179 // |----------------------|----------------------|
181 // The effective Process id (PID) forms the index into the Process Table.
183 // Each entry in the partition table contains 2 double words, PRTE0, PRTE1,
184 // corresponding to that process
186 // In case of Radix, The structure of PRTE0 and PRTE1 is as follows.
189 // -----------------------------------------------
190 // |/|RTS1|/| RPDB | RTS2 | RPDS |
191 // -----------------------------------------------
192 // 0 1 2 3 4 55 56 58 59 63
194 // RTS1[1:2] : Gives one fragment of the Radix treesize
195 // RTS2[56:58] : Gives the second fragment of the Radix Tree size.
196 // RTS = (RTS1 << 3 + RTS2) << 31,
197 // since minimal Radix Tree size is 4G.
199 // RPDB = Root Page Directory Base.
200 // RPDS = Root Page Directory Size right shifted by 3.
201 // Thus, Root page directory size = RPDS << 3.
205 // -----------------------------------------------
207 // -----------------------------------------------
209 // All bits are reserved.
214 # see qemu/target/ppc/mmu-radix64.c for reference
216 def __init__(self
, mem
, caller
):
220 # cached page table stuff
222 self
.pt0_valid
= False
224 self
.pt3_valid
= False
226 def __call__(self
,*args
, **kwargs
):
227 print("TODO: implement RADIX.__call__()")
232 def ld(self
, address
, width
=8, swap
=True, check_in_mem
=False):
233 print("RADIX: ld from addr 0x%x width %d" % (address
, width
))
234 dsisr
= self
.caller
.spr
[DEC_SPR
.DSISR
.value
]
235 dar
= self
.caller
.spr
[DEC_SPR
.DAR
.value
]
236 pidr
= self
.caller
.spr
[DEC_SPR
.PIDR
.value
]
237 prtbl
= self
.caller
.spr
[DEC_SPR
.PRTBL
.value
]
239 pte
= self
._walk
_tree
()
240 # use pte to caclculate phys address
241 return self
.mem
.ld(address
, width
, swap
, check_in_mem
)
243 # XXX set SPRs on error
246 def st(self
, addr
, v
, width
=8, swap
=True):
247 print("RADIX: st to addr 0x%x width %d data %x" % (addr
, width
, v
))
248 dsisr
= self
.caller
.spr
[DEC_SPR
.DSISR
.value
]
249 dar
= self
.caller
.spr
[DEC_SPR
.DAR
.value
]
250 pidr
= self
.caller
.spr
[DEC_SPR
.PIDR
.value
]
251 prtbl
= self
.caller
.spr
[DEC_SPR
.PRTBL
.value
]
253 # use pte to caclculate phys address (addr)
254 return self
.mem
.st(addr
, v
, width
, swap
)
256 # XXX set SPRs on error
258 def memassign(self
, addr
, sz
, val
):
259 print("memassign", addr
, sz
, val
)
260 self
.st(addr
.value
, val
.value
, sz
, swap
=False)
262 def _next_level(self
):
267 ## Prepare for next iteration
269 def _walk_tree(self
):
273 // vaddr |-----------------------------------------------------|
275 // |-----------|-----------------------------------------|
276 // | 0000000 | usefulBits = X bits (typically 52) |
277 // |-----------|-----------------------------------------|
278 // | |<--Cursize---->| |
282 // |-----------------------------------------------------|
285 // PDE |---------------------------| |
286 // |V|L|//| NLB |///|NLS| |
287 // |---------------------------| |
288 // PDE = Page Directory Entry |
289 // [0] = V = Valid Bit |
290 // [1] = L = Leaf bit. If 0, then |
291 // [4:55] = NLB = Next Level Base |
292 // right shifted by 8 |
293 // [59:63] = NLS = Next Level Size |
296 // | |--------------------------|
297 // | | usfulBits = X-Cursize |
298 // | |--------------------------|
299 // |---------------------><--NLS-->| |
303 // |--------------------------|
305 // If the next PDE obtained by |
306 // (NLB << 8 + 8 * index) is a |
307 // nonleaf, then repeat the above. |
309 // If the next PDE is a leaf, |
310 // then Leaf PDE structure is as |
315 // |------------------------------| |----------------|
316 // |V|L|sw|//|RPN|sw|R|C|/|ATT|EAA| | usefulBits |
317 // |------------------------------| |----------------|
318 // [0] = V = Valid Bit |
319 // [1] = L = Leaf Bit = 1 if leaf |
321 // [2] = Sw = Sw bit 0. |
322 // [7:51] = RPN = Real Page Number, V
323 // real_page = RPN << 12 -------------> Logical OR
324 // [52:54] = Sw Bits 1:3 |
325 // [55] = R = Reference |
326 // [56] = C = Change V
327 // [58:59] = Att = Physical Address
328 // 0b00 = Normal Memory
330 // 0b10 = Non Idenmpotent
331 // 0b11 = Tolerant I/O
332 // [60:63] = Encoded Access
336 # walk tree starts on prtbl
338 ret
= self
._next
_level
()
341 def _decode_prte(self
, data
):
343 -----------------------------------------------
344 |/|RTS1|/| RPDB | RTS2 | RPDS |
345 -----------------------------------------------
346 0 1 2 3 4 55 56 58 59 63
348 # note that SelectableInt does big-endian! so the indices
349 # below *directly* match the spec, unlike microwatt which
350 # has to turn them around (to LE)
351 zero
= SelectableInt(0, 1)
352 rts
= selectconcat(zero
,
356 masksize
= data
[59:64] # RPDS
357 mbits
= selectconcat(zero
, masksize
)
358 pgbase
= selectconcat(data
[8:56], # part of RPDB
359 SelectableInt(0, 16),)
361 return (rts
, mbits
, pgbase
)
363 def _segment_check(self
, addr
, mbits
, shift
):
364 """checks segment valid
365 mbits := '0' & r.mask_size;
366 v.shift := r.shift + (31 - 12) - mbits;
367 nonzero := or(r.addr(61 downto 31) and not finalmask(30 downto 0));
368 if r.addr(63) /= r.addr(62) or nonzero = '1' then
369 v.state := RADIX_FINISH;
371 elsif mbits < 5 or mbits > 16 or mbits > (r.shift + (31 - 12)) then
372 v.state := RADIX_FINISH;
375 v.state := RADIX_LOOKUP;
377 # note that SelectableInt does big-endian! so the indices
378 # below *directly* match the spec, unlike microwatt which
379 # has to turn them around (to LE)
380 mask
= genmask(shift
, 44)
381 nonzero
= addr
[1:32] & mask
[13:44] # mask 31 LSBs (BE numbered 13:44)
382 print ("RADIX _segment_check nonzero", bin(nonzero
.value
))
383 print ("RADIX _segment_check addr[0-1]", addr
[0].value
, addr
[1].value
)
384 if addr
[0] != addr
[1] or nonzero
== 1:
386 limit
= shift
+ (31 - 12)
387 if mbits
< 5 or mbits
> 16 or mbits
> limit
:
389 new_shift
= shift
+ (31 - 12) - mbits
392 def _check_perms(self
):
393 """check page permissions
395 if data(62) = '1' then
396 -- check permissions and RC bits
398 if r.priv = '1' or data(3) = '0' then
399 if r.iside = '0' then
400 perm_ok := data(1) or (data(2) and not r.store);
402 -- no IAMR, so no KUEP support for now
403 -- deny execute permission if cache inhibited
404 perm_ok := data(0) and not data(5);
407 rc_ok := data(8) and (data(7) or not r.store);
408 if perm_ok = '1' and rc_ok = '1' then
409 v.state := RADIX_LOAD_TLB;
411 v.state := RADIX_FINISH;
412 v.perm_err := not perm_ok;
413 -- permission error takes precedence over RC error
414 v.rc_error := perm_ok;
418 def _get_prtable_addr(self
, shift
, prtbl
, addr
, pid
):
420 if r.addr(63) = '1' then
421 effpid := x"00000000";
425 x"00" & r.prtbl(55 downto 36) &
426 ((r.prtbl(35 downto 12) and not finalmask(23 downto 0)) or
427 (effpid(31 downto 8) and finalmask(23 downto 0))) &
428 effpid(7 downto 0) & "0000";
430 finalmask
= genmask(shift
, 44)
431 finalmask24
= finalmask
[20:44]
432 if addr
[0].value
== 1:
433 effpid
= SelectableInt(0, 32)
435 effpid
= self
.pid
[32:64] # TODO, check on this
436 zero16
= SelectableInt(0, 16)
437 zero4
= SelectableInt(0, 4)
438 res
= selectconcat(zero16
,
440 (prtbl
[28:52] & ~finalmask24
) |
#
441 (effpid
[0:24] & finalmask24
), #
447 def _get_pgtable_addr(self
, mask_size
, pgbase
, addrsh
):
449 x"00" & r.pgbase(55 downto 19) &
450 ((r.pgbase(18 downto 3) and not mask) or (addrsh and mask)) &
453 mask16
= genmask(mask_size
+5, 16)
454 zero8
= SelectableInt(0, 8)
455 zero3
= SelectableInt(0, 3)
456 res
= selectconcat(zero8
,
458 (prtbl
[45:61] & ~mask16
) |
#
464 def _get_pte(self
, shift
, addr
, pde
):
467 ((r.pde(55 downto 12) and not finalmask) or
468 (r.addr(55 downto 12) and finalmask))
469 & r.pde(11 downto 0);
471 finalmask
= genmask(shift
, 44)
472 zero8
= SelectableInt(0, 8)
473 res
= selectconcat(zero8
,
474 (pde
[8:52] & ~finalmask
) |
#
475 (addr
[8:52] & finalmask
), #
481 # very quick test of maskgen function (TODO, move to util later)
482 if __name__
== '__main__':
483 shift
= SelectableInt(5, 6)
484 mask
= genmask(shift
, 43)
485 print (" mask", bin(mask
.value
))
487 mem
= Mem(row_bytes
=8)
488 mem
= RADIX(mem
, None)
489 # -----------------------------------------------
490 # |/|RTS1|/| RPDB | RTS2 | RPDS |
491 # -----------------------------------------------
492 # |0|1 2|3|4 55|56 58|59 63|
493 data
= SelectableInt(0, 64)
496 data
[59:64] = 0b01101 # mask
498 (rts
, mbits
, pgbase
) = mem
._decode
_prte
(data
)
499 print (" rts", bin(rts
.value
), rts
.bits
)
500 print (" mbits", bin(mbits
.value
), mbits
.bits
)
501 print (" pgbase", hex(pgbase
.value
), pgbase
.bits
)
502 addr
= SelectableInt(0x1000, 64)
503 check
= mem
._segment
_check
(addr
, mbits
, shift
)
504 print (" segment check", check
)