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/svc/cortex_journal.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 / svc / cortex_journal.c` 208 lines ยท 6602 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 /* cortex_journal.c -- CxJournalOps over the sealed Store, and the bounded 18 * Cortex heap. See cortex_journal.h. No I/O except through ss_read / 19 * ss_transact; no libc beyond the four memory routines. */ 20 #include "cortex_journal.h" 21 22 #include 23 24 /* ---- heap ---------------------------------------------------------------- */ 25 static struct { 26 uint8_t *base; 27 size_t size, used, peak; 28 } g_h; 29 30 void ck_cx_heap_init(void *base, size_t size) 31 { 32 g_h.base = base; 33 g_h.size = base ? size : 0; 34 g_h.used = 0; 35 g_h.peak = 0; 36 } 37 38 void ck_cx_heap_stats(size_t *used, size_t *peak, size_t *size) 39 { 40 if (used) *used = g_h.used; 41 if (peak) *peak = g_h.peak; 42 if (size) *size = g_h.size; 43 } 44 45 /* Each block carries its size in the 16 bytes before it (16-byte alignment). */ 46 void *ck_cx_alloc(size_t n) 47 { 48 size_t need = (n + 15u) & ~(size_t)15u; 49 if (!g_h.base || need > g_h.size - g_h.used || g_h.size - g_h.used - need < 16u) return 0; 50 uint8_t *p = g_h.base + g_h.used + 16u; 51 ((size_t *)(p - 16u))[0] = n; 52 g_h.used += need + 16u; 53 if (g_h.used > g_h.peak) g_h.peak = g_h.used; 54 return p; 55 } 56 57 void *ck_cx_realloc(void *p, size_t n) 58 { 59 if (!p) return ck_cx_alloc(n); 60 size_t old = ((size_t *)((uint8_t *)p - 16u))[0]; 61 if (n <= old) return p; 62 void *q = ck_cx_alloc(n); 63 if (!q) return 0; 64 memcpy(q, p, old); 65 return q; 66 } 67 68 void ck_cx_free(void *p) { (void)p; } 69 70 /* ---- journal ------------------------------------------------------------- */ 71 void ck_cj_init(ck_cj *j, ss_store *s) 72 { 73 memset(j, 0, sizeof *j); 74 j->s = s; 75 } 76 77 /* Rebuild the chunk index from the Store's verified claims, ordered by 78 * (store generation, object sequence): the order the chunks were committed. */ 79 static int cj_index(ck_cj *j) 80 { 81 uint32_t k, i; 82 j->nchunks = 0; 83 j->len = 0; 84 for (k = 0; k < j->s->nclaims; k++) { 85 const ss_claim *c = &j->s->ws->claims[k]; 86 if (c->c.obj.object_kind != CK_CJ_KIND) continue; 87 if (j->nchunks >= CK_CJ_MAX_CHUNKS) return CX_ERR_IO; 88 ck_cj_chunk e; 89 memcpy(e.sid, c->sid, 32); 90 e.gen = c->c.store_generation; 91 e.seq = c->c.object_sequence; 92 e.len = 0; 93 for (i = j->nchunks; i > 0; i--) { 94 const ck_cj_chunk *p = &j->chunks[i - 1]; 95 if (p->gen < e.gen || (p->gen == e.gen && p->seq < e.seq)) break; 96 j->chunks[i] = *p; 97 } 98 j->chunks[i] = e; 99 j->nchunks++; 100 } 101 /* Lengths come from the envelopes themselves (one decrypt per chunk). */ 102 for (k = 0; k < j->nchunks; k++) { 103 size_t n = 0; 104 uint16_t kind = 0; 105 if (ss_read(j->s, j->chunks[k].sid, j->buf, sizeof j->buf, &n, &kind) != 0 || kind != CK_CJ_KIND) return CX_ERR_IO; 106 j->chunks[k].len = (uint32_t)n; 107 j->len += n; 108 } 109 return CX_OK; 110 } 111 112 static int cj_open(void *ctx, const char *name, int ro, void **h) 113 { 114 ck_cj *j = ctx; 115 if (!j || !j->s || !name || !h) return CX_ERR_ARG; 116 if (!ro && j->writer) { j->refused++; return CX_ERR_WRITER; } 117 int rc = cj_index(j); 118 if (rc != CX_OK) return rc; 119 if (ro) { j->ro_handles++; *h = &j->ro_handles; } 120 else { j->writer = 1; *h = j; } 121 return CX_OK; 122 } 123 124 static int cj_size(void *ctx, void *h, size_t *len) 125 { 126 (void)h; 127 *len = ((ck_cj *)ctx)->len; 128 return CX_OK; 129 } 130 131 static int cj_read(void *ctx, void *h, size_t off, uint8_t *out, size_t n) 132 { 133 ck_cj *j = ctx; 134 (void)h; 135 if (off + n > j->len) return CX_ERR_IO; 136 size_t pos = 0; 137 uint32_t k; 138 j->reads++; 139 for (k = 0; k < j->nchunks && n; k++) { 140 size_t cl = j->chunks[k].len; 141 if (off >= pos + cl) { pos += cl; continue; } 142 size_t got = 0; 143 uint16_t kind = 0; 144 if (ss_read(j->s, j->chunks[k].sid, j->buf, sizeof j->buf, &got, &kind) != 0 || got != cl) return CX_ERR_IO; 145 size_t start = off - pos, take = cl - start; 146 if (take > n) take = n; 147 memcpy(out, j->buf + start, take); 148 out += take; 149 off += take; 150 n -= take; 151 pos += cl; 152 } 153 return n ? CX_ERR_IO : CX_OK; 154 } 155 156 /* One append = one sealed transaction: all chunks durable or none. sync is 157 * implied (ss_transact returns after the generation is durable). */ 158 static int cj_append(void *ctx, void *h, const uint8_t *b, size_t n, int sync) 159 { 160 ck_cj *j = ctx; 161 (void)sync; 162 if (h != (void *)j) return CX_ERR_IO; /* read-only handle */ 163 if (n == 0) return CX_OK; 164 if (n > CK_CJ_MAX_RECORD) { j->refused++; return CX_ERR_IO; } 165 size_t k = (n + SS_MAX_PLAINTEXT - 1) / SS_MAX_PLAINTEXT; 166 if (j->nchunks + k > CK_CJ_MAX_CHUNKS) { j->refused++; return CX_ERR_IO; } 167 ss_object objs[SS_MAX_OBJECTS]; 168 uint8_t ids[SS_MAX_OBJECTS][32]; 169 size_t i, left = n; 170 for (i = 0; i < k; i++) { 171 size_t cl = left > SS_MAX_PLAINTEXT ? SS_MAX_PLAINTEXT : left; 172 objs[i].kind = CK_CJ_KIND; 173 objs[i].version = 1; 174 objs[i].bytes = b + i * SS_MAX_PLAINTEXT; 175 objs[i].len = cl; 176 left -= cl; 177 } 178 if (ss_transact(j->s, objs, k, 0, 0, ids) != 0) return CX_ERR_IO; 179 uint64_t gen = ss_generation(j->s); 180 for (i = 0; i < k; i++) { 181 ck_cj_chunk *e = &j->chunks[j->nchunks++]; 182 memcpy(e->sid, ids[i], 32); 183 e->gen = gen; 184 e->seq = i; 185 e->len = (uint32_t)objs[i].len; 186 j->len += objs[i].len; 187 } 188 j->appends++; 189 return CX_OK; 190 } 191 192 /* Appends are atomic, so the journal never holds a torn tail: only the 193 * current length is accepted. */ 194 static int cj_truncate(void *ctx, void *h, size_t len) 195 { 196 ck_cj *j = ctx; 197 if (h != (void *)j) return CX_ERR_IO; 198 return len == j->len ? CX_OK : CX_ERR_IO; 199 } 200 201 static void cj_close(void *ctx, void *h) 202 { 203 ck_cj *j = ctx; 204 if (h == (void *)j) j->writer = 0; 205 else if (j->ro_handles) j->ro_handles--; 206 } 207 208 const CxJournalOps ck_cx_journal_store = { cj_open, cj_size, cj_read, cj_append, cj_truncate, cj_close };