aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / osh_op.h
| 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 | /* osh_op.h -- the AIENOS adapter's platform-operation layer for OSH (aien-architecture#158, contract: aien-protocols PR #16 |
| 18 | * OSH_PLATFORM_ABI.md sections 7 to 12, DRAFT, ABIs unfrozen). |
| 19 | * |
| 20 | * One real operation: `klog <words...>` writes one line to the kernel console ("osh_op_out: <words>"). It needs a |
| 21 | * kernel IPC-table capability (domain 1, ipc.h) on the console-log resource with the WRITE right. The shell core (lex, |
| 22 | * parse, expand) is NOT touched here: this layer only reads the request record the expander produced |
| 23 | * (PIPELINE_READY, section 7), maps it to the platform operation, and reports a status. Pure logic, no hardware: |
| 24 | * host-tested in tests/test_osh_op.c, driven in QEMU by core/osh_test.c (CK_OSH_OP_TEST). |
| 25 | * |
| 26 | * Intent and outcome records (section 10) are kept in RAM here. Omega's durable records are a separate PR; the |
| 27 | * integration point is struct osh_intent and osh_op_recover(): swap the RAM array for the durable store. */ |
| 28 | #ifndef AIENOS_CK_OSH_OP_H |
| 29 | #define AIENOS_CK_OSH_OP_H |
| 30 | |
| 31 | #include <stddef.h> |
| 32 | #include <stdint.h> |
| 33 | #include "ipc.h" |
| 34 | |
| 35 | /* This layer is a QEMU/host TEST adapter (RAM-only records, injection hooks that can revoke a capability). It is built only |
| 36 | * with CK_OSH_OP_TEST and never into a production or hardware-staging image. */ |
| 37 | #ifndef CK_OSH_OP_TEST |
| 38 | #error "osh_op is test-only: define CK_OSH_OP_TEST (make CK_OSH_TEST=1 CK_OSH_OP_TEST=1, or the host test rules)" |
| 39 | #endif |
| 40 | #ifdef CK_HARDWARE_STAGING |
| 41 | #error "CK_OSH_OP_TEST cannot be combined with CK_HARDWARE_STAGING" |
| 42 | #endif |
| 43 | |
| 44 | /* section 8.6 platform error codes: one shared numbering (osh_err.h), also used by the kernel pipe primitive (pipe.h) */ |
| 45 | #include "osh_err.h" |
| 46 | /* section 10 outcomes */ |
| 47 | enum osh_outcome { OSH_O_NOT_STARTED = 0, OSH_O_COMPLETED, OSH_O_FAILED_NO_EFFECT, OSH_O_CANCELLED, OSH_O_OUTCOME_UNKNOWN }; |
| 48 | |
| 49 | /* The one resource and operation this layer knows. The numeric ids are this adapter's own (the draft names the |
| 50 | * fields and leaves the numbering open: UNVERIFIED against a frozen contract). */ |
| 51 | #define OSH_RES_CONSOLE_LOG 0x4f534843u /* "OSHC" */ |
| 52 | #define OSH_RC_CONSOLE_LOG 1u /* binding.resource_class */ |
| 53 | #define OSH_OP_WRITE 1u /* binding.operation */ |
| 54 | |
| 55 | /* section 9 rule 2: the authority record carried by every effect */ |
| 56 | struct osh_binding { |
| 57 | uint8_t principal_id[32]; |
| 58 | uint8_t domain; /* 1 = kernel IPC table (32-bit generation) */ |
| 59 | uint32_t cap_index; |
| 60 | uint64_t cap_generation; |
| 61 | uint16_t resource_class; |
| 62 | uint16_t operation; |
| 63 | }; |
| 64 | |
| 65 | #define OSH_INTENT_MAX 16 |
| 66 | struct osh_intent { |
| 67 | uint8_t used; /* 1 once the intent is written */ |
| 68 | uint8_t closed; /* 1 once an outcome is written */ |
| 69 | uint8_t outcome; /* enum osh_outcome, valid when closed */ |
| 70 | uint8_t err; /* enum osh_err, valid when closed */ |
| 71 | uint32_t seq; /* 1-based pipeline number in the session */ |
| 72 | uint32_t status; /* shell status when COMPLETED */ |
| 73 | uint8_t digest[32]; |
| 74 | }; |
| 75 | |
| 76 | /* Test injection points (all zero in normal use). Each names the 1-based pipeline number of the session. */ |
| 77 | struct osh_hooks { |
| 78 | uint32_t revoke_before_perm_at; /* the capability is revoked after the binding is built, before the effect */ |
| 79 | uint32_t interrupt_before_effect_at; /* an interrupt arrives after the permission check, before the effect */ |
| 80 | uint32_t crash_after_effect_at; /* the adapter is lost after the effect, before the outcome is written */ |
| 81 | uint32_t sink_fail_at; /* the console write reports failure */ |
| 82 | }; |
| 83 | |
| 84 | struct osh_env { |
| 85 | void *ctx; |
| 86 | int (*out)(void *ctx, const char *line, size_t len); /* the effect: 0 = written */ |
| 87 | void (*log)(void *ctx, const char *line); /* receipts and records, one line, no newline */ |
| 88 | }; |
| 89 | |
| 90 | /* One principal per session is the current limit: `principal` is the task running the shell, `cap_holder` is the principal the |
| 91 | * console capability was granted to (recorded at grant). The binding carries `principal`; PERM_CHECK requires it to equal |
| 92 | * `cap_holder`, so a capability is only usable by the principal it was granted to. */ |
| 93 | struct osh_session { |
| 94 | struct ck_cap_table caps; |
| 95 | struct ck_handle console_cap; /* generation 0 = none held */ |
| 96 | uint8_t principal[32]; |
| 97 | uint8_t cap_holder[32]; /* zero when no capability was granted */ |
| 98 | struct osh_intent intents[OSH_INTENT_MAX]; |
| 99 | uint32_t pipelines; |
| 100 | struct osh_hooks hooks; |
| 101 | }; |
| 102 | |
| 103 | struct osh_result { |
| 104 | int status; /* shell status for $? */ |
| 105 | unsigned error; /* enum osh_err */ |
| 106 | unsigned outcome; /* enum osh_outcome */ |
| 107 | int stop; /* 1: interrupted or lost, the list must stop */ |
| 108 | int crashed; /* 1: lost after the effect, outcome open */ |
| 109 | uint32_t seq; |
| 110 | uint8_t digest[32]; |
| 111 | }; |
| 112 | |
| 113 | /* A fresh session. grant_rights = rights of the console capability inserted (0 = no capability held at all). */ |
| 114 | void osh_session_init(struct osh_session *s, unsigned grant_rights); |
| 115 | /* Task end: drops every capability the session holds. Returns 1 if the table is empty afterwards. */ |
| 116 | int osh_session_close(struct osh_session *s); |
| 117 | /* A restart: a new session that inherits only the intent records (what a durable log would give back), holding |
| 118 | * no capability. Intents without an outcome are then reported by osh_op_recover. */ |
| 119 | void osh_session_restart(struct osh_session *fresh, const struct osh_session *old); |
| 120 | /* Marks every open intent OUTCOME_UNKNOWN and logs it with its request digest. Never re-runs anything. |
| 121 | * Returns how many were open. */ |
| 122 | unsigned osh_op_recover(struct osh_session *s, const struct osh_env *env); |
| 123 | |
| 124 | /* Run one PIPELINE_READY request record. req = the REQUEST cells, out = the OUT cells (one byte per cell, n_out cells). */ |
| 125 | void osh_op_exec(struct osh_session *s, const struct osh_env *env, const uint64_t *req, const uint64_t *out, size_t n_out, |
| 126 | struct osh_result *r); |
| 127 | |
| 128 | /* section 9 PERM_CHECK, exposed for the tests: OSH_E_OK or the denial reason */ |
| 129 | unsigned osh_op_perm_check(const struct osh_session *s, const struct osh_binding *b); |
| 130 | |
| 131 | const char *osh_err_name(unsigned e); |
| 132 | const char *osh_outcome_name(unsigned o); |
| 133 | |
| 134 | #endif |