interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / om / cubin / cubin_elf.om

609 lines · 13399 bytes Raw Blame History
1note cubin_elf.om: explicitly selected sm_86/sm_121 cubin ELF packer.
2note It replaces the body of omega_cubin_pack in src/omega_cubin.c, which stays compiled only behind
3note CUDAPP_MIGRATION_PENDING_C_CORE as the explicit legacy oracle.
4note
5note Input (omega_cubin.c packs it):
6note m: 0 strlen of the name 1 text_len 2 nparams 3 cbank_size 4 gpr_count 5 shared_reserved
7note 6 target SM (86 or 121; 0 retains historical 121) 7 unused; from 8 parameter triples
8note nm: the name's first 64 bytes; tx: the text (16777216 bytes, the C limit 1 shl 24)
9note o: the output image, little-endian words; byte p is bits 8 (p rem 8) of word p / 8
10note Result: the image's byte count, or the refusal 1..7 of OMEGA_CUBIN_E_* (an image is never below 8
11note bytes). On a refusal o 0 holds the detail (PARAMS 0 count, 1 overlap, 2 size, 3 pointer size,
12note 4 alignment; CBANK 5 end, 6 range; MEM 7 over the output bound), o 1 the parameter, o 2 the end
13note or the image size, so the C caller prints the oracle's exact message.
14note Bound: the output view holds 134217728 bytes; an image above it (the in-file shared reservation plus the
15note text plus a few KiB of headers over 128 MiB)
16note is refused with 7 where the C oracle would allocate it. Arrays stop at 16777216 elements (error 23).
17note Every constant is the decimal form of the C writer's hex or text (conversions by Wolfram,
18note evidence/CUDAPP-OM-CUBIN/wolfram-constants.txt).
19
20const TXN ulong 16777216
21const OWN ulong 16777216
22const OBYTES ulong 134217728
23const MLEN ulong 104
24const PBASE ulong 896
25
26fn parbase m view ulong MLEN to ulong
27if eq get m 6 86
28ret 352
29end
30ret PBASE
31end
32
33fn smtarget m view ulong MLEN to ulong
34if eq get m 6 0
35ret 121
36end
37ret get m 6
38end
39
40note 1 when the image carries .note.nv.tkinfo (sm_121 only: its strings name sm_121; sm_86 has none)
41fn tkon m view ulong MLEN to ulong
42if eq smtarget m 121
43ret 1
44end
45ret 0
46end
47
48note one byte into the word image
49fn pb o edit ulong OWN p ulong v ulong to void
50k = shr p 3
51s = shl and p 7 3
52w = and get o k not shl 255 s
53put o k or w shl and v 255 s
54end
55
56fn p16 o edit ulong OWN p ulong v ulong to void
57pb o p v
58pb o add p 1 shr v 8
59end
60
61fn p32 o edit ulong OWN p ulong v ulong to void
62p16 o p v
63p16 o add p 2 shr v 16
64end
65
66fn p64 o edit ulong OWN p ulong v ulong to void
67p32 o p v
68p32 o add p 4 shr v 32
69end
70
71note the k low bytes of v at p; gives p + k
72fn ps o edit ulong OWN p ulong v ulong k ulong to ulong
73for i ulong k
74pb o add p i shr v mul i 8
75end
76ret add p k
77end
78
79note the n name bytes at p; gives p + n
80fn pn o edit ulong OWN p ulong nm view uchar 64 n ulong to ulong
81for i ulong n
82pb o add p i ulong get nm i
83end
84ret add p n
85end
86
87fn al x ulong a ulong to ulong
88ret mul div add x sub a 1 a a
89end
90
91fn isal c uchar to ulong
92if or or and ge c 65 le c 90 and ge c 97 le c 122 eq c 95
93ret 1
94end
95ret 0
96end
97
98fn isan c uchar to ulong
99if or ne isal c 0 and ge c 48 le c 57
100ret 1
101end
102ret 0
103end
104
105note name_ok of the C writer: 1..64 bytes, [A-Za-z_][A-Za-z0-9_]*
106fn nmok nm view uchar 64 n ulong to ulong
107if or eq n 0 gt n 64
108ret 0
109end
110if eq isal get nm 0 0
111ret 0
112end
113for i ulong n
114if eq isan get nm i 0
115ret 0
116end
117end
118ret 1
119end
120
121fn pfail o edit ulong OWN d ulong i ulong to ulong
122put o 0 d
123put o 1 i
124ret 3
125end
126
127note one parameter against the running end e: 0 good, else the detail 1..4 of the C order
128fn pbad of ulong sz ulong pt ulong e ulong to ulong
129if lt of e
130ret 1
131end
132if not or or or eq sz 8 eq sz 4 eq sz 2 eq sz 1
133ret 2
134end
135if and ne pt 0 ne sz 8
136ret 3
137end
138if and eq sz 8 ne and of 7 0
139ret 4
140end
141if and eq sz 4 ne and of 3 0
142ret 4
143end
144if and eq sz 2 ne and of 1 0
145ret 4
146end
147ret 0
148end
149
150note the parameter walk and the bank checks, in the C order
151fn pchk m view ulong MLEN o edit ulong OWN to ulong
152e = parbase m
153np = get m 2
154for i ulong np
155b = add 8 mul i 3
156d = pbad get m b get m add b 1 get m add b 2 e
157if ne d 0
158ret pfail o d i
159end
160e = add get m b get m add b 1
161end
162cb = get m 3
163if gt e cb
164put o 0 5
165put o 2 e
166ret 4
167end
168if or gt cb 1024 lt cb parbase m
169put o 0 6
170ret 4
171end
172ret 0
173end
174
175fn chk m view ulong MLEN nm view uchar 64 o edit ulong OWN to ulong
176if not or eq smtarget m 86 eq smtarget m 121
177ret 1
178end
179if eq nmok nm get m 0 0
180ret 1
181end
182tl = get m 1
183if or or eq tl 0 ne and tl 15 0 gt tl TXN
184ret 2
185end
186if gt get m 2 32
187ret pfail o 0 0
188end
189if gt get m 4 255
190ret 5
191end
192if ne and get m 5 3 0
193ret 6
194end
195ret pchk m o
196end
197
198note L: 1..10 section offsets, 11 shoff, 12 phoff, 13 total, 14 .nv.info.<fn> size,
199note 15 the offset of section 11 (.note.nv.tkinfo, sm_121 only)
200fn lay m view ulong MLEN l edit ulong 16 to void
201n = get m 0
202f = ulong 64
203put l 1 f
204f = add f add add 117 mul 16 tkon m mul 3 n
205put l 2 f
206f = add f add n 2
207f = al f 8
208put l 3 f
209f = add f 96
210f = al f 128
211put l 4 f
212f = add f get m 1
213f = al f 16
214put l 5 f
215f = add f get m 3
216f = al f 4
217put l 6 f
218f = add f 36
219f = al f 4
220put l 7 f
221f = add f 36
222il = add 48 mul 16 get m 2
223put l 14 il
224f = al f 4
225put l 8 f
226f = add f il
227put l 9 f
228f = add f get m 5
229f = al f 4
230put l 10 f
231f = add f 32
232if eq tkon m 1
233f = al f 4
234put l 15 f
235f = add f 120
236end
237f = al f 8
238put l 11 f
239put l 12 add f add 704 mul 64 tkon m
240put l 13 add f add 984 mul 64 tkon m
241end
242
243note zero words 0 .. ceil(total / 8) - 1, in parts of 4096 (a loop stops at 1000000 passes)
244fn wzero o edit ulong OWN total ulong to void
245nw = shr add total 7 3
246nc = shr add nw 4095 12
247for c ulong nc
248for j ulong 4096
249k = add shl c 12 j
250if lt k nw
251put o k 0
252end
253end
254end
255end
256
257fn txw tx view uchar TXN q ulong to ulong
258w = ulong 0
259for i ulong 8
260w = or w shl ulong get tx add q i mul i 8
261end
262ret w
263end
264
265note the text at p (a multiple of 128), whole words: text_len is a multiple of 16
266fn wtext o edit ulong OWN tx view uchar TXN p ulong tl ulong to void
267nw = shr tl 3
268nc = shr add nw 4095 12
269b = shr p 3
270for c ulong nc
271for j ulong 4096
272k = add shl c 12 j
273if lt k nw
274put o add b k txw tx shl k 3
275end
276end
277end
278end
279
280note ELF header: 0x7f E L F, ELFCLASS64, LSB, version 1, ELFOSABI_NV 0x41 as one word; ABI version 8
281fn whdr o edit ulong OWN l view ulong 16 m view ulong MLEN to void
282p64 o 0 4684026196722992511
283if eq smtarget m 86
284pb o 7 51
285end
286if eq smtarget m 86
287pb o 8 7
288else
289pb o 8 8
290end
291p16 o 16 2
292p16 o 18 190
293if eq smtarget m 86
294p32 o 20 130
295else
296p32 o 20 1
297end
298p64 o 32 get l 12
299p64 o 40 get l 11
300if eq smtarget m 86
301note ELF EF_CUDA_SM86 | EF_CUDA_VIRTUAL_SM86 | 64-bit addressing and unified texture mode.
302p32 o 48 or shl ulong 86 16 or shl ulong 5 8 ulong 86
303else
304p32 o 48 100694274
305end
306p16 o 52 64
307p16 o 54 56
308p16 o 56 5
309p16 o 58 64
310p16 o 60 add 11 tkon m
311p16 o 62 1
312end
313
314note .shstrtab from p (its first byte is the 0 already there)
315fn wshs o edit ulong OWN p ulong nm view uchar 64 n ulong tk ulong to void
316q = add p 1
317q = ps o q 7022363563455443758 8
318q = add ps o q 98 1 1
319q = add ps o q 27691700481979182 7 1
320q = add ps o q 27691700398420782 7 1
321q = ps o q 51077770998830 6
322q = add pn o q nm n 1
323q = ps o q 8317708033571843630 8
324q = ps o q 50785646698868 6
325q = add pn o q nm n 1
326q = add ps o q 8027224783864622638 8 1
327q = ps o q 8101253776481349166 8
328q = add ps o q 29793 2 1
329q = ps o q 8027224783864622638 8
330q = ps o q 46 1
331q = add pn o q nm n 1
332q = ps o q 8241983636974759470 8
333q = ps o q 8243121572516095077 8
334q = add ps o q 206936761718 5 1
335q = ps o q 8533809357193702958 8
336if eq tk 1
337q = add ps o q 31356346816619310 7 1
338q = ps o q 8533809357193702958 8
339q = ps o q 31356346815968302 7
340else
341q = ps o q 31356346816619310 7
342end
343end
344
345note .symtab: null, the .text section symbol, the function, the .nv.constant0 section symbol
346fn wsym o edit ulong OWN p ulong tl ulong to void
347pb o add p 28 3
348p16 o add p 30 4
349p32 o add p 48 1
350pb o add p 52 18
351pb o add p 53 16
352p16 o add p 54 4
353p64 o add p 64 tl
354pb o add p 76 3
355p16 o add p 78 5
356end
357
358note .nv.info: attributes 0x2f (registers), 0x11, 0x12
359fn wginfo o edit ulong OWN p ulong gpr ulong to void
360p16 o p 12036
361p16 o add p 2 8
362p32 o add p 4 2
363p32 o add p 8 gpr
364p16 o add p 12 4356
365p16 o add p 14 8
366p32 o add p 16 2
367p16 o add p 24 4612
368p16 o add p 26 8
369p32 o add p 28 2
370end
371
372note .nv.compat: nine little-endian words of the reference container
373fn wcompat o edit ulong OWN p ulong to void
374p32 o p 2306
375p32 o add p 4 131586
376p32 o add p 8 328962
377p32 o add p 12 16844547
378p32 o add p 16 770
379p32 o add p 20 67074
380p32 o add p 24 527108
381end
382
383note attribute 0x37 then one 0x17 per parameter from p; gives the parameter end
384fn wpar o edit ulong OWN p ulong m view ulong MLEN to ulong
385p16 o p 14084
386p16 o add p 2 4
387p32 o add p 4 130
388q = add p 8
389e = parbase m
390np = get m 2
391for i ulong np
392b = add 8 mul i 3
393of = get m b
394p16 o q 5892
395p16 o add q 2 12
396p32 o add q 8 or i shl sub of parbase m 16
397ty = ulong 1175552
398if ne get m add b 2 0
399ty = 2225408
400end
401p32 o add q 12 ty
402q = add q 16
403if gt add of get m add b 1 e
404e = add of get m add b 1
405end
406end
407ret e
408end
409
410note the attributes after the parameters, from q
411fn wtail o edit ulong OWN q ulong e ulong gpr ulong base ulong to void
412p16 o q 6403
413p16 o add q 2 sub e base
414p16 o add q 4 2564
415p16 o add q 6 8
416p32 o add q 8 3
417p32 o add q 12 or base shl sub e base 16
418p16 o add q 16 20483
419p16 o add q 20 6915
420p16 o add q 22 gpr
421p16 o add q 24 24323
422p16 o add q 26 257
423p16 o add q 28 18946
424p16 o add q 32 13828
425p16 o add q 34 4
426end
427
428note .note.nv.cuinfo: namesz 12, descsz 8, type 0x3e8, "NVIDIA Corp", 2, sm 121, 130
429fn wnote o edit ulong OWN p ulong sm ulong to void
430p32 o p 12
431p32 o add p 4 8
432p32 o add p 8 1000
433q = ps o add p 12 4836937782729987662 8
434q = ps o q 7369327 3
435p16 o add p 24 2
436p16 o add p 26 sm
437p32 o add p 28 130
438end
439
440note .note.nv.tkinfo, which the sm_121 driver requires (CUDApp#68): namesz 12, descsz 96, type 2000,
441note "NVIDIA Corp", then 2, 0 and the offsets 1 8 34 52 of the strings "cudapp", "CUDApp omega_cubin
442note sm_121", "Build omega_cubin" and "-arch sm_121" in the blob from p + 48 (its bytes 64.. stay 0)
443fn wtk o edit ulong OWN p ulong to void
444p32 o p 12
445p32 o add p 4 96
446p32 o add p 8 2000
447q = ps o add p 12 4836937782729987662 8
448q = ps o q 7369327 3
449p32 o add p 24 2
450p32 o add p 32 1
451p32 o add p 36 8
452p32 o add p 40 34
453p32 o add p 44 52
454q = ps o add p 48 31648760991146752 8
455q = ps o q 8007523764898387267 8
456q = ps o q 7094686049299293549 8
457q = ps o q 3616776899442798185 8
458q = ps o q 2334109707079385137 8
459q = ps o q 8458709397083090287 8
460q = ps o q 7165896803008538978 8
461q = ps o q 3544950142621917288 8
462end
463
464fn sh1 o edit ulong OWN b ulong nm ulong ty ulong fl ulong to void
465p32 o b nm
466p32 o add b 4 ty
467p64 o add b 8 fl
468end
469
470fn sh2 o edit ulong OWN b ulong of ulong sz ulong lk ulong inf ulong to void
471p64 o add b 24 of
472p64 o add b 32 sz
473p32 o add b 40 lk
474p32 o add b 44 inf
475end
476
477fn sh3 o edit ulong OWN b ulong a ulong es ulong to void
478p64 o add b 48 a
479p64 o add b 56 es
480end
481
482note section headers 1..5
483fn wsha o edit ulong OWN l view ulong 16 m view ulong MLEN to void
484n = get m 0
485b = add get l 11 64
486sh1 o b 1 3 0
487sh2 o b get l 1 add add 117 mul 16 tkon m mul 3 n 0 0
488sh3 o b 1 0
489b = add b 64
490sh1 o b 11 3 0
491sh2 o b get l 2 add n 2 0 0
492sh3 o b 1 0
493b = add b 64
494sh1 o b 19 2 0
495sh2 o b get l 3 96 2 2
496sh3 o b 8 24
497b = add b 64
498sh1 o b 27 1 6
499sh2 o b get l 4 get m 1 3 2
500sh3 o b 128 0
501b = add b 64
502sh1 o b add 34 n 1 66
503sh2 o b get l 5 get m 3 3 4
504sh3 o b 16 0
505end
506
507note section headers 6..11
508fn wshb o edit ulong OWN l view ulong 16 m view ulong MLEN to void
509n = get m 0
510b = add get l 11 384
511sh1 o b add 49 mul 2 n 1879048192 0
512sh2 o b get l 6 36 3 0
513sh3 o b 4 0
514b = add b 64
515sh1 o b add 58 mul 2 n 1879048326 0
516sh2 o b get l 7 36 0 0
517sh3 o b 4 0
518b = add b 64
519sh1 o b add 69 mul 2 n 1879048192 64
520sh2 o b get l 8 get l 14 3 4
521sh3 o b 4 0
522b = add b 64
523sh1 o b add 79 mul 3 n 8 3
524sh2 o b get l 9 get m 5 0 0
525sh3 o b 1 0
526b = add b 64
527sh1 o b add 101 mul 3 n 7 16777280
528sh2 o b get l 10 32 mul 11 tkon m 2
529sh3 o b 4 0
530if eq tkon m 1
531b = add b 64
532sh1 o b add 117 mul 3 n 7 33554432
533sh2 o b get l 15 120 0 0
534sh3 o b 4 0
535end
536end
537
538fn ph1 o edit ulong OWN b ulong ty ulong fl ulong of ulong to void
539p32 o b ty
540p32 o add b 4 fl
541p64 o add b 8 of
542p64 o add b 48 8
543end
544
545fn ph2 o edit ulong OWN b ulong fs ulong ms ulong to void
546p64 o add b 32 fs
547p64 o add b 40 ms
548end
549
550note program headers: PHDR, LOAD R (the table), LOAD R|E (text), LOAD R|W (shared), LOAD R (bank)
551fn wph o edit ulong OWN l view ulong 16 m view ulong MLEN to void
552b = get l 12
553ph1 o b 6 4 b
554ph2 o b 280 280
555ph1 o add b 56 1 4 b
556ph2 o add b 56 280 280
557ph1 o add b 112 1 5 get l 4
558ph2 o add b 112 get m 1 get m 1
559ph1 o add b 168 1 6 get l 9
560ph2 o add b 168 0 get m 5
561ph1 o add b 224 1 4 get l 5
562ph2 o add b 224 get m 3 get m 3
563end
564
565note .nv.info.<fn>: attribute 0x37, the parameters, then the tail
566fn winfo o edit ulong OWN l view ulong 16 m view ulong MLEN to void
567p = get l 8
568e = wpar o p m
569q = add add p 8 mul 16 get m 2
570wtail o q e get m 4 parbase m
571end
572
573note every section payload and both header tables into the zeroed image
574fn wbody o edit ulong OWN l view ulong 16 m view ulong MLEN nm view uchar 64 tx view uchar TXN to void
575whdr o l m
576n = get m 0
577wshs o get l 1 nm n tkon m
578q = pn o add get l 2 1 nm n
579wsym o get l 3 get m 1
580wtext o tx get l 4 get m 1
581wginfo o get l 6 get m 4
582wcompat o get l 7
583winfo o l m
584wnote o get l 10 smtarget m
585if eq tkon m 1
586wtk o get l 15
587end
588wsha o l m
589wshb o l m
590wph o l m
591end
592
593fn cubin_elf m view ulong 104 nm view uchar 64 tx view uchar 16777216 o edit ulong 16777216 to ulong
594l = new ulong 16 0
595rc = chk m nm o
596if ne rc 0
597ret rc
598end
599lay m edit l
600t = get l 13
601if gt t OBYTES
602put o 0 7
603put o 2 t
604ret 7
605end
606wzero o t
607wbody o view l m nm tx
608ret t
609end