interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / bridge / shlib / nvml.om

548 lines · 13097 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note bridge/shlib/nvml.om: the 11 functions CUDApp's libnvidia-ml.so.1 exports (its libnvidia-ml.map) with
3note the C signatures of its nvml.h, as real bodies: the shell of CUDApp's native/nvml/api.om and of the C
4note glue its bridge generated (compat/nvml/libnvidia-ml.c), in Omega. Composed after
5note gpu/nvidia/native/nvml/nvml.om, query.om, step.om and errstr.om (the request protocol, RM construction
6note and error text, unchanged) into one object and built by bridge/shlib/shlib.om from nvml.tsv: every export
7note is a frame export under the lock row's pthread mutex, and the transport is the three omega2_nvrm_*
8note imports of libomega-nvrm.so.1 (native/nvrm/transport.om; in the gate the Omega RM fake, rmfake.om).
9note States (nvml.tsv, frame words 8..13): NvS the 128-word step state, NvB the step buffer (request at 0,
10note payload at PO, output at OO), NvR the transport's result word (0) and a copy word (1, 2), NvT the 32
11note cached error strings of 64 bytes, then "Unknown Error" at 2048 and the 32 cache flags at 2112, NvH the 8
12note device handles (a magic word and the index, 16 bytes each), NvLock the mutex.
13note Every export answers as the C glue does: the body's code when it is an nvmlReturn_t value (0..30 or 999),
14note else NVML_ERROR_UNKNOWN (999); outputs are written through C pointers (memcpy) only when the glue's checks
15note pass (its o_ok), which holds for a body answering 999 with its outputs too.
16note Differences from the C, all on a path the C reaches only by a bug: a trap anywhere in the unit (step,
17note errstr or this shell) returns 999 at once (nvmlErrorString: NULL, not "Unknown Error"), without the
18note C's put s 22 0 after a trap inside the step; a lock that cannot be taken returns the same.
19
20const HMAG ulong 5723342196145933892
21const UMAX ulong 4294967295
22const WMAX ulong 18446744073709551615
23
24extern fn omega2_nvrm_ioctl r ulong fd ulong nr ulong b ulong hl ulong pl ulong to uint
25extern fn omega2_nvrm_open_file r ulong b ulong pn ulong to uint
26extern fn omega2_nvrm_close_file r ulong fd ulong to uint
27extern fn memcpy d ulong s ulong n ulong to ulong
28
29note ap_put: the low n bytes of v to the C pointer d (through NvR word 1)
30fn ap_put d ulong v ulong n ulong r edit ulong 8 fr view ulong 14 to void
31put r 1 v
32z = memcpy d add get fr 10 8 n
33end
34
35note ap_act: the action the step requested (1 ioctl, 2 open, 3 close), its result in s 14 and the transport
36note status in s 15; 0 is a malformed request.
37fn ap_act s edit ulong SN r edit ulong 8 fr view ulong 14 to ulong
38k = get s 8
39put r 0 0
40st = ulong 0
41if or eq k 0 gt k 3
42ret 0
43end
44if eq k 1
45hl = get s 11
46pl = get s 12
47if or gt hl 4608 gt pl 55888
48ret 0
49end
50st = ulong omega2_nvrm_ioctl get fr 10 get s 9 get s 10 get fr 9 hl pl
51end
52if eq k 2
53pn = get s 13
54if or eq pn 0 gt pn 55888
55ret 0
56end
57st = ulong omega2_nvrm_open_file get fr 10 get fr 9 pn
58end
59if eq k 3
60st = ulong omega2_nvrm_close_file get fr 10 get s 9
61end
62put s 14 get r 0
63put s 15 st
64ret 1
65end
66
67note ap_run: step until the unit reports done (1) or the protocol fails (0), at most 4096 steps.
68fn ap_run s edit ulong SN b edit uchar BN r edit ulong 8 fr view ulong 14 to ulong
69ok = ulong 0
70put s 22 0
71for n ulong 4096
72c = nvml_step s b
73if ne c 0
74break
75end
76if eq get s 8 0
77ok = 1
78break
79end
80if eq ap_act s r fr 0
81break
82end
83end
84put s 22 0
85ret ok
86end
87
88note ap_fin: the copy bounds: at most cap output bytes, output only with a destination, a scalar only with a
89note destination and below 2^32. 1 accepts the step's result.
90fn ap_fin s edit ulong SN cap ulong dst ulong scl ulong to ulong
91n = get s 7
92if gt n cap
93ret 0
94end
95if and ne n 0 eq dst 0
96ret 0
97end
98if ne get s 27 0
99if or eq scl 0 gt get s 5 UMAX
100ret 0
101end
102end
103ret 1
104end
105
106note ap_code: the step's status when it is an nvmlReturn_t value, else NVML_ERROR_UNKNOWN.
107fn ap_code s edit ulong SN to ulong
108v = get s 6
109if or le v 30 eq v 999
110ret v
111end
112ret 999
113end
114
115note ap_set: the operation and its four arguments in s 0..4.
116fn ap_set s edit ulong SN op ulong a1 ulong a2 ulong a3 ulong to void
117put s 0 op
118put s 1 a1
119put s 2 a2
120put s 3 a3
121put s 4 0
122end
123
124note ap_res: the export's answer: f 0 when it is an nvmlReturn_t value and f 1 holds no flag outside m
125fn ap_res f edit ulong 8 m ulong to int
126v = get f 0
127if gt v 30
128ret 999
129end
130if ne and get f 1 not m 0
131ret 999
132end
133ret int v
134end
135
136note ap_ok: the glue's o_ok before its pointer checks: f 0 is an nvmlReturn_t value (0..30 or 999) and f 1 holds
137note no flag outside m; a body answering NVML_ERROR_UNKNOWN (999) with outputs still has them written, as the C
138fn ap_ok f edit ulong 8 m ulong to bool
139v = get f 0
140if and gt v 30 ne v 999
141ret false
142end
143ret eq and get f 1 not m 0
144end
145
146note ap_hi: the pool index of the device handle d, or 2^64 - 1 when d is not one this library handed out
147fn ap_hi d ulong h view ulong 16 fr view ulong 14 to ulong
148a = get fr 12
149if lt d a
150ret WMAX
151end
152o = sub d a
153if or ge o 128 ne rem o 16 0
154ret WMAX
155end
156i = div o 16
157m = get h mul i 2
158x = get h add mul i 2 1
159if and eq m HMAG eq x i
160ret i
161end
162ret WMAX
163end
164
165note ap_nz: 1 when the C pointer v is not NULL
166fn ap_nz v ulong to ulong
167if eq v 0
168ret 0
169end
170ret 1
171end
172
173note Init and Shutdown: no outputs.
174fn ap_bare s edit ulong SN b edit uchar BN r edit ulong 8 fr view ulong 14 op ulong to int
175f = new ulong 8 0
176put f 0 999
177ap_set s op 0 0 0
178if ne ap_run s b r fr 0
179if ne ap_fin s 0 0 0 0
180put f 0 ap_code s
181end
182end
183ret ap_res f 0
184end
185
186export fn nvmlInit_v2 fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 to int
187ret ap_bare s b r fr 1
188end
189
190export fn nvmlShutdown fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 to int
191ret ap_bare s b r fr 2
192end
193
194note Count: f 2 deviceCount present, f 3 the count.
195export fn nvmlDeviceGetCount_v2 fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 to int
196f = new ulong 8 0
197put f 0 999
198p = get fr 0
199put f 2 ap_nz p
200ap_set s 3 0 get f 2 0
201if eq ap_run s b r fr 0
202ret 999
203end
204if eq ap_fin s 0 0 get f 2 0
205ret 999
206end
207if ne get s 27 0
208put f 3 get s 5
209put f 1 1
210end
211put f 0 ap_code s
212c = ap_res f 1
213if ne get f 1 0
214if or eq p 0 gt get f 3 UMAX
215ret 999
216end
217if not ap_ok f 1
218ret c
219end
220ap_put p get f 3 4 r fr
221end
222ret c
223end
224
225note HandleByIndex: f 2 index, f 3 device present, f 4 the handle index (below the pool of 8).
226export fn nvmlDeviceGetHandleByIndex_v2 fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 h edit ulong 16 to int
227f = new ulong 8 0
228put f 0 999
229put f 2 and get fr 0 UMAX
230p = get fr 1
231put f 3 ap_nz p
232ap_set s 4 get f 2 get f 3 0
233if eq ap_run s b r fr 0
234ret 999
235end
236if eq ap_fin s 0 0 1 0
237ret 999
238end
239k = ap_code s
240if eq k 0
241v = ulong 0
242if ne get s 27 0
243v = get s 5
244end
245if ge v 8
246ret 999
247end
248if ne get f 3 0
249put f 4 v
250put f 1 1
251end
252end
253put f 0 k
254c = ap_res f 1
255if or not ap_ok f 1 eq get f 1 0
256ret c
257end
258if or eq p 0 ge get f 4 8
259ret 999
260end
261i = get f 4
262put h mul i 2 HMAG
263put h add mul i 2 1 i
264ap_put p add get fr 12 mul i 16 8 r fr
265ret c
266end
267
268note Name and UUID: f 2 device, f 3 buffer present, f 4 bytes written (with the NUL), f 5 length.
269fn ap_str fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 h view ulong 16 op ulong to int
270f = new ulong 8 0
271put f 0 999
272put f 2 ap_hi get fr 0 h fr
273p = get fr 1
274put f 3 ap_nz p
275put f 5 and get fr 2 UMAX
276ap_set s op get f 2 get f 3 get f 5
277if eq ap_run s b r fr 0
278ret 999
279end
280if eq ap_fin s get f 5 get f 3 0 0
281ret 999
282end
283n = get s 7
284if ne n 0
285put f 4 n
286put f 1 1
287end
288put f 0 ap_code s
289c = ap_res f 1
290if or not ap_ok f 1 eq get f 1 0
291ret c
292end
293n = get f 4
294if or eq p 0 eq n 0
295ret 999
296end
297if or gt n get f 5 gt n 22800
298ret 999
299end
300if ne get b add OO sub n 1 0
301ret 999
302end
303z = memcpy p add get fr 9 OO n
304ret c
305end
306
307export fn nvmlDeviceGetName fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 h edit ulong 16 to int
308ret ap_str fr s b r h 5
309end
310
311export fn nvmlDeviceGetUUID fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 h edit ulong 16 to int
312ret ap_str fr s b r h 6
313end
314
315note Temperature: f 2 device, f 3 sensor, f 4 temp present, f 5 the temperature.
316export fn nvmlDeviceGetTemperature fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 h edit ulong 16 to int
317f = new ulong 8 0
318put f 0 999
319put f 2 ap_hi get fr 0 h fr
320put f 3 and get fr 1 UMAX
321p = get fr 2
322put f 4 ap_nz p
323ap_set s 7 get f 2 get f 4 get f 3
324if eq ap_run s b r fr 0
325ret 999
326end
327if eq ap_fin s 0 0 get f 4 0
328ret 999
329end
330if ne get s 27 0
331put f 5 get s 5
332put f 1 1
333end
334put f 0 ap_code s
335c = ap_res f 1
336if or not ap_ok f 1 eq get f 1 0
337ret c
338end
339if or eq p 0 gt get f 5 UMAX
340ret 999
341end
342ap_put p get f 5 4 r fr
343ret c
344end
345
346note Memory (24 bytes) and utilization (8 bytes): f 2 device, f 3 record present.
347fn ap_rec fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 h view ulong 16 op ulong to int
348f = new ulong 8 0
349put f 0 999
350put f 2 ap_hi get fr 0 h fr
351p = get fr 1
352put f 3 ap_nz p
353sz = ulong 24
354if eq op 9
355sz = 8
356end
357ap_set s op get f 2 get f 3 0
358if eq ap_run s b r fr 0
359ret 999
360end
361if eq ap_fin s sz get f 3 0 0
362ret 999
363end
364n = get s 7
365if ne n 0
366if ne n sz
367ret 999
368end
369put f 1 1
370end
371put f 0 ap_code s
372c = ap_res f 1
373if or not ap_ok f 1 eq get f 1 0
374ret c
375end
376if eq p 0
377ret 999
378end
379z = memcpy p add get fr 9 OO sz
380ret c
381end
382
383export fn nvmlDeviceGetMemoryInfo fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 h edit ulong 16 to int
384ret ap_rec fr s b r h 8
385end
386
387export fn nvmlDeviceGetUtilizationRates fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 h edit ulong 16 to int
388ret ap_rec fr s b r h 9
389end
390
391note Processes: f 2 device, f 3 infoCount present, f 4 *infoCount, f 5 infos present, f 6 records written;
392note the capacity is 24 bytes per requested entry; f 7 the caller's *infoCount.
393export fn nvmlDeviceGetComputeRunningProcesses_v3 fr view ulong 14 s edit ulong SN b edit uchar BN r edit ulong 8 h edit ulong 16 to int
394f = new ulong 8 0
395put f 0 999
396put f 2 ap_hi get fr 0 h fr
397p = get fr 1
398put f 3 ap_nz p
399if ne p 0
400put r 2 0
401z = memcpy add get fr 10 16 p 4
402put f 4 get r 2
403end
404put f 7 get f 4
405put f 5 ap_nz get fr 2
406c = ap_procs f s b r fr
407if eq c 1000
408ret 999
409end
410ap_pout f b r fr
411ret c
412end
413
414note the body of GetComputeRunningProcesses_v3 (nv_procs) and the glue's checks; its answer, or 1000 when the
415note glue's o_ok is false (the export answers 999 then and writes nothing)
416fn ap_procs f edit ulong 8 s edit ulong SN b edit uchar BN r edit ulong 8 fr view ulong 14 to int
417w = get f 4
418put s 0 10
419put s 1 get f 2
420put s 2 get f 3
421put s 3 w
422put s 4 get f 5
423if eq ap_run s b r fr 0
424ret 1000
425end
426if eq ap_fin s mul w 24 get f 5 get f 3 0
427ret 1000
428end
429m = ulong 0
430n = get s 7
431if ne n 0
432if ne rem n 24 0
433ret 1000
434end
435put f 6 div n 24
436m = 4
437end
438if ne get s 27 0
439put f 4 get s 5
440m = add m 2
441end
442put f 1 m
443put f 0 ap_code s
444ret ap_pchk f fr
445end
446
447note the glue's checks for GetComputeRunningProcesses_v3: *infoCount below 2^32, at most the requested and
448note at most 950 records, each output with its pointer; 1000 when one fails
449fn ap_pchk f edit ulong 8 fr view ulong 14 to int
450if not ap_ok f 6
451ret 1000
452end
453c = ap_res f 6
454m = get f 1
455if ne and m 2 0
456if or eq get fr 1 0 gt get f 4 UMAX
457ret 1000
458end
459end
460if eq and m 4 0
461ret c
462end
463e = get f 6
464if eq get fr 2 0
465ret 1000
466end
467if or gt e get f 7 gt e 950
468ret 1000
469end
470ret c
471end
472
473note the outputs of GetComputeRunningProcesses_v3: *infoCount, then each record field by field
474fn ap_pout f edit ulong 8 b edit uchar BN r edit ulong 8 fr view ulong 14 to void
475m = get f 1
476if ne and m 2 0
477ap_put get fr 1 get f 4 4 r fr
478end
479if eq and m 4 0
480ret
481end
482d = get fr 2
483o = add get fr 9 OO
484for j ulong get f 6 bound 950
485e = mul j 24
486z = memcpy add d e add o e 4
487z = memcpy add add d e 8 add add o e 8 8
488z = memcpy add add d e 16 add add o e 16 4
489z = memcpy add add d e 20 add add o e 20 4
490end
491end
492
493note ErrorString: the code sign-extended from the C int; each cache slot is filled once, so a returned
494note pointer stays valid and unchanged for the life of the process; anything else answers "Unknown Error"
495note (the fallback at NvT 2048, written from errstr's own text).
496export fn nvmlErrorString fr view ulong 14 t edit uchar 2144 to ulong
497fb = add get fr 11 2048
498k = ap_eslot t get fr 0
499if ge k 32
500ret fb
501end
502if ge ap_elen t mul k 64 64
503ret fb
504end
505ret add get fr 11 mul k 64
506end
507
508note ap_eslot: the cache slot of the C int x (sign-extended), filled on first use; 32 when the text does not fit.
509note Writes the fallback text at 2048 first, once.
510fn ap_eslot t edit uchar 2144 x ulong to ulong
511o = new uchar 64 0
512if eq get t 2048 0
513v = nvml_errstr 1000 o
514ap_ecopy t 2048 o
515end
516code = and x UMAX
517if ge code 2147483648
518code = or code 18446744069414584320
519end
520v = nvml_errstr code o
521n = and v 255
522k = and shr v 8 255
523if or ge n 64 ge k 32
524ret 32
525end
526if eq ulong get t add 2112 k 0
527ap_ecopy t mul k 64 o
528put t add mul k 64 n uchar 0
529put t add 2112 k uchar 1
530end
531ret k
532end
533
534fn ap_ecopy t edit uchar 2144 z ulong o view uchar 64 to void
535for j ulong 64
536put t add z j get o j
537end
538end
539
540note ap_elen: the offset of the first NUL in the 64 bytes at z (64: none)
541fn ap_elen t view uchar 2144 z ulong to ulong
542for i ulong 64
543if eq ulong get t add z i 0
544ret i
545end
546end
547ret 64
548end