Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions library/std/src/os/xous/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn lend_mut_impl(
let mut a1: usize = connection.try_into().unwrap();
let mut a2 = InvokeType::LendMut as usize;
let a3 = opcode;
let a4 = data.as_mut_ptr() as usize;
let a4 = data.as_mut_ptr();
let a5 = data.len();
let a6 = arg1;
let a7 = arg2;
Expand Down Expand Up @@ -86,7 +86,7 @@ fn lend_impl(
let a1: usize = connection.try_into().unwrap();
let a2 = InvokeType::Lend as usize;
let a3 = opcode;
let a4 = data.as_ptr() as usize;
let a4 = data.as_ptr();
let a5 = data.len();
let a6 = arg1;
let a7 = arg2;
Expand Down Expand Up @@ -359,15 +359,19 @@ pub(crate) fn do_yield() {
/// This function is safe unless a virtual address is specified. In that case,
/// the kernel will return an alias to the existing range. This violates Rust's
/// pointer uniqueness guarantee.
// The phys argument uses an integer rather than pointer type as pointer
// provenance only covers virtual memory, not physical memory.
pub(crate) unsafe fn map_memory<T>(
phys: Option<core::ptr::NonNull<T>>,
phys: Option<core::num::NonZeroUsize>,
Comment thread
bjorn3 marked this conversation as resolved.
virt: Option<core::ptr::NonNull<T>>,
count: usize,
flags: MemoryFlags,
) -> Result<&'static mut [T], Error> {
let mut a0 = Syscall::MapMemory as usize;
let mut a1 = phys.map(|p| p.as_ptr() as usize).unwrap_or_default();
let mut a2 = virt.map(|p| p.as_ptr() as usize).unwrap_or_default();
let a1 = phys.map_or(0, |p| p.get());
let a1_out: *mut T;
let a2 = virt.map_or(core::ptr::null(), |p| p.as_ptr());
let a2_out: usize;
let a3 = count * size_of::<T>();
let a4 = flags.bits();
let a5 = 0;
Expand All @@ -378,8 +382,8 @@ pub(crate) unsafe fn map_memory<T>(
core::arch::asm!(
"ecall",
inlateout("a0") a0,
inlateout("a1") a1,
inlateout("a2") a2,
inlateout("a1") a1 => a1_out,
inlateout("a2") a2 => a2_out,
inlateout("a3") a3 => _,
inlateout("a4") a4 => _,
inlateout("a5") a5 => _,
Expand All @@ -391,12 +395,12 @@ pub(crate) unsafe fn map_memory<T>(
let result = a0;

if result == SyscallResult::MemoryRange as usize {
let start = core::ptr::with_exposed_provenance_mut::<T>(a1);
let len = a2 / size_of::<T>();
let start = a1_out;
let len = a2_out / size_of::<T>();
let end = unsafe { start.add(len) };
Ok(unsafe { core::slice::from_raw_parts_mut(start, len) })
} else if result == SyscallResult::Error as usize {
Err(a1.into())
Err(a1_out.addr().into())
} else {
Err(Error::InternalError)
}
Expand All @@ -408,7 +412,7 @@ pub(crate) unsafe fn map_memory<T>(
/// function returns, even if this function returns Err().
pub(crate) unsafe fn unmap_memory<T>(range: *mut [T]) -> Result<(), Error> {
let mut a0 = Syscall::UnmapMemory as usize;
let mut a1 = range.as_mut_ptr() as usize;
let mut a1 = range.as_mut_ptr();
let a2 = range.len() * size_of::<T>();
let a3 = 0;
let a4 = 0;
Expand All @@ -435,7 +439,7 @@ pub(crate) unsafe fn unmap_memory<T>(range: *mut [T]) -> Result<(), Error> {
if result == SyscallResult::Ok as usize {
Ok(())
} else if result == SyscallResult::Error as usize {
Err(a1.into())
Err(a1.addr().into())
} else {
Err(Error::InternalError)
}
Expand All @@ -454,7 +458,8 @@ pub(crate) unsafe fn update_memory_flags<T>(
new_flags: MemoryFlags,
) -> Result<(), Error> {
let mut a0 = Syscall::UpdateMemoryFlags as usize;
let mut a1 = range.as_mut_ptr() as usize;
let a1 = range.as_mut_ptr();
let a1_out: usize;
let a2 = range.len() * size_of::<T>();
let a3 = new_flags.bits();
let a4 = 0; // Process ID is currently None
Expand All @@ -466,7 +471,7 @@ pub(crate) unsafe fn update_memory_flags<T>(
core::arch::asm!(
"ecall",
inlateout("a0") a0,
inlateout("a1") a1,
inlateout("a1") a1 => a1_out,
inlateout("a2") a2 => _,
inlateout("a3") a3 => _,
inlateout("a4") a4 => _,
Expand All @@ -481,24 +486,25 @@ pub(crate) unsafe fn update_memory_flags<T>(
if result == SyscallResult::Ok as usize {
Ok(())
} else if result == SyscallResult::Error as usize {
Err(a1.into())
Err(a1_out.into())
} else {
Err(Error::InternalError)
}
}

/// Creates a thread with a given stack and up to four arguments.
pub(crate) fn create_thread(
start: *mut usize,
pub(crate) unsafe fn create_thread<T>(

@Darksonn Darksonn Jul 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this generic lead to extra machine code due to monomorphisation?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is only a single monomorphic caller.

start: unsafe extern "C" fn(*mut usize, usize, usize) -> !,
stack: *mut [u8],
arg0: usize,
arg1: usize,
arg0: *mut T,
arg1: *const u8,
arg2: usize,
arg3: usize,
) -> Result<ThreadId, Error> {
let mut a0 = Syscall::CreateThread as usize;
let mut a1 = start as usize;
let a2 = stack.as_mut_ptr() as usize;
let a1 = start;
let a1_out: usize;
let a2 = stack.as_mut_ptr();
let a3 = stack.len();
let a4 = arg0;
let a5 = arg1;
Expand All @@ -509,7 +515,7 @@ pub(crate) fn create_thread(
core::arch::asm!(
"ecall",
inlateout("a0") a0,
inlateout("a1") a1,
inlateout("a1") a1 => a1_out,
inlateout("a2") a2 => _,
inlateout("a3") a3 => _,
inlateout("a4") a4 => _,
Expand All @@ -522,9 +528,9 @@ pub(crate) fn create_thread(
let result = a0;

if result == SyscallResult::ThreadId as usize {
Ok(a1.into())
Ok(a1_out.into())
} else if result == SyscallResult::Error as usize {
Err(a1.into())
Err(a1_out.into())
} else {
Err(Error::InternalError)
}
Expand Down
8 changes: 4 additions & 4 deletions library/std/src/sys/thread/xous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ impl Thread {
.map_err(|code| io::Error::from_raw_os_error(code as i32))?
};

let guard_page_pre = stack_plus_guard_pages.as_ptr() as usize;
let guard_page_pre = stack_plus_guard_pages.as_ptr();
let tid = create_thread(
thread_start as *mut usize,
thread_start,
&mut stack_plus_guard_pages[GUARD_PAGE_SIZE..(stack_size + GUARD_PAGE_SIZE)],
data as usize,
data,
guard_page_pre,
stack_size,
0,
Expand All @@ -75,7 +75,7 @@ impl Thread {
rust_start();
}

extern "C" fn thread_start(
unsafe extern "C" fn thread_start(
data: *mut usize,
guard_page_pre: usize,
stack_size: usize,
Expand Down
10 changes: 5 additions & 5 deletions library/std/src/sys/thread_local/key/xous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ unsafe extern "Rust" {

#[inline]
fn tls_ptr_addr() -> *mut *mut u8 {
let mut tp: usize;
let tp: *mut *mut u8;
unsafe {
asm!(
"mv {}, tp",
out(reg) tp,
);
}
core::ptr::with_exposed_provenance_mut::<*mut u8>(tp)
tp
}

/// Creates an area of memory that's unique per thread. This area will
Expand All @@ -97,7 +97,7 @@ fn tls_table() -> &'static mut [*mut u8] {
fn tls_table_slow() -> &'static mut [*mut u8] {
// If the TP register is `0`, then this thread hasn't initialized
// its TLS yet. Allocate a new page to store this memory.
let tp = unsafe {
let tp: &mut [*mut u8] = unsafe {
map_memory(
None,
None,
Expand All @@ -108,14 +108,14 @@ fn tls_table_slow() -> &'static mut [*mut u8] {
};

for val in tp.iter() {
assert!(*val as usize == 0);
assert!((*val).is_null());
}

unsafe {
// Set the thread's `$tp` register
asm!(
"mv tp, {}",
in(reg) tp.as_mut_ptr() as usize,
in(reg) tp.as_mut_ptr(),
);
}
tp
Expand Down
Loading