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

GitHub aien-dev/aienux#109 (code only)

closed derived ce70c0463a3e07fd #109

Imported from GitHub aien-dev/aienux#109 (code only)

GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed).

Base d4a6844016aad3989a9c706c1fcd49d3afd48790 · Head 7bcb5c3b328842703572adc90d30cb034a35ade6

Apply it

git checkout -b patch-ce70c0463a3e07fd d4a6844016aad3989a9c706c1fcd49d3afd48790
curl -fsSL https://interplane.io/drakestapleton/aienux/patches/ce70c0463a3e07fd.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/aienux
109
7bcb5c3b328842703572adc90d30cb034a35ade6
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":109,"url":"https://github.com/aien-dev/aienux/pull/109","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:35:44Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/crates/aienos-boot/src/handoff.rs b/crates/aienos-boot/src/handoff.rs
index be4a1915..963805c2 100644
--- a/crates/aienos-boot/src/handoff.rs
+++ b/crates/aienos-boot/src/handoff.rs
@@ -33,12 +33,12 @@ use aienos_kernel::display::{FramebufferInfo, PixelOrder, Screen, ACCENT, FOREGR
 use aienos_kernel::fatal::{self, FaultInfo};
 use aienos_kernel::mem::frame_allocator::PhysAddr;
 use aienos_kernel::mem::map_plan::{self, AddressRange, EfiMemoryDescriptor};
-use aienos_kernel::mem::pagetable::{FixedFramePool, MapFlags, PageTableBuilder, TableMemory};
+use aienos_kernel::mem::pagetable::{FixedFramePool, PageTableBuilder, TableMemory};
 use aienos_kernel::report::ReportBuf;
 use aienos_kernel::sync::spinlock::SpinLock;
 use core::alloc::{GlobalAlloc, Layout};
 use core::fmt::Write;
-use core::sync::atomic::{AtomicBool, AtomicU64, AtomicU8, AtomicUsize, Ordering};
+use core::sync::atomic::{AtomicBool, AtomicU64, AtomicU8, Ordering};
 use uefi::boot::{OpenProtocolAttributes, OpenProtocolParams};
 use uefi::mem::memory_map::{MemoryMap, MemoryType};
 use uefi::prelude::*;
diff --git a/crates/aienos-kernel/src/abi.rs b/crates/aienos-kernel/src/abi.rs
index 4b8b0873..4e3372cb 100644
--- a/crates/aienos-kernel/src/abi.rs
+++ b/crates/aienos-kernel/src/abi.rs
@@ -137,8 +137,8 @@ macro_rules! wire_impl {
 #[repr(C)]
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
 pub struct Handle {
-    index: u32,
-    generation: u32,
+    pub index: u32,
+    pub generation: u32,
 }
 
 impl Handle {
@@ -185,21 +185,7 @@ impl Handle {
 
 wire_impl!(Handle, EnvelopeTag::Handle, 8);
 
-impl TryFrom<caps::Handle> for Handle {
-    type Error = AbiError;
-    fn try_from(handle: caps::Handle) -> Result<Self, AbiError> {
-        Self::new(handle.index, handle.generation)
-    }
-}
-
-impl From<Handle> for caps::Handle {
-    fn from(handle: Handle) -> Self {
-        caps::Handle {
-            index: handle.index,
-            generation: handle.generation,
-        }
-    }
-}
+// caps::Handle is a direct re-export of crate::abi::Handle
 
 // ---------------------------------------------------------------------------
 // Plain identifiers
@@ -456,15 +442,20 @@ wire_impl!(Capability, EnvelopeTag::Capability, 16);
 /// A bounded memory extent in pages. Wire: base u64 LE, pages u32 LE,
 /// reserved u32 (zero); 16 bytes.
 #[repr(C)]
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
 pub struct MemoryRegion {
-    base: u64,
-    pages: u32,
-    reserved: u32,
+    pub base: u64,
+    pub pages: u32,
+    pub reserved: u32,
 }
 
 impl MemoryRegion {
     pub const SIZE: usize = 16;
+    pub const EMPTY: Self = Self {
+        base: 0,
+        pages: 0,
+        reserved: 0,
+    };
 
     pub const fn new(base: u64, pages: u32) -> Self {
         Self {
@@ -509,15 +500,22 @@ wire_impl!(MemoryRegion, EnvelopeTag::MemoryRegion, 16);
 #[repr(C)]
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
 pub struct Message {
-    kind: u32,
-    reserved: u32,
-    payload: [u64; 3],
-    region: MemoryRegion,
-    object: ObjectId,
+    pub kind: u32,
+    pub reserved: u32,
+    pub payload: [u64; 3],
+    pub region: MemoryRegion,
+    pub object: ObjectId,
 }
 
 impl Message {
     pub const SIZE: usize = 56;
+    pub const NEW: Self = Self {
+        kind: 0,
+        reserved: 0,
+        payload: [0; 3],
+        region: MemoryRegion::EMPTY,
+        object: ObjectId(0),
+    };
 
     pub const fn new(kind: u32, payload: [u64; 3], region: MemoryRegion, object: ObjectId) -> Self {
         Self {
@@ -784,20 +782,23 @@ mod tests {
             decode_enveloped::<Handle>(&env),
             Err(AbiError::InvalidHandle)
         );
-        let forged = caps::Handle {
+        let forged = Handle {
             index: 0,
             generation: 0,
         };
-        assert_eq!(Handle::try_from(forged), Err(AbiError::InvalidHandle));
+        assert_eq!(
+            Handle::from_raw(forged.to_raw()),
+            Err(AbiError::InvalidHandle)
+        );
     }
 
     #[test]
-    fn handle_converts_with_caps_handle() {
+    fn handle_raw_register_encoding() {
         let mut table = caps::CapTable::<4>::new(1);
         let issued = table.insert(9, caps::Rights::READ).unwrap();
-        let wire = Handle::try_from(issued).unwrap();
-        assert_eq!(wire.generation(), issued.generation);
-        assert_eq!(caps::Handle::from(wire), issued);
+        let raw = issued.to_raw();
+        assert_eq!(Handle::from_raw(raw), Ok(issued));
+        assert_eq!(Handle::from_raw(0), Err(AbiError::InvalidHandle));
     }
 
     #[test]
diff --git a/crates/aienos-kernel/src/caps.rs b/crates/aienos-kernel/src/caps.rs
index f818dbc6..44a44216 100644
--- a/crates/aienos-kernel/src/caps.rs
+++ b/crates/aienos-kernel/src/caps.rs
@@ -1,10 +1,6 @@
 //! Fixed-capacity capability handles with generation checks and revocation.
 
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct Handle {
-    pub index: u32,
-    pub generation: u32,
-}
+pub use crate::abi::Handle;
 
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
 pub struct Rights(u8);
diff --git a/crates/aienos-kernel/src/ipc.rs b/crates/aienos-kernel/src/ipc.rs
index 7cd71a13..04da07db 100644
--- a/crates/aienos-kernel/src/ipc.rs
+++ b/crates/aienos-kernel/src/ipc.rs
@@ -11,55 +11,7 @@
 use crate::caps::{CapError, CapTable, Handle, Rights};
 use core::sync::atomic::{AtomicU32, Ordering};
 
-/// Identity of an isolated principal (a task with its own capability table).
-#[repr(C)]
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct TaskId(pub u32);
-
-/// Identity of a kernel object reachable through a capability handle.
-#[repr(C)]
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct ObjectId(pub u64);
-
-/// Identity of a kernel-owned message channel.
-#[repr(C)]
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct ChannelId(pub u32);
-
-/// A bounded memory extent carried by reference inside a message.
-#[repr(C)]
-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
-pub struct MemoryRegion {
-    pub base: u64,
-    pub pages: u32,
-    pub _pad: u32,
-}
-
-/// Fixed-size typed message. `repr(C)` and allocator-free so it can cross the
-/// EL0 boundary in registers and sit in a static ring.
-#[repr(C)]
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct Message {
-    pub kind: u32,
-    pub _pad: u32,
-    pub payload: [u64; 3],
-    pub region: MemoryRegion,
-    pub object: ObjectId,
-}
-
-impl Message {
-    pub const NEW: Self = Self {
-        kind: 0,
-        _pad: 0,
-        payload: [0; 3],
-        region: MemoryRegion {
-            base: 0,
-            pages: 0,
-            _pad: 0,
-        },
-        object: ObjectId(0),
-    };
-}
+pub use crate::abi::{ChannelId, MemoryRegion, Message, ObjectId, TaskId};
 
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
 pub enum ChannelError {
@@ -449,17 +401,12 @@ mod tests {
     }
 
     fn message(kind: u32) -> Message {
-        Message {
+        Message::new(
             kind,
-            _pad: 0,
-            payload: [kind as u64, 0x22, 0x33],
-            region: MemoryRegion {
-                base: 0x1000,
-                pages: 2,
-                _pad: 0,
-            },
-            object: ObjectId(0x0a),
-        }
+            [kind as u64, 0x22, 0x33],
+            MemoryRegion::new(0x1000, 2),
+            ObjectId(0x0a),
+        )
     }
 
     #[test]
diff --git a/crates/aienos-kernel/src/user.rs b/crates/aienos-kernel/src/user.rs
index b6028073..dd7da9ef 100644
--- a/crates/aienos-kernel/src/user.rs
+++ b/crates/aienos-kernel/src/user.rs
@@ -130,14 +130,7 @@ pub fn set_write_hook(hook: WriteHook) {
 }
 
 const fn pack_handle(handle: Handle) -> u64 {
-    ((handle.generation as u64) << 32) | handle.index as u64
-}
-
-const fn unpack_handle(raw: u64) -> Handle {
-    Handle {
-        index: raw as u32,
-        generation: (raw >> 32) as u32,
-    }
+    handle.to_raw()
 }
 
 fn user_bytes(address: u64, length: usize) -> Option<&'static [u8]> {
@@ -158,8 +151,15 @@ fn user_bytes(address: u64, length: usize) -> Option<&'static [u8]> {
 
 fn dispatch_write(frame: &mut TrapFrame) {
     let raw = frame.x[0];
-    let allowed =
-        unsafe { USER_CAPS.lookup(unpack_handle(raw), Rights::WRITE) == Ok(CONSOLE_RESOURCE) };
+    let handle = match Handle::from_raw(raw) {
+        Ok(h) => h,
+        Err(_) => {
+            FORGED_DENIED.store(true, Ordering::Release);
+            frame.x[0] = SYSCALL_DENIED;
+            return;
+        }
+    };
+    let allowed = unsafe { USER_CAPS.lookup(handle, Rights::WRITE) == Ok(CONSOLE_RESOURCE) };
     let bytes = usize::try_from(frame.x[2])
         .ok()
         .and_then(|length| user_bytes(frame.x[1], length));
@@ -234,19 +234,20 @@ pub unsafe extern "C" fn aienos_lower_el_sync_dispatcher(frame: *mut TrapFrame)
 fn dispatch_ipc(frame: &mut TrapFrame) {
     match frame.x[8] {
         SYS_CHANNEL_SEND => {
-            let message = crate::ipc::Message {
-                kind: frame.x[1] as u32,
-                _pad: 0,
-                payload: [frame.x[2], frame.x[3], frame.x[4]],
-                region: crate::ipc::MemoryRegion {
-                    base: frame.x[5],
-                    pages: frame.x[6] as u32,
-                    _pad: 0,
-                },
-                object: crate::ipc::ObjectId(frame.x[7]),
+            let handle = match Handle::from_raw(frame.x[0]) {
+                Ok(h) => h,
+                Err(_) => {
+                    frame.x[0] = SYSCALL_DENIED;
+                    return;
+                }
             };
-            let result =
-                unsafe { crate::ipc::channel_send_active(unpack_handle(frame.x[0]), message) };
+            let message = crate::abi::Message::new(
+                frame.x[1] as u32,
+                [frame.x[2], frame.x[3], frame.x[4]],
+                crate::abi::MemoryRegion::new(frame.x[5], frame.x[6] as u32),
+                crate::abi::ObjectId(frame.x[7]),
+            );
+            let result = unsafe { crate::ipc::channel_send_active(handle, message) };
             match result {
                 Ok(()) => {
                     IPC_SENT_KIND.store(frame.x[1], Ordering::Release);
@@ -259,7 +260,14 @@ fn dispatch_ipc(frame: &mut TrapFrame) {
             }
         }
         SYS_CHANNEL_RECV => {
-            match unsafe { crate::ipc::channel_receive_active(unpack_handle(frame.x[0])) } {
+            let handle = match Handle::from_raw(frame.x[0]) {
+                Ok(h) => h,
+                Err(_) => {
+                    frame.x[0] = SYSCALL_DENIED;
+                    return;
+                }
+            };
+            match unsafe { crate::ipc::channel_receive_active(handle) } {
                 Ok(message) => {
                     let delivered = message.kind as u64 == IPC_SENT_KIND.load(Ordering::Acquire)
                         && message.payload[0] == IPC_SENT_P0.load(Ordering::Acquire)
@@ -278,37 +286,58 @@ fn dispatch_ipc(frame: &mut TrapFrame) {
                 Err(_) => frame.x[0] = SYSCALL_DENIED,
             }
         }
-        SYS_CAP_DELEGATE => match Rights::from_bits(frame.x[2] as u8) {
-            Some(rights) => {
-                match unsafe {
-                    crate::ipc::delegate_from_active(
-                        unpack_handle(frame.x[0]),
-                        frame.x[1] as u32,
-                        rights,
-                    )
-                } {
-                    Ok(child) => {
-                        IPC_CAP_DELEGATED.store(true, Ordering::Release);
-                        IPC_CHILD_HANDLE.store(pack_handle(child), Ordering::Release);
-                        frame.x[0] = 0;
-                    }
-                    Err(_) => frame.x[0] = SYSCALL_DENIED,
+        SYS_CAP_DELEGATE => {
+            let handle = match Handle::from_raw(frame.x[0]) {
+                Ok(h) => h,
+                Err(_) => {
+                    frame.x[0] = SYSCALL_DENIED;
+                    return;
                 }
-            }
-            None => frame.x[0] = SYSCALL_DENIED,
-        },
-        SYS_OBJECT_READ => {
+            };
+            let wire_rights = match crate::abi::Rights::from_bits(frame.x[2] as u32) {
+                Ok(r) => r,
+                Err(_) => {
+                    frame.x[0] = SYSCALL_DENIED;
+                    return;
+                }
+            };
+            let caps_rights: Rights = match wire_rights.try_into() {
+                Ok(r) => r,
+                Err(_) => {
+                    frame.x[0] = SYSCALL_DENIED;
+                    return;
+                }
+            };
             match unsafe {
-                crate::ipc::object_read_active(unpack_handle(frame.x[0]), frame.x[1] as usize)
+                crate::ipc::delegate_from_active(handle, frame.x[1] as u32, caps_rights)
             } {
+                Ok(child) => {
+                    IPC_CAP_DELEGATED.store(true, Ordering::Release);
+                    IPC_CHILD_HANDLE.store(child.to_raw(), Ordering::Release);
+                    frame.x[0] = 0;
+                }
+                Err(_) => frame.x[0] = SYSCALL_DENIED,
+            }
+        }
+        SYS_OBJECT_READ => {
+            let handle = match Handle::from_raw(frame.x[0]) {
+                Ok(h) => h,
+                Err(_) => {
+                    if IPC_PHASE.load(Ordering::Acquire) & 0xff != 0 {
+                        IPC_REVOKED_DENIED.store(true, Ordering::Release);
+                    } else {
+                        IPC_FORGED_DENIED.store(true, Ordering::Release);
+                    }
+                    frame.x[0] = SYSCALL_DENIED;
+                    return;
+                }
+            };
+            match unsafe { crate::ipc::object_read_active(handle, frame.x[1] as usize) } {
                 Ok(value) => frame.x[0] = value,
                 Err(crate::ipc::IpcError::InvalidHandle) => {
                     if IPC_PHASE.load(Ordering::Acquire) & 0xff != 0 {
-                        // Post-revocation retry: the ancestor revocation reached
-                        // this derived handle.
                         IPC_REVOKED_DENIED.store(true, Ordering::Release);
                     } else {
-                        // Deliberately corrupted handle.
                         IPC_FORGED_DENIED.store(true, Ordering::Release);
                     }
                     frame.x[0] = SYSCALL_DENIED;
@@ -317,12 +346,15 @@ fn dispatch_ipc(frame: &mut TrapFrame) {
             }
         }
         SYS_OBJECT_WRITE => {
+            let handle = match Handle::from_raw(frame.x[0]) {
+                Ok(h) => h,
+                Err(_) => {
+                    frame.x[0] = SYSCALL_DENIED;
+                    return;
+                }
+            };
             match unsafe {
-                crate::ipc::object_write_active(
-                    unpack_handle(frame.x[0]),
-                    frame.x[1] as usize,
-                    frame.x[2],
-                )
+                crate::ipc::object_write_active(handle, frame.x[1] as usize, frame.x[2])
             } {
                 Ok(()) => frame.x[0] = 0,
                 Err(crate::ipc::IpcError::MissingRights) => {
@@ -971,11 +1003,8 @@ mod tests {
 
     #[test]
     fn capability_handle_round_trips() {
-        let handle = Handle {
-            index: 7,
-            generation: 19,
-        };
-        assert_eq!(unpack_handle(pack_handle(handle)), handle);
+        let handle = Handle::new(7, 19).unwrap();
+        assert_eq!(Handle::from_raw(handle.to_raw()), Ok(handle));
     }
 
     #[test]