Skip to content

Commit

Permalink
Port several platforms from llvm_asm to asm where the change is t…
Browse files Browse the repository at this point in the history
…rivial

The reason for this change is that `llvm_asm` is removed from nightly, so it
doesn't compile anymore on any current compiler, while `asm` is being stabelised
(currently stable on beta).

The major ommission in this commit is the linux x86 (32 bit) platform. The new `asm`
macro doesn't allow the esi and ebp registers to be used as inputs or outputs or
clobbers, which makes the port non-trivial for this platform. Similarly on armeabi
the r6 register is not allowed as input, output or clobber.
  • Loading branch information
niluxv committed Jan 30, 2022
1 parent 431ecf9 commit 2ede791
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 189 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// Reference http://man7.org/linux/man-pages/man2/syscall.2.html

#![deny(warnings)]
#![feature(llvm_asm)]
#![no_std]

#[cfg(test)]
Expand Down
138 changes: 86 additions & 52 deletions src/platform/freebsd-x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,95 +9,129 @@

//! This library was built for x86-64 FreeBSD.

use core::arch::asm;

pub mod nr;

#[inline(always)]
pub unsafe fn syscall0(n: usize) -> usize {
let ret: usize;
llvm_asm!("syscall" : "={rax}"(ret)
: "{rax}"(n)
: "rcx", "r11", "memory"
: "volatile");
ret
pub unsafe fn syscall0(mut n: usize) -> usize {
asm!(
"syscall",
inout("rax") n,
out("rcx") _,
out("r11") _,
options(nostack),
);
n
}

#[inline(always)]
pub unsafe fn syscall1(n: usize, a1: usize) -> usize {
let ret: usize;
llvm_asm!("syscall" : "={rax}"(ret)
: "{rax}"(n), "{rdi}"(a1)
: "rcx", "r11", "memory"
: "volatile");
ret
pub unsafe fn syscall1(mut n: usize, a1: usize) -> usize {
asm!(
"syscall",
inout("rax") n,
in("rdi") a1,
out("rcx") _,
out("r11") _,
options(nostack),
);
n
}

#[inline(always)]
pub unsafe fn syscall2(n: usize, a1: usize, a2: usize) -> usize {
let ret: usize;
llvm_asm!("syscall" : "={rax}"(ret)
: "{rax}"(n), "{rdi}"(a1), "{rsi}"(a2)
: "rcx", "r11", "memory"
: "volatile");
ret
pub unsafe fn syscall2(mut n: usize, a1: usize, a2: usize) -> usize {
asm!(
"syscall",
inout("rax") n,
in("rdi") a1,
in("rsi") a2,
out("rcx") _,
out("r11") _,
options(nostack),
);
n
}

#[inline(always)]
pub unsafe fn syscall3(n: usize, a1: usize, a2: usize, a3: usize) -> usize {
let ret: usize;
llvm_asm!("syscall" : "={rax}"(ret)
: "{rax}"(n), "{rdi}"(a1), "{rsi}"(a2), "{rdx}"(a3)
: "rcx", "r11", "memory"
: "volatile");
ret
pub unsafe fn syscall3(mut n: usize, a1: usize, a2: usize, a3: usize) -> usize {
asm!(
"syscall",
inout("rax") n,
in("rdi") a1,
in("rsi") a2,
in("rdx") a3,
out("rcx") _,
out("r11") _,
options(nostack),
);
n
}

#[inline(always)]
pub unsafe fn syscall4(n: usize,
pub unsafe fn syscall4(mut n: usize,
a1: usize,
a2: usize,
a3: usize,
a4: usize)
-> usize {
let ret: usize;
llvm_asm!("syscall" : "={rax}"(ret)
: "{rax}"(n), "{rdi}"(a1), "{rsi}"(a2), "{rdx}"(a3),
"{r10}"(a4)
: "rcx", "r11", "memory"
: "volatile");
ret
asm!(
"syscall",
inout("rax") n,
in("rdi") a1,
in("rsi") a2,
in("rdx") a3,
in("r10") a4,
out("rcx") _,
out("r11") _,
options(nostack),
);
n
}

#[inline(always)]
pub unsafe fn syscall5(n: usize,
pub unsafe fn syscall5(mut n: usize,
a1: usize,
a2: usize,
a3: usize,
a4: usize,
a5: usize)
-> usize {
let ret: usize;
llvm_asm!("syscall" : "={rax}"(ret)
: "{rax}"(n), "{rdi}"(a1), "{rsi}"(a2), "{rdx}"(a3),
"{r10}"(a4), "{r8}"(a5)
: "rcx", "r11", "memory"
: "volatile");
ret
asm!(
"syscall",
inout("rax") n,
in("rdi") a1,
in("rsi") a2,
in("rdx") a3,
in("r10") a4,
in("r8") a5,
out("rcx") _,
out("r11") _,
options(nostack),
);
n
}

#[inline(always)]
pub unsafe fn syscall6(n: usize,
pub unsafe fn syscall6(mut n: usize,
a1: usize,
a2: usize,
a3: usize,
a4: usize,
a5: usize,
a6: usize)
-> usize {
let ret: usize;
llvm_asm!("syscall" : "={rax}"(ret)
: "{rax}"(n), "{rdi}"(a1), "{rsi}"(a2), "{rdx}"(a3),
"{r10}"(a4), "{r8}"(a5), "{r9}"(a6)
: "rcx", "r11", "memory"
: "volatile");
ret
asm!(
"syscall",
inout("rax") n,
in("rdi") a1,
in("rsi") a2,
in("rdx") a3,
in("r10") a4,
in("r8") a5,
in("r9") a6,
out("rcx") _,
out("r11") _,
options(nostack),
);
n
}
94 changes: 59 additions & 35 deletions src/platform/linux-aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,58 @@

//! This library was built for aarch64 Linux.

use core::arch::asm;

pub mod nr;

#[inline(always)]
pub unsafe fn syscall0(n: usize) -> usize {
let ret: usize;
llvm_asm!("svc 0"
: "={x0}"(ret)
: "{x8}"(n)
: "memory" "cc"
: "volatile");
asm!(
"svc 0",
in("x8") n,
out("x0") ret,
options(nostack),
);
ret
}

#[inline(always)]
pub unsafe fn syscall1(n: usize, a1: usize) -> usize {
let ret: usize;
llvm_asm!("svc 0"
: "={x0}"(ret)
: "{x8}"(n) "{x0}"(a1)
: "memory" "cc"
: "volatile");
asm!(
"svc 0",
in("x8") n,
inout("x0") a1 => ret,
options(nostack),
);
ret
}

#[inline(always)]
pub unsafe fn syscall2(n: usize, a1: usize, a2: usize) -> usize {
let ret: usize;
llvm_asm!("svc 0"
: "={x0}"(ret)
: "{x8}"(n) "{x0}"(a1) "{x1}"(a2)
: "memory" "cc"
: "volatile");
asm!(
"svc 0",
in("x8") n,
inout("x0") a1 => ret,
in("x1") a2,
options(nostack),
);
ret
}

#[inline(always)]
pub unsafe fn syscall3(n: usize, a1: usize, a2: usize, a3: usize) -> usize {
let ret: usize;
llvm_asm!("svc 0"
: "={x0}"(ret)
: "{x8}"(n) "{x0}"(a1) "{x1}"(a2) "{x2}"(a3)
: "memory" "cc"
: "volatile");
asm!(
"svc 0",
in("x8") n,
inout("x0") a1 => ret,
in("x1") a2,
in("x2") a3,
options(nostack),
);
ret
}

Expand All @@ -63,11 +72,15 @@ pub unsafe fn syscall4(n: usize,
a4: usize)
-> usize {
let ret: usize;
llvm_asm!("svc 0"
: "={x0}"(ret)
: "{x8}"(n) "{x0}"(a1) "{x1}"(a2) "{x2}"(a3) "{x3}"(a4)
: "memory" "cc"
: "volatile");
asm!(
"svc 0",
in("x8") n,
inout("x0") a1 => ret,
in("x1") a2,
in("x2") a3,
in("x3") a4,
options(nostack),
);
ret
}

Expand All @@ -80,10 +93,16 @@ pub unsafe fn syscall5(n: usize,
a5: usize)
-> usize {
let ret: usize;
llvm_asm!("svc 0" : "={x0}"(ret)
: "{x8}"(n) "{x0}"(a1) "{x1}"(a2) "{x2}"(a3) "{x3}"(a4) "{x4}"(a5)
: "memory" "cc"
: "volatile");
asm!(
"svc 0",
in("x8") n,
inout("x0") a1 => ret,
in("x1") a2,
in("x2") a3,
in("x3") a4,
in("x4") a5,
options(nostack),
);
ret
}

Expand All @@ -97,11 +116,16 @@ pub unsafe fn syscall6(n: usize,
a6: usize)
-> usize {
let ret: usize;
llvm_asm!("svc 0"
: "={x0}"(ret)
: "{x8}"(n) "{x0}"(a1) "{x1}"(a2) "{x2}"(a3) "{x3}"(a4) "{x4}"(a5)
"{x5}"(a6)
: "memory" "cc"
: "volatile");
asm!(
"svc 0",
in("x8") n,
inout("x0") a1 => ret,
in("x1") a2,
in("x2") a3,
in("x3") a4,
in("x4") a5,
in("x5") a6,
options(nostack),
);
ret
}
Loading

0 comments on commit 2ede791

Please sign in to comment.