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};
48
49using SystemCallArg = unsigned long;
50using SystemCallResult = long;
51
52namespace detail {
53 template<typename T>
54 concept SystemCallArgument = di::concepts::ConstructibleFrom<SystemCallArg, T>;
55
56 template<typename T>
57 concept SystemCallResult = di::concepts::ConstructibleFrom<T, dius::system::SystemCallResult>;
58}
59
60template<detail::SystemCallResult R>
61auto system_call(Number number) -> di::Expected<R, di::BasicError> {
62 SystemCallResult res = di::to_underlying(number);
67 if (res < 0) {
68 return di::Unexpected(di::BasicError(-res));
69 }
70 return R(res);
71}
72
73template<detail::SystemCallResult R, detail::SystemCallArgument T1>
74auto system_call(Number number, T1&& a1) -> di::Expected<R, di::BasicError> {
75 SystemCallResult res = di::to_underlying(number);
76 auto y1 = SystemCallArg(a1);
77 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
80 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1)
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, detail::SystemCallArgument T2>
89auto system_call(Number number, T1&& a1, T2&& a2) -> di::Expected<R, di::BasicError> {
90 SystemCallResult res = di::to_underlying(number);
91 auto y1 = SystemCallArg(a1);
92 auto y2 = SystemCallArg(a2);
93 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
94 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
97 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2)
99 if (res < 0) {
100 return di::Unexpected(di::BasicError(-res));
101 }
102 return R(res);
103}
104
105template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2,
106 detail::SystemCallArgument T3>
107auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3) -> di::Expected<R, di::BasicError> {
108 SystemCallResult res = di::to_underlying(number);
109 auto y1 = SystemCallArg(a1);
110 auto y2 = SystemCallArg(a2);
111 auto y3 = SystemCallArg(a3);
112 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
113 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
114 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
117 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3)
119 if (res < 0) {
120 return di::Unexpected(di::BasicError(-res));
121 }
122 return R(res);
123}
124
125template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2,
126 detail::SystemCallArgument T3, detail::SystemCallArgument T4>
127auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3, T4&& a4) -> di::Expected<R, di::BasicError> {
128 SystemCallResult res = di::to_underlying(number);
129 auto y1 = SystemCallArg(a1);
130 auto y2 = SystemCallArg(a2);
131 auto y3 = SystemCallArg(a3);
132 auto y4 = SystemCallArg(a4);
133 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
134 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
135 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
136 register SystemCallArg x4 asm(DIUS_SYSTEM_CALL_ASM_ARG4) = y4;
139 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3), "r"(x4)
141 if (res < 0) {
142 return di::Unexpected(di::BasicError(-res));
143 }
144 return R(res);
145}
146
147template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2,
148 detail::SystemCallArgument T3, detail::SystemCallArgument T4, detail::SystemCallArgument T5>
149auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3, T4&& a4, T5&& a5) -> di::Expected<R, di::BasicError> {
150 SystemCallResult res = di::to_underlying(number);
156 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
157 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
158 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
159 register SystemCallArg x4 asm(DIUS_SYSTEM_CALL_ASM_ARG4) = y4;
160 register SystemCallArg x5 asm(DIUS_SYSTEM_CALL_ASM_ARG5) = y5;
163 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5)
165 if (res < 0) {
166 return di::Unexpected(di::BasicError(-res));
167 }
168 return R(res);
169}
170
171template<detail::SystemCallResult R, detail::SystemCallArgument T1, detail::SystemCallArgument T2,
172 detail::SystemCallArgument T3, detail::SystemCallArgument T4, detail::SystemCallArgument T5,
173 detail::SystemCallArgument T6>
174auto system_call(Number number, T1&& a1, T2&& a2, T3&& a3, T4&& a4, T5&& a5, T6&& a6)
175 -> di::Expected<R, di::BasicError> {
176 SystemCallResult res = di::to_underlying(number);
177 auto y1 = SystemCallArg(a1);
178 auto y2 = SystemCallArg(a2);
179 auto y3 = SystemCallArg(a3);
180 auto y4 = SystemCallArg(a4);
181 auto y5 = SystemCallArg(a5);
182 auto y6 = SystemCallArg(a6);
183 register SystemCallArg x1 asm(DIUS_SYSTEM_CALL_ASM_ARG1) = y1;
184 register SystemCallArg x2 asm(DIUS_SYSTEM_CALL_ASM_ARG2) = y2;
185 register SystemCallArg x3 asm(DIUS_SYSTEM_CALL_ASM_ARG3) = y3;
186 register SystemCallArg x4 asm(DIUS_SYSTEM_CALL_ASM_ARG4) = y4;
187 register SystemCallArg x5 asm(DIUS_SYSTEM_CALL_ASM_ARG5) = y5;
188 register SystemCallArg x6 asm(DIUS_SYSTEM_CALL_ASM_ARG6) = y6;
191 : DIUS_SYSTEM_CALL_ASM_NUMBER(res), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x6)
193 if (res < 0) {
194 return di::Unexpected(di::BasicError(-res));
195 }
196 return R(res);
197}
198}
#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:54
Definition system_call.h:57
Definition system_call.h:52
Definition process.h:14
long SystemCallResult
Definition system_call.h:50
unsigned long SystemCallArg
Definition system_call.h:49
Number
Definition system_call.h:10
@ bind
Definition system_call.h:36
@ 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
@ 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
@ close
Definition system_call.h:18
@ setsid
Definition system_call.h:45
@ listen
Definition system_call.h:37
@ execve
Definition system_call.h:29
@ brk
Definition system_call.h:26
@ 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:61