From 1d5e56659af7945daac0f79a06b839bfd59c8f1f Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 4 Oct 2021 21:01:31 +0200 Subject: remove support for processes returning strings on exit This isn't really all that useful, it doesn't enable anything that wasn't possible before. With it removed I'll be able to implement process_exit() in a much simpler way. --- src/init/main.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src/init/main.c') diff --git a/src/init/main.c b/src/init/main.c index 5c798d2..71a2d4e 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -23,7 +23,7 @@ int main(void) { tty_fd = _syscall_open("/tty", sizeof("/tty") - 1); if (tty_fd < 0) - _syscall_exit(argify("couldn't open tty")); + _syscall_exit(1); fs_test(); test_await(); @@ -32,7 +32,7 @@ int main(void) { while (_syscall_read(tty_fd, &c, 1, 0)) _syscall_write(tty_fd, &c, 1, 0); - _syscall_exit(argify("my job here is done.")); + _syscall_exit(0); } void read_file(const char *path, size_t len) { @@ -83,19 +83,15 @@ void fs_test(void) { } void test_await(void) { - char buf[16]; - int len; + int ret; - // the child immediately dies - if (!_syscall_fork()) - _syscall_exit(argify("i'm dead")); - if (!_syscall_fork()) - _syscall_exit(argify("i'm also dead")); + if (!_syscall_fork()) _syscall_exit(69); + if (!_syscall_fork()) _syscall_exit(420); - while ((len = _syscall_await(buf, 16)) >= 0) { + while ((ret = _syscall_await()) != ~0) { log("await returned: "); - _syscall_write(tty_fd, buf, len, 0); + //_syscall_write(tty_fd, buf, len, 0); TODO printf log("\n"); } - log("await: negative len\n"); + log("await: no more children\n"); } -- cgit v1.2.3 From 8d3ef773f8daea3a0c2b110ab48f235f5d2bf22d Mon Sep 17 00:00:00 2001 From: dzwdz Date: Tue, 5 Oct 2021 20:57:01 +0200 Subject: kill the process that caused an exception instead of panicking --- src/init/main.c | 13 +++++++++++++ src/kernel/arch/i386/interrupts/isr.c | 27 +++++++++++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) (limited to 'src/init/main.c') diff --git a/src/init/main.c b/src/init/main.c index 71a2d4e..ec6d5be 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -85,9 +85,22 @@ void fs_test(void) { void test_await(void) { int ret; + // regular exit()s if (!_syscall_fork()) _syscall_exit(69); if (!_syscall_fork()) _syscall_exit(420); + // faults + if (!_syscall_fork()) { // invalid memory access + asm volatile("movb $69, 0" ::: "memory"); + log("this shouldn't happen"); + _syscall_exit(-1); + } + if (!_syscall_fork()) { // #GP + asm volatile("hlt" ::: "memory"); + log("this shouldn't happen"); + _syscall_exit(-1); + } + while ((ret = _syscall_await()) != ~0) { log("await returned: "); //_syscall_write(tty_fd, buf, len, 0); TODO printf diff --git a/src/kernel/arch/i386/interrupts/isr.c b/src/kernel/arch/i386/interrupts/isr.c index dacecba..6701d58 100644 --- a/src/kernel/arch/i386/interrupts/isr.c +++ b/src/kernel/arch/i386/interrupts/isr.c @@ -1,29 +1,40 @@ #include #include #include +#include #include #include -#define log_n_panic(x) {tty_const(x); panic_unimplemented();} // TODO kill the current process instead of panicking - bool isr_test_interrupt_called = false; +/** Kills the process that caused the exception */ +_Noreturn static void exception_finish(void) { + // TODO check if the exception was in the kernel + process_kill(process_current, 0); // TODO make the return code mean something + process_switch_any(); +} + void isr_stage3(int interrupt) { switch (interrupt) { - case 0x08: log_n_panic("#DF"); // double fault - case 0x0D: log_n_panic("#GP"); // general protection fault + case 0x08: // double fault + tty_const("#DF"); + panic_invalid_state(); + case 0x0D: // general protection fault + exception_finish(); case 0x0E: { // page fault - int cr2; + /*int cr2; tty_const("#PF at "); asm ("mov %%cr2, %0;" : "=r"(cr2) ::); - _tty_var(cr2); - panic_unimplemented(); + _tty_var(cr2);*/ + exception_finish(); } case 0x34: isr_test_interrupt_called = true; return; - default: log_n_panic("unknown interrupt"); + default: + tty_const("unknown interrupt"); + panic_unimplemented(); } } -- cgit v1.2.3 From a3469d648a4d0f276d979ceb14b8716b46f4f72e Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 6 Oct 2021 06:28:04 +0000 Subject: init: printf base --- src/init/main.c | 3 +++ src/init/stdlib.c | 18 ++++++++++++++++++ src/init/stdlib.h | 2 ++ 3 files changed, 23 insertions(+) (limited to 'src/init/main.c') diff --git a/src/init/main.c b/src/init/main.c index ec6d5be..a819152 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -28,6 +29,8 @@ int main(void) { fs_test(); test_await(); + printf("printf test\n"); + char c; while (_syscall_read(tty_fd, &c, 1, 0)) _syscall_write(tty_fd, &c, 1, 0); diff --git a/src/init/stdlib.c b/src/init/stdlib.c index 6ed5a0a..ac80c28 100644 --- a/src/init/stdlib.c +++ b/src/init/stdlib.c @@ -1,4 +1,6 @@ #include +#include +#include int memcmp(const void *s1, const void *s2, size_t n) { const unsigned char *c1 = s1, *c2 = s2; @@ -10,3 +12,19 @@ int memcmp(const void *s1, const void *s2, size_t n) { } return 0; } + +int printf(const char *fmt, ...) { + const char *seg = fmt; // beginning of the current segment + int total = 0; + va_list argp; + va_start(argp, fmt); + for (;;) { + char c = *fmt++; + switch (c) { + case '\0': + // TODO don't assume that stdout is @ fd 0 + _syscall_write(0, seg, fmt - seg, 0); + return total + (fmt - seg); + } + } +} diff --git a/src/init/stdlib.h b/src/init/stdlib.h index 1cabc7a..357a396 100644 --- a/src/init/stdlib.h +++ b/src/init/stdlib.h @@ -5,3 +5,5 @@ // stb-style header file int memcmp(const void *s1, const void *s2, size_t n); + +int printf(const char *fmt, ...); -- cgit v1.2.3 From 44d308149282debf314cb48789b9084767c1c288 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 6 Oct 2021 06:34:23 +0000 Subject: init printf: implement %s --- src/init/main.c | 2 +- src/init/stdlib.c | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'src/init/main.c') diff --git a/src/init/main.c b/src/init/main.c index a819152..7c827eb 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -29,7 +29,7 @@ int main(void) { fs_test(); test_await(); - printf("printf test\n"); + printf("%s\n", "printf test"); char c; while (_syscall_read(tty_fd, &c, 1, 0)) diff --git a/src/init/stdlib.c b/src/init/stdlib.c index 56d6da6..876c74c 100644 --- a/src/init/stdlib.c +++ b/src/init/stdlib.c @@ -29,8 +29,22 @@ int printf(const char *fmt, ...) { switch (c) { case '\0': // TODO don't assume that stdout is @ fd 0 - _syscall_write(0, seg, fmt - seg, 0); - return total + (fmt - seg); + _syscall_write(0, seg, fmt - seg - 1, 0); + return total + (fmt - seg - 1); + + case '%': + _syscall_write(0, seg, fmt - seg - 1, 0); + total += fmt - seg - 1; + c = *fmt++; + switch (c) { + case 's': + const char *s = va_arg(argp, char*); + _syscall_write(0, s, strlen(s), 0); + total += strlen(s); + break; + } + seg = fmt; + break; } } } -- cgit v1.2.3 From a41f08402e78e9066551d72a9835a352db4069e4 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 6 Oct 2021 06:43:27 +0000 Subject: init printf: implement %x --- src/init/main.c | 2 +- src/init/stdlib.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'src/init/main.c') diff --git a/src/init/main.c b/src/init/main.c index 7c827eb..2029bc9 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -29,7 +29,7 @@ int main(void) { fs_test(); test_await(); - printf("%s\n", "printf test"); + printf("%s %x\n", "printf test", 0xACAB1312); char c; while (_syscall_read(tty_fd, &c, 1, 0)) diff --git a/src/init/stdlib.c b/src/init/stdlib.c index 876c74c..a60854b 100644 --- a/src/init/stdlib.c +++ b/src/init/stdlib.c @@ -42,6 +42,21 @@ int printf(const char *fmt, ...) { _syscall_write(0, s, strlen(s), 0); 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; + + while (i > 0) { + i -= 4; + char h = '0' + ((n >> i) & 0xf); + if (h > '9') h += 'a' - '9' - 1; + _syscall_write(0, &h, 1, 0); + total++; + } + break; } seg = fmt; break; -- cgit v1.2.3 From 0cd25153a0556b988959c10c5ecab04cbacc9506 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 6 Oct 2021 19:24:18 +0200 Subject: init: use printf for output --- src/init/main.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'src/init/main.c') diff --git a/src/init/main.c b/src/init/main.c index 2029bc9..401e4a0 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -5,7 +5,6 @@ #include #define argify(str) str, sizeof(str) - 1 -#define log(str) _syscall_write(tty_fd, argify(str), 0) extern char _bss_start; // provided by the linker extern char _bss_end; @@ -29,8 +28,6 @@ int main(void) { fs_test(); test_await(); - printf("%s %x\n", "printf test", 0xACAB1312); - char c; while (_syscall_read(tty_fd, &c, 1, 0)) _syscall_write(tty_fd, &c, 1, 0); @@ -44,9 +41,9 @@ void read_file(const char *path, size_t len) { int buf_len = 64; _syscall_write(tty_fd, path, len, 0); - log(": "); + printf(": "); if (fd < 0) { - log("couldn't open.\n"); + printf("couldn't open.\n"); return; } @@ -67,7 +64,7 @@ void fs_test(void) { } // parent: accesses the fs - log("\n\n"); + printf("\n\n"); // the trailing slash should be ignored by mount() _syscall_mount(front, argify("/init/")); read_file(argify("/init/fake.txt")); @@ -75,14 +72,14 @@ void fs_test(void) { read_file(argify("/init/2.txt")); read_file(argify("/init/dir/3.txt")); - log("\nshadowing /init/dir...\n"); + printf("\nshadowing /init/dir...\n"); _syscall_mount(-1, argify("/init/dir")); read_file(argify("/init/fake.txt")); read_file(argify("/init/1.txt")); read_file(argify("/init/2.txt")); read_file(argify("/init/dir/3.txt")); - log("\n"); + printf("\n"); } void test_await(void) { @@ -95,19 +92,16 @@ void test_await(void) { // faults if (!_syscall_fork()) { // invalid memory access asm volatile("movb $69, 0" ::: "memory"); - log("this shouldn't happen"); + printf("this shouldn't happen"); _syscall_exit(-1); } if (!_syscall_fork()) { // #GP asm volatile("hlt" ::: "memory"); - log("this shouldn't happen"); + printf("this shouldn't happen"); _syscall_exit(-1); } - while ((ret = _syscall_await()) != ~0) { - log("await returned: "); - //_syscall_write(tty_fd, buf, len, 0); TODO printf - log("\n"); - } - log("await: no more children\n"); + while ((ret = _syscall_await()) != ~0) + printf("await returned: %x\n", ret); + printf("await: no more children\n"); } -- cgit v1.2.3