(no commit message)
[libreriscv.git] / overloadable_opcodes.mdwn
1 # Overloadable opcodes.
2
3 The xext proposal defines a small number N (e.g. N= 8) standardised R-type instructions
4 xcmd0, xcmd1, ...xcmd[N-1], preferably in the brownfield opcode space.
5 Each xcmd takes (in rs1) a 12 bit "logical unit" (lun) identifying a (sub)device on the cpu
6 that implements some "extension interface" (xintf) together with some additional data.
7 Extension devices may be implemented in any convenient form, e.g. non standard extensions
8 of the CPU iteself, IP tiles, or closely coupled external devices.
9
10 An xintf is a set of up to N commands with 2 input and 1 output port (i.e. like an
11 R-type instruction), together with a description of the semantics of the commands. Calling
12 e.g. xcmd3 routes its two inputs and one output ports to command 3 on the device determined
13 by the lun bits in rs1. Thus, the N standard xcmd instructions are standard-designated
14 overloadable opcodes, with the non standard semantics of the opcode determined by the lun.
15
16 Portable software, does not use luns directly. Instead, it goes through a level of
17 indirection using a further instruction xext. The xext instruction translates a 20 bit globally
18 unique identifier UUID of an xintf, to the lun of a device on the cpu that implements that xintf.
19 The cpu can do this, because it knows (at manufacturing or boot time) which devices it has, and
20 which xintfs they provide. This includes devices that would be described as non standard extension
21 of the cpu if the designers had used custom opcodes instead of xintf as an interface. If the
22 UUID of the xintf is not recognised at the current privilege level, the xext instruction returns
23 the special lun = 0, causing any xcmd to trap. Minor variations of this scheme (requiring two
24 more instructions xext0 and xextm1) cause xcmd instructions to fallback to always return 0
25 or -1 instead of trapping.
26
27 Remark1: the main difference with a previous "ioctl like proposal" is that UUID translation
28 is stateless and does not use resources. The xext instruction _neither_ initialises a
29 device _nor_ builds global state identified by a cookie. If a device needs initialisation
30 it can do this using xcmds as init and deinit instructions. Likewise, it can hand out
31 cookies (which can include the lun) as a return value .
32
33 Remark2: Implementing devices can respond to an (essentially) arbitrary number of xintfs.
34 Hence, while an xintf is restricted to N commands, an implementing device can have an
35 arbitrary number of commands. Organising related commands in xintfs, helps avoid UUID space
36 pollution, and allows to amortise the (small) cost of UUID to lun translation if related
37 commands are used in combination.
38
39 Tl;DR see below for a C description of how this is supposed to work.
40
41 == Description of the instructions ==
42
43 xcmd0 rd, rs1, rs2
44 xcmd1 rd, rs1, rs2
45 ....
46 xcmdN rd, rs1, rs2
47
48 * rs1 contains a 12 bit "logical unit" (lun) together with xlen - 12 bits of additional data.
49 * rs2 is arbitrary
50
51 For e.g xmd3, route the inputs rs1, rs2 and output port rd to command 3 of the (sub)device on the cpu identified by the lun bits of rs1.
52
53 after execution:
54 * rd contains the value that of the output port of the implementing device
55
56 --------
57 xext rd, rs1, rs2
58 xext0 rd, rs1, rs2
59 xextm1 rd, rs1, rs2
60
61
62 * rs1 contains
63 --a UUID of at least 20 bit in bit 12 .. XLEN of rs1 identifying an xintf.
64 --the sequence number of a device at the current privilege level on the cpu implementing the xintf in bit 0..11 .
65 In particular, if bit 0..11 is zero, the default implemententation is requested.
66 * rs2 is arbitrary (but bit XLEN-12 to XLEN -1 is discarded)
67
68 after execution,
69 rd contains the lun of a device implementing the xintf or the luns 0 (for xext), 1 (for xext0) or 2 (for xextm1).
70
71 ---
72 The net effect is that, when the CPU implements an xintf with UUID 0xABCDE a sequence like
73
74 //fake UUID of an xintf
75 lui rd 0xABCDE
76 xext rd rd rs1
77 xcmd0 rd rd rs2
78
79 acts like a single namespaced instruction cmd0_ABCDE rd rs1 rs2 (with the annoying caveat that the last 12 of rs1 are discarded) The sequence not indivisible but the crucial semantics that you might want to be indivisible is in xcmd0.
80
81 Delegation and UUID is expected to come at a small performance price compared to a "native" instruction. This should, however, be an acceptable tradeoff in many cases. Moreover implementations may opcode-fuse the whole instruction sequence or the first or last two instructions.
82 If several instructions of the same interface are used, one can also use instruction sequences like
83
84 lui t1 0xABCDE //org_tinker_tinker__RocknRoll_uuid
85 xext t1 t1 zero
86 xcmd0 a5, t1, a0 // org_tinker_tinker__RocknRoll__rock(a5, t1, a0)
87 xcmd1 t2, t1, a1 // org_tinker_tinker__RocknRoll__roll(t2, t1, a5)
88 xcmd0 a0, t1, t2 // org_tinker_tinker__RocknRoll__rock(a0, t1, t2)
89
90 If 0xABCDE is an unknown UUID at the current privilege level, the sequence results in a trap just like cmd0_ABCDE rd rs1 rs2 would. The sequence
91
92 //fake UUID of an xintf
93 lui rd 0xABCDE
94 xext0 rd rd rs1
95 xcmd0 rd rd rs2
96
97 acts exactly like the sequence with xext, except that 0 is returned by xcmd0 if the UUID is unknown at the current privilege level. Likewise usage of xextm1 results in -1 being returned. This requires lun = 0 , 1 and 2 to be routed to three mandatory fallback
98 interfaces defined below.
99
100 On the software level, the xintf is just a set of glorified assembler macros
101
102 org.tinker.tinker:RocknRoll{
103 uuid : 0xABCDE
104 rock rd rs1 rs2 : xcmd0 rd rs1 rs2
105 roll rd rs1 rs2 : xcmd1 rd rs1 rs2
106 }
107
108 so that the above sequence can be more clearly written as
109
110 import(org.tinker.tinker:RocknRoll)
111
112 lui rd org.tinker.tinker:RocknRoll:uuid
113 xext rd rd rs1
114 org.tinker.tinker:RocknRoll:rock rd rd rs2
115
116
117 ------
118 The following standard xintfs shall be implemented by the CPU.
119
120 For lun == 0:
121
122 At privilege level user mode, supervisor mode and hypervisor mode
123
124 org.RiscV:Fallback:Trap{
125 uuid: 0
126 trap0 rd rs1 rs2: xcmd0 rd rs1 rs2
127 ...
128 trap[N-1] rd rs1 rs2: xcmd[N-1] rd rs1 rs2
129 }
130
131 each of the xcmd instructions shall trap to one level higher.
132
133 At privilege level machine mode each trap command has unspecified behaviour, but in debug mode
134 should cause an exception to a debug environment.
135
136 For lun == 1, at all privilege levels
137
138 org.RiscV:Fallback:ReturnZero{
139 uuid: 1
140 return_zero0 rd rs1 rs2: xcmd0 rd rs1 rs2
141 ...
142 return_zero[N-1] rd rs1 rs2: xcmd[N-1] rd rs1 rs2
143 }
144
145 each return_zero command shall return 0 in rd.
146
147 For lun == 2, at all privilege levels
148
149 org.RiscV:Fallback:ReturnMinusOne{
150 uuid: 2
151 return_minusone0 rd rs1 rs2: xcmd0 rd rs1 rs2
152 ...
153 return_minusone[N-1] rd rs1 rs2: xcmd[N-1] rd rs1 rs2
154 }
155
156 each return_minusone shall return -1.
157
158 ---
159
160 Remark:
161 Quite possibly even glorified standard assembler macros are overkill and it is easier to just use defines or ordinary macro's with long names. E.g. writing
162
163 #define org_tinker_tinker__RocknRoll__uuid 0xABCDE
164 #define org_tinker_tinker__RocknRoll__rock(rd, rs1, rs2) xcmd0 rd, rs1, rs2
165 #define org_tinker_tinker__RocknRoll__roll(rd, rs1, rs2) xcmd1 rd, rs1, rs2
166
167 allows the same sequence to be written as
168
169 lui rd org_tinker_tinker__RocknRoll__uuid
170 xext rd rs1
171 org_tinker_tinker__RocknRoll__rock(rd, rd, rs2)
172
173 Readability of assembler is no big deal for a compiler, but people are supposed to _document_ the semantics of the interface. In particular specifying the semantics of the xintf in same way as the semantics of the cpu should allow formal verification.
174
175 ==Implications for the RiscV ecosystem ==
176
177
178 The proposal allows independent groups to define one or more extension
179 interfaces of (slightly crippled) R-type instructions implemented by an
180 extension device. Such an extension device would be an native but non standard
181 extension of the CPU, an IP tile or a closely coupled external chip and would
182 be configured at manufacturing time or bootup of the CPU.
183
184 The 20 bit provided by the UUID of an xintf is much more room than provided by the 2 custom 32 bit, or even 4 custom 64/48 bit opcode spaces. Thus the overloadable opcodes proposal avoids most of the need to put a claim on opcode space and the associated collisions when combining independent extensions. In this respect it is similar to POSIX ioctls, which (almost) obviate the need for defining new syscalls to control new or nonstandard hardware.
185
186 The expanded flexibility comes at the cost: the standard can specify the
187 semantics of the delegation mechanism and the interfacing with the rest
188 of the cpu, but the actual semantics of the overloaded instructions can
189 only be defined by the designer of the interface. Likewise, a device
190 can be conforming as far as delegation and interaction with the CPU
191 is concerned, but whether the hardware is conforming to the semantics
192 of the interface is outside the scope of spec. Being able to specify
193 that semantics using the methods used for RV itself is clearly very
194 valuable. One impetus for doing that is using it for purposes of its own,
195 effectively freeing opcode space for other purposes. Also, some interfaces
196 may become de facto or de jure standards themselves, necessitating
197 hardware to implement competing interfaces. I.e., facilitating a free
198 for all, may lead to standards proliferation. C'est la vie.
199
200 The only "ISA-collisions" that can still occur are in the 20 bit (~10^6)
201 interface identifier space, with 12 more bits to identify a device on
202 a hart that implements the interface. One suggestion is setting aside
203 2^19 id's that are handed out for a small fee by a central (automated)
204 registration (making sure the space is not just claimed), while the
205 remaining 2^19 are used as a good hash on a long, plausibly globally
206 unique human readable interface name. This gives implementors the choice
207 between a guaranteed private identifier paying a fee, or relying on low
208 probabilities. On RV64 the UUID can also be extended to 52 bits (> 10^15).
209
210
211 ==== Description of the extension as C functions.==
212
213 /* register format of rs1 for xext instructions */
214 typedef struct uuid_device{
215 long dev:12;
216 long uuid: 8*sizeof(long) - 12;
217 } uuid_device_t
218
219 /* register format for rd of xext and rs1 of xcmd instructions, packs lun and data */
220 typedef struct lun_data{
221 long lun:12;
222 long data: 8*sizeof(long) - 12;
223 } lun_data_t
224
225 /* proposed R-type instructions
226 xext rd rs1 rs2
227 xcmd0 rd rs1 rs2
228 xcmd1 rd rs1 rs2
229 ...
230 xcmd7 rd rs1 rs2
231 */
232
233 lun_data_t xext(uuid_dev_t rs1, long rs2);
234 long xcmd0(lun_data_t rs1, long rs2);
235 long xcmd1(lun_data_t rs1, long rs2);
236 ...
237 long xcmd<N>(lun_data_t rs1, long rs2);
238
239 /* hardware interface presented by an implementing device. */
240 typedef
241 long device_fn(unsigned short subdevice_xcmd, lun_data_t rs1, long rs2);
242
243 /* cpu internal datatypes */
244
245 enum privilege = {user = 0b0001, super = 0b0010, hyper = 0b0100, mach = 0b1000};
246
247 /* cpu internal, does what is on the label */
248 static
249 enum privilege cpu__current_privilege_level()
250
251 typedef
252 struct lun{
253 unsigned short id:12
254 } lun_t;
255
256 struct uuid_device_priv2lun{
257 struct{
258 uuid_dev_t uuid_dev;
259 enum privilege reqpriv;
260 };
261 lun_t lun;
262 };
263
264 struct device_subdevice{
265 device_fn* device_addr;
266 unsigned short subdeviceId:12;
267 };
268
269 struct lun_priv2device_subdevice{
270 struct{
271 lun_t lun;
272 enum privilege reqpriv
273 }
274 struct device_subdevice devAddr_subdevId;
275 }
276
277 static
278 struct uuid_device_priv2lun cpu__lun_map[];
279
280 /*
281 map (UUID, device, privilege) to a 12 bit lun,
282 return (lun_t){0} on unknown (at acces level)
283
284 does associative memory lookup and tests privilege.
285 */
286 static
287 lun_t cpu__lookup_lun(const struct uuid_device_priv2lun* lun_map, uuid_dev_t uuid_dev, enum privilege priv);
288
289
290
291 lun_data_t xext(uuid_dev_t rs1, long rs2)
292 {
293 lun_t lun = cpu__lookup_lun(lun_map, rs1, current_privilege_level());
294
295 return (lun_data_t){.lun = lun.id, .data = rs2 % (1<< (8*sizeof(long) - 12))}
296 }
297
298
299 struct lun_priv2device_subdevice cpu__device_subdevice_map[];
300
301 /* map (lun, priv) to struct device_subdevice pair.
302 For lun = 0, or unknown (lun, priv) pair, returns (struct device_subdevice){NULL,0}
303 */
304 static
305 device_subdevice_t cpu__lookup_device_subdevice(const struct lun_priv2device_subdevice_map* dev_subdev_map,
306 lun_t lun, enum privileges priv);
307
308
309
310 /* functional description of the delegating xcmd0 .. xcmd7 instructions */
311 template<k = 0..N-1> //pretend this is C
312 long xcmd<k>(lun_data_t rs1, long rs2)
313 {
314 struct device_subdevice dev_subdev = cpu__lookup_device_subdevice(device_subdevice_map, rs1.lun, current_privilege());
315 return dev_subdev.devAddr(dev_subdev.subdevId | k >> 12 , rs1, rs2);
316 }
317
318 /*Fallback interfaces*/
319 #define org_RiscV__Fallback__Trap__uuid 0
320 #define org_RiscV__Fallback__ReturnZero__uuid 1
321 #define org_RiscV__Fallback__ReturnMinusOne__uuid 2
322
323 /* fallback device */
324 static
325 long cpu__falback(short subdevice_xcmd, lun_data_t rs1, long rs2)
326 {
327 switch(subdevice_xcmd % (1 << 12) ){
328 case 0 /* org.RiscV:Trap */: trap_to(cpu__next_higher_privilege_level());
329 case 1 /* org.RiscV:ReturnZero */: return 0;
330 case 2 /* org.RiscV:ReturnMinus1 */: return -1
331 case 3 /* org.RiscV:Trap Machinelevel */: printf("something is rotten in machinemode: unknown xintf device"); return 31415926;
332 default: trap("hardware configuration error");
333 }
334
335 Example:
336
337
338
339 #define com_bigbucks__Frobate__uuid 0xABCDE
340 #define org_tinker_tinker__RocknRoll__uuid 0x12345
341 #define org_tinker_tinker__Jazz__uuid 0xD0B0D
342 /*
343 com.bigbucks:Frobate{
344 uuid: com_bigbucks__Frobate__uuid
345 frobate rd rs1 rs2 : cmd0 rd rs1 rs2
346 foo rd rs1 rs2 : cmd1 rd rs1 rs2
347 bar rd rs1 rs2 : cmd1 rd rs1 rs2
348 }
349 */
350 org.tinker.tinker:RocknRoll{
351 uuid: org_tinker_tinker__RocknRoll__uuid
352 rock rd rs1 rs2: cmd0 rd rs1 rs2
353 roll rd rs1 rs2: cmd1 rd rs1 rs2
354 }
355
356 long com_bigbucks__device1(short subdevice_xcmd, lun_data_t rs1, long rs2)
357 {
358 switch(subdevice_xcmd) {
359 case 0 | 0 << 12 /* com.bigbucks:Frobate:frobate */ : return device1_frobate(rs1, rs2);
360 case 42| 0 << 12 /* com.bigbucks:FrobateMach:frobate : return device1_frobate_machine_level(rs1, rs2);
361 case 0 | 1 << 12 /* com.bigbucks:Frobate:foo */ : return device1_foo(rs1, rs2);
362 case 0 | 2 << 12 /* com.bigbucks:Frobate:bar */ : return device1_bar(rs1, rs2);
363 case 1 | 0 << 12 /* org.tinker.tinker:RocknRoll:rock */ : return device1_rock(rs1, rs2);
364 case 1 | 1 << 12 /* org.tinker.tinker:RocknRoll:roll */ : return device1_roll(rs1, rs2);
365 default: trap(“hardware configuration error”);
366 }
367 }
368
369 /*
370 org.tinker.tinker:Jazz{
371 uuid: org_tinker_tinker__Jazz__uuid
372 boogy rd rs1 rs2: cmd0 rd rs1 rs2
373 }
374 */
375
376 long org_tinker_tinker__device2(short subdevice_xcmd, lun_data_t rs1, long rs2)
377 {
378 switch(dev_cmd.interfId){
379 case 0 | 0 << 12 /* com.bigbucks:Frobate:frobate */: return device2_frobate(rs1, rs2);
380 case 0 | 1 << 12 /* com.bigbucks:Frobate:foo */ : return device2_foo(rs1, rs2);
381 case 0 | 2 << 12 /* com.bigbucks:Frobate:bar */ : return device2_foo(rs1, rs2);
382 case 1 | 0 << 12 /* org_tinker_tinker:Jazz:boogy */: return device2_boogy(rs1, rs2);
383 default: trap(“hardware configuration error”);
384 }
385 }
386
387
388 /* struct uuid_dev2lun_map[] */
389 lun_map = {
390 {{.uuid_devId = {org_RiscV__Fallback__Trap__uuid , 0}, .priv = user}, .lun = 0},
391 {{.uuid_devId = {org_RiscV__Fallback__Trap__uuid , 0}, .priv = super}, .lun = 0},
392 {{.uuid_devId = {org_RiscV__Fallback__Trap__uuid , 0}, .priv = hyper}, .lun = 0},
393 {{.uuid_devId = {org_RiscV__Fallback__Trap__uuid , 0}, .priv = mach}, .lun = 0},
394 {{.uuid_devId = {org_RiscV__Fallback__ReturnZero__uuid , 0}, .priv = user}, .lun = 1},
395 {{.uuid_devId = {org_RiscV__Fallback__ReturnZero__uuid , 0}, .priv = super}, .lun = 1},
396 {{.uuid_devId = {org_RiscV__Fallback__ReturnZero__uuid , 0}, .priv = hyper}, .lun = 1},
397 {{.uuid_devId = {org_RiscV__Fallback__ReturnZero__uuid , 0}, .priv = mach} .lun = 1},
398 {{.uuid_devId = {org_RiscV__Fallback__ReturnMinusOne__uuid, 0}, .priv = user}, .lun = 2},
399 {{.uuid_devId = {org_RiscV__Fallback__ReturnMinusOne__uuid, 0}, .priv = super}, .lun = 2},
400 {{.uuid_devId = {org_RiscV__Fallback__ReturnMinusOne__uuid, 0}, .priv = hyper}, .lun = 2},
401 {{.uuid_devId = {org_RiscV__Fallback__ReturnMinusOne__uuid, 0}, .priv = mach}, .lun = 2},
402 {{.uuid_devId = {com_bigbucks__Frobate__uuid, 0}, .priv = user} .lun = 32}, //32 sic!
403 {{.uuid_devId = {com_bigbucks__Frobate__uuid, 1}, .priv = super} .lun = 32},
404 {{.uuid_devId = {com_bigbucks__Frobate__uuid, 1}, .priv = hyper} .lun = 32},
405 {{.uuid_devId = {com_bigbucks__Frobate__uuid, 1}, .priv = mach} .lun = 32},
406 {{.uuid_devId = {com_bigbucks__Frobate__uuid, 0}, .priv = super} .lun = 34}, //34 sic!
407 {{.uuid_devId = {com_bigbucks__Frobate__uuid, 0}, .priv = hyper} .lun = 34},
408 {{.uuid_devId = {com_bigbucks__Frobate__uuid, 0}, .priv = mach} .lun = 34},
409 {{.uuid_devId = {org_tinker_tinker__RocknRoll__uuid, 0}, .priv = user} .lun = 33}, //33 sic!
410 {{.uuid_devId = {org_tinker_tinker__RocknRoll__uuid, 0}, .priv = super} .lun = 33},
411 {{.uuid_devId = {org_tinker_tinker__RocknRoll__uuid, 0}, .priv = hyper} .lun = 33},
412 {{.uuid_devId = {org_tinker_tinker__RocknRoll__uuid, 0}, .priv = super}, .lun = 35},
413 {{.uuid_devId = {org_tinker_tinker__RocknRoll__uuid, 0}, .priv = hyper}, .lun = 35},
414 }
415
416 /* struct lun2dev_subdevice_map[] */
417 dev_subdevice_map = {
418 {{.lun = 0, .priv = user}, .devAddr_interfId = {fallback, 0 /* Trap */}},
419 {{.lun = 0, .priv = super}, .devAddr_interfId = {fallback, 0 /* Trap */}},
420 {{.lun = 0, .priv = hyper}, .devAddr_interfId = {fallback, 0 /* Trap */}},
421 {{.lun = 0, .priv = mach}, .devAddr_interfId = {fallback, 3 /* Trap */}},
422 {{.lun = 1, .priv = user}, .devAddr_interfId = {fallback, 1 /* ReturnZero */}},
423 {{.lun = 1, .priv = super}, .devAddr_interfId = {fallback, 1 /* ReturnZero */}},
424 {{.lun = 1, .priv = hyper}, .devAddr_interfId = {fallback, 1 /* ReturnZero */}},
425 {{.lun = 1, .priv = mach}, .devAddr_interfId = {fallback, 1 /* ReturnZero */}},
426 {{.lun = 2, .priv = user}, .devAddr_interfId = {fallback, 2 /* ReturnMinusOne*/}},
427 {{.lun = 2, .priv = super}, .devAddr_interfId = {fallback, 2 /* ReturnMinusOne*/}},
428 {{.lun = 2, .priv = hyper}, .devAddr_interfId = {fallback, 2 /* ReturnMinusOne*/}},
429 {{.lun = 2, .priv = mach}, .devAddr_interfId = {fallback, 2 /* ReturnMinusOne*/}},
430 // .lun = 3 .. 7 reserved for other fallback RV interfaces
431 // .lun = 8 .. 30 reserved as error numbers, c.li t1 31; bltu rd t1 L_fail tests errors
432 // .lun = 31 reserved out of caution
433 {{.lun = 32, .priv = user}, .devAddr_interfId = {device1, 0 /* Frobate interface */}},
434 {{.lun = 32, .priv = super}, .devAddr_interfId = {device1, 0 /* Frobate interface */}},
435 {{.lun = 32, .priv = hyper}, .devAddr_interfId = {device1, 0 /* Frobate interface */}},
436 {{.lun = 32, .priv = mach}, .devAddr_interfId = {device1,64 /* Frobate machine level interface */}},
437 {{.lun = 33, .priv = user}, .devAddr_InterfId = {device1, 1 /* RocknRoll interface */}},
438 {{.lun = 33, .priv = super}, .devAddr_InterfId = {device1, 1 /* RocknRoll interface */}},
439 {{.lun = 33, .priv = hyper}, .devAddr_InterfId = {device1, 1 /* RocknRoll interface */}},
440 {{.lun = 34, .priv = super}, .devAddr_interfId = {device2, 0 /* Frobate interface */}},
441 {{.lun = 34, .priv = hyper}, .devAddr_interfId = {device2, 0 /* Frobate interface */}},
442 {{.lun = 34, .priv = mach}, .devAddr_interfId = {device2, 0 /* Frobate interface */}},
443 {{.lun = 35, .priv = super}, .devAddr_interfId = {device2, 1 /* Jazz interface */}},
444 {{.lun = 35, .priv = hyper}, .devAddr_interfId = {device2, 1 /* Jazz interface */}},
445 }