From 180a5e0d30b57978ce2818d10e1c0cff92082a94 Mon Sep 17 00:00:00 2001 From: templexxx Date: Sat, 5 Nov 2022 15:15:29 +0800 Subject: [PATCH] combine syscall_ to syscall_4 --- omo/src/os/linux/file.rs | 26 ++++++++++++------------ omo/src/os/linux/syscall.rs | 40 +------------------------------------ 2 files changed, 14 insertions(+), 52 deletions(-) diff --git a/omo/src/os/linux/file.rs b/omo/src/os/linux/file.rs index b045164..8e7f6d2 100644 --- a/omo/src/os/linux/file.rs +++ b/omo/src/os/linux/file.rs @@ -1,12 +1,12 @@ use crate::{ - errors::{from_raw_syscall_ret, EmulatorError}, + errors::{EmulatorError, from_raw_syscall_ret}, os::linux::syscall::*, }; pub fn open(path: *const u8, flags: u64, mode: u64) -> Result { let flags = flags & 0xffffffff; let open_mode = mode & 0x7fffffff; - let fd = unsafe { syscall_3(LinuxSysCalls::Open as u64, path as u64, flags, open_mode) }; + let fd = unsafe { syscall_4(LinuxSysCalls::Open as u64, path as u64, flags, open_mode, 0) }; if fd < 0 { Err(from_raw_syscall_ret(fd)) } else { @@ -15,7 +15,7 @@ pub fn open(path: *const u8, flags: u64, mode: u64) -> Result Result { - let size = unsafe { syscall_3(LinuxSysCalls::Read as u64, fd, buf as u64, size) }; + let size = unsafe { syscall_4(LinuxSysCalls::Read as u64, fd, buf as u64, size, 0) }; if size < 0 { Err(from_raw_syscall_ret(size)) } else { @@ -24,7 +24,7 @@ pub fn read(fd: u64, buf: *mut u8, size: u64) -> Result { } pub fn write(fd: u64, data: *const u8, len: u64) -> Result { - let size = unsafe { syscall_3(LinuxSysCalls::Write as u64, fd, data as u64, len) }; + let size = unsafe { syscall_4(LinuxSysCalls::Write as u64, fd, data as u64, len, 0) }; if size < 0 { Err(from_raw_syscall_ret(size)) } else { @@ -33,7 +33,7 @@ pub fn write(fd: u64, data: *const u8, len: u64) -> Result { } pub fn close(fd: u64) -> Result { - let ret = unsafe { syscall_1(LinuxSysCalls::Close as u64, fd) }; + let ret = unsafe { syscall_4(LinuxSysCalls::Close as u64, fd, 0, 0, 0) }; if ret < 0 { Err(from_raw_syscall_ret(ret)) } else { @@ -42,7 +42,7 @@ pub fn close(fd: u64) -> Result { } pub fn lseek(fd: u64, offset: u64, whence: u64) -> Result { - let off = unsafe { syscall_3(LinuxSysCalls::Lseek as u64, fd, offset, whence) }; + let off = unsafe { syscall_4(LinuxSysCalls::Lseek as u64, fd, offset, whence, 0) }; if off < 0 { Err(from_raw_syscall_ret(off)) } else { @@ -51,7 +51,7 @@ pub fn lseek(fd: u64, offset: u64, whence: u64) -> Result { } pub fn fcntl(fd: u64, cmd: u64, arg: u64) -> Result { - let ret = unsafe { syscall_3(LinuxSysCalls::Fcntl as u64, fd, cmd, arg) }; + let ret = unsafe { syscall_4(LinuxSysCalls::Fcntl as u64, fd, cmd, arg, 0) }; if ret < 0 { Err(from_raw_syscall_ret(ret)) } else { @@ -61,12 +61,12 @@ pub fn fcntl(fd: u64, cmd: u64, arg: u64) -> Result { pub fn readlink(path: *const u8, buf: *mut u8, buf_size: u64) -> Result { let ret = unsafe { - syscall_3( + syscall_4( LinuxSysCalls::Readlink as u64, path as u64, buf as u64, buf_size, - ) + 0) }; if ret < 0 { Err(from_raw_syscall_ret(ret)) @@ -76,7 +76,7 @@ pub fn readlink(path: *const u8, buf: *mut u8, buf_size: u64) -> Result Result { - let ret = unsafe { syscall_2(LinuxSysCalls::Stat as u64, path as u64, stat_buf as u64) }; + let ret = unsafe { syscall_4(LinuxSysCalls::Stat as u64, path as u64, stat_buf as u64, 0, 0) }; if ret < 0 { Err(from_raw_syscall_ret(ret)) } else { @@ -85,7 +85,7 @@ pub fn stat(path: *const u8, stat_buf: *mut StatX8664) -> Result Result { - let ret = unsafe { syscall_2(LinuxSysCalls::Fstat as u64, fd, stat_buf as u64) }; + let ret = unsafe { syscall_4(LinuxSysCalls::Fstat as u64, fd, stat_buf as u64, 0, 0) }; if ret < 0 { Err(from_raw_syscall_ret(ret)) } else { @@ -94,7 +94,7 @@ pub fn fstat(fd: u64, stat_buf: *mut StatX8664) -> Result { } pub fn lstat(path: *const u8, stat_buf: *mut StatX8664) -> Result { - let ret = unsafe { syscall_2(LinuxSysCalls::Lstat as u64, path as u64, stat_buf as u64) }; + let ret = unsafe { syscall_4(LinuxSysCalls::Lstat as u64, path as u64, stat_buf as u64, 0, 0) }; if ret < 0 { Err(from_raw_syscall_ret(ret)) } else { @@ -125,7 +125,7 @@ pub fn fstatat64( } pub fn ioctl(fd: u64, cmd: u64, arg: u64) -> Result { - let ret = unsafe { syscall_3(LinuxSysCalls::Ioctl as u64, fd, cmd, arg) }; + let ret = unsafe { syscall_4(LinuxSysCalls::Ioctl as u64, fd, cmd, arg, 0) }; if ret < 0 { Err(from_raw_syscall_ret(ret)) } else { diff --git a/omo/src/os/linux/syscall.rs b/omo/src/os/linux/syscall.rs index 90f282b..5bbaf21 100644 --- a/omo/src/os/linux/syscall.rs +++ b/omo/src/os/linux/syscall.rs @@ -25,44 +25,6 @@ pub enum LinuxSysCalls { Newfstatat = 262, } -// x86_64 raw syscall. -pub unsafe fn syscall_1(trap: u64, arg1: u64) -> i64 { - let res; - asm!( - "syscall", - in("rax") trap, - in("rdi") arg1, - lateout("rax") res, - ); - res -} - -pub unsafe fn syscall_2(trap: u64, arg1: u64, arg2: u64) -> i64 { - let res; - asm!( - "syscall", - in("rax") trap, - in("rdi") arg1, - in("rsi") arg2, - lateout("rax") res, - ); - res -} - -pub unsafe fn syscall_3(trap: u64, arg1: u64, arg2: u64, arg3: u64) -> i64 { - let res; - asm!( - "syscall", - in("rax") trap, - in("rdi") arg1, - in("rsi") arg2, - in("rdx") arg3, - lateout("rax") res, - ); - - res -} - pub unsafe fn syscall_4(trap: u64, arg1: u64, arg2: u64, arg3: u64, arg4: u64) -> i64 { let res; asm!( @@ -265,7 +227,7 @@ lazy_static! { #[allow(non_camel_case_types)] #[derive( - Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, EnumVariantNames, EnumString, +Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, EnumVariantNames, EnumString, )] #[serde(rename_all = "lowercase")] #[strum(serialize_all = "lowercase")]