spawn: compiles, but does not work yet

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2022-05-28 05:10:48 +08:00
parent d8eb7be352
commit 2e69ae8a12
11 changed files with 341 additions and 52 deletions

14
Cargo.lock generated
View file

@ -2689,6 +2689,13 @@ dependencies = [
"no-std-net",
]
[[package]]
name = "spawn"
version = "0.1.0"
dependencies = [
"xous",
]
[[package]]
name = "spectrum-analyzer"
version = "0.5.2"
@ -2881,6 +2888,13 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "test-spawn"
version = "0.1.0"
dependencies = [
"xous",
]
[[package]]
name = "test-stub"
version = "0.1.0"

View file

@ -94,6 +94,8 @@ members = [
"services/libstd-test",
"services/ffi-test",
"services/tts",
"test-spawn",
"spawn",
"kernel",
"loader",
]

View file

@ -35,8 +35,8 @@ gdbserver = ["gdbstub", "gdbstub_arch"]
print-panics = []
report-memory = ["stats_alloc"]
wrap-print = []
# default = ["print-panics", "debug-print", "wrap-print"]
default = ["print-panics", "gdbserver"]
default = ["print-panics", "debug-print", "wrap-print"]
# default = ["print-panics", "gdbserver"]
[target.'cfg(any(windows, unix))'.dependencies]
crossbeam-channel = "0.5"

View file

