Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/tests/test_pi5_rng.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / tests / test_pi5_rng.c` 199 lines ยท 6347 bytes Raw Blame History 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 /* test_pi5_rng.c -- Pi 5 RNG200 driver (dev/pi5_rng.c) against a mocked 18 * register block, driving the real core/entropy.c rules: init sequence, 19 * empty FIFO, lockout (one soft reset, then refuse, latched), warm-up that 20 * never finishes, stuck output, block that will not enable. Hosted: this is 21 * NOT the hardware; the block itself is proved on the hub Pi (pi5-run.sh). */ 22 #include 23 #include 24 #include "ck_test.h" 25 #include "pi5_rng.h" 26 27 struct mock { 28 uint32_t ctrl, thresh, fifo_thr, status, bitcount; 29 uint64_t seq; /* next generated word source */ 30 int fifo_n; /* words available */ 31 int same; /* every word is this value when same_on */ 32 int same_on; 33 int fifo_empty; /* count register always 0 */ 34 int ignore_enable; /* control writes do not stick */ 35 int lockout_sticks; /* reset does not clear the lockout */ 36 int resets, writes, fifo_reads; 37 uint32_t last_w_off[16], last_w_val[16]; 38 }; 39 40 static uint32_t m_rd(void *c, uint32_t off) 41 { 42 struct mock *m = c; 43 switch (off) { 44 case CK_RNG200_CTRL: return m->ctrl; 45 case CK_RNG200_BITCOUNT: return m->bitcount; 46 case CK_RNG200_INT_STATUS: return m->status; 47 case CK_RNG200_FIFO_COUNT: return m->fifo_empty ? 0 : (m->fifo_thr << 8) | (uint32_t)(m->fifo_n > 255 ? 255 : m->fifo_n); 48 case CK_RNG200_FIFO_DATA: 49 m->fifo_reads++; 50 if (m->same_on) return (uint32_t)m->same; 51 return (uint32_t)(m->seq++ * 0x9e3779b97f4a7c15ull >> 17) | 1u; 52 default: return 0; 53 } 54 } 55 56 static void m_wr(void *c, uint32_t off, uint32_t v) 57 { 58 struct mock *m = c; 59 if (m->writes < 16) { m->last_w_off[m->writes] = off; m->last_w_val[m->writes] = v; } 60 m->writes++; 61 switch (off) { 62 case CK_RNG200_CTRL: if (!m->ignore_enable) m->ctrl = v; break; 63 case CK_RNG200_BITCOUNT_THRESH: m->thresh = v; break; 64 case CK_RNG200_FIFO_COUNT: m->fifo_thr = v >> 8; break; 65 case CK_RNG200_INT_STATUS: 66 if (v == 0xffffffffu && !m->lockout_sticks) m->status = 0; 67 break; 68 case CK_RNG200_SOFT_RESET: if (v & 1u) m->resets++; break; 69 default: break; 70 } 71 } 72 73 static void fresh(struct mock *m) 74 { 75 memset(m, 0, sizeof *m); 76 m->bitcount = 100; 77 m->fifo_n = 200; 78 m->seq = 1; 79 } 80 81 static void bring_up(struct mock *m, struct ck_rng200 *r) 82 { 83 struct ck_rng200_io io = {m_rd, m_wr, m}; 84 ck_rng200_init(r, &io); 85 } 86 87 static void ops_of(struct ck_rng200 *r, struct ck_rng_ops *o) 88 { 89 o->present = ck_rng200_present; 90 o->read = ck_rng200_read; 91 o->ctx = r; 92 } 93 94 int main(void) 95 { 96 struct mock m; 97 struct ck_rng200 r; 98 struct ck_rng_ops ops; 99 struct ck_rng rng; 100 uint8_t buf[64]; 101 uint64_t w; 102 103 printf(" [init from cold: threshold, FIFO threshold, enable at 1 MHz]\n"); 104 fresh(&m); 105 bring_up(&m, &r); 106 CHECK(m.thresh == 0x40000u); 107 CHECK(m.fifo_thr == 2u); 108 CHECK(m.ctrl == ((3u << 13) | 1u)); 109 CHECK(r.up == 1); 110 CHECK(ck_rng200_present(&r) == 1); 111 112 printf(" [init when firmware already enabled it: no writes]\n"); 113 fresh(&m); 114 m.ctrl = (3u << 13) | 1u; 115 bring_up(&m, &r); 116 CHECK(m.writes == 0); 117 CHECK(r.up == 1); 118 119 printf(" [block that will not enable: absent, probe refuses]\n"); 120 fresh(&m); 121 m.ignore_enable = 1; 122 bring_up(&m, &r); 123 ops_of(&r, &ops); 124 CHECK(r.up == 0); 125 CHECK(ck_rng_probe(&rng, &ops) == CK_RNG_ABSENT); 126 127 printf(" [healthy: probe OK, fill delivers, words come from two FIFO reads]\n"); 128 fresh(&m); 129 bring_up(&m, &r); 130 ops_of(&r, &ops); 131 CHECK(ck_rng_probe(&rng, &ops) == CK_RNG_OK); 132 CHECK(m.fifo_reads == 4); 133 memset(buf, 0, sizeof buf); 134 CHECK(ck_rng_fill(&rng, buf, sizeof buf) == 0); 135 { 136 int nz = 0; 137 for (unsigned i = 0; i < sizeof buf; i++) nz |= buf[i]; 138 CHECK(nz != 0); 139 } 140 CHECK(m.resets == 0); 141 142 printf(" [FIFO stays empty: read fails, probe FAILED, buffer zeroed]\n"); 143 fresh(&m); 144 bring_up(&m, &r); 145 m.fifo_empty = 1; 146 ops_of(&r, &ops); 147 CHECK(ck_rng200_read(&r, &w) == 0); 148 CHECK(ck_rng_probe(&rng, &ops) == CK_RNG_FAILED); 149 memset(buf, 0xaa, sizeof buf); 150 CHECK(ck_rng_fill(&rng, buf, sizeof buf) == CK_RNG_FAILED); 151 CHECK(buf[0] == 0 && buf[63] == 0); 152 153 printf(" [warm-up never completes: no data fabricated]\n"); 154 fresh(&m); 155 bring_up(&m, &r); 156 m.bitcount = 16; 157 CHECK(ck_rng200_read(&r, &w) == 0); 158 CHECK(m.fifo_reads == 0); 159 160 printf(" [lockout that a soft reset clears: exactly one reset, then data]\n"); 161 fresh(&m); 162 bring_up(&m, &r); 163 m.status = CK_RNG200_INT_LOCKOUT; 164 CHECK(ck_rng200_read(&r, &w) == 1); 165 CHECK(m.resets == 1); 166 CHECK(r.refused == 0); 167 168 printf(" [NIST fail that a reset clears]\n"); 169 fresh(&m); 170 bring_up(&m, &r); 171 m.status = CK_RNG200_INT_NIST_FAIL; 172 CHECK(ck_rng200_read(&r, &w) == 1); 173 CHECK(m.resets == 1); 174 175 printf(" [lockout that persists: one reset, refuse, latched, no more resets]\n"); 176 fresh(&m); 177 bring_up(&m, &r); 178 m.status = CK_RNG200_INT_LOCKOUT; 179 m.lockout_sticks = 1; 180 ops_of(&r, &ops); 181 CHECK(ck_rng_probe(&rng, &ops) == CK_RNG_FAILED); 182 CHECK(m.resets == 1); 183 CHECK(r.refused == 1); 184 CHECK(ck_rng200_present(&r) == 0); 185 m.status = 0; /* even if the flag clears, the refusal stays */ 186 CHECK(ck_rng200_read(&r, &w) == 0); 187 CHECK(m.resets == 1); 188 CHECK(m.fifo_reads == 0); 189 190 printf(" [stuck output (same word forever): existing entropy rule refuses]\n"); 191 fresh(&m); 192 bring_up(&m, &r); 193 m.same_on = 1; 194 m.same = 0x12345678; 195 ops_of(&r, &ops); 196 CHECK(ck_rng_probe(&rng, &ops) == CK_RNG_STUCK); 197 198 return ck_t_verdict("test_pi5_rng"); 199 }