feature: add execve() system call

This commit is contained in:
2021-11-05 00:42:14 +02:00
parent ee5daf32b9
commit ad5fac4bbb
6 changed files with 56 additions and 32 deletions
+1
View File
@@ -12,6 +12,7 @@ SECTIONS {
.text : {
*(.text._start)
*(.text*)
*(.eh_frame*)
} :text
. = ALIGN(0x1000);
-25
View File
@@ -7,30 +7,5 @@ extern crate libusr;
#[no_mangle]
fn main() -> i32 {
let mut buf = [0; 128];
print!("\x1B[2J\x1B[1;1H");
println!("Hello!");
loop {
print!("> ");
let count = unsafe { libusr::sys::sys_read(0, &mut buf) };
if count < 0 {
trace!("Read from stdio failed");
break;
}
let count = count as usize;
if let Ok(s) = core::str::from_utf8(&buf[..count]) {
println!("Got string {:?}", s);
if s == "quit" {
break;
}
} else {
println!("Got string (non-utf8) {:?}", &buf[..count]);
}
}
-1
}
+6
View File
@@ -405,6 +405,12 @@ impl Process {
unsafe {
SCHED.hack_current_pid(lock.id);
}
} else {
// Invalidate user ASID
let input = (lock.id.asid() as usize) << 48;
unsafe {
asm!("tlbi aside1, {}", in(reg) input);
}
}
let new_space = Space::alloc_empty()?;
+15 -1
View File
@@ -2,7 +2,7 @@
use crate::arch::platform::exception::ExceptionFrame;
use crate::debug::Level;
use crate::proc::{wait, Pid, Process};
use crate::proc::{elf, wait, Pid, Process};
use core::mem::size_of;
use core::time::Duration;
use error::Errno;
@@ -91,6 +91,20 @@ pub fn syscall(num: usize, args: &[usize]) -> Result<usize, Errno> {
io.close_file(fd)?;
Ok(0)
}
abi::SYS_EXECVE => {
let node = {
let proc = Process::current();
let mut io = proc.io.lock();
let filename = validate_user_str(args[0], args[1])?;
// TODO argv, envp array passing ABI?
let node = io.ioctx().find(None, filename, true)?;
drop(io);
node
};
let mut file = node.open(OpenFlags::O_RDONLY)?;
Process::execve(|space| elf::load_elf(space, &mut file), 0).unwrap();
panic!();
}
// Extra system calls
abi::SYS_EX_DEBUG_TRACE => {
+1
View File
@@ -9,3 +9,4 @@ pub const SYS_OPENAT: usize = 4;
pub const SYS_FSTATAT: usize = 5;
pub const SYS_CLOSE: usize = 6;
pub const SYS_FORK: usize = 7;
pub const SYS_EXECVE: usize = 8;
+33 -6
View File
@@ -1,5 +1,5 @@
use crate::abi;
use crate::stat::{Stat, OpenFlags, FileMode};
use crate::stat::{FileMode, OpenFlags, Stat};
// TODO document the syscall ABI
@@ -48,11 +48,15 @@ macro_rules! syscall {
/// Integer/size argument
macro_rules! argn {
($a:expr) => ($a as usize)
($a:expr) => {
$a as usize
};
}
/// Pointer/base argument
macro_rules! argp {
($a:expr) => ($a as *mut core::ffi::c_void as usize)
($a:expr) => {
$a as *mut core::ffi::c_void as usize
};
}
// /// Immutable pointer/base argument
// macro_rules! argpi {
@@ -77,7 +81,11 @@ pub unsafe fn sys_ex_nanosleep(ns: u64, rem: &mut [u64; 2]) -> i32 {
#[inline(always)]
pub unsafe fn sys_ex_debug_trace(msg: &[u8]) -> usize {
syscall!(abi::SYS_EX_DEBUG_TRACE, argp!(msg.as_ptr()), argn!(msg.len()))
syscall!(
abi::SYS_EX_DEBUG_TRACE,
argp!(msg.as_ptr()),
argn!(msg.len())
)
}
#[inline(always)]
@@ -94,12 +102,22 @@ pub unsafe fn sys_openat(at: i32, pathname: &str, mode: FileMode, flags: OpenFla
#[inline(always)]
pub unsafe fn sys_read(fd: i32, data: &mut [u8]) -> isize {
syscall!(abi::SYS_READ, argn!(fd), argp!(data.as_mut_ptr()), argn!(data.len())) as isize
syscall!(
abi::SYS_READ,
argn!(fd),
argp!(data.as_mut_ptr()),
argn!(data.len())
) as isize
}
#[inline(always)]
pub unsafe fn sys_write(fd: i32, data: &[u8]) -> isize {
syscall!(abi::SYS_WRITE, argn!(fd), argp!(data.as_ptr()), argn!(data.len())) as isize
syscall!(
abi::SYS_WRITE,
argn!(fd),
argp!(data.as_ptr()),
argn!(data.len())
) as isize
}
#[inline(always)]
@@ -118,3 +136,12 @@ pub unsafe fn sys_fstatat(at: i32, pathname: &str, statbuf: &mut Stat, flags: i3
pub unsafe fn sys_fork() -> i32 {
syscall!(abi::SYS_FORK) as i32
}
#[inline(always)]
pub unsafe fn sys_execve(pathname: &str) -> i32 {
syscall!(
abi::SYS_EXECVE,
argp!(pathname.as_ptr()),
argn!(pathname.len())
) as i32
}