physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / m3 / effect_broker.s
| 1 | /* |
| 2 | * m3/effect_broker.s - Deterministic Effect Broker & Admission Pipeline |
| 3 | * Milestone 3: PHYSICS_EFFECTS (Sovereign Machine) |
| 4 | * |
| 5 | * Single admission path: submit_effect(intent) -> EffectReceipt. |
| 6 | * 10-stage fail-closed validation pipeline. |
| 7 | * Physical execution ONLY occurs after all admission stages pass. |
| 8 | * Every intent attempt produces an immutable cryptographic receipt. |
| 9 | */ |
| 10 | |
| 11 | .global submit_effect |
| 12 | |
| 13 | .extern sha256_be |
| 14 | .extern lookup_capability |
| 15 | .extern lookup_replay |
| 16 | .extern record_replay |
| 17 | .extern is_receipt_ledger_full |
| 18 | .extern commit_receipt |
| 19 | .extern get_receipt_count |
| 20 | .extern allocate_frame_in_range |
| 21 | .extern release_frame |
| 22 | |
| 23 | .section .text, "ax" |
| 24 | .balign 4 |
| 25 | |
| 26 | .equ UART_MMIO_BASE, 0x09000000 |
| 27 | .equ RECEIPT_ENTRIES_BASE, 0x4020A080 |
| 28 | .equ RECEIPT_RECORD_SIZE, 192 |
| 29 | |
| 30 | .equ RES_UNIVERSAL_ROOT, 1 |
| 31 | .equ RES_MEMORY_FRAME, 2 |
| 32 | .equ RES_CONSOLE, 3 |
| 33 | .equ RES_MEASUREMENT, 4 |
| 34 | |
| 35 | .equ OP_FRAME_GRANT, 1 |
| 36 | .equ OP_FRAME_RELEASE, 2 |
| 37 | .equ OP_CONSOLE_WRITE, 1 |
| 38 | .equ OP_MEASUREMENT_READ, 1 |
| 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 | .equ REPLAY_MISS, 0 |
| 55 | .equ REPLAY_HIT, 1 |
| 56 | .equ REPLAY_CONFLICT, 2 |
| 57 | |
| 58 | /* |
| 59 | * submit_effect(intent_ptr) -> x0: committed EffectReceipt pointer (or 0) |
| 60 | * x0: intent_ptr |
| 61 | * |
| 62 | * Stack Frame Layout (192 bytes): |
| 63 | * [sp+0] x29, x30 |
| 64 | * [sp+16] x19, x20 (intent_ptr, cap_ptr) |
| 65 | * [sp+32] x21, x22 (decision, rejection_reason) |
| 66 | * [sp+48] x23, x24 (actual_effect, output) |
| 67 | * [sp+64] ReceiptData (96 bytes: 0x00..0x5F) |
| 68 | * [sp+64]: version(u16), length(u16), decision(u32) |
| 69 | * [sp+72]: rejection_reason(u32), reserved0(u32) |
| 70 | * [sp+80]: request_id(u64) |
| 71 | * [sp+88]: intent_digest(32 bytes) |
| 72 | * [sp+120]: actual_effect(u64) |
| 73 | * [sp+128]: output(u64) |
| 74 | * [sp+136]: cap_slot(u32), cap_gen(u32) |
| 75 | * [sp+144]: machine_gen(u64) |
| 76 | * [sp+152]: measurement(u64) |
| 77 | * [sp+160] 32 bytes scratch |
| 78 | */ |
| 79 | .equ FRAME_SIZE, 192 |
| 80 | .equ OFF_RECEIPT, 64 |
| 81 | |
| 82 | submit_effect: |
| 83 | stp x29, x30, [sp, #-FRAME_SIZE]! |
| 84 | mov x29, sp |
| 85 | stp x19, x20, [sp, #16] |
| 86 | stp x21, x22, [sp, #32] |
| 87 | stp x23, x24, [sp, #48] |
| 88 | |
| 89 | mov x19, x0 /* x19 = intent_ptr */ |
| 90 | mov x20, #0 /* x20 = cap_ptr (null initially) */ |
| 91 | mov w21, #DEC_ADMITTED /* w21 = decision */ |
| 92 | mov w22, #0 /* w22 = rejection_reason */ |
| 93 | mov x23, #0 /* x23 = actual_effect */ |
| 94 | mov x24, #0 /* x24 = output */ |
| 95 | |
| 96 | /* Zero ReceiptData scratchpad (96 bytes at sp+64) */ |
| 97 | add x0, sp, #OFF_RECEIPT |
| 98 | mov x1, #96 |
| 99 | .Lzero_rcpt_scratch: |
| 100 | cbz x1, .Lzero_rcpt_scratch_done |
| 101 | stp xzr, xzr, [x0], #16 |
| 102 | sub x1, x1, #16 |
| 103 | b .Lzero_rcpt_scratch |
| 104 | .Lzero_rcpt_scratch_done: |
| 105 | |
| 106 | /* ------------------------------------------------------------- |
| 107 | * Stage 1: Structural Validation |
| 108 | * ------------------------------------------------------------- */ |
| 109 | /* Check alignment of intent_ptr (must be 8-byte aligned) */ |
| 110 | tst x19, #7 |
| 111 | b.ne .Lfail_structural |
| 112 | |
| 113 | /* Check version == 1, length == 64, reserved0 == 0 */ |
| 114 | ldrh w1, [x19, #0] /* version */ |
| 115 | cmp w1, #1 |
| 116 | b.ne .Lfail_structural |
| 117 | |
| 118 | ldrh w2, [x19, #2] /* length */ |
| 119 | cmp w2, #64 |
| 120 | b.ne .Lfail_structural |
| 121 | |
| 122 | ldr w3, [x19, #4] /* reserved0 */ |
| 123 | cbnz w3, .Lfail_structural |
| 124 | b .Lstage2 |
| 125 | |
| 126 | .Lfail_structural: |
| 127 | mov w21, #DEC_REJECTED_STRUCTURAL |
| 128 | b .Lcommit_rejection |
| 129 | |
| 130 | /* ------------------------------------------------------------- |
| 131 | * Stage 2: Principal Validation |
| 132 | * ------------------------------------------------------------- */ |
| 133 | .Lstage2: |
| 134 | ldr x7, [x19, #0x10] /* principal_id */ |
| 135 | cbz x7, .Lfail_principal |
| 136 | b .Lstage3 |
| 137 | |
| 138 | .Lfail_principal: |
| 139 | mov w21, #DEC_REJECTED_WRONG_PRINCIPAL |
| 140 | b .Lcommit_rejection |
| 141 | |
| 142 | /* ------------------------------------------------------------- |
| 143 | * Stage 3: Digest Intent & Check Replay Cache |
| 144 | * ------------------------------------------------------------- */ |
| 145 | .Lstage3: |
| 146 | /* Compute SHA-256 of the 64-byte EffectIntent into ReceiptData.intent_digest (+88) */ |
| 147 | mov x0, x19 /* data */ |
| 148 | mov x1, #64 /* len */ |
| 149 | add x2, sp, #(OFF_RECEIPT + 24) /* out = sp + 88 */ |
| 150 | bl sha256_be |
| 151 | cbnz w0, .Lfail_structural |
| 152 | |
| 153 | /* Check Replay Cache: lookup_replay(request_id, intent_digest) */ |
| 154 | ldr x0, [x19, #0x08] /* request_id */ |
| 155 | add x1, sp, #(OFF_RECEIPT + 24) /* intent_digest */ |
| 156 | bl lookup_replay |
| 157 | /* w0: REPLAY_MISS (0), REPLAY_HIT (1), REPLAY_CONFLICT (2) */ |
| 158 | cmp w0, #REPLAY_CONFLICT |
| 159 | b.eq .Lfail_replay_conflict |
| 160 | cmp w0, #REPLAY_HIT |
| 161 | b.eq .Lhandle_replay_hit |
| 162 | b .Lstage4 |
| 163 | |
| 164 | .Lfail_replay_conflict: |
| 165 | mov w21, #DEC_REJECTED_REPLAY_CONFLICT |
| 166 | b .Lcommit_rejection |
| 167 | |
| 168 | .Lhandle_replay_hit: |
| 169 | /* Idempotent replay: return existing committed receipt without re-executing */ |
| 170 | ldr x2, =RECEIPT_ENTRIES_BASE |
| 171 | mov x3, #RECEIPT_RECORD_SIZE |
| 172 | madd x0, x1, x3, x2 /* x0 = existing receipt ptr */ |
| 173 | b .Lexit |
| 174 | |
| 175 | /* ------------------------------------------------------------- |
| 176 | * Stage 4: Check Ledger Space (Fail-Closed Exhaustion) |
| 177 | * ------------------------------------------------------------- */ |
| 178 | .Lstage4: |
| 179 | bl is_receipt_ledger_full |
| 180 | cbnz w0, .Lfail_exhausted |
| 181 | b .Lstage5 |
| 182 | |
| 183 | .Lfail_exhausted: |
| 184 | mov w21, #DEC_REJECTED_EXHAUSTED |
| 185 | /* On exhaustion, halt admission fail-closed without modifying memory */ |
| 186 | mov x0, #0 |
| 187 | b .Lexit |
| 188 | |
| 189 | /* ------------------------------------------------------------- |
| 190 | * Stage 5: Capability Lookup & Verification |
| 191 | * ------------------------------------------------------------- */ |
| 192 | .Lstage5: |
| 193 | ldr w0, [x19, #0x18] /* capability_slot */ |
| 194 | ldr w1, [x19, #0x1C] /* capability_generation */ |
| 195 | ldr x2, [x19, #0x10] /* principal_id */ |
| 196 | bl lookup_capability |
| 197 | cbz x0, .Lfail_cap_lookup |
| 198 | mov x20, x0 /* x20 = validated CapabilityRecord ptr */ |
| 199 | b .Lstage6 |
| 200 | |
| 201 | .Lfail_cap_lookup: |
| 202 | /* w1 carries exact rejection code from lookup_capability */ |
| 203 | mov w21, w1 |
| 204 | b .Lcommit_rejection |
| 205 | |
| 206 | /* ------------------------------------------------------------- |
| 207 | * Stage 6: Resource Type Check |
| 208 | * ------------------------------------------------------------- */ |
| 209 | .Lstage6: |
| 210 | ldr w3, [x19, #0x20] /* intent.resource_type */ |
| 211 | ldr w4, [x20, #0x10] /* cap.resource_type */ |
| 212 | cmp w3, w4 |
| 213 | b.ne .Lfail_resource |
| 214 | b .Lstage7 |
| 215 | |
| 216 | .Lfail_resource: |
| 217 | mov w21, #DEC_REJECTED_RESOURCE |
| 218 | b .Lcommit_rejection |
| 219 | |
| 220 | /* ------------------------------------------------------------- |
| 221 | * Stage 7: Operation Rights Check |
| 222 | * ------------------------------------------------------------- */ |
| 223 | .Lstage7: |
| 224 | ldr w5, [x19, #0x24] /* intent.operation */ |
| 225 | cbz w5, .Lfail_operation |
| 226 | ldr w6, [x20, #0x14] /* cap.allowed_ops */ |
| 227 | mvn w7, w6 |
| 228 | tst w5, w7 |
| 229 | b.ne .Lfail_operation |
| 230 | b .Lstage8 |
| 231 | |
| 232 | .Lfail_operation: |
| 233 | mov w21, #DEC_REJECTED_OPERATION |
| 234 | b .Lcommit_rejection |
| 235 | |
| 236 | /* ------------------------------------------------------------- |
| 237 | * Stage 8: Spatial Bounds & Overflow Check |
| 238 | * ------------------------------------------------------------- */ |
| 239 | .Lstage8: |
| 240 | ldr x8, [x19, #0x28] /* target_base */ |
| 241 | ldr x9, [x19, #0x30] /* target_size */ |
| 242 | ldr x10, [x20, #0x18] /* cap.bound_base */ |
| 243 | ldr x11, [x20, #0x20] /* cap.bound_size */ |
| 244 | |
| 245 | /* target_base >= cap.bound_base */ |
| 246 | cmp x8, x10 |
| 247 | b.lo .Lfail_bounds |
| 248 | |
| 249 | /* target_base + target_size overflow */ |
| 250 | adds x12, x8, x9 |
| 251 | b.cs .Lfail_bounds |
| 252 | |
| 253 | /* cap.bound_base + cap.bound_size */ |
| 254 | adds x13, x10, x11 |
| 255 | b.cs .Lfail_bounds |
| 256 | |
| 257 | /* target_base + target_size <= cap.bound_base + cap.bound_size */ |
| 258 | cmp x12, x13 |
| 259 | b.hi .Lfail_bounds |
| 260 | |
| 261 | /* Concrete resource class rules */ |
| 262 | ldr w3, [x19, #0x20] /* resource_type */ |
| 263 | cmp w3, #RES_MEMORY_FRAME |
| 264 | b.eq .Lcheck_memory_bounds |
| 265 | cmp w3, #RES_CONSOLE |
| 266 | b.eq .Lcheck_console_bounds |
| 267 | b .Lstage9_admitted |
| 268 | |
| 269 | .Lcheck_memory_bounds: |
| 270 | /* Frame target must be 4 KiB aligned and size must be 4 KiB */ |
| 271 | cmp x9, #4096 |
| 272 | b.ne .Lfail_bounds |
| 273 | tst x8, #0xFFF |
| 274 | b.ne .Lfail_bounds |
| 275 | b .Lstage9_admitted |
| 276 | |
| 277 | .Lcheck_console_bounds: |
| 278 | /* Console MMIO target must be strictly UART_MMIO_BASE (0x09000000) */ |
| 279 | ldr x14, =UART_MMIO_BASE |
| 280 | cmp x8, x14 |
| 281 | b.ne .Lfail_bounds |
| 282 | b .Lstage9_admitted |
| 283 | |
| 284 | .Lfail_bounds: |
| 285 | mov w21, #DEC_REJECTED_BOUNDS |
| 286 | b .Lcommit_rejection |
| 287 | |
| 288 | /* ------------------------------------------------------------- |
| 289 | * Stage 9: PHYSICAL EXECUTION (Admitted Operations Only) |
| 290 | * ------------------------------------------------------------- */ |
| 291 | .Lstage9_admitted: |
| 292 | mov w21, #DEC_ADMITTED |
| 293 | |
| 294 | ldr w3, [x19, #0x20] /* resource_type */ |
| 295 | ldr w5, [x19, #0x24] /* operation */ |
| 296 | ldr x8, [x19, #0x28] /* target_base */ |
| 297 | ldr x9, [x19, #0x30] /* target_size */ |
| 298 | |
| 299 | cmp w3, #RES_MEMORY_FRAME |
| 300 | b.eq .Lexec_memory |
| 301 | cmp w3, #RES_CONSOLE |
| 302 | b.eq .Lexec_console |
| 303 | cmp w3, #RES_MEASUREMENT |
| 304 | b.eq .Lexec_measurement |
| 305 | b .Lfail_exec |
| 306 | |
| 307 | .Lexec_memory: |
| 308 | cmp w5, #OP_FRAME_GRANT |
| 309 | b.eq .Lexec_frame_grant |
| 310 | cmp w5, #OP_FRAME_RELEASE |
| 311 | b.eq .Lexec_frame_release |
| 312 | b .Lfail_exec |
| 313 | |
| 314 | .Lexec_frame_grant: |
| 315 | mov x0, x8 /* target_base */ |
| 316 | mov x1, x9 /* target_size */ |
| 317 | bl allocate_frame_in_range |
| 318 | cbz x0, .Lfail_exec |
| 319 | mov x23, x0 /* actual_effect = granted frame address */ |
| 320 | mov x24, #0 /* output = 0 */ |
| 321 | b .Lcommit_admitted |
| 322 | |
| 323 | .Lexec_frame_release: |
| 324 | mov x0, x8 /* target_base */ |
| 325 | bl release_frame |
| 326 | cbz x0, .Lfail_exec |
| 327 | mov x23, x0 /* actual_effect = released frame address */ |
| 328 | mov x24, #0 |
| 329 | b .Lcommit_admitted |
| 330 | |
| 331 | .Lexec_console: |
| 332 | cmp w5, #OP_CONSOLE_WRITE |
| 333 | b.ne .Lfail_exec |
| 334 | /* Write single character from param0 (+0x38) */ |
| 335 | ldr x1, =UART_MMIO_BASE |
| 336 | ldrb w2, [x19, #0x38] /* param0 */ |
| 337 | strb w2, [x1] |
| 338 | mov x23, x1 /* actual_effect = UART base */ |
| 339 | mov x24, #1 /* output = 1 byte written */ |
| 340 | b .Lcommit_admitted |
| 341 | |
| 342 | .Lexec_measurement: |
| 343 | cmp w5, #OP_MEASUREMENT_READ |
| 344 | b.ne .Lfail_exec |
| 345 | /* Return receipt count as measurement */ |
| 346 | bl get_receipt_count |
| 347 | mov x23, #0 |
| 348 | mov x24, x0 /* output = receipt_count */ |
| 349 | b .Lcommit_admitted |
| 350 | |
| 351 | .Lfail_exec: |
| 352 | mov w21, #DEC_EXECUTION_FAILED |
| 353 | b .Lcommit_rejection |
| 354 | |
| 355 | /* ------------------------------------------------------------- |
| 356 | * Stage 10: Commit Receipt & Update Replay Cache |
| 357 | * ------------------------------------------------------------- */ |
| 358 | .Lcommit_admitted: |
| 359 | .Lcommit_rejection: |
| 360 | /* Populate ReceiptData scratchpad at [sp + OFF_RECEIPT] */ |
| 361 | add x0, sp, #OFF_RECEIPT |
| 362 | mov w1, #1 |
| 363 | strh w1, [x0, #0] /* version = 1 */ |
| 364 | mov w1, #RECEIPT_RECORD_SIZE |
| 365 | strh w1, [x0, #2] /* length = 192 */ |
| 366 | str w21, [x0, #4] /* decision */ |
| 367 | str w22, [x0, #8] /* rejection_reason */ |
| 368 | |
| 369 | ldr x1, [x19, #0x08] /* request_id */ |
| 370 | str x1, [x0, #16] |
| 371 | |
| 372 | /* intent_digest already computed at +24 */ |
| 373 | str x23, [x0, #0x38] /* actual_effect */ |
| 374 | str x24, [x0, #0x40] /* output */ |
| 375 | |
| 376 | /* Bind capability slot and generation */ |
| 377 | ldr w2, [x19, #0x18] |
| 378 | str w2, [x0, #0x48] /* capability_slot */ |
| 379 | ldr w3, [x19, #0x1C] |
| 380 | str w3, [x0, #0x4C] /* capability_generation */ |
| 381 | |
| 382 | /* Commit receipt to ledger */ |
| 383 | add x0, sp, #OFF_RECEIPT |
| 384 | bl commit_receipt |
| 385 | cbz x0, .Lexit /* Ledger was exhausted */ |
| 386 | |
| 387 | mov x19, x0 /* x19 = committed receipt ptr */ |
| 388 | |
| 389 | /* Read assigned receipt_slot from committed receipt offset */ |
| 390 | ldr x1, =RECEIPT_ENTRIES_BASE |
| 391 | sub x2, x19, x1 |
| 392 | mov x3, #RECEIPT_RECORD_SIZE |
| 393 | udiv x2, x2, x3 /* x2 = receipt_slot index */ |
| 394 | |
| 395 | /* Record in Replay Cache: record_replay(request_id, intent_digest, slot, decision, effect, output) */ |
| 396 | ldr x0, [x19, #0x10] /* request_id */ |
| 397 | add x1, x19, #0x18 /* intent_digest */ |
| 398 | mov w2, w2 /* receipt_slot */ |
| 399 | mov w3, w21 /* decision */ |
| 400 | mov x4, x23 /* actual_effect */ |
| 401 | mov x5, x24 /* output */ |
| 402 | bl record_replay |
| 403 | |
| 404 | mov x0, x19 /* Return committed receipt pointer */ |
| 405 | |
| 406 | .Lexit: |
| 407 | ldp x23, x24, [sp, #48] |
| 408 | ldp x21, x22, [sp, #32] |
| 409 | ldp x19, x20, [sp, #16] |
| 410 | ldp x29, x30, [sp], #FRAME_SIZE |
| 411 | ret |