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 / core / cubin.om

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