-
Notifications
You must be signed in to change notification settings - Fork 2
/
minisys.c
136 lines (122 loc) · 2.96 KB
/
minisys.c
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
#include "minisys.h"
#include <asm/unistd.h>
static long syscall0(long sysnr)
{
long ret;
asm volatile (
"syscall"
: "=a" (ret)
: "0" (sysnr)
: "rcx", "r11", "memory");
return ret;
}
static long syscall1(long sysnr, long arg1)
{
long ret;
asm volatile (
"syscall"
: "=a" (ret)
: "0" (sysnr), "D" (arg1)
: "rcx", "r11", "memory");
return ret;
}
static long syscall2(long sysnr, long arg1, long arg2)
{
long ret;
asm volatile (
"syscall"
: "=a" (ret)
: "0" (sysnr), "D" (arg1), "S" (arg2)
: "rcx", "r11", "memory");
return ret;
}
static long syscall3(long sysnr, long arg1, long arg2, long arg3)
{
long ret;
asm volatile (
"syscall"
: "=a" (ret)
: "0" (sysnr), "D" (arg1), "S" (arg2), "d" (arg3)
: "rcx", "r11", "memory");
return ret;
}
static long syscall4(long sysnr, long arg1, long arg2, long arg3, long arg4)
{
long ret;
register long r10 asm("r10") = arg4;
asm volatile (
"syscall"
: "=a" (ret)
: "0" (sysnr), "D" (arg1), "S" (arg2), "d" (arg3), "r" (r10)
: "rcx", "r11", "memory");
return ret;
}
static long syscall5(long sysnr, long arg1, long arg2, long arg3, long arg4, long arg5)
{
long ret;
register long r10 asm("r10") = arg4;
register long r8 asm("r8") = arg5;
asm volatile (
"syscall"
: "=a" (ret)
: "0" (sysnr), "D" (arg1), "S" (arg2), "d" (arg3), "r" (r10), "r" (r8)
: "rcx", "r11", "memory");
return ret;
}
static long syscall6(long sysnr, long arg1, long arg2, long arg3, long arg4, long arg5, long arg6)
{
long ret;
register long r10 asm("r10") = arg4;
register long r8 asm("r8") = arg5;
register long r9 asm("r9") = arg6;
asm volatile (
"syscall"
: "=a" (ret)
: "0" (sysnr), "D" (arg1), "S" (arg2), "d" (arg3), "r" (r10), "r" (r8), "r" (r9)
: "rcx", "r11", "memory");
return ret;
}
long pause(void)
{
return syscall0(__NR_pause);
}
__attribute__((naked, noreturn))
static void signal_restorer(void)
{
asm volatile (
"syscall"
:
: "a" (__NR_rt_sigreturn)
: "rcx", "r11", "memory"
);
}
long sigaction(int signum,
struct sigaction* restrict act,
struct sigaction* restrict oldact)
{
act->sa_flags |= SA_RESTORER;
act->sa_restorer = &signal_restorer;
return syscall4(__NR_rt_sigaction, signum, (long)act, (long)oldact, sizeof(sigset_t));
}
__attribute__((noreturn))
void _exit(int exit_status)
{
syscall1(__NR_exit_group, exit_status);
}
long renameat2(int olddirfd, char const* oldpath,
int newdirfd, char const* newpath,
unsigned flags)
{
return syscall5(__NR_renameat2,
olddirfd, (long)oldpath,
newdirfd, (long)newpath,
(unsigned long)flags);
}
int socket(int domain, int type, int protocol)
{
return syscall3(__NR_socket, domain, type, protocol);
}
int connect(int sockfd, struct sockaddr const* addr, unsigned addrlen)
{
return syscall3(__NR_connect, sockfd, addr, addrlen);
}