interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / svc / store_crash.c

264 lines · 8875 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/* store_crash.c -- see store_crash.h. TEST-ONLY; empty unless
18 * CK_TEST_STORE_CRASH=1 (make full CK_TEST_STORE_CRASH=1). */
19#include "store_crash.h"
20
21#ifdef CK_TEST_STORE_CRASH
22#ifdef CK_HARDWARE_STAGING
23#error "CK_TEST_STORE_CRASH (TEST-ONLY Store crash hook) cannot be combined with CK_HARDWARE_STAGING"
24#endif
25#ifdef CK_QEMU_UNSAFE_DMA
26#error "CK_TEST_STORE_CRASH cannot be combined with CK_QEMU_UNSAFE_DMA"
27#endif
28#include <stddef.h>
29#include "ck.h"
30#include "disk_layout.h"
31#include "store_engine.h"
32#include "store_sealed.h"
33
34#define CR_MAX_WRITES 64u
35#define CR_MAX_BYTES (256u * 1024u)
36#define CR_PLAN_MAGIC "AIENCRSH v1 cp="
37
38typedef struct {
39 uint64_t lba;
40 uint32_t count;
41 uint32_t off;
42} cr_write;
43
44static struct {
45 const disk_dev *real;
46 disk_dev dev;
47 cr_write w[CR_MAX_WRITES];
48 uint32_t n, used;
49 uint8_t *buf;
50 int armed, cp, policy;
51 uint64_t gen_open;
52} cr;
53
54static const char *cp_name(int cp)
55{
56 if (cp == SS_CP_BEFORE_ANCHOR) return "before_anchor";
57 if (cp == SS_CP_AFTER_ANCHOR) return "after_anchor";
58 return st_checkpoint_name(cp);
59}
60static const char *const policy_names[] = {"drop", "all", "newest", "torn"};
61
62static int str_eq_n(const char *a, const char *b, size_t n)
63{
64 for (size_t i = 0; i < n; i++)
65 if (a[i] != b[i]) return 0;
66 return 1;
67}
68static size_t str_len(const char *s)
69{
70 size_t n = 0;
71 while (s[n]) n++;
72 return n;
73}
74static void cp_bytes(uint8_t *d, const uint8_t *s, size_t n)
75{
76 while (n--) *d++ = *s++;
77}
78
79/* Parses "AIENCRSH v1 cp=<name> policy=<name>\n". 0 ok, -1 malformed. */
80static int parse_plan(const char *p, size_t len, int *cp, int *policy)
81{
82 size_t m = sizeof CR_PLAN_MAGIC - 1, i = m, s;
83 static const int cps[] = {ST_CP_BEFORE_FIRST_WRITE, ST_CP_AFTER_PAYLOAD_OBJECTS, ST_CP_AFTER_CATALOG,
84 ST_CP_AFTER_COMMIT_RECORD, ST_CP_AFTER_FIRST_FLUSH, ST_CP_AFTER_INACTIVE_SUPERBLOCK,
85 ST_CP_AFTER_FINAL_FLUSH, SS_CP_BEFORE_ANCHOR, SS_CP_AFTER_ANCHOR};
86 *cp = -1;
87 *policy = -1;
88 for (s = i; i < len && p[i] != ' '; i++)
89 ;
90 if (i >= len) return -1;
91 for (size_t k = 0; k < sizeof cps / sizeof cps[0]; k++) {
92 const char *nm = cp_name(cps[k]);
93 if (str_len(nm) == i - s && str_eq_n(p + s, nm, i - s)) *cp = cps[k];
94 }
95 if (*cp < 0 || len - i < 8 || !str_eq_n(p + i, " policy=", 8)) return -1;
96 for (s = i = i + 8; i < len && p[i] != '\n'; i++)
97 ;
98 if (i >= len) return -1;
99 for (int k = 0; k < 4; k++)
100 if (str_len(policy_names[k]) == i - s && str_eq_n(p + s, policy_names[k], i - s)) *policy = k;
101 if (*policy < 0) return -1;
102 for (i = i + 1; i < len; i++) /* nothing after the line */
103 if (p[i]) return -1;
104 return 0;
105}
106
107static uint32_t pending_blocks(void)
108{
109 uint32_t b = 0;
110 for (uint32_t i = 0; i < cr.n; i++) b += cr.w[i].count;
111 return b;
112}
113
114/* Writes cached write i (its first `count` blocks) to the real disk. */
115static int land(uint32_t i, uint32_t count)
116{
117 if (!count) return 0;
118 return disk_write(cr.real, cr.w[i].lba, count, cr.buf + cr.w[i].off);
119}
120
121static int cr_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *out)
122{
123 (void)ctx;
124 int rc = disk_read(cr.real, lba, count, out);
125 if (rc) return rc;
126 uint32_t bs = cr.real->block_size;
127 for (uint32_t i = 0; i < cr.n; i++) { /* later writes win */
128 const cr_write *w = &cr.w[i];
129 uint64_t a = lba > w->lba ? lba : w->lba;
130 uint64_t e1 = lba + count, e2 = w->lba + w->count, b = e1 < e2 ? e1 : e2;
131 if (a >= b) continue;
132 cp_bytes(out + (a - lba) * bs, cr.buf + w->off + (a - w->lba) * bs, (size_t)(b - a) * bs);
133 }
134 return 0;
135}
136
137static int cr_write_fn(void *ctx, uint64_t lba, uint32_t count, const uint8_t *in)
138{
139 (void)ctx;
140 uint32_t bytes = count * cr.real->block_size;
141 if (cr.n >= CR_MAX_WRITES || cr.used + bytes > CR_MAX_BYTES) {
142 ck_printf("store_crash: volatile cache full (writes=%u bytes=%u); write refused, fail closed\n", cr.n, cr.used);
143 return DISK_EIO;
144 }
145 cr.w[cr.n] = (cr_write){lba, count, cr.used};
146 cp_bytes(cr.buf + cr.used, in, bytes);
147 cr.n++;
148 cr.used += bytes;
149 return 0;
150}
151
152static int cr_flush(void *ctx)
153{
154 (void)ctx;
155 for (uint32_t i = 0; i < cr.n; i++) {
156 int rc = land(i, cr.w[i].count);
157 if (rc) return rc;
158 }
159 cr.n = 0;
160 cr.used = 0;
161 return disk_flush(cr.real);
162}
163
164const disk_dev *ck_store_crash_setup(const disk_dev *d)
165{
166 ck_puts("store_crash: TEST-ONLY Store crash hook image (CK_TEST_STORE_CRASH=1); "
167 "never counts toward a PASS of M4_STORE\n");
168#ifdef CK_TEST_STORE_MUTANT_SKIP_ROOT_FLUSH
169 ck_puts("store_crash: TEST-ONLY MUTANT skip_root_flush: the Store does not flush before the root write\n");
170#endif
171#ifdef CK_TEST_STORE_MUTANT_ACCEPT_BAD_ROOT_CRC
172 ck_puts("store_crash: TEST-ONLY MUTANT accept_bad_root_crc: the Store ignores the superblock CRC\n");
173#endif
174 if (!d || (d->block_size != 512 && d->block_size != 4096)) return d;
175 uint32_t bpu = CK_LAYOUT_UNIT / d->block_size;
176 uint64_t units = d->block_count / bpu;
177 uint8_t *plan = ck_alloc(CK_LAYOUT_UNIT);
178 if (!units || !plan) {
179 ck_puts("store_crash: plan REFUSED (no plan buffer or no unit); Store runs on the disk directly\n");
180 return d;
181 }
182 int rc = disk_read(d, (units - 1u) * bpu, bpu, plan);
183 if (rc) {
184 ck_printf("store_crash: plan REFUSED (read rc=%d); Store runs on the disk directly\n", rc);
185 return d;
186 }
187 size_t m = sizeof CR_PLAN_MAGIC - 1;
188 if (!str_eq_n((const char *)plan, CR_PLAN_MAGIC, m)) {
189 ck_puts("store_crash: no plan; Store runs on the disk directly\n");
190 return d;
191 }
192 if (parse_plan((const char *)plan, CK_LAYOUT_UNIT, &cr.cp, &cr.policy)) {
193 ck_puts("store_crash: plan REFUSED (malformed); Store runs on the disk directly\n");
194 return d;
195 }
196 cr.buf = ck_alloc(CR_MAX_BYTES);
197 if (!cr.buf) {
198 ck_puts("store_crash: plan REFUSED (no cache buffer); Store runs on the disk directly\n");
199 return d;
200 }
201 cr.real = d;
202 cr.dev = *d;
203 cr.dev.ctx = &cr;
204 cr.dev.read = cr_read;
205 cr.dev.write = cr_write_fn;
206 cr.dev.flush = cr_flush;
207 cr.n = cr.used = 0;
208 cr.armed = 1;
209 ck_printf("store_crash: plan halt_at=%s policy=%s; Store runs on a volatile write cache\n", cp_name(cr.cp),
210 policy_names[cr.policy]);
211 return &cr.dev;
212}
213
214void ck_store_crash_opened(uint64_t generation)
215{
216 cr.gen_open = generation;
217}
218
219static __attribute__((noreturn)) void power_cut(int checkpoint)
220{
221 uint32_t pend = pending_blocks(), landed = 0, nw = cr.n;
222 int rc = 0;
223 switch (cr.policy) {
224 case CK_STORE_CRASH_POLICY_ALL:
225 for (uint32_t i = 0; i < cr.n && !rc; i++) {
226 rc = land(i, cr.w[i].count);
227 landed += cr.w[i].count;
228 }
229 break;
230 case CK_STORE_CRASH_POLICY_NEWEST:
231 if (cr.n) {
232 rc = land(cr.n - 1u, cr.w[cr.n - 1u].count);
233 landed = cr.w[cr.n - 1u].count;
234 }
235 break;
236 case CK_STORE_CRASH_POLICY_TORN:
237 if (cr.n) {
238 landed = cr.w[cr.n - 1u].count / 2u;
239 rc = land(cr.n - 1u, landed);
240 }
241 break;
242 default:
243 break;
244 }
245 if (!rc) rc = disk_flush(cr.real);
246 cr.n = 0;
247 cr.used = 0;
248 ck_printf("store_crash: HALT at %s policy=%s generation_open=%llu target=%llu pending_writes=%u "
249 "pending_blocks=%u landed_blocks=%u rc=%d (power cut: the host kills QEMU now)\n",
250 cp_name(checkpoint), policy_names[cr.policy], (unsigned long long)cr.gen_open,
251 (unsigned long long)(cr.gen_open + 1u), nw, pend, landed, rc);
252 for (;;) __asm__ volatile("wfi");
253}
254
255void ck_store_crash_hook(void *arg, int checkpoint)
256{
257 (void)arg;
258 ck_printf("store_crash: checkpoint %s generation_open=%llu pending_writes=%u\n", cp_name(checkpoint),
259 (unsigned long long)cr.gen_open, cr.armed ? cr.n : 0u);
260 if (cr.armed && checkpoint == cr.cp) power_cut(checkpoint);
261}
262#else
263typedef int ck_store_crash_not_built; /* ISO C: no empty translation unit */
264#endif