dius 0.1.0
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 "di/util/prelude.h"
6#include "dius/c_definitions.h"
7#include "dius/error.h"
8
9namespace dius::system {
10enum class Number : int {
11 io_uring_enter = __NR_io_uring_enter,
12 io_uring_setup = __NR_io_uring_setup,
13 io_uring_register = __NR_io_uring_register,
14 pread = __NR_pread64,
15 pwrite = __NR_pwrite64,
16 read = __NR_read,
17 write = __NR_write,
18 close = __NR_close,
19 openat = __NR_openat,
20 mmap = __NR_mmap,
21 munmap = __NR_munmap,
22 getdents64 = __NR_getdents64,
23 fstatat64 = __NR_newfstatat,
24 ftruncate = __NR_ftruncate,
25 arch_prctl = __NR_arch_prctl,
26 brk = __NR_brk,
27 exit_group = __NR_exit_group,
28 clone3 = __NR_clone3,
29 execve = __NR_execve,
30 wait4 = __NR_wait4,
31 exit = __NR_exit,
32 futex = __NR_futex,
33 lseek = __NR_lseek,
34 mknodat = __NR_mknodat,
35 mkdirat = __NR_mkdirat,
36 bind = __NR_bind,
37 listen = __NR_listen,
38 ioctl = __NR_ioctl,
39 rt_sigprocmask = __NR_rt_sigprocmask,
40 rt_sigtimedwait = __NR_rt_sigtimedwait,
41 kill = __NR_kill,
42 getpid = __NR_getpid,
43 clock_nanosleep = __NR_clock_nanosleep,
44 clock_gettime = __NR_clock_gettime,
45 setsid = __NR_setsid,
46 dup2 = __NR_dup2,
47 pipe2 = __NR_pipe2,
48 chdir = __NR_chdir,
49 fchdir = __NR_fchdir,
50 rmdir = __NR_rmdir,
51 unlink = __NR_unlink,
52 uname = __NR_uname,
53};
54
55using SystemCallArg = unsigned long;
56using SystemCallResult = long;
57
58namespace detail {
59 template<typename T>
60 concept SystemCallArgument = di::concepts::ConstructibleFrom<SystemCallArg, T>;
61
62 template<typename T>
63 concept SystemCallResult = di::concepts::ConstructibleFrom<T, dius::system::SystemCallResult>;
64}
65
66template<detail::SystemCallResult R>
67auto system_call(Number number) -> di::Expected<R, di::BasicError> {
68 SystemCallResult res = di::to_underlying(number);
73 if (res < 0) {
74 return di::Unexpected(di::BasicError(-res));
75 }
76 return R(res);
77}
78
79template<detail::SystemCallResult R, detail::SystemCallArgument T1>
80auto system_call(Number number, T1&& a1) -> di::Expected<R, di::BasicError> {
81 SystemCallResult res = di::to_underlying(number);
82 auto y1 = SystemCallArg(a1);
83 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
86 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1)
88 if (res < 0) {
89 return di::Unexpected(di::BasicError(-res));
90 }
91 return R(res);
92}
93
94template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2>
95auto system_call(Number number, T1&& a1, T2&& a2) -> di::Expected<R, di::BasicError> {
96 SystemCallResult res = di::to_underlying(number);
97 auto y1 = SystemCallArg(a1);
98 auto y2 = SystemCallArg(a2);
99 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
100 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
103 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2)
105 if (res < 0) {
106 return di::Unexpected(di::BasicError(-res));
107 }
108 return R(res);
109}
110
111template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2,
112 detail::SystemCallArgument T3>
113auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3) -> di::Expected<R, di::BasicError> {
114 SystemCallResult res = di::to_underlying(number);
115 auto y1 = SystemCallArg(a1);
116 auto y2 = SystemCallArg(a2);
117 auto y3 = SystemCallArg(a3);
118 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
119 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
120 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
123 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3)
125 if (res < 0) {
126 return di::Unexpected(di::BasicError(-res));
127 }
128 return R(res);
129}
130
131template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2,
132 detail::SystemCallArgument T3, detail::SystemCallArgument T4>
133auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3, T4&& a4) -> di::Expected<R, di::BasicError> {
134 SystemCallResult res = di::to_underlying(number);
135 auto y1 = SystemCallArg(a1);
136 auto y2 = SystemCallArg(a2);
137 auto y3 = SystemCallArg(a3);
138 auto y4 = SystemCallArg(a4);
139 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
140 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
141 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
142 register SystemCallArg x4 asm(DIUS_SYSTEM_CALL_ASM_ARG4) = y4;
145 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3), "r"(x4)
147 if (res < 0) {
148 return di::Unexpected(di::BasicError(-res));
149 }
150 return R(res);
151}
152
153template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2,
154 detail::SystemCallArgument T3, detail::SystemCallArgument T4, detail::SystemCallArgument T5>
155auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3, T4&& a4, T5&& a5) -> di::Expected<R, di::BasicError> {
156 SystemCallResult res = di::to_underlying(number);
162 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
163 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
164 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
165 register SystemCallArg x4 asm(DIUS_SYSTEM_CALL_ASM_ARG4) = y4;
166 register SystemCallArg x5 asm(DIUS_SYSTEM_CALL_ASM_ARG5) = y5;
169 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5)
171 if (res < 0) {
172 return di::Unexpected(di::BasicError(-res));
173 }
174 return R(res);
175}
176
177template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2,
178 detail::SystemCallArgument T3, detail::SystemCallArgument T4, detail::SystemCallArgument T5,
179 detail::SystemCallArgument T6>
180auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3, T4&& a4, T5&& a5, T6&& a6)
181 -> di::Expected<R, di::BasicError> {
182 SystemCallResult res = di::to_underlying(number);
183 auto y1 = SystemCallArg(a1);
184 auto y2 = SystemCallArg(a2);
185 auto y3 = SystemCallArg(a3);
186 auto y4 = SystemCallArg(a4);
187 auto y5 = SystemCallArg(a5);
188 auto y6 = SystemCallArg(a6);
189 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
190 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
191 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
192 register SystemCallArg x4 asm(DIUS_SYSTEM_CALL_ASM_ARG4) = y4;
193 register SystemCallArg x5 asm(DIUS_SYSTEM_CALL_ASM_ARG5) = y5;
194 register SystemCallArg x6 asm(DIUS_SYSTEM_CALL_ASM_ARG6) = y6;
197 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x6)
199 if (res < 0) {
200 return di::Unexpected(di::BasicError(-res));
201 }
202 return R(res);
203}
204}
#define DIUS_SYSTEM_CALL_ASM_ARG5
Definition arch_system_call.h:12
#define DIUS_SYSTEM_CALL_CLOBBER
Definition arch_system_call.h:15
#define DIUS_SYSTEM_CALL_ASM_ARG2
Definition arch_system_call.h:9
#define DIUS_SYSTEM_CALL_ASM_ARG1
Definition arch_system_call.h:8
#define DIUS_SYSTEM_CALL_ASM_RESULT
Definition arch_system_call.h:5
#define DIUS_SYSTEM_CALL_ASM_NUMBER
Definition arch_system_call.h:6
#define DIUS_SYSTEM_CALL_INSTRUCTION
Definition arch_system_call.h:3
#define DIUS_SYSTEM_CALL_ASM_ARG4
Definition arch_system_call.h:11
#define DIUS_SYSTEM_CALL_ASM_ARG3
Definition arch_system_call.h:10
#define DIUS_SYSTEM_CALL_ASM_ARG6
Definition arch_system_call.h:13
Definition system_call.h:60
Definition system_call.h:63
Definition system_call.h:58
Definition process.h:14
long SystemCallResult
Definition system_call.h:56
unsigned long SystemCallArg
Definition system_call.h:55
Number
Definition system_call.h:10
@ pipe2
Definition system_call.h:47
@ bind
Definition system_call.h:36
@ rmdir
Definition system_call.h:50
@ rt_sigprocmask
Definition system_call.h:39
@ clone3
Definition system_call.h:28
@ pwrite
Definition system_call.h:15
@ clock_nanosleep
Definition system_call.h:43
@ mknodat
Definition system_call.h:34
@ exit_group
Definition system_call.h:27
@ uname
Definition system_call.h:52
@ pread
Definition system_call.h:14
@ wait4
Definition system_call.h:30
@ clock_gettime
Definition system_call.h:44
@ mkdirat
Definition system_call.h:35
@ io_uring_setup
Definition system_call.h:12
@ dup2
Definition system_call.h:46
@ kill
Definition system_call.h:41
@ io_uring_enter
Definition system_call.h:11
@ futex
Definition system_call.h:32
@ arch_prctl
Definition system_call.h:25
@ unlink
Definition system_call.h:51
@ close
Definition system_call.h:18
@ setsid
Definition system_call.h:45
@ listen
Definition system_call.h:37
@ execve
Definition system_call.h:29
@ chdir
Definition system_call.h:48
@ brk
Definition system_call.h:26
@ fchdir
Definition system_call.h:49
@ ioctl
Definition system_call.h:38
@ mmap
Definition system_call.h:20
@ fstatat64
Definition system_call.h:23
@ rt_sigtimedwait
Definition system_call.h:40
@ lseek
Definition system_call.h:33
@ getpid
Definition system_call.h:42
@ openat
Definition system_call.h:19
@ read
Definition system_call.h:16
@ io_uring_register
Definition system_call.h:13
@ write
Definition system_call.h:17
@ exit
Definition system_call.h:31
@ ftruncate
Definition system_call.h:24
@ getdents64
Definition system_call.h:22
@ munmap
Definition system_call.h:21
auto system_call(Number number) -> di::Expected< R, di::BasicError >
Definition system_call.h:67