-
Notifications
You must be signed in to change notification settings - Fork 2
/
syscalls.c
245 lines (203 loc) · 3.59 KB
/
syscalls.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//
// Created by yqszxx on 1/24/22.
//
#include <sys/stat.h>
#include <newlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/timeb.h>
#include <sys/times.h>
#include <sys/utime.h>
#undef errno
extern int errno;
/* write to this reg for outputting strings */
#define STDOUT_REG 0x10010000
#define STDOUT_FILENO 1
void unimplemented_syscall()
{
const char *p = "Unimplemented system call called!\n";
while (*p) {
*(volatile int *) STDOUT_REG = *(p++);
while (*(volatile int *) STDOUT_REG);
}
}
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
errno = ENOSYS;
return -1;
}
int _access(const char *file, int mode)
{
errno = ENOSYS;
return -1;
}
int _chdir(const char *path)
{
errno = ENOSYS;
return -1;
}
int _chmod(const char *path, mode_t mode)
{
errno = ENOSYS;
return -1;
}
int _chown(const char *path, uid_t owner, gid_t group)
{
errno = ENOSYS;
return -1;
}
int _close(int file)
{
return -1;
}
int _execve(const char *name, char *const argv[], char *const env[])
{
errno = ENOMEM;
return -1;
}
void _exit(int exit_status)
{
asm volatile("wfi");
while (1);
}
int _faccessat(int dirfd, const char *file, int mode, int flags)
{
errno = ENOSYS;
return -1;
}
int _fork(void)
{
errno = EAGAIN;
return -1;
}
int _fstat(int file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
// errno = -ENOSYS;
// return -1;
}
int _fstatat(int dirfd, const char *file, struct stat *st, int flags)
{
errno = ENOSYS;
return -1;
}
int _ftime(struct timeb *tp)
{
errno = ENOSYS;
return -1;
}
char *_getcwd(char *buf, size_t size)
{
errno = -ENOSYS;
return NULL;
}
int _getpid()
{
return 1;
}
int _gettimeofday(struct timeval *tp, void *tzp)
{
errno = -ENOSYS;
return -1;
}
int _isatty(int file)
{
return (file == STDOUT_FILENO);
}
int _kill(int pid, int sig)
{
errno = EINVAL;
return -1;
}
int _link(const char *old_name, const char *new_name)
{
errno = EMLINK;
return -1;
}
off_t _lseek(int file, off_t ptr, int dir)
{
return 0;
}
int _lstat(const char *file, struct stat *st)
{
errno = ENOSYS;
return -1;
}
int _open(const char *name, int flags, int mode)
{
return -1;
}
int _openat(int dirfd, const char *name, int flags, int mode)
{
errno = ENOSYS;
return -1;
}
ssize_t _read(int file, void *ptr, size_t len)
{
return 0;
}
int _stat(const char *file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
// errno = ENOSYS;
// return -1;
}
long _sysconf(int name)
{
return -1;
}
clock_t _times(struct tms *buf)
{
return -1;
}
int _unlink(const char *name)
{
errno = ENOENT;
return -1;
}
int _utime(const char *path, const struct utimbuf *times)
{
errno = ENOSYS;
return -1;
}
int _wait(int *status)
{
errno = ECHILD;
return -1;
}
ssize_t _write(int file, const void *ptr, size_t len)
{
if (file != STDOUT_FILENO && file != STDERR_FILENO) {
errno = ENOSYS;
return -1;
}
const void *eptr = ptr + len;
while (ptr != eptr) {
*(volatile int *) STDOUT_REG = *(char *) (ptr++);
while (*(volatile int *) STDOUT_REG);
}
return len;
}
extern char __heap_start[];
extern char __heap_end[];
static char *brk = __heap_start;
int _brk(void *addr)
{
brk = addr;
return 0;
}
void *_sbrk(ptrdiff_t incr)
{
char *old_brk = brk;
if (__heap_start == __heap_end) {
return NULL;
}
if ((brk + incr) < __heap_end) {
brk += incr;
} else {
brk = __heap_end;
}
return old_brk;
}