Initial attempt at using spawn

This still doesn't work because of some weird data corruption that
changes the CID from `6` to a number greater than 32
This commit is contained in:
Vihaan Jim 2023-05-10 17:31:09 -04:00
parent b76953378a
commit c86ac65743
4 changed files with 998 additions and 825 deletions

1734
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -21,8 +21,6 @@ num-traits = {version = "0.2.14", default-features = false}
rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false} rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false}
xmas-elf = "0.9.0"
[features] [features]
default = ["app_loader"] default = ["app_loader"]
app_loader = [] app_loader = []

View file

@ -29,38 +29,69 @@ fn main() -> ! {
// a dummy server for testing purposes // a dummy server for testing purposes
let name = "_Hello World_"; // note: this MUST match up with the gam name let name = "_Hello World_"; // note: this MUST match up with the gam name
// create the spawn process
let stub = include_bytes!("spawn-stub");
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 process!");
log::info!("Spawn PID: {}, Spawn CID: {}", spawn.pid, spawn.cid);
// perform a ping to make sure that spawn is running
let result = xous::send_message(
spawn.cid,
xous::Message::new_blocking_scalar(4, 1, 2, 3, 4),
)
.unwrap();
log::info!("Result of ping: {:?}", result);
// load the app from the binary file // load the app from the binary file
let bin = include_bytes!("hello"); let bin = include_bytes!("hello");
let elf = xmas_elf::ElfFile::new(bin.as_slice()).expect("Couldn't load elf!"); // make sure that this is a 32 bit elf file
let entry_point = elf.header.pt2.entry_point(); assert!(bin[0] == 0x7F);
let code_ph = elf assert!(bin[1] == 0x45);
.program_iter() assert!(bin[2] == 0x4c);
.find(|ph| (ph.virtual_addr()..ph.virtual_addr() + ph.mem_size()).contains(&entry_point)) assert!(bin[3] == 0x46);
.expect("Couldn't find segment with entry point!"); assert!(bin[4] == 0x01);
if let xmas_elf::program::SegmentData::Undefined(code) = code_ph.get_data(&elf).expect("Couldn't load section!") {
let remainder = if code.len() & 0xFFF == 0 { let to_usize = |start, size| {
0 if size == 1 {
} else { return bin[start] as usize;
0x1000 - (code.len() & 0xFFF) }
}; if size == 2 {
let mut target_memory = xous::map_memory( // assumes little endianness
None, return u16::from_le_bytes(bin[start..start+size].try_into().unwrap()) as usize;
None, }
code.len() + remainder, if size == 4 {
xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::X return u32::from_le_bytes(bin[start..start+size].try_into().unwrap()) as usize;
).expect("Couldn't allocate new memory"); }
for (src, dest) in code.iter().zip(target_memory.as_slice_mut::<u8>()) { panic!("Tried to get usize of invalid size!");
*dest = *src; };
let entry_point = to_usize(0x18, 4);
let ph_start = to_usize(0x1c, 4);
let ph_size = to_usize(0x2A, 2);
let ph_count = to_usize(0x2C, 2);
// add the segments we should load
for i in 0..ph_count {
let start = ph_start + i * ph_size;
// only load PT_LOAD segments
if to_usize(start, 4) == 0x00000001 {
let offset = to_usize(start+0x04, 4);
let vaddr = to_usize(start+0x08, 4);
let mem_size = to_usize(start+0x14, 4);
log::info!("Loading offset {} to virtual address {} with memory size {}", offset, vaddr, mem_size);
let buf = unsafe { xous::MemoryRange::new(bin[offset..offset+mem_size].as_ptr() as usize, core::mem::size_of::<u8>()).expect("Couldn't create a buffer from the segment") };
xous::send_message(spawn.cid, xous::Message::new_lend(1, buf, xous::MemoryAddress::new(vaddr), None)).expect("Couldn't send a message to spawn");
} }
let entry_offset = entry_point - code_ph.virtual_addr();
let entry_point = unsafe { target_memory.as_ptr().add(entry_offset as usize) };
let run_app: fn() -> ! = unsafe { core::mem::transmute(entry_point) };
std::thread::spawn(move || {
run_app();
});
} }
// tell spawn to switch to the new program
xous::send_message(spawn.cid, 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) };
// xous::create_thread_0(run_app).expect("Couldn't run app!");
apps.push(xous_ipc::String::from_str(name)); apps.push(xous_ipc::String::from_str(name));
log::info!("Added app `{}'!", load_app_req.name); log::info!("Added app `{}'!", load_app_req.name);

Binary file not shown.