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/x86/local_tools.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 / x86 / local_tools.c` 341 lines ยท 14651 bytes Raw Blame History 1 /* Bounded BSP-only Omega runner. Intel SDM Vol 3A: paging 5.6, TSS/IRET 7.14; 2 * Vol 1 10.5: FXSAVE. Local ABI supports x87/SSE2, never AVX or Linux syscalls. 3 * The kernel identity map remains supervisor-only. A private CR3 exposes only 4 * these fixed user pages; neither model output nor requests supply addresses. */ 5 #include "local_tools.h" 6 #include "local_wire.h" 7 #include "../core/ck_internal.h" 8 #include "../artifact/ck_artifact.h" 9 #include "sha256.h" 10 #include "local_bundle.h" 11 12 #define PAGE 4096u 13 #define BASE (1ull << 46) 14 #define CODE (BASE + PAGE) 15 #define STUB (BASE + 18 * PAGE) 16 #define META (BASE + 20 * PAGE) 17 #define INPUT (BASE + 22 * PAGE) 18 #define WORK (BASE + 24 * PAGE) 19 #define STACK (BASE + 57 * PAGE) 20 #define TOP (STACK + 16 * PAGE) 21 #define NX (1ull << 63) 22 #define USER 5ull 23 #define WRITE 2ull 24 #define ADDR 0x000ffffffffff000ull 25 #define ALIGNED __attribute__((aligned(PAGE))) 26 27 static uint64_t root[512] ALIGNED, pdpt[512] ALIGNED, pd[512] ALIGNED, pt[512] ALIGNED; 28 static uint8_t code[16 * PAGE] ALIGNED, stub[PAGE] ALIGNED, meta[PAGE] ALIGNED; 29 static uint8_t input[PAGE] ALIGNED, workspace[32 * PAGE] ALIGNED, stack[16 * PAGE] ALIGNED; 30 static uint8_t irqstack[16 * PAGE] ALIGNED; 31 uint8_t ck_local_user_fp[512] __attribute__((aligned(16))); 32 uint8_t ck_local_kernel_fp[512] __attribute__((aligned(16))); 33 uint64_t ck_local_kernel_sp; 34 extern void ck_local_enter(struct ck_x86_frame *); 35 static struct ck_x86_frame frame; 36 static uint64_t kernel_root, hz, active_request, started, slice_start, cpu_used; 37 static uint64_t cpu_limit, elapsed_limit; 38 static uint64_t snapshot[5]; 39 static unsigned used; 40 static struct { 41 struct ck_local_result result; 42 unsigned tool; 43 size_t input_len; 44 uint8_t input_hash[32]; 45 } calls[CK_LOCAL_CALLS]; 46 static struct ck_local_result refusal; 47 static struct ck_local_result *running; 48 static struct { const uint8_t *code; size_t len; uint8_t hash[32]; int ok; } approved[3]; 49 50 #ifdef CK_LOCAL_MODEL 51 #include "model/model_bundle.h" 52 #define MODEL_WORK (BASE + 2 * 1024 * 1024) 53 #define MODEL_CODE (BASE + 7 * 1024 * 1024) 54 #define MODEL_WEIGHTS (BASE + 8 * 1024 * 1024) 55 static uint64_t model_pt[34][512] ALIGNED; 56 static uint8_t model_code[32 * PAGE] ALIGNED, model_work[1024 * PAGE] ALIGNED; 57 extern const uint8_t ck_model_weights[], ck_model_weights_end[]; 58 extern const uint8_t ck_model_tokenizer[], ck_model_tokenizer_end[]; 59 static const uint8_t *model_bytes; 60 static size_t model_len; 61 static int model_ok, model_running, model_demo_state; 62 static uint64_t model_cpu_ms, model_elapsed_ms; 63 static uint8_t model_code_hash[32]; 64 static struct ck_local_result model_result; 65 static void model_init(void); 66 #endif 67 68 static void wipe(void *p, size_t n) 69 { 70 volatile uint8_t *b = p; 71 while (n--) *b++ = 0; 72 } 73 static int equal(const uint8_t *a, const uint8_t *b, size_t n) 74 { 75 unsigned d = 0; 76 for (size_t i = 0; i < n; i++) d |= a[i] ^ b[i]; 77 return d == 0; 78 } 79 static void clear_task(void) 80 { 81 /* CR3 was restored before this is called. No global user translations. */ 82 wipe(root, sizeof root); wipe(pdpt, sizeof pdpt); wipe(pd, sizeof pd); wipe(pt, sizeof pt); 83 wipe(code, sizeof code); wipe(stub, sizeof stub); wipe(meta, sizeof meta); 84 wipe(input, sizeof input); wipe(workspace, sizeof workspace); wipe(stack, sizeof stack); 85 wipe(&frame, sizeof frame); wipe(ck_local_user_fp, sizeof ck_local_user_fp); 86 wipe(irqstack, sizeof irqstack); 87 ck_local_machine_stack(0); 88 #ifdef CK_LOCAL_MODEL 89 if (model_running) { 90 model_cpu_ms = cpu_used / (hz / 1000); 91 model_elapsed_ms = (ck_rdtsc() - started) / (hz / 1000); 92 wipe(model_pt, sizeof model_pt); wipe(model_code, sizeof model_code); 93 wipe(model_work, sizeof model_work); model_running = 0; 94 } 95 #endif 96 running = 0; 97 } 98 static void finish(unsigned state, unsigned reason) 99 { 100 running->state = state; 101 running->reason = reason; 102 if (state != CK_LOCAL_RETURNED) { 103 running->length = 0; 104 wipe(running->output, sizeof running->output); 105 } 106 } 107 void ck_local_cancel(void) 108 { 109 if (running) { 110 finish(CK_LOCAL_ABNORMAL, CK_LOCAL_CANCELLED); 111 clear_task(); 112 } 113 } 114 void ck_local_begin(uint64_t request) 115 { 116 ck_local_cancel(); 117 #ifdef CK_LOCAL_MODEL 118 /* A new question also suppresses a demo that returned just before polling 119 * delivered the question. Never paint its sample over the new exchange. */ 120 if (request && model_demo_state == 1) model_demo_state = 2; 121 #endif 122 wipe(calls, sizeof calls); 123 wipe(&refusal, sizeof refusal); 124 active_request = request; 125 used = 0; 126 } 127 void ck_local_snapshot(uint64_t cores, uint64_t ram, uint64_t w, uint64_t h, uint64_t network) 128 { 129 snapshot[0] = cores; snapshot[1] = ram; snapshot[2] = w; 130 snapshot[3] = h; snapshot[4] = network; 131 } 132 133 /* Signature + immutable build allowlist + closed ABI + tighter local quotas. 134 * Existing ARM parser entry points still require the ARM profile. */ 135 static int admit(const struct ck_local_artifact *a, const uint8_t **bytes, size_t *len) 136 { 137 struct cka_parsed p; 138 struct cka_ident id; 139 uint8_t fp[32], digest[32]; 140 if (cka_verify_profile(a->bytes, a->length, ck_local_keys, 1, &p, &id, fp, 141 CKA_TARGET_X86_64_LE, CKA_ABI_OMEGA_X86_LOCAL_V1)) 142 return -1; 143 sha256_hash(a->bytes, a->length, digest); 144 if (!equal(digest, a->hash, 32) || p.entry || p.ncaps || p.code.flen > sizeof code || 145 p.env.code_pages > 16 || p.env.data_pages != 32 || p.env.stack_pages != 16 || 146 p.env.max_caps || p.env.ipc_msgs || p.env.ipc_bytes || p.env.cpu != 100000 || 147 p.env.elapsed != 500000 || p.env.syscalls != 1 || p.data.flen != 1 || 148 a->bytes[p.payload_off + p.data.rel]) 149 return -1; 150 *bytes = a->bytes + p.payload_off; 151 *len = p.code.flen; 152 return 0; 153 } 154 void ck_local_init(uint64_t kr, uint64_t frequency) 155 { 156 kernel_root = kr; hz = frequency; 157 #ifdef CK_LOCAL_MODEL 158 model_init(); 159 #endif 160 for (unsigned i = 0; i < 3; i++) { 161 approved[i].ok = !admit(&ck_local_artifacts[i], &approved[i].code, &approved[i].len); 162 if (approved[i].ok) 163 sha256_hash(approved[i].code, approved[i].len, approved[i].hash); 164 } 165 ck_printf("local_tools: approved=%u/3 profile=omega-x86-local-v1 slice_us=1000 cpu_us=100000 elapsed_us=500000\n", 166 (unsigned)(approved[0].ok + approved[1].ok + approved[2].ok)); 167 } 168 static void map_pages(unsigned first, uint8_t *backing, unsigned n, uint64_t flags) 169 { 170 for (unsigned i = 0; i < n; i++) 171 pt[first + i] = (uint64_t)(uintptr_t)(backing + i * PAGE) | USER | flags; 172 } 173 static int prepare(const uint8_t *bytes, size_t n, const void *in, size_t length) 174 { 175 if (!kernel_root || !hz || !n || n > sizeof code || length > sizeof input) 176 return -1; 177 memcpy(root, (void *)(uintptr_t)kernel_root, sizeof root); 178 /* A future kernel mapping must not silently be replaced. All inherited 179 * entries are made supervisor-only, even if a future kernel adds U/S. */ 180 if (root[128]) return -1; 181 for (unsigned i = 0; i < 512; i++) root[i] &= ~4ull; 182 root[128] = (uint64_t)(uintptr_t)pdpt | USER | WRITE; 183 pdpt[0] = (uint64_t)(uintptr_t)pd | USER | WRITE; 184 pd[0] = (uint64_t)(uintptr_t)pt | USER | WRITE; 185 memcpy(code, bytes, n); 186 if (length) memcpy(input, in, length); 187 /* Two trusted return veneers: MOV EDX,reason; INT 80h; UD2. */ 188 static const uint8_t veneers[] = {0xba,1,0,0,0,0xcd,0x80,0x0f,0x0b, 189 0xba,2,0,0,0,0xcd,0x80,0x0f,0x0b}; 190 memcpy(stub, veneers, sizeof veneers); 191 uint64_t *vt = (void *)meta; 192 for (unsigned i = 0; i < 32; i++) vt[i] = STUB + 16; /* UD2 for unsupported runtime entries */ 193 vt[2] = STUB + 9; 194 map_pages(1, code, (unsigned)((n + PAGE - 1) / PAGE), 0); 195 map_pages(18, stub, 1, 0); map_pages(20, meta, 1, NX); 196 map_pages(22, input, 1, NX); map_pages(24, workspace, 32, NX | WRITE); 197 map_pages(57, stack, 16, NX | WRITE); 198 /* Slots for three fixed-array/scalar parameters; disjoint from callee's 199 * initial 272-byte frame and return address. No kernel addresses escape. */ 200 uint64_t *end = (void *)(stack + sizeof stack); 201 end[-1] = INPUT; end[-2] = WORK; end[-3] = length; 202 end[-33] = STUB; 203 frame.rip = CODE; frame.cs = 0x2b; frame.ss = 0x33; frame.rflags = 0x202; 204 frame.rsp = TOP - 264; frame.rbp = TOP; frame.r12 = TOP - 264; 205 frame.r14 = META; frame.r15 = STACK; 206 ck_local_user_fp[0] = 0x7f; ck_local_user_fp[1] = 3; /* FCW=037f, empty tags */ 207 ck_local_user_fp[24] = 0x80; ck_local_user_fp[25] = 0x1f; /* MXCSR=1f80 */ 208 started = ck_rdtsc(); cpu_used = 0; 209 cpu_limit = hz / 10; elapsed_limit = hz / 2; 210 ck_local_machine_stack((uint64_t)(uintptr_t)(irqstack + sizeof irqstack)); 211 return 0; 212 } 213 #ifdef CK_LOCAL_MODEL 214 #include "model/runtime.inc" 215 #else 216 const char *ck_local_model_demo_step(void) { return 0; } 217 #endif 218 219 static const struct ck_local_result *refuse(uint64_t request, uint64_t invocation, unsigned why) 220 { 221 wipe(&refusal, sizeof refusal); 222 refusal.request = request; refusal.invocation = invocation; 223 refusal.state = CK_LOCAL_REFUSED; refusal.reason = why; 224 return &refusal; 225 } 226 const struct ck_local_result *ck_local_status(uint64_t request, uint64_t invocation) 227 { 228 if (!request || request != active_request || !invocation) return 0; 229 for (unsigned i = 0; i < used; i++) 230 if (calls[i].result.invocation == invocation) return &calls[i].result; 231 return 0; 232 } 233 const struct ck_local_result *ck_local_start(uint64_t request, uint64_t invocation, 234 unsigned tool, const void *in, size_t length) 235 { 236 if (!request || request != active_request || !invocation || length > CK_LOCAL_INPUT || 237 (length && !in) || tool < 1 || tool > 3) 238 return refuse(request, invocation, CK_LOCAL_BAD_REQUEST); 239 uint8_t digest[32]; sha256_hash(in, length, digest); 240 for (unsigned i = 0; i < used; i++) 241 if (calls[i].result.invocation == invocation) { 242 if (calls[i].tool != tool || calls[i].input_len != length || 243 !equal(calls[i].input_hash, digest, 32)) 244 return refuse(request, invocation, CK_LOCAL_BAD_REQUEST); 245 return &calls[i].result; 246 } 247 if (running || used == CK_LOCAL_CALLS) 248 return refuse(request, invocation, CK_LOCAL_LIMIT); 249 unsigned i = used++; 250 calls[i].tool = tool; calls[i].input_len = length; 251 memcpy(calls[i].input_hash, digest, 32); 252 running = &calls[i].result; 253 running->request = request; running->invocation = invocation; 254 running->state = CK_LOCAL_RUNNING; 255 if ((tool == 1 && length != 24) || (tool == 3 && length)) { 256 finish(CK_LOCAL_REFUSED, CK_LOCAL_BAD_REQUEST); 257 } else if (!approved[tool - 1].ok) { 258 finish(CK_LOCAL_REFUSED, CK_LOCAL_ADMISSION); 259 } else { 260 if (tool == 3) { in = snapshot; length = sizeof snapshot; } 261 if (prepare(approved[tool - 1].code, approved[tool - 1].len, in, length)) { 262 finish(CK_LOCAL_REFUSED, CK_LOCAL_ADMISSION); 263 } else { 264 sha256_hash(code, approved[tool - 1].len, digest); 265 if (!equal(digest, approved[tool - 1].hash, 32)) 266 finish(CK_LOCAL_REFUSED, CK_LOCAL_ADMISSION); 267 } 268 } 269 if (running->state != CK_LOCAL_RUNNING) clear_task(); 270 return &calls[i].result; 271 } 272 void ck_local_trap(struct ck_x86_frame *f) 273 { 274 ck_wr_cr3(kernel_root); 275 ck_local_machine_timer(0); 276 ck_local_machine_eoi(f->vector); 277 if (!running || (f->cs & 3) != 3 || f->vector == 2 || f->vector == 8 || f->vector == 18) 278 ck_panic("local_tools: fatal interrupt %llu", (unsigned long long)f->vector); 279 uint64_t now = ck_rdtsc(); 280 cpu_used += now - slice_start; 281 frame = *f; 282 /* Prevent user NT/VM/TF/IOPL/AC from changing the next kernel IRET. */ 283 frame.rflags = (f->rflags & 0xcd5ull) | 0x202; 284 frame.cs = 0x2b; frame.ss = 0x33; 285 if (cpu_used >= cpu_limit) finish(CK_LOCAL_ABNORMAL, CK_LOCAL_CPU); 286 else if (now - started >= elapsed_limit) finish(CK_LOCAL_ABNORMAL, CK_LOCAL_ELAPSED); 287 else if (f->vector == 0x80) { 288 if (f->rip == STUB + 7 && f->rdx == 1 && f->rax <= CK_LOCAL_OUTPUT) { 289 running->length = (unsigned)f->rax; 290 const uint8_t *result = workspace; 291 #ifdef CK_LOCAL_MODEL 292 if (model_running) result = model_work; 293 #endif 294 memcpy(running->output, result, running->length); 295 finish(CK_LOCAL_RETURNED, 0); 296 } else if (f->rip == STUB + 16 && f->rdx == 2 && f->rsi >= 1 && f->rsi <= 14) { 297 finish(CK_LOCAL_TRAPPED, (unsigned)f->rsi); 298 } else finish(CK_LOCAL_ABNORMAL, CK_LOCAL_BAD_RETURN); 299 } else if (f->vector < 32) finish(CK_LOCAL_ABNORMAL, CK_LOCAL_FAULT); 300 /* Timer/other external IRQ: return to device polling with saved user state. */ 301 } 302 void ck_local_step(void) 303 { 304 if (!running) return; 305 if (ck_rdtsc() - started >= elapsed_limit) { 306 finish(CK_LOCAL_ABNORMAL, CK_LOCAL_ELAPSED); clear_task(); return; 307 } 308 uint64_t cr0 = ck_rd_cr0(), cr4 = ck_rd_cr4(), efer = ck_rdmsr(0xc0000080); 309 uint64_t sysenter = ck_rdmsr(0x174); 310 uint16_t ds, es, fs_sel, gs_sel, ss; 311 __asm__ volatile("mov %%ds,%0; mov %%es,%1; mov %%fs,%2; mov %%gs,%3; mov %%ss,%4" 312 : "=r"(ds), "=r"(es), "=r"(fs_sel), "=r"(gs_sel), "=r"(ss)); 313 uint64_t fs = ck_rdmsr(0xc0000100), gs = ck_rdmsr(0xc0000101); 314 /* Disable FSGSBASE, OSXSAVE/AVX, PKE/WRPKRU and user RDPMC. SCE off makes 315 * any Linux SYSCALL a contained #UD, with no firmware LSTAR to reach. */ 316 __asm__ volatile("mov %0, %%cr0" :: "r"((cr0 | 2) & ~12ull) : "memory"); 317 __asm__ volatile("mov %0, %%cr4" :: "r"((cr4 | 0x600) & ~((1ull<<16)|(1ull<<18)|(1ull<<22)|(1ull<<8))) : "memory"); 318 ck_wrmsr(0xc0000080, efer & ~1ull); 319 ck_wrmsr(0x174, 0); 320 if (ck_local_machine_timer(1)) finish(CK_LOCAL_ABNORMAL, CK_LOCAL_CPU); 321 else { 322 slice_start = ck_rdtsc(); 323 ck_wr_cr3((uint64_t)(uintptr_t)root); 324 ck_local_enter(&frame); 325 } 326 __asm__ volatile("mov %0,%%ds; mov %1,%%es; mov %2,%%fs; mov %3,%%gs; mov %4,%%ss" 327 :: "r"(ds), "r"(es), "r"(fs_sel), "r"(gs_sel), "r"(ss) : "memory"); 328 ck_wrmsr(0x174, sysenter); 329 ck_wrmsr(0xc0000100, fs); ck_wrmsr(0xc0000101, gs); 330 ck_wrmsr(0xc0000080, efer); 331 __asm__ volatile("mov %0, %%cr4" :: "r"(cr4) : "memory"); 332 __asm__ volatile("mov %0, %%cr0" :: "r"(cr0) : "memory"); 333 wipe(ck_local_kernel_fp, sizeof ck_local_kernel_fp); ck_local_kernel_sp = 0; 334 if (running->state != CK_LOCAL_RUNNING) clear_task(); 335 } 336 337 #ifndef CK_LOCAL_TEST 338 void ck_local_selftest(void) {} 339 #else 340 #include "local_tests.inc" 341 #endif