-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.hh
408 lines (342 loc) · 14.4 KB
/
lib.hh
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#ifndef CHICKADEE_LIB_HH
#define CHICKADEE_LIB_HH
#include "types.h"
#include <new> // for placement new
#include <type_traits>
// lib.hh
//
// Functions, constants, and definitions useful in both the kernel
// and applications.
//
// Contents: (1) C library subset, (2) system call numbers, (3) console.
extern "C" {
void* memcpy(void* dst, const void* src, size_t n);
void* memmove(void* dst, const void* src, size_t n);
void* memset(void* s, int c, size_t n);
int memcmp(const void* a, const void* b, size_t n);
size_t strlen(const char* s);
size_t strnlen(const char* s, size_t maxlen);
char* strcpy(char* dst, const char* src);
int strcmp(const char* a, const char* b);
int strncmp(const char* a, const char* b, size_t maxlen);
char* strchr(const char* s, int c);
long strtol(const char* s, char** endptr = nullptr, int base = 0);
unsigned long strtoul(const char* s, char** endptr = nullptr, int base = 0);
ssize_t snprintf(char* s, size_t size, const char* format, ...);
ssize_t vsnprintf(char* s, size_t size, const char* format, va_list val);
inline bool isspace(unsigned char c);
}
#define RAND_MAX 0x7FFFFFFF
int rand();
void srand(unsigned seed);
int rand(int min, int max);
// Return the offset of `member` relative to the beginning of a struct type
#ifndef offsetof
#define offsetof(type, member) __builtin_offsetof(type, member)
#endif
// Return the number of elements in an array
#define arraysize(array) (sizeof(array) / sizeof(array[0]))
// Type information
// printfmt<T>
// `printfmt<T>::spec` defines a printf specifier for type T.
// E.g., `printfmt<int>::spec` is `"d"`.
template <typename T> struct printfmt {};
template <> struct printfmt<bool> { static constexpr char spec[] = "d"; };
template <> struct printfmt<char> { static constexpr char spec[] = "c"; };
template <> struct printfmt<signed char> { static constexpr char spec[] = "d"; };
template <> struct printfmt<unsigned char> { static constexpr char spec[] = "u"; };
template <> struct printfmt<short> { static constexpr char spec[] = "d"; };
template <> struct printfmt<unsigned short> { static constexpr char spec[] = "u"; };
template <> struct printfmt<int> { static constexpr char spec[] = "d"; };
template <> struct printfmt<unsigned> { static constexpr char spec[] = "u"; };
template <> struct printfmt<long> { static constexpr char spec[] = "ld"; };
template <> struct printfmt<unsigned long> { static constexpr char spec[] = "lu"; };
template <typename T> struct printfmt<T*> { static constexpr char spec[] = "p"; };
template <typename T> constexpr char printfmt<T*>::spec[];
// Min, max, and rounding operations
#define MIN(_a, _b) ({ \
typeof(_a) __a = (_a); typeof(_b) __b = (_b); \
__a <= __b ? __a : __b; })
#define MAX(_a, _b) ({ \
typeof(_a) __a = (_a); typeof(_b) __b = (_b); \
__a >= __b ? __a : __b; })
// Round down to the nearest multiple of n
#define ROUNDDOWN(a, n) ({ \
uint64_t __a = (uint64_t) (a); \
(typeof(a)) (__a - __a % (n)); })
// Round up to the nearest multiple of n
#define ROUNDUP(a, n) ({ \
uint64_t __n = (uint64_t) (n); \
(typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n)); })
template <typename T>
inline constexpr T min(T a, T b) {
return a < b ? a : b;
}
template <typename T>
inline constexpr T max(T a, T b) {
return b < a ? a : b;
}
// msb(x)
// Return index of most significant one bit in `x`, plus one.
// Returns 0 if `x == 0`.
inline constexpr int msb(int x) {
return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
}
inline constexpr int msb(unsigned x) {
return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
}
inline constexpr int msb(long x) {
return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
}
inline constexpr int msb(unsigned long x) {
return x ? sizeof(x) * 8 - __builtin_clzl(x) : 0;
}
inline constexpr int msb(long long x) {
return x ? sizeof(x) * 8 - __builtin_clzll(x) : 0;
}
inline constexpr int msb(unsigned long long x) {
return x ? sizeof(x) * 8 - __builtin_clzll(x) : 0;
}
// lsb(x)
// Return index of least significant one bit in `x`, plus one.
// Returns 0 if `x == 0`.
inline constexpr int lsb(int x) {
return __builtin_ffs(x);
}
inline constexpr int lsb(unsigned x) {
return __builtin_ffs(x);
}
inline constexpr int lsb(long x) {
return __builtin_ffsl(x);
}
inline constexpr int lsb(unsigned long x) {
return __builtin_ffsl(x);
}
inline constexpr int lsb(long long x) {
return __builtin_ffsll(x);
}
inline constexpr int lsb(unsigned long long x) {
return __builtin_ffsll(x);
}
// rounddown_pow2(x)
// Round x down to the nearest power of 2.
template <typename T>
inline constexpr T rounddown_pow2(T x) {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
return x ? T(1) << (msb(x) - 1) : 0;
}
// roundup_pow2(x)
// Round x up to the nearest power of 2.
template <typename T>
inline constexpr T roundup_pow2(T x) {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
return x ? T(1) << msb(x - 1) : 0;
}
// Character traits
inline bool isspace(unsigned char ch) {
return (ch >= '\t' && ch <= '\r') || ch == ' ';
}
// Checksums
uint32_t crc32c(uint32_t crc, const void* buf, size_t sz);
inline uint32_t crc32c(const void* buf, size_t sz) {
return crc32c(0, buf, sz);
}
// System call numbers: an application calls `int NUM` to call a system call
#define SYSCALL_GETPID 1
#define SYSCALL_YIELD 2
#define SYSCALL_PAUSE 3
#define SYSCALL_PANIC 4
#define SYSCALL_PAGE_ALLOC 5
#define SYSCALL_FORK 6
#define SYSCALL_EXIT 7
#define SYSCALL_MAP_CONSOLE 8
#define SYSCALL_MSLEEP 9
#define SYSCALL_GETPPID 10
#define SYSCALL_WAITPID 11
#define SYSCALL_MAP_SCREEN 12
#define SYSCALL_COMMIT_SEPPUKU 44
#define SYSCALL_LOG_PRINTF 69
#define SYSCALL_GETTICKS 77
#define SYSCALL_MEMSET 88
#define SYSCALL_KDISPLAY 100
#define SYSCALL_READ 101
#define SYSCALL_WRITE 102
#define SYSCALL_CLOSE 103
#define SYSCALL_DUP2 104
#define SYSCALL_PIPE 105
#define SYSCALL_EXECV 106
#define SYSCALL_OPEN 107
#define SYSCALL_UNLINK 108
#define SYSCALL_READDISKFILE 109
#define SYSCALL_SYNC 110
#define SYSCALL_LSEEK 111
#define SYSCALL_FTRUNCATE 112
#define SYSCALL_RENAME 113
#define SYSCALL_GETTID 114
#define SYSCALL_CLONE 115
#define SYSCALL_TEXIT 116
// DOOM specific calls
#define SYSCALL_MALLOC 90
#define SYSCALL_SWAPCOLOR 91
#define SYSCALL_FREE 92
// System call error return values
#define E_AGAIN -11 // Try again
#define E_BADF -9 // Bad file number
#define E_CHILD -10 // No child processes
#define E_FAULT -14 // Bad address
#define E_FBIG -27 // File too large
#define E_INTR -4 // Interrupted system call
#define E_INVAL -22 // Invalid argument
#define E_IO -5 // I/O error
#define E_MFILE -24 // Too many open files
#define E_NFILE -23 // File table overflow
#define E_NOENT -2 // No such file or directory
#define E_NOEXEC -8 // Exec format error
#define E_NOMEM -12 // Out of memory
#define E_NOSPC -28 // No space left on device
#define E_NOSYS -38 // Invalid system call number
#define E_NXIO -6 // No such device or address
#define E_PERM -1 // Operation not permitted
#define E_PIPE -32 // Broken pipe
#define E_SPIPE -29 // Illegal seek
#define E_SRCH -3 // No such process
#define E_TXTBSY -26 // Text file busy
#define E_2BIG -7 // Argument list too long
#define E_MINERROR -100
inline bool is_error(uintptr_t r) {
return r >= static_cast<uintptr_t>(E_MINERROR);
}
// System call constants
// sys_kdisplay() types
#define KDISPLAY_NONE 0
#define KDISPLAY_MEMVIEWER 1
// sys_waitpid() options
#define W_NOHANG 1
// sys_open() flags
#define OF_READ 1
#define OF_WRITE 2
#define OF_CREATE 4
#define OF_CREAT OF_CREATE // ¯\_(ツ)_/¯
#define OF_TRUNC 8
// sys_lseek() origins
#define LSEEK_SET 0 // Seek from beginning of file
#define LSEEK_CUR 1 // Seek from current position
#define LSEEK_END 2 // Seek from end of file
#define LSEEK_SIZE 3 // Do not seek; return file size
#define SEEK_SET LSEEK_SET // ¯\_(ツ)_/¯
#define O_RDONLY OF_READ // ¯\_(ツ)_/¯
#define O_WRONLY OF_WRITE // ¯\_(ツ)_/¯
#define O_CREAT OF_CREAT
#define O_TRUNC OF_TRUNC
#define SEEK_END LSEEK_END
#define R_OK 1 // TODO
// Console printing
#define CPOS(row, col) ((row) * 80 + (col))
#define CROW(cpos) ((cpos) / 80)
#define CCOL(cpos) ((cpos) % 80)
#define CONSOLE_COLUMNS 80
#define CONSOLE_ROWS 25
extern uint16_t console[CONSOLE_ROWS * CONSOLE_COLUMNS];
// current position of the cursor (80 * ROW + COL)
extern volatile int cursorpos;
// console_clear
// Erases the console and moves the cursor to the upper left (CPOS(0, 0)).
void console_clear();
#define COLOR_ERROR 0xC000
// console_printf(cursor, color, format, ...)
// Format and print a message to the x86 console.
//
// The `format` argument supports some of the C printf function's escapes:
// %d (to print an integer in decimal notation), %u (to print an unsigned
// integer in decimal notation), %x (to print an unsigned integer in
// hexadecimal notation), %c (to print a character), and %s (to print a
// string). It also takes field widths and precisions like '%10s'.
//
// The `cursor` argument is a cursor position, such as `CPOS(r, c)` for
// row number `r` and column number `c`.
//
// The `color` argument is the initial color used to print. 0x0700 is a
// good choice (grey on black). The `format` escape %C changes the color
// being printed. It takes an integer from the parameter list.
//
// Returns the final position of the cursor.
int console_printf(int cpos, int color, const char* format, ...)
__attribute__((noinline));
// console_vprintf(cpos, color, format val)
// The vprintf version of console_printf.
int console_vprintf(int cpos, int color, const char* format, va_list val)
__attribute__((noinline));
// Helper versions that default to printing white-on-black at the cursor.
void console_printf(int color, const char* format, ...)
__attribute__((noinline));
void console_printf(const char* format, ...)
__attribute__((noinline));
// Generic print library
typedef struct printer printer;
struct printer {
void (*putc)(printer* p, unsigned char c, int color);
};
void printer_vprintf(printer* p, int color, const char* format, va_list val);
// error_printf(cursor, color, format, ...)
// Like `console_printf`, but `color` defaults to `COLOR_ERROR`, and
// in the kernel, the message is also printed to the log.
int error_printf(int cpos, int color, const char* format, ...)
__attribute__((noinline, cold));
int error_vprintf(int cpos, int color, const char* format, va_list val)
__attribute__((noinline, cold));
void error_printf(int color, const char* format, ...)
__attribute__((noinline, cold));
void error_printf(const char* format, ...)
__attribute__((noinline, cold));
// Assertions
// assert(x)
// If `x == 0`, print a message and fail.
#define assert(x) do { \
if (!(x)) { \
assert_fail(__FILE__, __LINE__, #x); \
} \
} while (0)
void __attribute__((noinline, noreturn, cold))
assert_fail(const char* file, int line, const char* msg);
// assert_[eq, ne, lt, le, gt, ge](x, y)
// Like `assert(x OP y)`, but also prints the values of `x` and `y` on
// failure.
#define assert_op(x, op, y) do { \
auto __x = (x); auto __y = (y); \
using __t = typename std::common_type<typeof(__x), typeof(__y)>::type; \
if (!(__x op __y)) { \
assert_op_fail<__t>(__FILE__, __LINE__, #x " " #op " " #y, \
__x, #op, __y); \
} } while (0)
#define assert_eq(x, y) assert_op(x, ==, y)
#define assert_ne(x, y) assert_op(x, !=, y)
#define assert_lt(x, y) assert_op(x, <, y)
#define assert_le(x, y) assert_op(x, <=, y)
#define assert_gt(x, y) assert_op(x, >, y)
#define assert_ge(x, y) assert_op(x, >=, y)
template <typename T>
void __attribute__((noinline, noreturn, cold))
assert_op_fail(const char* file, int line, const char* msg,
const T& x, const char* op, const T& y) {
char fmt[48];
snprintf(fmt, sizeof(fmt), "%%s:%%d: expected %%%s %s %%%s\n",
printfmt<T>::spec, op, printfmt<T>::spec);
error_printf(CPOS(22, 0), COLOR_ERROR, fmt, file, line, x, y);
assert_fail(file, line, msg);
}
// assert_memeq(x, y, sz)
// If `memcmp(x, y, sz) != 0`, print a message and fail.
#define assert_memeq(x, y, sz) do { \
auto __x = (x); auto __y = (y); size_t __sz = (sz); \
if (memcmp(__x, __y, __sz) != 0) { \
assert_memeq_fail(__FILE__, __LINE__, "memcmp(" #x ", " #y ", " #sz ") == 0", __x, __y, __sz); \
} \
} while (0)
void __attribute__((noinline, noreturn, cold))
assert_memeq_fail(const char* file, int line, const char* msg,
const char* x, const char* y, size_t sz);
// panic(format, ...)
// Print the message determined by `format` and fail.
void __attribute__((noinline, noreturn, cold))
panic(const char* format, ...);
#endif /* !CHICKADEE_LIB_H */