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