rustubs/proc/
exec.rs

1//! exec a user program by loading the binary and replace the current address
2//! space
3//! TODO rework this code, this is only POC
4use crate::fs;
5use crate::proc::loader::load;
6use crate::proc::task::Task;
7use crate::{fs::*, Mem};
8use core::arch::asm;
9#[allow(unreachable_code)]
10pub fn exec(file_name: &str) {
11	let task = Task::current().unwrap();
12	let mm = &mut task.mm;
13	// wipe user vmas;
14	// NOTE this doesn't wipe the pagetables
15	mm.vmas
16		.retain(|vma| vma.vm_range.start >= Mem::ID_MAP_START);
17
18	let archive = get_archive();
19	let file = fs::iter(archive).find(|f| f.hdr.name() == file_name);
20	if file.is_none() {
21		println!("error: no such file {}", file_name);
22		return;
23	}
24	let f = file.unwrap();
25	let entry = load(&f);
26	if entry.is_none() {
27		println!("failed to load elf");
28		return;
29	}
30	println!("exec entry: {:#X}", entry.unwrap());
31	go(entry.unwrap());
32	return;
33}
34
35extern "C" fn go(entry: u64) {
36	unsafe {
37		asm!("push {}; ret", in(reg) entry);
38	}
39}