Allow building a custom spawn binary

For the moment I have replaced test-spawn/spawn with app-loader/spawn
in Cargo.toml, but I might end up changing the name eventually.
This commit is contained in:
Vihaan Jim 2023-06-29 19:41:53 -04:00
parent a37c06fe1f
commit f2b1f7ef09
10 changed files with 496 additions and 10 deletions

15
Cargo.lock generated
View file

@ -4015,11 +4015,10 @@ dependencies = [
[[package]]
name = "svd2utra"
version = "0.1.16"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1f345c999b1ea8d2072d3c59521640e3dde487873c7c5630b33ad0bfe959f0e"
checksum = "2ba87b81de81a553ec5904993a676aa29d1583a4f6ab96e03f8b44e4d6c05a61"
dependencies = [
"convert_case",
"quick-xml",
]
@ -4223,7 +4222,7 @@ dependencies = [
"log",
"pem",
"ring",
"svd2utra 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"svd2utra 0.1.18",
"xmas-elf",
"xous-semver",
]
@ -4693,11 +4692,11 @@ checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "utralib"
version = "0.1.18"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00ac0e73f1b4c2beb1f55ab4c2b51dba6ad707aa4a7102483611e456ab3a655c"
checksum = "99e69095d8616b8ff89850fb3693710ceab2923b4d094844c701ca5141563683"
dependencies = [
"svd2utra 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"svd2utra 0.1.18",
]
[[package]]
@ -5434,7 +5433,7 @@ dependencies = [
"rustc_version 0.4.0",
"serde",
"serde_json",
"svd2utra 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"svd2utra 0.1.18",
"tempfile",
"ureq",
"zip",

View file

@ -83,7 +83,8 @@ members = [
"services/ffi-test",
"services/tts",
"services/test-spawn",
"services/test-spawn/spawn",
# "services/test-spawn/spawn",
"services/app-loader/spawn",
"services/usb-test",
"services/usb-device-xous",
"tools/perflib",

View file

@ -0,0 +1,22 @@
use std::process::Command;
fn main() {
// this should be changed at some point to support other platforms
let target = "riscv32imac-unknown-xous-elf";
// compile the spawn package
let status = Command::new("cargo").args(&["build", "--package", "spawn", "--target", target])
.status().unwrap();
if !status.success() {
panic!();
}
// turn it into a stub
let status = Command::new("cargo").args(&["run", "--package", "tools", "--bin", "copy-object", "--"])
.arg(&format!("../../target/{}/debug/spawn", target))
.status().unwrap();
if !status.success() {
panic!();
}
println!("cargo:rerun-if-changed=spawn");
}

View file

@ -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 = "0.9.42"

View file

@ -0,0 +1,31 @@
# Spawn a process
This crate represents an 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(a1: u32, a2: u32, a3: u32, a4: u32) -> ! {
let sid = xous::SID::from_u32(a1, a2, a3, a4);
loop {}
}
```
This process is responsible for performing its own bootstrapping (i.e. allocating/initializing
.data and .bss), 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. |

View file

@ -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");
}

View file

@ -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 | */

View file

@ -0,0 +1,113 @@
use core::fmt::{Error, Write};
use utralib::generated::*;
#[macro_export]
macro_rules! print
{
($($args:tt)+) => ({
use core::fmt::Write;
let _ = write!(crate::debug::Uart {}, $($args)+);
});
}
#[macro_export]
macro_rules! println
{
() => ({
print!("\r\n")
});
($fmt:expr) => ({
print!(concat!($fmt, "\r\n"))
});
($fmt:expr, $($args:tt)+) => ({
print!(concat!($fmt, "\r\n"), $($args)+)
});
}
#[allow(dead_code)]
fn handle_irq(irq_no: usize, arg: *mut usize) {
print!("Handling IRQ {} (arg: {:08x}): ", irq_no, arg as usize);
while let Some(c) = { crate::debug::Uart {} }.getc() {
print!("{}", c as char);
}
println!();
}
pub struct Uart {}
// this is a hack to bypass an explicit initialization/allocation step for the debug structure
pub static mut DEFAULT_UART_ADDR: *mut usize = 0x0000_0000 as *mut usize;
pub const DEFAULT: Uart = Uart {};
impl Uart {
fn map_uart(&self) {
/*
Note: the memory address and interrupt specified here needs to map to a unique hardware
UART resource. Modify in this function as necessary.
*/
let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::console::HW_CONSOLE_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map debug uart");
unsafe {
DEFAULT_UART_ADDR = uart.as_mut_ptr() as _;
}
println!("Mapped UART @ {:08x}", uart.as_ptr() as usize);
// core::mem::forget(uart);
println!("Allocating IRQ...");
/* xous::claim_interrupt(
utra::console::CONSOLE_IRQ,
handle_irq,
core::ptr::null_mut::<usize>(),
)
.expect("unable to allocate IRQ"); */
self.enable_rx();
}
pub fn putc(&self, c: u8) {
if unsafe { DEFAULT_UART_ADDR } as usize == 0 {
self.map_uart();
}
let mut uart_csr = CSR::new(unsafe { DEFAULT_UART_ADDR as *mut u32 });
// Wait until TXFULL is `0`
while uart_csr.r(utra::uart::TXFULL) != 0 {}
uart_csr.wo(utra::uart::RXTX, c as u32);
}
pub fn enable_rx(&self) {
let mut uart_csr = CSR::new(unsafe { DEFAULT_UART_ADDR as *mut u32 });
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
}
#[allow(dead_code)]
pub fn getc(&self) -> Option<u8> {
if unsafe { DEFAULT_UART_ADDR } as usize == 0 {
self.map_uart();
}
let mut uart_csr = CSR::new(unsafe { DEFAULT_UART_ADDR as *mut u32 });
match uart_csr.rf(utra::uart::EV_PENDING_RX) {
0 => None,
ack => {
let c = Some(uart_csr.rf(utra::uart::RXTX_RXTX) as u8);
uart_csr.wfo(utra::uart::EV_PENDING_RX, ack);
c
}
}
}
}
impl Write for Uart {
fn write_str(&mut self, s: &str) -> Result<(), Error> {
for c in s.bytes() {
self.putc(c);
}
Ok(())
}
}

View file

@ -0,0 +1,87 @@
#![no_std]
#![no_main]
enum StartupCommand {
Unhandled = 0,
WriteMemory = 1,
WriteArgs = 2,
WriteEnvironment = 3,
PingResponse = 4,
FinishStartup = 255,
}
impl From<xous::MessageId> 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::MessageEnvelope(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 | xous::MemoryFlags::X,
)
.unwrap();
for (src, dest) in memory
.buf
.as_slice::<usize>()
.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();
}

View file

@ -30,7 +30,7 @@ fn main() -> ! {
let name = "_Hello World_"; // note: this MUST match up with the gam name
// create the spawn process
let stub = include_bytes!("spawn-stub");
let stub = include_bytes!("../../../target/riscv32imac-unknown-xous-elf/debug/spawn.bin");
let args = xous::ProcessArgs::new(stub, xous::MemoryAddress::new(0x2050_1000).unwrap(), xous::MemoryAddress::new(0x2050_1000).unwrap());
let spawn = xous::create_process(args).expect("Couldn't create spawn process");
log::info!("Spawn PID: {}, Spawn CID: {}", spawn.pid, spawn.cid);
@ -96,6 +96,7 @@ fn main() -> ! {
}
// tell spawn to switch to the new program
log::info!("Jumping...");
xous::send_message(6, xous::Message::new_scalar(255, entry_point, 0, 0, 0)).expect("Couldn't send a message to spawn");
// let run_app: fn() -> ! = unsafe { core::mem::transmute(entry_point) };