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/memory_alloc.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 / memory_alloc.s` 189 lines ยท 5179 bytes Raw Blame History 1 /* 2 * memory_alloc.s - Physical Frame Authority 3 * Milestone 2: PHYSICS_BOOT (Patched Specification) 4 * 5 * Scope: Physical Frame Authority over [FREE_FRAME_BASE, DRAM_END). 6 * Excludes all reserved Physics pages: [0x40000000, 0x40208000) permanently reserved. 7 * Bitmap Anchor: PHYSICS_STATIC_DATA @ 0x40207000 8 */ 9 10 .global init_frame_authority 11 .global init_frame_allocator 12 .global frame_allocator_init 13 .global allocate_frame 14 .global is_frame_reserved 15 16 .extern print_string 17 18 .section .text, "ax" 19 .balign 4 20 21 .equ FREE_FRAME_BASE, 0x40208000 22 .equ FRAME_BITMAP_BASE, 0x40207000 23 .equ FRAME_SIZE, 4096 24 .equ FRAME_SHIFT, 12 25 .equ CONTRACT_DRAM_END, 0x48000000 26 .equ BITMAP_OFFSET, 32 27 .equ STATIC_DATA_SIZE, 4096 28 29 /* 30 * PHYSICS_STATIC_DATA layout (0x40207000, 4 KiB): 31 * +0x00: DRAM_END (u64) 32 * +0x08: total_frames (u64) 33 * +0x10: next_free_cursor (u64, frame index) 34 * +0x18: reserved (u64, zero) 35 * +0x20: frame bitmap, ceil(total_frames / 8) bytes, bit set = allocated 36 * For CONTRACT-QEMU-VIRT-AARCH64-M2: 32,248 frames, 4,031 bitmap bytes, 37 * bitmap spans [0x40207020, 0x40207FFF), last byte 0x40207FFE < 0x40208000. 38 */ 39 40 /* 41 * init_frame_authority(ram_base, ram_size): 42 * Initializes frame bitmap at 0x40207000 for frames [0x40208000, ram_base + ram_size). 43 * Parameters: 44 * x0: RAM Base (0x40000000) 45 * x1: RAM Size (0x08000000) 46 * Returns: 47 * x0: Total managed frame count, or 0 if the requested range is refused. 48 * 49 * Refuses (returns 0, writes nothing) unless ram_base + ram_size is exactly 50 * CONTRACT_DRAM_END without overflow and the bitmap fits in PHYSICS_STATIC_DATA. 51 * Ingress already enforces the exact machine profile; this is the allocator's 52 * own guard so no caller can widen frame authority. 53 */ 54 init_frame_authority: 55 init_frame_allocator: 56 frame_allocator_init: 57 stp x29, x30, [sp, #-16]! 58 mov x29, sp 59 60 /* Calculate DRAM_END with overflow check */ 61 adds x2, x0, x1 /* x2 = DRAM_END */ 62 b.cs .Lno_frames 63 ldr x4, =CONTRACT_DRAM_END 64 cmp x2, x4 65 b.ne .Lno_frames 66 67 ldr x4, =FREE_FRAME_BASE 68 cmp x2, x4 69 b.ls .Lno_frames 70 71 sub x5, x2, x4 /* x5 = managed bytes */ 72 lsr x5, x5, #FRAME_SHIFT /* x5 = total frame count */ 73 74 /* Bitmap bytes = (total_frames + 7) / 8; must fit after the header */ 75 add x6, x5, #7 76 lsr x6, x6, #3 /* x6 = bitmap bytes */ 77 add x7, x6, #BITMAP_OFFSET 78 cmp x7, #STATIC_DATA_SIZE 79 b.hi .Lno_frames 80 81 ldr x3, =FRAME_BITMAP_BASE 82 str x2, [x3, #0] /* +0: DRAM_END */ 83 str x5, [x3, #8] /* +8: total_frames */ 84 str xzr, [x3, #16] /* +16: next_free_cursor = 0 */ 85 str xzr, [x3, #24] /* +24: reserved */ 86 add x7, x3, #BITMAP_OFFSET /* x7 = bitmap start */ 87 88 .Lzero_loop: 89 cbz x6, .Lzero_done 90 strb wzr, [x7], #1 91 sub x6, x6, #1 92 b .Lzero_loop 93 94 .Lzero_done: 95 /* Emit telemetry: "PHYSICS: FRAME_AUTH_BOUND\n" */ 96 ldr x0, =msg_frame_auth_bound 97 bl print_string 98 99 mov x0, x5 100 ldp x29, x30, [sp], #16 101 ret 102 103 .Lno_frames: 104 mov x0, #0 105 ldp x29, x30, [sp], #16 106 ret 107 108 /* 109 * allocate_frame(): 110 * Allocates a single 4 KiB frame from [FREE_FRAME_BASE, DRAM_END). 111 * Returns: 112 * x0: Physical frame base address, or 0 (FRAME_EXHAUSTED) on exhaustion. 113 * 0 is never a valid frame: every frame is >= FREE_FRAME_BASE. 114 */ 115 allocate_frame: 116 ldr x3, =FRAME_BITMAP_BASE 117 ldr x2, [x3, #0] /* DRAM_END */ 118 ldr x5, [x3, #8] /* total_frames */ 119 cbz x5, .Lalloc_fail 120 121 ldr x8, [x3, #16] /* Frame index cursor */ 122 add x7, x3, #32 /* Bitmap pointer */ 123 124 .Lfind_bit_loop: 125 cmp x8, x5 126 b.hs .Lalloc_fail /* Exhausted */ 127 128 lsr x9, x8, #3 /* Byte index = x8 / 8 */ 129 and x10, x8, #7 /* Bit index = x8 % 8 */ 130 131 ldrb w11, [x7, x9] 132 mov w12, #1 133 lsl w12, w12, w10 /* Mask = 1 << bit_index */ 134 135 tst w11, w12 136 b.eq .Lfound_free_frame 137 138 add x8, x8, #1 139 b .Lfind_bit_loop 140 141 .Lfound_free_frame: 142 /* Compute physical address: FREE_FRAME_BASE + (frame_index << 12) */ 143 ldr x4, =FREE_FRAME_BASE 144 lsl x13, x8, #FRAME_SHIFT 145 add x0, x4, x13 146 147 /* Assert address < DRAM_END before granting anything */ 148 cmp x0, x2 149 b.hs .Lalloc_fail 150 151 /* Mark bit as allocated and advance next_free_cursor */ 152 orr w11, w11, w12 153 strb w11, [x7, x9] 154 add x14, x8, #1 155 str x14, [x3, #16] 156 ret 157 158 .Lalloc_fail: 159 mov x0, #0 160 ret 161 162 /* 163 * is_frame_reserved(addr): 164 * Parameters: 165 * x0: physical address 166 * Returns: 167 * x0: 1 if reserved, 0 if within free-frame authority 168 */ 169 is_frame_reserved: 170 ldr x1, =FREE_FRAME_BASE 171 cmp x0, x1 172 b.lo .Lreserved /* Any address below 0x40208000 is permanently reserved */ 173 174 ldr x3, =FRAME_BITMAP_BASE 175 ldr x2, [x3, #0] /* DRAM_END */ 176 cmp x0, x2 177 b.hs .Lreserved /* At or above DRAM_END is reserved/unmapped */ 178 179 mov x0, #0 180 ret 181 182 .Lreserved: 183 mov x0, #1 184 ret 185 186 .section .rodata 187 .balign 8 188 msg_frame_auth_bound: 189 .asciz "PHYSICS: FRAME_AUTH_BOUND\n"