summaryrefslogtreecommitdiff
path: root/src/init
diff options
context:
space:
mode:
authordzwdz2022-07-11 21:54:51 +0200
committerdzwdz2022-07-11 21:54:51 +0200
commit2e79a2e5af3affa7a6a3becdffb1c91d89af90af (patch)
treec253761c4ec010ad14b08f9ee8dfc1a50411b30f /src/init
parent6c01d9a7e34e1fccc2775b0e2187ac5e50dd4392 (diff)
user: reorganize the userland sources
Diffstat (limited to 'src/init')
-rw-r--r--src/init/driver/ansiterm.c103
-rw-r--r--src/init/driver/driver.h5
-rw-r--r--src/init/driver/ps2.c90
-rw-r--r--src/init/driver/tmpfs.c95
-rw-r--r--src/init/fs/misc.c168
-rw-r--r--src/init/fs/misc.h16
-rw-r--r--src/init/lib/esemaphore.c55
-rw-r--r--src/init/lib/esemaphore.h12
-rw-r--r--src/init/linker.ld31
-rw-r--r--src/init/main.c89
-rw-r--r--src/init/malloc.c65
-rw-r--r--src/init/malloc.h5
-rw-r--r--src/init/shell.c171
-rw-r--r--src/init/shell.h3
-rw-r--r--src/init/stdlib.c120
-rw-r--r--src/init/stdlib.h23
-rw-r--r--src/init/syscall.s24
-rw-r--r--src/init/syscalls.c64
-rw-r--r--src/init/tar.c162
-rw-r--r--src/init/tar.h4
-rw-r--r--src/init/tests/main.c205
-rw-r--r--src/init/tests/main.h18
-rw-r--r--src/init/tests/pipe.c116
-rw-r--r--src/init/tests/semaphore.c89
-rw-r--r--src/init/tests/stress.c28
25 files changed, 0 insertions, 1761 deletions
diff --git a/src/init/driver/ansiterm.c b/src/init/driver/ansiterm.c
deleted file mode 100644
index b7f0b04..0000000
--- a/src/init/driver/ansiterm.c
+++ /dev/null
@@ -1,103 +0,0 @@
-#include <init/driver/driver.h>
-#include <shared/syscalls.h>
-#include <stdbool.h>
-
-struct vga_cell {
- unsigned char c;
- unsigned char style;
-} __attribute__((__packed__));
-static struct vga_cell vga[80 * 25];
-
-static handle_t vga_fd;
-
-static struct {int x, y;} cursor = {0};
-static bool dirty = false;
-static bool pendingFlush = false;
-
-static void flush(void) {
- size_t off = 0;
- /* we have to do multiple write() calls if we're behind a shitty passthrough fs
- * i don't like this either */
- while (off < sizeof(vga))
- off += _syscall_write(vga_fd, (void*)vga + off, sizeof(vga) - off, off);
- dirty = false;
- pendingFlush = false;
-}
-
-static void scroll(void) {
- for (size_t i = 0; i < 80 * 24; i++)
- vga[i] = vga[i + 80];
- for (size_t i = 80 * 24; i < 80 * 25; i++)
- vga[i].c = ' ';
- cursor.y--;
- pendingFlush = true;
-}
-
-static void in_char(char c) {
- switch (c) {
- case '\n':
- cursor.x = 0;
- cursor.y++;
- pendingFlush = true;
- break;
- case '\b':
- if (--cursor.x < 0) cursor.x = 0;
- break;
- default:
- vga[cursor.y * 80 + cursor.x++].c = c;
- }
-
- if (cursor.x >= 80) {
- cursor.x = 0;
- cursor.y++;
- }
- while (cursor.y >= 25) scroll();
- dirty = true;
-}
-
-void ansiterm_drv(void) {
- vga_fd = _syscall_open("/kdev/vga", 9, 0);
- _syscall_read(vga_fd, vga, sizeof vga, 0);
-
- // find first empty line
- for (cursor.y = 0; cursor.y < 25; cursor.y++) {
- for (cursor.x = 0; cursor.x < 80; cursor.x++) {
- char c = vga[cursor.y * 80 + cursor.x].c;
- if (c != ' ' && c != '\0') break;
- }
- if (cursor.x == 80) break;
- }
- cursor.x = 0;
-
- for (int i = 0; i < 80 * 25; i++)
- vga[i].style = 0x70;
- flush();
-
- static char buf[512];
- struct fs_wait_response res;
- while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
- switch (res.op) {
- case VFSOP_OPEN:
- if (res.flags & OPEN_CREATE) {
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- // TODO check path
- _syscall_fs_respond(NULL, 0, 0);
- break;
-
- case VFSOP_WRITE:
- for (size_t i = 0; i < res.len; i++)
- in_char(buf[i]);
- /* if (pendingFlush) */ flush();
- _syscall_fs_respond(NULL, res.len, 0);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- }
-
- _syscall_exit(1);
-}
diff --git a/src/init/driver/driver.h b/src/init/driver/driver.h
deleted file mode 100644
index 69c4529..0000000
--- a/src/init/driver/driver.h
+++ /dev/null
@@ -1,5 +0,0 @@
-#pragma once
-
-void ansiterm_drv(void);
-void ps2_drv(void);
-void tmpfs_drv(void);
diff --git a/src/init/driver/ps2.c b/src/init/driver/ps2.c
deleted file mode 100644
index d01c522..0000000
--- a/src/init/driver/ps2.c
+++ /dev/null
@@ -1,90 +0,0 @@
-#include <init/driver/driver.h>
-#include <shared/container/ring.h>
-#include <shared/syscalls.h>
-#include <stdbool.h>
-
-
-static const char keymap_lower[] = {
- '\0', '\0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t',
- 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\r', '\0', 'a', 's',
- 'd', 'f', 'g', 'h', 'j', 'k', 'l', '\0', '\'', '`', '\0', '\\', 'z', 'x', 'c', 'v',
- 'b', 'n', 'm', ',', '.', '/', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1',
- '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
-};
-
-static const char keymap_upper[] = {
- '\0', '\0', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t',
- 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\r', '\0', 'A', 'S',
- 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', '\0', '|', 'Z', 'X', 'C', 'V',
- 'B', 'N', 'M', '<', '>', '?', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1',
- '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
- '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
-};
-
-
-static volatile uint8_t backlog_buf[16];
-static volatile ring_t backlog = {(void*)backlog_buf, sizeof backlog_buf, 0, 0};
-
-static handle_t fd;
-
-static bool keys[0x80] = {0};
-
-static void parse_scancode(uint8_t s) {
- bool ctrl = keys[0x1D];
- bool shift = keys[0x2A] || keys[0x36];
- bool down = !(s & 0x80);
- char c;
- s &= 0x7f;
- keys[s] = down;
-
- c = shift ? keymap_upper[s] : keymap_lower[s];
- if (ctrl && keymap_upper[s] >= 'A' && keymap_upper[s] <= 'Z')
- c = keymap_upper[s] - 'A' + 1;
- if (down && c) ring_put1b((void*)&backlog, c);
-}
-
-static void main_loop(void) {
- static char buf[512];
- struct fs_wait_response res;
- int ret;
- while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
- switch (res.op) {
- case VFSOP_OPEN:
- if (res.flags & OPEN_CREATE) {
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- _syscall_fs_respond(NULL, 1, 0);
- break;
-
- case VFSOP_READ:
- while (ring_size((void*)&backlog) == 0) {
- /* read raw input until we have something to output */
- int len = _syscall_read(fd, buf, sizeof buf, 0);
- if (len == 0) break;
- for (int i = 0; i < len; i++)
- parse_scancode(buf[i]);
- }
- ret = ring_get((void*)&backlog, buf, res.capacity);
- _syscall_fs_respond(buf, ret, 0);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- }
-}
-
-void ps2_drv(void) {
- fd = _syscall_open("/kdev/ps2", 9, 0);
- if (fd < 0) _syscall_exit(1);
-
- main_loop();
- _syscall_exit(0);
-}
diff --git a/src/init/driver/tmpfs.c b/src/init/driver/tmpfs.c
deleted file mode 100644
index bbed095..0000000
--- a/src/init/driver/tmpfs.c
+++ /dev/null
@@ -1,95 +0,0 @@
-#include <init/malloc.h>
-#include <shared/mem.h>
-#include <shared/syscalls.h>
-#include <stddef.h>
-
-struct node {
- const char *name;
- size_t len;
- struct node *next;
-};
-
-struct node *root = NULL;
-static struct node special_root;
-
-static struct node *lookup(const char *path, size_t len) {
- for (struct node *iter = root; iter; iter = iter->next) {
- if (iter->len == len && !memcmp(path, iter->name, len))
- return iter;
- }
- return NULL;
-}
-
-static struct node *tmpfs_open(const char *path, struct fs_wait_response *res) {
- if (res->len == 0) return NULL;
- path++;
- res->len--;
-
- if (res->len == 0) return &special_root;
-
- // no directory support (yet)
- if (memchr(path, '/', res->len)) return NULL;
-
- if (res->flags & OPEN_CREATE) {
- if (lookup(path, res->len)) return NULL; /* already exists */
- struct node *new = malloc(sizeof *new);
- char *namebuf = malloc(res->len);
- memcpy(namebuf, path, res->len);
- new->name = namebuf;
- new->len = res->len;
- new->next = root;
- root = new;
- return new;
- }
-
- return lookup(path, res->len);
-}
-
-void tmpfs_drv(void) {
- // TODO replace all the static allocations in drivers with mallocs
- static char buf[512];
- struct fs_wait_response res;
- struct node *ptr;
- while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
- switch (res.op) {
- case VFSOP_OPEN:
- ptr = tmpfs_open(buf, &res);
- _syscall_fs_respond(ptr, ptr ? 0 : -1, 0);
- break;
-
- case VFSOP_READ:
- if (res.id != &special_root) {
- // rw unimplemented
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- size_t buf_pos = 0;
- size_t to_skip = res.offset;
-
- for (struct node *iter = root; iter; iter = iter->next) {
- if (iter->len <= to_skip) {
- to_skip -= iter->len;
- continue;
- }
-
- if (iter->len + buf_pos - to_skip >= sizeof(buf)) {
- memcpy(buf + buf_pos, iter->name + to_skip, sizeof(buf) - buf_pos - to_skip);
- buf_pos = sizeof(buf);
- break;
- }
- memcpy(buf + buf_pos, iter->name + to_skip, iter->len - to_skip);
- buf_pos += iter->len - to_skip;
- buf[buf_pos++] = '\0';
- to_skip = 0;
- }
- _syscall_fs_respond(buf, buf_pos, 0);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- }
-
- _syscall_exit(1);
-}
diff --git a/src/init/fs/misc.c b/src/init/fs/misc.c
deleted file mode 100644
index a218bf5..0000000
--- a/src/init/fs/misc.c
+++ /dev/null
@@ -1,168 +0,0 @@
-#include <init/fs/misc.h>
-#include <init/stdlib.h>
-#include <shared/flags.h>
-#include <shared/mem.h>
-#include <shared/syscalls.h>
-#include <stdbool.h>
-
-bool fork2_n_mount(const char *path) {
- handle_t h;
- if (_syscall_fork(FORK_NEWFS, &h) > 0) { /* parent */
- _syscall_mount(h, path, strlen(path));
- _syscall_close(h);
- return true;
- }
- return false;
-}
-
-void fs_passthru(const char *prefix) {
- struct fs_wait_response res;
- const size_t buf_len = 1024;
- char *buf = malloc(buf_len);
- int prefix_len = prefix ? strlen(prefix) : 0;
- if (!buf) _syscall_exit(1);
-
- while (!_syscall_fs_wait(buf, buf_len, &res)) {
- switch (res.op) {
- case VFSOP_OPEN:
- if (prefix) {
- if (prefix_len + res.len > buf_len) {
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
-
- // TODO memmove
- char tmp[64];
- memcpy(tmp, buf, res.len);
- memcpy(buf + prefix_len, tmp, res.len);
- memcpy(buf, prefix, prefix_len);
- res.len += prefix_len;
- }
- _syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- }
- _syscall_exit(0);
-}
-
-void fs_whitelist(const char **list) {
- struct fs_wait_response res;
- const size_t buf_len = 1024;
- char *buf = malloc(buf_len);
- bool allow;
- if (!buf) _syscall_exit(1);
-
- while (!_syscall_fs_wait(buf, buf_len, &res)) {
- switch (res.op) {
- case VFSOP_OPEN:
- allow = false;
- // TODO reverse dir_inject
- for (const char **iter = list; *iter; iter++) {
- size_t len = strlen(*iter); // inefficient, whatever
- if (len <= res.len && !memcmp(buf, *iter, len)) {
- allow = true;
- break;
- }
- }
- _syscall_fs_respond(NULL, allow ? _syscall_open(buf, res.len, res.flags) : -1, FSR_DELEGATE);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- }
- _syscall_exit(0);
-}
-
-
-void fs_dir_inject(const char *path) {
- struct fs_dir_handle {
- const char *inject;
- int delegate, inject_len;
- };
-
- const size_t path_len = strlen(path);
- struct fs_wait_response res;
- struct fs_dir_handle *data;
- const size_t buf_len = 1024;
- char *buf = malloc(buf_len);
- int ret, inject_len;
-
- if (!buf) _syscall_exit(1);
-
- while (!_syscall_fs_wait(buf, buf_len, &res)) {
- data = res.id;
- switch (res.op) {
- case VFSOP_OPEN:
- if (buf[res.len - 1] == '/' &&
- res.len < path_len && !memcmp(path, buf, res.len))
- {
- /* opening a directory that we're injecting into */
-
- if (res.flags & OPEN_CREATE) {
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
-
- data = malloc(sizeof *data);
- data->delegate = _syscall_open(buf, res.len, res.flags);
- data->inject = path + res.len;
-
- /* inject up to the next slash */
- inject_len = 0;
- while (data->inject[inject_len] && data->inject[inject_len] != '/')
- inject_len++;
- if (data->inject[inject_len] == '/')
- inject_len++;
- data->inject_len = inject_len;
-
- _syscall_fs_respond(data, 0, 0);
- } else {
- _syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
- }
- break;
-
- case VFSOP_CLOSE:
- if (data->delegate >= 0)
- _syscall_close(data->delegate);
- _syscall_fs_respond(NULL, 0, 0);
- break;
-
- case VFSOP_READ:
- if (res.offset > 0) _syscall_fs_respond(NULL, 0, 0); // TODO working offsets
-
- int out_len = data->inject_len;
- memcpy(buf, data->inject, out_len);
- buf[out_len++] = '\0';
-
- if (data->delegate >= 0) {
- int to_read = res.capacity < buf_len ? res.capacity : buf_len;
- to_read -= out_len;
- ret = _syscall_read(data->delegate, buf + out_len, to_read, 0);
- if (ret > 0) out_len += ret;
- // TODO deduplicate entries
- }
-
- _syscall_fs_respond(buf, out_len, 0);
- break;
-
- case VFSOP_WRITE:
- if (data->delegate >= 0)
- ret = _syscall_write(data->delegate, buf, res.len, res.offset);
- else
- ret = -1;
- _syscall_fs_respond(NULL, ret, 0);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- }
- _syscall_exit(0);
-}
diff --git a/src/init/fs/misc.h b/src/init/fs/misc.h
deleted file mode 100644
index d64ab1e..0000000
--- a/src/init/fs/misc.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#pragma once
-#include <init/stdlib.h>
-#include <stdbool.h>
-
-bool fork2_n_mount(const char *path);
-
-void fs_passthru(const char *prefix);
-void fs_whitelist(const char **list);
-
-void fs_dir_inject(const char *path);
-
-/** Mounts something and injects its path into the fs */
-// TODO path needs to have a trailing slash
-#define MOUNT(path, impl) \
- if (!fork2_n_mount(path)) {_klogf("impl %s", path); impl;} \
- if (!fork2_n_mount("/")) {_klogf("dir for %s", path); fs_dir_inject(path);}
diff --git a/src/init/lib/esemaphore.c b/src/init/lib/esemaphore.c
deleted file mode 100644
index ac65b02..0000000
--- a/src/init/lib/esemaphore.c
+++ /dev/null
@@ -1,55 +0,0 @@
-#include <init/lib/esemaphore.h>
-#include <init/stdlib.h>
-#include <shared/flags.h>
-#include <shared/syscalls.h>
-
-void esem_signal(struct evil_sem *sem) {
- _syscall_write(sem->signal, NULL, 0, 0);
-}
-
-void esem_wait(struct evil_sem *sem) {
- _syscall_read(sem->wait, NULL, 0, 0);
-}
-
-struct evil_sem *esem_new(int value) {
- handle_t ends_wait[2], ends_signal[2];
- struct evil_sem *sem;
-
- if (value < 0) return NULL;
- if (_syscall_pipe(ends_wait, 0) < 0) return NULL;
- if (_syscall_pipe(ends_signal, 0) < 0) goto fail_signal;
- if (!(sem = malloc(sizeof *sem))) goto fail_malloc;
-
- if (!_syscall_fork(FORK_NOREAP, NULL)) {
- _syscall_close(ends_signal[1]);
- while (_syscall_read(ends_signal[0], NULL, 0, 0) >= 0) {
- if (!_syscall_fork(FORK_NOREAP, NULL)) {
- _syscall_write(ends_wait[1], NULL, 0, 0);
- _syscall_exit(0);
- }
- }
- _syscall_exit(0);
- }
- _syscall_close(ends_signal[0]);
- _syscall_close(ends_wait[1]);
-
- sem->wait = ends_wait[0];
- sem->signal = ends_signal[1];
-
- while (value--) esem_signal(sem);
- return sem;
-
-fail_malloc:
- _syscall_close(ends_signal[0]);
- _syscall_close(ends_signal[1]);
-fail_signal:
- _syscall_close(ends_wait[0]);
- _syscall_close(ends_wait[1]);
- return NULL;
-}
-
-void esem_free(struct evil_sem *sem) {
- _syscall_close(sem->wait);
- _syscall_close(sem->signal);
- free(sem);
-}
diff --git a/src/init/lib/esemaphore.h b/src/init/lib/esemaphore.h
deleted file mode 100644
index e746bd7..0000000
--- a/src/init/lib/esemaphore.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#pragma once
-#include <shared/types.h>
-
-struct evil_sem {
- handle_t wait, signal;
-};
-
-void esem_signal(struct evil_sem *sem);
-void esem_wait(struct evil_sem *sem);
-
-struct evil_sem *esem_new(int value);
-void esem_free(struct evil_sem *sem);
diff --git a/src/init/linker.ld b/src/init/linker.ld
deleted file mode 100644
index f2c9d24..0000000
--- a/src/init/linker.ld
+++ /dev/null
@@ -1,31 +0,0 @@
-ENTRY(main)
-OUTPUT_FORMAT("binary")
-
-SECTIONS
-{
- . = 2M;
- .text BLOCK(4K) : ALIGN(4K)
- {
- *(.text.startup)
- *(.text)
- }
- .rodata BLOCK(4K) : ALIGN(4K)
- {
- *(.rodata)
- }
- .data BLOCK(4K) : ALIGN(4K)
- {
- *(.data)
- }
-
- _initrd = .; /* is just appended onto the end of the binary */
- . += 2M;
-
- _bss_start = .;
- .bss BLOCK(4K) : ALIGN(4K)
- {
- *(COMMON)
- *(.bss)
- }
- _bss_end = .;
-}
diff --git a/src/init/main.c b/src/init/main.c
deleted file mode 100644
index 88553e9..0000000
--- a/src/init/main.c
+++ /dev/null
@@ -1,89 +0,0 @@
-#include <init/driver/driver.h>
-#include <init/fs/misc.h>
-#include <init/shell.h>
-#include <init/stdlib.h>
-#include <init/tar.h>
-#include <init/tests/main.h>
-#include <shared/flags.h>
-#include <shared/syscalls.h>
-#include <stdint.h>
-
-extern char _bss_start; // provided by the linker
-extern char _bss_end;
-extern char _initrd;
-
-void read_file(const char *path, size_t len);
-
-__attribute__((section(".text.startup")))
-int main(void) {
- // allocate bss
- _syscall_memflag(&_bss_start, &_bss_end - &_bss_start, MEMFLAG_PRESENT);
-
- file_reopen(stdout, "/com1", 0);
- printf("preinit\n");
-
- /* move everything provided by the kernel to /kdev */
- MOUNT("/kdev/", fs_passthru(NULL));
- if (!fork2_n_mount("/")) {
- const char *l[] = {"/kdev/", NULL};
- fs_whitelist(l);
- }
- if (!fork2_n_mount("/")) fs_dir_inject("/kdev/"); // TODO should be part of fs_whitelist
-
- MOUNT("/init/", tar_driver(&_initrd));
- MOUNT("/tmp/", tmpfs_drv());
- MOUNT("/keyboard", ps2_drv());
- MOUNT("/vga_tty", ansiterm_drv());
-
- MOUNT("/bind/", fs_passthru(NULL));
-
- if (_syscall_fork(0, NULL)) {
- /* (used to) expose a bug in the kernel
- * the program will flow like this:
- * 1. we launch the forked init
- * 2. the forked init launches both shells
- * 3. one of the shells quit
- * 4. the forked init picks it up and quits
- *
- * then, in process_kill, the other shell will be deathbedded
- *
- * before i implement(ed) reparenting, it was a lingering running child
- * of a dead process, which is invalid state
- */
- _syscall_await();
- _syscall_exit(1);
- }
-
- if (!_syscall_fork(0, NULL)) {
- if (!file_reopen(stdout, "/kdev/com1", 0)) {
- printf("couldn't open /kdev/com1\n"); // TODO borked
- _syscall_exit(1);
- }
- if (!file_reopen(stdin, "/kdev/com1", 0)) {
- printf("couldn't open /kdev/com1\n");
- _syscall_exit(1);
- }
-
- shell_loop();
- _syscall_exit(1);
- }
-
-
- if (!_syscall_fork(0, NULL)) {
- if (!file_reopen(stdout, "/vga_tty", 0)) {
- printf("couldn't open /vga_tty\n"); // TODO borked
- _syscall_exit(1);
- }
- if (!file_reopen(stdin, "/keyboard", 0)) {
- printf("couldn't open /keyboard\n");
- _syscall_exit(1);
- }
-
- shell_loop();
- _syscall_exit(1);
- }
-
- _syscall_await();
- printf("init: quitting\n");
- _syscall_exit(0);
-}
diff --git a/src/init/malloc.c b/src/init/malloc.c
deleted file mode 100644
index eb63d82..0000000
--- a/src/init/malloc.c
+++ /dev/null
@@ -1,65 +0,0 @@
-#include <init/malloc.h>
-#include <shared/flags.h>
-#include <shared/syscalls.h>
-#include <stdbool.h>
-
-#include <init/stdlib.h>
-
-#define MBLOCK_MAGIC 0x1337BABE
-
-struct mblock {
- uint32_t magic;
- size_t length; // including this struct
- bool used;
- struct mblock *next;
-};
-
-static struct mblock *first = NULL, *last = NULL;
-static struct mblock *expand(size_t size);
-
-void *malloc(size_t size) {
- struct mblock *iter = first;
- size += sizeof(struct mblock);
- while (iter) {
- if (!iter->used && iter->length >= size)
- break;
- iter = iter->next;
- }
-
- if (!iter) iter = expand(size);
- if (!iter) return NULL;
-
- iter->used = true;
- // TODO truncate and split
-
- return &iter[1];
-}
-
-void free(void *ptr) {
- struct mblock *block = ptr - sizeof(struct mblock);
- if (block->magic != MBLOCK_MAGIC) {
- // TODO debug log switch
- printf("didn't find MBLOCK_MAGIC @ 0x%x\n", block);
- return;
- }
-
- block->used = false;
-}
-
-static struct mblock *expand(size_t size) {
- struct mblock *block = _syscall_memflag(0, size, MEMFLAG_PRESENT | MEMFLAG_FINDFREE);
- if (!block) return NULL;
-
- block->magic = MBLOCK_MAGIC;
- block->length = size;
- block->used = false;
- block->next = NULL;
-
- if (!first) first = block;
- if (last) last->next = block;
- last = block;
-
- // TODO collapse
-
- return block;
-}
diff --git a/src/init/malloc.h b/src/init/malloc.h
deleted file mode 100644
index 5916ebc..0000000
--- a/src/init/malloc.h
+++ /dev/null
@@ -1,5 +0,0 @@
-#pragma once
-#include <stddef.h>
-
-void *malloc(size_t size);
-void free(void *ptr);
diff --git a/src/init/shell.c b/src/init/shell.c
deleted file mode 100644
index a4e96f9..0000000
--- a/src/init/shell.c
+++ /dev/null
@@ -1,171 +0,0 @@
-#include <init/shell.h>
-#include <init/stdlib.h>
-#include <init/tests/main.h>
-#include <shared/syscalls.h>
-#include <stdbool.h>
-
-static char *split(char *base) {
- while (*base) {
- if (*base == ' ' || *base == '\t') {
- *base++ = '\0';
- return base;
- }
- base++;
- }
- return NULL;
-}
-
-static int readline(char *buf, size_t max) {
- char c;
- size_t pos = 0;
- while (file_read(stdin, &c, 1)) {
- switch (c) {
- case '\b':
- case 0x7f:
- /* for some reason backspace outputs 0x7f (DEL) */
- if (pos != 0) {
- printf("\b \b");
- pos--;
- }
- break;
- case '\n':
- case '\r':
- printf("\n");
- buf[pos++] = '\0';
- return pos;
- default:
- if (pos < max) {
- printf("%c", c);
- buf[pos] = c;
- pos++;
- }
- }
- }
- return -1; // error
-}
-
-static void cmd_cat_ls(const char *args, bool ls) {
- libc_file *file;
- static char buf[512];
- int len; // first used for strlen(args), then length of buffer
-
- if (!args) args = "/";
- len = strlen(args);
- memcpy(buf, args, len + 1); // no overflow check - the shell is just a PoC
-
- if (ls) { // paths to directories always have a trailing slash
- char *p = buf;
- while (*p) p++;
- if (p[-1] != '/') {
- p[0] = '/';
- p[1] = '\0';
- len++;
- }
- }
-
- file = file_open(buf, 0);
- if (!file) {
- printf("couldn't open.\n");
- return;
- }
-
- while (!file->eof) {
- int len = file_read(file, buf, sizeof buf);
- if (len <= 0) break;
-
- if (ls) {
- for (int i = 0; i < len; i++)
- if (buf[i] == '\0') buf[i] = '\n';
- }
- file_write(stdout, buf, len);
- }
- file_close(file);
-}
-
-static void cmd_hexdump(const char *args) {
- static uint8_t buf[512];
- int fd, len;
-
- fd = _syscall_open(args, strlen(args), 0);
- if (fd < 0) {
- printf("couldn't open.\n");
- return;
- }
-
- len = _syscall_read(fd, buf, sizeof buf, 0);
- for (int i = 0; i < len; i += 16) {
- printf("%08x ", i);
-
- for (int j = i; j < i + 8 && j < len; j++)
- printf("%02x ", buf[j]);
- printf(" ");
- for (int j = i + 8; j < i + 16 && j < len; j++)
- printf("%02x ", buf[j]);
- printf(" |");
-
- for (int j = i; j < i + 16 && j < len; j++) {
- char c = '.';
- if (0x20 <= buf[j] && buf[j] < 0x7f) c = buf[j];
- printf("%c", c);
- }
- printf("|\n");
- }
-
- _syscall_close(fd);
-}
-
-static void cmd_touch(const char *args) {
- int fd = _syscall_open(args, strlen(args), OPEN_CREATE);
- if (fd < 0) {
- printf("couldn't create file.\n");
- return;
- }
- _syscall_close(fd);
-}
-
-void shell_loop(void) {
- static char cmd[256];
- int level = 0;
- char *args;
-
- for (;;) {
- printf("%x$ ", level);
- readline(cmd, 256);
- args = split(cmd);
- if (!strcmp(cmd, "echo")) {
- printf("%s\n", args);
- } else if (!strcmp(cmd, "cat")) {
- cmd_cat_ls(args, false);
- } else if (!strcmp(cmd, "ls")) {
- cmd_cat_ls(args, true);
- } else if (!strcmp(cmd, "hexdump")) {
- cmd_hexdump(args);
- } else if (!strcmp(cmd, "catall")) {
- const char *files[] = {
- "/init/fake.txt",
- "/init/1.txt", "/init/2.txt",
- "/init/dir/3.txt", NULL};
- for (int i = 0; files[i]; i++) {
- printf("%s:\n", files[i]);
- cmd_cat_ls(files[i], false);
- printf("\n");
- }
- } else if (!strcmp(cmd, "touch")) {
- cmd_touch(args);
- } else if (!strcmp(cmd, "shadow")) {
- _syscall_mount(-1, args, strlen(args));
- } else if (!strcmp(cmd, "exit")) {
- _syscall_exit(0);
- } else if (!strcmp(cmd, "fork")) {
- if (_syscall_fork(0, NULL))
- _syscall_await();
- else level++;
- } else if (!strcmp(cmd, "run_tests")) {
- test_all();
- } else if (!strcmp(cmd, "stress")) {
- stress_all();
- } else {
- printf("unknown command :(\n");
- }
- }
-}
diff --git a/src/init/shell.h b/src/init/shell.h
deleted file mode 100644
index fb27410..0000000
--- a/src/init/shell.h
+++ /dev/null
@@ -1,3 +0,0 @@
-#pragma once
-
-void shell_loop(void);
diff --git a/src/init/stdlib.c b/src/init/stdlib.c
deleted file mode 100644
index 4d26c2e..0000000
--- a/src/init/stdlib.c
+++ /dev/null
@@ -1,120 +0,0 @@
-#include <init/stdlib.h>
-#include <shared/printf.h>
-#include <shared/syscalls.h>
-
-// TODO oh god this garbage - malloc, actually open, [...]
-static libc_file _stdin_null = { .fd = 0 };
-static libc_file _stdout_null = { .fd = 1 };
-
-libc_file *stdin = &_stdin_null, *stdout = &_stdout_null;
-
-static void backend_file(void *arg, const char *buf, size_t len) {
- file_write((libc_file*)arg, buf, len);
-}
-
-int printf(const char *fmt, ...) {
- int ret = 0;
- va_list argp;
- va_start(argp, fmt);
- ret = __printf_internal(fmt, argp, backend_file, (void*)stdout);
- va_end(argp);
- return ret;
-}
-
-static void backend_buf(void *arg, const char *buf, size_t len) {
- char **ptrs = arg;
- size_t space = ptrs[1] - ptrs[0];
- if (len > space) len = space;
- memcpy(ptrs[0], buf, len);
- ptrs[0] += len;
-}
-
-int snprintf(char *str, size_t len, const char *fmt, ...) {
- int ret = 0;
- char *ptrs[2] = {str, str + len};
- va_list argp;
- va_start(argp, fmt);
- ret = __printf_internal(fmt, argp, backend_buf, &ptrs);
- va_end(argp);
- if (ptrs[0] < ptrs[1]) *ptrs[0] = '\0';
- return ret;
-}
-
-int _klogf(const char *fmt, ...) {
- // idiotic. however, this hack won't matter anyways
- char buf[256];
- int ret = 0;
- char *ptrs[2] = {buf, buf + sizeof buf};
- va_list argp;
- va_start(argp, fmt);
- ret = __printf_internal(fmt, argp, backend_buf, &ptrs);
- va_end(argp);
- if (ptrs[0] < ptrs[1]) *ptrs[0] = '\0';
- _syscall_debug_klog(buf, ret);
- return ret;
-}
-
-
-libc_file *file_open(const char *path, int flags) {
- handle_t h = _syscall_open(path, strlen(path), flags);
- libc_file *f;
- if (h < 0) return NULL;
-
- f = malloc(sizeof *f);
- if (!f) {
- _syscall_close(h);
- return NULL;
- }
- f->pos = 0;
- f->eof = false;
- f->fd = h;
- return f;
-}
-
-libc_file *file_reopen(libc_file *f, const char *path, int flags) {
- /* partially based on the musl implementation of freopen */
- libc_file *f2;
- if (!path) goto fail;
- f2 = file_open(path, flags);
- if (!f2) goto fail;
-
- /* shouldn't happen, but if it happens, let's roll with it. */
- if (f->fd == f2->fd) f2->fd = -1;
-
- if (_syscall_dup(f2->fd, f->fd, 0) < 0) goto fail2;
- f->pos = f2->pos;
- f->eof = f2->eof;
- file_close(f2);
- return f;
-
-fail2:
- file_close(f2);
-fail:
- file_close(f);
- return NULL;
-}
-
-int file_read(libc_file *f, char *buf, size_t len) {
- if (f->fd < 0) return -1;
-
- int res = _syscall_read(f->fd, buf, len, f->pos);
- if (res < 0) return res;
- if (res == 0 && len > 0) f->eof = true;
- f->pos += res;
- return res;
-}
-
-int file_write(libc_file *f, const char *buf, size_t len) {
- if (f->fd < 0) return -1;
-
- int res = _syscall_write(f->fd, buf, len, f->pos);
- if (res < 0) return res;
- f->pos += res;
- return res;
-}
-
-void file_close(libc_file *f) {
- if (f->fd > 0) _syscall_close(f->fd);
- if (f != &_stdin_null && f != &_stdout_null)
- free(f);
-}
diff --git a/src/init/stdlib.h b/src/init/stdlib.h
deleted file mode 100644
index e1e753d..0000000
--- a/src/init/stdlib.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#pragma once
-#include <init/malloc.h>
-#include <shared/mem.h>
-#include <stdbool.h>
-#include <stddef.h>
-
-int printf(const char *fmt, ...);
-int snprintf(char *str, size_t len, const char *fmt, ...);
-
-int _klogf(const char *fmt, ...); // for kernel debugging only
-
-typedef struct {
- int fd;
- int pos;
- bool eof;
-} libc_file;
-libc_file *file_open(const char *path, int flags);
-libc_file *file_reopen(libc_file*, const char *path, int flags);
-int file_read(libc_file*, char *buf, size_t len);
-int file_write(libc_file*, const char *buf, size_t len);
-void file_close(libc_file*);
-
-extern libc_file *stdin, *stdout;
diff --git a/src/init/syscall.s b/src/init/syscall.s
deleted file mode 100644
index 0af49f3..0000000
--- a/src/init/syscall.s
+++ /dev/null
@@ -1,24 +0,0 @@
-.section .text
-.global _syscall
-.type _syscall, @function
-_syscall:
- push %ebx // preserve registers
- push %esi
- push %edi
- push %ebp
-
- mov 20(%esp), %eax
- mov 24(%esp), %ebx
- mov %esp, %ecx
- mov $_syscall_ret, %edx
- mov 28(%esp), %esi
- mov 32(%esp), %edi
- mov 36(%esp), %ebp
- sysenter
-
-_syscall_ret:
- pop %ebp
- pop %edi
- pop %esi
- pop %ebx
- ret
diff --git a/src/init/syscalls.c b/src/init/syscalls.c
deleted file mode 100644
index 7c2bb68..0000000
--- a/src/init/syscalls.c
+++ /dev/null
@@ -1,64 +0,0 @@
-/* generated by tools/syscall_wrappers.awk
- * don't modify manually, instead run:
- * make src/init/syscalls.c
- */
-#include <shared/syscalls.h>
-
-
-_Noreturn void _syscall_exit(int ret) {
- _syscall(_SYSCALL_EXIT, ret, 0, 0, 0);
- __builtin_unreachable();
-}
-
-int _syscall_await(void) {
- return _syscall(_SYSCALL_AWAIT, 0, 0, 0, 0);
-}
-
-int _syscall_fork(int flags, handle_t __user *fs_front) {
- return _syscall(_SYSCALL_FORK, flags, (int)fs_front, 0, 0);
-}
-
-handle_t _syscall_open(const char __user *path, int len, int flags) {
- return (handle_t)_syscall(_SYSCALL_OPEN, (int)path, len, flags, 0);
-}
-
-int _syscall_mount(handle_t h, const char __user *path, int len) {
- return _syscall(_SYSCALL_MOUNT, (int)h, (int)path, len, 0);
-}
-
-handle_t _syscall_dup(handle_t from, handle_t to, int flags) {
- return (handle_t)_syscall(_SYSCALL_DUP, (int)from, (int)to, flags, 0);
-}
-
-int _syscall_read(handle_t h, void __user *buf, size_t len, int offset) {
- return _syscall(_SYSCALL_READ, (int)h, (int)buf, (int)len, offset);
-}
-
-int _syscall_write(handle_t h, const void __user *buf, size_t len, int offset) {
- return _syscall(_SYSCALL_WRITE, (int)h, (int)buf, (int)len, offset);
-}
-
-int _syscall_close(handle_t h) {
- return _syscall(_SYSCALL_CLOSE, (int)h, 0, 0, 0);
-}
-
-int _syscall_fs_wait(char __user *buf, int max_len, struct fs_wait_response __user *res) {
- return _syscall(_SYSCALL_FS_WAIT, (int)buf, max_len, (int)res, 0);
-}
-
-int _syscall_fs_respond(void __user *buf, int ret, int flags) {
- return _syscall(_SYSCALL_FS_RESPOND, (int)buf, ret, flags, 0);
-}
-
-void __user *_syscall_memflag(void __user *addr, size_t len, int flags) {
- return (void __user *)_syscall(_SYSCALL_MEMFLAG, (int)addr, (int)len, flags, 0);
-}
-
-int _syscall_pipe(handle_t __user user_ends[2], int flags) {
- return _syscall(_SYSCALL_PIPE, (int)user_ends, flags, 0, 0);
-}
-
-void _syscall_debug_klog(const void __user *buf, size_t len) {
- return (void)_syscall(_SYSCALL_DEBUG_KLOG, (int)buf, (int)len, 0, 0);
-}
-
diff --git a/src/init/tar.c b/src/init/tar.c
deleted file mode 100644
index 87236ae..0000000
--- a/src/init/tar.c
+++ /dev/null
@@ -1,162 +0,0 @@
-#include <init/stdlib.h>
-#include <shared/flags.h>
-#include <shared/syscalls.h>
-#include <stdint.h>
-
-#define BUF_SIZE 64
-
-static void *tar_open(const char *path, int len, void *base, size_t base_len);
-static void tar_read(struct fs_wait_response *res, void *base, size_t base_len);
-static int tar_size(void *sector);
-static void *tar_find(const char *path, size_t path_len, void *base, size_t base_len);
-static int oct_parse(char *str, size_t len);
-
-
-static const char *root_fakemeta = ""; /* see comment in tar_open */
-
-
-void tar_driver(void *base) {
- static char buf[BUF_SIZE];
- struct fs_wait_response res;
- void *ptr;
- while (!_syscall_fs_wait(buf, BUF_SIZE, &res)) {
- switch (res.op) {
- case VFSOP_OPEN:
- if (res.flags & OPEN_CREATE) {
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
- ptr = tar_open(buf, res.len, base, ~0);
- _syscall_fs_respond(ptr, ptr ? 0 : -1, 0);
- break;
-
- case VFSOP_READ:
- tar_read(&res, base, ~0);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0); // unsupported
- break;
- }
- }
- _syscall_exit(0);
-}
-
-static void *tar_open(const char *path, int len, void *base, size_t base_len) {
- if (len <= 0) return NULL;
- path += 1; // skip the leading slash
- len -= 1;
-
- /* TAR archives don't (seem to) contain an entry for the root dir, so i'm
- * returning a fake one. this isn't a full entry because i'm currently too
- * lazy to create a full one - thus, it has to be special cased in tar_read */
- if (len == 0)
- return (void*)root_fakemeta;
-
- return tar_find(path, len, base, base_len);
-}
-
-static void tar_read(struct fs_wait_response *res, void *base, size_t base_len) {
- void *meta = (void*)res->id;
- char type = *(char*)(meta + 156);
- size_t meta_len;
- int size;
-
- static char buf[BUF_SIZE]; // TODO reuse a single buffer
- size_t buf_pos = 0;
-
- if (meta == root_fakemeta) type = '5'; /* see comment in tar_open() */
-
- switch (type) {
- case '\0':
- case '0': /* normal files */
- size = tar_size(meta);
- if (res->offset < 0 || res->offset > size) {
- // TODO support negative offsets
- _syscall_fs_respond(NULL, -1, 0);
- } else {
- _syscall_fs_respond(meta + 512 + res->offset, size - res->offset, 0);
- }
- break;
-
- case '5': /* directory */
- meta_len = strlen(meta);
- size_t to_skip = res->offset;
-
- /* find files in dir */
- for (size_t off = 0; off < base_len;) {
- if (0 != memcmp(base + off + 257, "ustar", 5))
- break; // not a metadata sector
- // TODO more meaningful variable names and clean code up
-
- /* check if prefix matches */
- if (0 == memcmp(base + off, meta, meta_len) &&
- *(char*)(base + off + meta_len) != '\0') {
- char *suffix = base + off + meta_len;
- size_t suffix_len = strlen(suffix);
-
- /* check if the path contains any non-trailing slashes */
- char *next = suffix;
- while (*next && *next != '/') next++;
- if (*next == '/') next++;
- if (*next == '\0') {
- if (to_skip > suffix_len) {
- to_skip -= suffix_len;
- } else {
- suffix += to_skip;
- suffix_len -= to_skip;
- to_skip = 0;
-
- /* it doesn't - so let's add it to the result */
- memcpy(buf + buf_pos, suffix, suffix_len);
- buf[buf_pos + suffix_len] = '\0';
- buf_pos += suffix_len + 1;
- // TODO no buffer overrun check
- }
- }
- }
-
- size = tar_size(base + off);
- off += 512; // skip this metadata sector
- off += (size + 511) & ~511; // skip the data sectors
- }
-
- _syscall_fs_respond(buf, buf_pos, 0);
- break;
-
- default:
- _syscall_fs_respond(NULL, -1, 0);
- break;
- }
-}
-
-static int tar_size(void *sector) {
- return oct_parse(sector + 124, 11);
-}
-
-static void *tar_find(const char *path, size_t path_len, void *base, size_t base_len) {
- int size;
- if (path_len > 100) return NULL; // illegal path
-
- for (size_t off = 0; off < base_len;) {
- if (0 != memcmp(base + off + 257, "ustar", 5))
- break; // not a metadata sector
- if (0 == memcmp(base + off, path, path_len) &&
- *(char*)(base + off + path_len) == '\0')
- return base + off; // file found, quit
-
- size = tar_size(base + off);
- off += 512; // skip this metadata sector
- off += (size + 511) & ~511; // skip the data sectors
- }
- return NULL;
-}
-
-static int oct_parse(char *str, size_t len) {
- int res = 0;
- for (size_t i = 0; i < len; i++) {
- res *= 8;
- res += str[i] - '0'; // no format checking
- }
- return res;
-}
diff --git a/src/init/tar.h b/src/init/tar.h
deleted file mode 100644
index c1dee78..0000000
--- a/src/init/tar.h
+++ /dev/null
@@ -1,4 +0,0 @@
-#pragma once
-#include <shared/types.h>
-
-_Noreturn void tar_driver(void *base);
diff --git a/src/init/tests/main.c b/src/init/tests/main.c
deleted file mode 100644
index 97ed798..0000000
--- a/src/init/tests/main.c
+++ /dev/null
@@ -1,205 +0,0 @@
-#define TEST_MACROS
-#include <init/stdlib.h>
-#include <init/tests/main.h>
-#include <shared/flags.h>
-#include <shared/syscalls.h>
-
-static void run_forked(void (*fn)()) {
- if (!_syscall_fork(0, NULL)) {
- fn();
- _syscall_exit(0);
- } else {
- /* successful tests must return 0
- * TODO add a better fail msg */
- if (_syscall_await() != 0) test_fail();
- }
-}
-
-
-
-static void test_await(void) {
- /* creates 16 child processes, each returning a different value. then checks
- * if await() returns every value exactly once */
- int ret;
- int counts[16] = {0};
-
- for (int i = 0; i < 16; i++)
- if (!_syscall_fork(0, NULL))
- _syscall_exit(i);
-
- while ((ret = _syscall_await()) != ~0) {
- assert(0 <= ret && ret < 16);
- counts[ret]++;
- }
-
- for (int i = 0; i < 16; i++)
- assert(counts[i] == 1);
-}
-
-static void test_faults(void) {
- /* tests what happens when child processes fault.
- * expected behavior: parent processes still manages to finish, and it can
- * reap all its children */
- int await_cnt = 0;
-
- if (!_syscall_fork(0, NULL)) { // invalid memory access
- asm volatile("movb $69, 0" ::: "memory");
- printf("this shouldn't happen");
- _syscall_exit(-1);
- }
- if (!_syscall_fork(0, NULL)) { // #GP
- asm volatile("hlt" ::: "memory");
- printf("this shouldn't happen");
- _syscall_exit(-1);
- }
-
- while (_syscall_await() != ~0) await_cnt++;
- assert(await_cnt == 2);
-}
-
-static void test_interrupted_fs(void) {
- handle_t h;
- if (_syscall_fork(FORK_NEWFS, &h)) { /* child */
- // TODO make a similar test with all 0s passed to fs_wait
- struct fs_wait_response res;
- _syscall_fs_wait(NULL, 0, &res);
- _syscall_exit(0);
- } else { /* parent */
- _syscall_mount(h, "/", 1);
- int ret = _syscall_open("/", 1, 0);
- // the handler quits while handling that call - but this syscall should return anyways
- _syscall_exit(ret < 0 ? 0 : -1);
- }
-}
-
-static void test_orphaned_fs(void) {
- handle_t h;
- if (_syscall_fork(FORK_NEWFS, &h)) { /* child */
- _syscall_exit(0);
- } else { /* parent */
- _syscall_mount(h, "/", 1);
- int ret = _syscall_open("/", 1, 0);
- // no handler will ever be available to handle this call - the syscall should instantly return
- _syscall_exit(ret < 0 ? 0 : -1);
- }
-}
-
-static void test_memflag(void) {
- void *page = (void*)0x77777000;
- _syscall_memflag(page, 4096, MEMFLAG_PRESENT); // allocate page
- memset(page, 77, 4096); // write to it
- _syscall_memflag(page, 4096, 0); // free it
-
- if (!_syscall_fork(0, NULL)) {
- memset(page, 11, 4096); // should segfault
- _syscall_exit(0);
- } else {
- assert(_syscall_await() != 0); // test if the process crashed
- }
-
- char *pages[4];
- for (size_t i = 0; i < 4; i++) {
- pages[i] = _syscall_memflag(NULL, 4096, MEMFLAG_FINDFREE);
- printf("[test_memflag] findfree: 0x%x\n", pages[i]);
-
- assert(pages[i] == _syscall_memflag(NULL, 4096, MEMFLAG_FINDFREE | MEMFLAG_PRESENT));
- if (i > 0) assert(pages[i] != pages[i-1]);
- *pages[i] = 0x77;
- }
-
- for (size_t i = 0; i < 4; i++)
- _syscall_memflag(pages, 4096, MEMFLAG_PRESENT);
-
- // TODO check if reclaims
-}
-
-static void test_dup(void) {
- handle_t pipe[2];
- handle_t h1, h2;
- assert(_syscall_pipe(pipe, 0) >= 0);
-
- if (!_syscall_fork(0, NULL)) {
- _syscall_close(pipe[0]);
-
- h1 = _syscall_dup(pipe[1], -1, 0);
- assert(h1 >= 0);
- assert(h1 != pipe[1]);
- h2 = _syscall_dup(h1, -1, 0);
- assert(h2 >= 0);
- assert(h2 != pipe[1] && h2 != h1);
-
- _syscall_write(pipe[1], "og", 2, 0);
- _syscall_write(h1, "h1", 2, 0);
- _syscall_write(h2, "h2", 2, 0);
-
- _syscall_close(pipe[1]);
- _syscall_write(h1, "h1", 2, 0);
- _syscall_write(h2, "h2", 2, 0);
-
- assert(_syscall_dup(h1, pipe[1], 0) == pipe[1]);
- assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]);
- assert(_syscall_dup(h1, pipe[1], 0) == pipe[1]);
- assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]);
- _syscall_close(h1);
- _syscall_close(h2);
-
- assert(_syscall_dup(pipe[1], h2, 0) == h2);
- _syscall_write(h2, "h2", 2, 0);
- _syscall_close(h2);
-
- assert(_syscall_dup(pipe[1], h1, 0) == h1);
- _syscall_write(h1, "h1", 2, 0);
- _syscall_close(h1);
-
- _syscall_exit(0);
- } else {
- char buf[16];
- size_t count = 0;
- _syscall_close(pipe[1]);
- while (_syscall_read(pipe[0], buf, sizeof buf, 0) >= 0)
- count++;
- assert(count == 7);
- _syscall_await();
- }
-
-
- _syscall_close(pipe[0]);
-}
-
-static void test_malloc(void) {
- // not really a test
- void *p1, *p2;
-
- p1 = malloc(420);
- printf("p1 = 0x%x\n", p1);
-
- p2 = malloc(1024);
- printf("p2 = 0x%x\n", p2);
- free(p2);
- p2 = malloc(256);
- printf("p2 = 0x%x\n", p2);
- free(p2);
- p2 = malloc(4096);
- printf("p2 = 0x%x\n", p2);
- free(p2);
-
- free(p1);
-}
-
-static void test_misc(void) {
- assert(_syscall(~0, 0, 0, 0, 0) < 0); /* try making an invalid syscall */
-}
-
-
-void test_all(void) {
- run_forked(test_await);
- run_forked(test_faults);
- run_forked(test_interrupted_fs);
- run_forked(test_orphaned_fs);
- run_forked(test_memflag);
- run_forked(test_dup);
- run_forked(test_malloc);
- run_forked(test_pipe);
- run_forked(test_semaphore);
- run_forked(test_misc);
-}
diff --git a/src/init/tests/main.h b/src/init/tests/main.h
deleted file mode 100644
index ed11c5e..0000000
--- a/src/init/tests/main.h
+++ /dev/null
@@ -1,18 +0,0 @@
-#pragma once
-
-void stress_all(void);
-void test_all(void);
-
-void test_pipe(void);
-void test_semaphore(void);
-
-#ifdef TEST_MACROS
-
-#define argify(str) str, sizeof(str) - 1
-#define test_fail() do { \
- printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \
- _syscall_exit(0); \
-} while (0)
-#define assert(cond) if (!(cond)) test_fail();
-
-#endif
diff --git a/src/init/tests/pipe.c b/src/init/tests/pipe.c
deleted file mode 100644
index 8e9d243..0000000
--- a/src/init/tests/pipe.c
+++ /dev/null
@@ -1,116 +0,0 @@
-#define TEST_MACROS
-#include <init/stdlib.h>
-#include <init/tests/main.h>
-#include <shared/flags.h>
-#include <shared/syscalls.h>
-
-static const char *pipe_msgs[2] = {"hello", "world"};
-
-void test_pipe(void) {
- handle_t ends[2];
- char buf[16];
- int ret;
-
- /* test regular reads / writes */
- assert(_syscall_pipe(ends, 0) >= 0);
- if (!_syscall_fork(0, NULL)) {
- /* those repeated asserts ensure that you can't read/write to the wrong ends */
- assert(_syscall_read(ends[1], buf, 16, 0) < 0);
- assert(_syscall_write(ends[0], buf, 16, 0) < 0);
-
- ret = _syscall_write(ends[1], pipe_msgs[0], 5, -1);
- assert(ret == 5);
-
- assert(_syscall_read(ends[1], buf, 16, 0) < 0);
- assert(_syscall_write(ends[0], buf, 16, 0) < 0);
-
- ret = _syscall_write(ends[1], pipe_msgs[1], 5, -1);
- assert(ret == 5);
-
- _syscall_exit(0);
- } else {
- assert(_syscall_read(ends[1], buf, 16, 0) < 0);
- assert(_syscall_write(ends[0], buf, 16, 0) < 0);
-
- ret = _syscall_read(ends[0], buf, 16, 0);
- assert(ret == 5);
- assert(!memcmp(buf, pipe_msgs[0], 5));
-
- assert(_syscall_read(ends[1], buf, 16, 0) < 0);
- assert(_syscall_write(ends[0], buf, 16, 0) < 0);
-
- _syscall_read(ends[0], buf, 16, 0);
- assert(ret == 5);
- assert(!memcmp(buf, pipe_msgs[1], 5));
-
- _syscall_await();
- }
- _syscall_close(ends[0]);
- _syscall_close(ends[1]);
-
-
- /* writing to pipes with one end closed */
- assert(_syscall_pipe(ends, 0) >= 0);
- for (int i = 0; i < 16; i++) {
- if (!_syscall_fork(0, NULL)) {
- _syscall_close(ends[1]);
- assert(_syscall_read(ends[0], buf, 16, 0) < 0);
- _syscall_exit(0);
- }
- }
- _syscall_close(ends[1]);
- for (int i = 0; i < 16; i++)
- _syscall_await();
- _syscall_close(ends[0]);
-
- assert(_syscall_pipe(ends, 0) >= 0);
- for (int i = 0; i < 16; i++) {
- if (!_syscall_fork(0, NULL)) {
- _syscall_close(ends[0]);
- assert(_syscall_write(ends[1], buf, 16, 0) < 0);
- _syscall_exit(0);
- }
- }
- _syscall_close(ends[0]);
- for (int i = 0; i < 16; i++)
- _syscall_await();
- _syscall_close(ends[1]);
-
-
- /* queueing */
- assert(_syscall_pipe(ends, 0) >= 0);
- for (int i = 0; i < 16; i++) {
- if (!_syscall_fork(0, NULL)) {
- assert(_syscall_write(ends[1], pipe_msgs[0], 5, -1) == 5);
- _syscall_exit(0);
- }
- }
- _syscall_close(ends[1]);
- for (int i = 0; i < 16; i++) {
- assert(_syscall_read(ends[0], buf, sizeof buf, 0) == 5);
- _syscall_await();
- }
- assert(_syscall_read(ends[0], buf, sizeof buf, 0) < 0);
- _syscall_close(ends[0]);
-
- assert(_syscall_pipe(ends, 0) >= 0);
- for (int i = 0; i < 16; i++) {
- if (!_syscall_fork(0, NULL)) {
- memset(buf, 0, sizeof buf);
- assert(_syscall_read(ends[0], buf, 5, -1) == 5);
- assert(!memcmp(buf, pipe_msgs[1], 5));
- _syscall_exit(0);
- }
- }
- _syscall_close(ends[0]);
- for (int i = 0; i < 16; i++) {
- assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) == 5);
- _syscall_await();
- }
- assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) < 0);
- _syscall_close(ends[1]);
-
-
- // not a to.do detect when all processes that can read are stuck on writing to the pipe and vice versa
- // it seems like linux just lets the process hang endlessly.
-}
diff --git a/src/init/tests/semaphore.c b/src/init/tests/semaphore.c
deleted file mode 100644
index cb47d41..0000000
--- a/src/init/tests/semaphore.c
+++ /dev/null
@@ -1,89 +0,0 @@
-#define TEST_MACROS
-#include <init/lib/esemaphore.h>
-#include <init/stdlib.h>
-#include <init/tests/main.h>
-#include <shared/flags.h>
-#include <shared/syscalls.h>
-
-static void odd(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) {
- _syscall_write(out, "1", 1, -1);
- esem_signal(sem1);
-
- esem_wait(sem2);
- _syscall_write(out, "3", 1, -1);
- esem_signal(sem1);
-
- esem_wait(sem2);
- _syscall_write(out, "5", 1, -1);
- esem_signal(sem1);
-}
-
-static void even(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) {
- esem_wait(sem1);
- _syscall_write(out, "2", 1, -1);
- esem_signal(sem2);
-
- esem_wait(sem1);
- _syscall_write(out, "4", 1, -1);
- esem_signal(sem2);
-
- esem_wait(sem1);
- _syscall_write(out, "6", 1, -1);
- esem_signal(sem2);
-}
-
-void test_semaphore(void) {
- struct evil_sem *sem1, *sem2;
- handle_t pipe[2];
- assert(_syscall_pipe(pipe, 0) >= 0);
-
- if (!_syscall_fork(0, NULL)) {
- sem1 = esem_new(0);
- sem2 = esem_new(0);
- assert(sem1 && sem2);
- if (!_syscall_fork(0, NULL)) {
- odd(pipe[1], sem1, sem2);
- _syscall_exit(69);
- } else {
- even(pipe[1], sem1, sem2);
- assert(_syscall_await() == 69);
- }
- esem_free(sem1);
- esem_free(sem2);
-
- _syscall_write(pipe[1], "|", 1, -1);
-
- sem1 = esem_new(0);
- sem2 = esem_new(0);
- assert(sem1 && sem2);
- if (!_syscall_fork(0, NULL)) {
- even(pipe[1], sem1, sem2);
- _syscall_exit(69);
- } else {
- odd(pipe[1], sem1, sem2);
- assert(_syscall_await() == 69);
- _syscall_await();
- }
- esem_free(sem1);
- esem_free(sem2);
-
- _syscall_exit(0);
- } else {
- _syscall_close(pipe[1]);
-
- char buf[16];
- size_t pos = 0;
- for (;;) {
- int ret = _syscall_read(pipe[0], buf + pos, sizeof(buf) - pos, 0);
- if (ret < 0) break;
- pos += ret;
- }
- buf[pos] = '\0'; // idc about the "potential" overflow
- if (strcmp(buf, "123456|123456")) {
- printf("%s\n", buf);
- test_fail();
- }
-
- _syscall_await();
- }
-}
diff --git a/src/init/tests/stress.c b/src/init/tests/stress.c
deleted file mode 100644
index 11c30cb..0000000
--- a/src/init/tests/stress.c
+++ /dev/null
@@ -1,28 +0,0 @@
-#define TEST_MACROS
-#include <init/stdlib.h>
-#include <init/tests/main.h>
-#include <shared/flags.h>
-#include <shared/syscalls.h>
-
-static void run_forked(void (*fn)()) {
- if (!_syscall_fork(0, NULL)) {
- fn();
- _syscall_exit(0);
- } else {
- /* successful tests must return 0
- * TODO add a better fail msg */
- if (_syscall_await() != 0) test_fail();
- }
-}
-
-
-static void stress_fork(void) {
- for (size_t i = 0; i < 2048; i++) {
- if (!_syscall_fork(0, NULL)) _syscall_exit(0);
- _syscall_await();
- }
-}
-
-void stress_all(void) {
- run_forked(stress_fork);
-}