whitespace
[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
23 import math
24 import sys
25
26 # very quick, TODO move to SelectableInt utils later
27 def genmask(shift, size):
28 res = SelectableInt(0, size)
29 for i in range(size):
30 if i < shift:
31 res[size-1-i] = SelectableInt(1, 1)
32 return res
33
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
39
40 def rpte_valid(r):
41 return bool(r[0])
42
43 def rpte_leaf(r):
44 return bool(r[1])
45
46 def NLB(x):
47 """
48 Next Level Base
49 right shifted by 8
50 """
51 return x[4:55]
52
53 def NLS(x):
54 """
55 Next Level Size
56 NLS >= 5
57 """
58 return x[59:63]
59
60 """
61 Get Root Page
62
63 //Accessing 2nd double word of partition table (pate1)
64 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.1
65 // PTCR Layout
66 // ====================================================
67 // -----------------------------------------------
68 // | /// | PATB | /// | PATS |
69 // -----------------------------------------------
70 // 0 4 51 52 58 59 63
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.
76 //
77 // Thus, The Partition Table Base is obtained by (PATB << 12)
78 //
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).
82 //
83 // Partition Table
84 // ====================================================
85 // 0 PATE0 63 PATE1 127
86 // |----------------------|----------------------|
87 // | | |
88 // |----------------------|----------------------|
89 // | | |
90 // |----------------------|----------------------|
91 // | | | <-- effLPID
92 // |----------------------|----------------------|
93 // .
94 // .
95 // .
96 // |----------------------|----------------------|
97 // | | |
98 // |----------------------|----------------------|
99 //
100 // The effective LPID forms the index into the Partition Table.
101 //
102 // Each entry in the partition table contains 2 double words, PATE0, PATE1,
103 // corresponding to that partition.
104 //
105 // In case of Radix, The structure of PATE0 and PATE1 is as follows.
106 //
107 // PATE0 Layout
108 // -----------------------------------------------
109 // |1|RTS1|/| RPDB | RTS2 | RPDS |
110 // -----------------------------------------------
111 // 0 1 2 3 4 55 56 58 59 63
112 //
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.
117 //
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).
121 // Note: RPDS >= 5.
122 //
123 // PATE1 Layout
124 // -----------------------------------------------
125 // |///| PRTB | // | PRTS |
126 // -----------------------------------------------
127 // 0 3 4 51 52 58 59 63
128 //
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).
133 // Note: PRTS <= 24.
134 //
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
138 // be zero.
139
140
141 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.2
142 //
143 // Process Table
144 // ==========================
145 // 0 PRTE0 63 PRTE1 127
146 // |----------------------|----------------------|
147 // | | |
148 // |----------------------|----------------------|
149 // | | |
150 // |----------------------|----------------------|
151 // | | | <-- effPID
152 // |----------------------|----------------------|
153 // .
154 // .
155 // .
156 // |----------------------|----------------------|
157 // | | |
158 // |----------------------|----------------------|
159 //
160 // The effective Process id (PID) forms the index into the Process Table.
161 //
162 // Each entry in the partition table contains 2 double words, PRTE0, PRTE1,
163 // corresponding to that process
164 //
165 // In case of Radix, The structure of PRTE0 and PRTE1 is as follows.
166 //
167 // PRTE0 Layout
168 // -----------------------------------------------
169 // |/|RTS1|/| RPDB | RTS2 | RPDS |
170 // -----------------------------------------------
171 // 0 1 2 3 4 55 56 58 59 63
172 //
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.
177 //
178 // RPDB = Root Page Directory Base.
179 // RPDS = Root Page Directory Size right shifted by 3.
180 // Thus, Root page directory size = RPDS << 3.
181 // Note: RPDS >= 5.
182 //
183 // PRTE1 Layout
184 // -----------------------------------------------
185 // | /// |
186 // -----------------------------------------------
187 // 0 63
188 // All bits are reserved.
189
190
191 """
192
193 # see qemu/target/ppc/mmu-radix64.c for reference
194 class RADIX:
195 def __init__(self, mem, caller):
196 self.mem = mem
197 self.caller = caller
198 #TODO move to lookup
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"]
203
204 # cached page table stuff
205 self.pgtbl0 = 0
206 self.pt0_valid = False
207 self.pgtbl3 = 0
208 self.pt3_valid = False
209
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)
214
215 def ld(self, address, width=8, swap=True, check_in_mem=False):
216 print("RADIX: ld from addr 0x%x width %d" % (address, width))
217
218 mode = 'LOAD' # XXX TODO: executable load (icache)
219 addr = SelectableInt(address, 64)
220 (shift, mbits, pgbase) = self._decode_prte(addr)
221 #shift = SelectableInt(0, 32)
222
223 pte = self._walk_tree(addr, pgbase, mode, mbits, shift)
224 # use pte to caclculate phys address
225 return self.mem.ld(address, width, swap, check_in_mem)
226
227 # XXX set SPRs on error
228
229 # TODO implement
230 def st(self, address, v, width=8, swap=True):
231 print("RADIX: st to addr 0x%x width %d data %x" % (address, width, v))
232
233 mode = 'STORE'
234 addr = SelectableInt(address, 64)
235 (shift, mbits, pgbase) = self._decode_prte(addr)
236 pte = self._walk_tree(addr, pgbase, mode, mbits, shift)
237
238 # use pte to caclculate phys address (addr)
239 return self.mem.st(addr.value, v, width, swap)
240
241 # XXX set SPRs on error
242
243 def memassign(self, addr, sz, val):
244 print("memassign", addr, sz, val)
245 self.st(addr.value, val.value, sz, swap=False)
246
247 def _next_level(self,r):
248 return rpte_valid(r), rpte_leaf(r)
249 ## DSISR_R_BADCONFIG
250 ## read_entry
251 ## DSISR_NOPTE
252 ## Prepare for next iteration
253
254 def _walk_tree(self, addr, pgbase, mode, mbits, shift):
255 """walk tree
256
257 // vaddr 64 Bit
258 // vaddr |-----------------------------------------------------|
259 // | Unused | Used |
260 // |-----------|-----------------------------------------|
261 // | 0000000 | usefulBits = X bits (typically 52) |
262 // |-----------|-----------------------------------------|
263 // | |<--Cursize---->| |
264 // | | Index | |
265 // | | into Page | |
266 // | | Directory | |
267 // |-----------------------------------------------------|
268 // | |
269 // V |
270 // PDE |---------------------------| |
271 // |V|L|//| NLB |///|NLS| |
272 // |---------------------------| |
273 // PDE = Page Directory Entry |
274 // [0] = V = Valid Bit |
275 // [1] = L = Leaf bit. If 0, then |
276 // [4:55] = NLB = Next Level Base |
277 // right shifted by 8 |
278 // [59:63] = NLS = Next Level Size |
279 // | NLS >= 5 |
280 // | V
281 // | |--------------------------|
282 // | | usfulBits = X-Cursize |
283 // | |--------------------------|
284 // |---------------------><--NLS-->| |
285 // | Index | |
286 // | into | |
287 // | PDE | |
288 // |--------------------------|
289 // |
290 // If the next PDE obtained by |
291 // (NLB << 8 + 8 * index) is a |
292 // nonleaf, then repeat the above. |
293 // |
294 // If the next PDE is a leaf, |
295 // then Leaf PDE structure is as |
296 // follows |
297 // |
298 // |
299 // Leaf PDE |
300 // |------------------------------| |----------------|
301 // |V|L|sw|//|RPN|sw|R|C|/|ATT|EAA| | usefulBits |
302 // |------------------------------| |----------------|
303 // [0] = V = Valid Bit |
304 // [1] = L = Leaf Bit = 1 if leaf |
305 // PDE |
306 // [2] = Sw = Sw bit 0. |
307 // [7:51] = RPN = Real Page Number, V
308 // real_page = RPN << 12 -------------> Logical OR
309 // [52:54] = Sw Bits 1:3 |
310 // [55] = R = Reference |
311 // [56] = C = Change V
312 // [58:59] = Att = Physical Address
313 // 0b00 = Normal Memory
314 // 0b01 = SAO
315 // 0b10 = Non Idenmpotent
316 // 0b11 = Tolerant I/O
317 // [60:63] = Encoded Access
318 // Authority
319 //
320 """
321 # get sprs
322 print("_walk_tree")
323 pidr = self.caller.spr["PIDR"]
324 prtbl = self.caller.spr["PRTBL"]
325 print(pidr)
326 print(prtbl)
327 p = addr[55:63]
328 print("last 8 bits ----------")
329 print
330
331 # get address of root entry
332 prtable_addr = self._get_prtable_addr(shift, prtbl, addr, pidr)
333 print("prtable_addr",prtable_addr)
334
335 # read root entry - imcomplete
336 swap = False
337 check_in_mem = False
338 entry_width = 8
339 value = self.mem.ld(prtable_addr.value, entry_width, swap, check_in_mem)
340 print("value",value)
341
342 test_input = [
343 SelectableInt(0x8000000000000007, 64), #valid
344 SelectableInt(0xc000000000000000, 64) #exit
345 ]
346 index = 0
347
348 # walk tree starts on prtbl
349 while True:
350 print("nextlevel----------------------------")
351 l = test_input[index]
352 index += 1
353 valid,leaf = self._next_level(l)
354 if not leaf:
355 mbits = l[59:64]
356 print("mbits=")
357 print(mbits)
358 if mbits < 5 or mbits > 16:
359 print("badtree")
360 return None
361 """
362 mbits := unsigned('0' & data(4 downto 0));
363 if mbits < 5 or mbits > 16 or mbits > r.shift then
364 v.state := RADIX_FINISH;
365 v.badtree := '1'; -- throw error
366 else
367 v.shift := v.shift - mbits;
368 v.mask_size := mbits(4 downto 0);
369 v.pgbase := data(55 downto 8) & x"00"; NLB?
370 v.state := RADIX_LOOKUP; --> next level
371 end if;
372 """
373 print(valid)
374 print(leaf)
375 if not valid: return None
376 if leaf: return None
377
378 def _decode_prte(self, data):
379 """PRTE0 Layout
380 -----------------------------------------------
381 |/|RTS1|/| RPDB | RTS2 | RPDS |
382 -----------------------------------------------
383 0 1 2 3 4 55 56 58 59 63
384 """
385 # note that SelectableInt does big-endian! so the indices
386 # below *directly* match the spec, unlike microwatt which
387 # has to turn them around (to LE)
388 zero = SelectableInt(0, 1)
389 rts = selectconcat(zero,
390 data[56:59], # RTS2
391 data[1:3], # RTS1
392 )
393 masksize = data[59:64] # RPDS
394 mbits = selectconcat(zero, masksize)
395 pgbase = selectconcat(data[8:56], # part of RPDB
396 SelectableInt(0, 16),)
397
398 return (rts, mbits, pgbase)
399
400 def _segment_check(self, addr, mbits, shift):
401 """checks segment valid
402 mbits := '0' & r.mask_size;
403 v.shift := r.shift + (31 - 12) - mbits;
404 nonzero := or(r.addr(61 downto 31) and not finalmask(30 downto 0));
405 if r.addr(63) /= r.addr(62) or nonzero = '1' then
406 v.state := RADIX_FINISH;
407 v.segerror := '1';
408 elsif mbits < 5 or mbits > 16 or mbits > (r.shift + (31 - 12)) then
409 v.state := RADIX_FINISH;
410 v.badtree := '1';
411 else
412 v.state := RADIX_LOOKUP;
413 """
414 # note that SelectableInt does big-endian! so the indices
415 # below *directly* match the spec, unlike microwatt which
416 # has to turn them around (to LE)
417 mask = genmask(shift, 44)
418 nonzero = addr[1:32] & mask[13:44] # mask 31 LSBs (BE numbered 13:44)
419 print ("RADIX _segment_check nonzero", bin(nonzero.value))
420 print ("RADIX _segment_check addr[0-1]", addr[0].value, addr[1].value)
421 if addr[0] != addr[1] or nonzero == 1:
422 return "segerror"
423 limit = shift + (31 - 12)
424 if mbits < 5 or mbits > 16 or mbits > limit:
425 return "badtree"
426 new_shift = shift + (31 - 12) - mbits
427 return new_shift
428
429 def _check_perms(self, data, priv, iside, store):
430 """check page permissions
431 // Leaf PDE |
432 // |------------------------------| |----------------|
433 // |V|L|sw|//|RPN|sw|R|C|/|ATT|EAA| | usefulBits |
434 // |------------------------------| |----------------|
435 // [0] = V = Valid Bit |
436 // [1] = L = Leaf Bit = 1 if leaf |
437 // PDE |
438 // [2] = Sw = Sw bit 0. |
439 // [7:51] = RPN = Real Page Number, V
440 // real_page = RPN << 12 -------------> Logical OR
441 // [52:54] = Sw Bits 1:3 |
442 // [55] = R = Reference |
443 // [56] = C = Change V
444 // [58:59] = Att = Physical Address
445 // 0b00 = Normal Memory
446 // 0b01 = SAO
447 // 0b10 = Non Idenmpotent
448 // 0b11 = Tolerant I/O
449 // [60:63] = Encoded Access
450 // Authority
451 //
452 -- test leaf bit
453 -- check permissions and RC bits
454 perm_ok := '0';
455 if r.priv = '1' or data(3) = '0' then
456 if r.iside = '0' then
457 perm_ok := data(1) or (data(2) and not r.store);
458 else
459 -- no IAMR, so no KUEP support for now
460 -- deny execute permission if cache inhibited
461 perm_ok := data(0) and not data(5);
462 end if;
463 end if;
464 rc_ok := data(8) and (data(7) or not r.store);
465 if perm_ok = '1' and rc_ok = '1' then
466 v.state := RADIX_LOAD_TLB;
467 else
468 v.state := RADIX_FINISH;
469 v.perm_err := not perm_ok;
470 -- permission error takes precedence over RC error
471 v.rc_error := perm_ok;
472 end if;
473 """
474 # check permissions and RC bits
475 perm_ok = 0
476 if priv == 1 or data[60] == 0:
477 if iside == 0:
478 perm_ok = data[62] | (data[61] & (store == 0))
479 # no IAMR, so no KUEP support for now
480 # deny execute permission if cache inhibited
481 perm_ok = data[63] & ~data[58]
482 rc_ok = data[55] & (data[56] | (store == 0))
483 if perm_ok == 1 and rc_ok == 1:
484 return True
485 return "perm_err" if perm_ok == 0 else "rc_err"
486
487 def _get_prtable_addr(self, shift, prtbl, addr, pid):
488 """
489 if r.addr(63) = '1' then
490 effpid := x"00000000";
491 else
492 effpid := r.pid;
493 end if;
494 x"00" & r.prtbl(55 downto 36) &
495 ((r.prtbl(35 downto 12) and not finalmask(23 downto 0)) or
496 (effpid(31 downto 8) and finalmask(23 downto 0))) &
497 effpid(7 downto 0) & "0000";
498 """
499 print ("_get_prtable_addr_", shift, prtbl, addr, pid)
500 finalmask = genmask(shift, 44)
501 finalmask24 = finalmask[20:44]
502 if addr[0].value == 1:
503 effpid = SelectableInt(0, 32)
504 else:
505 effpid = pid #self.pid # TODO, check on this
506 zero16 = SelectableInt(0, 16)
507 zero4 = SelectableInt(0, 4)
508 res = selectconcat(zero16,
509 prtbl[8:28], #
510 (prtbl[28:52] & ~finalmask24) | #
511 (effpid[0:24] & finalmask24), #
512 effpid[24:32],
513 zero4
514 )
515 return res
516
517 def _get_pgtable_addr(self, mask_size, pgbase, addrsh):
518 """
519 x"00" & r.pgbase(55 downto 19) &
520 ((r.pgbase(18 downto 3) and not mask) or (addrsh and mask)) &
521 "000";
522 """
523 mask16 = genmask(mask_size+5, 16)
524 zero8 = SelectableInt(0, 8)
525 zero3 = SelectableInt(0, 3)
526 res = selectconcat(zero8,
527 pgbase[8:45], #
528 (prtbl[45:61] & ~mask16) | #
529 (addrsh & mask16), #
530 zero3
531 )
532 return res
533
534 def _get_pte(self, shift, addr, pde):
535 """
536 x"00" &
537 ((r.pde(55 downto 12) and not finalmask) or
538 (r.addr(55 downto 12) and finalmask))
539 & r.pde(11 downto 0);
540 """
541 finalmask = genmask(shift, 44)
542 zero8 = SelectableInt(0, 8)
543 res = selectconcat(zero8,
544 (pde[8:52] & ~finalmask) | #
545 (addr[8:52] & finalmask), #
546 pde[52:64],
547 )
548 return res
549
550
551 # very quick test of maskgen function (TODO, move to util later)
552 if __name__ == '__main__':
553 # set up dummy minimal ISACaller
554 spr = {'DSISR': SelectableInt(0, 64),
555 'DAR': SelectableInt(0, 64),
556 'PIDR': SelectableInt(0, 64),
557 'PRTBL': SelectableInt(0, 64)
558 }
559 class ISACaller: pass
560 caller = ISACaller()
561 caller.spr = spr
562
563 shift = SelectableInt(5, 6)
564 mask = genmask(shift, 43)
565 print (" mask", bin(mask.value))
566
567 mem = Mem(row_bytes=8)
568 mem = RADIX(mem, caller)
569 # -----------------------------------------------
570 # |/|RTS1|/| RPDB | RTS2 | RPDS |
571 # -----------------------------------------------
572 # |0|1 2|3|4 55|56 58|59 63|
573 data = SelectableInt(0, 64)
574 data[1:3] = 0b01
575 data[56:59] = 0b11
576 data[59:64] = 0b01101 # mask
577 data[55] = 1
578 (rts, mbits, pgbase) = mem._decode_prte(data)
579 print (" rts", bin(rts.value), rts.bits)
580 print (" mbits", bin(mbits.value), mbits.bits)
581 print (" pgbase", hex(pgbase.value), pgbase.bits)
582 addr = SelectableInt(0x1000, 64)
583 check = mem._segment_check(addr, mbits, shift)
584 print (" segment check", check)
585
586 print("walking tree")
587 # addr = unchanged
588 # pgbase = None
589 mode = None
590 #mbits = None
591 shift = rts
592 result = mem._walk_tree(addr, pgbase, mode, mbits, shift)
593 print(result)