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 / frames.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 | /* frames.h -- physical frame allocator over a sorted list of free ranges. |
| 18 | * Built from the final UEFI memory map: only EfiConventionalMemory is free; |
| 19 | * everything else (loader image and data, boot/runtime services, ACPI, MMIO, |
| 20 | * reserved) is never handed out. Pure: no globals, host testable. */ |
| 21 | #ifndef AIENOS_CK_FRAMES_H |
| 22 | #define AIENOS_CK_FRAMES_H |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | #define CK_PAGE 4096ull |
| 27 | #define CK_FRAMES_MAX 128 |
| 28 | |
| 29 | struct ck_frange { |
| 30 | uint64_t base, end; /* [base, end), page aligned */ |
| 31 | }; |
| 32 | |
| 33 | struct ck_frames { |
| 34 | struct ck_frange r[CK_FRAMES_MAX]; |
| 35 | unsigned n; |
| 36 | unsigned dropped; /* ranges lost because the table was full */ |
| 37 | }; |
| 38 | |
| 39 | void ck_frames_init(struct ck_frames *f); |
| 40 | /* Adds [base, end) rounded inward to pages; merges neighbours. 0 or -1 (full). */ |
| 41 | int ck_frames_add(struct ck_frames *f, uint64_t base, uint64_t end); |
| 42 | /* Removes [base, end) rounded outward to pages from the free set. */ |
| 43 | void ck_frames_reserve(struct ck_frames *f, uint64_t base, uint64_t end); |
| 44 | /* Adds every EfiConventionalMemory descriptor of a UEFI memory map. |
| 45 | * Returns the number of descriptors added, -1 on bad arguments. */ |
| 46 | int ck_frames_from_efi(struct ck_frames *f, const void *map, uint64_t map_size, uint64_t desc_size); |
| 47 | /* npages contiguous frames aligned to `align` (power of two >= 4096). |
| 48 | * 0 on success with *out set; -1 when no range fits (exhaustion). */ |
| 49 | int ck_frames_alloc(struct ck_frames *f, uint64_t npages, uint64_t align, uint64_t *out); |
| 50 | /* As ck_frames_alloc, but the whole block must end at or below `limit` |
| 51 | * (exclusive). Free frames above the limit are left free. */ |
| 52 | int ck_frames_alloc_below(struct ck_frames *f, uint64_t npages, uint64_t align, uint64_t limit, uint64_t *out); |
| 53 | void ck_frames_free(struct ck_frames *f, uint64_t base, uint64_t npages); |
| 54 | uint64_t ck_frames_free_bytes(const struct ck_frames *f); |
| 55 | |
| 56 | /* UEFI descriptor fields used by the allocator and the mapper. */ |
| 57 | struct ck_efi_desc { |
| 58 | uint32_t type; |
| 59 | uint32_t pad; |
| 60 | uint64_t phys; |
| 61 | uint64_t virt; |
| 62 | uint64_t pages; |
| 63 | uint64_t attr; |
| 64 | }; |
| 65 | #define CK_EFI_CONVENTIONAL 7u |
| 66 | |
| 67 | #endif |