1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//! Invokes an OS system-call handler at privilege level 0.
//!
//! It does so by loading RIP from the IA32_LSTAR MSR (after saving the address of the instruction
//! following SYSCALL into RCX).
//!
//! The code follows "A.2 AMD64 Linux Kernel Conventions" of System V Application
//! Binary Interface AMD64 Architecture Processor Supplement:
//!
//! * The kernel interface uses %rdi, %rsi, %rdx, %r10, %r8 and %r9.
//! * A system-call is done via the syscall instruction. The kernel destroys registers %rcx and %r11.
//! * The number of the syscall has to be passed in register %rax.
//! * System-calls are limited to six arguments, no argument is passed directly on the stack.
//! * Returning from the syscall, register %rax contains the result of the system-call.
//! * Only values of class INTEGER or class MEMORY are passed to the kernel.
//!
//! This code is inspired by the syscall.rs (https://github.com/kmcallister/syscall.rs/) project.

#[cfg(target_arch = "x86_64")]
use core::arch::asm;

#[macro_export]
macro_rules! syscall {
    ($arg0:expr) => {
        x86::bits64::syscall::syscall0($arg0 as u64)
    };

    ($arg0:expr, $arg1:expr) => {
        x86::bits64::syscall::syscall1($arg0 as u64, $arg1 as u64)
    };

    ($arg0:expr, $arg1:expr, $arg2:expr) => {
        x86::bits64::syscall::syscall2($arg0 as u64, $arg1 as u64, $arg2 as u64)
    };

    ($arg0:expr, $arg1:expr, $arg2:expr, $arg3:expr) => {
        x86::bits64::syscall::syscall3($arg0 as u64, $arg1 as u64, $arg2 as u64, $arg3 as u64)
    };

    ($arg0:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr) => {
        x86::bits64::syscall::syscall4(
            $arg0 as u64,
            $arg1 as u64,
            $arg2 as u64,
            $arg3 as u64,
            $arg4 as u64,
        )
    };

    ($arg0:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr) => {
        x86::bits64::syscall::syscall5(
            $arg0 as u64,
            $arg1 as u64,
            $arg2 as u64,
            $arg3 as u64,
            $arg4 as u64,
            $arg5 as u64,
        )
    };

    ($arg0:expr, $arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr, $arg6:expr) => {
        x86::bits64::syscall::syscall6(
            $arg0 as u64,
            $arg1 as u64,
            $arg2 as u64,
            $arg3 as u64,
            $arg4 as u64,
            $arg5 as u64,
            $arg6 as u64,
        )
    };

    (
        $arg0:expr,
        $arg1:expr,
        $arg2:expr,
        $arg3:expr,
        $arg4:expr,
        $arg5:expr,
        $arg6:expr,
        $arg7:expr
    ) => {
        x86::bits64::syscall::syscall7(
            $arg0 as u64,
            $arg1 as u64,
            $arg2 as u64,
            $arg3 as u64,
            $arg4 as u64,
            $arg5 as u64,
            $arg6 as u64,
            $arg7 as u64,
        )
    };
}

/// Invoke a syscall.
///
/// # Safety
/// Throws `#UD` if IA32_EFER.SCE = 0.
#[cfg(target_arch = "x86_64")]
#[inline(always)]
#[allow(unused_mut)]
pub unsafe fn syscall0(arg0: u64) -> u64 {
    let mut ret: u64;
    asm!("syscall", lateout("rax") ret, in("rax") arg0, options(att_syntax));
    ret
}

/// Invoke a syscall.
///
/// # Safety
/// Throws `#UD` if IA32_EFER.SCE = 0.
#[cfg(target_arch = "x86_64")]
#[inline(always)]
#[allow(unused_mut)]
pub unsafe fn syscall1(arg0: u64, arg1: u64) -> u64 {
    let mut ret: u64;
    asm!(
        "syscall",
        lateout("rax") ret, in("rax") arg0, in("rdi") arg1,
        out("rcx") _, out("r11") _, options(att_syntax),
    );
    ret
}

/// Invoke a syscall.
///
/// # Safety
/// Throws `#UD` if IA32_EFER.SCE = 0.
#[cfg(target_arch = "x86_64")]
#[inline(always)]
#[allow(unused_mut)]
pub unsafe fn syscall2(arg0: u64, arg1: u64, arg2: u64) -> u64 {
    let mut ret: u64;
    asm!(
        "syscall",
        lateout("rax") ret,
        in("rax") arg0, in("rdi") arg1, in("rsi") arg2,
        out("rcx") _, out("r11") _, options(att_syntax),
    );
    ret
}

/// Invoke a syscall.
///
/// # Safety
/// Throws `#UD` if IA32_EFER.SCE = 0.
#[cfg(target_arch = "x86_64")]
#[inline(always)]
#[allow(unused_mut)]
pub unsafe fn syscall3(arg0: u64, arg1: u64, arg2: u64, arg3: u64) -> u64 {
    let mut ret: u64;
    asm!(
        "syscall",
        lateout("rax") ret,
        in("rax") arg0, in("rdi") arg1, in("rsi") arg2, in("rdx") arg3,
        out("rcx") _, out("r11") _, options(att_syntax),
    );
    ret
}

/// Invoke a syscall.
///
/// # Safety
/// Throws `#UD` if IA32_EFER.SCE = 0.
#[cfg(target_arch = "x86_64")]
#[inline(always)]
#[allow(unused_mut)]
pub unsafe fn syscall4(arg0: u64, arg1: u64, arg2: u64, arg3: u64, arg4: u64) -> u64 {
    let mut ret: u64;
    asm!(
        "syscall",
        lateout("rax") ret,
        in("rax") arg0, in("rdi") arg1, in("rsi") arg2, in("rdx") arg3, in("r10") arg4,
        out("rcx") _, out("r11") _, options(att_syntax),
    );
    ret
}

/// Invoke a syscall.
///
/// # Safety
/// Throws `#UD` if IA32_EFER.SCE = 0.
#[cfg(target_arch = "x86_64")]
#[inline(always)]
#[allow(unused_mut)]
pub unsafe fn syscall5(arg0: u64, arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) -> u64 {
    let mut ret: u64;
    asm!(
        "syscall",
        lateout("rax") ret,
        in("rax") arg0, in("rdi") arg1, in("rsi") arg2, in("rdx") arg3, in("r10") arg4, in("r8") arg5,
        out("rcx") _, out("r11") _, options(att_syntax),
    );
    ret
}

/// Invoke a syscall.
///
/// # Safety
/// Throws `#UD` if IA32_EFER.SCE = 0.
#[cfg(target_arch = "x86_64")]
#[inline(always)]
#[allow(unused_mut)]
pub unsafe fn syscall6(
    arg0: u64,
    arg1: u64,
    arg2: u64,
    arg3: u64,
    arg4: u64,
    arg5: u64,
    arg6: u64,
) -> u64 {
    let mut ret: u64;
    asm!(
        "syscall",
        lateout("rax") ret,
        in("rax") arg0, in("rdi") arg1, in("rsi") arg2, in("rdx") arg3,
        in("r10") arg4, in("r8") arg5, in("r9") arg6,
        out("rcx") _, out("r11") _, options(att_syntax),
    );
    ret
}