Skip to content

Commit

Permalink
add x86_64 syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayol committed Apr 8, 2024
1 parent 4f32954 commit 575a1ff
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 17 deletions.
23 changes: 17 additions & 6 deletions api/ruxos_posix_api/src/imp/execve/load_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl ElfProg {
pub fn new(filepath: *const c_char) -> Self {
let name = ptr2vec(filepath);
let path = ptr2vec(filepath);
debug!("new elf prog: {}", vec_u8_to_str(&path));
debug!("sys_execve: new elf prog: {}", vec_u8_to_str(&path));

// open file
let fd = sys_open(filepath, ctypes::O_RDWR as i32, 0);
Expand All @@ -42,7 +42,10 @@ impl ElfProg {

// read file
let buf = sys_mmap(null_mut(), bufsize, 0, 0, fd, 0);
debug!("mmap to read file at {:p} size {:x}", buf, bufsize);
debug!(
"sys_execve: mmap to read file at {:p} size {:x}",
buf, bufsize
);
sys_read(fd, buf, bufsize);

// parse elf
Expand All @@ -61,7 +64,7 @@ impl ElfProg {

// mmap memory to copy LOAD segmeants
let a = crate::sys_mmap(null_mut(), msize as usize, 0, 0, 0, 0);
debug!("mmap to copy LOAD at {:p} size {:x}", a, msize);
debug!("sys_execve: mmap to copy LOAD at {:p} size {:x}", a, msize);
let base = a as usize;

// copy LOAD segs into base
Expand Down Expand Up @@ -95,13 +98,18 @@ impl ElfProg {

let sec = file.section_header_by_name(".got").unwrap().unwrap();
let addr = sec.sh_addr as usize + base;
debug!("got vaddr = {:#X}", addr);
debug!("sys_execve: got vaddr = {:#X}", addr);

// return Self
let mut platform = Vec::new();
#[cfg(target_arch = "aarch64")]
for c in b"aarch64" {
platform.push(*c);
}
#[cfg(target_arch = "x86_64")]
for c in b"x86_64" {
platform.push(*c);
}
let ret = Self {
base,
entry,
Expand All @@ -121,14 +129,17 @@ impl ElfProg {
.unwrap()
.sh_offset;
debug!(
"loaded ELF in 0x{:x}, .text is 0x{:x}",
"sys_execve: loaded ELF in 0x{:x}, .text is 0x{:x}",
ret.base,
ret.base + text_off as usize
);

// unmap file
sys_munmap(buf, bufsize);
debug!("unmap ELF file in memory at {:p} len {:x}", buf, bufsize);
debug!(
"sys_execve: unmap ELF file in memory at {:p} len {:x}",
buf, bufsize
);

ret
}
Expand Down
17 changes: 14 additions & 3 deletions api/ruxos_posix_api/src/imp/execve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {
let interp_prog = load_elf::ElfProg::new(prog.interp_path);
entry = interp_prog.entry;
at_base = interp_prog.base;
debug!("INTERP base is {:x}", at_base);
debug!("sys_execve: INTERP base is {:x}", at_base);
};

// create stack
Expand Down Expand Up @@ -124,10 +124,11 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {

// try run
debug!(
"run at entry 0x{entry:x}, then it will jump to 0x{:x} ",
"sys_execve: run at entry 0x{entry:x}, then it will jump to 0x{:x} ",
prog.entry
);

#[cfg(target_arch = "aarch64")]
unsafe {
core::arch::asm!("
mov sp, {}
Expand All @@ -137,6 +138,16 @@ pub fn sys_execve(pathname: *const c_char, argv: usize, envp: usize) -> ! {
in(reg)entry,
);
}
#[cfg(target_arch = "x86_64")]
unsafe {
core::arch::asm!("
mov rsp, {}
jmp {}
",
in(reg)sp,
in(reg)entry,
);
}

unreachable!("should not return");
unreachable!("sys_execve: unknown arch");
}
8 changes: 4 additions & 4 deletions api/ruxos_posix_api/src/imp/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,8 @@ pub unsafe fn sys_preadv(
/// If pathname is a symbolic link, it is dereferenced.
/// The mode is either the value F_OK, for the existence of the file,
/// or a mask consisting of the bitwise OR of one or more of R_OK, W_OK, and X_OK, for the read, write, execute permissions.
pub fn sys_faccessat(dirfd: c_int, pathname: usize, mode: c_int, flags: c_int) -> c_int {
let path = char_ptr_to_str(pathname as *const c_char).unwrap();
pub fn sys_faccessat(dirfd: c_int, pathname: *const c_char, mode: c_int, flags: c_int) -> c_int {
let path = char_ptr_to_str(pathname).unwrap();
debug!(
"sys_faccessat <= dirfd {} path {} mode {} flags {}",
dirfd, path, mode, flags
Expand All @@ -676,8 +676,8 @@ pub fn sys_faccessat(dirfd: c_int, pathname: usize, mode: c_int, flags: c_int) -
}

/// changes the current working directory to the directory specified in path.
pub fn sys_chdir(path: usize) -> c_int {
let p = char_ptr_to_str(path as *const c_char).unwrap();
pub fn sys_chdir(path: *const c_char) -> c_int {
let p = char_ptr_to_str(path).unwrap();
debug!("sys_chdir <= path: {}", p);
syscall_body!(sys_chdir, {
set_current_dir(p)?;
Expand Down
4 changes: 2 additions & 2 deletions ulib/ruxmusl/src/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize {
#[cfg(feature = "fs")]
SyscallId::FACCESSAT => ruxos_posix_api::sys_faccessat(
args[0] as c_int,
args[1] as usize,
args[1] as *const c_char,
args[2] as c_int,
args[3] as c_int,
) as _,
#[cfg(feature = "fs")]
SyscallId::CHDIR => ruxos_posix_api::sys_chdir(args[0] as usize) as _,
SyscallId::CHDIR => ruxos_posix_api::sys_chdir(args[0] as *const c_char) as _,
#[cfg(feature = "fs")]
SyscallId::OPENAT => ruxos_posix_api::sys_openat(
args[0],
Expand Down
43 changes: 41 additions & 2 deletions ulib/ruxmusl/src/x86_64/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod syscall_id;

use core::ffi::{c_int, c_ulong, c_void};
use ruxos_posix_api::ctypes;
use core::ffi::{c_char, c_int, c_ulong, c_void};
use ruxos_posix_api::ctypes::{self, gid_t, pid_t, uid_t};
use syscall_id::SyscallId;

pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize {
Expand Down Expand Up @@ -267,13 +267,22 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize {
args[5] as *mut c_void,
) as _,

#[cfg(feature = "fs")]
#[allow(unreachable_code)]
SyscallId::EXECVE => {
ruxos_posix_api::sys_execve(args[0] as *const c_char, args[1], args[2]) as _
}

#[cfg(not(feature = "multitask"))]
SyscallId::EXIT => ruxos_posix_api::sys_exit(args[0] as c_int) as _,

#[allow(unreachable_code)]
#[cfg(feature = "multitask")]
SyscallId::EXIT => ruxos_posix_api::sys_pthread_exit(args[0] as *mut c_void) as _,

#[cfg(feature = "signal")]
SyscallId::KILL => ruxos_posix_api::sys_kill(args[0] as pid_t, args[1] as c_int) as _,

SyscallId::UNAME => ruxos_posix_api::sys_uname(args[0] as *mut c_void) as _,

#[cfg(feature = "fd")]
Expand All @@ -299,6 +308,9 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize {
ruxos_posix_api::sys_getcwd(args[0] as *mut core::ffi::c_char, args[1]) as _
}

#[cfg(feature = "fs")]
SyscallId::CHDIR => ruxos_posix_api::sys_chdir(args[0] as *const c_char) as _,

#[cfg(feature = "fs")]
SyscallId::RENAME => ruxos_posix_api::sys_rename(
args[0] as *const core::ffi::c_char,
Expand Down Expand Up @@ -345,6 +357,22 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize {
ruxos_posix_api::sys_sysinfo(args[0] as *mut ctypes::sysinfo) as _
}

SyscallId::TIMES => ruxos_posix_api::sys_times(args[0] as *mut usize) as _,

SyscallId::GETUID => ruxos_posix_api::sys_getuid() as _,

SyscallId::GETGID => ruxos_posix_api::sys_getgid() as _,

SyscallId::SETUID => ruxos_posix_api::sys_setuid(args[0] as uid_t) as _,

SyscallId::SETGID => ruxos_posix_api::sys_setgid(args[0] as gid_t) as _,

SyscallId::GETPPID => ruxos_posix_api::sys_getppid() as _,

SyscallId::GETPGID => ruxos_posix_api::sys_getpgid(args[0] as pid_t) as _,

SyscallId::CAPGET => ruxos_posix_api::sys_cap_get(args[0], args[1]) as _,

#[cfg(feature = "signal")]
SyscallId::SIGALTSTACK => {
ruxos_posix_api::sys_sigaltstack(args[0] as *const c_void, args[1] as *mut c_void)
Expand All @@ -363,6 +391,9 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize {
ruxos_posix_api::sys_arch_prctl(args[0] as c_int, args[1] as c_ulong) as _
}

#[cfg(feature = "multitask")]
SyscallId::GETTID => ruxos_posix_api::sys_gettid() as _,

#[cfg(feature = "multitask")]
SyscallId::FUTEX => ruxos_posix_api::sys_futex(
args[0],
Expand Down Expand Up @@ -458,6 +489,14 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize {
args[3],
) as _,

#[cfg(feature = "fs")]
SyscallId::FACCESSAT => ruxos_posix_api::sys_faccessat(
args[0] as c_int,
args[1] as *const c_char,
args[2] as c_int,
args[3] as c_int,
) as _,

#[cfg(feature = "select")]
SyscallId::PSELECT6 => ruxos_posix_api::sys_pselect6(
args[0] as c_int,
Expand Down
31 changes: 31 additions & 0 deletions ulib/ruxmusl/src/x86_64/syscall_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ pub enum SyscallId {
#[cfg(feature = "multitask")]
CLONE = 56,

#[cfg(feature = "fs")]
EXECVE = 59,

EXIT = 60,

#[cfg(feature = "signal")]
KILL = 62,

UNAME = 63,

#[cfg(feature = "fd")]
Expand All @@ -143,6 +149,9 @@ pub enum SyscallId {
#[cfg(feature = "fs")]
GETCWD = 79,

#[cfg(feature = "fs")]
CHDIR = 80,

#[cfg(feature = "fs")]
RENAME = 82,

Expand All @@ -166,13 +175,32 @@ pub enum SyscallId {

SYSINFO = 99,

TIMES = 100,

GETUID = 102,

GETGID = 104,

SETUID = 105,

SETGID = 106,

GETPPID = 110,

GETPGID = 121,

CAPGET = 125,

#[cfg(feature = "signal")]
SIGALTSTACK = 131,

PRCTL = 157,

ARCH_PRCTL = 158,

#[cfg(feature = "multitask")]
GETTID = 186,

#[cfg(feature = "multitask")]
FUTEX = 202,

Expand Down Expand Up @@ -213,6 +241,9 @@ pub enum SyscallId {
#[cfg(feature = "fs")]
READLINKAT = 267,

#[cfg(feature = "fs")]
FACCESSAT = 269,

#[cfg(feature = "select")]
PSELECT6 = 270,

Expand Down

0 comments on commit 575a1ff

Please sign in to comment.