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 / core / fpu.c
| 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 | /* fpu.c -- run ONE floating-point/SIMD-enabled unit (a Rust no_std staticlib, |
| 18 | * crates/aienos-fpu-probe) from the general-regs-only C kernel (aienos#34 |
| 19 | * lane 0). Only compiled into the probe build: Makefile CK_RUST_LIBS=<.a> |
| 20 | * adds -DCK_FPU_PROBE=1; the default image carries none of this. |
| 21 | * |
| 22 | * The kernel runs at EL1h (EL2 -> EL1 drop in arch/vectors.S). FP/SIMD |
| 23 | * access at EL1 is governed by CPACR_EL1.FPEN (0b11 = no trap) and, from |
| 24 | * EL2, by CPTR_EL2 (vectors.S sets it to no-trap before the drop). The |
| 25 | * kernel never saves FP state: no exception handler touches v0-v31 and the |
| 26 | * kernel is built -mgeneral-regs-only. So the unit runs with every exception |
| 27 | * masked (DAIF.DAIF) from the first FP instruction to the last, and FP |
| 28 | * registers are left dirty (nothing else in the kernel reads them; EL0 runs |
| 29 | * with FPEN=01 and traps, never sees them). A future FP-using scheduler must |
| 30 | * save/restore v0-v31/FPSR/FPCR before dropping that restriction. */ |
| 31 | #include <stdint.h> |
| 32 | |
| 33 | #include "arch.h" |
| 34 | #include "ck_internal.h" |
| 35 | |
| 36 | #ifdef CK_FPU_PROBE |
| 37 | extern int aienos_fpu_probe(uint32_t *out); |
| 38 | |
| 39 | #define CPACR_FPEN_SHIFT 20 |
| 40 | |
| 41 | /* Set CPACR_EL1.FPEN = 0b11 and synchronise. Idempotent: vectors.S already |
| 42 | * does this at the EL2 -> EL1 drop; doing it here makes the unit not depend |
| 43 | * on that. */ |
| 44 | void ck_fpu_enable(void) |
| 45 | { |
| 46 | uint64_t v = ck_rd(cpacr_el1); |
| 47 | v |= 3ull << CPACR_FPEN_SHIFT; |
| 48 | ck_wr(cpacr_el1, v); |
| 49 | ck_isb(); |
| 50 | } |
| 51 | |
| 52 | void ck_fpu_run(void) |
| 53 | { |
| 54 | uint32_t out[6] = {0, 0, 0, 0, 0, 0}; |
| 55 | uint64_t daif; |
| 56 | int rc; |
| 57 | |
| 58 | ck_fpu_enable(); |
| 59 | daif = ck_rd(daif); |
| 60 | __asm__ volatile("msr daifset, #0xf" ::: "memory"); |
| 61 | rc = aienos_fpu_probe(out); |
| 62 | ck_wr(daif, daif); |
| 63 | |
| 64 | int ok = rc == 0 && out[0] == 0x42880000u /* 68.0f */ && out[2] == 0x41300000u /* 11.0f */ && |
| 65 | out[3] == 0x41b00000u /* 22.0f */ && out[4] == 0x42040000u /* 33.0f */ && |
| 66 | out[5] == 0x42300000u /* 44.0f */; |
| 67 | ck_printf("fpu: el1_cpacr=0x%llx rc=%d dot_bits=0x%x exp1_bits=0x%x neon_bits=0x%x,0x%x,0x%x,0x%x\n", |
| 68 | (unsigned long long)ck_rd(cpacr_el1), rc, out[0], out[1], out[2], out[3], out[4], out[5]); |
| 69 | ck_printf("AIENOS_CK_FPU: %s\n", ok ? "PASS" : "FAIL"); |
| 70 | } |
| 71 | #else |
| 72 | void ck_fpu_run(void) {} |
| 73 | #endif |