Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/physics-mv/blob/main/m3/capability.s # physics-mv FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `physics-mv / m3 / capability.s` 533 lines ยท 15512 bytes Raw Blame History 1 /* 2 * m3/capability.s - Capability Ledger, Genesis Root, Attenuation & Revocation 3 * Milestone 3: PHYSICS_EFFECTS (Sovereign Machine) 4 * 5 * Fixed-capacity (32 records) kernel-private capability ledger. 6 * No heap, no dynamic memory allocation. Checked arithmetic. 7 * Storage: PHYSICS_CAPABILITY_TABLE @ 0x40209000 (4 KiB) 8 */ 9 10 .global init_capability_table 11 .global lookup_capability 12 .global derive_capability 13 .global revoke_capability 14 15 .extern sha256_be 16 .extern print_string 17 18 .section .text, "ax" 19 .balign 4 20 21 .equ CAP_TABLE_BASE, 0x40209000 22 .equ CAP_TABLE_CAPACITY, 32 23 .equ CAP_RECORD_SIZE, 128 24 .equ CAP_RECORD_SHIFT, 7 25 26 .equ RES_UNIVERSAL_ROOT, 1 27 .equ RES_MEMORY_FRAME, 2 28 .equ RES_CONSOLE, 3 29 .equ RES_MEASUREMENT, 4 30 31 .equ OP_ALL_AUTHORITY, 0xFFFFFFFF 32 .equ OP_FRAME_GRANT, 1 33 .equ OP_FRAME_RELEASE, 2 34 .equ OP_CONSOLE_WRITE, 1 35 .equ OP_MEASUREMENT_READ, 1 36 37 .equ STATE_ACTIVE, 1 38 .equ STATE_REVOKED, 2 39 40 .equ DEC_ADMITTED, 1 41 .equ DEC_REJECTED_STRUCTURAL, 2 42 .equ DEC_REJECTED_UNKNOWN_CAP, 3 43 .equ DEC_REJECTED_STALE_GENERATION, 4 44 .equ DEC_REJECTED_REVOKED, 5 45 .equ DEC_REJECTED_WRONG_PRINCIPAL, 6 46 .equ DEC_REJECTED_OPERATION, 7 47 .equ DEC_REJECTED_RESOURCE, 8 48 .equ DEC_REJECTED_BOUNDS, 9 49 .equ DEC_REJECTED_CONSTRAINT, 10 50 .equ DEC_REJECTED_REPLAY_CONFLICT, 11 51 .equ DEC_REJECTED_EXHAUSTED, 12 52 .equ DEC_EXECUTION_FAILED, 13 53 54 /* 55 * init_capability_table(): 56 * Zeros the 4 KiB capability table, then synthesizes CAP_ROOT at slot 0: 57 * +0x00: slot = 0, generation = 1 58 * +0x08: principal_id = 1 (Physics) 59 * +0x10: resource_type = 1 (RES_UNIVERSAL_ROOT), allowed_ops = 0xFFFFFFFF 60 * +0x18: bound_base = 0x40210000 (FREE_FRAME_BASE) 61 * +0x20: bound_size = 0x07DF0000 (DRAM_END - FREE_FRAME_BASE = 32,240 frames) 62 * +0x28: revocation_state = 1 (ACTIVE), attenuation_depth = 0 63 * +0x30: provenance_digest (32 bytes = atlas.sha256) 64 * +0x50: parent_slot = 0xFFFFFFFF, parent_generation = 0xFFFFFFFF 65 * +0x58: lifetime = 0 (unbounded) 66 * +0x60: reserved (32 zero bytes) 67 */ 68 init_capability_table: 69 stp x29, x30, [sp, #-16]! 70 mov x29, sp 71 72 /* Zero all 4096 bytes of the capability table */ 73 ldr x0, =CAP_TABLE_BASE 74 mov x1, #4096 75 .Lzero_cap_loop: 76 cbz x1, .Lzero_cap_done 77 stp xzr, xzr, [x0], #16 78 sub x1, x1, #16 79 b .Lzero_cap_loop 80 81 .Lzero_cap_done: 82 ldr x0, =CAP_TABLE_BASE /* Target: Slot 0 */ 83 84 /* +0x00: slot = 0, generation = 1 */ 85 mov w1, #0 86 str w1, [x0, #0] 87 mov w1, #1 88 str w1, [x0, #4] 89 90 /* +0x08: principal_id = 1 (Physics) */ 91 mov x1, #1 92 str x1, [x0, #8] 93 94 /* +0x10: resource_type = 1 (RES_UNIVERSAL_ROOT), allowed_ops = 0xFFFFFFFF */ 95 mov w1, #RES_UNIVERSAL_ROOT 96 str w1, [x0, #16] 97 mov w1, #-1 98 str w1, [x0, #20] 99 100 /* +0x18: bound_base = 0x40210000 (FREE_FRAME_BASE) */ 101 ldr x1, =0x40210000 102 str x1, [x0, #24] 103 104 /* +0x20: bound_size = 0x07DF0000 */ 105 ldr x1, =0x07DF0000 106 str x1, [x0, #32] 107 108 /* +0x28: revocation_state = 1 (ACTIVE), attenuation_depth = 0 */ 109 mov w1, #STATE_ACTIVE 110 str w1, [x0, #40] 111 mov w1, #0 112 str w1, [x0, #44] 113 114 /* +0x30: Pinned provenance digest from atlas.sha256 (32 bytes) */ 115 adr x1, pinned_atlas_sha256 116 ldp x2, x3, [x1, #0] 117 ldp x4, x5, [x1, #16] 118 stp x2, x3, [x0, #48] 119 stp x4, x5, [x0, #64] 120 121 /* +0x50: parent_slot = -1, parent_generation = -1 */ 122 mov w1, #-1 123 str w1, [x0, #0x50] 124 str w1, [x0, #0x54] 125 126 /* +0x58: lifetime = 0 (unbounded) */ 127 str xzr, [x0, #0x58] 128 129 ldp x29, x30, [sp], #16 130 ret 131 132 /* 133 * lookup_capability(slot, generation, principal_id) 134 * w0: slot, w1: generation, x2: principal_id 135 * Returns: 136 * x0: Pointer to CapabilityRecord (or 0 if invalid/denied) 137 * w1: Decision / rejection code (DEC_ADMITTED or DEC_REJECTED_*) 138 */ 139 lookup_capability: 140 stp x29, x30, [sp, #-32]! 141 mov x29, sp 142 stp x19, x20, [sp, #16] 143 144 /* 1. Slot range check */ 145 cmp w0, #CAP_TABLE_CAPACITY 146 b.hs .Llookup_unknown 147 148 /* 2. Compute record address: CAP_TABLE_BASE + (slot << 7) */ 149 ldr x19, =CAP_TABLE_BASE 150 ubfiz x3, x0, #CAP_RECORD_SHIFT, #5 151 add x19, x19, x3 /* x19 = record ptr */ 152 153 /* 3. Check record allocated (generation > 0) */ 154 ldr w4, [x19, #4] /* record.generation */ 155 cbz w4, .Llookup_unknown 156 157 /* 4. Generation check: presented == record */ 158 cmp w1, w4 159 b.ne .Llookup_stale 160 161 /* 5. Immediate revocation check */ 162 ldr w5, [x19, #40] /* record.revocation_state */ 163 cmp w5, #STATE_ACTIVE 164 b.ne .Llookup_revoked 165 166 /* 6. Principal check */ 167 ldr x6, [x19, #8] /* record.principal_id */ 168 cmp x2, x6 169 b.ne .Llookup_wrong_principal 170 171 /* 7. Ancestor-chain validation on use */ 172 ldr w7, [x19, #0] /* slot */ 173 cbz w7, .Llookup_success /* CAP_ROOT has no parent, admitted */ 174 175 mov x20, x19 /* x20 = current node in traversal */ 176 mov w8, #8 /* loop guard (max depth 8) */ 177 178 .Lancestor_loop: 179 cbz w8, .Llookup_revoked /* depth exceeded fail-closed */ 180 sub w8, w8, #1 181 182 ldr w9, [x20, #0x50] /* parent_slot */ 183 ldr w10, [x20, #0x54] /* parent_generation */ 184 185 /* If parent_slot is CAP_ROOT (0), check slot 0 */ 186 cmp w9, #CAP_TABLE_CAPACITY 187 b.hs .Llookup_revoked 188 189 ldr x11, =CAP_TABLE_BASE 190 ubfiz x12, x9, #CAP_RECORD_SHIFT, #5 191 add x11, x11, x12 /* x11 = parent record ptr */ 192 193 /* Parent generation check */ 194 ldr w13, [x11, #4] 195 cmp w10, w13 196 b.ne .Llookup_stale 197 198 /* Parent revocation state check */ 199 ldr w14, [x11, #40] 200 cmp w14, #STATE_ACTIVE 201 b.ne .Llookup_revoked 202 203 /* If parent is slot 0 (CAP_ROOT), ancestor chain fully verified! */ 204 cbz w9, .Llookup_success 205 206 /* Advance to parent */ 207 mov x20, x11 208 b .Lancestor_loop 209 210 .Llookup_success: 211 mov x0, x19 212 mov w1, #DEC_ADMITTED 213 ldp x19, x20, [sp, #16] 214 ldp x29, x30, [sp], #32 215 ret 216 217 .Llookup_unknown: 218 mov x0, #0 219 mov w1, #DEC_REJECTED_UNKNOWN_CAP 220 ldp x19, x20, [sp, #16] 221 ldp x29, x30, [sp], #32 222 ret 223 224 .Llookup_stale: 225 mov x0, #0 226 mov w1, #DEC_REJECTED_STALE_GENERATION 227 ldp x19, x20, [sp, #16] 228 ldp x29, x30, [sp], #32 229 ret 230 231 .Llookup_revoked: 232 mov x0, #0 233 mov w1, #DEC_REJECTED_REVOKED 234 ldp x19, x20, [sp, #16] 235 ldp x29, x30, [sp], #32 236 ret 237 238 .Llookup_wrong_principal: 239 mov x0, #0 240 mov w1, #DEC_REJECTED_WRONG_PRINCIPAL 241 ldp x19, x20, [sp, #16] 242 ldp x29, x30, [sp], #32 243 ret 244 245 /* 246 * derive_capability( 247 * w0: parent_slot, 248 * w1: parent_generation, 249 * x2: caller_principal, 250 * x3: child_principal, 251 * w4: requested_resource_type, 252 * w5: requested_ops, 253 * x6: requested_base, 254 * x7: requested_size, 255 * x8: requested_lifetime [passed in x8] 256 * ) 257 * Returns: 258 * x0: (new_generation << 32) | child_slot, or 0 on error. 259 */ 260 derive_capability: 261 stp x29, x30, [sp, #-192]! 262 mov x29, sp 263 stp x19, x20, [sp, #16] 264 stp x21, x22, [sp, #32] 265 stp x23, x24, [sp, #48] 266 stp x25, x26, [sp, #64] 267 stp x27, x28, [sp, #80] 268 269 /* Save arguments into callee-saved registers */ 270 mov x22, x3 /* child_principal */ 271 mov w23, w4 /* rtype */ 272 mov w24, w5 /* ops */ 273 mov x25, x6 /* base */ 274 mov x26, x7 /* size */ 275 mov x27, x8 /* lifetime */ 276 277 /* 1. Lookup and validate parent (w0, w1, x2 already set) */ 278 bl lookup_capability 279 cbz x0, .Lderive_fail 280 mov x19, x0 /* x19 = validated parent record ptr */ 281 282 /* 2. Child principal cannot be 0 */ 283 cbz x22, .Lderive_fail 284 285 /* 3. Resource type containment */ 286 ldr w4, [x19, #16] /* parent.resource_type */ 287 cmp w4, #RES_UNIVERSAL_ROOT 288 b.eq .Lcheck_root_narrowing 289 /* Specific resource: child must match exactly */ 290 cmp w23, w4 291 b.ne .Lderive_fail 292 b .Lcheck_ops 293 294 .Lcheck_root_narrowing: 295 /* Narrowing from root: child can be MEMORY_FRAME, CONSOLE, or MEASUREMENT */ 296 cmp w23, #RES_MEMORY_FRAME 297 b.eq .Lcheck_ops 298 cmp w23, #RES_CONSOLE 299 b.eq .Lcheck_ops 300 cmp w23, #RES_MEASUREMENT 301 b.eq .Lcheck_ops 302 b .Lderive_fail /* Cannot derive ROOT or unknown type */ 303 304 .Lcheck_ops: 305 /* 4. Rights narrowing: (child_ops & ~parent_ops) == 0 */ 306 cbz w24, .Lderive_fail /* Cannot have 0 ops */ 307 ldr w5, [x19, #20] /* parent.allowed_ops */ 308 mvn w6, w5 309 tst w24, w6 310 b.ne .Lderive_fail /* Child has forbidden rights */ 311 312 /* 5. Bounds containment with checked arithmetic */ 313 ldr w4, [x19, #16] /* parent.resource_type */ 314 cmp w4, #RES_UNIVERSAL_ROOT 315 b.ne .Lcheck_standard_bounds 316 317 /* Parent is CAP_ROOT: check bounds according to requested concrete resource */ 318 cmp w23, #RES_CONSOLE 319 b.eq .Lcheck_root_console_bounds 320 cmp w23, #RES_MEASUREMENT 321 b.eq .Lcheck_root_measurement_bounds 322 323 /* Otherwise MEMORY_FRAME: fall through to check standard bounds within DRAM */ 324 325 .Lcheck_standard_bounds: 326 ldr x5, [x19, #24] /* parent.bound_base */ 327 ldr x6, [x19, #32] /* parent.bound_size */ 328 329 /* Check child base >= parent base */ 330 cmp x25, x5 331 b.lo .Lderive_fail 332 333 /* Check child base + size overflow */ 334 adds x7, x25, x26 /* x7 = child end */ 335 b.cs .Lderive_fail 336 337 /* Check parent base + size overflow */ 338 adds x8, x5, x6 /* x8 = parent end */ 339 b.cs .Lderive_fail 340 341 /* Check child end <= parent end */ 342 cmp x7, x8 343 b.hi .Lderive_fail 344 345 /* For memory frame: size must be > 0 and 4 KiB aligned */ 346 cmp w23, #RES_MEMORY_FRAME 347 b.ne .Lcheck_lifetime 348 cbz x26, .Lderive_fail 349 tst x25, #0xFFF 350 b.ne .Lderive_fail 351 tst x26, #0xFFF 352 b.ne .Lderive_fail 353 b .Lcheck_lifetime 354 355 .Lcheck_root_console_bounds: 356 ldr x5, =0x09000000 /* PL011 UART MMIO base */ 357 cmp x25, x5 358 b.ne .Lderive_fail 359 cmp x26, #0x1000 360 b.hi .Lderive_fail 361 cbz x26, .Lderive_fail 362 b .Lcheck_lifetime 363 364 .Lcheck_root_measurement_bounds: 365 cbnz x25, .Lderive_fail 366 cbnz x26, .Lderive_fail 367 b .Lcheck_lifetime 368 369 .Lcheck_lifetime: 370 /* 6. Lifetime narrowing: if parent.lifetime != 0, child <= parent and child != 0 */ 371 ldr x9, [x19, #0x58] /* parent.lifetime */ 372 cbz x9, .Lcheck_depth 373 cbz x27, .Lderive_fail /* Cannot widen to unbounded */ 374 cmp x27, x9 375 b.hi .Lderive_fail 376 377 .Lcheck_depth: 378 /* 7. Attenuation depth: parent.depth + 1 <= 8 */ 379 ldr w10, [x19, #44] /* parent.attenuation_depth */ 380 cmp w10, #8 381 b.ge .Lderive_fail 382 add w20, w10, #1 /* w20 = child depth */ 383 384 /* 8. Find a free slot in [1..31] */ 385 ldr x11, =CAP_TABLE_BASE 386 mov w21, #1 /* slot index 1..31 in callee-saved w21 */ 387 .Lsearch_slot: 388 cmp w21, #CAP_TABLE_CAPACITY 389 b.ge .Lderive_fail /* Table full */ 390 391 ubfiz x13, x21, #CAP_RECORD_SHIFT, #5 392 add x28, x11, x13 /* x28 = candidate slot ptr (callee-saved) */ 393 394 ldr w15, [x28, #40] /* slot.revocation_state */ 395 cmp w15, #STATE_ACTIVE 396 b.ne .Lslot_found 397 398 add w21, w21, #1 399 b .Lsearch_slot 400 401 .Lslot_found: 402 /* Generation bumping */ 403 ldr w16, [x28, #4] /* old generation */ 404 add w16, w16, #1 405 cbnz w16, 1f 406 /* Wrapped to 0? Generation wrap fails closed */ 407 add w21, w21, #1 408 b .Lsearch_slot 409 1: 410 str w16, [sp, #184] /* Save new_generation on stack */ 411 412 /* 9. Compute Provenance Digest: SHA-256 of parent_prov (32B) + child fields (48B) = 80B */ 413 /* Construct hash buffer on stack at [sp + 96] */ 414 add x0, sp, #96 415 /* Copy 32 bytes parent provenance */ 416 add x1, x19, #48 /* parent.provenance */ 417 ldp x2, x3, [x1, #0] 418 ldp x4, x5, [x1, #16] 419 stp x2, x3, [x0, #0] 420 stp x4, x5, [x0, #16] 421 422 /* Copy child fields: child_principal (8B), rtype (4B), ops (4B), base (8B), size (8B), depth (4B), slot (4B), lifetime (8B) */ 423 str x22, [x0, #32] /* child_principal */ 424 str w23, [x0, #40] /* rtype */ 425 str w24, [x0, #44] /* ops */ 426 str x25, [x0, #48] /* base */ 427 str x26, [x0, #56] /* size */ 428 str w20, [x0, #64] /* child_depth */ 429 str w21, [x0, #68] /* child_slot (binds identity) */ 430 str x27, [x0, #72] /* lifetime */ 431 432 /* Call sha256_be(data, len=80, out) */ 433 /* Target child provenance location: x28 + 48 */ 434 mov x1, #80 435 add x2, x28, #48 /* child.provenance */ 436 bl sha256_be 437 cbnz w0, .Lderive_fail 438 439 /* 10. Write child record fields into slot (x28 is preserved!) */ 440 ldr w16, [sp, #184] /* reload new generation */ 441 str w21, [x28, #0] /* slot */ 442 str w16, [x28, #4] /* generation */ 443 str x22, [x28, #8] /* principal_id */ 444 str w23, [x28, #16] /* resource_type */ 445 str w24, [x28, #20] /* allowed_ops */ 446 str x25, [x28, #24] /* bound_base */ 447 str x26, [x28, #32] /* bound_size */ 448 mov w1, #STATE_ACTIVE 449 str w1, [x28, #40] /* revocation_state */ 450 str w20, [x28, #44] /* attenuation_depth */ 451 ldr w2, [x19, #0] /* parent.slot */ 452 str w2, [x28, #0x50] /* parent_slot */ 453 ldr w3, [x19, #4] /* parent.generation */ 454 str w3, [x28, #0x54] /* parent_generation */ 455 str x27, [x28, #0x58] /* lifetime */ 456 457 /* Zero reserved bytes at +0x60..+0x7F */ 458 stp xzr, xzr, [x28, #0x60] 459 stp xzr, xzr, [x28, #0x70] 460 461 /* Return (generation << 32) | slot */ 462 ubfiz x0, x16, #32, #32 463 orr x0, x0, x21 464 465 ldp x19, x20, [sp, #16] 466 ldp x21, x22, [sp, #32] 467 ldp x23, x24, [sp, #48] 468 ldp x25, x26, [sp, #64] 469 ldp x27, x28, [sp, #80] 470 ldp x29, x30, [sp], #192 471 ret 472 473 .Lderive_fail: 474 mov x0, #0 475 ldp x19, x20, [sp, #16] 476 ldp x21, x22, [sp, #32] 477 ldp x23, x24, [sp, #48] 478 ldp x25, x26, [sp, #64] 479 ldp x27, x28, [sp, #80] 480 ldp x29, x30, [sp], #192 481 ret 482 483 /* 484 * revoke_capability(slot, generation, caller_principal) 485 * w0: slot, w1: generation, x2: caller_principal 486 * Returns: 487 * x0: 1 on success, 0 on failure. 488 */ 489 revoke_capability: 490 stp x29, x30, [sp, #-16]! 491 mov x29, sp 492 493 /* Cannot revoke CAP_ROOT (slot 0) */ 494 cbz w0, .Lrevoke_fail 495 cmp w0, #CAP_TABLE_CAPACITY 496 b.ge .Lrevoke_fail 497 498 ldr x3, =CAP_TABLE_BASE 499 ubfiz x4, x0, #CAP_RECORD_SHIFT, #5 500 add x3, x3, x4 /* record ptr */ 501 502 /* Check generation matches */ 503 ldr w5, [x3, #4] 504 cmp w1, w5 505 b.ne .Lrevoke_fail 506 507 /* Check caller is owner OR Physics (principal 1) */ 508 ldr x6, [x3, #8] 509 cmp x2, x6 510 b.eq 1f 511 cmp x2, #1 512 b.ne .Lrevoke_fail 513 1: 514 /* Mark REVOKED */ 515 mov w7, #STATE_REVOKED 516 str w7, [x3, #40] 517 518 mov x0, #1 519 ldp x29, x30, [sp], #16 520 ret 521 522 .Lrevoke_fail: 523 mov x0, #0 524 ldp x29, x30, [sp], #16 525 ret 526 527 .section .rodata 528 .balign 8 529 pinned_atlas_sha256: 530 .byte 0xf7, 0x80, 0x25, 0x01, 0xb4, 0x10, 0xa0, 0xc1 531 .byte 0x9e, 0xff, 0x7b, 0x8f, 0xca, 0x88, 0x65, 0xc9 532 .byte 0xba, 0x9c, 0x96, 0xbf, 0xa4, 0xf8, 0x06, 0x5c 533 .byte 0xa8, 0xf5, 0x08, 0xb6, 0x77, 0x48, 0xb9, 0xa5