Skip to content

Commit

Permalink
combine syscall_<param_num> to syscall_4
Browse files Browse the repository at this point in the history
  • Loading branch information
templexxx committed Nov 5, 2022
1 parent eb53480 commit 180a5e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 52 deletions.
26 changes: 13 additions & 13 deletions omo/src/os/linux/file.rs
Original file line number Diff line number Diff line change
@@ -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<i64, EmulatorError> {
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 {
Expand All @@ -15,7 +15,7 @@ pub fn open(path: *const u8, flags: u64, mode: u64) -> Result<i64, EmulatorError
}

pub fn read(fd: u64, buf: *mut u8, size: u64) -> Result<i64, EmulatorError> {
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 {
Expand All @@ -24,7 +24,7 @@ pub fn read(fd: u64, buf: *mut u8, size: u64) -> Result<i64, EmulatorError> {
}

pub fn write(fd: u64, data: *const u8, len: u64) -> Result<i64, EmulatorError> {
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 {
Expand All @@ -33,7 +33,7 @@ pub fn write(fd: u64, data: *const u8, len: u64) -> Result<i64, EmulatorError> {
}

pub fn close(fd: u64) -> Result<i64, EmulatorError> {
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 {
Expand All @@ -42,7 +42,7 @@ pub fn close(fd: u64) -> Result<i64, EmulatorError> {
}

pub fn lseek(fd: u64, offset: u64, whence: u64) -> Result<i64, EmulatorError> {
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 {
Expand All @@ -51,7 +51,7 @@ pub fn lseek(fd: u64, offset: u64, whence: u64) -> Result<i64, EmulatorError> {
}

pub fn fcntl(fd: u64, cmd: u64, arg: u64) -> Result<i64, EmulatorError> {
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 {
Expand All @@ -61,12 +61,12 @@ pub fn fcntl(fd: u64, cmd: u64, arg: u64) -> Result<i64, EmulatorError> {

pub fn readlink(path: *const u8, buf: *mut u8, buf_size: u64) -> Result<i64, EmulatorError> {
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))
Expand All @@ -76,7 +76,7 @@ pub fn readlink(path: *const u8, buf: *mut u8, buf_size: u64) -> Result<i64, Emu
}

pub fn stat(path: *const u8, stat_buf: *mut StatX8664) -> Result<i64, EmulatorError> {
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 {
Expand All @@ -85,7 +85,7 @@ pub fn stat(path: *const u8, stat_buf: *mut StatX8664) -> Result<i64, EmulatorEr
}

pub fn fstat(fd: u64, stat_buf: *mut StatX8664) -> Result<i64, EmulatorError> {
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 {
Expand All @@ -94,7 +94,7 @@ pub fn fstat(fd: u64, stat_buf: *mut StatX8664) -> Result<i64, EmulatorError> {
}

pub fn lstat(path: *const u8, stat_buf: *mut StatX8664) -> Result<i64, EmulatorError> {
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 {
Expand Down Expand Up @@ -125,7 +125,7 @@ pub fn fstatat64(
}

pub fn ioctl(fd: u64, cmd: u64, arg: u64) -> Result<i64, EmulatorError> {
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 {
Expand Down
40 changes: 1 addition & 39 deletions omo/src/os/linux/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit 180a5e0

Please sign in to comment.