diff options
-rw-r--r-- | src/init/stdlib.c | 76 | ||||
-rw-r--r-- | src/kernel/arch/generic.h | 4 | ||||
-rw-r--r-- | src/kernel/arch/i386/ata.c | 2 | ||||
-rw-r--r-- | src/kernel/arch/i386/boot.c | 12 | ||||
-rw-r--r-- | src/kernel/arch/i386/interrupts/isr.c | 4 | ||||
-rw-r--r-- | src/kernel/arch/i386/tty/tty.c | 17 | ||||
-rw-r--r-- | src/kernel/arch/io.h | 29 | ||||
-rw-r--r-- | src/kernel/main.c | 8 | ||||
-rw-r--r-- | src/kernel/mem/alloc.c | 11 | ||||
-rw-r--r-- | src/kernel/panic.h | 6 | ||||
-rw-r--r-- | src/kernel/proc.c | 2 | ||||
-rw-r--r-- | src/kernel/syscalls.c | 2 | ||||
-rw-r--r-- | src/kernel/tests/base.h | 6 | ||||
-rw-r--r-- | src/kernel/vfs/request.h | 1 | ||||
-rw-r--r-- | src/shared/printf.c | 71 | ||||
-rw-r--r-- | src/shared/printf.h | 6 |
16 files changed, 121 insertions, 136 deletions
diff --git a/src/init/stdlib.c b/src/init/stdlib.c index 82410d6..e928d2c 100644 --- a/src/init/stdlib.c +++ b/src/init/stdlib.c @@ -1,6 +1,6 @@ #include <init/stdlib.h> +#include <shared/printf.h> #include <shared/syscalls.h> -#include <stdarg.h> int __stdin = -1; int __stdout = -1; @@ -9,82 +9,12 @@ static void backend_file(void *arg, const char *buf, size_t len) { _syscall_write(*(handle_t*)arg, buf, len, -1); } -static int __printf_internal(const char *fmt, va_list argp, - void (*back)(void*, const char*, size_t), void *back_arg) -{ - const char *seg = fmt; // beginning of the current segment - int total = 0; - - for (;;) { - char c = *fmt++; - switch (c) { - case '\0': - back(back_arg, seg, fmt - seg - 1); - return total + (fmt - seg - 1); - - case '%': - back(back_arg, seg, fmt - seg - 1); - total += fmt - seg - 1; - size_t pad_len = 0; - - c = *fmt++; - while (1) { - switch (c) { - case '0': - pad_len = *fmt++ - '0'; // can skip over the null byte, idc - break; - default: - goto modifier_break; - } - c = *fmt++; - } -modifier_break: - switch (c) { - case 'c': - char c = va_arg(argp, int); - back(back_arg, &c, 1); - total += 1; - break; - - case 's': - const char *s = va_arg(argp, char*); - if (s) { - back(back_arg, s, strlen(s)); - total += strlen(s); - } - break; - - case 'x': - unsigned int n = va_arg(argp, int); - size_t i = 4; // nibbles * 4 - while (n >> i && i < (sizeof(int) * 8)) - i += 4; - - if (i < pad_len * 4) - i = pad_len * 4; - - while (i > 0) { - i -= 4; - char h = '0' + ((n >> i) & 0xf); - if (h > '9') h += 'a' - '9' - 1; - back(back_arg, &h, 1); - total++; - } - break; - } - seg = fmt; - break; - } - } -} - int printf(const char *fmt, ...) { int ret = 0; va_list argp; va_start(argp, fmt); - if (__stdout < 0) goto end; - __printf_internal(fmt, argp, backend_file, (void*)&__stdout); -end: + if (__stdout >= 0) + ret = __printf_internal(fmt, argp, backend_file, (void*)&__stdout); va_end(argp); return ret; } diff --git a/src/kernel/arch/generic.h b/src/kernel/arch/generic.h index 84be937..f8dfe51 100644 --- a/src/kernel/arch/generic.h +++ b/src/kernel/arch/generic.h @@ -1,9 +1,9 @@ #pragma once #include <kernel/arch/i386/registers.h> -#include <kernel/arch/io.h> #include <shared/types.h> #include <stdbool.h> +#include <stdarg.h> // i have no idea where else to put it // some code assumes that it's a power of 2 @@ -37,3 +37,5 @@ void pagedir_switch(struct pagedir *); // return 0 on failure void *pagedir_virt2phys(struct pagedir *dir, const void __user *virt, bool user, bool writeable); + +int kprintf(const char *fmt, ...); diff --git a/src/kernel/arch/i386/ata.c b/src/kernel/arch/i386/ata.c index b546c07..56344e4 100644 --- a/src/kernel/arch/i386/ata.c +++ b/src/kernel/arch/i386/ata.c @@ -3,8 +3,6 @@ #include <kernel/panic.h> #include <stdbool.h> -#include <kernel/arch/io.h> - static struct { enum { DEV_UNKNOWN, diff --git a/src/kernel/arch/i386/boot.c b/src/kernel/arch/i386/boot.c index a2b4f01..a3716ee 100644 --- a/src/kernel/arch/i386/boot.c +++ b/src/kernel/arch/i386/boot.c @@ -8,24 +8,26 @@ #include <kernel/main.h> #include <kernel/panic.h> +void tty_init(void); // TODO put this in a header file + void kmain_early(struct multiboot_info *multiboot) { struct kmain_info info; // setup some basic stuff tty_init(); - tty_const("gdt..."); + kprintf("gdt..."); gdt_init(); - tty_const("idt..."); + kprintf("idt..."); idt_init(); - tty_const("irq..."); + kprintf("irq..."); irq_init(); - tty_const("ata..."); + kprintf("ata..."); ata_init(); { // find the init module struct multiboot_mod *module = &multiboot->mods[0]; if (multiboot->mods_count < 1) { - tty_const("can't find init! "); + kprintf("can't find init! "); panic_invalid_state(); // no init } info.init.at = module->start; diff --git a/src/kernel/arch/i386/interrupts/isr.c b/src/kernel/arch/i386/interrupts/isr.c index 5bba43d..2d893cb 100644 --- a/src/kernel/arch/i386/interrupts/isr.c +++ b/src/kernel/arch/i386/interrupts/isr.c @@ -3,7 +3,7 @@ #include <kernel/arch/i386/interrupts/irq.h> #include <kernel/arch/i386/interrupts/isr.h> #include <kernel/arch/i386/port_io.h> -#include <kernel/arch/io.h> +#include <kernel/arch/generic.h> #include <kernel/panic.h> #include <kernel/proc.h> #include <stdbool.h> @@ -14,7 +14,7 @@ bool isr_test_interrupt_called = false; void isr_stage3(int interrupt) { switch (interrupt) { case 0x08: // double fault - tty_const("#DF"); + kprintf("#DF"); panic_invalid_state(); case 0x34: isr_test_interrupt_called = true; diff --git a/src/kernel/arch/i386/tty/tty.c b/src/kernel/arch/i386/tty/tty.c index fa3ce72..d4a97fb 100644 --- a/src/kernel/arch/i386/tty/tty.c +++ b/src/kernel/arch/i386/tty/tty.c @@ -1,17 +1,26 @@ +#include <kernel/arch/generic.h> #include <kernel/arch/i386/driver/serial.h> #include <kernel/arch/i386/tty/vga.h> -#include <kernel/arch/io.h> +#include <shared/printf.h> void tty_init(void) { vga_clear(); serial_init(); - // write hearts - vga_write("\x03 ", 2); + vga_write("\x03 ", 2); // cp437 heart serial_write("<3 ", 3); } -void tty_write(const char *buf, size_t len) { +static void backend(void *arg, const char *buf, size_t len) { vga_write(buf, len); serial_write(buf, len); } + +int kprintf(const char *fmt, ...) { + int ret; + va_list argp; + va_start(argp, fmt); + ret = __printf_internal(fmt, argp, backend, NULL); + va_end(argp); + return ret; +} diff --git a/src/kernel/arch/io.h b/src/kernel/arch/io.h deleted file mode 100644 index ef8cb17..0000000 --- a/src/kernel/arch/io.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include <stddef.h> - -void tty_init(void); -void tty_write(const char *buf, size_t len); - -static inline void _tty_hex(const char *buf, size_t len) { - char hex[2]; - for (size_t i = 0; i < len; i++) { - hex[0] = (buf[i] & 0xF0) >> 4; - hex[0] += '0'; - if (hex[0] > '9') - hex[0] += 'a' - '9' - 1; - - hex[1] = buf[i] & 0xF; - hex[1] += '0'; - if (hex[1] > '9') - hex[1] += 'a' - '9' - 1; - - tty_write(hex, 2); - } -} - -// used for static strings -#define tty_const(str) tty_write(str, sizeof(str) - 1) - -// very hacky, shouldn't be actually used - only for debugging -// prints backwards -#define _tty_var(var) _tty_hex((void*)&var, sizeof(var)) diff --git a/src/kernel/main.c b/src/kernel/main.c index e6aa878..19cc970 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -16,17 +16,17 @@ _Noreturn static void run_init(struct kmain_info *info) { true, true); proc->regs.eip = init_base; - tty_const("switching...\n"); + kprintf("switching...\n"); process_switch(proc); } void kmain(struct kmain_info info) { - tty_const("mem...\n"); + kprintf("mem...\n"); mem_init(&info); - tty_const("tests...\n"); + kprintf("tests...\n"); tests_all(); - tty_const("loading init...\n"); + kprintf("loading init...\n"); run_init(&info); } diff --git a/src/kernel/mem/alloc.c b/src/kernel/mem/alloc.c index 0495eca..44a46f9 100644 --- a/src/kernel/mem/alloc.c +++ b/src/kernel/mem/alloc.c @@ -37,9 +37,7 @@ void mem_init(struct kmain_info *info) { } void mem_debugprint(void) { - tty_const("malloc balance: "); - _tty_var(malloc_balance); - tty_const(" "); + kprintf("malloc balance: 0x%x\n", malloc_balance); } static bool bitmap_get(size_t i) { @@ -72,7 +70,7 @@ void *page_alloc(size_t pages) { return page_bitmap_start + i * PAGE_SIZE; } } - tty_const("we ran out of memory :(\ngoodbye.\n"); + kprintf("we ran out of memory :(\ngoodbye.\n"); panic_unimplemented(); } @@ -101,9 +99,8 @@ void kfree(void *ptr) { if (ptr == NULL) return; if (*magic != MALLOC_MAGIC) { // TODO add some kind of separate system log - tty_const("WARNING kfree() didn't find MALLOC_MAGIC, ptr == "); - _tty_var(ptr); - tty_const(" "); + kprintf("kfree() didn't find MALLOC_MAGIC @ 0x%x\n", ptr); + panic_invalid_state(); } else { // change the magic marker to detect double frees *magic = 0xBADF2EED; diff --git a/src/kernel/panic.h b/src/kernel/panic.h index 738bff3..c4cd194 100644 --- a/src/kernel/panic.h +++ b/src/kernel/panic.h @@ -3,9 +3,9 @@ #include <kernel/util.h> #define _panic(type) do { \ - tty_const(" an "type" PANIC! at the "); \ - tty_const(__func__); \ - tty_const(" (" __FILE__ ":" NUM2STR(__LINE__) ") "); \ + kprintf(" an "type" PANIC! at the "); \ + kprintf(__func__); \ + kprintf(" (" __FILE__ ":" NUM2STR(__LINE__) ") "); \ halt_cpu(); \ } while (0) diff --git a/src/kernel/proc.c b/src/kernel/proc.c index 2f407c3..2c4cc5c 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -169,7 +169,7 @@ void process_kill(struct process *proc, int ret) { proc->death_msg = ret; process_try2collect(proc); if (proc == process_first) { - tty_const("init killed, quitting..."); + kprintf("init killed, quitting..."); mem_debugprint(); cpu_shutdown(); } diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index dfd7dd4..0a25a63 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -286,7 +286,7 @@ int _syscall(int num, int a, int b, int c, int d) { case _SYSCALL_MEMFLAG: return _syscall_memflag((userptr_t)a, b, c); default: - tty_const("unknown syscall "); + kprintf("unknown syscall "); panic_unimplemented(); // TODO fail gracefully } } diff --git a/src/kernel/tests/base.h b/src/kernel/tests/base.h index ac50254..ec314c5 100644 --- a/src/kernel/tests/base.h +++ b/src/kernel/tests/base.h @@ -1,5 +1,5 @@ #pragma once -#include <kernel/arch/io.h> +#include <kernel/arch/generic.h> #include <kernel/util.h> #include <stdbool.h> @@ -17,7 +17,5 @@ extern bool _did_tests_fail; #define TEST_COND(cond) \ if (!(cond)) { \ _did_tests_fail = true; \ - tty_const("test failed at " \ - __FILE__ ":" NUM2STR(__LINE__) \ - " "); \ + kprintf("test failed at " __FILE__ ":" NUM2STR(__LINE__) " "); \ } diff --git a/src/kernel/vfs/request.h b/src/kernel/vfs/request.h index 151a066..ce5a7fb 100644 --- a/src/kernel/vfs/request.h +++ b/src/kernel/vfs/request.h @@ -2,6 +2,7 @@ #include <shared/flags.h> #include <shared/types.h> #include <stdbool.h> +#include <stddef.h> enum vfs_backend_type { VFS_BACK_ROOT, diff --git a/src/shared/printf.c b/src/shared/printf.c new file mode 100644 index 0000000..496d5b8 --- /dev/null +++ b/src/shared/printf.c @@ -0,0 +1,71 @@ +#include <shared/mem.h> +#include <shared/printf.h> + +int __printf_internal(const char *fmt, va_list argp, + void (*back)(void*, const char*, size_t), void *back_arg) +{ + const char *seg = fmt; // beginning of the current segment + int total = 0; + + for (;;) { + char c = *fmt++; + switch (c) { + case '\0': + back(back_arg, seg, fmt - seg - 1); + return total + (fmt - seg - 1); + + case '%': + back(back_arg, seg, fmt - seg - 1); + total += fmt - seg - 1; + size_t pad_len = 0; + + c = *fmt++; + while (1) { + switch (c) { + case '0': + pad_len = *fmt++ - '0'; // can skip over the null byte, idc + break; + default: + goto modifier_break; + } + c = *fmt++; + } +modifier_break: + switch (c) { + case 'c': + char c = va_arg(argp, int); + back(back_arg, &c, 1); + total += 1; + break; + + case 's': + const char *s = va_arg(argp, char*); + if (s) { + back(back_arg, s, strlen(s)); + total += strlen(s); + } + break; + + case 'x': + unsigned int n = va_arg(argp, int); + size_t i = 4; // nibbles * 4 + while (n >> i && i < (sizeof(int) * 8)) + i += 4; + + if (i < pad_len * 4) + i = pad_len * 4; + + while (i > 0) { + i -= 4; + char h = '0' + ((n >> i) & 0xf); + if (h > '9') h += 'a' - '9' - 1; + back(back_arg, &h, 1); + total++; + } + break; + } + seg = fmt; + break; + } + } +} diff --git a/src/shared/printf.h b/src/shared/printf.h new file mode 100644 index 0000000..45fd358 --- /dev/null +++ b/src/shared/printf.h @@ -0,0 +1,6 @@ +#pragma once +#include <stdarg.h> +#include <stddef.h> + +int __printf_internal(const char *fmt, va_list argp, + void (*back)(void*, const char*, size_t), void *back_arg); |