Iros
 
Loading...
Searching...
No Matches
system_call.h
Go to the documentation of this file.
1#pragma once
2
3#include <asm/unistd.h>
4#include <linux/io_uring.h>
5
6#ifdef DIUS_USE_RUNTIME
7#include <asm-generic/ioctls.h>
8#include <asm-generic/termios.h>
9#include <linux/fcntl.h>
10#include <linux/mman.h>
11#else
12#include <fcntl.h>
13#include <sys/ioctl.h>
14#include <sys/mman.h>
15#include <termios.h>
16#endif
17
18#include "di/util/prelude.h"
19#include "dius/config.h"
20#include "dius/error.h"
21
22#include DIUS_ARCH_PLATFORM_PATH(system_call.h)
23
24namespace dius::system {
25enum class Number : int {
26 io_uring_enter = __NR_io_uring_enter,
27 io_uring_setup = __NR_io_uring_setup,
28 io_uring_register = __NR_io_uring_register,
29 pread = __NR_pread64,
30 pwrite = __NR_pwrite64,
31 read = __NR_read,
32 write = __NR_write,
33 close = __NR_close,
34 openat = __NR_openat,
35 mmap = __NR_mmap,
36 munmap = __NR_munmap,
37 getdents64 = __NR_getdents64,
38 fstatat64 = __NR_newfstatat,
39 ftruncate = __NR_ftruncate,
40 arch_prctl = __NR_arch_prctl,
41 brk = __NR_brk,
42 exit_group = __NR_exit_group,
43 clone3 = __NR_clone3,
44 execve = __NR_execve,
45 wait4 = __NR_wait4,
46 exit = __NR_exit,
47 futex = __NR_futex,
48 lseek = __NR_lseek,
49 mknodat = __NR_mknodat,
50 mkdirat = __NR_mkdirat,
51 bind = __NR_bind,
52 listen = __NR_listen,
53 ioctl = __NR_ioctl,
54 rt_sigprocmask = __NR_rt_sigprocmask,
55 rt_sigtimedwait = __NR_rt_sigtimedwait,
56 kill = __NR_kill,
57 getpid = __NR_getpid,
58 clock_nanosleep = __NR_clock_nanosleep,
59 clock_gettime = __NR_clock_gettime,
60 setsid = __NR_setsid,
61 dup2 = __NR_dup2,
62};
63
64using SystemCallArg = unsigned long;
65using SystemCallResult = long;
66
67namespace detail {
68 template<typename T>
69 concept SystemCallArgument = di::concepts::ConstructibleFrom<SystemCallArg, T>;
70
71 template<typename T>
73}
74
75template<detail::SystemCallResult R>
82 if (res < 0) {
83 return di::Unexpected(di::BasicError(-res));
84 }
85 return R(res);
86}
87
88template<detail::SystemCallResult R, detail::SystemCallArgument T1>
89auto system_call(Number number, T1&& a1) -> di::Expected<R, di::BasicError> {
91 auto y1 = SystemCallArg(a1);
92 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
95 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1)
97 if (res < 0) {
98 return di::Unexpected(di::BasicError(-res));
99 }
100 return R(res);
101}
102
103template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2>
104auto system_call(Number number, T1&& a1, T2&& a2) -> di::Expected<R, di::BasicError> {
106 auto y1 = SystemCallArg(a1);
107 auto y2 = SystemCallArg(a2);
108 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
109 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
112 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2)
114 if (res < 0) {
115 return di::Unexpected(di::BasicError(-res));
116 }
117 return R(res);
118}
119
122auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3) -> di::Expected<R, di::BasicError> {
124 auto y1 = SystemCallArg(a1);
125 auto y2 = SystemCallArg(a2);
126 auto y3 = SystemCallArg(a3);
127 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
128 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
129 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
132 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3)
134 if (res < 0) {
135 return di::Unexpected(di::BasicError(-res));
136 }
137 return R(res);
138}
139
142auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3, T4&& a4) -> di::Expected<R, di::BasicError> {
144 auto y1 = SystemCallArg(a1);
145 auto y2 = SystemCallArg(a2);
146 auto y3 = SystemCallArg(a3);
147 auto y4 = SystemCallArg(a4);
148 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
149 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
150 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
151 register SystemCallArg x4 asm(DIUS_SYSTEM_CALL_ASM_ARG4) = y4;
154 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3), "r"(x4)
156 if (res < 0) {
157 return di::Unexpected(di::BasicError(-res));
158 }
159 return R(res);
160}
161
164auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3, T4&& a4, T5&& a5) -> di::Expected<R, di::BasicError> {
171 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
172 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
173 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
174 register SystemCallArg x4 asm(DIUS_SYSTEM_CALL_ASM_ARG4) = y4;
175 register SystemCallArg x5 asm(DIUS_SYSTEM_CALL_ASM_ARG5) = y5;
178 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5)
180 if (res < 0) {
181 return di::Unexpected(di::BasicError(-res));
182 }
183 return R(res);
184}
185
189auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3, T4&& a4, T5&& a5, T6&& a6)
190 -> di::Expected<R, di::BasicError> {
192 auto y1 = SystemCallArg(a1);
193 auto y2 = SystemCallArg(a2);
194 auto y3 = SystemCallArg(a3);
195 auto y4 = SystemCallArg(a4);
196 auto y5 = SystemCallArg(a5);
197 auto y6 = SystemCallArg(a6);
198 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
199 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
200 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
201 register SystemCallArg x4 asm(DIUS_SYSTEM_CALL_ASM_ARG4) = y4;
202 register SystemCallArg x5 asm(DIUS_SYSTEM_CALL_ASM_ARG5) = y5;
203 register SystemCallArg x6 asm(DIUS_SYSTEM_CALL_ASM_ARG6) = y6;
206 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x6)
208 if (res < 0) {
209 return di::Unexpected(di::BasicError(-res));
210 }
211 return R(res);
212}
213}
#define DIUS_SYSTEM_CALL_ASM_ARG5
Definition system_call.h:13
#define DIUS_SYSTEM_CALL_CLOBBER
Definition system_call.h:16
#define DIUS_SYSTEM_CALL_ASM_ARG2
Definition system_call.h:10
#define DIUS_SYSTEM_CALL_ASM_ARG1
Definition system_call.h:9
#define DIUS_SYSTEM_CALL_ASM_RESULT
Definition system_call.h:5
#define DIUS_SYSTEM_CALL_ASM_NUMBER
Definition system_call.h:7
#define DIUS_SYSTEM_CALL_INSTRUCTION
Definition system_call.h:3
#define DIUS_SYSTEM_CALL_ASM_ARG4
Definition system_call.h:12
#define DIUS_SYSTEM_CALL_ASM_ARG3
Definition system_call.h:11
#define DIUS_SYSTEM_CALL_ASM_ARG6
Definition system_call.h:14
Definition expected_forward_declaration.h:8
Definition unexpected.h:14
Definition operations.h:11
Definition system_call.h:20
Definition system_call.h:23
BasicError
Definition default_generic_domain.h:10
constexpr auto to_underlying
Definition to_underlying.h:15
Definition print.h:8
Definition system_call.h:11
unsigned long SystemCallArg
Definition system_call.h:14
Number
Definition system_call.h:25
@ bind
Definition system_call.h:51
@ rt_sigprocmask
Definition system_call.h:54
@ clone3
Definition system_call.h:43
@ pwrite
Definition system_call.h:30
@ clock_nanosleep
Definition system_call.h:58
@ mknodat
Definition system_call.h:49
@ exit_group
Definition system_call.h:42
@ pread
Definition system_call.h:29
@ wait4
Definition system_call.h:45
@ clock_gettime
Definition system_call.h:59
@ mkdirat
Definition system_call.h:50
@ io_uring_setup
Definition system_call.h:27
@ dup2
Definition system_call.h:61
@ kill
Definition system_call.h:56
@ io_uring_enter
Definition system_call.h:26
@ futex
Definition system_call.h:47
@ arch_prctl
Definition system_call.h:40
@ close
Definition system_call.h:33
@ setsid
Definition system_call.h:60
@ listen
Definition system_call.h:52
@ execve
Definition system_call.h:44
@ brk
Definition system_call.h:41
@ ioctl
Definition system_call.h:53
@ mmap
Definition system_call.h:35
@ fstatat64
Definition system_call.h:38
@ rt_sigtimedwait
Definition system_call.h:55
@ lseek
Definition system_call.h:48
@ getpid
Definition system_call.h:57
@ openat
Definition system_call.h:34
@ read
Definition system_call.h:31
@ io_uring_register
Definition system_call.h:28
@ write
Definition system_call.h:32
@ exit
Definition system_call.h:46
@ ftruncate
Definition system_call.h:39
@ getdents64
Definition system_call.h:37
@ munmap
Definition system_call.h:36
unsigned long SystemCallResult
Definition system_call.h:15
auto system_call(Number number) -> di::Expected< R, di::BasicError >
Definition system_call.h:27
@ R
Definition key.h:27