omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / kernel / arm64 / frames.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/kernel/arm64/frames.om: Pure-Omega 121.69+ GiB unified LPDDR5x interval frame allocator |
| 3 | note + pre-carved Normal-NC DMA pool allocator (ported from aienux-macbook@5e0c797:mm/frames.c). |
| 4 | note Ingests ONLY EfiConventionalMemory (type 7) and reserves all non-conventional regions, |
| 5 | note image, UEFI memory map, ACPI tables, Normal-NC DMA pool, and GB10 IORT RMR carveouts. |
| 6 | |
| 7 | const FR_PAGE ulong 4096 |
| 8 | const FR_MAX_RANGES ulong 256 |
| 9 | const FR_EFI_CONVENTIONAL ulong 7 |
| 10 | const FR_PA_40BIT_LIMIT ulong 0x0000010000000000 |
| 11 | |
| 12 | note fr_meta: 0=count, 1=dropped, 2=total_pages_ingested, 3=nc_base, 4=nc_cur, 5=nc_end |
| 13 | global fr_meta ulong 8 |
| 14 | global fr_base ulong 256 |
| 15 | global fr_end ulong 256 |
| 16 | |
| 17 | fn fr_align_up v ulong a ulong to ulong |
| 18 | if eq a 0 |
| 19 | ret v |
| 20 | end |
| 21 | m = sub a 1 |
| 22 | if gt m sub 0xffffffffffffffff v |
| 23 | ret 0xffffffffffffffff |
| 24 | end |
| 25 | ret and add v m xor 0xffffffffffffffff m |
| 26 | end |
| 27 | |
| 28 | fn fr_align_down v ulong a ulong to ulong |
| 29 | if eq a 0 |
| 30 | ret v |
| 31 | end |
| 32 | m = sub a 1 |
| 33 | ret and v xor 0xffffffffffffffff m |
| 34 | end |
| 35 | |
| 36 | fn frames_init to void |
| 37 | for i ulong 8 |
| 38 | put fr_meta i 0 |
| 39 | end |
| 40 | for j ulong 256 |
| 41 | put fr_base j 0 |
| 42 | put fr_end j 0 |
| 43 | end |
| 44 | end |
| 45 | |
| 46 | fn fr_insert_at idx ulong b ulong e ulong to int |
| 47 | n = get fr_meta 0 |
| 48 | if ge n FR_MAX_RANGES |
| 49 | put fr_meta 1 add get fr_meta 1 1 |
| 50 | ret -1 |
| 51 | end |
| 52 | j = n |
| 53 | while gt j idx bound 256 |
| 54 | put fr_base j get fr_base sub j 1 |
| 55 | put fr_end j get fr_end sub j 1 |
| 56 | j = sub j 1 |
| 57 | end |
| 58 | put fr_base idx b |
| 59 | put fr_end idx e |
| 60 | put fr_meta 0 add n 1 |
| 61 | ret 0 |
| 62 | end |
| 63 | |
| 64 | fn fr_remove_at idx ulong to void |
| 65 | n = get fr_meta 0 |
| 66 | j = idx |
| 67 | while lt add j 1 n bound 256 |
| 68 | put fr_base j get fr_base add j 1 |
| 69 | put fr_end j get fr_end add j 1 |
| 70 | j = add j 1 |
| 71 | end |
| 72 | put fr_meta 0 sub n 1 |
| 73 | end |
| 74 | |
| 75 | fn frames_reserve base ulong end_pa ulong to void |
| 76 | b = fr_align_down base FR_PAGE |
| 77 | e = fr_align_up end_pa FR_PAGE |
| 78 | if le e b |
| 79 | ret |
| 80 | end |
| 81 | i = ulong 0 |
| 82 | while lt i get fr_meta 0 bound 512 |
| 83 | rb = get fr_base i |
| 84 | re = get fr_end i |
| 85 | if or le re b ge rb e |
| 86 | i = add i 1 |
| 87 | else |
| 88 | if and ge rb b le re e |
| 89 | fr_remove_at i |
| 90 | else |
| 91 | if and lt rb b gt re e |
| 92 | put fr_end i b |
| 93 | rc = fr_insert_at add i 1 e re |
| 94 | if ne rc 0 |
| 95 | ret |
| 96 | end |
| 97 | i = add i 2 |
| 98 | else |
| 99 | if lt rb b |
| 100 | put fr_end i b |
| 101 | else |
| 102 | put fr_base i e |
| 103 | end |
| 104 | i = add i 1 |
| 105 | end |
| 106 | end |
| 107 | end |
| 108 | end |
| 109 | end |
| 110 | |
| 111 | fn frames_add base ulong end_pa ulong to int |
| 112 | b = fr_align_up base FR_PAGE |
| 113 | e = fr_align_down end_pa FR_PAGE |
| 114 | if le e b |
| 115 | ret 0 |
| 116 | end |
| 117 | frames_reserve b e |
| 118 | n = get fr_meta 0 |
| 119 | i = ulong 0 |
| 120 | while lt i n bound 256 |
| 121 | if ge get fr_base i b |
| 122 | break |
| 123 | end |
| 124 | i = add i 1 |
| 125 | end |
| 126 | if gt i 0 |
| 127 | if eq get fr_end sub i 1 b |
| 128 | put fr_end sub i 1 e |
| 129 | if lt i n |
| 130 | if eq get fr_base i e |
| 131 | put fr_end sub i 1 get fr_end i |
| 132 | fr_remove_at i |
| 133 | end |
| 134 | end |
| 135 | ret 0 |
| 136 | end |
| 137 | end |
| 138 | if lt i n |
| 139 | if eq get fr_base i e |
| 140 | put fr_base i b |
| 141 | ret 0 |
| 142 | end |
| 143 | end |
| 144 | ret fr_insert_at i b e |
| 145 | end |
| 146 | |
| 147 | note Allocate pages * 4 KiB aligned to align (power of 2, >= 4096) with end <= max_pa (0 = no cap). |
| 148 | note Returns physical base address, or 0 on failure (never returns page 0). |
| 149 | fn frames_alloc pages ulong align ulong max_pa ulong to ulong |
| 150 | if or eq pages 0 gt pages 0x000fffffffffffff |
| 151 | ret 0 |
| 152 | end |
| 153 | a = align |
| 154 | if lt a FR_PAGE |
| 155 | a = FR_PAGE |
| 156 | end |
| 157 | bytes = mul pages FR_PAGE |
| 158 | n = get fr_meta 0 |
| 159 | i = ulong 0 |
| 160 | while lt i n bound 256 |
| 161 | rb = get fr_base i |
| 162 | re = get fr_end i |
| 163 | cand = fr_align_up rb a |
| 164 | if eq cand 0 |
| 165 | cand = a |
| 166 | end |
| 167 | if and ne cand 0xffffffffffffffff lt cand re |
| 168 | if le bytes sub re cand |
| 169 | cend = add cand bytes |
| 170 | ok_cap = true |
| 171 | if ne max_pa 0 |
| 172 | if gt cend max_pa |
| 173 | ok_cap = false |
| 174 | end |
| 175 | end |
| 176 | if ok_cap |
| 177 | frames_reserve cand cend |
| 178 | ret cand |
| 179 | end |
| 180 | end |
| 181 | end |
| 182 | i = add i 1 |
| 183 | end |
| 184 | ret 0 |
| 185 | end |
| 186 | |
| 187 | fn frames_free_bytes to ulong |
| 188 | total = ulong 0 |
| 189 | n = get fr_meta 0 |
| 190 | i = ulong 0 |
| 191 | while lt i n bound 256 |
| 192 | total = add total sub get fr_end i get fr_base i |
| 193 | i = add i 1 |
| 194 | end |
| 195 | ret total |
| 196 | end |
| 197 | |
| 198 | fn frames_region_count to ulong |
| 199 | ret get fr_meta 0 |
| 200 | end |
| 201 | |
| 202 | note Pre-carved Normal-NC DMA pool (registered before SCTLR_EL1.M=1). |
| 203 | fn dma_pool_init base ulong length ulong to void |
| 204 | put fr_meta 3 base |
| 205 | put fr_meta 4 base |
| 206 | put fr_meta 5 add base length |
| 207 | end |
| 208 | |
| 209 | fn dma_pool_base to ulong |
| 210 | ret get fr_meta 3 |
| 211 | end |
| 212 | |
| 213 | fn dma_pool_size to ulong |
| 214 | ret sub get fr_meta 5 get fr_meta 3 |
| 215 | end |
| 216 | |
| 217 | note Allocate from the pre-carved Normal-NC DMA pool (aligned to align, >= 4096). Returns PA or 0. |
| 218 | fn dma_alloc_nc pages ulong align ulong to ulong |
| 219 | if eq pages 0 |
| 220 | ret 0 |
| 221 | end |
| 222 | a = align |
| 223 | if lt a FR_PAGE |
| 224 | a = FR_PAGE |
| 225 | end |
| 226 | cur = fr_align_up get fr_meta 4 a |
| 227 | bytes = mul pages FR_PAGE |
| 228 | limit = get fr_meta 5 |
| 229 | if or eq cur 0 gt cur limit |
| 230 | ret 0 |
| 231 | end |
| 232 | if gt bytes sub limit cur |
| 233 | ret 0 |
| 234 | end |
| 235 | put fr_meta 4 add cur bytes |
| 236 | ret cur |
| 237 | end |
| 238 | |
| 239 | note Ingest UEFI memory map from raw physical buffer (map_pa, map_size, desc_size). |
| 240 | note Only type 7 (EfiConventionalMemory) is added; page 0 is always reserved; all non-7 types are reserved. |
| 241 | fn frames_ingest_efi_raw map_pa ulong map_size ulong desc_size ulong to ulong |
| 242 | if or eq map_pa 0 or lt desc_size 40 eq map_size 0 |
| 243 | ret 0 |
| 244 | end |
| 245 | cnt = ulong 0 |
| 246 | off = ulong 0 |
| 247 | while le add off desc_size map_size bound 4096 |
| 248 | d = add map_pa off |
| 249 | mtype = fwld d 4 |
| 250 | pstart = fwld add d 8 8 |
| 251 | npages = fwld add d 24 8 |
| 252 | if and gt npages 0 le npages 0x000fffffffffffff |
| 253 | bytes = mul npages FR_PAGE |
| 254 | if le bytes sub 0xffffffffffffffff pstart |
| 255 | pend = add pstart bytes |
| 256 | if eq mtype FR_EFI_CONVENTIONAL |
| 257 | rc = frames_add pstart pend |
| 258 | if eq rc 0 |
| 259 | cnt = add cnt npages |
| 260 | end |
| 261 | end |
| 262 | end |
| 263 | end |
| 264 | off = add off desc_size |
| 265 | end |
| 266 | note Second pass: explicitly reserve every non-EfiConventionalMemory descriptor + page 0. |
| 267 | frames_reserve 0 FR_PAGE |
| 268 | off = ulong 0 |
| 269 | while le add off desc_size map_size bound 4096 |
| 270 | d = add map_pa off |
| 271 | mtype = fwld d 4 |
| 272 | pstart = fwld add d 8 8 |
| 273 | npages = fwld add d 24 8 |
| 274 | if and gt npages 0 and le npages 0x000fffffffffffff ne mtype FR_EFI_CONVENTIONAL |
| 275 | bytes = mul npages FR_PAGE |
| 276 | if le bytes sub 0xffffffffffffffff pstart |
| 277 | frames_reserve pstart add pstart bytes |
| 278 | end |
| 279 | end |
| 280 | off = add off desc_size |
| 281 | end |
| 282 | put fr_meta 2 cnt |
| 283 | ret cnt |
| 284 | end |