From 2e79a2e5af3affa7a6a3becdffb1c91d89af90af Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 11 Jul 2022 21:54:51 +0200 Subject: user: reorganize the userland sources --- Makefile | 4 +- src/init/driver/ansiterm.c | 103 ----------------------- src/init/driver/driver.h | 5 -- src/init/driver/ps2.c | 90 -------------------- src/init/driver/tmpfs.c | 95 --------------------- src/init/fs/misc.c | 168 ------------------------------------- src/init/fs/misc.h | 16 ---- src/init/lib/esemaphore.c | 55 ------------ src/init/lib/esemaphore.h | 12 --- src/init/linker.ld | 31 ------- src/init/main.c | 89 -------------------- src/init/malloc.c | 65 -------------- src/init/malloc.h | 5 -- src/init/shell.c | 171 ------------------------------------- src/init/shell.h | 3 - src/init/stdlib.c | 120 -------------------------- src/init/stdlib.h | 23 ----- src/init/syscall.s | 24 ------ src/init/syscalls.c | 64 -------------- src/init/tar.c | 162 ----------------------------------- src/init/tar.h | 4 - src/init/tests/main.c | 205 --------------------------------------------- src/init/tests/main.h | 18 ---- src/init/tests/pipe.c | 116 ------------------------- src/init/tests/semaphore.c | 89 -------------------- src/init/tests/stress.c | 28 ------- src/user/app/main.c | 89 ++++++++++++++++++++ src/user/app/shell.c | 171 +++++++++++++++++++++++++++++++++++++ src/user/app/shell.h | 3 + src/user/driver/ansiterm.c | 103 +++++++++++++++++++++++ src/user/driver/driver.h | 5 ++ src/user/driver/ps2.c | 90 ++++++++++++++++++++ src/user/driver/tmpfs.c | 95 +++++++++++++++++++++ src/user/fs/misc.c | 168 +++++++++++++++++++++++++++++++++++++ src/user/fs/misc.h | 16 ++++ src/user/fs/tar.c | 162 +++++++++++++++++++++++++++++++++++ src/user/fs/tar.h | 4 + src/user/lib/esemaphore.c | 55 ++++++++++++ src/user/lib/esemaphore.h | 12 +++ src/user/lib/malloc.c | 65 ++++++++++++++ src/user/lib/malloc.h | 5 ++ src/user/lib/stdlib.c | 120 ++++++++++++++++++++++++++ src/user/lib/stdlib.h | 23 +++++ src/user/lib/syscall.c | 64 ++++++++++++++ src/user/lib/syscall.c.awk | 51 +++++++++++ src/user/lib/syscall.s | 24 ++++++ src/user/linker.ld | 31 +++++++ src/user/tests/main.c | 205 +++++++++++++++++++++++++++++++++++++++++++++ src/user/tests/main.h | 18 ++++ src/user/tests/pipe.c | 116 +++++++++++++++++++++++++ src/user/tests/semaphore.c | 89 ++++++++++++++++++++ src/user/tests/stress.c | 28 +++++++ tools/syscall_wrappers.awk | 51 ----------- 53 files changed, 1814 insertions(+), 1814 deletions(-) delete mode 100644 src/init/driver/ansiterm.c delete mode 100644 src/init/driver/driver.h delete mode 100644 src/init/driver/ps2.c delete mode 100644 src/init/driver/tmpfs.c delete mode 100644 src/init/fs/misc.c delete mode 100644 src/init/fs/misc.h delete mode 100644 src/init/lib/esemaphore.c delete mode 100644 src/init/lib/esemaphore.h delete mode 100644 src/init/linker.ld delete mode 100644 src/init/main.c delete mode 100644 src/init/malloc.c delete mode 100644 src/init/malloc.h delete mode 100644 src/init/shell.c delete mode 100644 src/init/shell.h delete mode 100644 src/init/stdlib.c delete mode 100644 src/init/stdlib.h delete mode 100644 src/init/syscall.s delete mode 100644 src/init/syscalls.c delete mode 100644 src/init/tar.c delete mode 100644 src/init/tar.h delete mode 100644 src/init/tests/main.c delete mode 100644 src/init/tests/main.h delete mode 100644 src/init/tests/pipe.c delete mode 100644 src/init/tests/semaphore.c delete mode 100644 src/init/tests/stress.c create mode 100644 src/user/app/main.c create mode 100644 src/user/app/shell.c create mode 100644 src/user/app/shell.h create mode 100644 src/user/driver/ansiterm.c create mode 100644 src/user/driver/driver.h create mode 100644 src/user/driver/ps2.c create mode 100644 src/user/driver/tmpfs.c create mode 100644 src/user/fs/misc.c create mode 100644 src/user/fs/misc.h create mode 100644 src/user/fs/tar.c create mode 100644 src/user/fs/tar.h create mode 100644 src/user/lib/esemaphore.c create mode 100644 src/user/lib/esemaphore.h create mode 100644 src/user/lib/malloc.c create mode 100644 src/user/lib/malloc.h create mode 100644 src/user/lib/stdlib.c create mode 100644 src/user/lib/stdlib.h create mode 100644 src/user/lib/syscall.c create mode 100644 src/user/lib/syscall.c.awk create mode 100644 src/user/lib/syscall.s create mode 100644 src/user/linker.ld create mode 100644 src/user/tests/main.c create mode 100644 src/user/tests/main.h create mode 100644 src/user/tests/pipe.c create mode 100644 src/user/tests/semaphore.c create mode 100644 src/user/tests/stress.c delete mode 100644 tools/syscall_wrappers.awk diff --git a/Makefile b/Makefile index 98397ca..f28ba8b 100644 --- a/Makefile +++ b/Makefile @@ -60,7 +60,7 @@ out/fs/boot/kernel.bin: src/kernel/linker.ld $(call from_sources, src/kernel/) $ @$(CC) $(LFLAGS) -T $^ -o $@ grub-file --is-x86-multiboot $@ -out/raw_init: src/init/linker.ld $(call from_sources, src/init/) $(call from_sources, src/shared/) +out/raw_init: src/user/linker.ld $(call from_sources, src/user/) $(call from_sources, src/shared/) @mkdir -p $(@D) @$(CC) $(LFLAGS) -T $^ -o $@ @@ -87,5 +87,5 @@ out/obj/%.c.o: src/%.c @mkdir -p $(@D) @$(CC) $(CFLAGS) -c $^ -o $@ -src/init/syscalls.c: tools/syscall_wrappers.awk src/shared/syscalls.h +src/user/lib/syscall.c: src/user/lib/syscall.c.awk src/shared/syscalls.h awk -f $^ > $@ 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 -#include -#include - -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 -#include -#include -#include - - -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 -#include -#include -#include - -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 -#include -#include -#include -#include -#include - -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 -#include - -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 -#include -#include -#include - -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 - -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 -#include -#include -#include -#include -#include -#include -#include -#include - -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 -#include -#include -#include - -#include - -#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 - -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 -#include -#include -#include -#include - -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 -#include -#include - -// 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 -#include -#include -#include - -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 - - -_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 -#include -#include -#include - -#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 - -_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 -#include -#include -#include - -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 -#include -#include -#include - -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 -#include -#include -#include -#include - -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 -#include -#include -#include - -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); -} diff --git a/src/user/app/main.c b/src/user/app/main.c new file mode 100644 index 0000000..e59b5a6 --- /dev/null +++ b/src/user/app/main.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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/user/app/shell.c b/src/user/app/shell.c new file mode 100644 index 0000000..0a8c44f --- /dev/null +++ b/src/user/app/shell.c @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include + +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/user/app/shell.h b/src/user/app/shell.h new file mode 100644 index 0000000..fb27410 --- /dev/null +++ b/src/user/app/shell.h @@ -0,0 +1,3 @@ +#pragma once + +void shell_loop(void); diff --git a/src/user/driver/ansiterm.c b/src/user/driver/ansiterm.c new file mode 100644 index 0000000..e332ca2 --- /dev/null +++ b/src/user/driver/ansiterm.c @@ -0,0 +1,103 @@ +#include +#include +#include + +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/user/driver/driver.h b/src/user/driver/driver.h new file mode 100644 index 0000000..69c4529 --- /dev/null +++ b/src/user/driver/driver.h @@ -0,0 +1,5 @@ +#pragma once + +void ansiterm_drv(void); +void ps2_drv(void); +void tmpfs_drv(void); diff --git a/src/user/driver/ps2.c b/src/user/driver/ps2.c new file mode 100644 index 0000000..b2811c3 --- /dev/null +++ b/src/user/driver/ps2.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include + + +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/user/driver/tmpfs.c b/src/user/driver/tmpfs.c new file mode 100644 index 0000000..225204b --- /dev/null +++ b/src/user/driver/tmpfs.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include + +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/user/fs/misc.c b/src/user/fs/misc.c new file mode 100644 index 0000000..bf09718 --- /dev/null +++ b/src/user/fs/misc.c @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +#include + +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/user/fs/misc.h b/src/user/fs/misc.h new file mode 100644 index 0000000..3a8b071 --- /dev/null +++ b/src/user/fs/misc.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include + +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/user/fs/tar.c b/src/user/fs/tar.c new file mode 100644 index 0000000..4b4a9a3 --- /dev/null +++ b/src/user/fs/tar.c @@ -0,0 +1,162 @@ +#include +#include +#include +#include + +#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/user/fs/tar.h b/src/user/fs/tar.h new file mode 100644 index 0000000..c1dee78 --- /dev/null +++ b/src/user/fs/tar.h @@ -0,0 +1,4 @@ +#pragma once +#include + +_Noreturn void tar_driver(void *base); diff --git a/src/user/lib/esemaphore.c b/src/user/lib/esemaphore.c new file mode 100644 index 0000000..ed42ee9 --- /dev/null +++ b/src/user/lib/esemaphore.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +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/user/lib/esemaphore.h b/src/user/lib/esemaphore.h new file mode 100644 index 0000000..e746bd7 --- /dev/null +++ b/src/user/lib/esemaphore.h @@ -0,0 +1,12 @@ +#pragma once +#include + +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/user/lib/malloc.c b/src/user/lib/malloc.c new file mode 100644 index 0000000..5157e91 --- /dev/null +++ b/src/user/lib/malloc.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +#include + +#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/user/lib/malloc.h b/src/user/lib/malloc.h new file mode 100644 index 0000000..5916ebc --- /dev/null +++ b/src/user/lib/malloc.h @@ -0,0 +1,5 @@ +#pragma once +#include + +void *malloc(size_t size); +void free(void *ptr); diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c new file mode 100644 index 0000000..c055d04 --- /dev/null +++ b/src/user/lib/stdlib.c @@ -0,0 +1,120 @@ +#include +#include +#include + +// 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/user/lib/stdlib.h b/src/user/lib/stdlib.h new file mode 100644 index 0000000..9bbcc5f --- /dev/null +++ b/src/user/lib/stdlib.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include +#include +#include + +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/user/lib/syscall.c b/src/user/lib/syscall.c new file mode 100644 index 0000000..d86df19 --- /dev/null +++ b/src/user/lib/syscall.c @@ -0,0 +1,64 @@ +/* generated by syscall.c.awk + * don't modify manually, instead run: + * make src/user/lib/syscall.c + */ +#include + + +_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/user/lib/syscall.c.awk b/src/user/lib/syscall.c.awk new file mode 100644 index 0000000..a8dd22b --- /dev/null +++ b/src/user/lib/syscall.c.awk @@ -0,0 +1,51 @@ +BEGIN { + print "\ +/* generated by syscall.c.awk\n\ + * don't modify manually, instead run:\n\ + * make src/user/lib/syscall.c\n\ + */\n\ +#include \n\ +\n"; +} + +/_syscall\(/ { next; } # skipping _syscall(), it's implemented elsewhere + +/\);/ { + sub(/;/, " {"); + print $0; + + name = substr($0, match($0, /_syscall_[^(]+/), RLENGTH); + rets = substr($0, 0, RSTART - 1); + sub(/ *$/, "", rets) + + params = substr($0, match($0, /\(.+\)/) + 1, RLENGTH - 2); + gsub(/\[[^\]]\]/, "", params); + if (params == "void") params = "" + + split(params, p, /,/); + for (i = 0; i <= 4; i += 1) { + if (p[i]) { + # p[i] is a parameter, convert it into an expression to pass to _syscall() + sub(/^ */, "", p[i]); # strip + split(p[i], words, / /); + if (length(words) != 1) { + var = words[length(words)]; + sub(/\*/, "", var); + if (words[1] != "int") var = "(int)" var; + } + p[i] = var; + } else { + p[i] = 0; + } + } + + printf "\t"; + if (!index($0, "_Noreturn")) { + printf "return "; + if (rets != "int") printf "(%s)", rets; + } + printf "_syscall(%s, %s, %s, %s, %s);\n", toupper(name), p[1], p[2], p[3], p[4]; + if (index($0, "_Noreturn")) print "\t__builtin_unreachable();"; + + print "}\n"; +} diff --git a/src/user/lib/syscall.s b/src/user/lib/syscall.s new file mode 100644 index 0000000..0af49f3 --- /dev/null +++ b/src/user/lib/syscall.s @@ -0,0 +1,24 @@ +.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/user/linker.ld b/src/user/linker.ld new file mode 100644 index 0000000..f2c9d24 --- /dev/null +++ b/src/user/linker.ld @@ -0,0 +1,31 @@ +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/user/tests/main.c b/src/user/tests/main.c new file mode 100644 index 0000000..fd5ffca --- /dev/null +++ b/src/user/tests/main.c @@ -0,0 +1,205 @@ +#define TEST_MACROS +#include +#include +#include +#include + +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/user/tests/main.h b/src/user/tests/main.h new file mode 100644 index 0000000..ed11c5e --- /dev/null +++ b/src/user/tests/main.h @@ -0,0 +1,18 @@ +#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/user/tests/pipe.c b/src/user/tests/pipe.c new file mode 100644 index 0000000..e973993 --- /dev/null +++ b/src/user/tests/pipe.c @@ -0,0 +1,116 @@ +#define TEST_MACROS +#include +#include +#include +#include + +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/user/tests/semaphore.c b/src/user/tests/semaphore.c new file mode 100644 index 0000000..e5aacaa --- /dev/null +++ b/src/user/tests/semaphore.c @@ -0,0 +1,89 @@ +#define TEST_MACROS +#include +#include +#include +#include +#include + +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/user/tests/stress.c b/src/user/tests/stress.c new file mode 100644 index 0000000..51b9c4e --- /dev/null +++ b/src/user/tests/stress.c @@ -0,0 +1,28 @@ +#define TEST_MACROS +#include +#include +#include +#include + +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); +} diff --git a/tools/syscall_wrappers.awk b/tools/syscall_wrappers.awk deleted file mode 100644 index 4a295f2..0000000 --- a/tools/syscall_wrappers.awk +++ /dev/null @@ -1,51 +0,0 @@ -BEGIN { - print "\ -/* generated by tools/syscall_wrappers.awk\n\ - * don't modify manually, instead run:\n\ - * make src/init/syscalls.c\n\ - */\n\ -#include \n\ -\n"; -} - -/_syscall\(/ { next; } # skipping _syscall(), it's implemented elsewhere - -/\);/ { - sub(/;/, " {"); - print $0; - - name = substr($0, match($0, /_syscall_[^(]+/), RLENGTH); - rets = substr($0, 0, RSTART - 1); - sub(/ *$/, "", rets) - - params = substr($0, match($0, /\(.+\)/) + 1, RLENGTH - 2); - gsub(/\[[^\]]\]/, "", params); - if (params == "void") params = "" - - split(params, p, /,/); - for (i = 0; i <= 4; i += 1) { - if (p[i]) { - # p[i] is a parameter, convert it into an expression to pass to _syscall() - sub(/^ */, "", p[i]); # strip - split(p[i], words, / /); - if (length(words) != 1) { - var = words[length(words)]; - sub(/\*/, "", var); - if (words[1] != "int") var = "(int)" var; - } - p[i] = var; - } else { - p[i] = 0; - } - } - - printf "\t"; - if (!index($0, "_Noreturn")) { - printf "return "; - if (rets != "int") printf "(%s)", rets; - } - printf "_syscall(%s, %s, %s, %s, %s);\n", toupper(name), p[1], p[2], p[3], p[4]; - if (index($0, "_Noreturn")) print "\t__builtin_unreachable();"; - - print "}\n"; -} -- cgit v1.2.3