diff --git a/spawn/Cargo.toml b/spawn/Cargo.toml new file mode 100644 index 000000000..6609bb1c6 --- /dev/null +++ b/spawn/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "spawn" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +xous = "*" \ No newline at end of file diff --git a/spawn/README.md b/spawn/README.md new file mode 100644 index 000000000..4f55cbed3 --- /dev/null +++ b/spawn/README.md @@ -0,0 +1,30 @@ +# Spawn a process + +This crate represents the initial program that will run when a process +starts up. It will listen on a Server and take orders from the parent +process to finish setting itself up. + +## Initialization + +The process starts with a stack and a text section but no heap, BSS, +or data section. The entrypoint looks like this: + +```rust +pub extern "C" init(connection: xous::CID) -> ! { + loop {} +} +``` + +This process is responsible for performing its own bootstrapping, after +which it should accept messages on the connection and copy data as necessary. + +## API + +The following messages are supported: + +Mnemonic | Opcode | Type | Description +--- | ----- | ---- | ---- +WriteMemory|1| M | Write memory into an area of memory. The `Offset` field is used to determine where the block will start. +WriteArgs | 2 | M | Reserved +WriteEnvironment | 3 | M | Reserved +FinishSetup | 255 | * | Terminate the loop, shutdown the server, and start the program. \ No newline at end of file diff --git a/spawn/build.rs b/spawn/build.rs new file mode 100644 index 000000000..2fbc2e034 --- /dev/null +++ b/spawn/build.rs @@ -0,0 +1,33 @@ +// NOTE: Adapted from cortex-m/build.rs +use std::env; +use std::fs; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + let target = env::var("TARGET").unwrap(); + // let name = env::var("CARGO_PKG_NAME").unwrap(); + + if target.starts_with("riscv") { + // fs::copy( + // format!("bin/{}.a", target), + // out_dir.join(format!("lib{}.a", name)), + // ).unwrap(); + + // println!("cargo:rustc-link-lib=static={}", name); + // println!("cargo:rustc-link-search={}", out_dir.display()); + // println!("cargo:rerun-if-changed=bin/{}.a", target); + println!("cargo:rustc-link-arg=-Tlink.x"); + } + + // Put the linker script somewhere the linker can find it + fs::File::create(out_dir.join("link.x")) + .unwrap() + .write_all(include_bytes!("link.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out_dir.display()); + + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=link.x"); +} diff --git a/spawn/link.x b/spawn/link.x new file mode 100644 index 000000000..c24dd7e3d --- /dev/null +++ b/spawn/link.x @@ -0,0 +1,190 @@ +MEMORY +{ + RAM : ORIGIN = 0x40000000, LENGTH = 16M + FLASH : ORIGIN = 0x20501000, LENGTH = 188k + FONTS : ORIGIN = 0x20530000, LENGTH = 4352k + MEMLCD: ORIGIN = 0xB0000000, LENGTH = 32k +} +/* +Fonts go from 0x2053_0000 to 0x2097_0000 +*/ + +REGION_ALIAS("REGION_TEXT", FLASH); +REGION_ALIAS("REGION_RODATA", FLASH); +REGION_ALIAS("REGION_DATA", RAM); +REGION_ALIAS("REGION_BSS", RAM); +REGION_ALIAS("REGION_STACK", RAM); +REGION_ALIAS("REGION_HEAP", RAM); + +_lcdfb = ORIGIN(MEMLCD); +/* Size of the main kernel stack */ +_stack_size = 16K; +_eheap = ORIGIN(RAM) + LENGTH(RAM); + + +PROVIDE(_stext = ORIGIN(REGION_TEXT)); +PROVIDE(_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK)); +PROVIDE(_max_hart_id = 0); +PROVIDE(_hart_stack_size = 2K); +PROVIDE(_heap_size = 0); + +PROVIDE(trap_handler = default_trap_handler); + +/* # Pre-initialization function */ +/* If the user overrides this using the `#[pre_init]` attribute or by creating a `__pre_init` function, + then the function this points to will be called before the RAM is initialized. */ +PROVIDE(__pre_init = default_pre_init); + +/* # Multi-processing hook function + fn _mp_hook() -> bool; + + This function is called from all the harts and must return true only for one hart, + which will perform memory initialization. For other harts it must return false + and implement wake-up in platform-dependent way (e.g. after waiting for a user interrupt). +*/ +PROVIDE(_mp_hook = default_mp_hook); + +ENTRY(init) + +SECTIONS +{ + .text.dummy (NOLOAD) : + { + /* This section is intended to make _stext address work */ + . = _stext; + } > REGION_TEXT + + .text _stext : + { + /* Put reset handler first in .text section so it ends up as the entry */ + /* point of the program. */ + KEEP(*(.text.init)); + KEEP(*(.init)); + KEEP(*(.init.rust)); + . = ALIGN(4); + KEEP(*(.trap)); + KEEP(*(.trap.rust)); + + *(.text .text.*); + } > REGION_TEXT + + .rodata : ALIGN(4) + { + *(.rodata .rodata.*); + + /* 4-byte align the end (VMA) of this section. + This is required by LLD to ensure the LMA of the following .data + section will have the correct alignment. */ + . = ALIGN(4); + _etext = .; + } > REGION_RODATA + + .data : ALIGN(4) + { + _sidata = LOADADDR(.data); + _sdata = .; + /* Must be called __global_pointer$ for linker relaxations to work. */ + PROVIDE(__global_pointer$ = . + 0x800); + *(.sdata .sdata.* .sdata2 .sdata2.*); + *(.data .data.*); + . = ALIGN(4); + _edata = .; + } > REGION_DATA AT > REGION_RODATA + + .fonts ALIGN(65536) : SUBALIGN(65536) + { + KEEP(*(.fontdata)); + } > FONTS + + .bss (NOLOAD) : + { + _sbss = .; + *(.sbss .sbss.* .bss .bss.*); + . = ALIGN(4); + _ebss = .; + } > REGION_BSS + + /* fictitious region that represents the memory available for the stack */ + .stack (NOLOAD) : + { + _sstack = .; + . += _stack_size; + . = ALIGN(4096); + _estack = .; + } > REGION_STACK + + /* fictitious region that represents the memory available for the heap */ + .heap (NOLOAD) : + { + . = ALIGN(4); + _sheap = .; + /* _eheap is defined elsewhere and is the remainder of RAM */ + } > REGION_HEAP + + /* fake output .got section */ + /* Dynamic relocations are unsupported. This section is only used to detect + relocatable code in the input files and raise an error if relocatable code + is found */ + .got (INFO) : + { + KEEP(*(.got .got.*)); + } + + /* Discard .eh_frame, we are not doing unwind on panic so it is not needed */ + /DISCARD/ : + { + *(.eh_frame); + *(.eh_frame_hdr); + } +} + +/* Do not exceed this mark in the error messages above | */ +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_RODATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_RODATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_DATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_DATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_HEAP) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_HEAP must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_STACK) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_STACK must be 4-byte aligned"); + +ASSERT(_stext % 4 == 0, " +ERROR(riscv-rt): `_stext` must be 4-byte aligned"); + +ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, " +BUG(riscv-rt): .data is not 4-byte aligned"); + +ASSERT(_sidata % 4 == 0, " +BUG(riscv-rt): the LMA of .data is not 4-byte aligned"); + +ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, " +BUG(riscv-rt): .bss is not 4-byte aligned"); + +ASSERT(_sheap % 4 == 0, " +BUG(riscv-rt): start of .heap is not 4-byte aligned"); + +ASSERT(_stext + SIZEOF(.text) < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), " +ERROR(riscv-rt): The .text section must be placed inside the REGION_TEXT region. +Set _stext to an address smaller than 'ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT)'"); + +ASSERT(SIZEOF(.stack) > (_max_hart_id + 1) * _hart_stack_size, " +ERROR(riscv-rt): .stack section is too small for allocating stacks for all the harts. +Consider changing `_max_hart_id` or `_hart_stack_size`."); + +ASSERT(SIZEOF(.got) == 0, " +.got section detected in the input files. Dynamic relocations are not +supported. If you are linking to C code compiled using the `gcc` crate +then modify your build script to compile the C code _without_ the +-fPIC flag. See the documentation of the `gcc::Config.fpic` method for +details."); + +/* Do not exceed this mark in the error messages above | */ diff --git a/spawn/src/main.rs b/spawn/src/main.rs new file mode 100644 index 000000000..9f7703cc9 --- /dev/null +++ b/spawn/src/main.rs @@ -0,0 +1,87 @@ +#![no_std] +#![no_main] + +enum StartupCommand { + Unhandled = 0, + WriteMemory = 1, + WriteArgs = 2, + WriteEnvironment = 3, + PingResponse = 4, + FinishStartup = 255, +} + +impl From for StartupCommand { + fn from(src: xous::MessageId) -> StartupCommand { + match src { + 1 => StartupCommand::WriteMemory, + 2 => StartupCommand::WriteArgs, + 3 => StartupCommand::WriteEnvironment, + 4 => StartupCommand::PingResponse, + 255 => StartupCommand::FinishStartup, + _ => StartupCommand::Unhandled, + } + } +} + +#[panic_handler] +fn handle_panic(_arg: &core::panic::PanicInfo) -> ! { + loop {} +} + +#[no_mangle] +pub extern "C" fn init(server1: u32, server2: u32, server3: u32, server4: u32) -> ! { + let server = xous::SID::from_u32(server1, server2, server3, server4); + loop { + if let Ok(xous::Result::Message(envelope)) = + xous::rsyscall(xous::SysCall::ReceiveMessage(server)) + { + match envelope.id().into() { + StartupCommand::WriteMemory => write_memory(envelope.body.memory_message()), + StartupCommand::FinishStartup => finish_startup(server, envelope), + StartupCommand::PingResponse => ping_response(envelope), + + _ => panic!("Unsupported"), + } + } + } +} + +fn ping_response(envelope: xous::MessageEnvelope) { + if let Some(msg) = envelope.body.scalar_message() { + if envelope.body.is_blocking() { + xous::syscall::return_scalar(envelope.sender, msg.arg1 + 1).unwrap(); + } + } +} + +fn write_memory(memory: Option<&xous::MemoryMessage>) { + let memory = match memory { + Some(s) => s, + None => return, + }; + + let mut target_memory = xous::map_memory( + None, + memory.offset, + memory.buf.len(), + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .unwrap(); + + for (src, dest) in memory + .buf + .as_slice::() + .iter() + .zip(target_memory.as_slice_mut()) + { + *dest = *src; + } +} + +fn finish_startup(server: xous::SID, envelope: xous::MessageEnvelope) -> ! { + let entrypoint = envelope.body.scalar_message().unwrap().arg1; + drop(envelope); + xous::destroy_server(server).unwrap(); + let entry_fn = unsafe { core::mem::transmute::<_, fn() -> !>(entrypoint) }; + entry_fn(); +}