From e8c6577617bffa4402c07c7aa20e3c24f03c1c20 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sun, 6 Sep 2020 00:48:07 -0400 Subject: program loading, others big kernel additions: paging, elf loading, separate kernel and user page allocation it now properly loads and runs sd0:bin/init.elf still need to determine which disk was booted from, and start the init on that disk --- src/user/knob/entry.asm | 23 +++++++++ src/user/knob/env.c | 3 ++ src/user/knob/file.c | 82 +++++++++++++++++++++++++++++ src/user/knob/format.c | 13 +++++ src/user/knob/heap.c | 112 ++++++++++++++++++++++++++++++++++++++++ src/user/knob/quit.c | 29 +++++++++++ src/user/knob/user.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 396 insertions(+) create mode 100644 src/user/knob/entry.asm create mode 100644 src/user/knob/env.c create mode 100644 src/user/knob/file.c create mode 100644 src/user/knob/format.c create mode 100644 src/user/knob/heap.c create mode 100644 src/user/knob/quit.c create mode 100644 src/user/knob/user.c (limited to 'src/user/knob') diff --git a/src/user/knob/entry.asm b/src/user/knob/entry.asm new file mode 100644 index 0000000..ae024c4 --- /dev/null +++ b/src/user/knob/entry.asm @@ -0,0 +1,23 @@ +bits 32 + +global _entry + +extern main +extern quit + +extern current_disk + +section .text +_entry: + mov esp, stack + + ;TODO: heap stuff? + ;TODO: determine current_disk + ;any further needed initialization + + push quit + jmp main + +section .stack nobits alloc noexec write align=16 +resb 1024 +stack: \ No newline at end of file diff --git a/src/user/knob/env.c b/src/user/knob/env.c new file mode 100644 index 0000000..952ab86 --- /dev/null +++ b/src/user/knob/env.c @@ -0,0 +1,3 @@ +#include + +uint32_t current_drive; \ No newline at end of file diff --git a/src/user/knob/file.c b/src/user/knob/file.c new file mode 100644 index 0000000..8ab7acd --- /dev/null +++ b/src/user/knob/file.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +struct file { + _file_handle_t handle; + uint32_t position; + uint32_t length; +}; + +static const char *try_remove_prefix(const char *path, uint8_t *dn_out) { + if ((path[0] != 's') || (path[1] != 'd')) + return 0; + + const char *num_part = path + 2; + for (uint32_t i = 0; num_part[i]; ++i) + if (num_part[i] == ':') { + + uint32_t dn_large; + if (!try_sntoi(num_part, i, &dn_large) || dn_large > 255) + return 0; + + *dn_out = (uint8_t)dn_large; + return num_part + i + 1; + } + + return 0; +} + +struct file *open_file(const char *path) { + uint8_t dn; + const char *path_part = try_remove_prefix(path, &dn); + if (path_part) + path = path_part; + else + dn = current_drive; + + _file_handle_t h = _open_file(dn, path); + if (!h) + return 0; + + struct file *f = get_block(sizeof(struct file)); + f->handle = h; + f->position = 0; + f->length = _file_size(h); + + return f; +} + +void close_file(struct file *f) { + _close_file(f->handle); + free_block(f); +} + +uint32_t read_from_file(struct file *f, uint32_t max, void *buf) { + if (f->position + max > f->length) + max = f->length - f->position; + + uint32_t read = _file_read(f->handle, f->position, max, buf); + + f->position += read; + return read; +} + +uint32_t seek_file_to(struct file *f, uint32_t to) { + if (to > f->length) + to = f->length; + return f->position = to; +} + +int32_t seek_file_by(struct file *f, int32_t by) { + uint32_t old = f->position; + uint32_t to = old + by > f->length ? f->length : old + by; + f->position = to; + return to - old; +} + +__attribute__ ((pure)) +uint32_t file_size(struct file *f) { + return f->length; +} \ No newline at end of file diff --git a/src/user/knob/format.c b/src/user/knob/format.c new file mode 100644 index 0000000..f55e857 --- /dev/null +++ b/src/user/knob/format.c @@ -0,0 +1,13 @@ +#include +#include + +bool try_sntoi(const char *s, uint32_t n, uint32_t *out) { + uint32_t calc = 0; + for (uint32_t i = 0; i < n; ++i) { + if ((s[i] < '0') || (s[i] > '9')) + return false; + calc = calc * 10 + s[i] - '0'; + } + *out = calc; + return true; +} \ No newline at end of file diff --git a/src/user/knob/heap.c b/src/user/knob/heap.c new file mode 100644 index 0000000..ec21129 --- /dev/null +++ b/src/user/knob/heap.c @@ -0,0 +1,112 @@ +#include +#include +#include + +//structure appears at the start of each block +struct block_header { + struct block_header *next; + struct block_header *prev; + + //does not include header + uint32_t length; + bool allocated; +}; + +static struct block_header *first_block = 0; + +static void add_header(struct block_header *bh) { + bh->next = first_block; + bh->prev = 0; + if (first_block) + first_block->prev = bh; + first_block = bh; +} + +__attribute__ ((malloc)) +void *get_block(uint32_t bytes) { + struct block_header *header = 0; + + for (struct block_header *ptr = first_block; ptr; ptr = ptr->next) { + if (ptr->allocated) + continue; + if (ptr->length == bytes) { + header = ptr; + break; + } + if (ptr->length > bytes + sizeof(struct block_header)) { + struct block_header *bh = (void *)ptr + sizeof(struct block_header) + bytes; + + bh->length = ptr->length - sizeof(struct block_header) - bytes; + bh->allocated = false; + add_header(bh); + + ptr->length = bytes; + header = ptr; + break; + } + } + + if (!header) { + uint32_t size_with_header = bytes + sizeof(struct block_header); + if (!(size_with_header % 4096)) { + header = _allocate_ram(size_with_header / 4096); + if (!header) + return 0; + header->length = bytes; + add_header(header); + } + else { + uint32_t pages = (bytes + sizeof(struct block_header) * 2) / 4096 + 1; + header = _allocate_ram(pages); + if (!header) + return 0; + header->length = bytes; + add_header(header); + + struct block_header *bh = (void *)header + sizeof(struct block_header) + bytes; + bh->length = pages * 4096 - bytes - 2 * sizeof(struct block_header); + bh->allocated = false; + add_header(bh); + } + } + + header->allocated = true; + return (void *)header + sizeof(struct block_header); +} + +static void remove_header(struct block_header *bh) { + if (bh == first_block) + first_block = bh->next; + if (bh->prev) + bh->prev->next = bh->next; + if (bh->next) + bh->next->prev = bh->prev; +} + +void free_block(void *block) { + struct block_header *header = block - sizeof(struct block_header); + + header->allocated = false; + + void *after = block + header->length; + for (struct block_header *ptr = first_block; ptr; ptr = ptr->next) { + if (ptr == after) { + if (!ptr->allocated) { + header->length += ptr->length + sizeof(struct block_header); + remove_header(ptr); + } + break; + } + } + + //we traverse the linked list twice. it would probably be more efficient + //to do both finding the after and finding the before in the same loop. + for (struct block_header *ptr = first_block; ptr; ptr = ptr->next) + if ((void *)ptr + sizeof(struct block_header) + ptr->length == header) { + if (!ptr->allocated) { + ptr->length += sizeof(struct block_header) + header->length; + remove_header(header); + } + break; + } +} \ No newline at end of file diff --git a/src/user/knob/quit.c b/src/user/knob/quit.c new file mode 100644 index 0000000..a2ef5aa --- /dev/null +++ b/src/user/knob/quit.c @@ -0,0 +1,29 @@ +#include +#include + +struct quit_list_node { + struct quit_list_node *prev; + void (*f)(); +}; + +static struct quit_list_node head = { + .f = &_exit_task +}; + +static struct quit_list_node *last = &head; + +void on_quit(void (*run_f)()) { + struct quit_list_node *new = get_block(sizeof(struct quit_list_node)); + new->prev = last; + new->f = run_f; + last = new; +} + +__attribute__ ((noreturn)) +void quit() { + struct quit_list_node *node = last; + while (1) { + node->f(); + node = node->prev; + } +} \ No newline at end of file diff --git a/src/user/knob/user.c b/src/user/knob/user.c new file mode 100644 index 0000000..fb39851 --- /dev/null +++ b/src/user/knob/user.c @@ -0,0 +1,134 @@ +#include +#include +#include + +static const uint8_t caps_and_shift[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x22, + 0x28, 0x29, 0x2a, 0x2b, 0x3c, 0x5f, 0x3e, 0x3f, + 0x29, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5e, 0x26, + 0x2a, 0x28, 0x3a, 0x3a, 0x3c, 0x2d, 0x3e, 0x3f, + + 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x57, 0x7c, 0x7d, 0x5e, 0x5f, + 0x7e, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x00 + + //TODO: higher +}; + +static const uint8_t caps_no_shift[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x00 + + //TODO: higher +}; + +static const uint8_t shifted[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x22, + 0x28, 0x29, 0x2a, 0x2b, 0x3c, 0x5f, 0x3e, 0x3f, + 0x29, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5e, 0x26, + 0x2a, 0x28, 0x3a, 0x3a, 0x3c, 0x2d, 0x3e, 0x3f, + + 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x57, 0x7c, 0x7d, 0x5e, 0x5f, + 0x7e, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x00 + + //TODO: higher +}; + +static char get_key_char() { + _key_code_t key; + while (!(key = _get_key())) + _yield_task(); + return (char)( + (key & (_KEY_ALT | _KEY_CTRL | _KEY_META)) + ? 0 + : (key & _KEY_CAPS) + ? (key & _KEY_SHIFT) + ? caps_and_shift[key & 0xff] + : caps_no_shift[key & 0xff] + : (key & _KEY_SHIFT) + ? shifted[key & 0xff] + : (key & 0x80) + ? 0 + : (key & 0x7f) + ); +} + +void tell_user_sz(const char *sz) { + _log_string(sz); +} + +void tell_user_n(uint32_t n) { + char buf[11]; + char *buf_ptr = buf; + bool zero_yet = false; + for (uint32_t d = 1000000000U; d; d /= 10) { + uint8_t v = (n / d ) % 10; + if (v || zero_yet) { + zero_yet = true; + *buf_ptr++ = v | '0'; + } + } + if (buf_ptr == buf) + *buf_ptr++ = '0'; + *buf_ptr = '\0'; + tell_user_sz(buf); +} + +//return value and max_length don't include null terminator +uint32_t ask_user_line_sz(char *sz, uint32_t max_length) { + char log_buf[2]; + log_buf[1] = '\0'; + + uint32_t i; + for (i = 0; i != max_length; ++i) { + char key = get_key_char(); + if (key) { + log_buf[0] = key; + _log_string(log_buf); + + if (key == '\b') + i -= i ? 2 : 1; + else if (key == '\n') + break; + else + sz[i] = key; + } + } + + sz[i] = '\0'; + return i; +} \ No newline at end of file -- cgit v1.2.3