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 / store / store_v1.h
| 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 | /* |
| 18 | * store_v1.h -- C twin of the System Store v1 format primitives (ADR 0015), |
| 19 | * byte-identical to crates/aienos-kernel/src/store/v1.rs. |
| 20 | * |
| 21 | * No heap, no clock, no I/O. Every function either fills caller memory or |
| 22 | * returns a FormatError code. Error precedence follows the Rust code line by |
| 23 | * line so a malformed input fails with the same error in both languages. |
| 24 | */ |
| 25 | #ifndef AIENOS_STORE_V1_H |
| 26 | #define AIENOS_STORE_V1_H |
| 27 | |
| 28 | #include <stddef.h> |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | #include "sha256.h" /* native/argus */ |
| 32 | |
| 33 | #define SV1_UNIT 4096u |
| 34 | #define SV1_MAX_CATALOG_ENTRIES 4096u |
| 35 | #define SV1_CATALOG_HEADER 16u |
| 36 | #define SV1_CATALOG_ENTRY 64u |
| 37 | #define SV1_MAX_OBJECT_BYTES UINT64_C(67108864) |
| 38 | #define SV1_MAX_OBJECT_UNITS 16384u |
| 39 | #define SV1_MAX_TRANSACTION_OBJECTS 256u |
| 40 | #define SV1_MAX_TRANSACTION_UNITS UINT64_C(32768) |
| 41 | #define SV1_MAX_REGION_UNITS UINT64_C(4294967296) |
| 42 | #define SV1_KIND_CATALOG 1u |
| 43 | #define SV1_KIND_COMMIT 2u |
| 44 | #define SV1_VERSION 1u |
| 45 | #define SV1_COMMIT_BYTES 200u |
| 46 | #define SV1_SB_CRC_OFFSET 168u |
| 47 | #define SV1_SB_RESERVED_OFFSET 172u |
| 48 | /* A full catalog: 16 + 4096 * 64 bytes = 65 units. */ |
| 49 | #define SV1_MAX_CATALOG_UNITS 65u |
| 50 | |
| 51 | /* FormatError, in the Rust enum order. 0 is success. */ |
| 52 | enum { |
| 53 | SV1_OK = 0, |
| 54 | SV1_E_INVALID_LENGTH = -1, |
| 55 | SV1_E_INVALID_OBJECT = -2, |
| 56 | SV1_E_OBJECT_TOO_LARGE = -3, |
| 57 | SV1_E_LENGTH_OVERFLOW = -4, |
| 58 | SV1_E_MALFORMED_DESCRIPTOR = -5, |
| 59 | SV1_E_OUT_OF_BOUNDS = -6, |
| 60 | SV1_E_OVERLAP = -7, |
| 61 | SV1_E_CATALOG_TOO_LARGE = -8, |
| 62 | SV1_E_CATALOG_ORDER = -9, |
| 63 | SV1_E_DUPLICATE_OBJECT = -10, |
| 64 | SV1_E_BAD_CATALOG_MAGIC = -11, |
| 65 | SV1_E_UNSUPPORTED_VERSION = -12, |
| 66 | SV1_E_MALFORMED_COMMIT = -13, |
| 67 | SV1_E_MALFORMED_SUPERBLOCK = -14, |
| 68 | SV1_E_BAD_SUPERBLOCK_MAGIC = -15, |
| 69 | SV1_E_BAD_SUPERBLOCK_CRC = -16, |
| 70 | SV1_E_NONZERO_RESERVED = -17, |
| 71 | SV1_E_WRONG_SUPERBLOCK_SLOT = -18, |
| 72 | SV1_E_UNSUPPORTED_FEATURES = -19, |
| 73 | SV1_E_INTEGRITY = -20, |
| 74 | SV1_E_INVALID_GENERATION = -21, |
| 75 | SV1_E_CONFLICTING_ROOTS = -22, |
| 76 | SV1_E_INCONSISTENT_HISTORY = -23, |
| 77 | /* C only: a caller buffer is too small or a pointer is NULL. Rust has |
| 78 | * no equivalent because it allocates. */ |
| 79 | SV1_E_ARG = -24, |
| 80 | }; |
| 81 | |
| 82 | const char *sv1_strerror(int e); |
| 83 | |
| 84 | typedef struct { |
| 85 | uint8_t object_id[32]; |
| 86 | uint16_t kind; |
| 87 | uint16_t version; |
| 88 | uint64_t first_unit; |
| 89 | uint64_t byte_length; |
| 90 | uint32_t unit_count; |
| 91 | uint16_t flags; |
| 92 | } sv1_entry; |
| 93 | |
| 94 | typedef struct { |
| 95 | uint8_t store_uuid[16]; |
| 96 | uint64_t region_units; |
| 97 | uint64_t generation; |
| 98 | uint64_t previous_generation; |
| 99 | uint8_t previous_commit_id[32]; |
| 100 | uint8_t previous_catalog_id[32]; |
| 101 | uint8_t catalog_id[32]; |
| 102 | uint64_t catalog_first_unit; |
| 103 | uint64_t catalog_byte_length; |
| 104 | uint32_t catalog_unit_count; |
| 105 | uint32_t catalog_entry_count; |
| 106 | uint64_t committed_high_water; |
| 107 | } sv1_commit; |
| 108 | |
| 109 | typedef struct { |
| 110 | uint8_t store_uuid[16]; |
| 111 | uint32_t slot_id; |
| 112 | uint64_t region_units; |
| 113 | uint64_t generation; |
| 114 | uint8_t commit_record_id[32]; |
| 115 | uint64_t commit_record_unit; |
| 116 | uint8_t catalog_id[32]; |
| 117 | uint64_t catalog_first_unit; |
| 118 | uint64_t catalog_byte_length; |
| 119 | uint32_t catalog_unit_count; |
| 120 | uint32_t catalog_entry_count; |
| 121 | uint64_t committed_high_water; |
| 122 | } sv1_superblock; |
| 123 | |
| 124 | /* (first_unit, end) pair used by the overlap check. */ |
| 125 | typedef struct { |
| 126 | uint64_t first; |
| 127 | uint64_t end; |
| 128 | } sv1_extent; |
| 129 | |
| 130 | /* ---- byte helpers (freestanding) ---- */ |
| 131 | void sv1_put16(uint8_t *b, uint16_t v); |
| 132 | void sv1_put32(uint8_t *b, uint32_t v); |
| 133 | void sv1_put64(uint8_t *b, uint64_t v); |
| 134 | uint16_t sv1_get16(const uint8_t *b); |
| 135 | uint32_t sv1_get32(const uint8_t *b); |
| 136 | uint64_t sv1_get64(const uint8_t *b); |
| 137 | void sv1_copy(void *dst, const void *src, size_t n); |
| 138 | void sv1_zero(void *dst, size_t n); |
| 139 | int sv1_equal(const void *a, const void *b, size_t n); /* 1 if equal */ |
| 140 | int sv1_all_zero(const void *a, size_t n); /* 1 if all zero */ |
| 141 | int sv1_cmp_id(const uint8_t a[32], const uint8_t b[32]); /* memcmp order */ |
| 142 | |
| 143 | /* CRC-32C (Castagnoli), reflected 0x82f63b78, init/xorout 0xffffffff. */ |
| 144 | uint32_t sv1_crc32c(const uint8_t *bytes, size_t len); |
| 145 | |
| 146 | /* ---- ObjectId ---- */ |
| 147 | /* ObjectId::calculate. */ |
| 148 | int sv1_object_id(uint16_t kind, uint16_t version, const uint8_t *bytes, uint64_t len, |
| 149 | uint8_t out[32]); |
| 150 | /* Streaming form: checks kind/version/len like calculate and hashes the |
| 151 | * domain and descriptor into ctx (an argus sha256_ctx). The caller then |
| 152 | * feeds exactly len semantic bytes and finalizes. */ |
| 153 | |
| 154 | int sv1_object_id_begin(sha256_ctx *ctx, uint16_t kind, uint16_t version, uint64_t len); |
| 155 | /* ObjectId::validate_extent. */ |
| 156 | int sv1_validate_extent(const uint8_t id[32], uint16_t kind, uint16_t version, |
| 157 | uint64_t byte_length, const uint8_t *extent, size_t extent_len); |
| 158 | /* object_unit_count. */ |
| 159 | int sv1_unit_count(uint64_t byte_length, uint32_t *units); |
| 160 | |
| 161 | /* ---- CatalogEntry ---- */ |
| 162 | int sv1_entry_encode(const sv1_entry *e, uint8_t out[64]); |
| 163 | int sv1_entry_decode(const uint8_t *bytes, size_t len, sv1_entry *out); |
| 164 | int sv1_entry_validate_bounds(const sv1_entry *e, uint64_t region_units, |
| 165 | uint64_t catalog_first_unit, uint64_t committed_high_water); |
| 166 | int sv1_entry_equal(const sv1_entry *a, const sv1_entry *b); |
| 167 | |
| 168 | /* ---- Catalog ---- */ |
| 169 | /* Semantic length for n entries (16 + 64n). */ |
| 170 | uint64_t sv1_catalog_len(uint64_t n); |
| 171 | int sv1_catalog_encode(const sv1_entry *entries, size_t n, uint8_t *out, size_t cap, |
| 172 | size_t *out_len); |
| 173 | int sv1_catalog_decode(const uint8_t *bytes, size_t len, sv1_entry *entries, size_t cap, |
| 174 | uint32_t *n_out); |
| 175 | /* Catalog::validate_extents; scratch must hold n extents. */ |
| 176 | int sv1_catalog_validate_extents(const sv1_entry *entries, size_t n, uint64_t region_units, |
| 177 | uint64_t catalog_first_unit, uint64_t committed_high_water, |
| 178 | sv1_extent *scratch); |
| 179 | /* Index of id in a sorted catalog, or -1. */ |
| 180 | long sv1_catalog_find(const sv1_entry *entries, size_t n, const uint8_t id[32]); |
| 181 | /* In-place sort by ObjectId (heapsort; ids are expected unique). */ |
| 182 | void sv1_sort_entries(sv1_entry *entries, size_t n); |
| 183 | /* In-place sort by (first, end). */ |
| 184 | void sv1_sort_extents(sv1_extent *x, size_t n); |
| 185 | |
| 186 | /* ---- CommitRecord ---- */ |
| 187 | int sv1_commit_validate(const sv1_commit *c); |
| 188 | int sv1_commit_encode(const sv1_commit *c, uint8_t out[SV1_COMMIT_BYTES]); |
| 189 | int sv1_commit_decode(const uint8_t *bytes, size_t len, sv1_commit *out); |
| 190 | int sv1_commit_object_id(const sv1_commit *c, uint8_t out[32]); |
| 191 | /* CommitRecord::validates_catalog. The Rust re-encoding of entries is |
| 192 | * hashed as a stream, so no catalog-sized buffer is needed. */ |
| 193 | int sv1_commit_validates_catalog(const sv1_commit *c, const uint8_t catalog_id[32], |
| 194 | const sv1_entry *entries, size_t n); |
| 195 | int sv1_commit_identifies_predecessor(const sv1_commit *self, const sv1_commit *prev, |
| 196 | const uint8_t prev_id[32]); |
| 197 | |
| 198 | /* ---- Superblock ---- */ |
| 199 | int sv1_superblock_encode(const sv1_superblock *s, uint8_t out[SV1_UNIT]); |
| 200 | int sv1_superblock_decode(const uint8_t *bytes, size_t len, uint32_t expected_slot, |
| 201 | sv1_superblock *out); |
| 202 | int sv1_superblock_validate_commit(const sv1_superblock *s, const uint8_t commit_id[32], |
| 203 | const sv1_commit *c); |
| 204 | int sv1_superblock_equivalent(const sv1_superblock *a, const sv1_superblock *b); |
| 205 | int sv1_superblock_crc_ok(const uint8_t b[SV1_UNIT]); |
| 206 | |
| 207 | /* ---- genesis (genesis.rs) ---- */ |
| 208 | /* Units 0..3: superblock A (slot 0), all-zero slot B, empty catalog at 2, |
| 209 | * genesis CommitRecord at 3. Fails (format error) for an invalid region. */ |
| 210 | int sv1_genesis_units(const uint8_t uuid[16], uint64_t region_units, uint8_t out[4][SV1_UNIT]); |
| 211 | |
| 212 | #endif /* AIENOS_STORE_V1_H */ |