mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
Move elf file processing to spawn
This moves the part where the elf file is processed (i.e. parts moved into memory) into the spawn process.
This commit is contained in:
parent
6f46443065
commit
26849382ef
3 changed files with 107 additions and 96 deletions
22
Cargo.lock
generated
22
Cargo.lock
generated
|
|
@ -113,10 +113,10 @@ dependencies = [
|
||||||
"num-derive",
|
"num-derive",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"rkyv",
|
"rkyv",
|
||||||
"xous 0.9.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
"xous 0.9.45 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"xous-api-log",
|
"xous-api-log",
|
||||||
"xous-api-names",
|
"xous-api-names",
|
||||||
"xous-ipc 0.9.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
"xous-ipc 0.9.45 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -3873,6 +3873,15 @@ dependencies = [
|
||||||
"no-std-net",
|
"no-std-net",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "spawn"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"log",
|
||||||
|
"xous 0.9.45 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"xous-api-log",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spin"
|
name = "spin"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
|
|
@ -4067,6 +4076,13 @@ dependencies = [
|
||||||
"winapi-util",
|
"winapi-util",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "test-spawn"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"xous 0.9.45 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "textwrap"
|
name = "textwrap"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
|
|
@ -5212,8 +5228,6 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "xous-api-log"
|
name = "xous-api-log"
|
||||||
version = "0.1.41"
|
version = "0.1.41"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "24aad5ecdee87526a97eaaa7f26d22c0fe419667727598d694bda183dfa31847"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"num-derive",
|
"num-derive",
|
||||||
|
|
|
||||||
|
|
@ -3,28 +3,23 @@
|
||||||
|
|
||||||
enum StartupCommand {
|
enum StartupCommand {
|
||||||
Unhandled = 0,
|
Unhandled = 0,
|
||||||
WriteMemory = 1,
|
LoadElf = 1,
|
||||||
WriteArgs = 2,
|
PingResponse = 2,
|
||||||
WriteEnvironment = 3,
|
|
||||||
PingResponse = 4,
|
|
||||||
FinishStartup = 255,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<xous::MessageId> for StartupCommand {
|
impl From<xous::MessageId> for StartupCommand {
|
||||||
fn from(src: xous::MessageId) -> StartupCommand {
|
fn from(src: xous::MessageId) -> StartupCommand {
|
||||||
match src {
|
match src {
|
||||||
1 => StartupCommand::WriteMemory,
|
1 => StartupCommand::LoadElf,
|
||||||
2 => StartupCommand::WriteArgs,
|
2 => StartupCommand::PingResponse,
|
||||||
3 => StartupCommand::WriteEnvironment,
|
|
||||||
4 => StartupCommand::PingResponse,
|
|
||||||
255 => StartupCommand::FinishStartup,
|
|
||||||
_ => StartupCommand::Unhandled,
|
_ => StartupCommand::Unhandled,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn handle_panic(_arg: &core::panic::PanicInfo) -> ! {
|
fn handle_panic(arg: &core::panic::PanicInfo) -> ! {
|
||||||
|
log::info!("{arg}");
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,10 +48,14 @@ pub extern "C" fn init(server1: u32, server2: u32, server3: u32, server4: u32) -
|
||||||
xous::rsyscall(xous::SysCall::ReceiveMessage(server))
|
xous::rsyscall(xous::SysCall::ReceiveMessage(server))
|
||||||
{
|
{
|
||||||
match envelope.id().into() {
|
match envelope.id().into() {
|
||||||
StartupCommand::WriteMemory => write_memory(envelope.body.memory_message()),
|
StartupCommand::LoadElf => {
|
||||||
StartupCommand::FinishStartup => finish_startup(server, envelope),
|
let entry_point = read_elf(envelope.body.memory_message());
|
||||||
|
drop(envelope); // we have to get rid of all messages to destroy the server
|
||||||
|
// destroy the server
|
||||||
|
xous::destroy_server(server).expect("Couldn't destroy spawn server");
|
||||||
|
jump(entry_point);
|
||||||
|
},
|
||||||
StartupCommand::PingResponse => ping_response(envelope),
|
StartupCommand::PingResponse => ping_response(envelope),
|
||||||
|
|
||||||
_ => panic!("Unsupported"),
|
_ => panic!("Unsupported"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -71,34 +70,84 @@ fn ping_response(envelope: xous::MessageEnvelope) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_memory(memory: Option<&xous::MemoryMessage>) {
|
fn read_elf(memory: Option<&xous::MemoryMessage>) -> usize {
|
||||||
let memory = match memory {
|
let memory = match memory {
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
None => return,
|
None => panic!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut target_memory = xous::map_memory(
|
// get the elf binary from the message
|
||||||
None,
|
let mut bin = memory.buf.as_slice::<u8>();
|
||||||
memory.offset,
|
|
||||||
memory.buf.len(),
|
|
||||||
xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::X,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
for (src, dest) in memory
|
// go to the beginning of the ELF file. The chance of us accidentally finding a false
|
||||||
.buf
|
// beginning is very small.
|
||||||
.as_slice::<usize>()
|
while !(bin[0] == 0x7F && bin[1] == 0x45 && bin[2] == 0x4c && bin[3] == 0x46) {
|
||||||
.iter()
|
bin = &bin[1..];
|
||||||
.zip(target_memory.as_slice_mut())
|
|
||||||
{
|
|
||||||
*dest = *src;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let max = bin.len();
|
||||||
|
|
||||||
|
// make sure that this is a 32 bit
|
||||||
|
assert!(bin[4] == 0x01);
|
||||||
|
|
||||||
|
// a helper function to get a region of the file as a usize
|
||||||
|
let to_usize = |start, size| {
|
||||||
|
if size == 1 {
|
||||||
|
return bin[start] as usize;
|
||||||
|
}
|
||||||
|
if size == 2 {
|
||||||
|
// assumes little endianness
|
||||||
|
return u16::from_le_bytes(bin[start..start+size].try_into().unwrap()) as usize;
|
||||||
|
}
|
||||||
|
if size == 4 {
|
||||||
|
return u32::from_le_bytes(bin[start..start+size].try_into().unwrap()) as usize;
|
||||||
|
}
|
||||||
|
panic!("Tried to get usize of invalid size!");
|
||||||
|
};
|
||||||
|
|
||||||
|
// some basic stuff to know
|
||||||
|
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 src_addr = to_usize(start+0x04, 4);
|
||||||
|
let vaddr = to_usize(start+0x08, 4);
|
||||||
|
let padding = if vaddr & 0xFFF == 0 { 0 } else { vaddr & 0xFFF };
|
||||||
|
let mem_size = to_usize(start+0x14, 4);
|
||||||
|
let mem_size = mem_size + padding;
|
||||||
|
let mem_size = mem_size + if mem_size & 0xFFF == 0 { 0 } else { 0x1000 - (mem_size & 0xFFF) };
|
||||||
|
|
||||||
|
assert_eq!(0, mem_size & 0xFFF);
|
||||||
|
assert_eq!(0, (vaddr - padding) & 0xFFF);
|
||||||
|
|
||||||
|
log::info!("Loading offset {} to virtual address {} with memory size {}", src_addr, vaddr, mem_size);
|
||||||
|
let mut target_memory = xous::map_memory(
|
||||||
|
None,
|
||||||
|
core::num::NonZeroUsize::new(vaddr-padding),
|
||||||
|
mem_size,
|
||||||
|
xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::X,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
for (dest, src) in target_memory.as_slice_mut().iter_mut().skip(padding)
|
||||||
|
.zip(bin[src_addr..core::cmp::min(max, src_addr+mem_size-padding)].iter())
|
||||||
|
{
|
||||||
|
*dest = *src;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log::info!("Finished writing");
|
||||||
|
return entry_point;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish_startup(server: xous::SID, envelope: xous::MessageEnvelope) -> ! {
|
fn jump(entry_point: usize) -> ! {
|
||||||
let entrypoint = envelope.body.scalar_message().unwrap().arg1;
|
log::info!("Jumping to {}", entry_point);
|
||||||
drop(envelope);
|
let entry_fn = unsafe { core::mem::transmute::<_, fn() -> !>(entry_point as *const u8) };
|
||||||
xous::destroy_server(server).unwrap();
|
|
||||||
let entry_fn = unsafe { core::mem::transmute::<_, fn() -> !>(entrypoint) };
|
|
||||||
entry_fn();
|
entry_fn();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,69 +38,17 @@ fn main() -> ! {
|
||||||
// perform a ping to make sure that spawn is running
|
// perform a ping to make sure that spawn is running
|
||||||
let result = xous::send_message(
|
let result = xous::send_message(
|
||||||
6,
|
6,
|
||||||
xous::Message::new_blocking_scalar(4, 1, 2, 3, 4),
|
xous::Message::new_blocking_scalar(2, 1, 2, 3, 4),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
log::info!("Result of ping: {:?}", result);
|
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");
|
||||||
// make sure that this is a 32 bit elf file
|
let bin_len = bin.len();
|
||||||
assert!(bin[0] == 0x7F);
|
let bin_loc = bin.as_ptr() as usize;
|
||||||
assert!(bin[1] == 0x45);
|
let buf = unsafe { xous::MemoryRange::new(bin_loc & !0xFFF, bin_len + if bin_len & 0xFFF == 0 { 0 } else {0x1000 - (bin_len & 0xFFF)}).expect("Couldn't create a buffer from the segment") };
|
||||||
assert!(bin[2] == 0x4c);
|
xous::send_message(6, xous::Message::new_lend(1, buf, None, None)).expect("Couldn't send a message to spawn");
|
||||||
assert!(bin[3] == 0x46);
|
|
||||||
assert!(bin[4] == 0x01);
|
|
||||||
|
|
||||||
let to_usize = |start, size| {
|
|
||||||
if size == 1 {
|
|
||||||
return bin[start] as usize;
|
|
||||||
}
|
|
||||||
if size == 2 {
|
|
||||||
// assumes little endianness
|
|
||||||
return u16::from_le_bytes(bin[start..start+size].try_into().unwrap()) as usize;
|
|
||||||
}
|
|
||||||
if size == 4 {
|
|
||||||
return u32::from_le_bytes(bin[start..start+size].try_into().unwrap()) as usize;
|
|
||||||
}
|
|
||||||
panic!("Tried to get usize of invalid size!");
|
|
||||||
};
|
|
||||||
|
|
||||||
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 src_addr = bin.as_ptr() as usize + offset;
|
|
||||||
let src_addr = src_addr - if src_addr & 0xFFF == 0 { 0 } else { src_addr & 0xFFF };
|
|
||||||
let vaddr = to_usize(start+0x08, 4);
|
|
||||||
let vaddr = vaddr + if vaddr & 0xFFF == 0 { 0 } else { 0x1000 - (vaddr & 0xFFF) };
|
|
||||||
let mem_size = to_usize(start+0x14, 4);
|
|
||||||
let mem_size = mem_size + if mem_size & 0xFFF == 0 { 0 } else { 0x1000 - (mem_size & 0xFFF) };
|
|
||||||
|
|
||||||
assert_eq!(0, mem_size & 0xFFF);
|
|
||||||
assert_eq!(0, vaddr & 0xFFF);
|
|
||||||
assert_eq!(0, src_addr & 0xFFF);
|
|
||||||
|
|
||||||
log::info!("Loading offset {} to virtual address {} with memory size {}", offset, vaddr, mem_size);
|
|
||||||
|
|
||||||
let buf = unsafe { xous::MemoryRange::new(src_addr, mem_size).expect("Couldn't create a buffer from the segment") };
|
|
||||||
xous::send_message(6, xous::Message::new_lend(1, buf, xous::MemoryAddress::new(vaddr), None)).expect("Couldn't send a message to spawn");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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) };
|
|
||||||
// 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);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue