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/crates/aienos-kernel/src/user.rs # 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 / crates / aienos-kernel / src / user.rs` 1065 lines ยท 35601 bytes Raw Blame History 1 #![allow(static_mut_refs)] 2 // SPDX-License-Identifier: Apache-2.0 3 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 //! Single-core EL0 task entry and capability-checked syscall substrate. 18 19 use crate::caps::{CapTable, Handle, Rights}; 20 /// Complete general-purpose and floating-point state captured by the lower-EL 21 /// synchronous trampoline in this file. The assembly hard-codes the field 22 /// offsets, so `trap_frame_layout_matches_assembly` fails the build if this 23 /// layout ever drifts from the exception ABI. 24 #[repr(C, align(16))] 25 #[derive(Clone, Copy, Default)] 26 pub struct TrapFrame { 27 pub x: [u64; 31], 28 pub _pad: u64, 29 pub elr_el1: u64, 30 pub spsr_el1: u64, 31 pub fpcr: u64, 32 pub fpsr: u64, 33 pub q: [u128; 32], 34 } 35 use core::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize, Ordering}; 36 37 pub const SYS_WRITE: u64 = 1; 38 pub const SYS_EXIT: u64 = 2; 39 pub const CONSOLE_RESOURCE: u32 = 1; 40 pub const SYSCALL_DENIED: u64 = u64::MAX; 41 pub const SYS_CHANNEL_SEND: u64 = 3; 42 pub const SYS_CHANNEL_RECV: u64 = 4; 43 pub const SYS_CAP_DELEGATE: u64 = 5; 44 pub const SYS_OBJECT_READ: u64 = 6; 45 pub const SYS_OBJECT_WRITE: u64 = 7; 46 47 const PAGE_BYTES: usize = 4096; 48 const TABLE_DESCRIPTOR: u64 = 0b11; 49 const ADDRESS_MASK: u64 = 0x0000_ffff_ffff_f000; 50 const PAGE_DESCRIPTOR: u64 = 0b11; 51 const AF: u64 = 1 << 10; 52 const NG: u64 = 1 << 11; 53 const PXN: u64 = 1 << 53; 54 const UXN: u64 = 1 << 54; 55 56 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] 57 pub struct DemoResult { 58 pub write_granted: bool, 59 pub forged_denied: bool, 60 pub fault_contained: bool, 61 pub exit_code: u64, 62 } 63 64 #[repr(C, align(4096))] 65 struct Page([u64; 512]); 66 67 static mut USER_L0: Page = Page([0; 512]); 68 static mut USER_L1: Page = Page([0; 512]); 69 static mut USER_L2: Page = Page([0; 512]); 70 static mut USER_L3: Page = Page([0; 512]); 71 static mut USER_CODE: Page = Page([0; 512]); 72 static mut USER_STACK: Page = Page([0; 512]); 73 static mut USER_CAPS: CapTable<4> = CapTable::new(0x454c_3001); 74 75 static USER_WRITE_HOOK: AtomicUsize = AtomicUsize::new(0); 76 static USER_ACTIVE: AtomicBool = AtomicBool::new(false); 77 static WRITE_GRANTED: AtomicBool = AtomicBool::new(false); 78 static FORGED_DENIED: AtomicBool = AtomicBool::new(false); 79 static FAULT_CONTAINED: AtomicBool = AtomicBool::new(false); 80 static EXIT_CODE: AtomicU64 = AtomicU64::new(u64::MAX); 81 static AUTHORIZED_HANDLE: AtomicU64 = AtomicU64::new(0); 82 static USER_VA_BASE: AtomicU64 = AtomicU64::new(0); 83 // --- typed IPC proof state (PR #30): kernel-owned, dispatcher-consumed ----- 84 static IPC_ACTIVE: AtomicBool = AtomicBool::new(false); 85 static IPC_PHASE: AtomicU32 = AtomicU32::new(0); 86 static IPC_EXIT: AtomicU64 = AtomicU64::new(u64::MAX); 87 static IPC_MESSAGE_DELIVERED: AtomicBool = AtomicBool::new(false); 88 static IPC_CAP_DELEGATED: AtomicBool = AtomicBool::new(false); 89 static IPC_RIGHTS_ATTENUATED: AtomicBool = AtomicBool::new(false); 90 static IPC_FORGED_DENIED: AtomicBool = AtomicBool::new(false); 91 static IPC_REVOKED_DENIED: AtomicBool = AtomicBool::new(false); 92 static IPC_CHILD_HANDLE: AtomicU64 = AtomicU64::new(0); 93 static IPC_SENT_KIND: AtomicU64 = AtomicU64::new(0); 94 static IPC_SENT_P0: AtomicU64 = AtomicU64::new(0); 95 static IPC_SENT_P1: AtomicU64 = AtomicU64::new(0); 96 static IPC_SENT_OBJ: AtomicU64 = AtomicU64::new(0); 97 98 /// A private EL0 address window: four translation levels plus code and stack. 99 #[repr(C, align(4096))] 100 struct TaskWindow { 101 l0: Page, 102 l1: Page, 103 l2: Page, 104 l3: Page, 105 code: Page, 106 stack: Page, 107 } 108 109 impl TaskWindow { 110 const fn new() -> Self { 111 Self { 112 l0: Page([0; 512]), 113 l1: Page([0; 512]), 114 l2: Page([0; 512]), 115 l3: Page([0; 512]), 116 code: Page([0; 512]), 117 stack: Page([0; 512]), 118 } 119 } 120 } 121 122 static mut IPC_WINDOW_A: TaskWindow = TaskWindow::new(); 123 static mut IPC_WINDOW_B: TaskWindow = TaskWindow::new(); 124 125 /// Observed result of the two-principal EL0 IPC proof. 126 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] 127 pub struct IpcResult { 128 pub ok: bool, 129 pub exit_a: u64, 130 pub child_raw: u64, 131 pub exit_b0: u64, 132 pub exit_b1: u64, 133 pub message_delivered: bool, 134 pub cap_delegated: bool, 135 pub rights_attenuated: bool, 136 pub forged_denied: bool, 137 pub revoked_denied: bool, 138 pub exit_codes_clean: bool, 139 } 140 141 pub type WriteHook = fn(&[u8]); 142 143 pub fn set_write_hook(hook: WriteHook) { 144 USER_WRITE_HOOK.store(hook as usize, Ordering::Release); 145 } 146 147 const fn pack_handle(handle: Handle) -> u64 { 148 handle.to_raw() 149 } 150 151 fn user_bytes(address: u64, length: usize) -> Option<&'static [u8]> { 152 let end = address.checked_add(length as u64)?; 153 let code_base = USER_VA_BASE.load(Ordering::Acquire); 154 let code_end = code_base + PAGE_BYTES as u64; 155 if address < code_base || end > code_end { 156 return None; 157 } 158 let offset = (address - code_base) as usize; 159 Some(unsafe { 160 core::slice::from_raw_parts( 161 (core::ptr::addr_of!(USER_CODE) as *const u8).add(offset), 162 length, 163 ) 164 }) 165 } 166 167 fn dispatch_write(frame: &mut TrapFrame) { 168 let raw = frame.x[0]; 169 let handle = match Handle::from_raw(raw) { 170 Ok(h) => h, 171 Err(_) => { 172 FORGED_DENIED.store(true, Ordering::Release); 173 frame.x[0] = SYSCALL_DENIED; 174 return; 175 } 176 }; 177 let allowed = unsafe { USER_CAPS.lookup(handle, Rights::WRITE) == Ok(CONSOLE_RESOURCE) }; 178 let bytes = usize::try_from(frame.x[2]) 179 .ok() 180 .and_then(|length| user_bytes(frame.x[1], length)); 181 if allowed { 182 if let Some(bytes) = bytes { 183 let hook = USER_WRITE_HOOK.load(Ordering::Acquire); 184 if hook != 0 { 185 let hook: WriteHook = unsafe { core::mem::transmute(hook) }; 186 hook(bytes); 187 } 188 WRITE_GRANTED.store(true, Ordering::Release); 189 frame.x[0] = 0; 190 return; 191 } 192 } 193 if raw != AUTHORIZED_HANDLE.load(Ordering::Acquire) { 194 FORGED_DENIED.store(true, Ordering::Release); 195 } 196 frame.x[0] = SYSCALL_DENIED; 197 } 198 199 /// Lower-EL synchronous exception dispatcher called by the assembly vector. 200 /// 201 /// # Safety 202 /// `frame` must be the complete 800-byte frame created by the lower-EL 203 /// trampoline on SP_EL1. 204 #[no_mangle] 205 pub unsafe extern "C" fn aienos_lower_el_sync_dispatcher(frame: *mut TrapFrame) { 206 let frame = unsafe { &mut *frame }; 207 let esr: u64; 208 #[cfg(target_arch = "aarch64")] 209 unsafe { 210 core::arch::asm!("mrs {0}, esr_el1", out(reg) esr, options(nomem, nostack)); 211 } 212 #[cfg(not(target_arch = "aarch64"))] 213 { 214 esr = 0; 215 } 216 // A loaded artifact task owns EL0: its syscalls and faults never reach 217 // the M3 demo paths below or the fatal handler. 218 if crate::task_runtime::is_active() { 219 let far: u64; 220 #[cfg(target_arch = "aarch64")] 221 unsafe { 222 core::arch::asm!("mrs {0}, far_el1", out(reg) far, options(nomem, nostack)); 223 } 224 #[cfg(not(target_arch = "aarch64"))] 225 { 226 far = 0; 227 } 228 unsafe { crate::task_runtime::dispatch_sync(frame, esr, far) }; 229 return; 230 } 231 match (esr >> 26) & 0x3f { 232 0x15 if esr & 0xffff == 0 => { 233 if IPC_ACTIVE.load(Ordering::Acquire) { 234 dispatch_ipc(frame); 235 } else { 236 match frame.x[8] { 237 SYS_WRITE => dispatch_write(frame), 238 SYS_EXIT => { 239 EXIT_CODE.store(frame.x[0], Ordering::Release); 240 USER_ACTIVE.store(false, Ordering::Release); 241 frame.elr_el1 = user_resume_address(); 242 frame.spsr_el1 = 0x5; 243 } 244 _ => frame.x[0] = SYSCALL_DENIED, 245 } 246 } 247 } 248 0x24 => { 249 FAULT_CONTAINED.store(true, Ordering::Release); 250 frame.elr_el1 = frame.elr_el1.wrapping_add(4); 251 } 252 _ => { 253 EXIT_CODE.store(SYSCALL_DENIED, Ordering::Release); 254 USER_ACTIVE.store(false, Ordering::Release); 255 frame.elr_el1 = user_resume_address(); 256 frame.spsr_el1 = 0x5; 257 } 258 } 259 } 260 261 /// EL1-side dispatcher for the typed IPC syscalls. Every operation resolves 262 /// handles against the active principal's own capability table; there is no 263 /// ambient authority. 264 fn dispatch_ipc(frame: &mut TrapFrame) { 265 match frame.x[8] { 266 SYS_CHANNEL_SEND => { 267 let handle = match Handle::from_raw(frame.x[0]) { 268 Ok(h) => h, 269 Err(_) => { 270 frame.x[0] = SYSCALL_DENIED; 271 return; 272 } 273 }; 274 let message = crate::abi::Message::new( 275 frame.x[1] as u32, 276 [frame.x[2], frame.x[3], frame.x[4]], 277 crate::abi::MemoryRegion::new(frame.x[5], frame.x[6] as u32), 278 crate::abi::ObjectId(frame.x[7]), 279 ); 280 let result = unsafe { crate::ipc::channel_send_active(handle, message) }; 281 match result { 282 Ok(()) => { 283 IPC_SENT_KIND.store(frame.x[1], Ordering::Release); 284 IPC_SENT_P0.store(frame.x[2], Ordering::Release); 285 IPC_SENT_P1.store(frame.x[3], Ordering::Release); 286 IPC_SENT_OBJ.store(frame.x[7], Ordering::Release); 287 frame.x[0] = 0; 288 } 289 Err(_) => frame.x[0] = SYSCALL_DENIED, 290 } 291 } 292 SYS_CHANNEL_RECV => { 293 let handle = match Handle::from_raw(frame.x[0]) { 294 Ok(h) => h, 295 Err(_) => { 296 frame.x[0] = SYSCALL_DENIED; 297 return; 298 } 299 }; 300 match unsafe { crate::ipc::channel_receive_active(handle) } { 301 Ok(message) => { 302 let delivered = message.kind as u64 == IPC_SENT_KIND.load(Ordering::Acquire) 303 && message.payload[0] == IPC_SENT_P0.load(Ordering::Acquire) 304 && message.payload[1] == IPC_SENT_P1.load(Ordering::Acquire) 305 && message.object.0 == IPC_SENT_OBJ.load(Ordering::Acquire); 306 if delivered { 307 IPC_MESSAGE_DELIVERED.store(true, Ordering::Release); 308 } 309 frame.x[0] = 0; 310 frame.x[1] = message.kind as u64; 311 frame.x[2] = message.payload[0]; 312 frame.x[3] = message.payload[1]; 313 frame.x[4] = message.payload[2]; 314 frame.x[5] = message.object.0; 315 } 316 Err(_) => frame.x[0] = SYSCALL_DENIED, 317 } 318 } 319 SYS_CAP_DELEGATE => { 320 let handle = match Handle::from_raw(frame.x[0]) { 321 Ok(h) => h, 322 Err(_) => { 323 frame.x[0] = SYSCALL_DENIED; 324 return; 325 } 326 }; 327 let wire_rights = match crate::abi::Rights::from_bits(frame.x[2] as u32) { 328 Ok(r) => r, 329 Err(_) => { 330 frame.x[0] = SYSCALL_DENIED; 331 return; 332 } 333 }; 334 let caps_rights: Rights = match wire_rights.try_into() { 335 Ok(r) => r, 336 Err(_) => { 337 frame.x[0] = SYSCALL_DENIED; 338 return; 339 } 340 }; 341 match unsafe { 342 crate::ipc::delegate_from_active(handle, frame.x[1] as u32, caps_rights) 343 } { 344 Ok(child) => { 345 IPC_CAP_DELEGATED.store(true, Ordering::Release); 346 IPC_CHILD_HANDLE.store(child.to_raw(), Ordering::Release); 347 frame.x[0] = 0; 348 } 349 Err(_) => frame.x[0] = SYSCALL_DENIED, 350 } 351 } 352 SYS_OBJECT_READ => { 353 let handle = match Handle::from_raw(frame.x[0]) { 354 Ok(h) => h, 355 Err(_) => { 356 if IPC_PHASE.load(Ordering::Acquire) & 0xff != 0 { 357 IPC_REVOKED_DENIED.store(true, Ordering::Release); 358 } else { 359 IPC_FORGED_DENIED.store(true, Ordering::Release); 360 } 361 frame.x[0] = SYSCALL_DENIED; 362 return; 363 } 364 }; 365 match unsafe { crate::ipc::object_read_active(handle, frame.x[1] as usize) } { 366 Ok(value) => frame.x[0] = value, 367 Err(crate::ipc::IpcError::InvalidHandle) => { 368 if IPC_PHASE.load(Ordering::Acquire) & 0xff != 0 { 369 IPC_REVOKED_DENIED.store(true, Ordering::Release); 370 } else { 371 IPC_FORGED_DENIED.store(true, Ordering::Release); 372 } 373 frame.x[0] = SYSCALL_DENIED; 374 } 375 Err(_) => frame.x[0] = SYSCALL_DENIED, 376 } 377 } 378 SYS_OBJECT_WRITE => { 379 let handle = match Handle::from_raw(frame.x[0]) { 380 Ok(h) => h, 381 Err(_) => { 382 frame.x[0] = SYSCALL_DENIED; 383 return; 384 } 385 }; 386 match unsafe { 387 crate::ipc::object_write_active(handle, frame.x[1] as usize, frame.x[2]) 388 } { 389 Ok(()) => frame.x[0] = 0, 390 Err(crate::ipc::IpcError::MissingRights) => { 391 IPC_RIGHTS_ATTENUATED.store(true, Ordering::Release); 392 frame.x[0] = SYSCALL_DENIED; 393 } 394 Err(_) => frame.x[0] = SYSCALL_DENIED, 395 } 396 } 397 SYS_EXIT => { 398 IPC_EXIT.store(frame.x[0], Ordering::Release); 399 frame.elr_el1 = user_resume_address(); 400 frame.spsr_el1 = 0x5; 401 } 402 _ => frame.x[0] = SYSCALL_DENIED, 403 } 404 } 405 406 /// Map a fresh EL0 window (kernel-derived tables, read-only code, read-write 407 /// non-executable stack), copy `image` into the code page, and clean caches. 408 /// 409 /// # Safety 410 /// Window pointers must address live, exclusive, 4096-aligned statics. 411 #[cfg(target_arch = "aarch64")] 412 unsafe fn build_window(window: *mut TaskWindow, image: &[u8], kernel_root: usize) -> u64 { 413 let window = unsafe { &mut *window }; 414 window.l0.0.fill(0); 415 window.l1.0.fill(0); 416 window.l2.0.fill(0); 417 window.l3.0.fill(0); 418 window.code.0.fill(0); 419 window.stack.0.fill(0); 420 unsafe { 421 core::ptr::copy_nonoverlapping(kernel_root as *const u64, window.l0.0.as_mut_ptr(), 512) 422 }; 423 let user_root_index = (1..256) 424 .find(|index| window.l0.0[*index] == 0) 425 .expect("kernel mappings leave no EL0 address window"); 426 let user_base = (user_root_index as u64) << 39; 427 window.l0.0[user_root_index] = 428 (core::ptr::addr_of!(window.l1) as u64 & ADDRESS_MASK) | TABLE_DESCRIPTOR; 429 window.l1.0[0] = (core::ptr::addr_of!(window.l2) as u64 & ADDRESS_MASK) | TABLE_DESCRIPTOR; 430 window.l2.0[0] = (core::ptr::addr_of!(window.l3) as u64 & ADDRESS_MASK) | TABLE_DESCRIPTOR; 431 let code_pa = core::ptr::addr_of!(window.code) as u64; 432 let stack_pa = core::ptr::addr_of!(window.stack) as u64; 433 window.l3.0[0] = 434 (code_pa & ADDRESS_MASK) | PAGE_DESCRIPTOR | (0b11 << 6) | (0b11 << 8) | AF | NG | PXN; 435 window.l3.0[1] = (stack_pa & ADDRESS_MASK) 436 | PAGE_DESCRIPTOR 437 | (0b01 << 6) 438 | (0b11 << 8) 439 | AF 440 | NG 441 | PXN 442 | UXN; 443 assert!(image.len() <= PAGE_BYTES, "EL0 code page overflow"); 444 unsafe { core::ptr::copy_nonoverlapping(image.as_ptr(), code_pa as *mut u8, image.len()) }; 445 unsafe { 446 clean_and_invalidate( 447 core::ptr::addr_of!(window.l0) as usize, 448 PAGE_BYTES * 4, 449 false, 450 ); 451 clean_and_invalidate(code_pa as usize, PAGE_BYTES, true); 452 clean_and_invalidate(stack_pa as usize, PAGE_BYTES, false); 453 } 454 user_base 455 } 456 457 #[cfg(target_arch = "aarch64")] 458 unsafe fn swap_ttbr0(root: u64) { 459 core::arch::asm!( 460 "dsb ish", 461 "msr ttbr0_el1, {0}", 462 "tlbi vmalle1", 463 "dsb ish", 464 "isb", 465 in(reg) root, 466 options(nostack) 467 ); 468 } 469 470 /// Two-principal typed-IPC proof at EL0. 471 /// 472 /// Principal A holds the channel send right and a full-rights object 473 /// capability; it sends one typed message and delegates a READ-only copy of 474 /// the object capability to principal B. B receives the exact message, reads 475 /// the object through the attenuated handle, is denied a write and a forged 476 /// handle. The kernel then revokes A's ancestor and B's derived handle stops 477 /// working. 478 /// 479 /// # Safety 480 /// `kernel_root` must be the active identity-mapped TTBR0 root and vectors 481 /// must already be installed at EL1. 482 pub unsafe fn run_ipc_demo(kernel_root: usize) -> IpcResult { 483 #[cfg(target_arch = "aarch64")] 484 { 485 unsafe { run_ipc_demo_inner(kernel_root) } 486 } 487 #[cfg(not(target_arch = "aarch64"))] 488 { 489 let _ = kernel_root; 490 IpcResult { 491 ok: true, 492 exit_a: 0, 493 child_raw: 0, 494 exit_b0: 0, 495 exit_b1: 0, 496 message_delivered: true, 497 cap_delegated: true, 498 rights_attenuated: true, 499 forged_denied: true, 500 revoked_denied: true, 501 exit_codes_clean: true, 502 } 503 } 504 } 505 506 #[cfg(target_arch = "aarch64")] 507 unsafe fn run_ipc_demo_inner(kernel_root: usize) -> IpcResult { 508 IPC_MESSAGE_DELIVERED.store(false, Ordering::Release); 509 IPC_CAP_DELEGATED.store(false, Ordering::Release); 510 IPC_RIGHTS_ATTENUATED.store(false, Ordering::Release); 511 IPC_FORGED_DENIED.store(false, Ordering::Release); 512 IPC_REVOKED_DENIED.store(false, Ordering::Release); 513 IPC_CHILD_HANDLE.store(0, Ordering::Release); 514 IPC_EXIT.store(u64::MAX, Ordering::Release); 515 516 let (a_send, a_object, b_recv) = crate::ipc::reset(); 517 let image_start = core::ptr::addr_of!(aienos_ipc_code_start) as usize; 518 let image_end = core::ptr::addr_of!(aienos_ipc_code_end) as usize; 519 let image = 520 unsafe { core::slice::from_raw_parts(image_start as *const u8, image_end - image_start) }; 521 522 let base_a = unsafe { build_window(core::ptr::addr_of_mut!(IPC_WINDOW_A), image, kernel_root) }; 523 let base_b = unsafe { build_window(core::ptr::addr_of_mut!(IPC_WINDOW_B), image, kernel_root) }; 524 525 let window_a = unsafe { &mut *core::ptr::addr_of_mut!(IPC_WINDOW_A) }; 526 let window_b = unsafe { &mut *core::ptr::addr_of_mut!(IPC_WINDOW_B) }; 527 let root_a = core::ptr::addr_of!(window_a.l0) as u64; 528 let root_b = core::ptr::addr_of!(window_b.l0) as u64; 529 let kernel_root_u64 = kernel_root as u64; 530 531 IPC_ACTIVE.store(true, Ordering::Release); 532 533 // --- principal A: send the typed message, delegate an attenuated copy --- 534 crate::ipc::set_active_principal(crate::ipc::TaskId(crate::ipc::PRINCIPAL_A)); 535 IPC_PHASE.store(0, Ordering::Release); 536 IPC_EXIT.store(u64::MAX, Ordering::Release); 537 unsafe { swap_ttbr0(root_a) }; 538 unsafe { 539 aienos_ipc_enter( 540 base_a, 541 base_a + 0x1000 + PAGE_BYTES as u64, 542 pack_handle(a_send), 543 pack_handle(a_object), 544 1, 545 ) 546 }; 547 unsafe { swap_ttbr0(kernel_root_u64) }; 548 let exit_a = IPC_EXIT.load(Ordering::Acquire); 549 550 // --- principal B, phase 0: receive, read, attempt write, forge ---------- 551 let child_handle = IPC_CHILD_HANDLE.load(Ordering::Acquire); 552 crate::ipc::set_active_principal(crate::ipc::TaskId(crate::ipc::PRINCIPAL_B)); 553 IPC_PHASE.store(0, Ordering::Release); 554 IPC_EXIT.store(u64::MAX, Ordering::Release); 555 unsafe { swap_ttbr0(root_b) }; 556 unsafe { 557 aienos_ipc_enter( 558 base_b, 559 base_b + 0x1000 + PAGE_BYTES as u64, 560 pack_handle(b_recv), 561 child_handle, 562 2, 563 ) 564 }; 565 unsafe { swap_ttbr0(kernel_root_u64) }; 566 let exit_b0 = IPC_EXIT.load(Ordering::Acquire); 567 568 // --- kernel revokes A's ancestor; B's derived handle must die ---------- 569 unsafe { crate::ipc::revoke_object_ancestor(crate::ipc::PRINCIPAL_A, a_object) } 570 .expect("ancestor revocation"); 571 572 // --- principal B, phase 1: the derived handle no longer resolves ------- 573 IPC_PHASE.store(1, Ordering::Release); 574 IPC_EXIT.store(u64::MAX, Ordering::Release); 575 unsafe { swap_ttbr0(root_b) }; 576 unsafe { 577 aienos_ipc_enter( 578 base_b, 579 base_b + 0x1000 + PAGE_BYTES as u64, 580 pack_handle(b_recv), 581 child_handle, 582 0x102, 583 ) 584 }; 585 unsafe { swap_ttbr0(kernel_root_u64) }; 586 let exit_b1 = IPC_EXIT.load(Ordering::Acquire); 587 588 IPC_ACTIVE.store(false, Ordering::Release); 589 590 let result = IpcResult { 591 ok: false, 592 exit_a, 593 child_raw: IPC_CHILD_HANDLE.load(Ordering::Acquire), 594 exit_b0, 595 exit_b1, 596 message_delivered: IPC_MESSAGE_DELIVERED.load(Ordering::Acquire), 597 cap_delegated: IPC_CAP_DELEGATED.load(Ordering::Acquire), 598 rights_attenuated: IPC_RIGHTS_ATTENUATED.load(Ordering::Acquire), 599 forged_denied: IPC_FORGED_DENIED.load(Ordering::Acquire), 600 revoked_denied: IPC_REVOKED_DENIED.load(Ordering::Acquire), 601 exit_codes_clean: exit_a == 0 && exit_b0 == 0 && exit_b1 == 0, 602 }; 603 IpcResult { 604 ok: result.message_delivered 605 && result.cap_delegated 606 && result.rights_attenuated 607 && result.forged_denied 608 && result.revoked_denied 609 && result.exit_codes_clean, 610 ..result 611 } 612 } 613 614 #[cfg(target_arch = "aarch64")] 615 core::arch::global_asm!( 616 ".global aienos_ipc_enter", 617 ".global aienos_ipc_code_start", 618 ".global aienos_ipc_code_end", 619 // x0=entry, x1=sp_el0, x2=handle0 -> x20, x3=handle1 -> x21, x4=arg -> x22 620 "aienos_ipc_enter:", 621 "sub sp, sp, #96", 622 "stp x19, x20, [sp, #0]", 623 "stp x21, x22, [sp, #16]", 624 "stp x23, x24, [sp, #32]", 625 "stp x25, x26, [sp, #48]", 626 "stp x27, x28, [sp, #64]", 627 "stp x29, x30, [sp, #80]", 628 "mov x20, x2", 629 "mov x21, x3", 630 "mov x22, x4", 631 "msr sp_el0, x1", 632 "msr elr_el1, x0", 633 "msr spsr_el1, xzr", 634 "mov x0, x4", 635 "eret", 636 "aienos_ipc_code_start:", 637 "mov x23, xzr", 638 "cmp x22, #1", 639 "b.eq 10f", 640 "tbnz x22, #8, 50f", 641 "b 20f", 642 // ---- principal A: send, delegate attenuated, exit ---- 643 "10:", 644 "mov x0, x20", 645 "mov x1, #7", 646 "mov x2, #0x1111", 647 "mov x3, #0x2222", 648 "mov x4, #0x3333", 649 "mov x5, #0xa", 650 "mov x6, #0x1000", 651 "mov x7, #2", 652 "mov x8, #3", 653 "svc #0", 654 "cmp x0, #0", 655 "b.eq 15f", 656 "orr x23, x23, #32", 657 "15:", 658 "mov x0, x21", 659 "mov x1, #1", 660 "mov x2, #1", 661 "mov x8, #5", 662 "svc #0", 663 "cmp x0, #0", 664 "b.eq 16f", 665 "orr x23, x23, #64", 666 "16:", 667 "mov x0, x23", 668 "mov x8, #2", 669 "svc #0", 670 "70: b 70b", 671 // ---- principal B, phase 0: receive, read, write-attempt, forge ---- 672 "20:", 673 "mov x0, x20", 674 "mov x8, #4", 675 "svc #0", 676 "cmp x0, #0", 677 "b.eq 21f", 678 "orr x23, x23, #1", 679 "21:", 680 "cmp x1, #7", 681 "b.eq 22f", 682 "orr x23, x23, #1", 683 "22:", 684 "movz x5, #0x1111", 685 "cmp x2, x5", 686 "b.eq 23f", 687 "orr x23, x23, #1", 688 "23:", 689 "movz x5, #0x2222", 690 "cmp x3, x5", 691 "b.eq 24f", 692 "orr x23, x23, #1", 693 "24:", 694 "mov x0, x21", 695 "mov x1, #0", 696 "mov x8, #6", 697 "svc #0", 698 "movz x5, #0xee01", 699 "movk x5, #0xc0ff, lsl #16", 700 "cmp x0, x5", 701 "b.eq 25f", 702 "orr x23, x23, #2", 703 "25:", 704 "mov x0, x21", 705 "mov x1, #0", 706 "mov x2, #0xbad", 707 "mov x8, #7", 708 "svc #0", 709 "cmp x0, #0", 710 "b.ne 26f", 711 "orr x23, x23, #4", 712 "26:", 713 "mov x6, #1", 714 "lsl x6, x6, #32", 715 "eor x0, x21, x6", 716 "mov x1, #0", 717 "mov x8, #6", 718 "svc #0", 719 "cmp x0, #0", 720 "b.ne 27f", 721 "orr x23, x23, #8", 722 "27:", 723 "mov x0, x23", 724 "mov x8, #2", 725 "svc #0", 726 "71: b 71b", 727 // ---- principal B, phase 1: revoked handle must not resolve ---- 728 "50:", 729 "mov x0, x21", 730 "mov x1, #0", 731 "mov x8, #6", 732 "svc #0", 733 "cmp x0, #0", 734 "b.ne 51f", 735 "orr x23, x23, #16", 736 "51:", 737 "mov x0, x23", 738 "mov x8, #2", 739 "svc #0", 740 "72: b 72b", 741 "aienos_ipc_code_end:", 742 ); 743 744 #[cfg(target_arch = "aarch64")] 745 extern "C" { 746 fn aienos_ipc_enter(entry: u64, stack: u64, handle0: u64, handle1: u64, arg: u64); 747 static aienos_ipc_code_start: u8; 748 static aienos_ipc_code_end: u8; 749 } 750 751 #[cfg(target_arch = "aarch64")] 752 core::arch::global_asm!( 753 ".global aienos_enter_user", 754 ".global aienos_user_resume", 755 ".global aienos_user_code_start", 756 ".global aienos_user_code_end", 757 ".global aienos_lower_el_sync_trampoline", 758 "aienos_enter_user:", 759 "sub sp, sp, #96", 760 "stp x19, x20, [sp, #0]", 761 "stp x21, x22, [sp, #16]", 762 "stp x23, x24, [sp, #32]", 763 "stp x25, x26, [sp, #48]", 764 "stp x27, x28, [sp, #64]", 765 "stp x29, x30, [sp, #80]", 766 "msr sp_el0, x1", 767 "msr elr_el1, x0", 768 "msr spsr_el1, xzr", 769 "mov x0, x2", 770 "eret", 771 "aienos_user_resume:", 772 "ldp x19, x20, [sp, #0]", 773 "ldp x21, x22, [sp, #16]", 774 "ldp x23, x24, [sp, #32]", 775 "ldp x25, x26, [sp, #48]", 776 "ldp x27, x28, [sp, #64]", 777 "ldp x29, x30, [sp, #80]", 778 "add sp, sp, #96", 779 "ret", 780 "aienos_user_code_start:", 781 "mov x19, x0", 782 "mov x0, x19", 783 "adr x1, 2f", 784 "mov x2, #16", 785 "mov x8, #1", 786 "svc #0", 787 "mov x0, xzr", 788 "adr x1, 2f", 789 "mov x2, #16", 790 "mov x8, #1", 791 "svc #0", 792 "ldr x21, [x3]", 793 "mov x0, xzr", 794 "mov x8, #2", 795 "svc #0", 796 "1: b 1b", 797 ".balign 4", 798 "2: .ascii \"el0 user: hello\\n\"", 799 "aienos_user_code_end:", 800 "aienos_lower_el_sync_trampoline:", 801 "sub sp, sp, #800", 802 "stp x0, x1, [sp, #0]", 803 "stp x2, x3, [sp, #16]", 804 "stp x4, x5, [sp, #32]", 805 "stp x6, x7, [sp, #48]", 806 "stp x8, x9, [sp, #64]", 807 "stp x10, x11, [sp, #80]", 808 "stp x12, x13, [sp, #96]", 809 "stp x14, x15, [sp, #112]", 810 "stp x16, x17, [sp, #128]", 811 "stp x18, x19, [sp, #144]", 812 "stp x20, x21, [sp, #160]", 813 "stp x22, x23, [sp, #176]", 814 "stp x24, x25, [sp, #192]", 815 "stp x26, x27, [sp, #208]", 816 "stp x28, x29, [sp, #224]", 817 "str x30, [sp, #240]", 818 "str xzr, [sp, #248]", 819 "mrs x0, elr_el1", 820 "mrs x1, spsr_el1", 821 "stp x0, x1, [sp, #256]", 822 "mrs x0, fpcr", 823 "mrs x1, fpsr", 824 "stp x0, x1, [sp, #272]", 825 "stp q0, q1, [sp, #288]", 826 "stp q2, q3, [sp, #320]", 827 "stp q4, q5, [sp, #352]", 828 "stp q6, q7, [sp, #384]", 829 "stp q8, q9, [sp, #416]", 830 "stp q10, q11, [sp, #448]", 831 "stp q12, q13, [sp, #480]", 832 "stp q14, q15, [sp, #512]", 833 "stp q16, q17, [sp, #544]", 834 "stp q18, q19, [sp, #576]", 835 "stp q20, q21, [sp, #608]", 836 "stp q22, q23, [sp, #640]", 837 "stp q24, q25, [sp, #672]", 838 "stp q26, q27, [sp, #704]", 839 "stp q28, q29, [sp, #736]", 840 "stp q30, q31, [sp, #768]", 841 "mov x0, sp", 842 "bl aienos_lower_el_sync_dispatcher", 843 "ldp q0, q1, [sp, #288]", 844 "ldp q2, q3, [sp, #320]", 845 "ldp q4, q5, [sp, #352]", 846 "ldp q6, q7, [sp, #384]", 847 "ldp q8, q9, [sp, #416]", 848 "ldp q10, q11, [sp, #448]", 849 "ldp q12, q13, [sp, #480]", 850 "ldp q14, q15, [sp, #512]", 851 "ldp q16, q17, [sp, #544]", 852 "ldp q18, q19, [sp, #576]", 853 "ldp q20, q21, [sp, #608]", 854 "ldp q22, q23, [sp, #640]", 855 "ldp q24, q25, [sp, #672]", 856 "ldp q26, q27, [sp, #704]", 857 "ldp q28, q29, [sp, #736]", 858 "ldp q30, q31, [sp, #768]", 859 "ldp x0, x1, [sp, #272]", 860 "msr fpcr, x0", 861 "msr fpsr, x1", 862 "ldp x0, x1, [sp, #256]", 863 "msr elr_el1, x0", 864 "msr spsr_el1, x1", 865 "ldp x2, x3, [sp, #16]", 866 "ldp x4, x5, [sp, #32]", 867 "ldp x6, x7, [sp, #48]", 868 "ldp x8, x9, [sp, #64]", 869 "ldp x10, x11, [sp, #80]", 870 "ldp x12, x13, [sp, #96]", 871 "ldp x14, x15, [sp, #112]", 872 "ldp x16, x17, [sp, #128]", 873 "ldp x18, x19, [sp, #144]", 874 "ldp x20, x21, [sp, #160]", 875 "ldp x22, x23, [sp, #176]", 876 "ldp x24, x25, [sp, #192]", 877 "ldp x26, x27, [sp, #208]", 878 "ldp x28, x29, [sp, #224]", 879 "ldr x30, [sp, #240]", 880 "ldp x0, x1, [sp, #0]", 881 "add sp, sp, #800", 882 "eret", 883 ); 884 885 #[cfg(target_arch = "aarch64")] 886 extern "C" { 887 fn aienos_enter_user(entry: u64, stack: u64, handle: u64, fault_address: u64); 888 static aienos_user_code_start: u8; 889 static aienos_user_code_end: u8; 890 static aienos_user_resume: u8; 891 } 892 893 fn user_resume_address() -> u64 { 894 #[cfg(target_arch = "aarch64")] 895 { 896 core::ptr::addr_of!(aienos_user_resume) as u64 897 } 898 #[cfg(not(target_arch = "aarch64"))] 899 { 900 0 901 } 902 } 903 904 #[cfg(target_arch = "aarch64")] 905 unsafe fn clean_and_invalidate(address: usize, length: usize, executable: bool) { 906 let ctr: u64; 907 unsafe { core::arch::asm!("mrs {0}, ctr_el0", out(reg) ctr, options(nomem, nostack)) }; 908 let dline = 4usize << ((ctr >> 16) & 0xf); 909 let iline = 4usize << (ctr & 0xf); 910 for at in (address..address + length).step_by(dline) { 911 unsafe { core::arch::asm!("dc cvau, {0}", in(reg) at, options(nostack)) }; 912 } 913 unsafe { core::arch::asm!("dsb ish", options(nostack)) }; 914 if executable { 915 for at in (address..address + length).step_by(iline) { 916 unsafe { core::arch::asm!("ic ivau, {0}", in(reg) at, options(nostack)) }; 917 } 918 unsafe { core::arch::asm!("dsb ish", "isb", options(nostack)) }; 919 } 920 } 921 922 /// Run the EL0 isolation proof using a per-task TTBR0 root. 923 /// 924 /// # Safety 925 /// `kernel_root` must be the active identity-mapped TTBR0 root and vectors 926 /// must already be installed at EL1. 927 pub unsafe fn run_demo(kernel_root: usize) -> DemoResult { 928 #[cfg(target_arch = "aarch64")] 929 { 930 unsafe { run_demo_inner(kernel_root) } 931 } 932 #[cfg(not(target_arch = "aarch64"))] 933 { 934 let _ = kernel_root; 935 DemoResult { 936 write_granted: true, 937 forged_denied: true, 938 fault_contained: true, 939 exit_code: 0, 940 } 941 } 942 } 943 944 #[cfg(target_arch = "aarch64")] 945 unsafe fn run_demo_inner(kernel_root: usize) -> DemoResult { 946 USER_CAPS = CapTable::new(0x454c_3001); 947 let handle = USER_CAPS 948 .insert(CONSOLE_RESOURCE, Rights::WRITE) 949 .expect("EL0 console capability table full"); 950 let raw_handle = pack_handle(handle); 951 AUTHORIZED_HANDLE.store(raw_handle, Ordering::Release); 952 WRITE_GRANTED.store(false, Ordering::Release); 953 FORGED_DENIED.store(false, Ordering::Release); 954 FAULT_CONTAINED.store(false, Ordering::Release); 955 EXIT_CODE.store(u64::MAX, Ordering::Release); 956 957 USER_L0.0.fill(0); 958 USER_L1.0.fill(0); 959 USER_L2.0.fill(0); 960 USER_L3.0.fill(0); 961 USER_CODE.0.fill(0); 962 USER_STACK.0.fill(0); 963 core::ptr::copy_nonoverlapping(kernel_root as *const u64, USER_L0.0.as_mut_ptr(), 512); 964 let user_root_index = (1..256) 965 .find(|index| USER_L0.0[*index] == 0) 966 .expect("kernel mappings leave no EL0 address window"); 967 let user_base = (user_root_index as u64) << 39; 968 USER_VA_BASE.store(user_base, Ordering::Release); 969 USER_L0.0[user_root_index] = 970 (core::ptr::addr_of!(USER_L1) as u64 & ADDRESS_MASK) | TABLE_DESCRIPTOR; 971 USER_L1.0[0] = (core::ptr::addr_of!(USER_L2) as u64 & ADDRESS_MASK) | TABLE_DESCRIPTOR; 972 USER_L2.0[0] = (core::ptr::addr_of!(USER_L3) as u64 & ADDRESS_MASK) | TABLE_DESCRIPTOR; 973 let user_code_pa = core::ptr::addr_of!(USER_CODE) as u64; 974 let user_stack_pa = core::ptr::addr_of!(USER_STACK) as u64; 975 USER_L3.0[0] = 976 (user_code_pa & ADDRESS_MASK) | PAGE_DESCRIPTOR | (0b11 << 6) | (0b11 << 8) | AF | NG | PXN; 977 USER_L3.0[1] = (user_stack_pa & ADDRESS_MASK) 978 | PAGE_DESCRIPTOR 979 | (0b01 << 6) 980 | (0b11 << 8) 981 | AF 982 | NG 983 | PXN 984 | UXN; 985 986 let start = core::ptr::addr_of!(aienos_user_code_start) as usize; 987 let end = core::ptr::addr_of!(aienos_user_code_end) as usize; 988 let length = end.checked_sub(start).expect("EL0 code symbols reversed"); 989 assert!(length <= PAGE_BYTES, "EL0 code page overflow"); 990 core::ptr::copy_nonoverlapping(start as *const u8, user_code_pa as *mut u8, length); 991 992 clean_and_invalidate(core::ptr::addr_of!(USER_L0) as usize, PAGE_BYTES * 4, false); 993 clean_and_invalidate(user_code_pa as usize, PAGE_BYTES, true); 994 clean_and_invalidate(user_stack_pa as usize, PAGE_BYTES, false); 995 996 let task_root = core::ptr::addr_of!(USER_L0) as u64; 997 core::arch::asm!( 998 "dsb ish", 999 "msr ttbr0_el1, {0}", 1000 "tlbi vmalle1", 1001 "dsb ish", 1002 "isb", 1003 in(reg) task_root | (1u64 << 48), 1004 options(nostack) 1005 ); 1006 USER_ACTIVE.store(true, Ordering::Release); 1007 aienos_enter_user( 1008 user_base, 1009 user_base + 0x1000 + PAGE_BYTES as u64, 1010 raw_handle, 1011 run_demo_inner as *const () as usize as u64, 1012 ); 1013 core::arch::asm!( 1014 "dsb ish", 1015 "msr ttbr0_el1, {0}", 1016 "tlbi vmalle1", 1017 "dsb ish", 1018 "isb", 1019 in(reg) kernel_root as u64, 1020 options(nostack) 1021 ); 1022 DemoResult { 1023 write_granted: WRITE_GRANTED.load(Ordering::Acquire), 1024 forged_denied: FORGED_DENIED.load(Ordering::Acquire), 1025 fault_contained: FAULT_CONTAINED.load(Ordering::Acquire), 1026 exit_code: EXIT_CODE.load(Ordering::Acquire), 1027 } 1028 } 1029 1030 #[cfg(test)] 1031 mod tests { 1032 use super::*; 1033 1034 #[test] 1035 fn capability_handle_round_trips() { 1036 let handle = Handle::new(7, 19).unwrap(); 1037 assert_eq!(Handle::from_raw(handle.to_raw()), Ok(handle)); 1038 } 1039 1040 #[test] 1041 fn trap_frame_layout_matches_assembly() { 1042 let frame = TrapFrame::default(); 1043 let base = core::ptr::addr_of!(frame) as usize; 1044 assert_eq!(core::mem::size_of::(), 800); 1045 assert_eq!(core::ptr::addr_of!(frame.x) as usize - base, 0); 1046 assert_eq!(core::ptr::addr_of!(frame._pad) as usize - base, 248); 1047 assert_eq!(core::ptr::addr_of!(frame.elr_el1) as usize - base, 256); 1048 assert_eq!(core::ptr::addr_of!(frame.spsr_el1) as usize - base, 264); 1049 assert_eq!(core::ptr::addr_of!(frame.fpcr) as usize - base, 272); 1050 assert_eq!(core::ptr::addr_of!(frame.fpsr) as usize - base, 280); 1051 assert_eq!(core::ptr::addr_of!(frame.q) as usize - base, 288); 1052 } 1053 1054 #[test] 1055 fn el0_mapping_attributes_are_wx_and_non_global() { 1056 let code = PAGE_DESCRIPTOR | (0b11 << 6) | AF | NG | PXN; 1057 let stack = PAGE_DESCRIPTOR | (0b01 << 6) | AF | NG | PXN | UXN; 1058 assert_eq!((code >> 6) & 3, 0b11, "EL0 read-only code"); 1059 assert_eq!(code & UXN, 0, "EL0 code executable"); 1060 assert_eq!((stack >> 6) & 3, 0b01, "EL0 read-write stack"); 1061 assert_ne!(stack & UXN, 0, "EL0 stack execute-never"); 1062 assert_ne!(code & NG, 0); 1063 assert_ne!(stack & NG, 0); 1064 } 1065 }