670ac87a7c410aa1581beea767e0100477dd22bd
[libresoc-isa-manual.git] / powerpc-add / src / isamux.tex
1 % ISAMUX
2
3 \chapter{Introduction}
4
5 \paragraph{}
6
7 A fixed number of additional (hidden) bits, conceptually a \textbf{namespace},
8 set by way of a CSR or other out-of-band mechanism,
9 that go directly and non-optionally
10 into the instruction decode phase, extending (in each implementation) the
11 opcode length to 16+N, 32+N, 48+N, where N is a hard fixed quantity on
12 a per-implementor basis.
13
14 \paragraph{}
15
16 Where the opcode is normally loaded from the location at the PC, the extra
17 bits, set via a CSR, are mandatorially appended to every instruction: hence why
18 they are described as "hidden" opcode bits, and as a \textbf{namespace}.
19
20 \paragraph{}
21
22 The parallels with c++ \textbf{using namespace} are direct and clear.
23 Alternative conceptual ways to understand this concept include
24 \textbf{escape-sequencing}.
25
26 \paragraph{}
27
28 TODO: reserve some bits which permit the namespace \textbf{escape-sequence} to
29 be relevant for a fixed number of instructions at a time. Caveat:
30 allowing such a countdown to cross branch-points is unwise (illegal
31 instruction?)
32
33 \paragraph{}
34
35 An example of a pre-existing \textbf{namespace} switch that has been in
36 prevalent use for several decades (SPARC and other architectures):
37 dynamic runtime selectability of littel-endian / big-endian \textbf{meaning}
38 of instructions by way of a \textbf{mode switch} instruction (of some kind).
39
40 \paragraph{}
41
42 That \textbf{switch} is in effect a 33rd (hidden) bit that is part of the opcode,
43 going directly into the mux / decode phase of instruction decode, and
44 thus qualifies categorically as a \textbf{namespace}. This proposal both formalises
45 and generalises that concept.
46
47 \section{Hypothetical Format} \label{hypotheticalformat}
48
49 \paragraph{}
50
51 Note that this is a hypothetical format, yet TBD, where particular attention
52 needs to be paid to the fact that there is an \textbf{immediate} version of CSRRW
53 (with 5 bits of immediate) that could save a lot of space in binaries.
54
55 \begin{verbatim}
56 3 2 1
57 |1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0|
58 |------------------------------ |-------|---------------------|-|
59 |1 custom custom custom custom custom | foreignarch |1|
60 |0 reserved reserved reserved reserved reserved | foreignarch |1|
61 |custom | reserved | official|B| rvcpage |0|
62 \end{verbatim}
63
64 \paragraph{}
65
66 RV Mode
67
68 \begin{itemize}
69 \parskip 0pt
70 \itemsep 1pt
71
72 \item
73
74 when bit 0 is 0, \textbf{RV} mode is selected.
75
76 \item
77
78 in RV mode, bits 1 thru 5 provide up to 16 possible alternative meanings
79 (namespaces) for 16 Bit opcodes. \textbf{pages} if you will. The top bit
80 indicates custom meanings. When set to 0, the top bit is for official usage.
81
82 \item
83
84 Bits 15 thru 23 are reserved.
85
86 \item
87
88 Bits 24 thru 31 are for custom usage.
89
90 \item
91
92 bit 6 (\textbf{B}) is endian-selection: LE/BE
93
94 \end{itemize}
95
96 \paragraph{}
97
98 16 bit page examples:
99
100 \begin{itemize}
101 \parskip 0pt
102 \itemsep 1pt
103
104 \item
105
106 0b0000 STANDARD (2019) RVC
107
108 \item
109
110 0b0001 RVCv2
111
112 \item
113
114 0b0010 RV16
115
116 \item
117
118 0b0011 RVCv3
119
120 \item
121
122 ...
123
124 \item
125
126 0b1000 custom 16 bit opcode meanings 1
127
128 \item
129
130 0b1001 custom 16 bit opcode meanings 2
131
132 \item
133
134 .....
135
136 \end{itemize}
137
138 \paragraph{}
139
140 Foreign Arch Mode
141
142 \begin{itemize}
143 \parskip 0pt
144 \itemsep 1pt
145
146 \item
147
148 when bit 0 is 1, \textbf{Foreign arch} mode is selected.
149
150 \item
151
152 Bits 1 thru 7 are a table of foreign arches.
153
154 \item
155
156 when the MSB is 1, this is for custom use.
157
158 \item
159
160 when the MSB is 0, bits 1 thru 6 are reserved for 64 possible official foreign archs.
161
162 \end{itemize}
163
164 \paragraph{}
165
166 Foreign archs could be (examples):
167
168 \begin{itemize}
169 \parskip 0pt
170 \itemsep 1pt
171
172 \item
173
174 0b0000000 x86\_32
175
176 \item
177
178 0b0000001 x86\_64
179
180 \item
181
182 0b0000010 MIPS32
183
184 \item
185
186 0b0000011 MIPS64
187
188 \item
189
190 ....
191
192 \item
193
194 0b0010000 Java Bytecode
195
196 \item
197
198 0b0010001 N.E.Other Bytecode
199
200 \item
201
202 ....
203
204 \item
205
206 0b1000000 custom foreign arch 1
207
208 \item
209
210 0b1000001 custom foreign arch 2
211
212 \item
213
214 ....
215
216 \end{itemize}
217
218 \paragraph{}
219
220 Note that \textbf{official} foreign archs have a binary value where the MSB is zero,
221 and custom foreign archs have a binary value where the MSB is 1.
222
223 \section{Namespaces are permitted to swap to new state} \label{stateswap}
224
225 \paragraph{}
226
227 In each privilege level, on a change of ISANS (whether through manual setting
228 of ISANS or through trap entry or exit changing the ISANS CSR), an
229 implementation is permitted to completely and arbitrarily switch not only the
230 instruction set, it is permitted to switch to a new bank of CSRs (or a subset
231 of the same), and even to switch to a new PC.
232
233 \paragraph{}
234
235 This to occur immediately and atomically at the point at which the change in ISANS occurs.
236
237 \paragraph{}
238
239 The most obvious application of this is for Foreign Archs, which may have their
240 own completely separate PC. Thus, foreign assembly code and RISCV assembly code
241 need not be mixed in the same binary.
242
243 \paragraph{}
244
245 Further use-cases may be envisaged however great care needs to be taken to not
246 cause massive complications for JIT emulation, as the RV ISANS is unary encoded
247 (2\^31 permutations).
248
249 \paragraph{}
250
251 In addition, the state information of \textbf{all} namespaces has to be saved
252 and restored on a context-switch (unless the SP is also switched as part of the
253 state!) which is quite severely burdensome and getting exceptionally complex.
254
255 \paragraph{}
256
257 Switching CSR, PC (and potentially SP) and other state on a NS change in the
258 RISCV unary NS therefore needs to be done wisely and responsibly, i.e.
259 minimised!
260
261 \paragraph{}
262
263 To be discussed. Context
264 href=https://groups.google.com/a/groups.riscv.org/d/msg/isa-dev/x-uFZDXiOxY/27QDW5KvBQAJ
265
266
267 \section{Privileged Modes / Traps} \label{privtraps}
268
269 \paragraph{}
270
271 An additional WLRL CSR per priv-level named \textbf{LAST-ISANS} is required, and
272 another called \textbf{TRAP-ISANS}
273 These mirrors the ISANS CSR, and, on a trap, the current ISANS in
274 that privilege level is atomically
275 transferred into LAST-ISANS by the hardware, and ISANS in that trap
276 is set to TRAP-ISANS. Hardware is \textbf{only then} permitted to modify the PC to
277 begin execution of the trap.
278
279 \paragraph{}
280
281 On exit from the trap, LAST-ISANS is copied into the ISANS CSR, and
282 LAST-ISANS is set to TRAP-ISANS. \textbf{Only then} is the hardware permitted
283 to modify the PC to begin execution where the trap left off.
284
285 \paragraph{}
286
287 This is identical to how xepc is handled.
288
289 \paragraph{}
290
291 Note 1: in the case of Supervisor Mode (context switches in particular),
292 saving and changing of LAST-ISANS (to and from the stack) must be done
293 atomically and under the protection of the SIE bit. Failure to do so
294 could result in corruption of LAST-ISANS when multiple traps occur in
295 the same privilege level.
296
297 \paragraph{}
298
299 Note 2: question - should the trap due to illegal (unsupported) values
300 written into LAST-ISANS occur when the \textbf{software} writes to LAST-ISANS,
301 or when the \textbf{trap} (on exit) writes into LAST-ISANS? this latter seems
302 fraught: a trap, on exit, causing another trap??
303
304 \paragraph{}
305
306 Per-privilege-level pseudocode (there exists UISANS, UTRAPISANS, ULASTISANS,
307 MISANS, MTRAPISANS, MLASTISANS and so on):
308
309 \begin{verbatim}
310 trap_entry()
311 {
312 LAST-ISANS = ISANS // record the old NS
313 ISANS = TRAP_ISANS // traps are executed in "trap" NS
314 }
315
316 and trap_exit:
317
318 trap_exit():
319 {
320 ISANS = LAST-ISANS
321 LAST-ISANS = TRAP_ISANS
322 }
323 \end{verbatim}
324
325 \section{Alternative RVC 16 Bit Opcode meanings} \label{alternativervc16bitopcodemeanings}
326
327 \paragraph{}
328
329 Here is appropriate to raise an idea how to cover RVC and future
330 variants, including RV16.
331
332 \paragraph{}
333
334 Just as with foreign archs, and you quite rightly highlight above, it
335 makes absolutely no sense to try to select both RVCv1, v2, v3 and so on,
336 all simultaneously. An unary bit vector for RVC modes, changing the 16
337 BIT opcode space meaning, is wasteful and again has us believe that WARL
338 is the \textbf{solution}.
339
340 \paragraph{}
341
342 The correct thing to do is, again, just like with foreign archs, to
343 treat RVCs as a \textbf{binary} namespace selector. Bits 1 thru 3 would give
344 8 possible completely new alternative meanings, just like how the Z80
345 and the 286 and 386 used to do bank switching.
346
347 \paragraph{}
348
349 All zeros is clearly reserved for the present RVC. 0b001 for RVCv2. 0b010
350 for RV16 (look it up) and there should definitely be room reserved here
351 for custom reencodings of the 16 bit opcode space.
352
353 \section{FAQ}\label{faq}
354
355 \subsection{Why not have TRAP-ISANS as a vector table, matching mtvec?} \label{trap-isans-vec}
356
357 \paragraph{}
358
359 Use case to be determined. Rather than be a global per-priv-level value,
360 TRAP-ISANS is a table of length exactly equal to the mtvec/utvec/stvec table,
361 with corresponding entries that specify the assembly-code namespace in which
362 the trap handler routine is written.
363
364 \paragraph{}
365
366 Open question: see https://groups.google.com/a/groups.riscv.org/d/msg/isa-dev/IAhyOqEZoWA/BM0G3J2zBgAJ
367
368 \begin{verbatim}
369 trap_entry(x_cause)
370 {
371 LAST-ISANS = ISANS // record the old NS
372 ISANS = TRAP_ISANS_VEC[xcause] // traps are executed in "trap" NS
373 }
374
375 and trap_exit:
376
377 trap_exit(x_cause):
378 {
379 ISANS = LAST-ISANS
380 LAST-ISANS = TRAP_ISANS_VEC[x_cause]
381 }
382 \end{verbatim}
383
384 \subsection{Is this like MISA?} \label{misa}
385
386 \paragraph{}
387
388 No.
389
390 \begin{itemize}
391 \parskip 0pt
392 \itemsep 1pt
393
394 \item
395
396 MISA's space is entirely taken up (and running out).
397
398 \item
399
400 There is no allocation (provision) for custom extensions.
401
402 \item
403
404 MISA switches on and off entire extensions: ISAMUX/NS may be used to switch
405 multiple opcodes (present and future), to alternate meanings.
406
407 \item
408
409 MISA is WARL and is inaccessible from everything but M-Mode (not even readable).
410
411 \end{itemize}
412
413 \paragraph{}
414
415 MISA is therefore wholly unsuited to U-Mode usage; ISANS is specifically
416 permitted to be called by userspace to switch (with no stalling) between
417 namespaces, repeatedly and in quick succession.
418
419 \subsection{What happens if this scheme is not adopted? Why is it better than leaving things well alone?} \label{laissezfaire}
420
421 \paragraph{}
422
423 At the first sign of an emergency non-backwards compatible and unavoidable
424 change to the \textbf{frozen} RISCV \textbf{official} Standards, the entire RISCV
425 community is fragmented and divided into two:
426
427 \begin{itemize}
428 \parskip 0pt
429 \itemsep 1pt
430
431 \item
432
433 Those vendors that are hardware compatible with the legacy standard.
434
435 \item
436
437 Those that are compatible with the new standard.
438
439 \end{itemize}
440
441 \paragraph{}
442
443 \textbf{These two communities would be mutually exclusively incompatible}. If
444 a second emergency occurs, RISCV becomes even less tenable.
445
446 \paragraph{}
447
448 Hardware that wished to be \textbf{compatible} with either flavour would require
449 JIT or offline static binary recompilation. No vendor would willingly
450 accept this as a condition of the standards divergence in the first place,
451 locking up decision making to the detriment of RISCV as a whole.
452
453 \paragraph{}
454
455 By providing a \textbf{safety valve} in the form of a hidden namespace, at least
456 newer hardware has the option to implement both (or more) variations,
457 \textbf{and still apply for Certification}.
458
459 \paragraph{}
460
461 However to also allow \textbf{legacy} hardware to at least be JIT soft
462 compatible, some very strict rules \textbf{must} be adhered to, that appear at
463 first sight not to make any sense.
464
465 \paragraph{}
466
467 It's complicated in other words!
468
469 \subsection{Surely it's okay to just tell people to use 48-bit encodings?} \label{use48bit}
470
471 \paragraph{}
472
473 Short answer: it doesn't help resolve conflicts, and costs hardware and
474 redesigns to do so. Softcores in cost-sensitive embedded applications may
475 even not actually be able to fit the required 48 bit instruction decode engine
476 into a (small, ICE40) FPGA. 48-bit instruction decoding is much more complex
477 than straight 32-bit decoding, requiring a queue.
478
479 \paragraph{}
480
481 Second answer: conflicts can still occur in the (unregulated, custom) 48-bit
482 space, which \textbf{could} be resolved by ISAMUX/ISANS as applied to the \textbf{48} bit
483 space in exactly the same way. And the 64-bit space.
484
485 \subsection{Why not leave this to individual custom vendors to solve on a case by case basis?} \label{case-by-case}
486
487 \paragraph{}
488
489 The suggestion was raised that a custom extension vendor could create
490 their own CSR that selects between conflicting namespaces that resolve
491 the meaning of the exact same opcode. This to be done by all and any
492 vendors, as they see fit, with little to no collaboration or coordination
493 towards standardisation in any form.
494
495 \paragraph{}
496
497 The problems with this approach are numerous, when presented to a
498 worldwide context that the UNIX Platform, in particular, has to face
499 (where the embedded platform does not)
500
501 \paragraph{}
502
503 First: lack of coordination, in the proliferation of arbitrary solutions,
504 has to primarily be borne by gcc, binutils, LLVM and other compilers.
505
506 \paragraph{}
507
508 Secondly: CSR space is precious. With each vendor likely needing only one
509 or two bits to express the namespace collision avoidance, if they make
510 even a token effort to use worldwide unique CSRs (an effort that would
511 benefit compiler writers), the CSR register space is quickly exhausted.
512
513 \paragraph{}
514
515 Thirdly: JIT Emulation of such an unregulated space becomes just as
516 much hell as it is for compiler writers. In addition, if two vendors
517 use conflicting CSR addresses, the only sane way to tell the emulator
518 what to do is to give the emulator a runtime commandline argument.
519
520 \paragraph{}
521
522 Fourthly: with each vendor coming up with their own way of handling
523 conflicts, not only are the chances of mistakes higher, it is against the
524 very principles of collaboration and cooperation that save vendors money
525 on development and ongoing maintenance. Each custom vendor will have
526 to maintain their own separate hard fork of the toolchain and software,
527 which is well known to result in security vulnerabilities.
528
529 \paragraph{}
530
531 By coordinating and managing the allocation of namespace bits (unary
532 or binary) the above issues are solved. CSR space is no longer wasted,
533 compiler and JIT software writers have an easier time, clashes are
534 avoided, and RISCV is stabilised and has a trustable long term future.
535
536 \subsection{ Why ISAMUX / ISANS has to be WLRL and mandatory trap on illegal writes} \label{wlrlmandatorytrap}
537
538 \paragraph{}
539
540 The namespaces, set by bits in the CSR, are functionally directly
541 equivalent to c++ namespaces, even down to the use of braces.
542
543 \paragraph{}
544
545 WARL, by allowing implementors to choose the value, prevents and prohibits
546 the critical and necessary raising of an exception that would begin the
547 JIT process in the case of ongoing standards evolution.
548
549 \paragraph{}
550
551 Without this opportunity, an implementation has no reliable guaranteed way of knowing
552 when to drop into full JIT mode,
553 which is the only guaranteed way to distinguish
554 any given conflicting opcode. It is as if the c++
555 standard was given a similar optional
556 opportunity to completely ignore the
557 \textbf{using namespace} prefix!
558
559 \paragraph{}
560
561 --
562
563 \paragraph{}
564
565 Ok so I trust it's now clear why WLRL (thanks Allen) is needed.
566
567 \paragraph{}
568
569 When Dan raised the WARL concern initially a situation was masked by
570 the conflict, that if gone unnoticed would jeapordise ISAMUX/ISANS
571 entirely. Actually, two separate errors. So thank you for raising the
572 question.
573
574 \paragraph{}
575
576 The situation arises when foreign archs are to be given their own NS
577 bit. MIPS is allocated bit 8, x86 bit 9, whilst LE/BE is given bit 0,
578 RVCv2 bit 1 andso on. All of this potential rather than actual, clearly.
579
580 \paragraph{}
581
582 Imagine then that software tries to write and set not just bit 8 and
583 bit 9, it also tries to set bit 0 and 1 as well.
584
585 \paragraph{}
586
587 This \textbf{IS} on the face of it a legitimate reason to make ISAMUX/ISANS WARL.
588
589 \paragraph{}
590
591 However it masks a fundamental flaw that has to be addressed, which
592 brings us back much closer to the original design of 18 months ago,
593 and it's highlighted thus:
594
595 \paragraph{}
596
597 x86 and simultaneous RVCv2 modes are total nonsense in the first place!
598
599 \paragraph{}
600
601 The solution instead is to have a NS bit (bit0) that SPECIFICALLY
602 determines if the arch is RV or not. If 0, the rest of the ISAMUX/ISANS
603 is very specifically RV \textbf{only}, and if 1, the ISAMUX/ISANS is a \textbf{binary}
604 table of foreign architectures and foreign architectures only.
605
606 \paragraph{}
607
608 Exactly how many bits are used for the foreign arch table, is to
609 be determined. 7 bits, one of which is reserved for custom usage,
610 leaving a whopping 64 possible \textbf{official} foreign instruction sets to
611 be hardware-supported/JIT-emulated seems to be sufficiently gratuitous,
612 to me.
613
614 \paragraph{}
615
616 One of those could even be Java Bytecode!
617
618 \paragraph{}
619
620 Now, it could \textbf{hypothetically} be argued that the permutation of setting
621 LE/BE and MIPS for example is desirable. A simple analysis shows this
622 not to be the case: once in the MIPS foreign NS, it is the MIPS hardware
623 implementation that should have its own way of setting and managing its
624 LE/BE mode, because to do otherwise drastically interferes with MIPS
625 binary compatibility.
626
627 \paragraph{}
628
629 Thus, it is officially Not Our Problem: only flipping into one foreign
630 arch at a time makes sense, thus this has to be reflected in the
631 ISAMUX/ISANS CSR itself, completely side-stepping the (apparent) need
632 to make the NS CSR WARL (which would not work anyway, as previously
633 mentioned).
634
635 \paragraph{}
636
637 So, thank you, again, Dan, for raising this. It would have completely
638 jeapordised ISAMUX/NS if not spotted.
639
640 \paragraph{}
641
642 The second issue is: how does any hardware system, whether it support
643 ISANS or not, and whether any future hardware supports some Namespaces
644 and, in a transitive fashion, has to support \textbf{more} future namespaces,
645 through JIT emulation, if this is not planned properly in advance?
646
647 \paragraph{}
648
649 Let us take the simple case first: a current 2019 RISCV fully compliant
650 RV64GC UNIX capable system (with mandatory traps on all unsupported CSRs).
651
652 \paragraph{}
653
654 Fast forward 20 years, there are now 5 ISAMUX/NS unary bits, and 3
655 foreign arch binary table entries.
656
657 \paragraph{}
658
659 Such a system is perfectly possible of software JIT emulating ALL of these
660 options because the write to the (illegal, for that system) ISAMUX/NS
661 CSR generates the trap that is needed for that system ti begin JIT mode.
662
663 \paragraph{}
664
665 (This again emphasises exactly why the trap is mandatory).
666
667 \paragraph{}
668
669 Now let us take the case of a hypothetical system from say 2021 that
670 implements RVCv2 at the hardware level.
671
672 \paragraph{}
673
674 Fast forward 20 years: if the CSR were made WARL, that system would be
675 absolutely screwed. The implementor would be under the false impression
676 that ignoring setting of \textbf{illegal} bits was acceptable, making the
677 transition to JIT mode flat-out impossible to detect.
678
679 \paragraph{}
680
681 When this is considered transitively, considering all future additions to
682 the NS, and all permutations, it can be logically deduced that there is
683 a need to reserve a \textbf{full} set of bits in the ISAMUX/NS CSR \textbf{in advance}.
684
685 \paragraph{}
686
687 i.e. that \textbf{right now}, in the year 2019, the entire ISAMUX/NS CSR cannot
688 be added to piecemeal, the full 32 (or 64) bits \textbf{has} to be reserved,
689 and reserved bits set at zero.
690
691 \paragraph{}
692
693 Furthermore, if any software attempts to write to those reserved bits,
694 it \textbf{must} be treated just as if those bits were distinct and nonexistent
695 CSRs, and a trap raised.
696
697 \paragraph{}
698
699 It makes more sense to consider each NS as having its own completely
700 separate CSR, which, if it does not exist, clearly it should be obvious
701 that, as an unsupported CSR, a trap should be raised (and JIT emulation
702 activated).
703
704 \paragraph{}
705
706 However given that only the one bit is needed (in RV NS Mode, not
707 Foreign NS Mode), it would be terribly wasteful of the CSRs to do this,
708 despite it being technically correct and much easier to understand why
709 trap raising is so essential (mandatory).
710
711 \paragraph{}
712
713 This again should emphasise how to mentally get one's head round this
714 mind-bendingly complex problem space: think of each NS bit as its own
715 totally separate CSR that every implementor is free and clear to implement
716 (or leave to JIT Emulation) as they see fit.
717
718 \paragraph{}
719
720 Only then does the mandatory need to trap on write really start to hit
721 home, as does the need to preallocate a full set of reserved zero values
722 in the RV ISAMUX/NS.
723
724 \paragraph{}
725
726 Lastly, I \textbf{think} it's ok to only reserve say 32 bits, and, in 50 years
727 time if that genuinely is not enough, start the process all over again
728 with a new CSR. ISAMUX2/NS2.
729
730 \paragraph{}
731
732 Subdivision of the RV NS (support for RVCv3/4/5/RV16 without wasting
733 precious CSR bits) best left for discussion another time, the above is
734 a heck of a lot to absorb, already.
735
736 \subsection{Why WARL will not work and why WLRL is required}
737
738 \paragraph{}
739
740 WARL requires a follow-up read of the CSR to ascertain what heuristic
741 the hardware \textbf{might} have applied, and if that procedure is followed in
742 this proposal, performance even on hardware would be severely compromised.
743
744 \paragraph{}
745
746 In addition when switching to foreign architectures, the switch has to
747 be done atomically and guaranteed to occur.
748
749 \paragraph{}
750
751 In the case of JIT emulation, the WARL \textbf{detection} code will be in an
752 assembly language that is alien to hardware.
753
754 \paragraph{}
755
756 Support for both assembly languages immediately after the CSR write
757 is clearly impossible, this leaves no other option but to have the CSR
758 be WLRL (on all platforms) and for traps to be mandatory (on the UNIX
759 Platform).
760
761 \subsection{Is it strictly necessary for foreign archs to switch back?} \label{foreignswitch}
762
763 \paragraph{}
764
765 No, because LAST-ISANS handles the setting and unsetting of the ISANS CSR
766 in a completely transparent fashion as far as the foreign arch is concerned.
767 Supervisor or Hypervisor traps take care of the context switch in a way
768 that the user mode (or guest) need not be aware of, in any way.
769
770 \paragraph{}
771
772 Thus, in e.g. Hypervisor Mode, the foreign guest arch has no knowledge
773 or need to know that the hypervisor is flipping back to RV at the time of
774 a trap.
775
776 \paragraph{}
777
778 Note however that this is \textbf{not} the same as the foreign arch executing
779 \textbf{foreign} traps! Foreign architecture trap and interrupt handling mechanisms
780 are \textbf{out of scope} of this document and MUST be handled by the foreign
781 architecture implementation in a completely transparent fashion that in
782 no way interacts or interferes with this proposal.
783
784 \subsection{Can we have dynamic declaration and runtime declaration of capabilities?} \label{dynamic}
785
786 \paragraph{}
787
788 Answer: don't know (yet). Quoted from Rogier:
789
790 \begin{quote}
791 "A SOC may have several devices that one may want to directly control
792 with custom instructions. If independent vendors use the same opcodes you
793 either have to change the encodings for every different chip (not very
794 nice for software) or you can give the device an ID which is defined in
795 some device tree or something like that and use that."
796 \end{quote}
797
798 \paragraph{}
799
800 dynamic detection wasn't originally planned: static
801 compilation was envisaged to solve the need, with a table of
802 mvendorid-marchid-isamux/isans being maintained inside gcc / binutils /
803 llvm (or separate library?) that, like the linux kernel ARCH table,
804 requires a world-wide atomic \textbf{git commit} to add globally-unique
805 registered entries that map functionality to actual namespaces.
806
807 \paragraph{}
808
809 where that goes wrong is if there is ever a pair (or more) of vendors
810 that use the exact same custom feature that maps to different opcodes,
811 a statically-compiled binary has no hope of executing natively on both
812 systems.
813
814 \paragraph{}
815
816 at that point: yes, something akin to device-tree would be needed.
817
818 \section{Open Questions}\label{open-questions}
819
820 \paragraph{}
821
822 This section from a post by Rogier Bruisse
823 \href{http://hands.com/~lkcl/gmail_re_isadev_isamux.html}{http://hands.com/~lkcl/gmail\_re\_isadev\_isamux.html}
824
825 \subsection{is the ISANS CSR a 32 or XLEN bit value?} \label{isans-32-or-xlen}
826
827 \paragraph{}
828
829 This is partly answered in another FAQ above: if 32 bits is not enough
830 for a full suite of official, custom-with-atomic-registration and custom-without
831 then a second CSR group (ISANS2) may be added at a future date (10-20 years
832 hence).
833
834 \paragraph{}
835
836 32 bits would not inconvenience RV32, and implementors wishing to
837 make significant altnernative modifications to opcodes in the RV32 ISA space
838 could do so without the burden of having to support a split 32/LO 32/HI
839 CSR across two locations.
840
841 \subsection{Is the ISANS a flat number space or should some bits be reserved for use as flags?}
842
843 \paragraph{}
844
845 See 16-bit RV namespace "page" concept, above. Some bits have to be unary
846 (multiple simultaneous features such as LE/BE in one bit, and augmented
847 Floating-point rounding / clipping in another), whilst others definitely
848 need to be binary (the most obvious one being \textbf{paging} in the space currently
849 occupied by RVC).
850
851 \subsection{Should the ISANS space be partitioned between reserved, custom with registration guaranteed non clashing, custom, very likely non clashing?}
852
853 \paragraph{}
854
855 Yes. Format TBD.
856
857 \subsection{Should only compiler visible/generated constant setting with CSRRWI and/or using a clearly recognisable LI/LUI be accommodated or should dynamic setting be accommodated as well?}
858
859 \paragraph{}
860
861 This is almost certainly a software design issue, not so much a hardware
862 issue.
863
864 \subsection{How should the ISANS be (re)stored in a trap and in context switch?}
865
866 \paragraph{}
867
868 See section above on privilege mode: LAST-ISANS has been introduced that
869 mirrors (x)CAUSE and (x)EPC pretty much exactly. Context switches change
870 uepc just before exit from the trap, in order to change the user-mode PC
871 to switch to a new process, and ulast-isans can - must - be treated in
872 exactly the same way. When the context switch sets ulast-isans (and uepc),
873 the hardware flips both ulast-isans into uisans and uepc into pc (atomically):
874 both the new NS and the new PC activate immediately, on return to usermode.
875
876 \paragraph{}
877
878 Quite simple.
879
880 \subsection{Should the mechanism accommodate "foreign ISA's" and if so how does one restore the ISA.}
881
882 \paragraph{}
883
884 See section above on LAST-ISANS. With the introduction of LAST-ISANS, the
885 change is entirely transparent, and handled by the Supervisor (or Hypervisor)
886 trap, in a fashion that the foreign ISA need not even know of the existence
887 of ISANS. At all.
888
889 \subsection{Where is the default ISA stored and what is responsible for what it is after}
890
891 \paragraph{}
892
893 Options:
894 \begin{itemize}
895 \parskip 0pt
896 \itemsep 1pt
897
898 \item
899
900 start up
901
902 \item
903
904 starting a program
905
906 \item
907
908 calling into a dynamically linked library
909
910 \item
911
912 taking a trap
913
914 \item
915
916 changing privilege levels
917
918 \end{itemize}
919
920 \paragraph{}
921
922 These first four are entirely at the discretion of (and the
923 responsibility of) the software. There is precedent for most of these
924 having been implemented, historically, at some point, in relation to
925 LE/BE mode CSRs in other hardware (MIPSEL vs MIPS distros for example).
926
927 \paragraph{}
928
929 Traps are responsible for saving LAST-ISANS on the stack, exactly as they
930 are also responsible for saving other context-sensitive information such
931 as the registers and xEPC.
932
933 \paragraph{}
934
935 The hardware is responsible for atomically switching out ISANS into the
936 relevant xLAST-ISANS (and back again on exit). See Privileged Traps,
937 above.
938
939 \subsection{If the ISANS is just bits of an instruction that are to be prefixed by the cpu, can those bits contain immediates? Register numbers?}
940
941 \paragraph{}
942
943 The concept of a CSR containing an immediate makes no sense. The concept
944 of a CSR containing a register number, the contents of which would, presumably,
945 be inserted into the NS, would immediately make that register a permanent
946 and irrevocably reserved register that could not be utilised for any other
947 purpose.
948
949 \paragraph{}
950
951 This is what the CSRs are supposed to be for!
952
953 \paragraph{}
954
955 It would be better just to have a second CSR - ISANS2 - potentially even ISANS3
956 in 60+ years time, rather than try to use a GPR for the purposes for which CSRs
957 are intended.
958
959 \subsection{How does the system indicate a namespace is not recognised? Does it trap or can/must a recoverable mechanism be provided?}
960
961 \paragraph{}
962
963 It doesn't "indicate" that a namespace is not recognised. WLRL fields only
964 hold supported values. If the hardware cannot hold the value, a trap
965 \textbf{MUST} be thrown (in the UNIX platform), and at that point it becomes the
966 responsibility of software to deal with it.
967
968 \subsection{What are the security implications? Can some ISA namespaces be set by user space?}
969
970 \paragraph{}
971
972 Of course they can. It becomes the responsibility of the Supervisor Mode
973 (the kernel) to treat ISANS in a fashion orthogonal to the PC. If the OS
974 is not capable of properly context-switching securely by setting the right
975 PC, it's not going to be capable of properly looking after changes to ISANS.
976
977 \subsection{Does the validity of an ISA namespace depend on privilege level? If so how?}
978
979 \paragraph{}
980
981 The question does not exactly make sense, and may need a re-reading of the
982 section on how Privilege Modes, above. In RISC-V, privilege modes do not
983 actually change very much state of the system: the absolute minimum changes
984 are made (swapped out) - xEPC, xSTATUS and so on - and the privilege mode
985 is expected to handle the context switching (or other actions) itself.
986
987 \paragraph{}
988
989 ISANS - through LAST-ISANS - is absolutely no different. The trap and the
990 kernel (Supervisor or Hypervisor) are provided the \textbf{mechanism} by which
991 ISA Namespace \textbf{may} be set: it is up to the software to use that mechanism
992 correctly, just as the software is expected to use the mechanisms provided
993 to correctly implement context-switching by saving and restoring register
994 files, the PC, and other state. The NS effectively becomes just another
995 part of that state.