omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
| 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 | #include "omega_exec.h" |
| 18 | #include <sys/mman.h> |
| 19 | #include <string.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <pthread.h> |
| 25 | |
| 26 | typedef uint64_t (*OmegaExecFn3)(uint64_t, uint64_t, uint64_t); |
| 27 | |
| 28 | /* One resident executable mapping. Identity is the exact code bytes. */ |
| 29 | typedef struct { |
| 30 | void *mem; |
| 31 | size_t alloc_size; |
| 32 | size_t code_len; |
| 33 | uint8_t code[AARCH64_MAX_CODE_BYTES]; |
| 34 | uint64_t last_use; |
| 35 | } ExecSlot; |
| 36 | |
| 37 | static pthread_mutex_t g_cache_mu = PTHREAD_MUTEX_INITIALIZER; |
| 38 | static ExecSlot g_slots[OMEGA_EXEC_CACHE_SLOTS]; |
| 39 | static OmegaExecCacheStats g_stats; |
| 40 | static uint64_t g_tick; |
| 41 | |
| 42 | static void slot_drop(ExecSlot *s) { |
| 43 | if (s->mem) { munmap(s->mem, s->alloc_size); g_stats.resident--; } |
| 44 | memset(s, 0, sizeof *s); |
| 45 | } |
| 46 | |
| 47 | void omega_exec_native_cache_reset(void) { |
| 48 | pthread_mutex_lock(&g_cache_mu); |
| 49 | for (unsigned i = 0; i < OMEGA_EXEC_CACHE_SLOTS; i++) slot_drop(&g_slots[i]); |
| 50 | memset(&g_stats, 0, sizeof g_stats); |
| 51 | g_tick = 0; |
| 52 | pthread_mutex_unlock(&g_cache_mu); |
| 53 | } |
| 54 | |
| 55 | void omega_exec_native_cache_stats(OmegaExecCacheStats *out) { |
| 56 | if (!out) return; |
| 57 | pthread_mutex_lock(&g_cache_mu); |
| 58 | *out = g_stats; |
| 59 | pthread_mutex_unlock(&g_cache_mu); |
| 60 | } |
| 61 | |
| 62 | #if defined(__aarch64__) |
| 63 | /* Returns the slot holding these bytes, mapping them if needed; NULL on a mapping failure. |
| 64 | * Victim choice: the first empty slot, else the least recently used. */ |
| 65 | static ExecSlot *slot_acquire(const RealizationObject *real) { |
| 66 | ExecSlot *empty = NULL, *lru = NULL; |
| 67 | for (unsigned i = 0; i < OMEGA_EXEC_CACHE_SLOTS; i++) { |
| 68 | ExecSlot *s = &g_slots[i]; |
| 69 | if (s->mem && s->code_len == real->code_len && memcmp(s->code, real->code_bytes, real->code_len) == 0) { |
| 70 | s->last_use = ++g_tick; |
| 71 | g_stats.hits++; |
| 72 | return s; |
| 73 | } |
| 74 | if (!s->mem) { if (!empty) empty = s; } |
| 75 | else if (!lru || s->last_use < lru->last_use) lru = s; |
| 76 | } |
| 77 | ExecSlot *victim = empty ? empty : lru; |
| 78 | if (victim->mem) { slot_drop(victim); g_stats.evictions++; } |
| 79 | |
| 80 | size_t page_size = (size_t)sysconf(_SC_PAGESIZE); |
| 81 | size_t alloc_size = (real->code_len + page_size - 1) & ~(page_size - 1); |
| 82 | void *mem = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
| 83 | if (mem == MAP_FAILED) return NULL; |
| 84 | memcpy(mem, real->code_bytes, real->code_len); |
| 85 | __builtin___clear_cache((char*)mem, (char*)mem + real->code_len); |
| 86 | if (mprotect(mem, alloc_size, PROT_READ | PROT_EXEC) != 0) { munmap(mem, alloc_size); return NULL; } |
| 87 | |
| 88 | victim->mem = mem; |
| 89 | victim->alloc_size = alloc_size; |
| 90 | victim->code_len = real->code_len; |
| 91 | memcpy(victim->code, real->code_bytes, real->code_len); |
| 92 | victim->last_use = ++g_tick; |
| 93 | g_stats.misses++; |
| 94 | g_stats.resident++; |
| 95 | return victim; |
| 96 | } |
| 97 | #endif |
| 98 | |
| 99 | int omega_exec_native_f3(const RealizationObject *real, uint64_t a, uint64_t b, uint64_t c, uint64_t *out_res) { |
| 100 | if (!real || !out_res || real->code_len == 0 || real->code_len > AARCH64_MAX_CODE_BYTES) return -1; |
| 101 | |
| 102 | #if defined(__aarch64__) |
| 103 | union { void *ptr; OmegaExecFn3 fn; } u; |
| 104 | pthread_mutex_lock(&g_cache_mu); |
| 105 | ExecSlot *s = slot_acquire(real); |
| 106 | if (!s) { pthread_mutex_unlock(&g_cache_mu); return -1; } |
| 107 | u.ptr = s->mem; |
| 108 | /* Held under the lock: no other thread can unmap this slot during the (short) call. */ |
| 109 | *out_res = u.fn(a, b, c); |
| 110 | pthread_mutex_unlock(&g_cache_mu); |
| 111 | return 0; |
| 112 | #else |
| 113 | (void)a; (void)b; (void)c; |
| 114 | return -2; /* Not native AArch64 host */ |
| 115 | #endif |
| 116 | } |
| 117 | |
| 118 | #include "aarch64_encoder.h" |
| 119 | |
| 120 | int omega_build_qemu_runner(const RealizationObject *real, const char *out_bin_path) { |
| 121 | if (!real || !out_bin_path) return -1; |
| 122 | |
| 123 | FILE *f = fopen(out_bin_path, "wb"); |
| 124 | if (!f) return -1; |
| 125 | |
| 126 | uint8_t boot_code[128]; |
| 127 | size_t boot_pos = 0; |
| 128 | |
| 129 | /* 1. Setup inputs: |
| 130 | * MOVZ X0, 7 |
| 131 | * MOVZ X1, 11 |
| 132 | * MOVZ X2, 3 |
| 133 | */ |
| 134 | aarch64_emit_movz(boot_code, &boot_pos, sizeof(boot_code), true, 0, 7, 0); |
| 135 | aarch64_emit_movz(boot_code, &boot_pos, sizeof(boot_code), true, 1, 11, 0); |
| 136 | aarch64_emit_movz(boot_code, &boot_pos, sizeof(boot_code), true, 2, 3, 0); |
| 137 | |
| 138 | /* 2. BL to synthesized function right after the branch */ |
| 139 | /* BL is relative word offset: BL +8 (skips 1 insn: the branch over the function) */ |
| 140 | uint32_t bl_insn = 0x94000002; |
| 141 | boot_code[boot_pos++] = (uint8_t)(bl_insn & 0xff); |
| 142 | boot_code[boot_pos++] = (uint8_t)((bl_insn >> 8) & 0xff); |
| 143 | boot_code[boot_pos++] = (uint8_t)((bl_insn >> 16) & 0xff); |
| 144 | boot_code[boot_pos++] = (uint8_t)((bl_insn >> 24) & 0xff); |
| 145 | |
| 146 | /* 3. Branch over synthesized function to post_code */ |
| 147 | size_t fn_words = real->code_len / 4; |
| 148 | aarch64_emit_b(boot_code, &boot_pos, sizeof(boot_code), (int32_t)(fn_words + 1)); |
| 149 | |
| 150 | fwrite(boot_code, 1, boot_pos, f); |
| 151 | fwrite(real->code_bytes, 1, real->code_len, f); |
| 152 | |
| 153 | /* 4. Post-check and UART telemetry */ |
| 154 | uint8_t post_code[1024]; |
| 155 | size_t post_pos = 0; |
| 156 | |
| 157 | /* Base UART = 0x09000000 (MOVZ X4, 0x0900, LSL #16) */ |
| 158 | aarch64_emit_movz(post_code, &post_pos, sizeof(post_code), true, 4, 0x0900, 16); |
| 159 | |
| 160 | /* Verify X0 == 15 */ |
| 161 | /* MOVZ X7, 15 */ |
| 162 | aarch64_emit_movz(post_code, &post_pos, sizeof(post_code), true, 7, 15, 0); |
| 163 | /* SUB X6, X0, X7 */ |
| 164 | aarch64_emit_sub_reg(post_code, &post_pos, sizeof(post_code), true, 6, 0, 7); |
| 165 | |
| 166 | /* CBNZ X6, fail_block (+74 instructions) */ |
| 167 | aarch64_emit_cbnz(post_code, &post_pos, sizeof(post_code), true, 6, 74); |
| 168 | |
| 169 | /* Pass block */ |
| 170 | const char *pass_msg = "OMEGA_QEMU_EXEC: PASS (observed=15)\n"; |
| 171 | for (size_t i = 0; pass_msg[i]; ++i) { |
| 172 | aarch64_emit_movz(post_code, &post_pos, sizeof(post_code), false, 5, (uint8_t)pass_msg[i], 0); |
| 173 | uint32_t strb = 0x39000085; /* STRB W5, [X4] */ |
| 174 | post_code[post_pos++] = (uint8_t)(strb & 0xff); |
| 175 | post_code[post_pos++] = (uint8_t)((strb >> 8) & 0xff); |
| 176 | post_code[post_pos++] = (uint8_t)((strb >> 16) & 0xff); |
| 177 | post_code[post_pos++] = (uint8_t)((strb >> 24) & 0xff); |
| 178 | } |
| 179 | /* Jump over fail block to exit sequence: |
| 180 | * fail_msg is 33 chars * 2 insns = 66 insns. |
| 181 | * Offset to exit sequence = 1 + 66 = 67 insns. |
| 182 | */ |
| 183 | aarch64_emit_b(post_code, &post_pos, sizeof(post_code), 67); |
| 184 | |
| 185 | /* Fail block */ |
| 186 | const char *fail_msg = "OMEGA_QEMU_EXEC: FAIL (mismatch)\n"; |
| 187 | for (size_t i = 0; fail_msg[i]; ++i) { |
| 188 | aarch64_emit_movz(post_code, &post_pos, sizeof(post_code), false, 5, (uint8_t)fail_msg[i], 0); |
| 189 | uint32_t strb = 0x39000085; /* STRB W5, [X4] */ |
| 190 | post_code[post_pos++] = (uint8_t)(strb & 0xff); |
| 191 | post_code[post_pos++] = (uint8_t)((strb >> 8) & 0xff); |
| 192 | post_code[post_pos++] = (uint8_t)((strb >> 16) & 0xff); |
| 193 | post_code[post_pos++] = (uint8_t)((strb >> 24) & 0xff); |
| 194 | } |
| 195 | |
| 196 | /* Exit sequence via AArch64 Semihosting (SYS_EXIT 0x18) */ |
| 197 | /* 1. MOVZ W0, 0x18 */ |
| 198 | uint32_t exit_insns[] = { |
| 199 | 0x52800300, /* MOVZ W0, #0x18 (SYS_EXIT) */ |
| 200 | 0x10000061, /* ADR X1, +12 bytes (pointing to param_block) */ |
| 201 | 0xD45E0000, /* HLT 0xF000 (Semihosting trap) */ |
| 202 | 0x14000000, /* B . (fallback halt loop) */ |
| 203 | 0x00020026, /* param_block: ADP_Stopped_ApplicationExit low word */ |
| 204 | 0x00000000, /* ADP_Stopped_ApplicationExit high word */ |
| 205 | 0x00000000, /* exit code 0 low word */ |
| 206 | 0x00000000 /* exit code 0 high word */ |
| 207 | }; |
| 208 | for (size_t i = 0; i < sizeof(exit_insns) / sizeof(exit_insns[0]); ++i) { |
| 209 | post_code[post_pos++] = (uint8_t)(exit_insns[i] & 0xff); |
| 210 | post_code[post_pos++] = (uint8_t)((exit_insns[i] >> 8) & 0xff); |
| 211 | post_code[post_pos++] = (uint8_t)((exit_insns[i] >> 16) & 0xff); |
| 212 | post_code[post_pos++] = (uint8_t)((exit_insns[i] >> 24) & 0xff); |
| 213 | } |
| 214 | |
| 215 | fwrite(post_code, 1, post_pos, f); |
| 216 | fclose(f); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | int omega_exec_qemu_virt(const char *runner_bin_path, char *out_log, size_t out_log_len) { |
| 221 | if (!runner_bin_path || !out_log || out_log_len == 0) return -1; |
| 222 | |
| 223 | const char *serial_file = "build/qemu_serial.txt"; |
| 224 | unlink(serial_file); |
| 225 | |
| 226 | char cmd[512]; |
| 227 | snprintf(cmd, sizeof(cmd), |
| 228 | "timeout 5 qemu-system-aarch64 -M virt -cpu cortex-a57 -m 128M -nographic " |
| 229 | "-monitor none -semihosting -serial file:%s -bios %s >/dev/null 2>&1", |
| 230 | serial_file, runner_bin_path); |
| 231 | |
| 232 | int ret = system(cmd); |
| 233 | (void)ret; |
| 234 | |
| 235 | FILE *f = fopen(serial_file, "r"); |
| 236 | if (!f) return -1; |
| 237 | |
| 238 | size_t n = fread(out_log, 1, out_log_len - 1, f); |
| 239 | out_log[n] = '\0'; |
| 240 | fclose(f); |
| 241 | |
| 242 | return (strstr(out_log, "OMEGA_QEMU_EXEC: PASS") != NULL) ? 0 : -1; |
| 243 | } |