sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / gpu / cuda-crack / check / reencode.om
| 1 | note check/reencode.om: rebuild a kernel's machine code from the isa/sm121-forms.txt tables alone. |
| 2 | note stdin: the table text, a line "E", then the kernel bytes (16 bytes per instruction). |
| 3 | note First the D records are checked against the F records: each F id equals its position, each field |
| 4 | note number 0..6 appears at most once per form, operand fields of one form never overlap, their union |
| 5 | note is exactly the form's mask, and a field with width > 0 is exactly the bits start .. start+width-1. |
| 6 | note For each instruction: find the first form whose fixed bits equal the instruction's bits outside |
| 7 | note the form's operand fields (words 0..2), then write fixed bits or the operand-field bits, and the |
| 8 | note control word 3 unchanged. Unmatched instructions are written as zero. |
| 9 | note stdout: the rebuilt bytes. stderr: "N U", instructions read and instructions no form explains. |
| 10 | note exit 0 all matched; 2 read error or input too big; 3 write error; 4 unmatched; 5 bad length; 6 no E line; |
| 11 | note 7 the F and D records disagree |
| 12 | |
| 13 | global inb uchar 8388608 |
| 14 | global ip ulong 1 |
| 15 | global tf ulong 32768 |
| 16 | global nt ulong 1 |
| 17 | global ow ulong 4 |
| 18 | global obuf uchar 4194304 |
| 19 | global dg uchar 32 |
| 20 | global lb uchar 64 |
| 21 | global ln ulong 1 |
| 22 | global du ulong 12288 |
| 23 | global dm ulong 3 |
| 24 | global ex ulong 3 |
| 25 | global bad ulong 1 |
| 26 | global dmax ulong 1 |
| 27 | global df ulong 4096 |
| 28 | |
| 29 | note mark the table inconsistent |
| 30 | fn flag to void |
| 31 | put bad 0 1 |
| 32 | end |
| 33 | |
| 34 | fn rdall to long |
| 35 | t = ulong 0 |
| 36 | while lt t len inb bound 1000000 |
| 37 | k = read 0 inb t sub len inb t |
| 38 | if lt k 0 |
| 39 | ret k |
| 40 | end |
| 41 | if eq k 0 |
| 42 | ret long t |
| 43 | end |
| 44 | t = add t ulong k |
| 45 | end |
| 46 | ret neg long 27 |
| 47 | end |
| 48 | |
| 49 | note the next decimal number at ip, skipping spaces |
| 50 | fn num to ulong |
| 51 | p = get ip 0 |
| 52 | while eq get inb p 32 bound 1000 |
| 53 | p = add p 1 |
| 54 | end |
| 55 | v = ulong 0 |
| 56 | while and ge get inb p 48 le get inb p 57 bound 24 |
| 57 | v = add mul v 10 sub ulong get inb p 48 |
| 58 | p = add p 1 |
| 59 | end |
| 60 | put ip 0 p |
| 61 | ret v |
| 62 | end |
| 63 | |
| 64 | fn skipline n ulong to void |
| 65 | p = get ip 0 |
| 66 | while and lt p n ne get inb p 10 bound 8388608 |
| 67 | p = add p 1 |
| 68 | end |
| 69 | put ip 0 add p 1 |
| 70 | end |
| 71 | |
| 72 | note one F record: id op sel fix0 fix1 fix2 msk0 msk1 msk2 ctl, kept as op fix0..2 msk0..2 |
| 73 | fn readf to void |
| 74 | put ip 0 add get ip 0 1 |
| 75 | id = num |
| 76 | j = get nt 0 |
| 77 | if ne id j |
| 78 | flag |
| 79 | end |
| 80 | b = mul j 8 |
| 81 | put tf b num |
| 82 | sel = num |
| 83 | for k ulong 6 |
| 84 | put tf add b add k 1 num |
| 85 | end |
| 86 | if lt j 4095 |
| 87 | put nt 0 add j 1 |
| 88 | end |
| 89 | end |
| 90 | |
| 91 | note OR the D record's mask (in dm) into form id's field union; overlapping fields are an error |
| 92 | fn dunion id ulong to void |
| 93 | b = mul id 3 |
| 94 | for k ulong 3 |
| 95 | m = get dm k |
| 96 | o = get du add b k |
| 97 | if ne and o m 0 |
| 98 | flag |
| 99 | end |
| 100 | put du add b k or o m |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | note each field number 0..6 appears at most once per form |
| 105 | fn dfield id ulong fd ulong to void |
| 106 | if lt fd 7 |
| 107 | bit = shl ulong 1 fd |
| 108 | o = get df id |
| 109 | if ne and o bit 0 |
| 110 | flag |
| 111 | end |
| 112 | put df id or o bit |
| 113 | else |
| 114 | flag |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | note the mask of wd consecutive instruction bits from bit st, into ex |
| 119 | fn span st ulong wd ulong to void |
| 120 | for k ulong 3 |
| 121 | put ex k 0 |
| 122 | end |
| 123 | for i ulong wd |
| 124 | c = add st i |
| 125 | if lt c 96 |
| 126 | w = div c 32 |
| 127 | put ex w or get ex w shl ulong 1 and c 31 |
| 128 | else |
| 129 | flag |
| 130 | end |
| 131 | end |
| 132 | end |
| 133 | |
| 134 | note a linear field's mask must be exactly its span |
| 135 | fn dspan st ulong wd ulong to void |
| 136 | span st wd |
| 137 | for k ulong 3 |
| 138 | if ne get ex k get dm k |
| 139 | flag |
| 140 | end |
| 141 | end |
| 142 | end |
| 143 | |
| 144 | note one D record: id field m0 m1 m2 start width |
| 145 | fn readd to void |
| 146 | put ip 0 add get ip 0 1 |
| 147 | id = num |
| 148 | fd = num |
| 149 | for k ulong 3 |
| 150 | put dm k num |
| 151 | end |
| 152 | st = num |
| 153 | wd = num |
| 154 | if lt id 4096 |
| 155 | dunion id |
| 156 | dfield id fd |
| 157 | if gt id get dmax 0 |
| 158 | put dmax 0 id |
| 159 | end |
| 160 | else |
| 161 | flag |
| 162 | end |
| 163 | if gt wd 96 |
| 164 | flag |
| 165 | else |
| 166 | if gt wd 0 |
| 167 | dspan st wd |
| 168 | end |
| 169 | end |
| 170 | end |
| 171 | |
| 172 | note form j's field union must equal its mask |
| 173 | fn dform j ulong to void |
| 174 | for k ulong 3 |
| 175 | u = get du add mul j 3 k |
| 176 | m = get tf add mul j 8 add k 4 |
| 177 | if ne u m |
| 178 | flag |
| 179 | end |
| 180 | end |
| 181 | end |
| 182 | |
| 183 | note every D record names a listed form, and each form's field union equals its mask |
| 184 | fn dcheck to void |
| 185 | n = get nt 0 |
| 186 | if ge get dmax 0 n |
| 187 | flag |
| 188 | end |
| 189 | for j ulong n |
| 190 | dform j |
| 191 | end |
| 192 | end |
| 193 | |
| 194 | note parse records up to the E line; the offset of the kernel bytes, or 0 when no E line |
| 195 | fn table n ulong to ulong |
| 196 | put ip 0 0 |
| 197 | while lt get ip 0 n bound 1000000 |
| 198 | c = get inb get ip 0 |
| 199 | if eq c 69 |
| 200 | skipline n |
| 201 | ret get ip 0 |
| 202 | end |
| 203 | if eq c 70 |
| 204 | readf |
| 205 | end |
| 206 | if eq c 68 |
| 207 | readd |
| 208 | end |
| 209 | skipline n |
| 210 | end |
| 211 | ret 0 |
| 212 | end |
| 213 | |
| 214 | fn rdw q ulong to ulong |
| 215 | a = ulong get inb q |
| 216 | b = shl ulong get inb add q 1 8 |
| 217 | c = shl ulong get inb add q 2 16 |
| 218 | d = shl ulong get inb add q 3 24 |
| 219 | ret or or a b or c d |
| 220 | end |
| 221 | |
| 222 | note 1 when form j explains the instruction at q |
| 223 | fn fits j ulong q ulong to bool |
| 224 | b = mul j 8 |
| 225 | ok = true |
| 226 | for k ulong 3 |
| 227 | x = rdw add q mul k 4 |
| 228 | m = get tf add b add k 4 |
| 229 | if ne and x xor m 4294967295 get tf add b add k 1 |
| 230 | ok = false |
| 231 | end |
| 232 | end |
| 233 | ret ok |
| 234 | end |
| 235 | |
| 236 | note the first form that explains the instruction at q, or nt when none does |
| 237 | fn find q ulong to ulong |
| 238 | r = get nt 0 |
| 239 | for j ulong get nt 0 |
| 240 | if and eq r get nt 0 fits j q |
| 241 | r = j |
| 242 | end |
| 243 | end |
| 244 | ret r |
| 245 | end |
| 246 | |
| 247 | fn putw o ulong x ulong to void |
| 248 | for j ulong 4 |
| 249 | put obuf add o j uchar and shr x mul j 8 255 |
| 250 | end |
| 251 | end |
| 252 | |
| 253 | note word k of form b's instruction: fixed bits, or the input's bits inside the operand fields |
| 254 | fn rword b ulong q ulong k ulong to ulong |
| 255 | x = rdw add q mul k 4 |
| 256 | m = get tf add b add k 4 |
| 257 | f = get tf add b add k 1 |
| 258 | ret or f and x m |
| 259 | end |
| 260 | |
| 261 | note rebuild instruction i (input at q) from form j into obuf |
| 262 | fn rebuild i ulong q ulong j ulong to void |
| 263 | b = mul j 8 |
| 264 | o = mul i 16 |
| 265 | for k ulong 3 |
| 266 | y = rword b q k |
| 267 | putw add o mul k 4 y |
| 268 | end |
| 269 | putw add o 12 rdw add q 12 |
| 270 | end |
| 271 | |
| 272 | fn pch c ulong to void |
| 273 | p = get ln 0 |
| 274 | put lb p uchar c |
| 275 | put ln 0 add p 1 |
| 276 | end |
| 277 | |
| 278 | fn pnum v ulong to void |
| 279 | n = ulong 0 |
| 280 | x = v |
| 281 | while gt x 0 bound 32 |
| 282 | q = div x 10 |
| 283 | put dg n uchar add 48 sub x mul q 10 |
| 284 | x = q |
| 285 | n = add n 1 |
| 286 | end |
| 287 | if eq n 0 |
| 288 | pch 48 |
| 289 | end |
| 290 | k = n |
| 291 | while gt k 0 bound 32 |
| 292 | k = sub k 1 |
| 293 | pch ulong get dg k |
| 294 | end |
| 295 | end |
| 296 | |
| 297 | fn wrall n ulong to long |
| 298 | d = ulong 0 |
| 299 | while lt d n bound 1000000 |
| 300 | w = write 1 obuf d sub n d |
| 301 | if le w 0 |
| 302 | ret w |
| 303 | end |
| 304 | d = add d ulong w |
| 305 | end |
| 306 | ret long d |
| 307 | end |
| 308 | |
| 309 | note "N U" on stderr: instructions read, instructions no form explains |
| 310 | fn report n ulong u ulong to void |
| 311 | put ln 0 0 |
| 312 | pnum n |
| 313 | pch 32 |
| 314 | pnum u |
| 315 | pch 10 |
| 316 | w = write 2 lb 0 get ln 0 |
| 317 | end |
| 318 | |
| 319 | fn main to int |
| 320 | r = rdall |
| 321 | if lt r 0 |
| 322 | ret 2 |
| 323 | end |
| 324 | n = ulong r |
| 325 | ks = table n |
| 326 | if eq ks 0 |
| 327 | ret 6 |
| 328 | end |
| 329 | dcheck |
| 330 | if ne get bad 0 0 |
| 331 | ret 7 |
| 332 | end |
| 333 | kl = sub n ks |
| 334 | if ne and kl 15 0 |
| 335 | ret 5 |
| 336 | end |
| 337 | ni = div kl 16 |
| 338 | u = ulong 0 |
| 339 | for i ulong ni |
| 340 | q = add ks mul i 16 |
| 341 | j = find q |
| 342 | if lt j get nt 0 |
| 343 | rebuild i q j |
| 344 | else |
| 345 | u = add u 1 |
| 346 | end |
| 347 | end |
| 348 | report ni u |
| 349 | w = wrall kl |
| 350 | if ne w long kl |
| 351 | ret 3 |
| 352 | end |
| 353 | if ne u 0 |
| 354 | ret 4 |
| 355 | end |
| 356 | ret 0 |
| 357 | end |