(no commit message)
[libreriscv.git] / isa_conflict_resolution / isamux_isans.mdwn
1 # Note-form on ISAMUX (aka "ISANS")
2
3 A fixed number of additional (hidden) bits, conceptually a "namespace", that go directly and non-optionally
4 into the instruction decode phase, extending (in each implementation) the
5 opcode length to 16+N, 32+N, 48+N, where N is a hard fixed quantity on
6 a per-implementor basis.
7
8 Where the opcode is normally loaded from the location at the PC, the extra
9 bits are instead set via a CSR and mandatorially appended to every instruction: hence why they are described as "hidden" opcode bits, and as a "namespace".
10
11 The parallels with c++ "using namespace" are direct and clear.
12
13 # Hypothetical Format
14
15 Note that this is a hypothetical format, yet TBD, where particular attention
16 needs to be paid to the fact that there is an "immediate" version of CSRRW
17 (with 5 bits of immediate) that could save a lot of space in binaries.
18
19 <code>
20 <pre>
21 3 2 1
22 |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|
23 |------------------------------ |-------|---------------------|-|
24 |reserved reserved reserved reserved reserved | foreignarch |1|
25 |custom | reserved | official|B| rvcpage |0|
26 </pre>
27 <code>
28
29 RV Mode
30
31 * when bit 0 is 0, "RV" mode is selected.
32 * in RV mode, bits 1 thru 5 provide up to 16 possible alternative meanings (namespaces) for 16 Bit opcodes. "pages" if you will. The top bit indicates custom meanings. When set to 0, the top bit is for official usage.
33 * Bits 15 thru 23 are reserved.
34 * Bits 24 thru 31 are for custom usage.
35 * bit 6 ("B") is LE/BE
36
37 16 bit page examples:
38
39 * 0b0000 STANDARD (2019) RVC
40 * 0b0001 RVCv2
41 * 0b0010 RV16
42 * 0b0011 RVCv3
43 * ...
44 * 0b1000 custom 16 bit opcode meanings 1
45 * 0b1001 custom 16 bit opcode meanings 2
46 * .....
47
48 Foreign Arch Mode
49
50 * when bit 0 is 1, "Foreign arch" mode is selected.
51 * Bits 1 thru 7 are a table of foreign arches.
52 * when the MSB is 1, this is for custom use.
53 * when the MSB is 0, bits 1 thru 6 are reserved for 64 possible official foreign archs.
54
55 Foreign archs could be (examples):
56
57 * 0b0000000 x86_32
58 * 0b0000001 x86_64
59 * 0b0000010 MIPS32
60 * 0b0000011 MIPS64
61 * ....
62 * 0b0010000 Java Bytecode
63 * 0b0010001 N.E.Other Bytecode
64 * ....
65 * 0b1000000 custom foreign arch 1
66 * 0b1000001 custom foreign arch 2
67 * ....
68
69 Note that "official" foreign archs have a binary value where the MSB is zero,
70 and custom foreign archs have a binary value where the MSB is 1.
71
72 # Namespaces are permitted to swap to new state <a name="stateswap"></a>
73
74 In each privilege level, on a change of ISANS (whether through manual setting of ISANS or through trap entry or exit changing the ISANS CSR), an implementation is permitted to completely and arbitrarily switch not only the instruction set, it is permitted to switch to a new bank of CSRs (or a subset of the same), and even to switch to a new PC.
75
76 This to occur immediately and atomically at the point at which the change in ISANS occurs.
77
78 The most obvious application of this is for Foreign Archs, which may have their own completely separate PC. Thus, foreign assembly code and RISCV assembly code need not be mixed in the same binary.
79
80 Further use-cases may be envisaged however great care needs to be taken to not cause massive complications for JIT emulation. Switching CSR and PC in the RISCV NS needs to be done wisely ane responsibly, i.e. minimised!
81
82 To be discussed. Context <https://groups.google.com/a/groups.riscv.org/d/msg/isa-dev/x-uFZDXiOxY/27QDW5KvBQAJ>
83
84 # Privileged Modes / Traps <a name="privtraps"></a>
85
86 An additional WLRL CSR per priv-level named "LAST-ISANS" is required, and
87 another called "TRAP-ISANS"
88 These mirrors the ISANS CSR, and, on a trap, if the current ISANS in
89 that privilege level is not equal to TRAP-ISANS, its value is atomically
90 transferred into LAST-ISANS by the hardware, and ISANS in that trap
91 is set to TRAP-ISANS. Hardware is *only then* permitted to modify the PC to
92 begin execution of the trap.
93
94 On exit from the trap, hardware must check to see if LAST-ISANS is equal
95 to TRAP-ISANS. If it is not, LAST-ISANS is copied into the ISANS CSR,
96 LAST-ISANS is set to TRAP-ISANS, and *only then* is the hardware permitted
97 to modify the PC to begin execution where the trap left off.
98
99 Note 1: in the case of Supervisor Mode (context switches in particular),
100 saving and changing of LAST-ISANS (to and from the stack) must be done
101 atomically and under the protection of the SIE bit. Failure to do so
102 could result in corruption of LAST-ISANS when multiple traps occur in
103 the same privilege level.
104
105 Note 2: question - should the trap due to illegal (unsupported) values
106 written into LAST-ISANS occur when the *software* writes to LAST-ISANS,
107 or when the *trap* (on exit) writes into LAST-ISANS? this latter seems
108 fraught: a trap, on exit, causing another trap??
109
110 Per-privilege-level pseudocode (there exists UISANS, UTRAPISANS, ULASTISANS,
111 MISANS, MTRAPISANS, MLASTISANS and so on):
112
113 <code>
114 <pre>
115 trap_entry()
116 {
117     if (ISANS != TRAP_ISANS) // musn't change if already there
118     {
119         LAST-ISANS = ISANS // record the old NS
120         ISANS = TRAP_ISANS // traps are executed in "trap" NS
121     }
122 }
123
124 and trap_exit:
125
126 trap_exit():
127 {
128     if (LAST-ISANS != TRAP_ISANS)
129     {
130         ISANS = LAST-ISANS
131         LAST-ISANS = TRAP_ISANS
132     }
133 }
134 </pre>
135 </code>
136
137 # Why not have TRAP-ISANS as a vector table, matching mtvec? <a name="trap-isans-vec"></a>
138
139 Use case to be determined. Rather than be a global per-priv-level value,
140 TRAP-ISANS is a table of length exactly equal to the mtvec/utvec/stvec table,
141 with corresponding entries that specify the assembly-code namespace in which
142 the trap handler routine is written.
143
144 Open question: see <https://groups.google.com/a/groups.riscv.org/d/msg/isa-dev/IAhyOqEZoWA/BM0G3J2zBgAJ>
145
146 <code>
147 <pre>
148 trap_entry(x_cause)
149 {
150     if (ISANS != TRAP_ISANS[xcause]) // musn't change if already there
151     {
152         LAST-ISANS = ISANS // record the old NS
153         ISANS = TRAP_ISANS[xcause] // traps are executed in "trap" NS
154     }
155 }
156
157 and trap_exit:
158
159 trap_exit(x_cause):
160 {
161     if (LAST-ISANS != TRAP_ISANS[x_cause])
162     {
163         ISANS = LAST-ISANS
164         LAST-ISANS = TRAP_ISANS[x_cause]
165     }
166 }
167 </pre>
168 </code>
169
170 # What happens if this scheme is not adopted? Why is it better than leaving things well alone? <a name="lassezfaire"></a>
171
172 At the first sign of an emergency non-backwards compatible and unavoidable
173 change to the *frozen* RISCV *official* Standards, the entire RISCV
174 community is fragmented and divided into two:
175
176 * Those vendors that are hardware compatible with the legacy standard.
177 * Those that are compatible with the new standard.
178
179 *These two communities would be mutually exclusively incompatible*. If
180 a second emergency occurs, RISCV becomes even less tenable.
181
182 Hardware that wished to be "compatible" with either flavour would require
183 JIT or offline static binary recompilation. No vendor would willingly
184 accept this as a condition of the standards divergence in the first place,
185 locking up decision making to the detriment of RISCV as a whole.
186
187 By providing a "safety valve" in the form of a hidden namespace, at least
188 newer hardware has the option to implement both (or more) variations,
189 *and still apply for Certification*.
190
191 However to also allow "legacy" hardware to at least be JIT soft
192 compatible, some very strict rules *must* be adhered to, that appear at
193 first sight not to make any sense.
194
195 It's complicated in other words!
196
197 # Surely it's okay to just tell people to use 48-bit encodings? <a name="use48bit"></a>
198
199 Short answer: it doesn't help resolve conflicts, and costs hardware and
200 redesigns to do so. Softcores in cost-sensitive embedded applications may
201 even not actually be able to fit the required 48 bit instruction decode engine
202 into a (small, ICE40) FPGA. 48-bit instruction decoding is much more complex
203 than straight 32-bit decoding, requiring a queue.
204
205 Second answer: conflicts can still occur in the (unregulated, custom) 48-bit
206 space, which *could* be resolved by ISAMUX/ISANS as applied to the *48* bit
207 space in exactly the same way. And the 64-bit space.
208
209 # Why not leave this to individual custom vendors to solve on a case by case basis?
210
211 The suggestion was raised that a custom extension vendor could create
212 their own CSR that selects between conflicting namespaces that resolve
213 the meaning of the exact same opcode. This to be done by all and any
214 vendors, as they see fit, with little to no collaboration or coordination
215 towards standardisation in any form.
216
217 The problems with this approach are numerous, when presented to a
218 worldwide context that the UNIX Platform, in particular, has to face
219 (where the embedded platform does not)
220
221 First: lack of coordination, in the proliferation of arbitrary solutions,
222 has to primarily be borne by gcc, binutils, LLVM and other compilers.
223
224 Secondly: CSR space is precious. With each vendor likely needing only one
225 or two bits to express the namespace collision avoidance, if they make
226 even a token effort to use worldwide unique CSRs (an effort that would
227 benefit compiler writers), the CSR register space is quickly exhausted.
228
229 Thirdly: JIT Emulation of such an unregulated space becomes just as
230 much hell as it is for compiler writers. In addition, if two vendors
231 use conflicting CSR addresses, the only sane way to tell the emulator
232 what to do is to give the emulator a runtime commandline argument.
233
234 Fourthly: with each vendor coming up with their own way of handling
235 conflicts, not only are the chances of mistakes higher, it is against the
236 very principles of collaboration and cooperation that save vendors money
237 on development and ongoing maintenance. Each custom vendor will have
238 to maintain their own separate hard fork of the toolchain and software,
239 which is well known to result in security vulnerabilities.
240
241 By coordinating and managing the allocation of namespace bits (unary
242 or binary) the above issues are solved. CSR space is no longer wasted,
243 compiler and JIT software writers have an easier time, clashes are
244 avoided, and RISCV is stabilised and has a trustable long term future.
245
246 # Why ISAMUX / ISANS has to be WLRL and mandatory trap on illegal writes <a name="wlrlmandatorytrap"></a>
247
248 The namespaces, set by bits in the CSR, are functionally directly
249 equivalent to c++ namespaces, even down to the use of braces.
250
251 WARL, by allowing implementors to choose the value, prevents and prohibits
252 the critical and necessary raising of an exception that would begin the
253 JIT process in the case of ongoing standards evolution.
254
255 Without this opportunity, an implementation has no way of knowing
256 how to JIT emulate any given conflicting opcode. It is as if the c++
257 standard was given the similar opportunity to completely ignore the
258 "using namespace" prefix!
259
260 --
261
262 Ok so I trust it's now clear why WLRL (thanks Allen) is needed.
263
264 When Dan raised the WARL concern initially a situation was masked by
265 the conflict, that if gone unnoticed would jeapordise ISAMUX/ISANS
266 entirely. Actually, two separate errors. So thank you for raising the
267 question.
268
269 The situation arises when foreign archs are to be given their own NS
270 bit. MIPS is allocated bit 8, x86 bit 9, whilst LE/BE is given bit 0,
271 RVCv2 bit 1 andso on. All of this potential rather than actual, clearly.
272
273 Imagine then that software tries to write and set not just bit 8 and
274 bit 9, it also tries to set bit 0 and 1 as well.
275
276 This *IS* on the face of it a legitimate reason to make ISAMUX/ISANS WARL.
277
278 However it masks a fundamental flaw that has to be addressed, which
279 brings us back much closer to the original design of 18 months ago,
280 and it's highlighted thus:
281
282 x86 and simultaneous RVCv2 modes are total nonsense in the first place!
283
284 The solution instead is to have a NS bit (bit0) that SPECIFICALLY
285 determines if the arch is RV or not. If 0, the rest of the ISAMUX/ISANS
286 is very specifically RV *only*, and if 1, the ISAMUX/ISANS is a *binary*
287 table of foreign architectures and foreign architectures only.
288
289 Exactly how many bits are used for the foreign arch table, is to
290 be determined. 7 bits, one of which is reserved for custom usage,
291 leaving a whopping 64 possible "official" foreign instruction sets to
292 be hardware-supported/JIT-emulated seems to be sufficiently gratuitous,
293 to me.
294
295 One of those could even be Java Bytecode!
296
297 Now, it could *hypothetically* be argued that the permutation of setting
298 LE/BE and MIPS for example is desirable. A simple analysis shows this
299 not to be the case: once in the MIPS foreign NS, it is the MIPS hardware
300 implementation that should have its own way of setting and managing its
301 LE/BE mode, because to do otherwise drastically interferes with MIPS
302 binary compatibility.
303
304 Thus, it is officially Not Our Problem: only flipping into one foreign
305 arch at a time makes sense, thus this has to be reflected in the
306 ISAMUX/ISANS CSR itself, completely side-stepping the (apparent) need
307 to make the NS CSR WARL (which would not work anyway, as previously
308 mentioned).
309
310 So, thank you, again, Dan, for raising this. It would have completely
311 jeapordised ISAMUX/NS if not spotted.
312
313 The second issue is: how does any hardware system, whether it support
314 ISANS or not, and whether any future hardware supports some Namespaces
315 and, in a transitive fashion, has to support *more* future namespaces,
316 through JIT emulation, if this is not planned properly in advance?
317
318 Let us take the simple case first: a current 2019 RISCV fully compliant
319 RV64GC UNIX capable system (with mandatory traps on all unsupported CSRs).
320
321 Fast forward 20 years, there are now 5 ISAMUX/NS unary bits, and 3
322 foreign arch binary table entries.
323
324 Such a system is perfectly possible of software JIT emulating ALL of these
325 options because the write to the (illegal, for that system) ISAMUX/NS
326 CSR generates the trap that is needed for that system ti begin JIT mode.
327
328 (This again emphasises exactly why the trap is mandatory).
329
330 Now let us take the case of a hypothetical system from say 2021 that
331 implements RVCv2 at the hardware level.
332
333 Fast forward 20 years: if the CSR were made WARL, that system would be
334 absolutely screwed. The implementor would be under the false impression
335 that ignoring setting of "illegal" bits was acceptable, making the
336 transition to JIT mode flat-out impossible to detect.
337
338 When this is considered transitively, considering all future additions to
339 the NS, and all permutations, it can be logically deduced that there is
340 a need to reserve a *full* set of bits in the ISAMUX/NS CSR *in advance*.
341
342 i.e. that *right now*, in the year 2019, the entire ISAMUX/NS CSR cannot
343 be added to piecemeal, the full 32 (or 64) bits *has* to be reserved,
344 and reserved bits set at zero.
345
346 Furthermore, if any software attempts to write to those reserved bits,
347 it *must* be treated just as if those bits were distinct and nonexistent
348 CSRs, and a trap raised.
349
350 It makes more sense to consider each NS as having its own completely
351 separate CSR, which, if it does not exist, clearly it should be obvious
352 that, as an unsupported CSR, a trap should be raised (and JIT emulation
353 activated).
354
355 However given that only the one bit is needed (in RV NS Mode, not
356 Foreign NS Mode), it would be terribly wasteful of the CSRs to do this,
357 despite it being technically correct and much easier to understand why
358 trap raising is so essential (mandatory).
359
360 This again should emphasise how to mentally get one's head round this
361 mind-bendingly complex problem space: think of each NS bit as its own
362 totally separate CSR that every implementor is free and clear to implement
363 (or leave to JIT Emulation) as they see fit.
364
365 Only then does the mandatory need to trap on write really start to hit
366 home, as does the need to preallocate a full set of reserved zero values
367 in the RV ISAMUX/NS.
368
369 Lastly, I *think* it's ok to only reserve say 32 bits, and, in 50 years
370 time if that genuinely is not enough, start the process all over again
371 with a new CSR. ISAMUX2/NS2.
372
373 Subdivision of the RV NS (support for RVCv3/4/5/RV16 without wasting
374 precious CSR bits) best left for discussion another time, the above is
375 a heck of a lot to absorb, already.
376
377 # Alternative RVC 16 Bit Opcode meanings
378
379 Ok, here is appropriate to raise an idea how to cover RVC and future
380 variants, including RV16.
381
382 Just as with foreign archs, and you quite rightly highlight above, it
383 makes absolutely no sense to try to select both RVCv1, v2, v3 and so on,
384 all simultaneously. An unary bit vector for RVC modes, changing the 16
385 BIT opcode space meaning, is wasteful and again has us believe that WARL
386 is the "solution".
387
388 The correct thing to do is, again, just like with foreign archs, to
389 treat RVCs as a *binary* namespace selector. Bits 1 thru 3 would give
390 8 possible completely new alternative meanings, just like how the Z80
391 and the 286 and 386 used to do bank switching.
392
393 All zeros is clearly reserved for the present RVC. 0b001 for RVCv2. 0b010
394 for RV16 (look it up) and there should definitely be room reserved here
395 for custom reencodings of the 16 bit opcode space.
396
397 # Why WARL will not work and why WLRL is required
398
399 WARL requires a follow-up read of the CSR to ascertain what heuristic
400 the hardware *might* have applied, and if that procedure is followed in
401 this proposal, performance even on hardware would be severely compromised.
402
403 In addition when switching to foreign architectures, the switch has to
404 be done atomically and guaranteed to occur.
405
406 In the case of JIT emulation, the WARL "detection" code will be in an
407 assembly language that is alien to hardware.
408
409 Support for both assembly languages immediately after the CSR write
410 is clearly impossible, this leaves no other option but to have the CSR
411 be WLRL (on all platforms) and for traps to be mandatory (on the UNIX
412 Platform).
413
414 # Is it strictly necessary for foreign archs to switch back? <a name="foreignswitch"></a>
415
416 No, because LAST-ISANS handles the setting and unsetting of the ISANS CSR
417 in a completely transparent fashion as far as the foreign arch is concerned.
418
419 Thus, in e.g. Hypervisor Mode, the foreign guest arch has no knowledge
420 or need to know that the hypervisor is flipping back to RV at the time of
421 a trap.
422
423 Note however that this is **not** the same as the foreign arch executing
424 *foreign* traps! Foreign architecture trap and interrupt handling mechanisms
425 are **out of scope** of this document and MUST be handled by the foreign
426 architecture implementation in a completely transparent fashion that in
427 no way interacts or interferes with this proposal.
428