mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
kernel: implement CreateProcess for riscv
Implement a functioning CreateProcess syscall on riscv. This call creates a server in the child process, and connects the server to the parent. The parent can communicate with the child process using this server. The Server ID is passed as four `u32` values to the entrypoint function. This can be reassembled into something that can then be passed to the `ReceiveMessage` syscall. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
806a31974a
commit
966d0aeb8a
6 changed files with 88 additions and 90 deletions
|
|
@ -1,9 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2020 Sean Cross <sean@xobs.io>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::{arch::mem, mem::MemoryManager};
|
||||
use crate::mem::MemoryManager;
|
||||
use core::fmt;
|
||||
use core::num::NonZeroUsize;
|
||||
use riscv::register::satp;
|
||||
use xous_kernel::{MemoryFlags, PID};
|
||||
|
||||
|
|
@ -14,7 +13,6 @@ pub const DEFAULT_BASE: usize = 0x6000_0000;
|
|||
|
||||
pub const USER_AREA_END: usize = 0xff00_0000;
|
||||
pub const EXCEPTION_STACK_TOP: usize = 0xffff_0000;
|
||||
pub const WORD_SIZE: usize = core::mem::size_of::<usize>();
|
||||
pub const PAGE_SIZE: usize = 4096;
|
||||
pub const PAGE_TABLE_OFFSET: usize = 0xff40_0000;
|
||||
pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000;
|
||||
|
|
@ -23,7 +21,7 @@ pub const THREAD_CONTEXT_AREA: usize = 0xff80_1000;
|
|||
pub const FLG_VALID: usize = 0x1;
|
||||
pub const FLG_R: usize = 0x2;
|
||||
pub const FLG_W: usize = 0x4;
|
||||
pub const FLG_X: usize = 0x8;
|
||||
// pub const FLG_X: usize = 0x8;
|
||||
pub const FLG_U: usize = 0x10;
|
||||
pub const FLG_A: usize = 0x40;
|
||||
pub const FLG_D: usize = 0x80;
|
||||
|
|
@ -32,10 +30,6 @@ extern "C" {
|
|||
fn flush_mmu();
|
||||
}
|
||||
|
||||
struct PageTable {
|
||||
entries: [usize; PAGE_SIZE / WORD_SIZE],
|
||||
}
|
||||
|
||||
pub unsafe fn memset(s: *mut u8, c: i32, n: usize) -> *mut u8 {
|
||||
let mut i = 0;
|
||||
while i < n {
|
||||
|
|
@ -166,78 +160,71 @@ impl MemoryMapping {
|
|||
return Err(xous_kernel::Error::MemoryInUse);
|
||||
}
|
||||
|
||||
let current_pid = crate::arch::process::current_pid();
|
||||
|
||||
crate::mem::MemoryManager::with_mut(|memory_manager| {
|
||||
// Address of the root pagetable
|
||||
let root_temp_virt = memory_manager.map_zeroed_page(pid, false)?;
|
||||
let root_temp_virt = memory_manager.map_zeroed_page(current_pid, false)?;
|
||||
let root_phys = super::mem::virt_to_phys(root_temp_virt as usize).unwrap() as usize;
|
||||
let root_virt = PAGE_TABLE_ROOT_OFFSET;
|
||||
let root_vpn0 = (root_virt as usize >> 12) & ((1 << 10) - 1);
|
||||
let root_ppn = ((root_phys >> 12) << 22) | FLG_VALID | FLG_R | FLG_W | FLG_D | FLG_A;
|
||||
let root_ppn = ((root_phys >> 12) << 10) | FLG_VALID | FLG_R | FLG_W | FLG_D | FLG_A;
|
||||
|
||||
// Superpage that points to all other pagetables
|
||||
let pages_l0_temp_virt = memory_manager.map_zeroed_page(pid, false)?;
|
||||
let pages_l0_temp_virt = memory_manager.map_zeroed_page(current_pid, false)?;
|
||||
let pages_l0_virt = PAGE_TABLE_OFFSET + 4096 * 1021;
|
||||
let pages_l0_phys = super::mem::virt_to_phys(pages_l0_temp_virt as usize)? as usize;
|
||||
let pages_l0_vpn0 = (pages_l0_virt as usize >> 12) & ((1 << 10) - 1);
|
||||
let pages_l0_ppn =
|
||||
((pages_l0_phys >> 12) << 22) | FLG_VALID | FLG_R | FLG_W | FLG_D | FLG_A;
|
||||
((pages_l0_phys >> 12) << 10) | FLG_VALID | FLG_R | FLG_W | FLG_D | FLG_A;
|
||||
|
||||
// Superpage that points to process-specific pages
|
||||
let process_l0_temp_virt = memory_manager.map_zeroed_page(pid, false)?;
|
||||
let process_l0_temp_virt = memory_manager.map_zeroed_page(current_pid, false)?;
|
||||
let process_l0_virt = PAGE_TABLE_OFFSET + 4096 * 1022;
|
||||
let process_l0_phys = super::mem::virt_to_phys(process_l0_temp_virt as usize)? as usize;
|
||||
let process_l0_vpn0 = (process_l0_virt as usize >> 12) & ((1 << 10) - 1);
|
||||
let process_l0_ppn =
|
||||
((process_l0_phys >> 12) << 22) | FLG_VALID | FLG_R | FLG_W | FLG_D | FLG_A;
|
||||
((process_l0_phys >> 12) << 10) | FLG_VALID | FLG_R | FLG_W | FLG_D | FLG_A;
|
||||
|
||||
// Context switch information containing all thread information.
|
||||
let context_temp_virt = memory_manager.map_zeroed_page(pid, false)?;
|
||||
let context_temp_virt = memory_manager.map_zeroed_page(current_pid, false)?;
|
||||
let context_virt = THREAD_CONTEXT_AREA;
|
||||
let context_phys = super::mem::virt_to_phys(context_temp_virt as usize)? as usize;
|
||||
let context_vpn0 = (context_virt as usize >> 12) & ((1 << 10) - 1);
|
||||
let context_ppn =
|
||||
((context_phys >> 12) << 22) | FLG_VALID | FLG_R | FLG_W | FLG_D | FLG_A;
|
||||
((context_phys >> 12) << 10) | FLG_VALID | FLG_R | FLG_W | FLG_D | FLG_A;
|
||||
|
||||
// Map the kernel into the new process mapping so we can continue
|
||||
// execution when it is activated. We can copy this value from our
|
||||
// current pagetable mapping.
|
||||
let krn_pg1023_ptr =
|
||||
unsafe { (PAGE_TABLE_ROOT_OFFSET as *const usize).add(1023).read() };
|
||||
unsafe { root_temp_virt.add(1023).write_volatile(krn_pg1023_ptr) };
|
||||
let krn_pg1023_ptr = (PAGE_TABLE_ROOT_OFFSET as *const usize)
|
||||
.add(1023)
|
||||
.read_volatile();
|
||||
root_temp_virt.add(1023).write_volatile(krn_pg1023_ptr);
|
||||
|
||||
// Map the process superpage into itself.
|
||||
unsafe {
|
||||
root_temp_virt
|
||||
.add(1022)
|
||||
.write((process_l0_phys >> 12) << 10 | FLG_VALID)
|
||||
};
|
||||
root_temp_virt
|
||||
.add(PAGE_TABLE_ROOT_OFFSET >> 22)
|
||||
.write_volatile((process_l0_phys >> 12) << 10 | FLG_VALID);
|
||||
|
||||
// Map the pagetable superpage into itself.
|
||||
unsafe {
|
||||
root_temp_virt
|
||||
.add(1021)
|
||||
.write((pages_l0_phys >> 12) << 10 | FLG_VALID)
|
||||
};
|
||||
root_temp_virt
|
||||
.add(PAGE_TABLE_OFFSET >> 22)
|
||||
.write_volatile((pages_l0_phys >> 12) << 10 | FLG_VALID);
|
||||
|
||||
// Map the root pagetable and the context page into the new process
|
||||
unsafe { process_l0_temp_virt.add(root_vpn0).write_volatile(root_ppn) };
|
||||
unsafe {
|
||||
process_l0_temp_virt
|
||||
.add(context_vpn0)
|
||||
.write_volatile(context_ppn)
|
||||
};
|
||||
process_l0_temp_virt.add(root_vpn0).write_volatile(root_ppn);
|
||||
process_l0_temp_virt
|
||||
.add(context_vpn0)
|
||||
.write_volatile(context_ppn);
|
||||
|
||||
// Add the the pagetable superpage to the l0 pagetable.
|
||||
unsafe {
|
||||
pages_l0_temp_virt
|
||||
.add(process_l0_vpn0)
|
||||
.write_volatile(process_l0_ppn)
|
||||
};
|
||||
unsafe {
|
||||
pages_l0_temp_virt
|
||||
.add(pages_l0_vpn0)
|
||||
.write_volatile(pages_l0_ppn)
|
||||
};
|
||||
pages_l0_temp_virt
|
||||
.add(process_l0_vpn0)
|
||||
.write_volatile(process_l0_ppn);
|
||||
pages_l0_temp_virt
|
||||
.add(pages_l0_vpn0)
|
||||
.write_volatile(pages_l0_ppn);
|
||||
|
||||
memory_manager.move_page_raw(root_phys as *mut usize, pid)?;
|
||||
memory_manager.move_page_raw(pages_l0_phys as *mut usize, pid)?;
|
||||
|
|
@ -909,7 +896,7 @@ pub fn ensure_page_exists_inner(address: usize) -> Result<usize, xous_kernel::Er
|
|||
// Map the page to our process
|
||||
*entry = (ppn1 << 20)
|
||||
| (ppn0 << 10)
|
||||
| (flags | (1 << 0) /* valid */ | (1 << 6) /* D */ | (1 << 7)/* A */);
|
||||
| (flags | FLG_VALID /* valid */ | FLG_D /* D */ | FLG_A/* A */);
|
||||
flush_mmu();
|
||||
|
||||
// Zero-out the page
|
||||
|
|
@ -918,7 +905,7 @@ pub fn ensure_page_exists_inner(address: usize) -> Result<usize, xous_kernel::Er
|
|||
// Move the page into userspace
|
||||
*entry = (ppn1 << 20)
|
||||
| (ppn0 << 10)
|
||||
| (flags | (1 << 0) /* valid */ | (1 << 4) /* USER */ | (1 << 6) /* D */ | (1 << 7)/* A */);
|
||||
| (flags | FLG_VALID /* valid */ | FLG_U /* USER */ | FLG_D /* D */ | FLG_A/* A */);
|
||||
flush_mmu();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ impl Process {
|
|||
pub fn current() -> 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);
|
||||
assert_eq!((pid.get() as usize), hardware_pid);
|
||||
Process { pid }
|
||||
}
|
||||
|
||||
|
|
@ -286,6 +286,11 @@ impl Process {
|
|||
let mut process = unsafe { &mut *PROCESS };
|
||||
let tid = INITIAL_TID;
|
||||
|
||||
assert_eq!(
|
||||
pid,
|
||||
crate::arch::current_pid(),
|
||||
"hardware pid does not match setup pid"
|
||||
);
|
||||
assert!(tid != IRQ_TID, "tried to init using the irq thread");
|
||||
assert!(
|
||||
mem::size_of::<ProcessImpl>() == PAGE_SIZE,
|
||||
|
|
@ -328,7 +333,6 @@ impl Process {
|
|||
*thread = Default::default();
|
||||
}
|
||||
|
||||
let process = unsafe { &mut *PROCESS };
|
||||
let mut thread = &mut process.threads[tid];
|
||||
|
||||
thread.sepc = unsafe { core::mem::transmute::<_, usize>(thread_init.call) };
|
||||
|
|
@ -361,7 +365,7 @@ impl Process {
|
|||
memory_manager
|
||||
.reserve_range(
|
||||
init_sp as *mut u8,
|
||||
stack_size + 4096,
|
||||
stack_size,
|
||||
xous_kernel::MemoryFlags::R | xous_kernel::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't reserve stack")
|
||||
|
|
@ -508,12 +512,12 @@ impl Process {
|
|||
services: &mut crate::SystemServices,
|
||||
) -> Result<ProcessStartup, xous_kernel::Error> {
|
||||
let current_pid = current_pid();
|
||||
let (server_id, cid) = services.create_server(pid)?;
|
||||
services.disconnect_from_server(cid).ok();
|
||||
let cid = services.connect_process_to_server(current_pid, server_id)?;
|
||||
|
||||
services.get_process(pid)?.mapping.activate()?;
|
||||
let server_id = services.create_server_id()?;
|
||||
let server_id_array = server_id.to_array();
|
||||
|
||||
klog!("Previous process init was {:x?}", init_data);
|
||||
// klog!("previous process init was {:x?}", init_data);
|
||||
let initial_thread = ThreadInit::new(
|
||||
init_data.start.get(),
|
||||
init_data.stack,
|
||||
|
|
@ -522,11 +526,15 @@ impl Process {
|
|||
server_id_array[2] as _,
|
||||
server_id_array[3] as _,
|
||||
);
|
||||
|
||||
services.get_process(pid)?.mapping.activate()?;
|
||||
|
||||
Self::setup_process(pid, initial_thread).unwrap();
|
||||
klog!("Activating existing process {}", current_pid.get());
|
||||
|
||||
services.create_server_with_address(pid, server_id, false)?;
|
||||
|
||||
// klog!("activating parent process {}", current_pid.get());
|
||||
services.get_process(current_pid)?.mapping.activate()?;
|
||||
// klog!("connecting to server in parent process");
|
||||
let cid = services.connect_process_to_server(current_pid, server_id)?;
|
||||
|
||||
services.send_memory(
|
||||
init_data.text.as_ptr() as *mut usize,
|
||||
|
|
|
|||
|
|
@ -455,7 +455,7 @@ impl MemoryManager {
|
|||
if is_user {
|
||||
crate::arch::mem::hand_page_to_user(virt as _)?;
|
||||
}
|
||||
// println!(
|
||||
// klog!(
|
||||
// "Mapped {:08x} -> {:08x} (user? {})",
|
||||
// phys as usize, virt as usize, is_user
|
||||
// );
|
||||
|
|
@ -684,7 +684,7 @@ impl MemoryManager {
|
|||
) -> Result<(), xous_kernel::Error> {
|
||||
if let Some(current_pid) = *owner_addr {
|
||||
if current_pid != pid {
|
||||
// println!(
|
||||
// klog!(
|
||||
// "In claim_or_release({}, {}, {:?}) -- addr is owned by {} not {}",
|
||||
// owner_addr.map(|v| v.get()).unwrap_or_default(),
|
||||
// pid,
|
||||
|
|
|
|||
|
|
@ -976,7 +976,7 @@ impl Server {
|
|||
original_address: Option<MemoryAddress>,
|
||||
) -> core::result::Result<usize, xous_kernel::Error> {
|
||||
// klog!(
|
||||
// "Queueing message: {:?} for pid: {} tid: {}",
|
||||
// "Queueing message: {:?} from pid: {} tid: {}",
|
||||
// message,
|
||||
// pid.get(),
|
||||
// tid
|
||||
|
|
|
|||
|
|
@ -419,12 +419,16 @@ impl SystemServices {
|
|||
return Err(xous_kernel::Error::ProcessNotFound);
|
||||
}
|
||||
let new_pid = new_pid.unwrap();
|
||||
let startup = arch::process::Process::create(new_pid, init_process, self)?;
|
||||
let mut entry = self.processes[entry_idx.unwrap()];
|
||||
println!(
|
||||
"Creating new process for PID {} with PPID {}",
|
||||
new_pid, ppid
|
||||
);
|
||||
let startup = arch::process::Process::create(new_pid, init_process, self).unwrap();
|
||||
|
||||
let mut entry = &mut self.processes[entry_idx.unwrap()];
|
||||
// The `Process::create()` call above set up the process so that it will
|
||||
// be ready to run right away, meaning we will not need to first set
|
||||
// the state to `ProcessState::Allocated` and we can go straight to running
|
||||
// this process.
|
||||
entry.state = ProcessState::Ready(1 << INITIAL_TID);
|
||||
// entry.ppid = ppid;
|
||||
klog!("created new process for PID {} with PPID {}", new_pid, ppid);
|
||||
return Ok(startup);
|
||||
}
|
||||
|
||||
|
|
@ -1663,10 +1667,11 @@ impl SystemServices {
|
|||
&mut self,
|
||||
pid: PID,
|
||||
sid: SID,
|
||||
connect: bool,
|
||||
) -> Result<(SID, CID), xous_kernel::Error> {
|
||||
// println!(
|
||||
// "KERNEL({}): Looking through server list for free server",
|
||||
// self.pid.get()
|
||||
// klog!(
|
||||
// "looking through server list for free server, connect? {}",
|
||||
// connect
|
||||
// );
|
||||
|
||||
// TODO: Come up with a way to randomize the server ID
|
||||
|
|
@ -1679,7 +1684,7 @@ impl SystemServices {
|
|||
}
|
||||
|
||||
for entry in self.servers.iter_mut() {
|
||||
if entry == &None {
|
||||
if *entry == None {
|
||||
#[cfg(baremetal)]
|
||||
// Allocate a single page for the server queue
|
||||
let backing = crate::mem::MemoryManager::with_mut(|mm| unsafe {
|
||||
|
|
@ -1691,17 +1696,16 @@ impl SystemServices {
|
|||
|
||||
#[cfg(not(baremetal))]
|
||||
let backing = unsafe { MemoryRange::new(4096, 4096).unwrap() };
|
||||
// println!(
|
||||
// "KERNEL({}): Found a free slot for server {:?} @ {} -- allocating an entry",
|
||||
// pid.get(),
|
||||
// sid,
|
||||
// _idx,
|
||||
// );
|
||||
|
||||
// klog!("initializing new server with backing at {:x?} -- entry is {:?} (connect? {:?})", backing, *entry, connect);
|
||||
// Initialize the server with the given memory page.
|
||||
Server::init(entry, pid, sid, backing).map_err(|x| x)?;
|
||||
Server::init(entry, pid, sid, backing).unwrap();
|
||||
|
||||
let cid = self.connect_to_server(sid)?;
|
||||
let cid = if connect {
|
||||
self.connect_to_server(sid)?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
return Ok((sid, cid));
|
||||
}
|
||||
}
|
||||
|
|
@ -1719,14 +1723,13 @@ impl SystemServices {
|
|||
/// queue.
|
||||
/// * **ServerNotFound**: The server queue was full and a free slot could not
|
||||
/// be found.
|
||||
pub fn create_server(&mut self, pid: PID) -> Result<(SID, CID), xous_kernel::Error> {
|
||||
let sid = SID::from_u32(
|
||||
arch::rand::get_u32(),
|
||||
arch::rand::get_u32(),
|
||||
arch::rand::get_u32(),
|
||||
arch::rand::get_u32(),
|
||||
);
|
||||
self.create_server_with_address(pid, sid)
|
||||
pub fn create_server(
|
||||
&mut self,
|
||||
pid: PID,
|
||||
connect: bool,
|
||||
) -> Result<(SID, CID), xous_kernel::Error> {
|
||||
let sid = self.create_server_id()?;
|
||||
self.create_server_with_address(pid, sid, connect)
|
||||
}
|
||||
|
||||
/// Generate a random server ID and return it to the caller. Doesn't create
|
||||
|
|
@ -1980,7 +1983,7 @@ impl SystemServices {
|
|||
&mut self,
|
||||
sidx: usize,
|
||||
pid: PID,
|
||||
context: TID,
|
||||
thread: TID,
|
||||
message: Message,
|
||||
original_address: Option<MemoryAddress>,
|
||||
) -> Result<usize, xous_kernel::Error> {
|
||||
|
|
@ -1997,7 +2000,7 @@ impl SystemServices {
|
|||
let server = self
|
||||
.server_from_sidx_mut(sidx)
|
||||
.expect("couldn't re-discover server index");
|
||||
server.queue_message(pid, context, message, original_address)
|
||||
server.queue_message(pid, thread, message, original_address)
|
||||
};
|
||||
let current_process = self
|
||||
.get_process(current_pid)
|
||||
|
|
|
|||
|
|
@ -806,11 +806,11 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
|
|||
.map(xous_kernel::Result::NewProcess)
|
||||
}),
|
||||
SysCall::CreateServerWithAddress(name) => SystemServices::with_mut(|ss| {
|
||||
ss.create_server_with_address(pid, name)
|
||||
ss.create_server_with_address(pid, name, true)
|
||||
.map(|(sid, cid)| xous_kernel::Result::NewServerID(sid, cid))
|
||||
}),
|
||||
SysCall::CreateServer => SystemServices::with_mut(|ss| {
|
||||
ss.create_server(pid)
|
||||
ss.create_server(pid, true)
|
||||
.map(|(sid, cid)| xous_kernel::Result::NewServerID(sid, cid))
|
||||
}),
|
||||
SysCall::CreateServerId => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue