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 / svc / allen_memory.h
| 1 | /* allen_memory.h -- Allen's memory on disk: ck_allen_memory (allen_reply.h) |
| 2 | * kept in two copies at the start of the AIENOS partition, so a power cut in |
| 3 | * the middle of a save can only spoil the copy being written. |
| 4 | * |
| 5 | * unit 0 copy A unit 1 copy B (a unit is 4096 bytes) |
| 6 | * unit 2 + i note i, for i below the note count held in the copies |
| 7 | * |
| 8 | * One copy is one unit: |
| 9 | * 0 "AIENALM1" 8 bytes |
| 10 | * 8 version (2..4) u32 little endian |
| 11 | * 12 payload length u32 |
| 12 | * 16 generation u64, +1 on every save |
| 13 | * 24 zero 8 bytes |
| 14 | * 32 SHA-256 of bytes 0..31 and the payload |
| 15 | * 64 payload: starts u32, lines u32, name[24], fact count u32, note |
| 16 | * count u32, then per fact key[48] val[64] are(u8); v3+: self[24]; |
| 17 | * v4+: sky_mode(u8) sky_region(u8) sky_have_loc(u8) pad(u8) lat_udeg(s32) lon_udeg(s32); |
| 18 | * A save writes generation g+1 over the OLDER copy, flushes the disk's write |
| 19 | * cache, then reads it back and checks it; the newer copy is never touched |
| 20 | * until the older one holds the newer state. Loading reads both and keeps |
| 21 | * the valid copy with the highest generation. A copy is valid only when every |
| 22 | * field checks out: magic, version, exact length, fingerprint, fact count, |
| 23 | * NUL-terminated folded text (a-z, 0-9, single spaces), a name of letters, |
| 24 | * and v4+ sky fields in range (mode 0..2, region 0..11, have_loc 0..1, pad 0, |
| 25 | * latitude within +-90 and longitude within +-180 degrees). |
| 26 | * Freestanding (no libc, no heap): every disk access goes through the |
| 27 | * partition view handed in. Host tested in svc/tests/allen_memory_test.c, |
| 28 | * power cuts included. */ |
| 29 | #ifndef AIENOS_ALLEN_MEMORY_H |
| 30 | #define AIENOS_ALLEN_MEMORY_H |
| 31 | #include <stdint.h> |
| 32 | #include "disk.h" |
| 33 | #include "allen_notes.h" |
| 34 | #include "allen_reply.h" |
| 35 | |
| 36 | #define CK_ALLEN_MEM_UNIT 4096u |
| 37 | #define CK_ALLEN_MEM_UNITS 2u /* the two copies */ |
| 38 | #define CK_ALLEN_NOTES_FIRST 2u /* the unit of note 0 */ |
| 39 | /* Units of the partition this file ever touches. */ |
| 40 | #define CK_ALLEN_DISK_UNITS (CK_ALLEN_NOTES_FIRST + CK_ALLEN_NOTES_MAX) |
| 41 | |
| 42 | enum { |
| 43 | CK_ALLEN_MEM_OK = 0, /* loaded the newest valid copy */ |
| 44 | CK_ALLEN_MEM_EMPTY = 1, /* no valid copy: a first start (or both spoiled) */ |
| 45 | CK_ALLEN_MEM_EIO = -501, /* a read, write or flush failed */ |
| 46 | CK_ALLEN_MEM_EGEOM = -502, /* partition view too small or odd block size */ |
| 47 | CK_ALLEN_MEM_EVERIFY = -503, /* the copy read back differs from the copy written */ |
| 48 | }; |
| 49 | |
| 50 | typedef struct { |
| 51 | const disk_dev *dev; /* the AIENOS partition view */ |
| 52 | uint64_t gen; /* generation of the newest valid copy, 0 = none */ |
| 53 | unsigned newest; /* which copy holds it (0 = A, 1 = B) */ |
| 54 | unsigned valid; /* copies that were valid at load (0..2) */ |
| 55 | } ck_allen_store; |
| 56 | |
| 57 | /* Encode / decode one copy (exposed for the tests). decode: 0 and *mem, |
| 58 | * *gen filled, or -1 when the copy is not valid. */ |
| 59 | void ck_allen_mem_encode(const ck_allen_memory *mem, uint64_t gen, uint8_t unit[CK_ALLEN_MEM_UNIT]); |
| 60 | int ck_allen_mem_decode(const uint8_t unit[CK_ALLEN_MEM_UNIT], ck_allen_memory *mem, uint64_t *gen); |
| 61 | |
| 62 | /* Read both copies. OK (mem = newest), EMPTY (mem zeroed) or an error. */ |
| 63 | int ck_allen_store_load(ck_allen_store *st, const disk_dev *part, ck_allen_memory *mem); |
| 64 | /* Save mem as the next generation (see above). OK or an error; on error |
| 65 | * the previous newest copy is still on disk and st is unchanged. */ |
| 66 | int ck_allen_store_save(ck_allen_store *st, const ck_allen_memory *mem); |
| 67 | |
| 68 | /* A note is one unit, written in place (a deleted note is rewritten with |
| 69 | * the gone flag): "AIENNOT1", version 1, text length u32, index u32, start |
| 70 | * u32, gone u32 (0|1), zero u32, SHA-256 of bytes 0..31 and the text at |
| 71 | * 32, the text (printable ASCII) at 64, the rest zero. A note counts only |
| 72 | * once the copies hold a note count past it: a save writes the note, then |
| 73 | * the copies, so a power cut leaves either no note or a whole one. A note |
| 74 | * that fails any check loads as gone and is counted in *damaged. */ |
| 75 | void ck_allen_note_encode(const ck_allen_note *n, uint32_t index, uint8_t unit[CK_ALLEN_MEM_UNIT]); |
| 76 | int ck_allen_note_decode(const uint8_t unit[CK_ALLEN_MEM_UNIT], uint32_t index, ck_allen_note *n); |
| 77 | int ck_allen_notes_load(const disk_dev *part, uint32_t count, ck_allen_notes *ns, uint32_t *damaged); |
| 78 | /* Writes note index, flushes, reads it back. OK, EIO, EGEOM or EVERIFY. */ |
| 79 | int ck_allen_note_write(const disk_dev *part, const ck_allen_notes *ns, uint32_t index); |
| 80 | |
| 81 | #endif |