Allow exec, emulate clone by pretending to be child
This commit is contained in:
parent
2fffe3ee77
commit
f0431f4de1
|
@ -138,7 +138,7 @@ pub unsafe fn switch() {
|
||||||
|
|
||||||
for (_pid, context_lock) in contexts().map.iter() {
|
for (_pid, context_lock) in contexts().map.iter() {
|
||||||
let mut context = context_lock.write();
|
let mut context = context_lock.write();
|
||||||
if ! context.running {
|
if ! context.running && ! context.blocked {
|
||||||
to_ptr = context.deref_mut() as *mut Context;
|
to_ptr = context.deref_mut() as *mut Context;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -165,6 +165,8 @@ pub struct Context {
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
/// Running or not
|
/// Running or not
|
||||||
pub running: bool,
|
pub running: bool,
|
||||||
|
/// Blocked or not
|
||||||
|
pub blocked: bool,
|
||||||
/// The architecture specific context
|
/// The architecture specific context
|
||||||
pub arch: ArchContext,
|
pub arch: ArchContext,
|
||||||
/// Kernel stack
|
/// Kernel stack
|
||||||
|
@ -179,6 +181,7 @@ impl Context {
|
||||||
Context {
|
Context {
|
||||||
id: id,
|
id: id,
|
||||||
running: false,
|
running: false,
|
||||||
|
blocked: true,
|
||||||
arch: ArchContext::new(),
|
arch: ArchContext::new(),
|
||||||
kstack: None,
|
kstack: None,
|
||||||
files: Vec::new()
|
files: Vec::new()
|
||||||
|
|
|
@ -60,6 +60,10 @@ impl<'a> Elf<'a> {
|
||||||
let end_page = Page::containing_address(VirtualAddress::new((segment.p_vaddr + segment.p_memsz) as usize));
|
let end_page = Page::containing_address(VirtualAddress::new((segment.p_vaddr + segment.p_memsz) as usize));
|
||||||
|
|
||||||
for page in Page::range_inclusive(start_page, end_page) {
|
for page in Page::range_inclusive(start_page, end_page) {
|
||||||
|
if active_table.translate_page(page).is_some() {
|
||||||
|
//TODO panic!("Elf::run: still mapped: {:?}", page);
|
||||||
|
active_table.unmap(page);
|
||||||
|
}
|
||||||
active_table.map(page, entry::NO_EXECUTE | entry::WRITABLE);
|
active_table.map(page, entry::NO_EXECUTE | entry::WRITABLE);
|
||||||
}
|
}
|
||||||
active_table.flush_all();
|
active_table.flush_all();
|
||||||
|
@ -100,6 +104,10 @@ impl<'a> Elf<'a> {
|
||||||
let end_page = Page::containing_address(VirtualAddress::new(0x80000000 + 64*1024 - 1));
|
let end_page = Page::containing_address(VirtualAddress::new(0x80000000 + 64*1024 - 1));
|
||||||
|
|
||||||
for page in Page::range_inclusive(start_page, end_page) {
|
for page in Page::range_inclusive(start_page, end_page) {
|
||||||
|
if active_table.translate_page(page).is_some() {
|
||||||
|
//TODO panic!("Elf::run: still mapped: {:?}", page);
|
||||||
|
active_table.unmap(page);
|
||||||
|
}
|
||||||
active_table.map(page, entry::NO_EXECUTE | entry::WRITABLE | entry::USER_ACCESSIBLE);
|
active_table.map(page, entry::NO_EXECUTE | entry::WRITABLE | entry::USER_ACCESSIBLE);
|
||||||
}
|
}
|
||||||
active_table.flush_all();
|
active_table.flush_all();
|
||||||
|
|
|
@ -133,7 +133,15 @@ pub extern fn kmain() {
|
||||||
let pid = syscall::getpid();
|
let pid = syscall::getpid();
|
||||||
println!("BSP: {:?}", pid);
|
println!("BSP: {:?}", pid);
|
||||||
|
|
||||||
context::contexts_mut().spawn(userspace_init).expect("failed to spawn userspace_init");
|
match context::contexts_mut().spawn(userspace_init) {
|
||||||
|
Ok(context_lock) => {
|
||||||
|
let mut context = context_lock.write();
|
||||||
|
context.blocked = false;
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
panic!("failed to spawn userspace_init: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unsafe { context::switch(); }
|
unsafe { context::switch(); }
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ pub fn brk(address: usize) -> Result<usize> {
|
||||||
|
|
||||||
pub fn clone(flags: usize) -> Result<usize> {
|
pub fn clone(flags: usize) -> Result<usize> {
|
||||||
println!("Clone {:X}", flags);
|
println!("Clone {:X}", flags);
|
||||||
Err(Error::NoCall)
|
Ok(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exit(status: usize) -> ! {
|
pub fn exit(status: usize) -> ! {
|
||||||
|
@ -76,22 +76,23 @@ pub fn exit(status: usize) -> ! {
|
||||||
pub fn exec(path: &[u8], _args: &[[usize; 2]]) -> Result<usize> {
|
pub fn exec(path: &[u8], _args: &[[usize; 2]]) -> Result<usize> {
|
||||||
//TODO: Use args
|
//TODO: Use args
|
||||||
//TODO: Unmap previous mappings
|
//TODO: Unmap previous mappings
|
||||||
//TODO: Drop init_data
|
//TODO: Drop data vec
|
||||||
|
println!("Exec {}", unsafe { str::from_utf8_unchecked(path) });
|
||||||
|
|
||||||
let init_file = syscall::open(path, 0)?;
|
let file = syscall::open(path, 0)?;
|
||||||
let mut init_data = vec![];
|
let mut data = vec![];
|
||||||
loop {
|
loop {
|
||||||
let mut buf = [0; 4096];
|
let mut buf = [0; 4096];
|
||||||
let count = syscall::read(init_file, &mut buf)?;
|
let count = syscall::read(file, &mut buf)?;
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
init_data.extend_from_slice(&buf[..count]);
|
data.extend_from_slice(&buf[..count]);
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let _ = syscall::close(init_file);
|
let _ = syscall::close(file);
|
||||||
|
|
||||||
match elf::Elf::from(&init_data) {
|
match elf::Elf::from(&data) {
|
||||||
Ok(elf) => {
|
Ok(elf) => {
|
||||||
elf.run();
|
elf.run();
|
||||||
Ok(0)
|
Ok(0)
|
||||||
|
|
2
libstd
2
libstd
|
@ -1 +1 @@
|
||||||
Subproject commit 7b20e739903a4877a10b64b875a3fd7a06cdcd16
|
Subproject commit eaad8f520ce853c15c2071eff08ed813e87118fa
|
|
@ -125,8 +125,8 @@ pub fn dup(fd: usize) -> Result<usize> {
|
||||||
unsafe { syscall1(SYS_DUP, fd) }
|
unsafe { syscall1(SYS_DUP, fd) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn execve(path: *const u8, args: *const *const u8) -> Result<usize> {
|
pub fn execve(path: &str) -> Result<usize> {
|
||||||
syscall2(SYS_EXECVE, path as usize, args as usize)
|
unsafe { syscall2(SYS_EXECVE, path.as_ptr() as usize, path.len()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exit(status: usize) -> Result<usize> {
|
pub fn exit(status: usize) -> Result<usize> {
|
||||||
|
|
Loading…
Reference in a new issue