xous: add create_thread_n functions

These are used to create threads with between 0 and 4 arguments.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-03-25 10:42:49 +08:00
parent 568d1263a3
commit 2b97ea25ab
5 changed files with 1402 additions and 76 deletions

View file

@ -125,9 +125,7 @@ impl Process {
let pid = unsafe { PROCESS_TABLE.current };
let hardware_pid = (riscv::register::satp::read().bits() >> 22) & ((1 << 9) - 1);
assert!((pid.get() as usize) == hardware_pid);
Process {
pid,
}
Process { pid }
}
/// Mark this process as running on the current core
@ -299,7 +297,10 @@ impl Process {
thread.sepc = unsafe { core::mem::transmute::<_, usize>(thread_init.call) };
thread.registers[1] = thread_init.stack.as_ptr() as usize + thread_init.stack.len();
thread.registers[9] = thread_init.arg.map(|x| x.get()).unwrap_or_default();
thread.registers[9] = thread_init.arg1;
thread.registers[10] = thread_init.arg2;
thread.registers[11] = thread_init.arg3;
thread.registers[12] = thread_init.arg4;
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
{
@ -331,7 +332,11 @@ impl Process {
Ok(())
}
pub fn setup_thread(&mut self, new_tid: TID, setup: ThreadInit) -> Result<(), xous_kernel::Error> {
pub fn setup_thread(
&mut self,
new_tid: TID,
setup: ThreadInit,
) -> Result<(), xous_kernel::Error> {
let entrypoint = unsafe { core::mem::transmute::<_, usize>(setup.call) };
// Create the new context and set it to run in the new address space.
let pid = self.pid.get();
@ -343,7 +348,7 @@ impl Process {
entrypoint,
setup.stack.as_ptr() as usize + setup.stack.len(),
EXIT_THREAD,
&[setup.arg.map(|x| x.get() as usize).unwrap_or_default()],
&[setup.arg1, setup.arg2, setup.arg3, setup.arg4],
);
Ok(())
}
@ -364,27 +369,45 @@ impl Process {
);
println!(
"T3:{:08x} T4:{:08x} T5:{:08x} T6:{:08x}",
_thread.registers[27], _thread.registers[28], _thread.registers[29], _thread.registers[30]
_thread.registers[27],
_thread.registers[28],
_thread.registers[29],
_thread.registers[30]
);
println!(
"S0:{:08x} S1:{:08x} S2:{:08x} S3:{:08x}",
_thread.registers[7], _thread.registers[8], _thread.registers[17], _thread.registers[18]
_thread.registers[7],
_thread.registers[8],
_thread.registers[17],
_thread.registers[18]
);
println!(
"S4:{:08x} S5:{:08x} S6:{:08x} S7:{:08x}",
_thread.registers[19], _thread.registers[20], _thread.registers[21], _thread.registers[22]
_thread.registers[19],
_thread.registers[20],
_thread.registers[21],
_thread.registers[22]
);
println!(
"S8:{:08x} S9:{:08x} S10:{:08x} S11:{:08x}",
_thread.registers[23], _thread.registers[24], _thread.registers[25], _thread.registers[26]
_thread.registers[23],
_thread.registers[24],
_thread.registers[25],
_thread.registers[26]
);
println!(
"A0:{:08x} A1:{:08x} A2:{:08x} A3:{:08x}",
_thread.registers[9], _thread.registers[10], _thread.registers[11], _thread.registers[12]
_thread.registers[9],
_thread.registers[10],
_thread.registers[11],
_thread.registers[12]
);
println!(
"A4:{:08x} A5:{:08x} A6:{:08x} A7:{:08x}",
_thread.registers[13], _thread.registers[14], _thread.registers[15], _thread.registers[16]
_thread.registers[13],
_thread.registers[14],
_thread.registers[15],
_thread.registers[16]
);
}

File diff suppressed because it is too large Load diff

View file

@ -321,8 +321,10 @@ impl SystemServices {
process.state = ProcessState::Setup(ThreadInit::new(
unsafe { core::mem::transmute::<usize, _>(init.entrypoint) },
MemoryRange::new(init.sp, crate::arch::process::DEFAULT_STACK_SIZE).unwrap(),
MemoryAddress::new(pid),
[0u8; 12],
pid,
0,
0,
0,
));
}
}

View file

@ -1,4 +1,4 @@
use crate::{MemoryAddress, MemoryRange, PID, TID};
use crate::{MemoryRange, PID, TID};
use core::convert::TryInto;
mod mem;
@ -11,24 +11,34 @@ pub struct ProcessArgs {
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ThreadInit {
pub call: fn(data: usize) -> usize,
/// Function pointer that accepts 0-4 arguments
pub call: usize,
pub stack: MemoryRange,
pub arg: Option<MemoryAddress>,
pub name: [u8; 12],
pub arg1: usize,
pub arg2: usize,
pub arg3: usize,
pub arg4: usize,
// pub name: [u8; 12],
}
impl ThreadInit {
pub fn new(
call: fn(data: usize) -> usize,
call: usize,
stack: MemoryRange,
arg: Option<MemoryAddress>,
name: [u8; 12],
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
// name: [u8; 12],
) -> Self {
ThreadInit {
call,
stack,
arg,
name,
arg1,
arg2,
arg3,
arg4,
// name,
}
}
}
@ -36,10 +46,13 @@ impl ThreadInit {
impl Default for ThreadInit {
fn default() -> Self {
ThreadInit {
call: unsafe { core::mem::transmute::<usize, fn(data: usize) -> usize>(4) },
call: 1,
stack: MemoryRange::new(4, 4).unwrap(),
arg: None,
name: [0; 12],
arg1: 0,
arg2: 0,
arg3: 0,
arg4: 0,
// name: [0; 12],
}
}
}
@ -60,16 +73,16 @@ pub struct ProcessInit {
pub struct WaitHandle<T>(core::marker::PhantomData<T>);
pub struct ProcessHandle(());
pub fn thread_to_args(call: usize, init: &ThreadInit) -> [usize; 8] {
pub fn thread_to_args(syscall: usize, init: &ThreadInit) -> [usize; 8] {
[
call as usize,
init.call as usize,
syscall,
init.call,
init.stack.as_ptr() as _,
init.stack.len(),
init.arg.map(|x| x.get()).unwrap_or_default(),
0,
0,
0,
init.arg1,
init.arg2,
init.arg3,
init.arg4,
]
}
@ -81,18 +94,18 @@ pub fn args_to_thread(
a2: usize,
a3: usize,
a4: usize,
_a5: usize,
_a6: usize,
_a7: usize,
a5: usize,
a6: usize,
a7: usize,
) -> core::result::Result<ThreadInit, crate::Error> {
let call = unsafe { core::mem::transmute(a1) };
let stack = MemoryRange::new(a2, a3).map_err(|_| crate::Error::InvalidSyscall)?;
let arg = MemoryAddress::new(a4);
Ok(ThreadInit {
call,
stack,
arg,
name: [0; 12],
call: a1,
stack: MemoryRange::new(a2, a3).map_err(|_| crate::Error::InvalidSyscall)?,
arg1: a4,
arg2: a5,
arg3: a6,
arg4: a7,
// name: [0; 12],
})
}
@ -103,7 +116,6 @@ where
T: Send + 'static,
{
todo!()
// Ok(ThreadInit {})
}
pub fn create_thread_post<F, T>(
@ -116,29 +128,10 @@ where
T: Send + 'static,
{
todo!()
// let server_address = xous_address();
// let server_connection =
// XOUS_SERVER_CONNECTION.with(|xsc| xsc.borrow().as_ref().unwrap().clone());
// let process_id = PROCESS_ID.with(|pid| *pid.borrow());
// Ok(std::thread::Builder::new()
// .spawn(move || {
// set_xous_address(server_address);
// THREAD_ID.with(|tid| *tid.borrow_mut() = thread_id);
// PROCESS_ID.with(|pid| *pid.borrow_mut() = process_id);
// XOUS_SERVER_CONNECTION.with(|xsc| *xsc.borrow_mut() = Some(server_connection));
// f()
// })
// .map(WaitHandle)
// .map_err(|_| crate::Error::InternalError)?)
}
pub fn wait_thread<T>(_joiner: WaitHandle<T>) -> crate::SysCallResult {
todo!()
// joiner
// .0
// .join()
// .map(|_| Result::Ok)
// .map_err(|_| crate::Error::InternalError)
}
pub fn process_to_args(call: usize, init: &ProcessInit) -> [usize; 8] {
@ -166,6 +159,129 @@ pub fn args_to_process(
todo!()
}
pub fn create_thread_0_pre<U>(f: &fn() -> U) -> core::result::Result<ThreadInit, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(*f) };
create_thread_n_pre(start, &0, &0, &0, &0)
}
pub fn create_thread_0_post<U>(
f: fn() -> U,
thread_id: TID,
) -> core::result::Result<WaitHandle<U>, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(f) };
create_thread_n_post(start, 0, 0, 0, 0, thread_id)
}
pub fn create_thread_1_pre<U>(
f: &fn(usize) -> U,
arg1: &usize,
) -> core::result::Result<ThreadInit, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(*f) };
create_thread_n_pre(start, arg1, &0, &0, &0)
}
pub fn create_thread_1_post<U>(
f: fn(usize) -> U,
arg1: usize,
thread_id: TID,
) -> core::result::Result<WaitHandle<U>, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(f) };
create_thread_n_post(start, arg1, 0, 0, 0, thread_id)
}
pub fn create_thread_2_pre<U>(
f: &fn(usize) -> U,
arg1: &usize,
arg2: &usize,
) -> core::result::Result<ThreadInit, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(*f) };
create_thread_n_pre(start, arg1, arg2, &0, &0)
}
pub fn create_thread_2_post<U>(
f: fn(usize) -> U,
arg1: usize,
arg2: usize,
thread_id: TID,
) -> core::result::Result<WaitHandle<U>, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(f) };
create_thread_n_post(start, arg1, arg2, 0, 0, thread_id)
}
pub fn create_thread_3_pre<U>(
f: &fn(usize) -> U,
arg1: &usize,
arg2: &usize,
arg3: &usize,
) -> core::result::Result<ThreadInit, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(*f) };
create_thread_n_pre(start, arg1, arg2, arg3, &0)
}
pub fn create_thread_3_post<U>(
f: fn(usize) -> U,
arg1: usize,
arg2: usize,
arg3: usize,
thread_id: TID,
) -> core::result::Result<WaitHandle<U>, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(f) };
create_thread_n_post(start, arg1, arg2, arg3, 0, thread_id)
}
pub fn create_thread_4_pre<U>(
f: &fn(usize) -> U,
arg1: &usize,
arg2: &usize,
arg3: &usize,
arg4: &usize,
) -> core::result::Result<ThreadInit, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(*f) };
create_thread_n_pre(start, arg1, arg2, arg3, arg4)
}
pub fn create_thread_4_post<U>(
f: fn(usize) -> U,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
thread_id: TID,
) -> core::result::Result<WaitHandle<U>, crate::Error>
where
U: Send + 'static,
{
let start = unsafe { core::mem::transmute(f) };
create_thread_n_post(start, arg1, arg2, arg3, arg4, thread_id)
}
pub fn create_thread_simple_pre<T, U>(
f: &fn(T) -> U,
arg: &T,
@ -174,27 +290,37 @@ where
T: Send + 'static,
U: Send + 'static,
{
#[cfg(feature = "bit-flags")]
let flags = crate::MemoryFlags::R | crate::MemoryFlags::W | crate::MemoryFlags::RESERVE;
#[cfg(not(feature = "bit-flags"))]
let flags = 0b0000_0010 | 0b0000_0100 | 0b0000_0001;
let stack = crate::map_memory(None, None, 131_072, flags)?;
let start = unsafe { core::mem::transmute(*f) };
let arg = unsafe { core::mem::transmute(arg) };
Ok(ThreadInit::new(start, stack, Some(arg), [0; 12]))
create_thread_n_pre(
unsafe { core::mem::transmute(*f) },
unsafe { core::mem::transmute(arg) },
&0,
&0,
&0,
)
}
pub fn create_thread_simple_post<T, U>(
_f: fn(T) -> U,
_arg: T,
_thread_id: TID,
f: fn(T) -> U,
arg: T,
thread_id: TID,
) -> core::result::Result<WaitHandle<U>, crate::Error>
where
T: Send + 'static,
U: Send + 'static,
{
Ok(WaitHandle(core::marker::PhantomData))
create_thread_n_post(
unsafe { core::mem::transmute(f) },
unsafe { core::mem::transmute(&arg) },
0,
0,
0,
thread_id,
)
// If we succeeded, the variable will be moved into the caller. Drop it from here.
.and_then(|f| {
core::mem::forget(arg);
Ok(f)
})
}
/// If no connection exists, create a new connection to the server. This means
@ -211,6 +337,36 @@ pub fn create_process_post(
todo!()
}
pub fn create_thread_n_pre(
start: usize,
arg1: &usize,
arg2: &usize,
arg3: &usize,
arg4: &usize,
) -> core::result::Result<ThreadInit, crate::Error> {
#[cfg(feature = "bit-flags")]
let flags = crate::MemoryFlags::R | crate::MemoryFlags::W | crate::MemoryFlags::RESERVE;
#[cfg(not(feature = "bit-flags"))]
let flags = 0b0000_0010 | 0b0000_0100 | 0b0000_0001;
let stack = crate::map_memory(None, None, 131_072, flags)?;
Ok(ThreadInit::new(start, stack, *arg1, *arg2, *arg3, *arg4))
}
pub fn create_thread_n_post<U>(
_f: usize,
_arg1: usize,
_arg2: usize,
_arg3: usize,
_arg4: usize,
_thread_id: TID,
) -> core::result::Result<WaitHandle<U>, crate::Error>
where
U: Send + 'static,
{
Ok(WaitHandle(core::marker::PhantomData))
}
pub fn wait_process(_joiner: ProcessHandle) -> crate::SysCallResult {
loop {
crate::wait_event();

View file

@ -1314,6 +1314,10 @@ pub fn wait_event() {
rsyscall(SysCall::WaitEvent).expect("wait_event returned an error");
}
#[deprecated(
since = "0.2",
note = "Please use create_thread_n() or create_thread()"
)]
pub fn create_thread_simple<T, U>(
f: fn(T) -> U,
arg: T,
@ -1332,6 +1336,96 @@ where
})
}
pub fn create_thread_0<T>(
f: fn() -> T,
) -> core::result::Result<crate::arch::WaitHandle<T>, Error>
where
T: Send + 'static,
{
let thread_info = crate::arch::create_thread_0_pre(&f)?;
rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| {
if let Result::ThreadID(thread_id) = result {
crate::arch::create_thread_0_post(f, thread_id)
} else {
Err(Error::InternalError)
}
})
}
pub fn create_thread_1<T>(
f: fn(usize) -> T,
arg1: usize,
) -> core::result::Result<crate::arch::WaitHandle<T>, Error>
where
T: Send + 'static,
{
let thread_info = crate::arch::create_thread_1_pre(&f, &arg1)?;
rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| {
if let Result::ThreadID(thread_id) = result {
crate::arch::create_thread_1_post(f, arg1, thread_id)
} else {
Err(Error::InternalError)
}
})
}
pub fn create_thread_2<T>(
f: fn(usize) -> T,
arg1: usize,
arg2: usize,
) -> core::result::Result<crate::arch::WaitHandle<T>, Error>
where
T: Send + 'static,
{
let thread_info = crate::arch::create_thread_2_pre(&f, &arg1, &arg2)?;
rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| {
if let Result::ThreadID(thread_id) = result {
crate::arch::create_thread_2_post(f, arg1, arg2, thread_id)
} else {
Err(Error::InternalError)
}
})
}
pub fn create_thread_3<T>(
f: fn(usize) -> T,
arg1: usize,
arg2: usize,
arg3: usize,
) -> core::result::Result<crate::arch::WaitHandle<T>, Error>
where
T: Send + 'static,
{
let thread_info = crate::arch::create_thread_3_pre(&f, &arg1, &arg2, &arg3)?;
rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| {
if let Result::ThreadID(thread_id) = result {
crate::arch::create_thread_3_post(f, arg1, arg2, arg3, thread_id)
} else {
Err(Error::InternalError)
}
})
}
pub fn create_thread_4<T>(
f: fn(usize) -> T,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
) -> core::result::Result<crate::arch::WaitHandle<T>, Error>
where
T: Send + 'static,
{
let thread_info = crate::arch::create_thread_4_pre(&f, &arg1, &arg2, &arg3, &arg4)?;
rsyscall(SysCall::CreateThread(thread_info)).and_then(|result| {
if let Result::ThreadID(thread_id) = result {
crate::arch::create_thread_4_post(f, arg1, arg2, arg3, arg4, thread_id)
} else {
Err(Error::InternalError)
}
})
}
/// Create a new thread with the given closure.
pub fn create_thread<F, T>(f: F) -> core::result::Result<crate::arch::WaitHandle<T>, Error>
where