From 1b59719a74c5cc7f423487a769745541c4a62d63 Mon Sep 17 00:00:00 2001 From: Sean Date: Thu, 3 Sep 2020 13:18:03 +0800 Subject: [PATCH] kernel: hosted: support per-thread memory messages Previously, each process could return one chunk of memory. With this change, processes can now return one chunk of memory per thread. This enables multiple threads to lend memory to a server. Signed-off-by: Sean --- kernel/src/arch/hosted/process.rs | 43 ++++++++++++++++++++++++------- kernel/src/services.rs | 6 ++++- kernel/src/syscall.rs | 4 ++- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/kernel/src/arch/hosted/process.rs b/kernel/src/arch/hosted/process.rs index 324fa353f..3ac462e2f 100644 --- a/kernel/src/arch/hosted/process.rs +++ b/kernel/src/arch/hosted/process.rs @@ -24,8 +24,8 @@ struct ProcessImpl { /// The network connection to the client process. conn: Option, - /// Memory that may need to be returned to the caller - memory_to_return: Option>, + /// Memory that may need to be returned to the caller for each thread + memory_to_return: [Option>; MAX_THREAD + 1], /// This enables the kernel to keep track of threads in the /// target process, and know which threads are ready to @@ -87,7 +87,10 @@ pub fn set_current_pid(pid: PID) { }); } -pub fn register_connection_for_key(conn: TcpStream, key: ProcessKey) -> Result { +pub fn register_connection_for_key( + conn: TcpStream, + key: ProcessKey, +) -> Result { PROCESS_TABLE.with(|pt| { let mut process_table = pt.borrow_mut(); for (pid_minus_1, process) in process_table.table.iter_mut().enumerate() { @@ -181,7 +184,11 @@ impl Process { tmp.setup_thread(INITIAL_TID, setup) } - pub fn setup_thread(&mut self, thread: TID, _setup: ThreadInit) -> Result<(), xous_kernel::Error> { + pub fn setup_thread( + &mut self, + thread: TID, + _setup: ThreadInit, + ) -> Result<(), xous_kernel::Error> { // println!( // "KERNEL({}): Setting up thread {} @ {:?}", // self.pid, @@ -256,15 +263,30 @@ impl Process { ); let mut response = vec![]; + // Add the destination thread ID to the start of the packet. response.extend_from_slice(&tid.to_le_bytes()); + + // Append the contents of the response packet. for word in result.to_args().iter_mut() { response.extend_from_slice(&word.to_le_bytes()); } - if let Some(buf) = process.memory_to_return.take() { + // If there is memory to return for this thread, also return that. + if let Some(buf) = process.memory_to_return[tid - 1].take() { + // if let xous_kernel::Result::Message(_) = result { + // } else { + // panic!( + // "memory was waiting to be returned, but message was not a result message" + // ); + // } response.extend_from_slice(&buf); } + // eprintln!( + // "KERNEL({}): Setting thread return value to {:?}", + // current_pid_idx + 1, + // response + // ); process .conn .as_mut() @@ -274,13 +296,13 @@ impl Process { }); } - pub fn return_memory(&mut self, buf: &[u8]) { + pub fn return_memory(&mut self, tid: TID, buf: &[u8]) { PROCESS_TABLE.with(|pt| { let mut process_table = pt.borrow_mut(); let current_pid_idx = process_table.current.get() as usize - 1; let process = &mut process_table.table[current_pid_idx].as_mut().unwrap(); - assert!(process.memory_to_return.is_none()); - process.memory_to_return = Some(buf.to_vec()); + assert!(process.memory_to_return[tid - 1].is_none()); + process.memory_to_return[tid - 1] = Some(buf.to_vec()); }); } @@ -291,12 +313,12 @@ impl Process { PROCESS_TABLE.with(|process_table| { let mut process_table = process_table.borrow_mut(); let pid_idx = (pid.get() - 1) as usize; - + use crate::filled_array; let process = ProcessImpl { inner: Default::default(), conn: None, key: init_data.key, - memory_to_return: None, + memory_to_return: filled_array![None; 32 /* MAX_THREAD */], current_thread: INITIAL_TID, threads: [Thread { allocated: false }; MAX_THREAD + 1], }; @@ -334,6 +356,7 @@ impl Process { } pub fn send(&mut self, bytes: &[u8]) -> Result<(), xous_kernel::Error> { + // eprintln!("KERNEL: Sending syscall response: {:?}", bytes); PROCESS_TABLE.with(|pt| { let mut process_table = pt.borrow_mut(); let current_pid_idx = process_table.current.get() as usize - 1; diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 631fbc3ab..4cff1ff9f 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -1164,7 +1164,9 @@ impl SystemServices { pub fn return_memory( &mut self, src_virt: *mut u8, + _src_tid: TID, dest_pid: PID, + _dest_tid: TID, dest_virt: *mut u8, len: usize, ) -> Result<*mut u8, xous_kernel::Error> { @@ -1212,7 +1214,9 @@ impl SystemServices { pub fn return_memory( &mut self, src_virt: *mut u8, + _src_tid: TID, dest_pid: PID, + dest_tid: TID, _dest_virt: *mut u8, len: usize, ) -> Result<*mut u8, xous_kernel::Error> { @@ -1222,7 +1226,7 @@ impl SystemServices { let target_process = self.get_process(dest_pid)?; target_process.activate()?; let mut arch_process = crate::arch::process::Process::current(); - arch_process.return_memory(buf); + arch_process.return_memory(dest_tid, buf); } let target_process = self.get_process(current_pid)?; target_process.activate()?; diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 15b8e537f..7187a9677 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -181,7 +181,7 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes }) } -fn return_memory(pid: PID, _tid: TID, sender: MessageSender, buf: MemoryRange) -> SysCallResult { +fn return_memory(pid: PID, tid: TID, sender: MessageSender, buf: MemoryRange) -> SysCallResult { SystemServices::with_mut(|ss| { let sender = SenderID::from_usize(sender)?; @@ -239,7 +239,9 @@ fn return_memory(pid: PID, _tid: TID, sender: MessageSender, buf: MemoryRange) - // Return the memory to the calling process ss.return_memory( server_addr.get() as _, + tid, client_pid, + client_tid, client_addr.get() as _, len.get(), )?;