loader: pass environment to processes via stack

Load environment variables to processes by placing the value onto the
stack. This will support additional data in the future such as command
line arguments.

Note this is a breaking change and requires a kernel modification.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2024-01-07 22:36:34 +08:00
parent f02d296ecc
commit d8c6881554
4 changed files with 49 additions and 4 deletions

View file

@ -1,6 +1,7 @@
pub const BACKUP_ARGS_ADDR: usize = crate::platform::RAM_BASE + crate::platform::RAM_SIZE - 0x2000;
pub const USER_STACK_TOP: usize = 0x8000_0000;
pub const USER_STACK_PADDING: usize = 16;
pub const PAGE_TABLE_OFFSET: usize = 0xff40_0000;
pub const PAGE_TABLE_ROOT_OFFSET: usize = 0xff80_0000;
pub const CONTEXT_OFFSET: usize = 0xff80_1000;

View file

@ -95,6 +95,7 @@ impl MiniElf {
allocator: &mut BootConfig,
load_offset: usize,
pid: XousPid,
params: &[u8],
xip: bool,
) -> usize {
println!("Mapping PID {} starting at offset {:08x}", pid, load_offset);
@ -121,7 +122,7 @@ impl MiniElf {
// The load offset is the end of this process. Shift it down by one page
// so we get the start of the first page.
let mut top = load_offset - PAGE_SIZE;
let stack_addr = USER_STACK_TOP - 16;
let stack_addr = USER_STACK_TOP - USER_STACK_PADDING;
// Allocate a page to handle the top-level memory translation
#[cfg(not(feature = "atsama5d27"))]
@ -183,6 +184,12 @@ impl MiniElf {
if i == 0 {
// For the initial stack frame, allocate a valid page
let sp_page = allocator.alloc() as usize;
// Copy the params block into this first page
let sp_page_slice = unsafe { slice::from_raw_parts_mut(sp_page as *mut u8, PAGE_SIZE) };
let params_start = sp_page_slice.len() - params.len();
sp_page_slice[params_start..].copy_from_slice(params);
// Attach the page to the process
allocator.map_page(
tt,
sp_page,
@ -377,7 +384,8 @@ impl MiniElf {
let process = &mut allocator.processes[pid as usize - 1];
process.entrypoint = self.entry_point as usize;
process.sp = stack_addr;
process.sp = (stack_addr - params.len()) & !0xf;
process.env = stack_addr - params.len() + USER_STACK_PADDING;
#[cfg(not(feature = "atsama5d27"))]
{
process.satp = 0x8000_0000 | ((pid as usize) << 22) | (_tt_address >> 12);

View file

@ -17,6 +17,9 @@ pub struct InitialProcess {
/// Address of the top of the stack
pub sp: usize,
/// Address of the start of the env block
pub env: usize,
}
/// Phase 1:

View file

@ -11,6 +11,36 @@ pub fn phase_2(cfg: &mut BootConfig) {
let mut process_offset = cfg.sram_start as usize + cfg.sram_size - cfg.init_size;
println!("\n\nPhase2: Processess start out @ {:08x}", process_offset);
// Construct an environment block. This will be used by processes
// to access environment variables and system parameters. This
// is hardcoded for now, and can be expanded later.
#[rustfmt::skip]
let mut env = [
0x41, 0x70, 0x70, 0x50, // 'AppP' indicating application parameters
0x08, 0x00, 0x00, 0x00, // Size of AppP tag contents
0xb2, 0x00, 0x00, 0x00, // Size of entire AppP block including all tags
0x02, 0x00, 0x00, 0x00, // Number of tags present
0x45, 0x6e, 0x76, 0x42, // 'EnvB' indicating an environment block
0x9a, 0x00, 0x00, 0x00, // Number of bytes that follows for the environment block
0x01, 0x00, // Number of environment variables
0x14, 0x00, // Length of name of first variable
// Name of first variable 'ROOT_FILESYSTEM_HASH':
0x52, 0x4f, 0x4f, 0x54, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53, 0x59, 0x53, 0x54, 0x45, 0x4d,
0x5f, 0x48, 0x41, 0x53, 0x48,
// Length of the contents of the first variable
0x80, 0x00,
// Root filesystem hash contents begin here:
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x41, 0x42, 0x43, 0x44, 0x45, 0x30, 0x30,
];
// Go through all Init processes and the kernel, setting up their
// page tables and mapping memory to them.
let mut pid = 2;
@ -34,14 +64,14 @@ pub fn phase_2(cfg: &mut BootConfig) {
if tag.name == u32::from_le_bytes(*b"IniE") {
let inie = MiniElf::new(&tag);
println!("\n\nCopying IniE program into memory");
let allocated = inie.load(cfg, process_offset, pid, false);
let allocated = inie.load(cfg, process_offset, pid, &env, false);
println!("IniE Allocated {:x}", allocated);
process_offset -= allocated;
pid += 1;
} else if tag.name == u32::from_le_bytes(*b"IniF") {
let inif = MiniElf::new(&tag);
println!("\n\nMapping IniF program into memory");
let allocated = inif.load(cfg, process_offset, pid, true);
let allocated = inif.load(cfg, process_offset, pid, &env, true);
println!("IniF Allocated {:x}", allocated);
process_offset -= allocated;
pid += 1;
@ -260,6 +290,8 @@ impl ProgramDescription {
STACK_PAGE_COUNT
} {
if i == 0 {
// Pre-allocate the first stack offset, since it
// will definitely be used
let sp_page = allocator.alloc() as usize;
allocator.map_page(
satp,
@ -358,6 +390,7 @@ impl ProgramDescription {
let process = &mut allocator.processes[pid_idx];
process.entrypoint = self.entrypoint as usize;
process.sp = stack_addr;
process.env = 0;
process.satp = 0x8000_0000 | ((pid as usize) << 22) | (satp_address >> 12);
}
}