aboutsummaryrefslogtreecommitdiff
path: root/libs/libc/sys.c
blob: ce90fedf610dcb30210c0ce9d10104a50e80e605 (plain) (blame)
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
// MIT License, Copyright (c) 2020 Marvin Borner
// Syscall implementation

#include <arg.h>
#include <assert.h>
#include <errno.h>
#include <sys.h>

#ifdef USER

/**
 * Definitions
 */

#define ERRIFY(ret)                                                                                \
	if (ret < 0) {                                                                             \
		errno = -ret;                                                                      \
		return -1;                                                                         \
	}                                                                                          \
	errno = 0;                                                                                 \
	return ret

res sys0(enum sys num);
res sys0(enum sys num)
{
	int a;
	__asm__ volatile("int $0x80" : "=a"(a) : "0"(num));
	ERRIFY(a);
}

res sys1(enum sys num, int d1);
res sys1(enum sys num, int d1)
{
	int a;
	__asm__ volatile("int $0x80" : "=a"(a) : "0"(num), "b"((int)d1));
	ERRIFY(a);
}

res sys2(enum sys num, int d1, int d2);
res sys2(enum sys num, int d1, int d2)
{
	int a;
	__asm__ volatile("int $0x80" : "=a"(a) : "0"(num), "b"((int)d1), "c"((int)d2));
	ERRIFY(a);
}

res sys3(enum sys num, int d1, int d2, int d3);
res sys3(enum sys num, int d1, int d2, int d3)
{
	int a;
	__asm__ volatile("int $0x80"
			 : "=a"(a)
			 : "0"(num), "b"((int)d1), "c"((int)d2), "d"((int)d3));
	ERRIFY(a);
}

res sys4(enum sys num, int d1, int d2, int d3, int d4);
res sys4(enum sys num, int d1, int d2, int d3, int d4)
{
	int a;
	__asm__ volatile("int $0x80"
			 : "=a"(a)
			 : "0"(num), "b"((int)d1), "c"((int)d2), "d"((int)d3), "S"((int)d4));
	ERRIFY(a);
}

res sys5(enum sys num, int d1, int d2, int d3, int d4, int d5);
res sys5(enum sys num, int d1, int d2, int d3, int d4, int d5)
{
	int a;
	__asm__ volatile("int $0x80"
			 : "=a"(a)
			 : "0"(num), "b"((int)d1), "c"((int)d2), "d"((int)d3), "S"((int)d4),
			   "D"((int)d5));
	ERRIFY(a);
}

/**
 * Syscalls
 */

res sys_alloc(u32 size, u32 *addr)
{
	u32 id = 0;
	return sys4(SYS_ALLOC, (int)size, (int)addr, (int)&id, 0);
}

res sys_free(void *ptr)
{
	return sys1(SYS_FREE, (int)ptr);
}

res shalloc(u32 size, u32 *addr, u32 *id)
{
	return sys4(SYS_ALLOC, (int)size, (int)addr, (int)id, 1);
}

res shaccess(u32 id, u32 *addr, u32 *size)
{
	return sys3(SYS_SHACCESS, (int)id, (int)addr, (int)size);
}

res read(const char *path, void *buf, u32 offset, u32 count)
{
	return sys4(SYS_READ, (int)path, (int)buf, (int)offset, (int)count);
}

res write(const char *path, const void *buf, u32 offset, u32 count)
{
	return sys4(SYS_WRITE, (int)path, (int)buf, (int)offset, (int)count);
}

res ioctl(const char *path, ...)
{
	va_list ap;
	int args[4] = { 0 };

	va_start(ap, path);
	for (int i = 0; i < 4; i++)
		args[i] = va_arg(ap, int);
	va_end(ap);

	return sys5(SYS_IOCONTROL, (int)path, args[0], args[1], args[2], args[3]);
}

res stat(const char *path, struct stat *buf)
{
	return sys2(SYS_STAT, (int)path, (int)buf);
}

res exec(const char *path, ...)
{
	va_list ap;
	int args[4] = { 0 };

	va_start(ap, path);
	for (int i = 0; i < 4; i++)
		args[i] = va_arg(ap, int);
	va_end(ap);

	return sys5(SYS_EXEC, (int)path, args[0], args[1], args[2], args[3]);
}

res yield(void)
{
	return sys0(SYS_YIELD);
}

static void atexit_trigger(void);
void exit(s32 status)
{
	atexit_trigger();
	sys1(SYS_EXIT, (int)status);
	while (1)
		yield();
}

res boot(u32 cmd)
{
	return sys2(SYS_BOOT, SYS_BOOT_MAGIC, cmd);
}

/**
 * At exit
 */

#define ATEXIT_MAX 32

static u32 slot = 0;
static void (*funcs[ATEXIT_MAX])(void) = { 0 };

static void atexit_trigger(void)
{
	while (slot-- > 0) {
		if (funcs[slot]) {
			funcs[slot]();
			funcs[slot] = NULL;
		}
	}
}

void atexit(void (*func)(void))
{
	if (slot < ATEXIT_MAX)
		funcs[slot++] = func;
}

#endif