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 / rndr.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 | /* rndr.c -- kernel glue for core/entropy.c: the real FEAT_RNG check |
| 18 | * (ID_AA64ISAR0_EL1.RNDR, bits [63:60]) and the RNDR read (NZCV.Z set means |
| 19 | * no random number was returned). Implements the ck.h entropy services. |
| 20 | * There is no other source: without RNDR every request is refused and the |
| 21 | * consumer fails closed. Not covered here: firmware that traps RNDR to EL3 |
| 22 | * (FEAT_RNG_TRAP) and answers with an UNDEFINED exception would fault the |
| 23 | * read; that shows as a kernel fault, never as weak bytes. |
| 24 | * QEMU is not hardware: GB10 RNDR availability is UNVERIFIED. */ |
| 25 | #include "arch.h" |
| 26 | #include "ck_internal.h" |
| 27 | #include "entropy.h" |
| 28 | #ifdef CK_PI5 |
| 29 | #include "pi5_rng.h" |
| 30 | #endif |
| 31 | |
| 32 | #ifndef CK_PI5 |
| 33 | static int hw_present(void *ctx) |
| 34 | { |
| 35 | (void)ctx; |
| 36 | return ((ck_rd(id_aa64isar0_el1) >> 60) & 0xfu) >= 1u; |
| 37 | } |
| 38 | |
| 39 | static int hw_read(void *ctx, uint64_t *out) |
| 40 | { |
| 41 | (void)ctx; |
| 42 | uint64_t v, nzcv; |
| 43 | /* s3_3_c2_c4_0 = RNDR; read NZCV straight after it. */ |
| 44 | __asm__ volatile("mrs %0, s3_3_c2_c4_0\n\tmrs %1, nzcv" : "=r"(v), "=r"(nzcv)::"cc"); |
| 45 | *out = v; |
| 46 | return (nzcv & (1ull << 30)) == 0; |
| 47 | } |
| 48 | |
| 49 | static const struct ck_rng_ops hw_ops = { hw_present, hw_read, 0 }; |
| 50 | #endif /* !CK_PI5 */ |
| 51 | static struct ck_rng rng = { 0, CK_RNG_UNPROBED, 0, 0, 0 }; |
| 52 | |
| 53 | int ck_entropy_init(void) |
| 54 | { |
| 55 | #ifdef CK_PI5 |
| 56 | /* Pi 5: no FEAT_RNG; the only source is the BCM2712 RNG200 block (dev/pi5_rng.c). */ |
| 57 | return ck_rng_probe(&rng, ck_pi5_rng_ops()); |
| 58 | #else |
| 59 | return ck_rng_probe(&rng, &hw_ops); |
| 60 | #endif |
| 61 | } |
| 62 | |
| 63 | const char *ck_entropy_source(void) |
| 64 | { |
| 65 | #ifdef CK_PI5 |
| 66 | return "rng200"; |
| 67 | #else |
| 68 | return "rndr"; |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | int ck_entropy_fill(void *buf, size_t len) |
| 73 | { |
| 74 | return ck_rng_fill(&rng, buf, len); |
| 75 | } |
| 76 | |
| 77 | int ck_entropy_status(void) |
| 78 | { |
| 79 | return rng.state; |
| 80 | } |
| 81 | |
| 82 | const char *ck_entropy_reason(void) |
| 83 | { |
| 84 | return ck_rng_reason(rng.state); |
| 85 | } |
| 86 | |
| 87 | uint64_t ck_entropy_retries(void) |
| 88 | { |
| 89 | return rng.retries; |
| 90 | } |