From 637e2c8d32ce726efb770f68fa3565fcc0465df1 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:15:28 -0500 Subject: [PATCH] fix(display): read a scanout buffer's address in the upper half of the address space as an address, where it was read as an error and a monitor on the GPU got no picture --- crates/stela-boot-core/src/page_window.rs | 24 ++++++++++++++++++++++ crates/stela-boot/src/engine/gpu/client.rs | 12 +++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/stela-boot-core/src/page_window.rs b/crates/stela-boot-core/src/page_window.rs index cd6c84d347..c3927ac985 100644 --- a/crates/stela-boot-core/src/page_window.rs +++ b/crates/stela-boot-core/src/page_window.rs @@ -67,6 +67,19 @@ pub fn code_leaf(physical: u64) -> u64 { leaf(physical, Cache::WriteBack) & !NO_EXECUTE } +/// What a mapping call returned, as Linux's `IS_ERR_VALUE` reads it: the +/// errno in the last page of the address space, or the mapping's address. +/// The window is in the upper half of the address space, so its addresses +/// are negative as an `i64` like an errno is. +pub fn mapped_address(result: i64) -> Result { + const MAX_ERRNO: i64 = 4095; + if (-MAX_ERRNO..=0).contains(&result) { + Err(result) + } else { + Ok(result as u64) + } +} + /// Translate a Linux client protection into a leaf for the shared data window. pub fn client_leaf(pfn: u64, prot: u64) -> Option { const MAX_PFN: u64 = (1 << (52 - 12)) - 1; @@ -266,6 +279,17 @@ pub fn finish_batch(batch: &[Range], retiring: &mut Retirements, free: &mut mod tests { use super::*; + #[test] + fn a_mapping_in_the_upper_half_is_an_address_and_only_an_errno_is_an_error() { + let upper = 0xffff_8010_0000_3000u64; + assert_eq!(mapped_address(upper as i64), Ok(upper)); + assert_eq!(mapped_address(0x7000_0000), Ok(0x7000_0000)); + assert_eq!(mapped_address(-12), Err(-12)); + assert_eq!(mapped_address(-4095), Err(-4095)); + assert_eq!(mapped_address(-4096), Ok(-4096i64 as u64)); + assert_eq!(mapped_address(0), Err(0)); + } + const GIB: u64 = 1 << 30; #[test] diff --git a/crates/stela-boot/src/engine/gpu/client.rs b/crates/stela-boot/src/engine/gpu/client.rs index 79d7d19521..8858bd050b 100644 --- a/crates/stela-boot/src/engine/gpu/client.rs +++ b/crates/stela-boot/src/engine/gpu/client.rs @@ -1181,8 +1181,16 @@ pub(super) fn control(fd: c_int, request: u32, argument: *mut c_void) -> i64 { /// Maps `size` bytes of `fd` at `offset`. pub(super) fn map(fd: c_int, offset: u64, size: usize) -> Option> { - let address = submit(Operation::Map { fd, offset, size }); - NonNull::new(u64::try_from(address).ok()? as *mut u8) + let result = submit(Operation::Map { fd, offset, size }); + match stela_boot_core::page_window::mapped_address(result) { + Ok(address) => NonNull::new(address as *mut u8), + Err(error) => { + output::arguments(format_args!( + "GPU map of {size} bytes at offset {offset:#x} refused: {error}\n" + )); + None + } + } } /// `NOUVEAU_GETPARAM_CHIPSET_ID` of the bound GPU at `index`.