From 575a1ff1370b73c109fcaaa33e3488019582ee13 Mon Sep 17 00:00:00 2001 From: thesayol Date: Mon, 8 Apr 2024 14:10:41 +0800 Subject: [PATCH] add x86_64 syscalls --- .../src/imp/execve/load_elf.rs | 23 +++++++--- api/ruxos_posix_api/src/imp/execve/mod.rs | 17 ++++++-- api/ruxos_posix_api/src/imp/fs.rs | 8 ++-- ulib/ruxmusl/src/aarch64/mod.rs | 4 +- ulib/ruxmusl/src/x86_64/mod.rs | 43 ++++++++++++++++++- ulib/ruxmusl/src/x86_64/syscall_id.rs | 31 +++++++++++++ 6 files changed, 109 insertions(+), 17 deletions(-) diff --git a/api/ruxos_posix_api/src/imp/execve/load_elf.rs b/api/ruxos_posix_api/src/imp/execve/load_elf.rs index a058c2ca8..f901044f6 100644 --- a/api/ruxos_posix_api/src/imp/execve/load_elf.rs +++ b/api/ruxos_posix_api/src/imp/execve/load_elf.rs @@ -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); @@ -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 @@ -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 @@ -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, @@ -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 } diff --git a/api/ruxos_posix_api/src/imp/execve/mod.rs b/api/ruxos_posix_api/src/imp/execve/mod.rs index 6ab1001f9..be26b304a 100644 --- a/api/ruxos_posix_api/src/imp/execve/mod.rs +++ b/api/ruxos_posix_api/src/imp/execve/mod.rs @@ -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 @@ -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, {} @@ -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"); } diff --git a/api/ruxos_posix_api/src/imp/fs.rs b/api/ruxos_posix_api/src/imp/fs.rs index c63a30865..7ca8127f7 100644 --- a/api/ruxos_posix_api/src/imp/fs.rs +++ b/api/ruxos_posix_api/src/imp/fs.rs @@ -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 @@ -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)?; diff --git a/ulib/ruxmusl/src/aarch64/mod.rs b/ulib/ruxmusl/src/aarch64/mod.rs index fa8480c46..6dac0f0b5 100644 --- a/ulib/ruxmusl/src/aarch64/mod.rs +++ b/ulib/ruxmusl/src/aarch64/mod.rs @@ -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], diff --git a/ulib/ruxmusl/src/x86_64/mod.rs b/ulib/ruxmusl/src/x86_64/mod.rs index 19792017a..9d5ed0089 100644 --- a/ulib/ruxmusl/src/x86_64/mod.rs +++ b/ulib/ruxmusl/src/x86_64/mod.rs @@ -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 { @@ -267,6 +267,12 @@ 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 _, @@ -274,6 +280,9 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize { #[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")] @@ -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, @@ -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) @@ -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], @@ -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, diff --git a/ulib/ruxmusl/src/x86_64/syscall_id.rs b/ulib/ruxmusl/src/x86_64/syscall_id.rs index 7992518ad..d0493c156 100644 --- a/ulib/ruxmusl/src/x86_64/syscall_id.rs +++ b/ulib/ruxmusl/src/x86_64/syscall_id.rs @@ -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")] @@ -143,6 +149,9 @@ pub enum SyscallId { #[cfg(feature = "fs")] GETCWD = 79, + #[cfg(feature = "fs")] + CHDIR = 80, + #[cfg(feature = "fs")] RENAME = 82, @@ -166,6 +175,22 @@ 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, @@ -173,6 +198,9 @@ pub enum SyscallId { ARCH_PRCTL = 158, + #[cfg(feature = "multitask")] + GETTID = 186, + #[cfg(feature = "multitask")] FUTEX = 202, @@ -213,6 +241,9 @@ pub enum SyscallId { #[cfg(feature = "fs")] READLINKAT = 267, + #[cfg(feature = "fs")] + FACCESSAT = 269, + #[cfg(feature = "select")] PSELECT6 = 270,