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/artifact/osc_launch.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 / artifact / osc_launch.c` 188 lines ยท 6094 bytes Raw Blame History 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 /* osc_launch.c -- see osc_launch.h. Pure. */ 18 #include "osc_launch.h" 19 20 #define MAXR 6u 21 22 static int is_slice(unsigned k) { return k == 11 || k == 12; } 23 24 static int fits(unsigned k, uint64_t v) 25 { 26 switch (k) { 27 case 1: return v <= 1; 28 case 2: return v <= 0xff; 29 case 3: return v <= 0xffff; 30 case 4: return v <= 0xffffffffull; 31 case 6: return (int64_t)v == (int8_t)v; 32 case 7: return (int64_t)v == (int16_t)v; 33 case 8: return (int64_t)v == (int32_t)v; 34 default: return 1; /* 5, 9 and the two registers of a slice */ 35 } 36 } 37 38 int osc_launch_args_ok(const uint8_t *kinds, unsigned nregs, const uint64_t *args, unsigned nargs) 39 { 40 if (nregs > MAXR || nargs != nregs) 41 return 0; 42 for (unsigned p = 0; p < nregs; p++) 43 if (!is_slice(kinds[p]) && !fits(kinds[p], args[p])) 44 return 0; 45 for (unsigned p = 0; p + 1 < nregs; p++) { 46 if (!is_slice(kinds[p])) 47 continue; 48 uint64_t ptr = args[p], n = args[p + 1], sz = n * (kinds[p] == 12 ? 8u : 1u); 49 if (kinds[p] == 12 && (n > UINT64_MAX / 8 || (n && (ptr & 7)))) 50 return 0; 51 if ((n && !ptr) || sz > UINT64_MAX - ptr) 52 return 0; 53 for (unsigned q = 0; q + 1 < nregs; q++) { 54 if (q == p || !is_slice(kinds[q])) 55 continue; 56 if (kinds[p] != 12 && kinds[q] != 12) 57 continue; 58 uint64_t qn = args[q + 1], qsz = qn * (kinds[q] == 12 ? 8u : 1u); 59 if (!sz || !qn || (kinds[q] == 12 && qn > UINT64_MAX / 8) || qsz > UINT64_MAX - args[q]) 60 continue; 61 if (ptr < args[q] + qsz && args[q] < ptr + sz) 62 return 0; 63 } 64 } 65 return 1; 66 } 67 68 static int inside(uint64_t ptr, uint64_t size, uint64_t base, uint64_t len) 69 { 70 return ptr >= base && ptr - base <= len && size <= len - (ptr - base); 71 } 72 73 int osc_launch_ranges_owned(const uint8_t *kinds, unsigned nregs, const uint64_t *args, uint64_t in_va, 74 uint64_t in_len, uint64_t ws_va, uint64_t ws_len) 75 { 76 for (unsigned p = 0; p + 1 < nregs; p++) { 77 if (!is_slice(kinds[p])) 78 continue; 79 uint64_t ptr = args[p], n = args[p + 1]; 80 if (n == 0) 81 continue; /* a zero-length range is never dereferenced */ 82 if (kinds[p] == 12) { 83 if (n > UINT64_MAX / 8 || !inside(ptr, n * 8, ws_va, ws_len)) 84 return 0; 85 } else if (!inside(ptr, n, in_va, in_len)) { 86 return 0; 87 } 88 } 89 return 1; 90 } 91 92 uint64_t osc_launch_budget(uint64_t unit_ticks, uint64_t caller_cap) 93 { 94 return caller_cap && caller_cap < unit_ticks ? caller_cap : unit_ticks; 95 } 96 97 int osc_launch_budget_expired(uint64_t seen, uint64_t budget) 98 { 99 return seen >= budget; 100 } 101 102 static int fault_class(uint64_t ec) 103 { 104 switch (ec) { 105 case 0x00: /* unknown / undefined instruction */ 106 case 0x07: /* FP or SIMD access */ 107 case 0x0e: /* illegal execution state */ 108 case 0x18: /* MSR, MRS or system instruction */ 109 case 0x20: /* instruction abort from EL0 */ 110 case 0x22: /* PC alignment */ 111 case 0x24: /* data abort from EL0 */ 112 case 0x26: /* SP alignment */ 113 case 0x3c: /* brk */ 114 return 1; 115 default: 116 return 0; 117 } 118 } 119 120 static void unknown(struct osc_result *r, uint8_t why) 121 { 122 r->cls = OSC_RES_UNKNOWN; 123 r->unknown_reason = why; 124 } 125 126 void osc_launch_classify(const struct osc_event *e, uint64_t rt_code_va, struct osc_result *r) 127 { 128 r->cls = 0; 129 r->trap_code = r->unknown_reason = r->refused_code = 0; 130 r->value = 0; 131 switch (e->kind) { 132 case OSC_EV_SVC: { 133 uint64_t imm = e->esr & 0xffff, ec = (e->esr >> 26) & 0x3f; 134 uint64_t site = e->elr - 4; 135 uint64_t off = site - rt_code_va; 136 if (ec != 0x15 || e->elr < 4 || site < rt_code_va || off >= (uint64_t)OSC_RT_STUBS * OSC_RT_STUB_BYTES || 137 off % OSC_RT_STUB_BYTES || imm != off / OSC_RT_STUB_BYTES) { 138 unknown(r, OSC_UNK_FAULT); /* an SVC outside the runtime entry stubs is refused (9.1.1 item 1) */ 139 return; 140 } 141 if (imm == OSC_SVC_RETURN) { 142 r->cls = OSC_RES_RETURNED; 143 r->value = e->x0; 144 } else if (imm == OSC_SVC_TRAP) { 145 if (e->x1 >= 1 && e->x1 <= OSC_TRAP_MAX) { 146 r->cls = OSC_RES_TRAPPED; 147 r->trap_code = (uint8_t)e->x1; 148 } else { 149 unknown(r, OSC_UNK_TRAP_CODE_UNKNOWN); 150 } 151 } else { 152 unknown(r, OSC_UNK_OTHER); /* alloc, arenas, pools: not provided by this cut */ 153 } 154 return; 155 } 156 case OSC_EV_SYNC: 157 unknown(r, fault_class((e->esr >> 26) & 0x3f) ? OSC_UNK_FAULT : OSC_UNK_OTHER); 158 return; 159 case OSC_EV_BUDGET: 160 unknown(r, OSC_UNK_TICK_OVERRUN); 161 return; 162 case OSC_EV_LOST: 163 unknown(r, OSC_UNK_LAUNCH_LOST); 164 return; 165 default: 166 unknown(r, OSC_UNK_OTHER); 167 return; 168 } 169 } 170 171 int osc_launch_ws_pages_ok(uint64_t pages) 172 { 173 return pages <= OSC_WS_MAX_PAGES; 174 } 175 176 int osc_launch_stack_ok(uint64_t declared, uint64_t hard_max) 177 { 178 return declared <= hard_max; 179 } 180 181 int osc_launch_disjoint(uint64_t a, uint64_t alen, uint64_t b, uint64_t blen) 182 { 183 if (alen == 0 || blen == 0) 184 return 1; 185 if (a + alen < a || b + blen < b) /* a wrapped range is never disjoint from anything */ 186 return 0; 187 return a + alen <= b || b + blen <= a; 188 }