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
omega-mv / gpu / amd / kfd / records.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Serialize KFD request records from requests.tsv, without issuing any ioctl or opening a device. |
| 3 | note usage: records TABLE RECORD_INDEX [FIELD_VALUE ...]; index is zero-based table order. |
| 4 | note No values means the whole record is zero-filled (output fields and reserved bytes start zero). |
| 5 | note Otherwise provide one unsigned value for every field, in table order; repeated fields expand. |
| 6 | note requests.tsv is the wire-data authority: REQUEST SIZE FIELD_COUNT followed by OFFSET WIDTH COUNT. |
| 7 | note # comments name requests and fields. No native struct is used in the producer. Little endian. |
| 8 | note The 88-byte scratch extent is exactly sizeof(kfd_ioctl_create_queue_args), the largest record |
| 9 | note in this fixed ABI table, not a queue/workload capacity. It is a constant because the pinned omc |
| 10 | note (omega-mv e5b32567) takes only a constant length in `new` (a table size there is error 16). |
| 11 | note A larger record in a later KFD revision refuses with exit 3, never truncates; the gate compares |
| 12 | note every field and size with the installed header. |
| 13 | note Includes GET_PROCESS_APERTURES_NEW, not deprecated GET_PROCESS_APERTURES (fixed seven GPU cap). |
| 14 | note Auxiliary records (request 0) are an aperture entry, a signal-event wait entry and one GPU ID. |
| 15 | note Addresses are serialized values supplied by the caller, not Omega pointers or validated mappings. |
| 16 | note Pointer binding, KFD calls, event-page signal lifecycle, MMIO and hardware coherency NOT_RUN. |
| 17 | note Exit: 0 emitted; 2 numeric input; 3 table/record/field shape; 64 usage; 74 I/O. |
| 18 | note Exhausting the pinned compiler's loop fuel bound 999999 traps with exit 104 before output. |
| 19 | note tests/amd/loop-fuel.sh alarms when a gate table passes 500000 bytes; route A (newer omc pin) removes the bound. |
| 20 | om2 |
| 21 | global rb uchar 1 |
| 22 | global rfd int 1 |
| 23 | global ri ulong 1 |
| 24 | fn rbyte to ulong |
| 25 | r = read get rfd 0 rb 0 1 |
| 26 | if eq r 0 |
| 27 | ret 256 |
| 28 | end |
| 29 | if ne r 1 |
| 30 | exit 74 |
| 31 | end |
| 32 | ret ulong get rb 0 |
| 33 | end |
| 34 | fn tnum to ulong |
| 35 | c = rbyte |
| 36 | while true bound 999999 |
| 37 | if eq c 256 |
| 38 | exit 3 |
| 39 | end |
| 40 | if eq c 35 |
| 41 | while ne c 10 bound 999999 |
| 42 | c = rbyte |
| 43 | if eq c 256 |
| 44 | exit 3 |
| 45 | end |
| 46 | end |
| 47 | end |
| 48 | if gt c 32 |
| 49 | break |
| 50 | end |
| 51 | c = rbyte |
| 52 | end |
| 53 | v = ulong 0 |
| 54 | while and ge c 48 le c 57 bound 999999 |
| 55 | d = sub c 48 |
| 56 | if gt v 1844674407370955161 |
| 57 | exit 3 |
| 58 | end |
| 59 | if and eq v 1844674407370955161 gt d 5 |
| 60 | exit 3 |
| 61 | end |
| 62 | v = add mul v 10 d |
| 63 | c = rbyte |
| 64 | end |
| 65 | if gt c 32 |
| 66 | if ne c 256 |
| 67 | exit 3 |
| 68 | end |
| 69 | end |
| 70 | ret v |
| 71 | end |
| 72 | fn store b edit uchar 88 off ulong wid ulong val ulong to void |
| 73 | if and ne wid 4 ne wid 8 |
| 74 | exit 3 |
| 75 | end |
| 76 | if or gt off len b gt wid sub len b off |
| 77 | exit 3 |
| 78 | end |
| 79 | if and eq wid 4 gt val 4294967295 |
| 80 | exit 3 |
| 81 | end |
| 82 | for i ulong wid |
| 83 | put b add off i uchar and shr val mul i 8 255 |
| 84 | end |
| 85 | end |
| 86 | fn fld b edit uchar 88 idx ulong sz ulong to void |
| 87 | off = tnum |
| 88 | wid = tnum |
| 89 | cnt = tnum |
| 90 | if eq cnt 0 |
| 91 | exit 3 |
| 92 | end |
| 93 | for j ulong cnt |
| 94 | pos = add off mul j wid |
| 95 | if gt pos sz |
| 96 | exit 3 |
| 97 | end |
| 98 | if gt wid sub sz pos |
| 99 | exit 3 |
| 100 | end |
| 101 | if eq idx 0 |
| 102 | v = ulong 0 |
| 103 | if gt argcnt 3 |
| 104 | if ge get ri 0 ulong argcnt |
| 105 | exit 3 |
| 106 | end |
| 107 | v = argnum get ri 0 |
| 108 | end |
| 109 | store b pos wid v |
| 110 | put ri 0 add get ri 0 1 |
| 111 | end |
| 112 | end |
| 113 | end |
| 114 | fn record b edit uchar 88 idx ulong sz ulong nf ulong to void |
| 115 | put ri 0 3 |
| 116 | for f ulong nf |
| 117 | fld b idx sz |
| 118 | end |
| 119 | if eq idx 0 |
| 120 | if gt argcnt 3 |
| 121 | if ne get ri 0 ulong argcnt |
| 122 | exit 3 |
| 123 | end |
| 124 | end |
| 125 | end |
| 126 | end |
| 127 | fn main to int |
| 128 | if lt argcnt 3 |
| 129 | ret 64 |
| 130 | end |
| 131 | path = new uchar 4095 0 |
| 132 | n = argread 1 path 0 len path |
| 133 | if lt n 1 |
| 134 | ret 74 |
| 135 | end |
| 136 | fd = fopen path 0 ulong n 0 0 |
| 137 | if lt fd 0 |
| 138 | ret 74 |
| 139 | end |
| 140 | put rfd 0 int fd |
| 141 | idx = argnum 2 |
| 142 | note idx + 1 bounds the row loop below; at the 64-bit maximum it would wrap to 0, so refuse it. |
| 143 | if eq idx 18446744073709551615 |
| 144 | ret 3 |
| 145 | end |
| 146 | buf = new uchar 88 0 |
| 147 | for i ulong add idx 1 |
| 148 | req = tnum |
| 149 | sz = tnum |
| 150 | nf = tnum |
| 151 | if or eq sz 0 gt sz len buf |
| 152 | ret 3 |
| 153 | end |
| 154 | record buf sub idx i sz nf |
| 155 | if eq i idx |
| 156 | emit part buf 0 sz |
| 157 | end |
| 158 | end |
| 159 | r = fclose int fd |
| 160 | if ne r 0 |
| 161 | ret 74 |
| 162 | end |
| 163 | ret 0 |
| 164 | end |