syscaller_core/
lib.rs

1#![no_std]
2
3/// Make a syscall with 0 arguments.
4///
5/// # Safety
6///
7/// This function directly invokes system calls which can have undefined behavior
8/// if called with invalid syscall numbers or if the kernel state is inconsistent.
9/// The caller must ensure the syscall number is valid for the target system.
10#[inline(always)]
11pub unsafe fn syscall0(n: usize) -> isize {
12    let ret: isize;
13    unsafe {
14        core::arch::asm!(
15            "syscall",
16            in("rax") n,
17            lateout("rax") ret,
18            lateout("rcx") _,
19            lateout("r11") _,
20        );
21    }
22    ret
23}
24
25/// Make a syscall with 1 argument.
26///
27/// # Safety
28///
29/// This function directly invokes system calls which can have undefined behavior
30/// if called with invalid syscall numbers, invalid arguments, or if the kernel
31/// state is inconsistent. The caller must ensure the syscall number and arguments
32/// are valid for the target system.
33#[inline(always)]
34pub unsafe fn syscall1(n: usize, a1: usize) -> isize {
35    let ret: isize;
36    unsafe {
37        core::arch::asm!(
38            "syscall",
39            in("rax") n,
40            in("rdi") a1,
41            lateout("rax") ret,
42            lateout("rcx") _,
43            lateout("r11") _,
44        );
45    }
46    ret
47}
48
49/// Make a syscall with 2 arguments.
50///
51/// # Safety
52///
53/// This function directly invokes system calls which can have undefined behavior
54/// if called with invalid syscall numbers, invalid arguments, or if the kernel
55/// state is inconsistent. The caller must ensure the syscall number and arguments
56/// are valid for the target system.
57#[inline(always)]
58pub unsafe fn syscall2(n: usize, a1: usize, a2: usize) -> isize {
59    let ret: isize;
60    unsafe {
61        core::arch::asm!(
62            "syscall",
63            in("rax") n,
64            in("rdi") a1,
65            in("rsi") a2,
66            lateout("rax") ret,
67            lateout("rcx") _,
68            lateout("r11") _,
69        );
70    }
71    ret
72}
73
74/// Make a syscall with 3 arguments.
75///
76/// # Safety
77///
78/// This function directly invokes system calls which can have undefined behavior
79/// if called with invalid syscall numbers, invalid arguments, or if the kernel
80/// state is inconsistent. The caller must ensure the syscall number and arguments
81/// are valid for the target system.
82#[inline(always)]
83pub unsafe fn syscall3(n: usize, a1: usize, a2: usize, a3: usize) -> isize {
84    let ret: isize;
85    unsafe {
86        core::arch::asm!(
87            "syscall",
88            in("rax") n,
89            in("rdi") a1,
90            in("rsi") a2,
91            in("rdx") a3,
92            lateout("rax") ret,
93            lateout("rcx") _,
94            lateout("r11") _,
95        );
96    }
97    ret
98}
99
100/// Make a syscall with 4 arguments.
101///
102/// # Safety
103///
104/// This function directly invokes system calls which can have undefined behavior
105/// if called with invalid syscall numbers, invalid arguments, or if the kernel
106/// state is inconsistent. The caller must ensure the syscall number and arguments
107/// are valid for the target system.
108#[inline(always)]
109pub unsafe fn syscall4(n: usize, a1: usize, a2: usize, a3: usize, a4: usize) -> isize {
110    let ret: isize;
111    unsafe {
112        core::arch::asm!(
113            "syscall",
114            in("rax") n,
115            in("rdi") a1,
116            in("rsi") a2,
117            in("rdx") a3,
118            in("r10") a4,
119            lateout("rax") ret,
120            lateout("rcx") _,
121            lateout("r11") _,
122        );
123    }
124    ret
125}
126
127/// Make a syscall with 5 arguments.
128///
129/// # Safety
130///
131/// This function directly invokes system calls which can have undefined behavior
132/// if called with invalid syscall numbers, invalid arguments, or if the kernel
133/// state is inconsistent. The caller must ensure the syscall number and arguments
134/// are valid for the target system.
135#[inline(always)]
136pub unsafe fn syscall5(n: usize, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) -> isize {
137    let ret: isize;
138    unsafe {
139        core::arch::asm!(
140            "syscall",
141            in("rax") n,
142            in("rdi") a1,
143            in("rsi") a2,
144            in("rdx") a3,
145            in("r10") a4,
146            in("r8")  a5,
147            lateout("rax") ret,
148            lateout("rcx") _,
149            lateout("r11") _,
150        );
151    }
152    ret
153}
154
155/// Make a syscall with 6 arguments.
156///
157/// # Safety
158///
159/// This function directly invokes system calls which can have undefined behavior
160/// if called with invalid syscall numbers, invalid arguments, or if the kernel
161/// state is inconsistent. The caller must ensure the syscall number and arguments
162/// are valid for the target system.
163#[inline(always)]
164pub unsafe fn syscall6(
165    n: usize,
166    a1: usize,
167    a2: usize,
168    a3: usize,
169    a4: usize,
170    a5: usize,
171    a6: usize,
172) -> isize {
173    let ret: isize;
174    unsafe {
175        core::arch::asm!(
176            "syscall",
177            in("rax") n,
178            in("rdi") a1,
179            in("rsi") a2,
180            in("rdx") a3,
181            in("r10") a4,
182            in("r8")  a5,
183            in("r9")  a6,
184            lateout("rax") ret,
185            lateout("rcx") _,
186            lateout("r11") _,
187        );
188    }
189    ret
190}