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 / arch / arch.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 | /* arch.h -- AArch64 system register access for the C kernel core. GICv3 CPU |
| 18 | * interface registers use their S3_* encodings so any assembler accepts them. */ |
| 19 | #ifndef AIENOS_CK_ARCH_H |
| 20 | #define AIENOS_CK_ARCH_H |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | |
| 24 | #define ck_rd(reg) ({ uint64_t _v; __asm__ volatile("mrs %0, " #reg : "=r"(_v)); _v; }) |
| 25 | #define ck_wr(reg, v) __asm__ volatile("msr " #reg ", %0" ::"r"((uint64_t)(v)) : "memory") |
| 26 | #define ck_isb() __asm__ volatile("isb" ::: "memory") |
| 27 | #define ck_dsb_sy() __asm__ volatile("dsb sy" ::: "memory") |
| 28 | #define ck_dsb_ish() __asm__ volatile("dsb ish" ::: "memory") |
| 29 | #define ck_irq_off() __asm__ volatile("msr daifset, #2" ::: "memory") |
| 30 | #define ck_irq_on() __asm__ volatile("msr daifclr, #2" ::: "memory") |
| 31 | |
| 32 | #define ICC_PMR_EL1 S3_0_C4_C6_0 |
| 33 | #define ICC_IAR1_EL1 S3_0_C12_C12_0 |
| 34 | #define ICC_EOIR1_EL1 S3_0_C12_C12_1 |
| 35 | #define ICC_BPR1_EL1 S3_0_C12_C12_3 |
| 36 | #define ICC_CTLR_EL1 S3_0_C12_C12_4 |
| 37 | #define ICC_SRE_EL1 S3_0_C12_C12_5 |
| 38 | #define ICC_IGRPEN1_EL1 S3_0_C12_C12_7 |
| 39 | #define ck_rd_s(reg) ck_rd(reg) |
| 40 | #define ck_wr_s(reg, v) ck_wr(reg, v) |
| 41 | |
| 42 | static inline unsigned ck_current_el(void) |
| 43 | { |
| 44 | return (unsigned)(ck_rd(CurrentEL) >> 2) & 3; |
| 45 | } |
| 46 | |
| 47 | /* Exception frame pushed by vectors.S (layout shared with the assembly). */ |
| 48 | struct ck_frame { |
| 49 | uint64_t x[31]; |
| 50 | uint64_t elr; |
| 51 | uint64_t spsr; |
| 52 | uint64_t vector; /* 0..15: entry index in the vector table */ |
| 53 | }; |
| 54 | _Static_assert(sizeof(struct ck_frame) == 272, "frame layout"); |
| 55 | |
| 56 | extern char ck_vectors[], ck_vectors_el2[]; |
| 57 | /* Lowest valid address of the active kernel stack; the vector entry switches |
| 58 | * to an emergency stack when an exception arrives with SP below it. */ |
| 59 | extern uint64_t ck_stack_floor; |
| 60 | |
| 61 | /* Recoverable probe: 0 if the 8-byte read at addr completed, 1 if it took a |
| 62 | * synchronous data abort (the handler resumes at the fixup). EL1 only. */ |
| 63 | int ck_probe_read(const volatile void *addr); |
| 64 | struct ck_probe { |
| 65 | uint64_t fixup; /* nonzero while a probe is armed */ |
| 66 | uint64_t insn; /* address of the probing load */ |
| 67 | uint64_t esr, far; |
| 68 | }; |
| 69 | extern struct ck_probe ck_probe_state; |
| 70 | |
| 71 | /* EL2 -> EL1h per ADR 0009, MMU on at EL1 with the given tables, then calls |
| 72 | * entry(arg) on `sp`. Never returns. */ |
| 73 | __attribute__((noreturn)) void ck_enter_el1(uint64_t root, uint64_t mair, uint64_t tcr, |
| 74 | uint64_t sctlr, uint64_t sp, |
| 75 | void (*entry)(void *), void *arg); |
| 76 | /* Same, when firmware already runs at EL1: switch tables in place. */ |
| 77 | __attribute__((noreturn)) void ck_switch_el1(uint64_t root, uint64_t mair, uint64_t tcr, |
| 78 | uint64_t sctlr, uint64_t sp, |
| 79 | void (*entry)(void *), void *arg); |
| 80 | |
| 81 | #endif |