Complete execve - add argument support using safe ABI
This commit is contained in:
parent
57f5699664
commit
cfbaccf4d2
|
@ -6,7 +6,7 @@ pub unsafe extern fn memcpy(dest: *mut u8, src: *const u8,
|
||||||
n: usize) -> *mut u8 {
|
n: usize) -> *mut u8 {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < n {
|
while i < n {
|
||||||
*dest.offset(i as isize) = *src.offset(i as isize);
|
*((dest as usize + i) as *mut u8) = *((src as usize + i) as *const u8);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,12 +23,12 @@ pub unsafe extern fn memmove(dest: *mut u8, src: *const u8,
|
||||||
let mut i = n;
|
let mut i = n;
|
||||||
while i != 0 {
|
while i != 0 {
|
||||||
i -= 1;
|
i -= 1;
|
||||||
*dest.offset(i as isize) = *src.offset(i as isize);
|
*((dest as usize + i) as *mut u8) = *((src as usize + i) as *const u8);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < n {
|
while i < n {
|
||||||
*dest.offset(i as isize) = *src.offset(i as isize);
|
*((dest as usize + i) as *mut u8) = *((src as usize + i) as *const u8);
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,14 +40,14 @@ pub unsafe extern fn memmove(dest: *mut u8, src: *const u8,
|
||||||
///
|
///
|
||||||
/// Fill a block of memory with a specified value.
|
/// Fill a block of memory with a specified value.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern fn memset(s: *mut u8, c: i32, n: usize) -> *mut u8 {
|
pub unsafe extern fn memset(dest: *mut u8, c: i32, n: usize) -> *mut u8 {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < n {
|
while i < n {
|
||||||
*s.offset(i as isize) = c as u8;
|
*((dest as usize + i) as *mut u8) = c as u8;
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
s
|
dest
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Memcmp
|
/// Memcmp
|
||||||
|
@ -58,8 +58,8 @@ pub unsafe extern fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
|
||||||
while i < n {
|
while i < n {
|
||||||
let a = *s1.offset(i as isize);
|
let a = *((s1 as usize + i) as *const u8);
|
||||||
let b = *s2.offset(i as isize);
|
let b = *((s2 as usize + i) as *const u8);
|
||||||
if a != b {
|
if a != b {
|
||||||
return a as i32 - b as i32
|
return a as i32 - b as i32
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,9 @@ pub extern crate x86;
|
||||||
/// Offset to user image
|
/// Offset to user image
|
||||||
pub const USER_OFFSET: usize = 0;
|
pub const USER_OFFSET: usize = 0;
|
||||||
|
|
||||||
|
/// Offset to user arguments
|
||||||
|
pub const USER_ARG_OFFSET: usize = USER_OFFSET + PML4_SIZE/2;
|
||||||
|
|
||||||
/// Offset to user heap
|
/// Offset to user heap
|
||||||
pub const USER_HEAP_OFFSET: usize = USER_OFFSET + PML4_SIZE;
|
pub const USER_HEAP_OFFSET: usize = USER_OFFSET + PML4_SIZE;
|
||||||
|
|
||||||
|
@ -139,7 +142,8 @@ macro_rules! interrupt_error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push scratch registers
|
// Push scratch registers
|
||||||
asm!("push rax
|
asm!("xchg bx, bx
|
||||||
|
push rax
|
||||||
push rcx
|
push rcx
|
||||||
push rdx
|
push rdx
|
||||||
push rdi
|
push rdi
|
||||||
|
|
|
@ -38,10 +38,9 @@ pub fn main() {
|
||||||
|
|
||||||
println!("init: spawning {}", line);
|
println!("init: spawning {}", line);
|
||||||
match command.spawn() {
|
match command.spawn() {
|
||||||
Ok(mut child) => if let Err(err) = child.wait() {
|
Ok(mut child) => match child.wait() {
|
||||||
println!("init: failed to wait for '{}': {}", line, err);
|
Ok(status) => println!("init: waited for {}: {:?}", line, status.code()),
|
||||||
} else {
|
Err(err) => println!("init: failed to wait for '{}': {}", line, err)
|
||||||
println!("init: waited for {}", line);
|
|
||||||
},
|
},
|
||||||
Err(err) => println!("init: failed to execute '{}': {}", line, err)
|
Err(err) => println!("init: failed to execute '{}': {}", line, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,17 +5,10 @@ use collections::String;
|
||||||
use core::str;
|
use core::str;
|
||||||
|
|
||||||
#[cfg(target_arch = "x86")]
|
#[cfg(target_arch = "x86")]
|
||||||
use goblin::elf32::{header, program_header};
|
pub use goblin::elf32::{header, program_header};
|
||||||
|
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(target_arch = "x86_64")]
|
||||||
use goblin::elf64::{header, program_header};
|
pub use goblin::elf64::{header, program_header};
|
||||||
|
|
||||||
use arch;
|
|
||||||
use arch::externs::memcpy;
|
|
||||||
use arch::paging::{entry, VirtualAddress};
|
|
||||||
use arch::start::usermode;
|
|
||||||
use context;
|
|
||||||
use syscall::{Error, Result as SysResult};
|
|
||||||
|
|
||||||
/// An ELF executable
|
/// An ELF executable
|
||||||
pub struct Elf<'a> {
|
pub struct Elf<'a> {
|
||||||
|
@ -52,76 +45,6 @@ impl<'a> Elf<'a> {
|
||||||
pub fn entry(&self) -> usize {
|
pub fn entry(&self) -> usize {
|
||||||
self.header.e_entry as usize
|
self.header.e_entry as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test function to run. Remove and replace with proper syscall
|
|
||||||
pub fn run(self) -> SysResult<!> {
|
|
||||||
{
|
|
||||||
let contexts = context::contexts();
|
|
||||||
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
|
||||||
let mut context = context_lock.write();
|
|
||||||
|
|
||||||
// Unmap previous image and stack
|
|
||||||
context.image.clear();
|
|
||||||
drop(context.heap.take());
|
|
||||||
drop(context.stack.take());
|
|
||||||
|
|
||||||
for segment in self.segments() {
|
|
||||||
if segment.p_type == program_header::PT_LOAD {
|
|
||||||
let mut memory = context::memory::Memory::new(
|
|
||||||
VirtualAddress::new(segment.p_vaddr as usize),
|
|
||||||
segment.p_memsz as usize,
|
|
||||||
entry::NO_EXECUTE | entry::WRITABLE,
|
|
||||||
true,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
// Copy file data
|
|
||||||
memcpy(segment.p_vaddr as *mut u8,
|
|
||||||
(self.data.as_ptr() as usize + segment.p_offset as usize) as *const u8,
|
|
||||||
segment.p_filesz as usize);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut flags = entry::NO_EXECUTE | entry::USER_ACCESSIBLE;
|
|
||||||
|
|
||||||
if segment.p_flags & program_header::PF_R == program_header::PF_R {
|
|
||||||
flags.insert(entry::PRESENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// W ^ X. If it is executable, do not allow it to be writable, even if requested
|
|
||||||
if segment.p_flags & program_header::PF_X == program_header::PF_X {
|
|
||||||
flags.remove(entry::NO_EXECUTE);
|
|
||||||
} else if segment.p_flags & program_header::PF_W == program_header::PF_W {
|
|
||||||
flags.insert(entry::WRITABLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
memory.remap(flags, true);
|
|
||||||
|
|
||||||
context.image.push(memory.to_shared());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
context.heap = Some(context::memory::Memory::new(
|
|
||||||
VirtualAddress::new(arch::USER_HEAP_OFFSET),
|
|
||||||
0,
|
|
||||||
entry::NO_EXECUTE | entry::WRITABLE | entry::USER_ACCESSIBLE,
|
|
||||||
true,
|
|
||||||
true
|
|
||||||
).to_shared());
|
|
||||||
|
|
||||||
// Map stack
|
|
||||||
context.stack = Some(context::memory::Memory::new(
|
|
||||||
VirtualAddress::new(arch::USER_STACK_OFFSET),
|
|
||||||
arch::USER_STACK_SIZE,
|
|
||||||
entry::NO_EXECUTE | entry::WRITABLE | entry::USER_ACCESSIBLE,
|
|
||||||
true,
|
|
||||||
true
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Go to usermode
|
|
||||||
unsafe { usermode(self.entry(), arch::USER_STACK_OFFSET + arch::USER_STACK_SIZE - 256); }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ElfSegments<'a> {
|
pub struct ElfSegments<'a> {
|
||||||
|
|
|
@ -7,13 +7,15 @@ use core::str;
|
||||||
use spin::Mutex;
|
use spin::Mutex;
|
||||||
|
|
||||||
use arch;
|
use arch;
|
||||||
|
use arch::externs::memcpy;
|
||||||
use arch::memory::allocate_frame;
|
use arch::memory::allocate_frame;
|
||||||
use arch::paging::{ActivePageTable, InactivePageTable, Page, VirtualAddress, entry};
|
use arch::paging::{ActivePageTable, InactivePageTable, Page, VirtualAddress, entry};
|
||||||
use arch::paging::temporary_page::TemporaryPage;
|
use arch::paging::temporary_page::TemporaryPage;
|
||||||
|
use arch::start::usermode;
|
||||||
use context;
|
use context;
|
||||||
use elf;
|
use elf::{self, program_header};
|
||||||
use scheme;
|
use scheme;
|
||||||
use syscall::{self, Error, Result};
|
use syscall::{self, Error, Result, validate_slice, validate_slice_mut};
|
||||||
|
|
||||||
pub fn brk(address: usize) -> Result<usize> {
|
pub fn brk(address: usize) -> Result<usize> {
|
||||||
let contexts = context::contexts();
|
let contexts = context::contexts();
|
||||||
|
@ -332,33 +334,147 @@ pub fn exit(status: usize) -> ! {
|
||||||
unreachable!();
|
unreachable!();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec(path: &[u8], _args: &[[usize; 2]]) -> Result<usize> {
|
pub fn exec(path: &[u8], arg_ptrs: &[[usize; 2]]) -> Result<usize> {
|
||||||
//TODO: Use args
|
let entry;
|
||||||
//TODO: Unmap previous mappings
|
let mut sp = arch::USER_STACK_OFFSET + arch::USER_STACK_SIZE - 256;
|
||||||
//TODO: Drop data vec
|
|
||||||
|
|
||||||
let file = syscall::open(path, 0)?;
|
{
|
||||||
let mut data = vec![];
|
let mut args = Vec::new();
|
||||||
loop {
|
for arg_ptr in arg_ptrs {
|
||||||
let mut buf = [0; 4096];
|
let arg = validate_slice(arg_ptr[0] as *const u8, arg_ptr[1])?;
|
||||||
let count = syscall::read(file, &mut buf)?;
|
args.push(arg.to_vec()); // Must be moved into kernel space before exec unmaps all memory
|
||||||
if count > 0 {
|
}
|
||||||
data.extend_from_slice(&buf[..count]);
|
|
||||||
} else {
|
let file = syscall::open(path, 0)?;
|
||||||
break;
|
//TODO: Only read elf header, not entire file. Then read required segments
|
||||||
|
let mut data = vec![];
|
||||||
|
loop {
|
||||||
|
let mut buf = [0; 4096];
|
||||||
|
let count = syscall::read(file, &mut buf)?;
|
||||||
|
if count > 0 {
|
||||||
|
data.extend_from_slice(&buf[..count]);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _ = syscall::close(file);
|
||||||
|
|
||||||
|
match elf::Elf::from(&data) {
|
||||||
|
Ok(elf) => {
|
||||||
|
entry = elf.entry();
|
||||||
|
|
||||||
|
drop(path); // Drop so that usage is not allowed after unmapping context
|
||||||
|
drop(arg_ptrs); // Drop so that usage is not allowed after unmapping context
|
||||||
|
|
||||||
|
let contexts = context::contexts();
|
||||||
|
let context_lock = contexts.current().ok_or(Error::NoProcess)?;
|
||||||
|
let mut context = context_lock.write();
|
||||||
|
|
||||||
|
// Unmap previous image and stack
|
||||||
|
context.image.clear();
|
||||||
|
drop(context.heap.take());
|
||||||
|
drop(context.stack.take());
|
||||||
|
|
||||||
|
for segment in elf.segments() {
|
||||||
|
if segment.p_type == program_header::PT_LOAD {
|
||||||
|
let mut memory = context::memory::Memory::new(
|
||||||
|
VirtualAddress::new(segment.p_vaddr as usize),
|
||||||
|
segment.p_memsz as usize,
|
||||||
|
entry::NO_EXECUTE | entry::WRITABLE,
|
||||||
|
true,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
// Copy file data
|
||||||
|
memcpy(segment.p_vaddr as *mut u8,
|
||||||
|
(elf.data.as_ptr() as usize + segment.p_offset as usize) as *const u8,
|
||||||
|
segment.p_filesz as usize);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut flags = entry::NO_EXECUTE | entry::USER_ACCESSIBLE;
|
||||||
|
|
||||||
|
if segment.p_flags & program_header::PF_R == program_header::PF_R {
|
||||||
|
flags.insert(entry::PRESENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// W ^ X. If it is executable, do not allow it to be writable, even if requested
|
||||||
|
if segment.p_flags & program_header::PF_X == program_header::PF_X {
|
||||||
|
flags.remove(entry::NO_EXECUTE);
|
||||||
|
} else if segment.p_flags & program_header::PF_W == program_header::PF_W {
|
||||||
|
flags.insert(entry::WRITABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
memory.remap(flags, true);
|
||||||
|
|
||||||
|
context.image.push(memory.to_shared());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
context.heap = Some(context::memory::Memory::new(
|
||||||
|
VirtualAddress::new(arch::USER_HEAP_OFFSET),
|
||||||
|
0,
|
||||||
|
entry::NO_EXECUTE | entry::WRITABLE | entry::USER_ACCESSIBLE,
|
||||||
|
true,
|
||||||
|
true
|
||||||
|
).to_shared());
|
||||||
|
|
||||||
|
// Map stack
|
||||||
|
context.stack = Some(context::memory::Memory::new(
|
||||||
|
VirtualAddress::new(arch::USER_STACK_OFFSET),
|
||||||
|
arch::USER_STACK_SIZE,
|
||||||
|
entry::NO_EXECUTE | entry::WRITABLE | entry::USER_ACCESSIBLE,
|
||||||
|
true,
|
||||||
|
true
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut arg_size = 0;
|
||||||
|
for arg in args.iter() {
|
||||||
|
sp -= mem::size_of::<usize>();
|
||||||
|
unsafe { *(sp as *mut usize) = arch::USER_ARG_OFFSET + arg_size; }
|
||||||
|
sp -= mem::size_of::<usize>();
|
||||||
|
unsafe { *(sp as *mut usize) = arg.len(); }
|
||||||
|
|
||||||
|
arg_size += arg.len();
|
||||||
|
}
|
||||||
|
|
||||||
|
sp -= mem::size_of::<usize>();
|
||||||
|
unsafe { *(sp as *mut usize) = args.len(); }
|
||||||
|
|
||||||
|
if arg_size > 0 {
|
||||||
|
let mut memory = context::memory::Memory::new(
|
||||||
|
VirtualAddress::new(arch::USER_ARG_OFFSET),
|
||||||
|
arg_size,
|
||||||
|
entry::NO_EXECUTE | entry::WRITABLE,
|
||||||
|
true,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut arg_offset = 0;
|
||||||
|
for arg in args.iter() {
|
||||||
|
unsafe {
|
||||||
|
memcpy((arch::USER_ARG_OFFSET + arg_offset) as *mut u8,
|
||||||
|
arg.as_ptr(),
|
||||||
|
arg.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
arg_offset += arg.len();
|
||||||
|
}
|
||||||
|
|
||||||
|
memory.remap(entry::NO_EXECUTE | entry::USER_ACCESSIBLE, true);
|
||||||
|
|
||||||
|
context.image.push(memory.to_shared());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
println!("failed to execute {}: {}", unsafe { str::from_utf8_unchecked(path) }, err);
|
||||||
|
return Err(Error::NoExec);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let _ = syscall::close(file);
|
|
||||||
|
|
||||||
match elf::Elf::from(&data) {
|
// Go to usermode
|
||||||
Ok(elf) => {
|
unsafe { usermode(entry, sp); }
|
||||||
elf.run().and(Ok(0))
|
|
||||||
},
|
|
||||||
Err(err) => {
|
|
||||||
println!("failed to execute {}: {}", unsafe { str::from_utf8_unchecked(path) }, err);
|
|
||||||
Err(Error::NoExec)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getpid() -> Result<usize> {
|
pub fn getpid() -> Result<usize> {
|
||||||
|
@ -378,7 +494,7 @@ pub fn sched_yield() -> Result<usize> {
|
||||||
Ok(0)
|
Ok(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn waitpid(pid: usize, _status_ptr: usize, _options: usize) -> Result<usize> {
|
pub fn waitpid(pid: usize, status_ptr: usize, _options: usize) -> Result<usize> {
|
||||||
//TODO: Implement status_ptr and options
|
//TODO: Implement status_ptr and options
|
||||||
loop {
|
loop {
|
||||||
{
|
{
|
||||||
|
@ -389,7 +505,10 @@ pub fn waitpid(pid: usize, _status_ptr: usize, _options: usize) -> Result<usize>
|
||||||
let context_lock = contexts.get(pid).ok_or(Error::NoProcess)?;
|
let context_lock = contexts.get(pid).ok_or(Error::NoProcess)?;
|
||||||
let context = context_lock.read();
|
let context = context_lock.read();
|
||||||
if let context::Status::Exited(status) = context.status {
|
if let context::Status::Exited(status) = context.status {
|
||||||
//TODO: set status_ptr
|
if status_ptr != 0 {
|
||||||
|
let status_slice = validate_slice_mut(status_ptr as *mut usize, 1)?;
|
||||||
|
status_slice[0] = status;
|
||||||
|
}
|
||||||
exited = true;
|
exited = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
libstd
2
libstd
|
@ -1 +1 @@
|
||||||
Subproject commit 3483097e4f5172fe8b4cbe0dee868d18201c12f8
|
Subproject commit 0d434cc168b1d88211f1a3b72c8290c911432826
|
|
@ -126,8 +126,8 @@ pub fn dup(fd: usize) -> Result<usize> {
|
||||||
unsafe { syscall1(SYS_DUP, fd) }
|
unsafe { syscall1(SYS_DUP, fd) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execve(path: &str) -> Result<usize> {
|
pub fn execve(path: &str, args: &[[usize; 2]]) -> Result<usize> {
|
||||||
unsafe { syscall2(SYS_EXECVE, path.as_ptr() as usize, path.len()) }
|
unsafe { syscall4(SYS_EXECVE, path.as_ptr() as usize, path.len(), args.as_ptr() as usize, args.len()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exit(status: usize) -> Result<usize> {
|
pub fn exit(status: usize) -> Result<usize> {
|
||||||
|
|
Loading…
Reference in a new issue