aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / mm / pt.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* pt.h -- AArch64 stage-1 translation tables, 4 KiB granule, 48-bit VA |
| 18 | * (T0SZ = 16, walk starts at level 0). Identity use only in the kernel, but |
| 19 | * the builder takes any va/pa. Table memory comes from a callback and is |
| 20 | * addressed by its physical address (identity), so the host tests can build |
| 21 | * and walk real tables in host memory. */ |
| 22 | #ifndef AIENOS_CK_PT_H |
| 23 | #define AIENOS_CK_PT_H |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | /* MAIR_EL1 attribute indexes. */ |
| 28 | #define CK_MAIR_IDX_NORMAL 0 /* 0xff Normal, inner/outer WB RA WA */ |
| 29 | #define CK_MAIR_IDX_DEVICE 1 /* 0x04 Device-nGnRE */ |
| 30 | #define CK_MAIR_IDX_NC 2 /* 0x44 Normal, inner/outer Non-cacheable */ |
| 31 | #define CK_MAIR_VALUE 0x4404ffull |
| 32 | |
| 33 | /* Leaf descriptor attribute bits. */ |
| 34 | #define CK_PTE_ATTRIDX(i) ((uint64_t)(i) << 2) |
| 35 | #define CK_PTE_AP_RO (1ull << 7) /* AP[2]: read-only; AP[1] = 0: no EL0 access */ |
| 36 | #define CK_PTE_SH_INNER (3ull << 8) |
| 37 | #define CK_PTE_AF (1ull << 10) |
| 38 | #define CK_PTE_PXN (1ull << 53) |
| 39 | #define CK_PTE_UXN (1ull << 54) |
| 40 | #define CK_PTE_ADDR 0x0000fffffffff000ull |
| 41 | |
| 42 | #define CK_PT_NORMAL_RW (CK_PTE_ATTRIDX(CK_MAIR_IDX_NORMAL) | CK_PTE_SH_INNER | CK_PTE_AF | CK_PTE_PXN | CK_PTE_UXN) |
| 43 | #define CK_PT_NORMAL_RO (CK_PT_NORMAL_RW | CK_PTE_AP_RO) |
| 44 | #define CK_PT_NORMAL_RX (CK_PTE_ATTRIDX(CK_MAIR_IDX_NORMAL) | CK_PTE_SH_INNER | CK_PTE_AF | CK_PTE_AP_RO | CK_PTE_UXN) |
| 45 | #define CK_PT_DEVICE (CK_PTE_ATTRIDX(CK_MAIR_IDX_DEVICE) | CK_PTE_AF | CK_PTE_PXN | CK_PTE_UXN) |
| 46 | #define CK_PT_NORMAL_NC (CK_PTE_ATTRIDX(CK_MAIR_IDX_NC) | CK_PTE_SH_INNER | CK_PTE_AF | CK_PTE_PXN | CK_PTE_UXN) |
| 47 | |
| 48 | #define CK_PT_EINVAL (-1) |
| 49 | #define CK_PT_ENOMEM (-2) |
| 50 | #define CK_PT_ELIVE (-3) /* change would need break-before-make on live tables */ |
| 51 | |
| 52 | struct ck_pt { |
| 53 | uint64_t root; /* level 0 table, physical */ |
| 54 | int (*alloc)(void *ctx, uint64_t *phys); /* one zero-able 4 KiB frame */ |
| 55 | void *ctx; |
| 56 | int live; /* tables are in use: refuse to change valid entries */ |
| 57 | unsigned tables; /* tables allocated, root included */ |
| 58 | }; |
| 59 | |
| 60 | int ck_pt_init(struct ck_pt *pt, int (*alloc)(void *, uint64_t *), void *ctx); |
| 61 | /* Maps [va, va+len) -> [pa, pa+len) with `attrs` (one of CK_PT_*), using |
| 62 | * 1 GiB / 2 MiB blocks where aligned. Not live: overrides earlier mappings, |
| 63 | * splitting blocks as needed. Live: only fills invalid entries (or accepts |
| 64 | * identical ones), else CK_PT_ELIVE. */ |
| 65 | int ck_pt_map(struct ck_pt *pt, uint64_t va, uint64_t pa, uint64_t len, uint64_t attrs); |
| 66 | /* Unmaps [va, va+len); splits blocks (not live only). */ |
| 67 | int ck_pt_unmap(struct ck_pt *pt, uint64_t va, uint64_t len); |
| 68 | /* Walks va. 0 if mapped (pa, leaf attributes, leaf level 1..3), -1 if not. */ |
| 69 | int ck_pt_lookup(const struct ck_pt *pt, uint64_t va, uint64_t *pa, uint64_t *attrs, int *level); |
| 70 | |
| 71 | #endif |