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 / core / pagetable.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/kernel/core/pagetable.om: Pure-Omega AArch64 Stage-1 4-KiB Granule 48-bit |
| 3 | note 4-Level Page-Table Builder, W^X/Device-NX Enforcer, Unmapper & Address Translator. |
| 4 | note Converted from upstream aienux crates/aienos-kernel/src/mem/pagetable.rs. |
| 5 | |
| 6 | const PT_MAIR_NORMAL_WB uchar 0 |
| 7 | const PT_MAIR_DEVICE_NGNRE uchar 1 |
| 8 | const PT_MAIR_DEVICE_NGNRNE uchar 2 |
| 9 | const PT_MAIR_NORMAL_NC uchar 3 |
| 10 | const PT_MAIR_VALUE ulong 0x00000000440004ff |
| 11 | |
| 12 | const PT_OK int 0 |
| 13 | const PT_ERR_MISALIGNED int -1 |
| 14 | const PT_ERR_ADDR_OOB int -2 |
| 15 | const PT_ERR_WX int -3 |
| 16 | const PT_ERR_DEVICE_EXEC int -4 |
| 17 | const PT_ERR_OVERLAP int -5 |
| 18 | const PT_ERR_NOT_MAPPED int -6 |
| 19 | const PT_ERR_NO_FRAMES int -7 |
| 20 | |
| 21 | const PT_PAGE_SIZE ulong 4096 |
| 22 | const PT_BLOCK_2M ulong 0x200000 |
| 23 | const PT_BLOCK_1G ulong 0x40000000 |
| 24 | const PT_ADDR_MASK ulong 0x0000fffffffff000 |
| 25 | const PT_PA_MAX ulong 0x0000ffffffffffff |
| 26 | const PT_U64_MAX ulong 0xffffffffffffffff |
| 27 | const PT_VALID ulong 1 |
| 28 | const PT_BLOCK ulong 1 |
| 29 | const PT_TABLE ulong 3 |
| 30 | const PT_ENTRIES ulong 512 |
| 31 | const PT_MAX_FRAMES ulong 64 |
| 32 | |
| 33 | note Static pool of 64 4-KiB page-table frames (64 * 512 = 32768 u64 descriptors) |
| 34 | note pt_meta: 0=pool_cap, 1=frames_used, 2=root_frame, 3=phys_base |
| 35 | global pt_meta ulong 4 |
| 36 | global pt_mem ulong 32768 |
| 37 | |
| 38 | fn pt_is_canonical va ulong to bool |
| 39 | if eq and va shl ulong 1 47 0 |
| 40 | ret eq shr va 48 0 |
| 41 | end |
| 42 | ret eq shr va 48 0xffff |
| 43 | end |
| 44 | |
| 45 | fn pt_level_index va ulong level ulong to ulong |
| 46 | if eq level 0 |
| 47 | ret and shr va 39 511 |
| 48 | end |
| 49 | if eq level 1 |
| 50 | ret and shr va 30 511 |
| 51 | end |
| 52 | if eq level 2 |
| 53 | ret and shr va 21 511 |
| 54 | end |
| 55 | ret and shr va 12 511 |
| 56 | end |
| 57 | |
| 58 | fn pt_leaf_attrs f view PtFlags to ulong |
| 59 | attr = shl ulong get f attr 2 |
| 60 | ap = ulong 2 |
| 61 | if ne get f user 0 |
| 62 | if ne get f writable 0 |
| 63 | ap = ulong 1 |
| 64 | else |
| 65 | ap = ulong 3 |
| 66 | end |
| 67 | else |
| 68 | if ne get f writable 0 |
| 69 | ap = ulong 0 |
| 70 | else |
| 71 | ap = ulong 2 |
| 72 | end |
| 73 | end |
| 74 | bits = or attr shl ap 6 |
| 75 | shv = get f shareable |
| 76 | if eq shv 1 |
| 77 | bits = or bits shl ulong 3 8 |
| 78 | else |
| 79 | if eq shv 2 |
| 80 | bits = or bits shl ulong 2 8 |
| 81 | end |
| 82 | end |
| 83 | note AF (bit 10) always set |
| 84 | bits = or bits shl ulong 1 10 |
| 85 | note nG (bit 11): 0 for global, 1 for non-global |
| 86 | if eq get f global_flag 0 |
| 87 | bits = or bits shl ulong 1 11 |
| 88 | end |
| 89 | note PXN (bit 53): kernel never executes non-executable or user pages |
| 90 | if or eq get f executable 0 ne get f user 0 |
| 91 | bits = or bits shl ulong 1 53 |
| 92 | end |
| 93 | note UXN (bit 54): user never executes non-executable or kernel pages |
| 94 | if or eq get f executable 0 eq get f user 0 |
| 95 | bits = or bits shl ulong 1 54 |
| 96 | end |
| 97 | ret bits |
| 98 | end |
| 99 | |
| 100 | fn pt_decode_flags desc ulong dst edit PtTrans to void |
| 101 | attr = uchar and shr desc 2 7 |
| 102 | ap = and shr desc 6 3 |
| 103 | sh = and shr desc 8 3 |
| 104 | ng = and shr desc 11 1 |
| 105 | pxn = and shr desc 53 1 |
| 106 | uxn = and shr desc 54 1 |
| 107 | is_user = uchar 0 |
| 108 | if or eq ap 1 eq ap 3 |
| 109 | is_user = uchar 1 |
| 110 | end |
| 111 | is_wr = uchar 0 |
| 112 | if or eq ap 0 eq ap 1 |
| 113 | is_wr = uchar 1 |
| 114 | end |
| 115 | is_exec = uchar 0 |
| 116 | if ne is_user 0 |
| 117 | if eq uxn 0 |
| 118 | is_exec = uchar 1 |
| 119 | end |
| 120 | else |
| 121 | if eq pxn 0 |
| 122 | is_exec = uchar 1 |
| 123 | end |
| 124 | end |
| 125 | put dst attr attr |
| 126 | put dst writable is_wr |
| 127 | put dst user is_user |
| 128 | put dst executable is_exec |
| 129 | if eq sh 3 |
| 130 | put dst shareable 1 |
| 131 | else |
| 132 | if eq sh 2 |
| 133 | put dst shareable 2 |
| 134 | else |
| 135 | put dst shareable 0 |
| 136 | end |
| 137 | end |
| 138 | if eq ng 0 |
| 139 | put dst global_flag 1 |
| 140 | else |
| 141 | put dst global_flag 0 |
| 142 | end |
| 143 | end |
| 144 | |
| 145 | fn pt_alloc_frame dst edit ulong 1 to int |
| 146 | used = get pt_meta 1 |
| 147 | cap = get pt_meta 0 |
| 148 | if ge used cap |
| 149 | ret PT_ERR_NO_FRAMES |
| 150 | end |
| 151 | base = mul used PT_ENTRIES |
| 152 | for i ulong 512 |
| 153 | put pt_mem add base i 0 |
| 154 | end |
| 155 | put pt_meta 1 add used 1 |
| 156 | put dst 0 used |
| 157 | ret PT_OK |
| 158 | end |
| 159 | |
| 160 | fn pt_frame_to_pa fidx ulong to ulong |
| 161 | ret add get pt_meta 3 mul fidx PT_PAGE_SIZE |
| 162 | end |
| 163 | |
| 164 | fn pt_pa_to_frame pa ulong to ulong |
| 165 | ret div sub pa get pt_meta 3 PT_PAGE_SIZE |
| 166 | end |
| 167 | |
| 168 | fn pt_init phys_base ulong pool_cap ulong to int |
| 169 | if ne and phys_base 0xfff 0 |
| 170 | ret PT_ERR_MISALIGNED |
| 171 | end |
| 172 | if or eq pool_cap 0 gt pool_cap PT_MAX_FRAMES |
| 173 | ret PT_ERR_NO_FRAMES |
| 174 | end |
| 175 | put pt_meta 0 pool_cap |
| 176 | put pt_meta 1 0 |
| 177 | put pt_meta 2 0 |
| 178 | put pt_meta 3 phys_base |
| 179 | f = new ulong 1 0 |
| 180 | rc = pt_alloc_frame f |
| 181 | if ne rc PT_OK |
| 182 | ret rc |
| 183 | end |
| 184 | put pt_meta 2 get f 0 |
| 185 | ret PT_OK |
| 186 | end |
| 187 | |
| 188 | fn pt_frames_used to ulong |
| 189 | ret get pt_meta 1 |
| 190 | end |
| 191 | |
| 192 | fn pt_validate_vrange va ulong length ulong to int |
| 193 | if or eq length 0 or ne and va 0xfff 0 ne and length 0xfff 0 |
| 194 | ret PT_ERR_MISALIGNED |
| 195 | end |
| 196 | lm1 = sub length 1 |
| 197 | if gt lm1 sub PT_U64_MAX va |
| 198 | ret PT_ERR_ADDR_OOB |
| 199 | end |
| 200 | vend = add va lm1 |
| 201 | if or not pt_is_canonical va not pt_is_canonical vend |
| 202 | ret PT_ERR_ADDR_OOB |
| 203 | end |
| 204 | ret PT_OK |
| 205 | end |
| 206 | |
| 207 | fn pt_validate_range va ulong pa ulong length ulong flags view PtFlags to int |
| 208 | rc = pt_validate_vrange va length |
| 209 | if ne rc PT_OK |
| 210 | ret rc |
| 211 | end |
| 212 | if ne and pa 0xfff 0 |
| 213 | ret PT_ERR_MISALIGNED |
| 214 | end |
| 215 | if and ne get flags writable 0 ne get flags executable 0 |
| 216 | ret PT_ERR_WX |
| 217 | end |
| 218 | if gt get flags attr PT_MAIR_NORMAL_NC |
| 219 | ret PT_ERR_DEVICE_EXEC |
| 220 | end |
| 221 | if and ne get flags executable 0 ne get flags attr PT_MAIR_NORMAL_WB |
| 222 | ret PT_ERR_DEVICE_EXEC |
| 223 | end |
| 224 | lm1 = sub length 1 |
| 225 | if gt lm1 sub PT_U64_MAX pa |
| 226 | ret PT_ERR_ADDR_OOB |
| 227 | end |
| 228 | pend = add pa lm1 |
| 229 | if gt pend PT_PA_MAX |
| 230 | ret PT_ERR_ADDR_OOB |
| 231 | end |
| 232 | ret PT_OK |
| 233 | end |
| 234 | |
| 235 | fn pt_ensure_table cur_frame ulong va ulong lvl ulong next_out edit ulong 1 to int |
| 236 | pos = add mul cur_frame PT_ENTRIES pt_level_index va lvl |
| 237 | ent = get pt_mem pos |
| 238 | if eq and ent PT_VALID 0 |
| 239 | rc = pt_alloc_frame next_out |
| 240 | if ne rc PT_OK |
| 241 | ret rc |
| 242 | end |
| 243 | cpa = pt_frame_to_pa get next_out 0 |
| 244 | put pt_mem pos or and cpa PT_ADDR_MASK PT_TABLE |
| 245 | ret PT_OK |
| 246 | end |
| 247 | if eq and ent 3 PT_TABLE |
| 248 | put next_out 0 pt_pa_to_frame and ent PT_ADDR_MASK |
| 249 | ret PT_OK |
| 250 | end |
| 251 | ret PT_ERR_OVERLAP |
| 252 | end |
| 253 | |
| 254 | fn pt_map_leaf va ulong pa ulong sz ulong flags view PtFlags to int |
| 255 | tlevel = ulong 3 |
| 256 | if eq sz PT_BLOCK_1G |
| 257 | tlevel = ulong 1 |
| 258 | else |
| 259 | if eq sz PT_BLOCK_2M |
| 260 | tlevel = ulong 2 |
| 261 | end |
| 262 | end |
| 263 | cur_frame = get pt_meta 2 |
| 264 | nf = new ulong 1 0 |
| 265 | lvl = ulong 0 |
| 266 | while lt lvl tlevel bound 4 |
| 267 | rc = pt_ensure_table cur_frame va lvl nf |
| 268 | if ne rc PT_OK |
| 269 | ret rc |
| 270 | end |
| 271 | cur_frame = get nf 0 |
| 272 | lvl = add lvl 1 |
| 273 | end |
| 274 | lidx = pt_level_index va tlevel |
| 275 | lpos = add mul cur_frame PT_ENTRIES lidx |
| 276 | if ne and get pt_mem lpos PT_VALID 0 |
| 277 | ret PT_ERR_OVERLAP |
| 278 | end |
| 279 | kind = PT_BLOCK |
| 280 | if eq tlevel 3 |
| 281 | kind = PT_TABLE |
| 282 | end |
| 283 | desc = or or and pa PT_ADDR_MASK pt_leaf_attrs flags kind |
| 284 | put pt_mem lpos desc |
| 285 | ret PT_OK |
| 286 | end |
| 287 | |
| 288 | fn pt_pick_block_size cva ulong cpa ulong rem_len ulong to ulong |
| 289 | if ge rem_len PT_BLOCK_1G |
| 290 | if eq rem cva PT_BLOCK_1G 0 |
| 291 | if eq rem cpa PT_BLOCK_1G 0 |
| 292 | ret PT_BLOCK_1G |
| 293 | end |
| 294 | end |
| 295 | end |
| 296 | if ge rem_len PT_BLOCK_2M |
| 297 | if eq rem cva PT_BLOCK_2M 0 |
| 298 | if eq rem cpa PT_BLOCK_2M 0 |
| 299 | ret PT_BLOCK_2M |
| 300 | end |
| 301 | end |
| 302 | end |
| 303 | ret PT_PAGE_SIZE |
| 304 | end |
| 305 | |
| 306 | fn pt_map va ulong pa ulong length ulong flags view PtFlags to int |
| 307 | rc = pt_validate_range va pa length flags |
| 308 | if ne rc PT_OK |
| 309 | ret rc |
| 310 | end |
| 311 | off = ulong 0 |
| 312 | note Each step advances by at least 4 KiB; bound 131072 covers up to 256 GiB at 2 MiB blocks or 128 TiB at 1 GiB blocks. |
| 313 | while lt off length bound 131072 |
| 314 | cva = add va off |
| 315 | cpa = add pa off |
| 316 | sz = pt_pick_block_size cva cpa sub length off |
| 317 | rc = pt_map_leaf cva cpa sz flags |
| 318 | if ne rc PT_OK |
| 319 | ret rc |
| 320 | end |
| 321 | off = add off sz |
| 322 | end |
| 323 | ret PT_OK |
| 324 | end |
| 325 | |
| 326 | note Walk 4-level page table for va. On hit sets info_out[0]=frame, [1]=idx, [2]=size and returns true. |
| 327 | fn pt_find_leaf va ulong info_out edit ulong 3 to bool |
| 328 | cur_frame = get pt_meta 2 |
| 329 | lvl = ulong 0 |
| 330 | while lt lvl 4 bound 4 |
| 331 | idx = pt_level_index va lvl |
| 332 | pos = add mul cur_frame PT_ENTRIES idx |
| 333 | ent = get pt_mem pos |
| 334 | if eq and ent PT_VALID 0 |
| 335 | ret false |
| 336 | end |
| 337 | is_tab = eq and ent 3 PT_TABLE |
| 338 | if or eq lvl 0 and is_tab lt lvl 3 |
| 339 | cur_frame = pt_pa_to_frame and ent PT_ADDR_MASK |
| 340 | else |
| 341 | put info_out 0 cur_frame |
| 342 | put info_out 1 idx |
| 343 | if eq lvl 1 |
| 344 | put info_out 2 PT_BLOCK_1G |
| 345 | else |
| 346 | if eq lvl 2 |
| 347 | put info_out 2 PT_BLOCK_2M |
| 348 | else |
| 349 | put info_out 2 PT_PAGE_SIZE |
| 350 | end |
| 351 | end |
| 352 | ret true |
| 353 | end |
| 354 | lvl = add lvl 1 |
| 355 | end |
| 356 | ret false |
| 357 | end |
| 358 | |
| 359 | fn pt_unmap va ulong length ulong to int |
| 360 | rc = pt_validate_vrange va length |
| 361 | if ne rc PT_OK |
| 362 | ret rc |
| 363 | end |
| 364 | info = new ulong 3 0 |
| 365 | off = ulong 0 |
| 366 | while lt off length bound 131072 |
| 367 | cva = add va off |
| 368 | if not pt_find_leaf cva info |
| 369 | ret PT_ERR_NOT_MAPPED |
| 370 | end |
| 371 | sz = get info 2 |
| 372 | if or ne rem cva sz 0 lt sub length off sz |
| 373 | ret PT_ERR_MISALIGNED |
| 374 | end |
| 375 | pos = add mul get info 0 PT_ENTRIES get info 1 |
| 376 | put pt_mem pos 0 |
| 377 | off = add off sz |
| 378 | end |
| 379 | ret PT_OK |
| 380 | end |
| 381 | |
| 382 | fn pt_translate va ulong dst edit PtTrans to int |
| 383 | if not pt_is_canonical va |
| 384 | ret PT_ERR_ADDR_OOB |
| 385 | end |
| 386 | info = new ulong 3 0 |
| 387 | if not pt_find_leaf va info |
| 388 | ret PT_ERR_NOT_MAPPED |
| 389 | end |
| 390 | pos = add mul get info 0 PT_ENTRIES get info 1 |
| 391 | desc = get pt_mem pos |
| 392 | sz = get info 2 |
| 393 | put dst pa add and desc PT_ADDR_MASK rem va sz |
| 394 | put dst page_size sz |
| 395 | pt_decode_flags desc dst |
| 396 | ret PT_OK |
| 397 | end |