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/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 / m3 / memory_alloc.s` 222 lines ยท 5143 bytes Raw Blame History 1 /* 2 * m3/memory_alloc.s - Physical Frame Authority for Milestone 3 3 * Target Platform: QEMU virt AArch64 / Sovereign Machine 4 * 5 * Scope: Physical Frame Authority over [FREE_FRAME_BASE, DRAM_END). 6 * Excludes all reserved Physics pages: [0x40000000, 0x40210000) permanently reserved. 7 * Bitmap Anchor: PHYSICS_STATIC_DATA @ 0x4020D000 (12 KiB) 8 */ 9 10 .global init_frame_authority 11 .global allocate_frame 12 .global allocate_frame_in_range 13 .global release_frame 14 .global is_frame_reserved 15 16 .section .text, "ax" 17 .balign 4 18 19 .equ FREE_FRAME_BASE, 0x40210000 20 .equ FRAME_BITMAP_BASE, 0x4020D000 21 .equ FRAME_SIZE, 4096 22 .equ FRAME_SHIFT, 12 23 .equ CONTRACT_DRAM_END, 0x48000000 24 .equ BITMAP_OFFSET, 32 25 .equ STATIC_DATA_SIZE, 12288 26 27 /* 28 * init_frame_authority(ram_base, ram_size) 29 * x0: ram_base, x1: ram_size 30 * Returns total managed frames in x0, or 0 on error. 31 */ 32 init_frame_authority: 33 stp x29, x30, [sp, #-16]! 34 mov x29, sp 35 36 adds x2, x0, x1 /* x2 = DRAM_END */ 37 b.cs .Lno_frames 38 ldr x4, =CONTRACT_DRAM_END 39 cmp x2, x4 40 b.ne .Lno_frames 41 42 ldr x4, =FREE_FRAME_BASE 43 cmp x2, x4 44 b.ls .Lno_frames 45 46 sub x5, x2, x4 /* x5 = managed bytes */ 47 lsr x5, x5, #FRAME_SHIFT /* x5 = total frames */ 48 49 add x6, x5, #7 50 lsr x6, x6, #3 /* x6 = bitmap bytes */ 51 add x7, x6, #BITMAP_OFFSET 52 ldr x8, =STATIC_DATA_SIZE 53 cmp x7, x8 54 b.hi .Lno_frames 55 56 ldr x3, =FRAME_BITMAP_BASE 57 str x2, [x3, #0] /* +0: DRAM_END */ 58 str x5, [x3, #8] /* +8: total_frames */ 59 str xzr, [x3, #16] /* +16: next_free_cursor = 0 */ 60 str xzr, [x3, #24] /* +24: reserved */ 61 add x7, x3, #BITMAP_OFFSET /* x7 = bitmap start */ 62 63 .Lzero_bitmap_loop: 64 cbz x6, .Lzero_bitmap_done 65 strb wzr, [x7], #1 66 sub x6, x6, #1 67 b .Lzero_bitmap_loop 68 69 .Lzero_bitmap_done: 70 mov x0, x5 71 ldp x29, x30, [sp], #16 72 ret 73 74 .Lno_frames: 75 mov x0, #0 76 ldp x29, x30, [sp], #16 77 ret 78 79 /* 80 * is_frame_reserved(frame_addr) -> x0: 1 if reserved, 0 if free DRAM 81 * x0: frame_addr 82 */ 83 is_frame_reserved: 84 ldr x1, =FREE_FRAME_BASE 85 cmp x0, x1 86 b.lo .Lframe_is_res 87 88 ldr x2, =CONTRACT_DRAM_END 89 cmp x0, x2 90 b.hs .Lframe_is_res 91 92 mov x0, #0 93 ret 94 .Lframe_is_res: 95 mov x0, #1 96 ret 97 98 /* 99 * allocate_frame() -> x0: physical frame address, or 0 if exhausted 100 */ 101 allocate_frame: 102 ldr x1, =FRAME_BITMAP_BASE 103 ldr x2, [x1, #8] /* total_frames */ 104 ldr x3, [x1, #16] /* cursor */ 105 106 cmp x3, x2 107 b.ge .Lalloc_exhausted 108 109 /* Mark bit in bitmap */ 110 lsr x4, x3, #3 /* byte offset */ 111 and x5, x3, #7 /* bit index */ 112 add x6, x1, #BITMAP_OFFSET 113 add x6, x6, x4 114 ldrb w7, [x6] 115 mov w8, #1 116 lsl w8, w8, w5 117 orr w7, w7, w8 118 strb w7, [x6] 119 120 /* Compute frame address: FREE_FRAME_BASE + (cursor << 12) */ 121 ldr x9, =FREE_FRAME_BASE 122 lsl x10, x3, #FRAME_SHIFT 123 add x0, x9, x10 124 125 /* Advance cursor */ 126 add x3, x3, #1 127 str x3, [x1, #16] 128 ret 129 130 .Lalloc_exhausted: 131 mov x0, #0 132 ret 133 134 /* 135 * allocate_frame_in_range(target_base, target_size) 136 * x0: target_base, x1: target_size 137 * Allocates 4 KiB frame at target_base. 138 * Returns target_base on success, or 0 on failure. 139 */ 140 allocate_frame_in_range: 141 /* Check 4 KiB alignment */ 142 tst x0, #0xFFF 143 b.ne .Lalloc_range_fail 144 145 /* Check bounds: target_base >= FREE_FRAME_BASE, target_base < CONTRACT_DRAM_END */ 146 ldr x2, =FREE_FRAME_BASE 147 cmp x0, x2 148 b.lo .Lalloc_range_fail 149 150 ldr x3, =CONTRACT_DRAM_END 151 cmp x0, x3 152 b.hs .Lalloc_range_fail 153 154 /* Compute frame index */ 155 sub x4, x0, x2 156 lsr x4, x4, #FRAME_SHIFT /* x4 = frame index */ 157 158 ldr x5, =FRAME_BITMAP_BASE 159 ldr x6, [x5, #8] /* total_frames */ 160 cmp x4, x6 161 b.ge .Lalloc_range_fail 162 163 /* Check if already allocated */ 164 lsr x7, x4, #3 /* byte offset */ 165 and x8, x4, #7 /* bit index */ 166 add x9, x5, #BITMAP_OFFSET 167 add x9, x9, x7 168 ldrb w10, [x9] 169 mov w11, #1 170 lsl w11, w11, w8 171 tst w10, w11 172 b.ne .Lalloc_range_fail /* Already allocated */ 173 174 /* Mark allocated */ 175 orr w10, w10, w11 176 strb w10, [x9] 177 178 /* Success: return target_base (in x0) */ 179 ret 180 181 .Lalloc_range_fail: 182 mov x0, #0 183 ret 184 185 /* 186 * release_frame(frame_addr) -> x0: frame_addr on success, 0 on failure 187 * x0: frame_addr 188 */ 189 release_frame: 190 ldr x2, =FREE_FRAME_BASE 191 cmp x0, x2 192 b.lo .Lrel_fail 193 ldr x3, =CONTRACT_DRAM_END 194 cmp x0, x3 195 b.hs .Lrel_fail 196 tst x0, #0xFFF 197 b.ne .Lrel_fail 198 199 sub x4, x0, x2 200 lsr x4, x4, #FRAME_SHIFT 201 ldr x5, =FRAME_BITMAP_BASE 202 ldr x6, [x5, #8] 203 cmp x4, x6 204 b.ge .Lrel_fail 205 206 lsr x7, x4, #3 207 and x8, x4, #7 208 add x9, x5, #BITMAP_OFFSET 209 add x9, x9, x7 210 ldrb w10, [x9] 211 mov w11, #1 212 lsl w11, w11, w8 213 tst w10, w11 214 b.eq .Lrel_fail /* Was not allocated */ 215 216 bic w10, w10, w11 217 strb w10, [x9] 218 ret 219 220 .Lrel_fail: 221 mov x0, #0 222 ret