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/CUDApp/blob/main/om/platform-linux/gen/link/plat_fx_effects.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. 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 `CUDApp / om / platform-linux / gen / link / plat_fx_effects.c` 943 lines · 50898 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* Generated by om/platform-linux/gen_effects.sh; do not edit. */ 3 #if defined(__aarch64__) 4 #define OMBR_USES_MAPS 1 5 /* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md). 6 * The Omega machine code below is embedded byte for byte as the seeded compiler wrote it. 7 * This file only adapts the C calling convention to it. Regenerate it; do not edit it. */ 8 #include 9 #include 10 #include 11 #include 12 #include 13 #define OMEGA_OK 0u 14 #define OMEGA_BAD_ARG 32u 15 #define OMEGA_REENTRY 33u 16 #define OMEGA_BAD_RT 34u 17 #define OMEGA_NO_STACK 35u 18 #define OMBR_STACK (8u << 20) 19 #define OMBR_GUARD (8u << 20) 20 /* entry[2] is the trap entry (OSC-1 section 7). */ 21 typedef struct ombr_rt { const void *entry[32]; } ombr_rt; 22 typedef struct ombr_span { uintptr_t lo, hi; int w; } ombr_span; 23 #define OMEGA_CONTEXT_BUSY 36u 24 typedef struct omega_native_context { 25 pthread_mutex_t lock; 26 uint64_t *maps; 27 const ombr_rt *rt; 28 } omega_native_context; 29 static _Alignas(16) uint64_t ombr_maps[516]; 30 static omega_native_context ombr_default = { .lock = PTHREAD_MUTEX_INITIALIZER, .maps = ombr_maps }; 31 static _Thread_local volatile sig_atomic_t ombr_active; 32 static _Thread_local unsigned char *ombr_stk; 33 static pthread_key_t ombr_key; 34 static int ombr_keyok; 35 static pthread_once_t ombr_once = PTHREAD_ONCE_INIT; 36 uint32_t plat_fx_ombr_enter(const void *code, const uint64_t *args, const ombr_rt *rt, uint64_t *out, void *top) 37 __attribute__((visibility("hidden"))); 38 extern const unsigned char plat_fx_ombr_trap[] __attribute__((visibility("hidden"))); 39 extern const unsigned char plat_fx_ombr_no_entry[] __attribute__((visibility("hidden"))); 40 /* The object format's spelling of the embedded code and runtime symbols. ELF keeps them local and hidden 41 * in .text.omega_bridge. Mach-O (macOS) prefixes C symbol names with an underscore and keeps them local 42 * by not declaring them .globl; .alt_entry keeps the runtime entries in plat_fx_ombr_enter's atom. */ 43 #if defined(__APPLE__) && defined(__MACH__) 44 #define OMBR_SYM(n) "_" #n 45 #define OMBR_LOCAL(n) "" 46 #define OMBR_HIDDEN(n) "" 47 #define OMBR_FUNC(n) "" 48 #define OMBR_ALT(n) ".alt_entry _" #n "\n" 49 #define OMBR_SIZE(n) "" 50 #define OMBR_LEAVE "Lombr_leave" 51 #define OMBR_CODE_SECTION ".section __TEXT,__text,regular,pure_instructions\n" 52 #define OMBR_PREVIOUS "" 53 #elif defined(__ELF__) 54 #define OMBR_SYM(n) #n 55 #define OMBR_LOCAL(n) ".local " #n "\n" 56 #define OMBR_HIDDEN(n) ".hidden " #n "\n" 57 #define OMBR_FUNC(n) ".type " #n ",%function\n" 58 #define OMBR_ALT(n) OMBR_FUNC(n) 59 #define OMBR_SIZE(n) ".size " #n ",.-" #n "\n" 60 #define OMBR_LEAVE "ombr_leave" 61 #define OMBR_CODE_SECTION ".section .text.omega_bridge,\"ax\",@progbits\n" 62 #define OMBR_PREVIOUS ".previous\n" 63 #else 64 #error "omega-bridge native binding: ELF or Mach-O objects only" 65 #endif 66 /* plat_fx_ombr_enter saves the callee-saved registers on the caller's stack, keeps that stack pointer in x19 and 67 * runs the code on the binding's own stack. A normal return stores x0 through out and 68 * returns 0. A runtime entry restores the stack pointer from x19 and returns a status; out is not 69 * written. A unit's code must not write x18, x19 or x29 and must keep sp balanced; it may write x0..x17, 70 * x20..x28 and v0..v31 (x20..x28 and d8..d15 are restored here). Runtime entries are reached by blr/br, so they start with bti jc. */ 71 #if !defined(__aarch64__) 72 #error "omega-bridge native binding: the embedded code is AArch64" 73 #endif 74 __asm__(".text\n.balign 4\n" OMBR_LOCAL( plat_fx_ombr_enter ) OMBR_HIDDEN(plat_fx_ombr_enter) OMBR_FUNC(plat_fx_ombr_enter) 75 OMBR_SYM(plat_fx_ombr_enter) ":\n" 76 "hint #34\nstp x29,x30,[sp,#-176]!\nmov x29,sp\nstp x19,x20,[sp,#16]\nstp x21,x22,[sp,#32]\n" 77 "stp x23,x24,[sp,#48]\nstp x25,x26,[sp,#64]\nstp x27,x28,[sp,#80]\nstp d8,d9,[sp,#96]\n" 78 "stp d10,d11,[sp,#112]\nstp d12,d13,[sp,#128]\nstp d14,d15,[sp,#144]\nstr x3,[sp,#160]\n" 79 "mov x19,sp\nmov x16,x0\nmov x17,x1\nmov x7,x2\nmov sp,x4\n" 80 "ldp x0,x1,[x17]\nldp x2,x3,[x17,#16]\nldp x4,x5,[x17,#32]\nblr x16\n" 81 "mov sp,x19\nldr x9,[sp,#160]\nstr x0,[x9]\nmov w0,#0\n" 82 OMBR_LEAVE ":\nldp d14,d15,[sp,#144]\nldp d12,d13,[sp,#128]\nldp d10,d11,[sp,#112]\n" 83 "ldp d8,d9,[sp,#96]\nldp x27,x28,[sp,#80]\nldp x25,x26,[sp,#64]\nldp x23,x24,[sp,#48]\n" 84 "ldp x21,x22,[sp,#32]\nldp x19,x20,[sp,#16]\nldp x29,x30,[sp],#176\nret\n" OMBR_SIZE(plat_fx_ombr_enter) 85 OMBR_LOCAL(plat_fx_ombr_trap) OMBR_HIDDEN(plat_fx_ombr_trap) OMBR_ALT(plat_fx_ombr_trap) OMBR_SYM(plat_fx_ombr_trap) ":\nhint #38\n" 86 "mov sp,x19\nsub x2,x1,#1\nmov w3,#34\ncmp x2,#13\ncsel w0,w1,w3,ls\nb " OMBR_LEAVE "\n" 87 OMBR_SIZE(plat_fx_ombr_trap) 88 OMBR_LOCAL(plat_fx_ombr_no_entry) OMBR_HIDDEN(plat_fx_ombr_no_entry) OMBR_ALT(plat_fx_ombr_no_entry) OMBR_SYM(plat_fx_ombr_no_entry) ":\nhint #38\n" 89 "mov sp,x19\nmov w0,#34\nb " OMBR_LEAVE "\n" OMBR_SIZE(plat_fx_ombr_no_entry)); 90 static void ombr_free(void *p) 91 { 92 munmap(p, OMBR_STACK + OMBR_GUARD); 93 } 94 static void ombr_init(void) 95 { 96 ombr_keyok = pthread_key_create(&ombr_key, ombr_free) == 0; 97 } 98 /* The thread's Omega stack: OMBR_STACK usable bytes above an OMBR_GUARD no-access region. A function-mode 99 * unit has no call cycles and at most 4 MiB of local arrays per function (OMEGESE, local arrays), so a frame that 100 * would pass the bottom starts inside the guard and faults there; nothing below the guard is touched. */ 101 static unsigned char *ombr_stack(void) 102 { 103 if (ombr_stk) 104 return ombr_stk; 105 pthread_once(&ombr_once, ombr_init); 106 if (!ombr_keyok) 107 return 0; 108 unsigned char *p = mmap(0, OMBR_STACK + OMBR_GUARD, PROT_READ | PROT_WRITE, 109 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); 110 if (p == MAP_FAILED) 111 return 0; 112 if (mprotect(p, OMBR_GUARD, PROT_NONE) != 0 || pthread_setspecific(ombr_key, p) != 0) { 113 munmap(p, OMBR_STACK + OMBR_GUARD); 114 return 0; 115 } 116 ombr_stk = p + OMBR_GUARD + OMBR_STACK; 117 return ombr_stk; 118 } 119 static int ombr_span_add(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t want, uint64_t es, int w) 120 { 121 uintptr_t lo = (uintptr_t)p; 122 if (!p || n != want || lo % es != 0u || n > (UINTPTR_MAX - lo) / es) 123 return 1; 124 sp[*ns].lo = lo; 125 sp[*ns].hi = lo + n * es; 126 sp[*ns].w = w; 127 (*ns)++; 128 return 0; 129 } 130 static int ombr_overlap(const ombr_span *sp, unsigned ns) 131 { 132 for (unsigned i = 0; i < ns; i++) 133 for (unsigned j = i + 1; j < ns; j++) 134 if ((sp[i].w || sp[j].w) && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi) 135 return 1; 136 return 0; 137 } 138 /* One read-only runtime table for every call and thread: the code only reads it (a write would fault). */ 139 static const ombr_rt ombr_rt_table = {{ plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_trap, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, ombr_maps }}; 140 /* Explicit C ABI contexts: compatibility-runtime ownership, not driver policy. */ 141 static pthread_once_t ombr_map_once = PTHREAD_ONCE_INIT; 142 static pthread_mutex_t ombr_id_lock = PTHREAD_MUTEX_INITIALIZER; 143 static uint64_t ombr_next_context = 2; 144 static void ombr_map_init(void) 145 { 146 ombr_maps[0] = 128; 147 ombr_maps[1] = `67108864`; 148 ombr_maps[2] = `134217727`; 149 for (unsigned i = 0; i < 128; i++) 150 ombr_maps[4 + i * 4 + 2] = ombr_maps[1]; 151 ombr_default.rt = &ombr_rt_table; 152 } 153 __attribute__((visibility("default"))) uint32_t plat_fx_context_create(uint64_t capacity, 154 omega_native_context **out) 155 { 156 if (!out || capacity == 0 || capacity > 4098) 157 return OMEGA_BAD_ARG; 158 if (ombr_active) 159 return OMEGA_REENTRY; 160 if (pthread_mutex_lock(&ombr_id_lock) != 0) 161 return OMEGA_BAD_RT; 162 uint64_t id = ombr_next_context; 163 if (id <= `16777215`) 164 ombr_next_context++; 165 int unlock_error = pthread_mutex_unlock(&ombr_id_lock); 166 if (unlock_error || id > `16777215`) 167 return OMEGA_BAD_RT; 168 omega_native_context *c = calloc(1, sizeof *c); 169 if (!c) 170 return OMEGA_BAD_RT; 171 c->maps = calloc((size_t)capacity + 1u, 32u); 172 if (!c->maps) { 173 free(c); 174 return OMEGA_BAD_RT; 175 } 176 if (pthread_mutex_init(&c->lock, 0) != 0) { 177 free(c->maps); 178 free(c); 179 return OMEGA_BAD_RT; 180 } 181 ombr_rt *rt = mmap(0, sizeof *rt, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 182 if (rt == MAP_FAILED) { 183 pthread_mutex_destroy(&c->lock); 184 free(c->maps); 185 free(c); 186 return OMEGA_BAD_RT; 187 } 188 *rt = ombr_rt_table; 189 rt->entry[31] = c->maps; 190 if (mprotect(rt, sizeof *rt, PROT_READ) != 0) { 191 munmap(rt, sizeof *rt); 192 pthread_mutex_destroy(&c->lock); 193 free(c->maps); 194 free(c); 195 return OMEGA_BAD_RT; 196 } 197 c->rt = rt; 198 c->maps[0] = capacity; 199 c->maps[1] = id << 26; 200 c->maps[2] = ((id + 1u) << 26) - 1u; 201 for (uint64_t i = 0; i < capacity; i++) 202 c->maps[4 + i * 4 + 2] = c->maps[1]; 203 *out = c; 204 return OMEGA_OK; 205 } 206 __attribute__((visibility("default"))) uint32_t plat_fx_context_destroy(omega_native_context *c) 207 { 208 if (!c || c == &ombr_default) 209 return OMEGA_BAD_ARG; 210 if (ombr_active) 211 return OMEGA_REENTRY; 212 /* Caller has joined/drained users and waiters before entering destruction. */ 213 if (pthread_mutex_lock(&c->lock) != 0) 214 return OMEGA_BAD_RT; 215 for (uint64_t i = 0; i < c->maps[0]; i++) { 216 if (c->maps[4 + i * 4] != 0) { 217 if (pthread_mutex_unlock(&c->lock) != 0) 218 return OMEGA_BAD_RT; 219 return OMEGA_CONTEXT_BUSY; 220 } 221 } 222 if (pthread_mutex_unlock(&c->lock) != 0 || pthread_mutex_destroy(&c->lock) != 0) 223 return OMEGA_BAD_RT; 224 int failed = munmap((void *)c->rt, sizeof *c->rt) != 0; 225 free(c->maps); 226 free(c); 227 return failed ? OMEGA_BAD_RT : OMEGA_OK; 228 } 229 static uint32_t ombr_context_lock(omega_native_context *c) 230 { 231 if (!c) 232 return OMEGA_BAD_ARG; 233 if (ombr_active) 234 return OMEGA_REENTRY; 235 ombr_active = 1; 236 if (c == &ombr_default && pthread_once(&ombr_map_once, ombr_map_init) != 0) { 237 ombr_active = 0; 238 return OMEGA_BAD_RT; 239 } 240 if (pthread_mutex_lock(&c->lock) != 0) { 241 ombr_active = 0; 242 return OMEGA_BAD_RT; 243 } 244 return OMEGA_OK; 245 } 246 static uint32_t ombr_context_unlock(omega_native_context *c, uint32_t status) 247 { 248 int failed = pthread_mutex_unlock(&c->lock); 249 ombr_active = 0; 250 return failed ? OMEGA_BAD_RT : status; 251 } 252 static uint32_t ombr_run(const void *code, const uint64_t *args, uint64_t *result, int has_value) 253 { 254 omega_native_context *context = &ombr_default; 255 uint32_t s = ombr_context_lock(context); 256 if (s != OMEGA_OK) 257 return s; 258 unsigned char *top = ombr_stack(); 259 if (!top) 260 return ombr_context_unlock(context, OMEGA_NO_STACK); 261 uint64_t v = 0; 262 s = plat_fx_ombr_enter(code, args, context->rt, &v, top); 263 if (s == OMEGA_OK) 264 *result = has_value ? v : 0u; 265 return ombr_context_unlock(context, s); 266 } 267 /* ABI v2 (docs/NATIVE-ABI.md, ABI v2): OM-2 units, bound as omega2_. An unsized view is its 268 * pointer and its element count; a layout is a pointer to its fixed bytes; an out parameter is written 269 * to a buffer the binding owns and copied to the caller only after status 0. */ 270 #include 271 typedef struct ombr2_out { unsigned slot; uint64_t n; void *dst; } ombr2_out; 272 /* A library of OM-2 units alone calls neither of the ABI v1 helpers. */ 273 __attribute__((unused)) static void ombr2_keep(void) 274 { 275 (void)ombr_overlap; 276 (void)ombr_run; 277 } 278 /* An unsized view of n elements of es bytes. n == 0 is the empty view whatever the pointer (it adds no 279 * range, so it overlaps nothing). Otherwise refused: a null pointer, a pointer not aligned to es, 280 * n * es over 64 bits, or an end past the top of the address space. */ 281 __attribute__((unused)) static int ombr2_view(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t es, int w) 282 { 283 uintptr_t lo = (uintptr_t)p; 284 if (n == 0u) 285 return 0; 286 if (!p || lo % es != 0u || n > UINT64_MAX / es || n * es > UINTPTR_MAX - lo) 287 return 1; 288 sp[*ns].lo = lo; 289 sp[*ns].hi = lo + n * es; 290 sp[*ns].w = w; 291 (*ns)++; 292 return 0; 293 } 294 /* A fixed range of nb > 0 bytes (a layout or an out parameter) aligned to es. */ 295 __attribute__((unused)) static int ombr2_fixed(ombr_span *sp, unsigned *ns, const void *p, uint64_t nb, uint64_t es, 296 int w) 297 { 298 uintptr_t lo = (uintptr_t)p; 299 if (!p || lo % es != 0u || nb > UINTPTR_MAX - lo) 300 return 1; 301 sp[*ns].lo = lo; 302 sp[*ns].hi = lo + nb; 303 sp[*ns].w = w; 304 (*ns)++; 305 return 0; 306 } 307 /* w: 0 view, 1 edit or the result, 2 out. Refused: an edit or the result overlapping any other range, 308 * or two out ranges overlapping. An out may overlap a view: it is written only after the code returns. */ 309 static int ombr2_overlap(const ombr_span *sp, unsigned ns) 310 { 311 for (unsigned i = 0; i < ns; i++) 312 for (unsigned j = i + 1; j < ns; j++) 313 if ((sp[i].w == 1 || sp[j].w == 1 || (sp[i].w == 2 && sp[j].w == 2)) 314 && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi) 315 return 1; 316 return 0; 317 } 318 /* As ombr_run, and each out buffer is carved from the top of the thread's Omega stack (16-byte aligned, 319 * zero-filled), passed in its slot and copied to the caller with memmove only after status 0. */ 320 static uint32_t ombr2_run(omega_native_context *context, const void *code, uint64_t *args, uint64_t *result, int has_value, const ombr2_out *o, 321 unsigned no) 322 { 323 uint32_t s = ombr_context_lock(context); 324 if (s != OMEGA_OK) 325 return s; 326 unsigned char *top = ombr_stack(); 327 if (!top) 328 return ombr_context_unlock(context, OMEGA_NO_STACK); 329 unsigned char *buf[6] = {0}; 330 for (unsigned i = 0; i < no; i++) { 331 top -= (o[i].n + 15u) / 16u * 16u; 332 memset(top, 0, o[i].n); 333 buf[i] = top; 334 args[o[i].slot] = (uint64_t)(uintptr_t)top; 335 } 336 uint64_t v = 0; 337 s = plat_fx_ombr_enter(code, args, context->rt, &v, top); 338 if (s == OMEGA_OK) { 339 for (unsigned i = 0; i < no; i++) 340 memmove(o[i].dst, buf[i], o[i].n); 341 *result = has_value ? v : 0u; 342 } 343 return ombr_context_unlock(context, s); 344 } 345 __asm__(OMBR_CODE_SECTION ".balign 4\n" OMBR_LOCAL(ombr2_code_plat_effects) OMBR_HIDDEN(ombr2_code_plat_effects) OMBR_SYM(ombr2_code_plat_effects) ":\n.byte 0x03,0x00,0x00,0x14,0x05,0x00,0x00,0x14,0x2c,0x00,0x00,0x14,0x9f,0x3f,0x03,0xd5,0x20,0x00,0x80,0xd2,0xc0,0x03,0x5f,0xd6,0x9f,0x00,0x00,0xf1,0x61,0x00,0x00,0x54,0x00,0x00,0x80,0xd2,0xc0,0x03,0x5f,0xd6,0x05,0x02,0xa0,0xd2,0xbf,0x00,0x04,0xeb,0x62,0x00,0x00,0x54,0x00,0x00,0x80,0xd2,0xc0,0x03,0x5f,0xd6,0x05,0x00,0x80,0xd2,0x06,0x00,0x80,0xd2,0xbf,0x00,0x04,0xeb,0xc2,0x01,0x00,0x54,0x08,0x48,0x88,0xd2,0xe8,0x01,0xa0,0xf2,0xdf,0x00,0x08,0xeb,0x82,0x02,0x00,0x54,0xc6,0x04,0x00,0x91,0xbf,0x00,0x03,0xeb,0xe2,0x01,0x00,0x54,0x48,0x68,0x65,0x38,0xbf,0x00,0x01,0xeb,0x82,0x01,0x00,0x54,0x08,0x68,0x25,0x38,0xa5,0x04,0x00,0x91,0xf2,0xff,0xff,0x17,0x9f,0x3f,0x03,0xd5,0x85,0xfc,0x7f,0xd3,0x45,0x01,0x00,0xb5,0xe0,0x03,0x04,0xaa,0xc0,0x03,0x5f,0xd6,0xe0,0x03,0x07,0xaa,0xf0,0x08,0x40,0xf9,0x00,0x02,0x1f,0xd6,0x61,0x00,0x80,0xd2,0xfc,0xff,0xff,0x17,0x81,0x00,0x80,0xd2,0xfa,0xff,0xff,0x17,0xa1,0x00,0x80,0xd2,0xf8,0xff,0xff,0x17,0x9f,0x00,0x00,0xf1,0xa1,0x00,0x00,0x54,0xc5,0x02,0x80,0xd2,0xe0,0x03,0x05,0xeb,0x86,0x0d,0x00,0x54,0xc0,0x03,0x5f,0xd6,0x05,0x02,0xa0,0xd2,0xbf,0x00,0x04,0xeb,0xa2,0x00,0x00,0x54,0xc5,0x02,0x80,0xd2,0xe0,0x03,0x05,0xeb,0xa6,0x0c,0x00,0x54,0xc0,0x03,0x5f,0xd6,0x06,0x00,0x80,0xd2,0x9f,0x00,0x03,0xeb,0x68,0x0c,0x00,0x54,0x68,0x00,0x04,0xcb,0xdf,0x00,0x08,0xeb,0x08,0x0c,0x00,0x54,0xff,0x43,0x01,0xd1,0xe0,0x03,0x00,0xf9,0xe1,0x07,0x00,0xf9,0xe2,0x0b,0x00,0xf9,0xe3,0x0f,0x00,0xf9,0xe4,0x13,0x00,0xf9,0xe5,0x17,0x00,0xf9,0xe6,0x1b,0x00,0xf9,0xe8,0x1f,0x00,0xf9,0xfe,0x23,0x00,0xf9,0xea,0x03,0x40,0xf9,0xef,0xff,0x83,0xd2,0x4b,0x01,0x0f,0x8a,0x7f,0x05,0x00,0xf1,0x23,0x0a,0x00,0x54,0xee,0x7c,0x40,0xf9,0xce,0x01,0x40,0xf9,0xdf,0x05,0x00,0xf1,0xa3,0x09,0x00,0x54,0x4f,0x00,0x82,0xd2,0xdf,0x01,0x0f,0xeb,0x48,0x09,0x00,0x54,0x7f,0x01,0x0e,0xeb,0x08,0x09,0x00,0x54,0x6b,0x05,0x00,0xd1,0x0f,0x04,0x80,0xd2,0x6b,0x7d,0x0f,0x9b,0xef,0x7c,0x40,0xf9,0xef,0x81,0x00,0x91,0x6b,0x01,0x0f,0x8b,0x6e,0x09,0x40,0xf9,0xdf,0x01,0x00,0xf1,0xe0,0x07,0x00,0x54,0xaf,0x01,0x80,0xd2,0x4a,0x25,0xcf,0x9a,0x5f,0x01,0x0e,0xeb,0x61,0x07,0x00,0x54,0x6d,0x01,0x40,0xf9,0xbf,0x01,0x00,0xf1,0x00,0x07,0x00,0x54,0x6c,0x05,0x40,0xf9,0x6f,0x0d,0x40,0xf9,0xff,0x01,0x00,0xf1,0x80,0x06,0x00,0x54,0xff,0x0d,0x00,0xf1,0x48,0x06,0x00,0x54,0xe0,0x07,0x40,0xf9,0x1f,0x00,0x0c,0xeb,0xe8,0x05,0x00,0x54,0x8c,0x01,0x00,0xcb,0xe2,0x13,0x40,0xf9,0x5f,0x00,0x0c,0xeb,0x68,0x05,0x00,0x54,0xa0,0x01,0x00,0x8b,0xe1,0x0b,0x40,0xf9,0xf0,0x1b,0x40,0xf9,0x21,0x00,0x10,0x8b,0x6f,0x0d,0x40,0xf9,0xff,0x0d,0x00,0xf1,0x81,0x04,0x00,0x54,0xef,0x03,0x00,0xaa,0xe0,0x03,0x01,0xaa,0xe1,0x03,0x0f,0xaa,0xe4,0x03,0x02,0xaa,0x5f,0x00,0x00,0xf1,0xe0,0x00,0x00,0x54,0x03,0x00,0x40,0x39,0x23,0x00,0x00,0x39,0x00,0x04,0x00,0x91,0x21,0x04,0x00,0x91,0x42,0x04,0x00,0xd1,0xf9,0xff,0xff,0x17,0xe0,0x03,0x04,0xaa,0xe5,0x03,0x00,0xaa,0xe0,0x03,0x40,0xf9,0xe1,0x07,0x40,0xf9,0xe2,0x0b,0x40,0xf9,0xe3,0x0f,0x40,0xf9,0xe4,0x13,0x40,0xf9,0xfe,0x23,0x40,0xf9,0xff,0x43,0x01,0x91,0x86,0xfc,0x7f,0xd3,0xe6,0x01,0x00,0xb5,0xbf,0x00,0x04,0xeb,0x60,0x00,0x00,0x54,0xe0,0x03,0x05,0xaa,0xc0,0x03,0x5f,0xd6,0x9f,0x3f,0x03,0xd5,0xe0,0x03,0x05,0xaa,0xc0,0x03,0x5f,0xd6,0xe0,0x03,0x07,0xaa,0xf0,0x08,0x40,0xf9,0x00,0x02,0x1f,0xd6,0x21,0x00,0x80,0xd2,0xfc,0xff,0xff,0x17,0x61,0x00,0x80,0xd2,0xfa,0xff,0xff,0x17,0xa1,0x00,0x80,0xd2,0xf8,0xff,0xff,0x17\n" OMBR_PREVIOUS); 346 static const char ombr2_ident_plat_effects[] = "omega-ident 1 unit=plat_effects abi=2 profile=OM-2 src=`50f1a64494248ffe9b92cafaf704fbe4ba647849b0ab9d906ea221ec941fbdcc` omc=`d1a3047153e14676c4baf9248fa2b88bb1b68c37cfeb71d76f30f5230c7d2765` host=mapwrite"; 347 __attribute__((visibility("default"))) const char *omega2_ident_plat_effects(void) 348 { 349 return ombr2_ident_plat_effects; 350 } 351 __asm__(OMBR_CODE_SECTION ".balign 4\n" OMBR_LOCAL(ombr2_entry_plat_fence) OMBR_HIDDEN(ombr2_entry_plat_fence) OMBR_SYM(ombr2_entry_plat_fence) ":\nhint #34\nb " OMBR_SYM(ombr2_code_plat_effects) "+0\n" OMBR_PREVIOUS); 352 extern const unsigned char ombr2_entry_plat_fence[] __attribute__((visibility("hidden"))); 353 /* export fn plat_fence to long */ 354 __attribute__((visibility("default"))) uint32_t omega2_plat_fence(uint64_t *result) 355 { 356 ombr_span sp[7]; 357 unsigned ns = 0; 358 uint64_t a[6] = {0}; 359 ombr2_out ou[6] = {{0, 0, 0}}; 360 unsigned no = 0; 361 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 362 return OMEGA_BAD_ARG; 363 if (ombr2_overlap(sp, ns)) 364 return OMEGA_BAD_ARG; 365 return ombr2_run(&ombr_default, ombr2_entry_plat_fence, a, result, 1, ou, no); 366 } 367 /* export fn plat_fence to long */ 368 __attribute__((visibility("default"))) uint32_t omega2_ctx_plat_fence(omega_native_context *context, uint64_t *result) 369 { 370 ombr_span sp[7]; 371 unsigned ns = 0; 372 uint64_t a[6] = {0}; 373 ombr2_out ou[6] = {{0, 0, 0}}; 374 unsigned no = 0; 375 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 376 return OMEGA_BAD_ARG; 377 if (ombr2_overlap(sp, ns)) 378 return OMEGA_BAD_ARG; 379 return ombr2_run(context, ombr2_entry_plat_fence, a, result, 1, ou, no); 380 } 381 __asm__(OMBR_CODE_SECTION ".balign 4\n" OMBR_LOCAL(ombr2_entry_plat_ring_store) OMBR_HIDDEN(ombr2_entry_plat_ring_store) OMBR_SYM(ombr2_entry_plat_ring_store) ":\nhint #34\nb " OMBR_SYM(ombr2_code_plat_effects) "+4\n" OMBR_PREVIOUS); 382 extern const unsigned char ombr2_entry_plat_ring_store[] __attribute__((visibility("hidden"))); 383 /* export fn plat_ring_store dst edit uchar * bytes view uchar * n ulong to long */ 384 __attribute__((visibility("default"))) uint32_t omega2_plat_ring_store(uint64_t *result, uint8_t *o_dst, uint64_t o_dst_len, const uint8_t *o_bytes, uint64_t o_bytes_len, uint64_t o_n) 385 { 386 ombr_span sp[7]; 387 unsigned ns = 0; 388 uint64_t a[6] = {0}; 389 ombr2_out ou[6] = {{0, 0, 0}}; 390 unsigned no = 0; 391 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 392 return OMEGA_BAD_ARG; 393 if (ombr2_view(sp, &ns, o_dst, o_dst_len, 1u, 1)) 394 return OMEGA_BAD_ARG; 395 a[0] = (uint64_t)(uintptr_t)o_dst; 396 a[1] = o_dst_len; 397 if (ombr2_view(sp, &ns, o_bytes, o_bytes_len, 1u, 0)) 398 return OMEGA_BAD_ARG; 399 a[2] = (uint64_t)(uintptr_t)o_bytes; 400 a[3] = o_bytes_len; 401 a[4] = o_n; 402 if (ombr2_overlap(sp, ns)) 403 return OMEGA_BAD_ARG; 404 return ombr2_run(&ombr_default, ombr2_entry_plat_ring_store, a, result, 1, ou, no); 405 } 406 /* export fn plat_ring_store dst edit uchar * bytes view uchar * n ulong to long */ 407 __attribute__((visibility("default"))) uint32_t omega2_ctx_plat_ring_store(omega_native_context *context, uint64_t *result, uint8_t *o_dst, uint64_t o_dst_len, const uint8_t *o_bytes, uint64_t o_bytes_len, uint64_t o_n) 408 { 409 ombr_span sp[7]; 410 unsigned ns = 0; 411 uint64_t a[6] = {0}; 412 ombr2_out ou[6] = {{0, 0, 0}}; 413 unsigned no = 0; 414 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 415 return OMEGA_BAD_ARG; 416 if (ombr2_view(sp, &ns, o_dst, o_dst_len, 1u, 1)) 417 return OMEGA_BAD_ARG; 418 a[0] = (uint64_t)(uintptr_t)o_dst; 419 a[1] = o_dst_len; 420 if (ombr2_view(sp, &ns, o_bytes, o_bytes_len, 1u, 0)) 421 return OMEGA_BAD_ARG; 422 a[2] = (uint64_t)(uintptr_t)o_bytes; 423 a[3] = o_bytes_len; 424 a[4] = o_n; 425 if (ombr2_overlap(sp, ns)) 426 return OMEGA_BAD_ARG; 427 return ombr2_run(context, ombr2_entry_plat_ring_store, a, result, 1, ou, no); 428 } 429 __asm__(OMBR_CODE_SECTION ".balign 4\n" OMBR_LOCAL(ombr2_entry_plat_map_store) OMBR_HIDDEN(ombr2_entry_plat_map_store) OMBR_SYM(ombr2_entry_plat_map_store) ":\nhint #34\nb " OMBR_SYM(ombr2_code_plat_effects) "+8\n" OMBR_PREVIOUS); 430 extern const unsigned char ombr2_entry_plat_map_store[] __attribute__((visibility("hidden"))); 431 /* export fn plat_map_store handle long off ulong bytes view uchar * n ulong to long */ 432 __attribute__((visibility("default"))) uint32_t omega2_plat_map_store(uint64_t *result, uint64_t o_handle, uint64_t o_off, const uint8_t *o_bytes, uint64_t o_bytes_len, uint64_t o_n) 433 { 434 ombr_span sp[7]; 435 unsigned ns = 0; 436 uint64_t a[6] = {0}; 437 ombr2_out ou[6] = {{0, 0, 0}}; 438 unsigned no = 0; 439 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 440 return OMEGA_BAD_ARG; 441 a[0] = o_handle; 442 a[1] = o_off; 443 if (ombr2_view(sp, &ns, o_bytes, o_bytes_len, 1u, 0)) 444 return OMEGA_BAD_ARG; 445 a[2] = (uint64_t)(uintptr_t)o_bytes; 446 a[3] = o_bytes_len; 447 a[4] = o_n; 448 if (ombr2_overlap(sp, ns)) 449 return OMEGA_BAD_ARG; 450 return ombr2_run(&ombr_default, ombr2_entry_plat_map_store, a, result, 1, ou, no); 451 } 452 /* export fn plat_map_store handle long off ulong bytes view uchar * n ulong to long */ 453 __attribute__((visibility("default"))) uint32_t omega2_ctx_plat_map_store(omega_native_context *context, uint64_t *result, uint64_t o_handle, uint64_t o_off, const uint8_t *o_bytes, uint64_t o_bytes_len, uint64_t o_n) 454 { 455 ombr_span sp[7]; 456 unsigned ns = 0; 457 uint64_t a[6] = {0}; 458 ombr2_out ou[6] = {{0, 0, 0}}; 459 unsigned no = 0; 460 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 461 return OMEGA_BAD_ARG; 462 a[0] = o_handle; 463 a[1] = o_off; 464 if (ombr2_view(sp, &ns, o_bytes, o_bytes_len, 1u, 0)) 465 return OMEGA_BAD_ARG; 466 a[2] = (uint64_t)(uintptr_t)o_bytes; 467 a[3] = o_bytes_len; 468 a[4] = o_n; 469 if (ombr2_overlap(sp, ns)) 470 return OMEGA_BAD_ARG; 471 return ombr2_run(context, ombr2_entry_plat_map_store, a, result, 1, ou, no); 472 } 473 474 #elif defined(__x86_64__) 475 #define OMBR_USES_MAPS 1 476 /* Generated by omega-bridge src/native.om: native call ABI v1 (docs/NATIVE-ABI.md). 477 * The Omega machine code below is embedded byte for byte as the seeded compiler wrote it. 478 * This file only adapts the C calling convention to it. Regenerate it; do not edit it. */ 479 #include 480 #include 481 #include 482 #include 483 #include 484 #define OMEGA_OK 0u 485 #define OMEGA_BAD_ARG 32u 486 #define OMEGA_REENTRY 33u 487 #define OMEGA_BAD_RT 34u 488 #define OMEGA_NO_STACK 35u 489 #define OMBR_STACK (8u << 20) 490 #define OMBR_GUARD (8u << 20) 491 /* entry[2] is the trap entry (OSC-1 section 7). */ 492 typedef struct ombr_rt { const void *entry[32]; } ombr_rt; 493 typedef struct ombr_span { uintptr_t lo, hi; int w; } ombr_span; 494 #define OMEGA_CONTEXT_BUSY 36u 495 typedef struct omega_native_context { 496 pthread_mutex_t lock; 497 uint64_t *maps; 498 const ombr_rt *rt; 499 } omega_native_context; 500 static _Alignas(16) uint64_t ombr_maps[516]; 501 static omega_native_context ombr_default = { .lock = PTHREAD_MUTEX_INITIALIZER, .maps = ombr_maps }; 502 static _Thread_local volatile sig_atomic_t ombr_active; 503 static _Thread_local unsigned char *ombr_stk; 504 static pthread_key_t ombr_key; 505 static int ombr_keyok; 506 static pthread_once_t ombr_once = PTHREAD_ONCE_INIT; 507 uint32_t plat_fx_ombr_enter(const void *code, const uint64_t *args, const ombr_rt *rt, uint64_t *out, void *top) 508 __attribute__((visibility("hidden"))); 509 extern const unsigned char plat_fx_ombr_trap[] __attribute__((visibility("hidden"))); 510 extern const unsigned char plat_fx_ombr_no_entry[] __attribute__((visibility("hidden"))); 511 /* The object format's spelling of the embedded code and runtime symbols. ELF keeps them local and hidden 512 * in .text.omega_bridge. Mach-O (macOS) prefixes C symbol names with an underscore and keeps them local 513 * by not declaring them .globl; .alt_entry keeps the runtime entries in plat_fx_ombr_enter's atom. */ 514 #if defined(__APPLE__) && defined(__MACH__) 515 #define OMBR_SYM(n) "_" #n 516 #define OMBR_LOCAL(n) "" 517 #define OMBR_HIDDEN(n) "" 518 #define OMBR_FUNC(n) "" 519 #define OMBR_ALT(n) ".alt_entry _" #n "\n" 520 #define OMBR_SIZE(n) "" 521 #define OMBR_LEAVE "Lombr_leave" 522 #define OMBR_CODE_SECTION ".section __TEXT,__text,regular,pure_instructions\n" 523 #define OMBR_PREVIOUS "" 524 #elif defined(__ELF__) 525 #define OMBR_SYM(n) #n 526 #define OMBR_LOCAL(n) ".local " #n "\n" 527 #define OMBR_HIDDEN(n) ".hidden " #n "\n" 528 #define OMBR_FUNC(n) ".type " #n ",%function\n" 529 #define OMBR_ALT(n) OMBR_FUNC(n) 530 #define OMBR_SIZE(n) ".size " #n ",.-" #n "\n" 531 #define OMBR_LEAVE "ombr_leave" 532 #define OMBR_CODE_SECTION ".section .text.omega_bridge,\"ax\",@progbits\n" 533 #define OMBR_PREVIOUS ".previous\n" 534 #else 535 #error "omega-bridge native binding: ELF or Mach-O objects only" 536 #endif 537 /* SysV AMD64 adapter to the pinned private Omega frame ABI. R12 anchors the native 538 * stack, R14 the runtime table, R15 the owned-stack floor. Hosted device helpers and legacy read/write preserve these reserved registers; 539 * the selected hosted compiler also restores its R13 argument-stack scratch. 540 * CET shadow-stack enforcement is not supported by the nonlocal trap return. */ 541 __asm__(".text\n.balign 16\n.local plat_fx_ombr_enter\n.hidden plat_fx_ombr_enter\n.type plat_fx_ombr_enter,@function\nplat_fx_ombr_enter:\n" 542 "endbr64\npush %rbx\npush %rbp\npush %r12\npush %r13\npush %r14\npush %r15\npush %rcx\n" 543 "mov %rsp,%r12\nmov %rdx,%r14\nmov %r9,%r15\nmov %rdi,%r10\n" 544 "mov %r8,%rbp\nmov %r8,%rsp\nsub $256,%rsp\n" 545 "mov 0(%rsi),%rax\nmov %rax,-8(%rbp)\nmov 8(%rsi),%rax\nmov %rax,-16(%rbp)\n" 546 "mov 16(%rsi),%rax\nmov %rax,-24(%rbp)\nmov 24(%rsi),%rax\nmov %rax,-32(%rbp)\n" 547 "mov 32(%rsi),%rax\nmov %rax,-40(%rbp)\nmov 40(%rsi),%rax\nmov %rax,-48(%rbp)\n" 548 "call *%r10\nmov %r12,%rsp\nmov (%rsp),%rcx\nmov %rax,(%rcx)\nxor %eax,%eax\n" 549 "ombr_leave:\nadd $8,%rsp\npop %r15\npop %r14\npop %r13\npop %r12\npop %rbp\npop %rbx\nret\n" 550 ".size plat_fx_ombr_enter,.-plat_fx_ombr_enter\n.local plat_fx_ombr_trap\n.hidden plat_fx_ombr_trap\n.type plat_fx_ombr_trap,@function\nplat_fx_ombr_trap:\n" 551 "endbr64\nmov %r12,%rsp\nlea -1(%rsi),%eax\ncmp $13,%eax\nmov $34,%eax\ncmovbe %esi,%eax\njmp ombr_leave\n" 552 ".size plat_fx_ombr_trap,.-plat_fx_ombr_trap\n.local plat_fx_ombr_no_entry\n.hidden plat_fx_ombr_no_entry\n.type plat_fx_ombr_no_entry,@function\nplat_fx_ombr_no_entry:\n" 553 "endbr64\nmov %r12,%rsp\nmov $34,%eax\njmp ombr_leave\n.size plat_fx_ombr_no_entry,.-plat_fx_ombr_no_entry\n"); 554 uint32_t ombr_xenter(const void *code, const uint64_t *args, const ombr_rt *rt, uint64_t *out, void *top, void *floor) __asm__("plat_fx_ombr_enter") __attribute__((visibility("hidden"))); 555 /* The true mapping floor must not move when ABI-v2 out buffers lower top. */ 556 #define plat_fx_ombr_enter(code,args,rt,out,top) ombr_xenter(code,args,rt,out,top,ombr_stk - OMBR_STACK) 557 static void ombr_free(void *p) 558 { 559 munmap(p, OMBR_STACK + OMBR_GUARD); 560 } 561 static void ombr_init(void) 562 { 563 ombr_keyok = pthread_key_create(&ombr_key, ombr_free) == 0; 564 } 565 /* The thread's Omega stack: OMBR_STACK usable bytes above an OMBR_GUARD no-access region. A function-mode 566 * unit has no call cycles and at most 4 MiB of local arrays per function (OMEGESE, local arrays), so a frame that 567 * would pass the bottom starts inside the guard and faults there; nothing below the guard is touched. */ 568 static unsigned char *ombr_stack(void) 569 { 570 if (ombr_stk) 571 return ombr_stk; 572 pthread_once(&ombr_once, ombr_init); 573 if (!ombr_keyok) 574 return 0; 575 unsigned char *p = mmap(0, OMBR_STACK + OMBR_GUARD, PROT_READ | PROT_WRITE, 576 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); 577 if (p == MAP_FAILED) 578 return 0; 579 if (mprotect(p, OMBR_GUARD, PROT_NONE) != 0 || pthread_setspecific(ombr_key, p) != 0) { 580 munmap(p, OMBR_STACK + OMBR_GUARD); 581 return 0; 582 } 583 ombr_stk = p + OMBR_GUARD + OMBR_STACK; 584 return ombr_stk; 585 } 586 static int ombr_span_add(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t want, uint64_t es, int w) 587 { 588 uintptr_t lo = (uintptr_t)p; 589 if (!p || n != want || lo % es != 0u || n > (UINTPTR_MAX - lo) / es) 590 return 1; 591 sp[*ns].lo = lo; 592 sp[*ns].hi = lo + n * es; 593 sp[*ns].w = w; 594 (*ns)++; 595 return 0; 596 } 597 static int ombr_overlap(const ombr_span *sp, unsigned ns) 598 { 599 for (unsigned i = 0; i < ns; i++) 600 for (unsigned j = i + 1; j < ns; j++) 601 if ((sp[i].w || sp[j].w) && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi) 602 return 1; 603 return 0; 604 } 605 /* One read-only runtime table for every call and thread: the code only reads it (a write would fault). */ 606 static const ombr_rt ombr_rt_table = {{ plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_trap, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, plat_fx_ombr_no_entry, ombr_maps }}; 607 /* Explicit C ABI contexts: compatibility-runtime ownership, not driver policy. */ 608 static pthread_once_t ombr_map_once = PTHREAD_ONCE_INIT; 609 static pthread_mutex_t ombr_id_lock = PTHREAD_MUTEX_INITIALIZER; 610 static uint64_t ombr_next_context = 2; 611 static void ombr_map_init(void) 612 { 613 ombr_maps[0] = 128; 614 ombr_maps[1] = `67108864`; 615 ombr_maps[2] = `134217727`; 616 for (unsigned i = 0; i < 128; i++) 617 ombr_maps[4 + i * 4 + 2] = ombr_maps[1]; 618 ombr_default.rt = &ombr_rt_table; 619 } 620 __attribute__((visibility("default"))) uint32_t plat_fx_context_create(uint64_t capacity, 621 omega_native_context **out) 622 { 623 if (!out || capacity == 0 || capacity > 4098) 624 return OMEGA_BAD_ARG; 625 if (ombr_active) 626 return OMEGA_REENTRY; 627 if (pthread_mutex_lock(&ombr_id_lock) != 0) 628 return OMEGA_BAD_RT; 629 uint64_t id = ombr_next_context; 630 if (id <= `16777215`) 631 ombr_next_context++; 632 int unlock_error = pthread_mutex_unlock(&ombr_id_lock); 633 if (unlock_error || id > `16777215`) 634 return OMEGA_BAD_RT; 635 omega_native_context *c = calloc(1, sizeof *c); 636 if (!c) 637 return OMEGA_BAD_RT; 638 c->maps = calloc((size_t)capacity + 1u, 32u); 639 if (!c->maps) { 640 free(c); 641 return OMEGA_BAD_RT; 642 } 643 if (pthread_mutex_init(&c->lock, 0) != 0) { 644 free(c->maps); 645 free(c); 646 return OMEGA_BAD_RT; 647 } 648 ombr_rt *rt = mmap(0, sizeof *rt, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 649 if (rt == MAP_FAILED) { 650 pthread_mutex_destroy(&c->lock); 651 free(c->maps); 652 free(c); 653 return OMEGA_BAD_RT; 654 } 655 *rt = ombr_rt_table; 656 rt->entry[31] = c->maps; 657 if (mprotect(rt, sizeof *rt, PROT_READ) != 0) { 658 munmap(rt, sizeof *rt); 659 pthread_mutex_destroy(&c->lock); 660 free(c->maps); 661 free(c); 662 return OMEGA_BAD_RT; 663 } 664 c->rt = rt; 665 c->maps[0] = capacity; 666 c->maps[1] = id << 26; 667 c->maps[2] = ((id + 1u) << 26) - 1u; 668 for (uint64_t i = 0; i < capacity; i++) 669 c->maps[4 + i * 4 + 2] = c->maps[1]; 670 *out = c; 671 return OMEGA_OK; 672 } 673 __attribute__((visibility("default"))) uint32_t plat_fx_context_destroy(omega_native_context *c) 674 { 675 if (!c || c == &ombr_default) 676 return OMEGA_BAD_ARG; 677 if (ombr_active) 678 return OMEGA_REENTRY; 679 /* Caller has joined/drained users and waiters before entering destruction. */ 680 if (pthread_mutex_lock(&c->lock) != 0) 681 return OMEGA_BAD_RT; 682 for (uint64_t i = 0; i < c->maps[0]; i++) { 683 if (c->maps[4 + i * 4] != 0) { 684 if (pthread_mutex_unlock(&c->lock) != 0) 685 return OMEGA_BAD_RT; 686 return OMEGA_CONTEXT_BUSY; 687 } 688 } 689 if (pthread_mutex_unlock(&c->lock) != 0 || pthread_mutex_destroy(&c->lock) != 0) 690 return OMEGA_BAD_RT; 691 int failed = munmap((void *)c->rt, sizeof *c->rt) != 0; 692 free(c->maps); 693 free(c); 694 return failed ? OMEGA_BAD_RT : OMEGA_OK; 695 } 696 static uint32_t ombr_context_lock(omega_native_context *c) 697 { 698 if (!c) 699 return OMEGA_BAD_ARG; 700 if (ombr_active) 701 return OMEGA_REENTRY; 702 ombr_active = 1; 703 if (c == &ombr_default && pthread_once(&ombr_map_once, ombr_map_init) != 0) { 704 ombr_active = 0; 705 return OMEGA_BAD_RT; 706 } 707 if (pthread_mutex_lock(&c->lock) != 0) { 708 ombr_active = 0; 709 return OMEGA_BAD_RT; 710 } 711 return OMEGA_OK; 712 } 713 static uint32_t ombr_context_unlock(omega_native_context *c, uint32_t status) 714 { 715 int failed = pthread_mutex_unlock(&c->lock); 716 ombr_active = 0; 717 return failed ? OMEGA_BAD_RT : status; 718 } 719 static uint32_t ombr_run(const void *code, const uint64_t *args, uint64_t *result, int has_value) 720 { 721 omega_native_context *context = &ombr_default; 722 uint32_t s = ombr_context_lock(context); 723 if (s != OMEGA_OK) 724 return s; 725 unsigned char *top = ombr_stack(); 726 if (!top) 727 return ombr_context_unlock(context, OMEGA_NO_STACK); 728 uint64_t v = 0; 729 s = plat_fx_ombr_enter(code, args, context->rt, &v, top); 730 if (s == OMEGA_OK) 731 *result = has_value ? v : 0u; 732 return ombr_context_unlock(context, s); 733 } 734 /* ABI v2 (docs/NATIVE-ABI.md, ABI v2): OM-2 units, bound as omega2_. An unsized view is its 735 * pointer and its element count; a layout is a pointer to its fixed bytes; an out parameter is written 736 * to a buffer the binding owns and copied to the caller only after status 0. */ 737 #include 738 typedef struct ombr2_out { unsigned slot; uint64_t n; void *dst; } ombr2_out; 739 /* A library of OM-2 units alone calls neither of the ABI v1 helpers. */ 740 __attribute__((unused)) static void ombr2_keep(void) 741 { 742 (void)ombr_overlap; 743 (void)ombr_run; 744 } 745 /* An unsized view of n elements of es bytes. n == 0 is the empty view whatever the pointer (it adds no 746 * range, so it overlaps nothing). Otherwise refused: a null pointer, a pointer not aligned to es, 747 * n * es over 64 bits, or an end past the top of the address space. */ 748 __attribute__((unused)) static int ombr2_view(ombr_span *sp, unsigned *ns, const void *p, uint64_t n, uint64_t es, int w) 749 { 750 uintptr_t lo = (uintptr_t)p; 751 if (n == 0u) 752 return 0; 753 if (!p || lo % es != 0u || n > UINT64_MAX / es || n * es > UINTPTR_MAX - lo) 754 return 1; 755 sp[*ns].lo = lo; 756 sp[*ns].hi = lo + n * es; 757 sp[*ns].w = w; 758 (*ns)++; 759 return 0; 760 } 761 /* A fixed range of nb > 0 bytes (a layout or an out parameter) aligned to es. */ 762 __attribute__((unused)) static int ombr2_fixed(ombr_span *sp, unsigned *ns, const void *p, uint64_t nb, uint64_t es, 763 int w) 764 { 765 uintptr_t lo = (uintptr_t)p; 766 if (!p || lo % es != 0u || nb > UINTPTR_MAX - lo) 767 return 1; 768 sp[*ns].lo = lo; 769 sp[*ns].hi = lo + nb; 770 sp[*ns].w = w; 771 (*ns)++; 772 return 0; 773 } 774 /* w: 0 view, 1 edit or the result, 2 out. Refused: an edit or the result overlapping any other range, 775 * or two out ranges overlapping. An out may overlap a view: it is written only after the code returns. */ 776 static int ombr2_overlap(const ombr_span *sp, unsigned ns) 777 { 778 for (unsigned i = 0; i < ns; i++) 779 for (unsigned j = i + 1; j < ns; j++) 780 if ((sp[i].w == 1 || sp[j].w == 1 || (sp[i].w == 2 && sp[j].w == 2)) 781 && sp[i].lo < sp[j].hi && sp[j].lo < sp[i].hi) 782 return 1; 783 return 0; 784 } 785 /* As ombr_run, and each out buffer is carved from the top of the thread's Omega stack (16-byte aligned, 786 * zero-filled), passed in its slot and copied to the caller with memmove only after status 0. */ 787 static uint32_t ombr2_run(omega_native_context *context, const void *code, uint64_t *args, uint64_t *result, int has_value, const ombr2_out *o, 788 unsigned no) 789 { 790 uint32_t s = ombr_context_lock(context); 791 if (s != OMEGA_OK) 792 return s; 793 unsigned char *top = ombr_stack(); 794 if (!top) 795 return ombr_context_unlock(context, OMEGA_NO_STACK); 796 unsigned char *buf[6] = {0}; 797 for (unsigned i = 0; i < no; i++) { 798 top -= (o[i].n + 15u) / 16u * 16u; 799 memset(top, 0, o[i].n); 800 buf[i] = top; 801 args[o[i].slot] = (uint64_t)(uintptr_t)top; 802 } 803 uint64_t v = 0; 804 s = plat_fx_ombr_enter(code, args, context->rt, &v, top); 805 if (s == OMEGA_OK) { 806 for (unsigned i = 0; i < no; i++) 807 memmove(o[i].dst, buf[i], o[i].n); 808 *result = has_value ? v : 0u; 809 } 810 return ombr_context_unlock(context, s); 811 } 812 __asm__(OMBR_CODE_SECTION ".balign 4\n" OMBR_LOCAL(ombr2_code_plat_effects) OMBR_HIDDEN(ombr2_code_plat_effects) OMBR_SYM(ombr2_code_plat_effects) ":\n.quad 0x32e9900000000de9,0x00025fe990000000,0x894855eb89499000,0x00000100ec8148e5,0x000001b848f0ae0f,0x8589480000000000,0xf8858b48fffffff8,0xeb8949c3c9ffffff,0x00ec8148e5894855,0xfff8838b49000001,0xfffff8858948ffff,0xfffffff0838b49ff,0x49fffffff0858948,0x8948ffffffe8838b,0x838b49ffffffe885,0xe0858948ffffffe0,0xffd8838b49ffffff,0xffffd8858948ffff,0xffffffd8858b48ff,0x000000000000b948,0x1a850fc839480000,0x000000b848000000,0x8589480000000000,0xf8858b48fffffff8,0x00b848c3c9ffffff,0x4800000000001000,0x8b48ffffffd08589,0x8d8b48ffffffd085,0x0fc83948ffffffd8,0x00b8480000001a83,0x4800000000000000,0x8b48fffffff88589,0x48c3c9fffffff885,0x00000000000000b8,0xffffffd085894800,0x000000000000b848,0xffffc88589480000,0xffffffd0858b48ff,0x48ffffffd88d8b48,0x000000de830fc839,0x48ffffffc8858b48,0x000000000f4240b9,0x00f2830fc8394800,0xffffc8858b480000,0x0000000001b948ff,0x8948c80148000000,0x858b48ffffffc885,0xc8858948ffffffc8,0xffd0858b48ffffff,0xffffe08d8b48ffff,0x00bb830fc83948ff,0xffffe8958b480000,0xffffffd08d8b48ff,0x48ca014800e1c148,0xffb885894802b60f,0xffffd0858b48ffff,0xfffffff08d8b48ff,0x00008d830fc83948,0xfffffff8958b4800,0x48ffffffd08d8b48,0x8b48ca014800e1c1,0x480288ffffffb885,0xb948ffffffd0858b,0x0000000000000001,0xffd0858948c80148,0xffffd0858b48ffff,0xffffffd0858948ff,0xae0f90ffffff0ce9,0xffffffd8858b48f0,0x0fc085483fe8c148,0x858b480000003285,0xf8858948ffffffd8,0xfff8858b48ffffff,0x000004bec3c9ffff,0x0003be1066ff4100,0x03be1066ff410000,0xbe1066ff41000000,0x1066ff4100000005,0x48e5894855eb8949,0x8b4900000100ec81,0x858948fffffff883,0xf0838b49fffffff8,0xfff0858948ffffff,0xffffe8838b49ffff,0xffffffe8858948ff,0x48ffffffe0838b49,0x8b49ffffffe08589,0x858948ffffffd883,0xd8858b48ffffffd8,0x000000b948ffffff,0xc839480000000000,0xb8480000003f850f,0x0000000000000016,0x48ffffffd0858948,0xf748ffffffd0858b,0x48000004c8800fd8,0x8b48fffffff88589,0x858948fffffff885,0xf8858b48fffffff8,0x00b848c3c9ffffff,0x4800000000001000,0x8b48ffffffd08589,0x8d8b48ffffffd085,0x0fc83948ffffffd8,0x16b8480000003f83,0x4800000000000000,0x8b48ffffffd08589,0xd8f748ffffffd085,0x89480000046a800f,0x858b48fffffff885,0xf8858948fffffff8,0xfff8858b48ffffff,0x0000b848c3c9ffff,0x8948000000000000,0x8d8b48ffffffc885,0xe0858b48ffffffd8,0x870fc13948ffffff,0x48c829480000042d,0x3948ffffffc88d8b,0x4800000423870fc1,0x4cffffff0024848d,0x0000041b820ff839,0x4800000100ec8148,0x8948fffffff8858b,0x8b48000000002484,0x848948fffffff085,0x858b480000000824,0x24848948ffffffe8,0xe0858b4800000010,0x1824848948ffffff,0xffd8858b48000000,0x002024848948ffff,0xffffd0858b480000,0x00002824848948ff,0xffffffc8858b4800,0x0000003024848948,0x48ffffffc0858b48,0x4800000038248489,0x8948ffffffb8858b,0x8b48000000402484,0x848948ffffffb085,0x858b480000004824,0x24848948ffffffa8,0xa0858b4800000050,0x5824848948ffffff,0xff98858b48000000,0x006024848948ffff,0xffff90858b480000,0x00006824848948ff,0xffffff88858b4800,0x0000007024848948,0x48ffffff80858b48,0x4800000078248489,0x8948ffffff78858b,0x8b48000000802484,0x848948ffffff7085,0x848d480000008824,0xf8394cfffffef024,0x894c000002fe820f,0x89480000009024ac,0x00000000938b4ce3,0x001fffb848d5894d,0xc521490000000000,0x000000000001bb49,0xd2820fdd394d0000,0x00f8868b49000002,0x000000808b480000,0x0000000001bb4900,0x820fd8394c000000,0x1002bb49000002ba,0x394c000000000000,0x49000002b0870fd8,0x000002b0870fc539,0x000000000001bb49,0xe5c149dd294d0000,0x000000f8868b4905,0x000000000020bb49,0xc50149d8014c0000,0x4d000000108d8b4d,0x00000281840fc985,0x0fca394d0deac149,0x858b4d0000027d85,0x0fc0854d00000000,0x8d8b4d0000027684,0x18858b4900000008,0x840fc08548000000,0x0003bb4900000268,0x394c000000000000,0x480000025e870fd8,0x394c00000008938b,0x4900000257870fca,0x0000208b8b48d129,0x024d870fc9394c00,0xbb8b48d001490000,0x30838b4800000010,0x894cc70148000000,0x00000018858b49c6,0x000000000003bb49,0x25850fd8394c0000,0x894cfe8948000002,0x8948a4f3ca8948c7,0x00000090ab8b4cd0,0x48ffffffd0858948,0x480000000024848b,0x8b48fffffff88589,0x8948000000082484,0x848b48fffffff085,0x8589480000001024,0x24848b48ffffffe8,0xe085894800000018,0x2024848b48ffffff,0xffd8858948000000,0x003024848b48ffff,0xffffc88589480000,0x00003824848b48ff,0xffffffc085894800,0x0000004024848b48,0x48ffffffb8858948,0x480000004824848b,0x8b48ffffffb08589,0x8948000000502484,0x848b48ffffffa885,0x8589480000005824,0x24848b48ffffffa0,0x9885894800000060,0x6824848b48ffffff,0xff90858948000000,0x007024848b48ffff,0xffff888589480000,0x00007824848b48ff,0xffffff8085894800,0x0000008024848b48,0x48ffffff78858948,0x480000008824848b,0x8148ffffff708589,0x858b4800000100c4,0xd0858948ffffffd0,0xffd8858b48ffffff,0x85483fe8c148ffff,0x48000000ea850fc0,0x8b48ffffffd0858b,0xc83948ffffffd88d,0x8b4800000017840f,0x858948ffffffd085,0xf8858b48fffffff8,0xf0ae0fc3c9ffffff,0x48ffffffd0858b48,0x8b48fffffff88589,0xbec3c9fffffff885,0x1066ff4100000001,0x66ff4100000001be,0xff4100000003be10,0x4100000003be1066,0x00000006be1066ff,0x000006be1066ff41,0x0003be1066ff4100,0x03be1066ff410000,0xbe1066ff41000000,0x1066ff4100000003,0x66ff4100000003be,0xff4100000003be10,0x4100000003be1066,0x00000003be1066ff,0x000003be1066ff41,0x0003be1066ff4100,0x03be1066ff410000,0xbe1066ff41000000,0x1066ff4100000003,0x66ff4100000003be,0xff4100000005be10\n.byte 0x66,0x10\n" OMBR_PREVIOUS); 813 static const char ombr2_ident_plat_effects[] = "omega-ident 1 unit=plat_effects abi=2 profile=OM-2 src=`50f1a64494248ffe9b92cafaf704fbe4ba647849b0ab9d906ea221ec941fbdcc` omc=`cf4fcbbb4752c5dd0652b426c0f2d08f82e380e8fc6f5114acf51a4c077bb579` host=mapwrite"; 814 __attribute__((visibility("default"))) const char *omega2_ident_plat_effects(void) 815 { 816 return ombr2_ident_plat_effects; 817 } 818 __asm__(OMBR_CODE_SECTION ".balign 4\n" OMBR_LOCAL(ombr2_entry_plat_fence) OMBR_HIDDEN(ombr2_entry_plat_fence) OMBR_SYM(ombr2_entry_plat_fence) ":\nendbr64\njmp " OMBR_SYM(ombr2_code_plat_effects) "+0\n" OMBR_PREVIOUS); 819 extern const unsigned char ombr2_entry_plat_fence[] __attribute__((visibility("hidden"))); 820 /* export fn plat_fence to long */ 821 __attribute__((visibility("default"))) uint32_t omega2_plat_fence(uint64_t *result) 822 { 823 ombr_span sp[7]; 824 unsigned ns = 0; 825 uint64_t a[6] = {0}; 826 ombr2_out ou[6] = {{0, 0, 0}}; 827 unsigned no = 0; 828 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 829 return OMEGA_BAD_ARG; 830 if (ombr2_overlap(sp, ns)) 831 return OMEGA_BAD_ARG; 832 return ombr2_run(&ombr_default, ombr2_entry_plat_fence, a, result, 1, ou, no); 833 } 834 /* export fn plat_fence to long */ 835 __attribute__((visibility("default"))) uint32_t omega2_ctx_plat_fence(omega_native_context *context, uint64_t *result) 836 { 837 ombr_span sp[7]; 838 unsigned ns = 0; 839 uint64_t a[6] = {0}; 840 ombr2_out ou[6] = {{0, 0, 0}}; 841 unsigned no = 0; 842 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 843 return OMEGA_BAD_ARG; 844 if (ombr2_overlap(sp, ns)) 845 return OMEGA_BAD_ARG; 846 return ombr2_run(context, ombr2_entry_plat_fence, a, result, 1, ou, no); 847 } 848 __asm__(OMBR_CODE_SECTION ".balign 4\n" OMBR_LOCAL(ombr2_entry_plat_ring_store) OMBR_HIDDEN(ombr2_entry_plat_ring_store) OMBR_SYM(ombr2_entry_plat_ring_store) ":\nendbr64\njmp " OMBR_SYM(ombr2_code_plat_effects) "+6\n" OMBR_PREVIOUS); 849 extern const unsigned char ombr2_entry_plat_ring_store[] __attribute__((visibility("hidden"))); 850 /* export fn plat_ring_store dst edit uchar * bytes view uchar * n ulong to long */ 851 __attribute__((visibility("default"))) uint32_t omega2_plat_ring_store(uint64_t *result, uint8_t *o_dst, uint64_t o_dst_len, const uint8_t *o_bytes, uint64_t o_bytes_len, uint64_t o_n) 852 { 853 ombr_span sp[7]; 854 unsigned ns = 0; 855 uint64_t a[6] = {0}; 856 ombr2_out ou[6] = {{0, 0, 0}}; 857 unsigned no = 0; 858 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 859 return OMEGA_BAD_ARG; 860 if (ombr2_view(sp, &ns, o_dst, o_dst_len, 1u, 1)) 861 return OMEGA_BAD_ARG; 862 a[0] = (uint64_t)(uintptr_t)o_dst; 863 a[1] = o_dst_len; 864 if (ombr2_view(sp, &ns, o_bytes, o_bytes_len, 1u, 0)) 865 return OMEGA_BAD_ARG; 866 a[2] = (uint64_t)(uintptr_t)o_bytes; 867 a[3] = o_bytes_len; 868 a[4] = o_n; 869 if (ombr2_overlap(sp, ns)) 870 return OMEGA_BAD_ARG; 871 return ombr2_run(&ombr_default, ombr2_entry_plat_ring_store, a, result, 1, ou, no); 872 } 873 /* export fn plat_ring_store dst edit uchar * bytes view uchar * n ulong to long */ 874 __attribute__((visibility("default"))) uint32_t omega2_ctx_plat_ring_store(omega_native_context *context, uint64_t *result, uint8_t *o_dst, uint64_t o_dst_len, const uint8_t *o_bytes, uint64_t o_bytes_len, uint64_t o_n) 875 { 876 ombr_span sp[7]; 877 unsigned ns = 0; 878 uint64_t a[6] = {0}; 879 ombr2_out ou[6] = {{0, 0, 0}}; 880 unsigned no = 0; 881 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 882 return OMEGA_BAD_ARG; 883 if (ombr2_view(sp, &ns, o_dst, o_dst_len, 1u, 1)) 884 return OMEGA_BAD_ARG; 885 a[0] = (uint64_t)(uintptr_t)o_dst; 886 a[1] = o_dst_len; 887 if (ombr2_view(sp, &ns, o_bytes, o_bytes_len, 1u, 0)) 888 return OMEGA_BAD_ARG; 889 a[2] = (uint64_t)(uintptr_t)o_bytes; 890 a[3] = o_bytes_len; 891 a[4] = o_n; 892 if (ombr2_overlap(sp, ns)) 893 return OMEGA_BAD_ARG; 894 return ombr2_run(context, ombr2_entry_plat_ring_store, a, result, 1, ou, no); 895 } 896 __asm__(OMBR_CODE_SECTION ".balign 4\n" OMBR_LOCAL(ombr2_entry_plat_map_store) OMBR_HIDDEN(ombr2_entry_plat_map_store) OMBR_SYM(ombr2_entry_plat_map_store) ":\nendbr64\njmp " OMBR_SYM(ombr2_code_plat_effects) "+12\n" OMBR_PREVIOUS); 897 extern const unsigned char ombr2_entry_plat_map_store[] __attribute__((visibility("hidden"))); 898 /* export fn plat_map_store handle long off ulong bytes view uchar * n ulong to long */ 899 __attribute__((visibility("default"))) uint32_t omega2_plat_map_store(uint64_t *result, uint64_t o_handle, uint64_t o_off, const uint8_t *o_bytes, uint64_t o_bytes_len, uint64_t o_n) 900 { 901 ombr_span sp[7]; 902 unsigned ns = 0; 903 uint64_t a[6] = {0}; 904 ombr2_out ou[6] = {{0, 0, 0}}; 905 unsigned no = 0; 906 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 907 return OMEGA_BAD_ARG; 908 a[0] = o_handle; 909 a[1] = o_off; 910 if (ombr2_view(sp, &ns, o_bytes, o_bytes_len, 1u, 0)) 911 return OMEGA_BAD_ARG; 912 a[2] = (uint64_t)(uintptr_t)o_bytes; 913 a[3] = o_bytes_len; 914 a[4] = o_n; 915 if (ombr2_overlap(sp, ns)) 916 return OMEGA_BAD_ARG; 917 return ombr2_run(&ombr_default, ombr2_entry_plat_map_store, a, result, 1, ou, no); 918 } 919 /* export fn plat_map_store handle long off ulong bytes view uchar * n ulong to long */ 920 __attribute__((visibility("default"))) uint32_t omega2_ctx_plat_map_store(omega_native_context *context, uint64_t *result, uint64_t o_handle, uint64_t o_off, const uint8_t *o_bytes, uint64_t o_bytes_len, uint64_t o_n) 921 { 922 ombr_span sp[7]; 923 unsigned ns = 0; 924 uint64_t a[6] = {0}; 925 ombr2_out ou[6] = {{0, 0, 0}}; 926 unsigned no = 0; 927 if (ombr_span_add(sp, &ns, result, 1u, 1u, 8u, 1)) 928 return OMEGA_BAD_ARG; 929 a[0] = o_handle; 930 a[1] = o_off; 931 if (ombr2_view(sp, &ns, o_bytes, o_bytes_len, 1u, 0)) 932 return OMEGA_BAD_ARG; 933 a[2] = (uint64_t)(uintptr_t)o_bytes; 934 a[3] = o_bytes_len; 935 a[4] = o_n; 936 if (ombr2_overlap(sp, ns)) 937 return OMEGA_BAD_ARG; 938 return ombr2_run(context, ombr2_entry_plat_map_store, a, result, 1, ou, no); 939 } 940 941 #else 942 #error "platform omega units require Linux AArch64 or x86-64" 943 #endif