interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / artifact / osc_launch.h

105 lines · 6016 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.h -- the pure parts of launching an admitted OSC unit (OSC_UNIT_ARTIFACT.md section 9):
18 * the argument rule (9.2), range ownership (9.1.1 item 4), the ticks budget, and the classification
19 * of what an EL0 unit did into the four result classes (9.3). No globals, no I/O, no MMU: host tested
20 * (tests/test_osc_launch.c) and used by core/osc_launch.c, which maps and runs the task.
21 * QEMU only so far; nothing here is physical. */
22#ifndef AIENOS_CK_OSC_LAUNCH_H
23#define AIENOS_CK_OSC_LAUNCH_H
24#include <stddef.h>
25#include <stdint.h>
26
27/* The OscRt vtable of runtime_abi_version 1 has 13 entries at offsets 0..96 (osc_rt.h); entry i is
28 * reached through the runtime stub i. Stub 2 is `trap` (offset 16). Stub 13 is not in the vtable: it is
29 * the return address the launcher hands the entry function in x30. */
30#define OSC_RT_SERVICES 13u
31#define OSC_SVC_TRAP 2u
32#define OSC_SVC_RETURN 13u
33#define OSC_RT_STUBS 14u
34#define OSC_RT_STUB_BYTES 8u /* `svc #i` then a spare word */
35#define OSC_TRAP_MAX 14u
36
37/* Kernel cap on a caller-owned workspace: 32 pages = 128 KiB. It bounds the user pages and page-table slots one
38 * task can map (the workspace window is 32 of the 512 leaf entries of its L3 table, clear of the stack), and the
39 * bss a caller must set aside. Large enough for the OSH resumable workspace (11,456 cells = 23 pages). The size is
40 * the caller's choice and never comes from the container. */
41#define OSC_WS_MAX_PAGES 32u
42
43/* Section 9.3: four classes, closed. */
44enum { OSC_RES_RETURNED = 1, OSC_RES_TRAPPED = 2, OSC_RES_REFUSED = 3, OSC_RES_UNKNOWN = 4 };
45/* unknown_reason, section 9.3 (diagnostic only; never changes the class). */
46enum { OSC_UNK_FAULT = 1, OSC_UNK_TICK_OVERRUN = 2, OSC_UNK_LAUNCH_LOST = 3, OSC_UNK_TRAP_CODE_UNKNOWN = 4,
47 OSC_UNK_OTHER = 255 };
48
49struct osc_result {
50 uint8_t cls; /* OSC_RES_* */
51 uint8_t trap_code; /* TRAPPED: 1..14 */
52 uint8_t unknown_reason; /* OUTCOME_UNKNOWN: OSC_UNK_*; 0 for every other class */
53 uint8_t refused_code; /* REFUSED_AT_ADMISSION: section 8.3 code (40, 41, 30...) */
54 uint64_t value; /* RETURNED: x0 */
55 uint64_t ticks; /* scheduler ticks the unit ran (diagnostic) */
56};
57
58/* Section 9.2 items 1 to 4, exactly the rule of tools/osc-launch-check.c. kinds are the entry
59 * record's reg_kind[0..nregs); args are the register values in order; nargs must equal nregs.
60 * 1 = acceptable, 0 = LAUNCH_ARG_SHAPE (41). */
61int osc_launch_args_ok(const uint8_t *kinds, unsigned nregs, const uint64_t *args, unsigned nargs);
62
63/* Section 9.1.1 item 4 (ownership, after the shape check passes): every non-empty `bytes` range lies
64 * wholly in the read-only input window [in_va, in_va+in_len), every non-empty `cells` range wholly in the
65 * caller's writable workspace window [ws_va, ws_va+ws_len). 1 = owned. */
66int osc_launch_ranges_owned(const uint8_t *kinds, unsigned nregs, const uint64_t *args, uint64_t in_va,
67 uint64_t in_len, uint64_t ws_va, uint64_t ws_len);
68
69/* 1 if the byte ranges [a, a+alen) and [b, b+blen) do not overlap (an empty range overlaps nothing; a
70 * range that wraps the address space overlaps everything, so 0). */
71int osc_launch_disjoint(uint64_t a, uint64_t alen, uint64_t b, uint64_t blen);
72
73/* 1 if a signer-declared max_stack_bytes is within the launcher hard maximum (else LIMIT_EXCEEDED, 14). */
74int osc_launch_stack_ok(uint64_t declared, uint64_t hard_max);
75
76/* 1 if a workspace of this many pages is allowed (0 = none, up to OSC_WS_MAX_PAGES). */
77int osc_launch_ws_pages_ok(uint64_t pages);
78
79/* The ticks the task may run: the unit's declared cpu_ticks, lowered (never raised) by a nonzero caller
80 * cap. One tick is one scheduler timer interrupt (10 ms, CNTFRQ/100). */
81uint64_t osc_launch_budget(uint64_t unit_ticks, uint64_t caller_cap);
82/* 1 once `seen` ticks have been delivered while the unit ran and that reaches the budget. */
83int osc_launch_budget_expired(uint64_t seen, uint64_t budget);
84
85/* What the kernel observed while the unit ran. */
86enum { OSC_EV_SVC = 1, OSC_EV_SYNC = 2, OSC_EV_BUDGET = 3, OSC_EV_LOST = 4 };
87struct osc_event {
88 unsigned kind; /* OSC_EV_* */
89 uint64_t esr, elr; /* ESR_EL1 and ELR_EL1 of the exception (SVC and SYNC) */
90 uint64_t x0, x1; /* unit registers at the exception */
91};
92/* Section 9.3 and 9.1.1: classify one terminating event into *r (cls, trap_code, unknown_reason, value).
93 * rt_code_va is the unit-space address of runtime stub 0.
94 * - SVC from a runtime stub (elr-4 is stub i and the immediate is i): return stub -> RETURNED(x0);
95 * trap stub with x1 in 1..14 -> TRAPPED(x1); with another x1 -> UNKNOWN(TRAP_CODE_UNKNOWN); any
96 * other stub (a runtime service this cut does not provide) -> UNKNOWN(OTHER).
97 * - SVC from anywhere else: refused, UNKNOWN(FAULT). The unit has no system calls (9.1.1 item 1).
98 * - SYNC with a CPU fault class (data or instruction abort, brk, undefined, alignment, FP, system
99 * register) -> UNKNOWN(FAULT). A brk reached directly is a fault, never TRAPPED (8.4).
100 * - SYNC with any other class -> UNKNOWN(OTHER); BUDGET -> UNKNOWN(TICK_OVERRUN);
101 * LOST -> UNKNOWN(LAUNCH_LOST).
102 * Nothing is ever mapped to RETURNED except the return stub. */
103void osc_launch_classify(const struct osc_event *e, uint64_t rt_code_va, struct osc_result *r);
104
105#endif