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 / om / gpu / dsv41 / decoder / resolve.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note The scheduler holds the exclusive CPU lease from miss through publish. |
| 3 | note maps[0] is Zone 4 staging, maps[1] the Zone 2/3 GPU-visible slab arena. |
| 4 | const deBytes ulong 18800640 |
| 5 | note Linux AF_ALG is an optional host accelerator for the same SHA-256 bytes. |
| 6 | note Failure to open the algorithm selects the seeded software implementation. |
| 7 | text deHashType "hash" |
| 8 | text deHashName "sha256" |
| 9 | global deAlgBase int 1 |
| 10 | fn deHashOpen to int |
| 11 | addr = new uchar 88 0 |
| 12 | base = get deAlgBase 0 |
| 13 | if eq base -1 |
| 14 | ret -1 |
| 15 | end |
| 16 | if eq base 0 |
| 17 | put addr 0 38 |
| 18 | for i ulong 4 |
| 19 | put addr add i 2 get deHashType i |
| 20 | end |
| 21 | for i ulong 6 |
| 22 | put addr add i 24 get deHashName i |
| 23 | end |
| 24 | sock = socknew 38 5 0 |
| 25 | if lt sock 0 |
| 26 | put deAlgBase 0 -1 |
| 27 | ret -1 |
| 28 | end |
| 29 | if ne bind int sock addr 0 88 0 |
| 30 | unused = fclose int sock |
| 31 | put deAlgBase 0 -1 |
| 32 | ret -1 |
| 33 | end |
| 34 | base = add int sock 1 |
| 35 | put deAlgBase 0 base |
| 36 | end |
| 37 | ret int accept sub base 1 0 |
| 38 | end |
| 39 | |
| 40 | fn deHashChunk fd int buf view uchar * n ulong ctx edit shactx to bool |
| 41 | if lt fd 0 |
| 42 | sha256_update ctx part buf 0 n |
| 43 | ret true |
| 44 | end |
| 45 | wrote = send fd buf 0 n 32768 |
| 46 | ret eq wrote long n |
| 47 | end |
| 48 | |
| 49 | fn deHashFinish fd int ctx edit shactx digest edit uchar 32 to bool |
| 50 | if lt fd 0 |
| 51 | sha256_final ctx digest |
| 52 | ret true |
| 53 | end |
| 54 | got = read fd digest 0 32 |
| 55 | unused = fclose fd |
| 56 | ret eq got 32 |
| 57 | end |
| 58 | |
| 59 | fn deStagePlane fds view int 2 off ulong stage long dest ulong size ulong ctx edit shactx to bool |
| 60 | buf = new uchar 1048576 0 |
| 61 | pos = ulong 0 |
| 62 | while lt pos size |
| 63 | chunk = sub size pos |
| 64 | if gt chunk len buf |
| 65 | chunk = len buf |
| 66 | end |
| 67 | if not diRead get fds 0 buf add off pos chunk |
| 68 | ret false |
| 69 | end |
| 70 | if ne mapwrite stage add dest pos buf 0 chunk long chunk |
| 71 | ret false |
| 72 | end |
| 73 | if not deHashChunk get fds 1 buf chunk ctx |
| 74 | ret false |
| 75 | end |
| 76 | pos = add pos chunk |
| 77 | end |
| 78 | ret true |
| 79 | end |
| 80 | |
| 81 | fn deVerifyAt fd int descriptor view uchar 64 stage long base ulong to bool |
| 82 | ctx = new uchar 104 0 |
| 83 | digest = new uchar 32 0 |
| 84 | sha256_init ctx |
| 85 | fds = new int 2 0 |
| 86 | put fds 0 fd |
| 87 | put fds 1 deHashOpen |
| 88 | pos = ulong 0 |
| 89 | for plane ulong 6 |
| 90 | size = ulong 5898240 |
| 91 | if ge plane 3 |
| 92 | size = 368640 |
| 93 | end |
| 94 | off = diGet descriptor add 16 mul plane 8 8 |
| 95 | if not deStagePlane fds off stage add base pos size ctx |
| 96 | if ge get fds 1 0 |
| 97 | unused = fclose get fds 1 |
| 98 | end |
| 99 | ret false |
| 100 | end |
| 101 | pos = add pos size |
| 102 | end |
| 103 | if not deHashFinish get fds 1 ctx digest |
| 104 | ret false |
| 105 | end |
| 106 | for i ulong 8 |
| 107 | if ne get digest i get descriptor add 8 i |
| 108 | ret false |
| 109 | end |
| 110 | end |
| 111 | ret true |
| 112 | end |
| 113 | |
| 114 | fn deHashPlaneOnly fds view int 2 off ulong size ulong ctx edit shactx to bool |
| 115 | buf = new uchar 1048576 0 |
| 116 | pos = ulong 0 |
| 117 | while lt pos size |
| 118 | chunk = sub size pos |
| 119 | if gt chunk len buf |
| 120 | chunk = len buf |
| 121 | end |
| 122 | if not diRead get fds 0 buf add off pos chunk |
| 123 | ret false |
| 124 | end |
| 125 | if not deHashChunk get fds 1 buf chunk ctx |
| 126 | ret false |
| 127 | end |
| 128 | pos = add pos chunk |
| 129 | end |
| 130 | ret true |
| 131 | end |
| 132 | |
| 133 | fn deVerifyOnly fd int descriptor view uchar 64 to bool |
| 134 | ctx = new uchar 104 0 |
| 135 | digest = new uchar 32 0 |
| 136 | sha256_init ctx |
| 137 | fds = new int 2 0 |
| 138 | put fds 0 fd |
| 139 | put fds 1 deHashOpen |
| 140 | for plane ulong 6 |
| 141 | size = ulong 5898240 |
| 142 | if ge plane 3 |
| 143 | size = 368640 |
| 144 | end |
| 145 | off = diGet descriptor add 16 mul plane 8 8 |
| 146 | if not deHashPlaneOnly fds off size ctx |
| 147 | if ge get fds 1 0 |
| 148 | unused = fclose get fds 1 |
| 149 | end |
| 150 | ret false |
| 151 | end |
| 152 | end |
| 153 | if not deHashFinish get fds 1 ctx digest |
| 154 | ret false |
| 155 | end |
| 156 | for i ulong 8 |
| 157 | if ne get digest i get descriptor add 8 i |
| 158 | ret false |
| 159 | end |
| 160 | end |
| 161 | ret true |
| 162 | end |
| 163 | |
| 164 | fn deWritePlaneOnly fd int off ulong stage long dest ulong size ulong to bool |
| 165 | buf = new uchar 1048576 0 |
| 166 | pos = ulong 0 |
| 167 | while lt pos size |
| 168 | chunk = sub size pos |
| 169 | if gt chunk len buf |
| 170 | chunk = len buf |
| 171 | end |
| 172 | if not diRead fd buf add off pos chunk |
| 173 | ret false |
| 174 | end |
| 175 | if ne mapwrite stage add dest pos buf 0 chunk long chunk |
| 176 | ret false |
| 177 | end |
| 178 | pos = add pos chunk |
| 179 | end |
| 180 | ret true |
| 181 | end |
| 182 | |
| 183 | fn deWriteVerifiedAt fd int descriptor view uchar 64 stage long base ulong to bool |
| 184 | pos = ulong 0 |
| 185 | for plane ulong 6 |
| 186 | size = ulong 5898240 |
| 187 | if ge plane 3 |
| 188 | size = 368640 |
| 189 | end |
| 190 | off = diGet descriptor add 16 mul plane 8 8 |
| 191 | if not deWritePlaneOnly fd off stage add base pos size |
| 192 | ret false |
| 193 | end |
| 194 | pos = add pos size |
| 195 | end |
| 196 | ret true |
| 197 | end |
| 198 | |
| 199 | fn deVerifyStage fd int descriptor view uchar 64 stage long to bool |
| 200 | ret deVerifyAt fd descriptor stage 0 |
| 201 | end |
| 202 | |
| 203 | fn deCopy stage long arena long dest ulong to bool |
| 204 | buf = new uchar 262144 0 |
| 205 | pos = ulong 0 |
| 206 | while lt pos deBytes |
| 207 | chunk = sub deBytes pos |
| 208 | if gt chunk len buf |
| 209 | chunk = len buf |
| 210 | end |
| 211 | if ne mapread stage pos buf 0 chunk long chunk |
| 212 | ret false |
| 213 | end |
| 214 | if ne mapwrite arena add dest pos buf 0 chunk long chunk |
| 215 | ret false |
| 216 | end |
| 217 | pos = add pos chunk |
| 218 | end |
| 219 | ret true |
| 220 | end |
| 221 | |
| 222 | fn deResidentDigest arena long dest ulong descriptor view uchar 64 to bool |
| 223 | ctx = new uchar 104 0 |
| 224 | digest = new uchar 32 0 |
| 225 | buf = new uchar 262144 0 |
| 226 | sha256_init ctx |
| 227 | pos = ulong 0 |
| 228 | while lt pos deBytes |
| 229 | chunk = sub deBytes pos |
| 230 | if gt chunk len buf |
| 231 | chunk = len buf |
| 232 | end |
| 233 | if ne mapread arena add dest pos buf 0 chunk long chunk |
| 234 | ret false |
| 235 | end |
| 236 | sha256_update ctx part buf 0 chunk |
| 237 | pos = add pos chunk |
| 238 | end |
| 239 | sha256_final ctx digest |
| 240 | for i ulong 8 |
| 241 | if ne get digest i get descriptor add 8 i |
| 242 | ret false |
| 243 | end |
| 244 | end |
| 245 | ret true |
| 246 | end |
| 247 | |
| 248 | fn deFind slots view uchar * descriptor view uchar 64 to long |
| 249 | for i ulong div len slots 64 |
| 250 | pos = mul i 64 |
| 251 | if eq get slots add pos 5 2 |
| 252 | if eq diGet slots pos 2 ulong get descriptor 1 |
| 253 | if eq diGet slots add pos 2 2 diGet descriptor 4 4 |
| 254 | if ne diGet slots add pos 32 8 diGet descriptor 8 8 |
| 255 | ret -2 |
| 256 | end |
| 257 | ret long i |
| 258 | end |
| 259 | end |
| 260 | end |
| 261 | end |
| 262 | ret -1 |
| 263 | end |
| 264 | |
| 265 | fn deResolve h edit zonehdr slots edit uchar * descriptor view uchar 64 maps view long 2 fd int to long |
| 266 | if or ne get ar_compute_kind 0 1 ne get ar_compute_pid 0 long getpid |
| 267 | ret -2 |
| 268 | end |
| 269 | if or ne get h owner 1 not arena_shape h slots |
| 270 | ret -2 |
| 271 | end |
| 272 | if or ge get descriptor 1 40 ne ulong get descriptor 0 add 3 ulong get descriptor 1 |
| 273 | ret -2 |
| 274 | end |
| 275 | if ge diGet descriptor 4 4 384 |
| 276 | ret -2 |
| 277 | end |
| 278 | hit = deFind slots descriptor |
| 279 | if ne hit -1 |
| 280 | ret hit |
| 281 | end |
| 282 | if not deVerifyStage fd descriptor get maps 0 |
| 283 | ret -2 |
| 284 | end |
| 285 | slot = z3_claim_fifo_slot h slots |
| 286 | if lt slot 0 |
| 287 | ret -1 |
| 288 | end |
| 289 | base = mul ulong slot 64 |
| 290 | dest = mul ulong slot deBytes |
| 291 | put slots add base 5 1 |
| 292 | if not deCopy get maps 0 get maps 1 dest |
| 293 | put slots add base 5 0 |
| 294 | ret -2 |
| 295 | end |
| 296 | if not deResidentDigest get maps 1 dest descriptor |
| 297 | put slots add base 5 0 |
| 298 | ret -2 |
| 299 | end |
| 300 | for j ulong 64 |
| 301 | put slots add base j 0 |
| 302 | end |
| 303 | diPut slots base 2 ulong get descriptor 1 |
| 304 | diPut slots add base 2 2 diGet descriptor 4 4 |
| 305 | note Lane 1 encodes the FIFO zone as 2 and full-width planes as 3. |
| 306 | put slots add base 4 2 |
| 307 | put slots add base 6 3 |
| 308 | diPut slots add base 16 8 dest |
| 309 | diPut slots add base 32 8 diGet descriptor 8 8 |
| 310 | put slots add base 5 2 |
| 311 | ret slot |
| 312 | end |