forked from samarthmahendraneu/virtual-memory-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.hh
More file actions
402 lines (336 loc) · 13.5 KB
/
lib.hh
File metadata and controls
402 lines (336 loc) · 13.5 KB
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
#ifndef WEENSYOS_LIB_HH
#define WEENSYOS_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);
void* memchr(const void* s, int c, 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);
char* strncpy(char* dst, const char* src, size_t maxlen);
size_t strlcpy(char* dst, const char* src, size_t maxlen);
int strcmp(const char* a, const char* b);
int strncmp(const char* a, const char* b, size_t maxlen);
int strcasecmp(const char* a, const char* b);
int strncasecmp(const char* a, const char* b, size_t maxlen);
char* strchr(const char* s, int c);
char* strstr(const char* haystack, const char* needle);
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(int c);
inline bool isdigit(int c);
inline bool islower(int c);
inline bool isupper(int c);
inline bool isalpha(int c);
inline bool isalnum(int c);
inline int tolower(int c);
inline int toupper(int c);
}
#define RAND_MAX 0x7FFFFFFF
int rand();
void srand(unsigned seed);
int rand(int min, int max);
struct from_chars_result {
const char* ptr;
int ec;
};
from_chars_result from_chars(const char* first, const char* last,
long& value, int base = 10);
from_chars_result from_chars(const char* first, const char* last,
unsigned long& value, int base = 10);
// 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]))
// Arithmetic
// min(a, b, ...)
// Return the minimum of the arguments.
template <typename T>
inline constexpr T min(T a, T b) {
return a < b ? a : b;
}
template <typename T, typename... Rest>
inline constexpr T min(T a, T b, Rest... c) {
return min(min(a, b), c...);
}
// max(a, b, ...)
// Return the maximum of the arguments.
template <typename T>
inline constexpr T max(T a, T b) {
return b < a ? a : b;
}
template <typename T, typename... Rest>
inline constexpr T max(T a, T b, Rest... c) {
return max(max(a, b), c...);
}
// 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);
}
// round_down(x, m)
// Return the largest multiple of `m` less than or equal to `x`.
// Equivalently, round `x` down to the nearest multiple of `m`.
template <typename T>
inline constexpr T round_down(T x, unsigned m) {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
return x - (x % m);
}
// round_up(x, m)
// Return the smallest multiple of `m` greater than or equal to `x`.
// Equivalently, round `x` up to the nearest multiple of `m`.
template <typename T>
inline constexpr T round_up(T x, unsigned m) {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
return round_down(x + m - 1, m);
}
// round_down_pow2(x)
// Return the largest power of 2 less than or equal to `x`.
// Equivalently, round `x` down to the nearest power of 2.
// Returns 0 if `x == 0`.
template <typename T>
inline constexpr T round_down_pow2(T x) {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
return x ? T(1) << (msb(x) - 1) : 0;
}
// round_up_pow2(x)
// Return the smallest power of 2 greater than or equal to `x`.
// Equivalently, round `x` up to the nearest power of 2.
// Returns 0 if `x == 0`.
template <typename T>
inline constexpr T round_up_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(int c) {
return (c >= '\t' && c <= '\r') || c == ' ';
}
inline bool isdigit(int c) {
return (unsigned(c) - unsigned('0')) < 10;
}
inline bool islower(int c) {
return (unsigned(c) - unsigned('a')) < 26;
}
inline bool isupper(int c) {
return (unsigned(c) - unsigned('A')) < 26;
}
inline bool isalpha(int c) {
return ((unsigned(c) | 0x20) - unsigned('a')) < 26;
}
inline bool isalnum(int c) {
return isalpha(c) || isdigit(c);
}
inline int tolower(int c) {
return isupper(c) ? c + 'a' - 'A' : c;
}
inline int toupper(int c) {
return islower(c) ? c + 'A' - 'a' : c;
}
// 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: the `syscall` instruction initiates a system
// call. The system call number `SYSCALL_XXX` is in the `%rax`
// register, and other arguments are arranged according to the system
// call calling convention.
#define SYSCALL_GETPID 1
#define SYSCALL_YIELD 2
#define SYSCALL_PANIC 3
#define SYSCALL_PAGE_ALLOC 4
#define SYSCALL_FORK 5
#define SYSCALL_EXIT 6
// System call error return values
#define E_INVAL -22 // Invalid argument
#define E_RANGE -34 // Out of range
#define E_MINERROR -100
inline bool is_error(uintptr_t r) {
return r >= static_cast<uintptr_t>(E_MINERROR);
}
// CGA 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 volatile 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_puts(cursor, color, s, len)
// Write a string to the CGA console. Writes exactly `len` characters.
//
// 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).
//
// Returns the final position of the cursor.
int console_puts(int cpos, int color, const char* s, size_t len);
// console_printf(cursor, color, format, ...)
// Format and print a message to the CGA 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, ...);
// 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);
// Helper versions that default to printing white-on-black at the cursor.
void console_printf(int color, const char* format, ...);
void console_printf(const char* format, ...);
// Generic print library
struct printer {
virtual void putc(unsigned char c, int color) = 0;
void vprintf(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.
[[gnu::noinline, gnu::cold]]
int error_printf(int cpos, int color, const char* format, ...);
[[gnu::noinline, gnu::cold]]
int error_vprintf(int cpos, int color, const char* format, va_list val);
[[gnu::noinline, gnu::cold]]
void error_printf(int color, const char* format, ...);
[[gnu::noinline, gnu::cold]]
void error_printf(const char* format, ...);
// 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[];
// Assertions
// assert(x)
// If `x == 0`, print a message and fail.
#define assert(x, ...) do { \
if (!(x)) { \
assert_fail(__FILE__, __LINE__, #x, ## __VA_ARGS__); \
} \
} while (false)
[[noreturn, gnu::noinline, gnu::cold]]
void assert_fail(const char* file, int line, const char* msg,
const char* description = nullptr);
// 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>
[[noreturn, gnu::noinline, gnu::cold]]
void 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)
[[noreturn, gnu::noinline, gnu::cold]]
void 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.
[[noreturn, gnu::noinline, gnu::cold]]
void panic(const char* format, ...);
#endif /* !WEENSYOS_LIB_HH */