@ -1,8 +1,9 @@
// SPDX-FileCopyrightText: 2020 Sean Cross <sean@xobs.io>
// SPDX-License-Identifier: Apache-2.0
use crate::mem::MemoryManager;
use crate::{arch::mem, mem::MemoryManager};
use core::fmt;
use core::num::NonZeroUsize;
use riscv::register::satp;
use xous_kernel::{MemoryFlags, PID};
@ -13,14 +14,28 @@ 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;
const PAGE_TABLE_OFFSET: usize = 0xff40_0000;
const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000;
pub const PAGE_TABLE_OFFSET: usize = 0xff40_0000;
pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000;
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_U: usize = 0x10;
pub const FLG_A: usize = 0x40;
pub const FLG_D: usize = 0x80;
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 {
@ -105,6 +120,138 @@ impl MemoryMapping {
self.satp = satp;
}
/// Allocate a brand-new memory mapping. When this memory mapping is created,
/// it will be ready to use in a new process, however it will have no actual
/// program code. It will, however, have the following pages mapped:
///
/// 1. The kernel will be mapped to superpage 1023, meaning the kernel can
/// switch to this process and do things.
/// 2. A page will be allocated for superpage 1022, to contain pages for
/// process-specific code.
/// 3. A page will be allocated for superpage 1021, to contain pages for
/// managing pages.
/// 4. The root pagetable will be allocated and mapped at 0xff800000,
/// ensuring new superpages can be allocated.
/// 5. A context page will be allocated at 0xff801000, ensuring the
/// process can actually be run.
/// 6. Individual pagetable mappings are mapped at 0xff400000
/// At the end of this operation, the following mapping will take place. Note that
/// names are repeated in the chart below to indicate they are the same page
/// represented multiple times. Items in brackets are offsets (in `usize`-words)
/// from the start of the page. For example, offset 1023 on the root pagetable
/// (address 4092) contains an entry that points to the kernel superpage.
/// +----------------+
/// | Root Pagetable |
/// | root |
/// +----------------+
/// |
/// +---------------+-------------------+------------------+
/// | | |
/// [1021] [1022] [1023]
/// v v v
/// +--------------+ +--------------+ +--------+
/// | Level 0/1021 | | Level 0/1022 | | Kernel |
/// | pages_l0 | | process_l0 | | |
/// +--------------+ +--------------+ +--------+
/// | |
/// +-------+---------+ +---+-----------+
/// | | | |
/// [1021] [1022] [0] [1]
/// v v v v
/// +--------------+ +--------------+ +----------------+ +---------+
/// | Level 0/1021 | | Level 0/1022 | | Root Pagetable | | Context |
/// +--------------+ +--------------+ +----------------+ +---------+
pub unsafe fn allocate(&mut self, pid: PID) -> Result<(), xous_kernel::Error> {
if self.satp != 0 {
return Err(xous_kernel::Error::MemoryInUse);
}
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_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;
// Superpage that points to all other pagetables
let pages_l0_temp_virt = memory_manager.map_zeroed_page(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;
// Superpage that points to process-specific pages
let process_l0_temp_virt = memory_manager.map_zeroed_page(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;
// Context switch information containing all thread information.
let context_temp_virt = memory_manager.map_zeroed_page(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;
// 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) };
// Map the process superpage into itself.
unsafe {
root_temp_virt
.add(1022)
.write((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)
};
// 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)
};
// 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)
};
memory_manager.move_page_raw(root_phys as *mut usize, pid)?;
memory_manager.move_page_raw(pages_l0_phys as *mut usize, pid)?;
memory_manager.move_page_raw(process_l0_phys as *mut usize, pid)?;
memory_manager.move_page_raw(context_phys as *mut usize, pid)?;
// Construct a dummy SATP that we will use to hand memory to the new process.
self.satp = 0x8000_0000 | ((pid.get() as usize) << 22) | (root_phys as usize >> 12);
Ok(())
})?;
Ok(())
}
/// Get the currently active memory mapping. Note that the actual root pages
/// may be found at virtual address `PAGE_TABLE_ROOT_OFFSET`.
pub fn current() -> MemoryMapping {

View file

@ -7,9 +7,10 @@ pub const MAX_THREAD: TID = 31;
pub const EXCEPTION_TID: TID = 1;
pub const INITIAL_TID: TID = 2;
pub const IRQ_TID: TID = 0;
use crate::arch::mem::PAGE_SIZE;
use crate::services::ProcessInner;
use xous_kernel::{ProcessInit, ThreadInit, PID, TID, ProcessStartup};
use xous_kernel::{ProcessInit, ProcessStartup, ThreadInit, PID, TID};
// use crate::args::KernelArguments;
pub const DEFAULT_STACK_SIZE: usize = 131072;
@ -306,6 +307,7 @@ impl Process {
INITIAL_TID
);
klog!("Setting up new process {}", pid.get());
unsafe {
let pid_idx = (pid.get() as usize) - 1;
assert!(
@ -336,6 +338,8 @@ impl Process {
thread.registers[11] = thread_init.arg3;
thread.registers[12] = thread_init.arg4;
klog!("thread_init: {:x?} thread: {:x?}", thread_init, thread);
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
{
let pid = pid.get();
@ -497,8 +501,40 @@ impl Process {
);
}
pub fn create(_pid: PID, _init_data: ProcessInit) -> ProcessStartup {
todo!();
/// Create a brand-new process. The memory space must already be set up.
pub fn create(
pid: PID,
init_data: ProcessInit,
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)?;
let server_id_array = server_id.to_array();
klog!("Previous process init was {:x?}", init_data);
let initial_thread = ThreadInit::new(
init_data.start.get(),
init_data.stack,
server_id_array[0] as _,
server_id_array[1] as _,
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.get_process(current_pid)?.mapping.activate()?;
services.send_memory(
init_data.text.as_ptr() as *mut usize,
pid,
init_data.text_destination.get() as *mut usize,
init_data.text.len(),
)?;
Ok(ProcessStartup::new(pid, cid))
}
pub fn destroy(pid: PID) -> Result<(), xous_kernel::Error> {

View file

@ -568,6 +568,38 @@ impl MemoryManager {
)
}
#[allow(dead_code)]
/// Move the page in the process mapping listing without manipulating
/// the pagetables at all.
pub fn move_page_raw(
&mut self,
phys_addr: *mut usize,
dest_pid: PID,
) -> Result<(), xous_kernel::Error> {
self.claim_release_move(
phys_addr as *mut usize,
dest_pid,
ClaimReleaseMove::Move(crate::arch::process::current_pid()),
)
}
// /// Transfer ownership of a page without moving it. This is a very
// /// specialized function which should only be used when creating a
// /// brand-new process.
// pub fn move_page_ownership(
// &mut self,
// src_addr: *mut u8,
// src_pid: PID,
// dest_pid: PID,
// ) -> Result<(), xous_kernel::Error> {
// let phys_addr = crate::arch::mem::virt_to_phys(src_addr as usize)?;
// self.claim_release_move(
// phys_addr as *mut usize,
// dest_pid,
// ClaimReleaseMove::Move(src_pid),
// )
// }
/// Mark the page in the current process as being lent. If the borrow is
/// read-only, then additionally remove the "write" bit on it. If the page
/// is writable, then remove it from the current process until the borrow is

View file

@ -5,8 +5,8 @@ use crate::arch;
use crate::arch::mem::MemoryMapping;
pub use crate::arch::process::Process as ArchProcess;
pub use crate::arch::process::Thread;
use xous_kernel::MemoryRange;
use xous_kernel::arch::ProcessStartup;
use xous_kernel::MemoryRange;
use core::num::NonZeroU8;
@ -390,21 +390,42 @@ impl SystemServices {
/// Add a new entry to the process table. This results in a new address space
/// and a new PID, though the process is in the state `Setup()`.
pub fn create_process(&mut self, init_process: ProcessInit) -> Result<ProcessStartup, xous_kernel::Error> {
for (idx, mut entry) in self.processes.iter_mut().enumerate() {
pub fn create_process(
&mut self,
init_process: ProcessInit,
) -> Result<ProcessStartup, xous_kernel::Error> {
let mut entry_idx = None;
let mut new_pid = None;
let ppid = crate::arch::process::current_pid();
for (idx, entry) in self.processes.iter_mut().enumerate() {
if entry.state != ProcessState::Free {
continue;
}
let new_pid = pid_from_usize(idx + 1)?;
let startup = arch::process::Process::create(new_pid, init_process);
let ppid = crate::arch::process::current_pid();
// println!("Creating new process for PID {} with PPID {}", new_pid, ppid);
entry_idx = Some(idx);
new_pid = Some(pid_from_usize(idx + 1)?);
entry.pid = new_pid.unwrap();
entry.ppid = PID::new(1).unwrap();
entry.state = ProcessState::Allocated;
entry.ppid = ppid;
entry.pid = new_pid;
return Ok(startup);
unsafe {
entry
.mapping
.allocate(new_pid.unwrap())
.or(Err(xous_kernel::Error::InternalError))?
};
break;
}
Err(xous_kernel::Error::ProcessNotFound)
if entry_idx.is_none() {
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
);
return Ok(startup);
}
pub fn get_process(&self, pid: PID) -> Result<&Process, xous_kernel::Error> {
@ -2212,7 +2233,7 @@ impl SystemServices {
let process = self.get_process_mut(pid).ok()?;
let handler = process.exception_handler?;
process.state = match process.state {
ProcessState::Running(x) => ProcessState::Exception(x | 1<<process.current_thread),
ProcessState::Running(x) => ProcessState::Exception(x | 1 << process.current_thread),
ProcessState::Ready(x) => ProcessState::Exception(x),
_ => return None,
};

View file

@ -20,7 +20,6 @@ fn main() {
println!("cargo:rerun-if-changed=bin/{}.a", target);
println!("cargo:rustc-link-arg=-Tlink.x");
}
println!("cargo:rustc-link-arg=-Map=loader.map");
// Put the linker script somewhere the linker can find it
fs::File::create(out_dir.join("link.x"))

View file

@ -4,8 +4,14 @@ use core::convert::TryFrom;
/// will be turned into `ProcessInit` by this library prior to sending
/// them into the kernel.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ProcessArgs {
name: [u8; 16],
pub struct ProcessArgs<'a> {
stub: &'a [u8],
}
impl<'a> ProcessArgs<'a> {
pub fn new(stub: &[u8]) -> ProcessArgs {
ProcessArgs { stub }
}
}
/// ProcessInit describes the values that are passed to the
@ -14,12 +20,13 @@ pub struct ProcessArgs {
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct ProcessInit {
// 0,1 -- Stack Base, Stack Size
stack: crate::MemoryRange,
pub stack: crate::MemoryRange,
// 2,3 -- Text Start, Text Size
text: crate::MemoryRange,
// 4
start: crate::MemoryAddress,
// 5
pub text: crate::MemoryRange,
// 4 -- Text destination address
pub text_destination: crate::MemoryAddress,
// 5 -- Entrypoint
pub start: crate::MemoryAddress,
// 6
}
@ -30,9 +37,9 @@ impl Into<[usize; 7]> for &ProcessInit {
self.stack.size.get(),
self.text.addr.get(),
self.text.size.get(),
self.text_destination.get(),
self.start.get(),
0,
0,
]
}
}
@ -47,7 +54,8 @@ impl TryFrom<[usize; 7]> for ProcessInit {
text: unsafe {
crate::MemoryRange::new(src[2], src[3]).or(Err(crate::Error::OutOfMemory))?
},
start: crate::MemoryAddress::new(src[4]).ok_or(crate::Error::OutOfMemory)?,
text_destination: crate::MemoryAddress::new(src[4]).ok_or(crate::Error::OutOfMemory)?,
start: crate::MemoryAddress::new(src[5]).ok_or(crate::Error::OutOfMemory)?,
})
}
}
@ -58,45 +66,49 @@ pub struct ProcessStartup {
/// The process ID of the new process
pid: crate::PID,
/// A server that the parent process can connect to in order to send further
/// initialization messages
server: crate::SID,
/// A connection to the initial server that is running in the new process.
connection: crate::CID,
}
impl ProcessStartup {
pub fn new(pid: crate::PID, connection: crate::CID) -> Self {
ProcessStartup { pid, connection }
}
}
impl From<&[usize; 7]> for ProcessStartup {
fn from(src: &[usize; 7]) -> ProcessStartup {
let server = crate::SID::from_array([src[1] as _, src[2] as _, src[3] as _, src[4] as _]);
ProcessStartup {
pid: crate::PID::new(src[0] as _).unwrap(),
server,
connection: src[1] as _,
}
}
}
impl From<[usize; 8]> for ProcessStartup {
fn from(src: [usize; 8]) -> ProcessStartup {
let pid = crate::PID::new(src[1] as _).unwrap();
let server = crate::SID::from_array([src[2] as _, src[3] as _, src[4] as _, src[5] as _]);
ProcessStartup { pid, server }
let pid = match crate::PID::new(src[1] as _) {
Some(o) => o,
None => unsafe { crate::PID::new_unchecked(255) },
};
let cid = src[2] as _;
ProcessStartup {
pid,
connection: cid,
}
}
}
impl Into<[usize; 7]> for &ProcessStartup {
fn into(self) -> [usize; 7] {
let server = self.server.to_array();
[
self.pid.get() as _,
server[0] as _,
server[1] as _,
server[2] as _,
server[3] as _,
0,
0,
]
[self.pid.get() as _, self.connection as _, 0, 0, 0, 0, 0]
}
}
pub struct ProcessHandle(crate::PID);
pub struct ProcessHandle {
pub pid: crate::PID,
pub cid: crate::CID,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct ProcessKey([u8; 8]);
@ -108,8 +120,27 @@ impl ProcessKey {
/// Convert the `ProcessArgs` structure passed by the user into a `ProcessInit`
/// structure suitable for consumption by the kernel.
pub fn create_process_pre(_args: &ProcessArgs) -> core::result::Result<ProcessInit, crate::Error> {
todo!()
pub fn create_process_pre(args: &ProcessArgs) -> core::result::Result<ProcessInit, crate::Error> {
let spawn_stub_rounded = (args.stub.len() + 4096 - 1) & !(4096 - 1);
let mut spawn_memory = crate::map_memory(
None,
None,
spawn_stub_rounded,
crate::MemoryFlags::R | crate::MemoryFlags::W | crate::MemoryFlags::X,
)?;
for (dest, src) in spawn_memory.as_slice_mut().iter_mut().zip(args.stub) {
*dest = *src;
}
Ok(ProcessInit {
// 0,1 -- Stack Base, Stack Size
stack: unsafe { crate::MemoryRange::new(0x7000_0000, 131072)? },
// 2,3 -- Text Start, Text Size
text: spawn_memory,
// 4 -- Text destination address
text_destination: crate::MemoryAddress::new(0x2050_1000).unwrap(),
// 5 -- Entrypoint
start: crate::MemoryAddress::new(0x2050_1000).unwrap(),
})
}
/// Any post-processing required to set up this process.
@ -118,7 +149,10 @@ pub fn create_process_post(
_init: ProcessInit,
startup: ProcessStartup,
) -> core::result::Result<ProcessHandle, crate::Error> {
Ok(ProcessHandle(startup.pid))
Ok(ProcessHandle {
pid: startup.pid,
cid: startup.connection,
})
}
/// Wait for a process to terminate.

View file

View file

@ -45,6 +45,10 @@ impl Envelope {
unsafe { core::ptr::read(&manual_self.body) }
}
pub fn id(&self) -> MessageId {
self.body.id()
}
/// Take this message and forward it to another server.
///
/// **Note**: For blocking messages, this will block until the other server responds to