Skip to content

Commit

Permalink
Merge pull request #33 from niluxv/stable_asm_port
Browse files Browse the repository at this point in the history
Port some platforms from `llvm_asm` to `asm`
  • Loading branch information
japaric committed Feb 4, 2022
2 parents 431ecf9 + 99c13cb commit c0ab4c2
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 190 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
# asm! is stable and llvm_asm! still exists
toolchain: nightly-2022-01-14
target: ${{ matrix.target }}
override: true

Expand Down
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@

// Reference http://man7.org/linux/man-pages/man2/syscall.2.html

#![allow(deprecated)] // llvm_asm!
#![deny(warnings)]
#![feature(llvm_asm)]
#![no_std]
#![cfg_attr(any(
target_arch = "arm",
target_arch = "mips",
target_arch = "mips64",
target_arch = "powerpc",
target_arch = "powerpc64",
target_arch = "sparc64",
target_arch = "x86"
), feature(llvm_asm))]

#[cfg(test)]
extern crate std;
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 c0ab4c2

Please sign in to comment.