diff options
author | Marvin Borner | 2020-05-09 17:39:31 +0200 |
---|---|---|
committer | Marvin Borner | 2020-05-09 17:39:31 +0200 |
commit | 09a66e91ec9e8a677aa48f27798753084f213713 (patch) | |
tree | 9685a13b459e60d830179c565417ed72ae855ebe | |
parent | 3a97fef4bb4780e4bc2423699063d40cbf5da923 (diff) |
Replaced fork() with spawn()!
Who needs forks anyway
-rwxr-xr-x | run | 7 | ||||
-rw-r--r-- | src/kernel/fs/elf.c | 5 | ||||
-rw-r--r-- | src/kernel/kernel.c | 2 | ||||
-rw-r--r-- | src/kernel/syscall/actions/sys_exec.c | 1 | ||||
-rw-r--r-- | src/kernel/syscall/actions/sys_fork.c | 23 | ||||
-rw-r--r-- | src/kernel/syscall/actions/sys_spawn.c | 7 | ||||
-rw-r--r-- | src/kernel/syscall/syscall.c | 7 | ||||
-rw-r--r-- | src/kernel/syscall/syscall.h | 4 | ||||
-rw-r--r-- | src/kernel/tasks/process.c | 34 | ||||
-rw-r--r-- | src/kernel/tasks/process.h | 5 | ||||
-rw-r--r-- | src/kernel/tasks/userspace.c | 3 | ||||
-rw-r--r-- | src/shared/common.h | 8 | ||||
-rw-r--r-- | src/userspace/libc/syscall.c | 4 | ||||
-rw-r--r-- | src/userspace/libc/syscall.h | 2 | ||||
-rw-r--r-- | src/userspace/libc/unistd.h | 4 | ||||
-rw-r--r-- | src/userspace/libc/unistd/fork.c | 7 | ||||
-rw-r--r-- | src/userspace/libc/unistd/spawn.c | 7 | ||||
-rw-r--r-- | src/userspace/programs/init.c | 26 | ||||
-rw-r--r-- | src/userspace/programs/root.c | 15 | ||||
-rw-r--r-- | src/userspace/programs/sh.c | 6 |
20 files changed, 68 insertions, 109 deletions
@@ -122,13 +122,6 @@ make_build() { mkdir -p ./mnt/usr/ mkdir -p ./mnt/bin/ - # TODO: Device file touching should be in kernel - mkdir -p ./mnt/dev/ - touch ./mnt/dev/stdin - touch ./mnt/dev/stdout - touch ./mnt/dev/stderr - touch ./mnt/dev/fb - cp ./build/res/font.bin ./mnt/bin/font cp ./build/user/* ./mnt/bin/ echo "Hello world, ext2!" | tee -a ./mnt/etc/test diff --git a/src/kernel/fs/elf.c b/src/kernel/fs/elf.c index 3d7fcde..be1bc95 100644 --- a/src/kernel/fs/elf.c +++ b/src/kernel/fs/elf.c @@ -1,6 +1,7 @@ #include <fs/elf.h> #include <fs/ext2.h> #include <gdt/gdt.h> +#include <io/io.h> #include <lib/lib.h> #include <lib/stdio.h> #include <lib/stdlib.h> @@ -24,6 +25,7 @@ int is_elf(struct elf_header *header) struct process *elf_load(char *path) { + cli(); u8 *file = read_file(path); if (!file) { warn("File or directory not found: %s", path); @@ -71,5 +73,6 @@ struct process *elf_load(char *path) } paging_switch_directory(paging_root_directory); + sti(); return proc; -} +}
\ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index c3ae0f3..6010716 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -68,7 +68,7 @@ void kernel_main(u32 magic, u32 multiboot_address, u32 esp) log("Content of /etc/test: %s", read_file("/etc/test")); syscalls_install(); - kexec("/bin/root"); + kexec("/bin/init"); halt_loop(); // asm ("div %0" :: "r"(0)); // Exception testing x/0 diff --git a/src/kernel/syscall/actions/sys_exec.c b/src/kernel/syscall/actions/sys_exec.c index a74bd29..049d085 100644 --- a/src/kernel/syscall/actions/sys_exec.c +++ b/src/kernel/syscall/actions/sys_exec.c @@ -1,4 +1,3 @@ -#include <io/io.h> #include <stdint.h> #include <tasks/process.h> diff --git a/src/kernel/syscall/actions/sys_fork.c b/src/kernel/syscall/actions/sys_fork.c deleted file mode 100644 index ae1b17f..0000000 --- a/src/kernel/syscall/actions/sys_fork.c +++ /dev/null @@ -1,23 +0,0 @@ -#include <interrupts/interrupts.h> -#include <io/io.h> -#include <lib/lib.h> -#include <memory/paging.h> -#include <stdint.h> -#include <system.h> -#include <tasks/process.h> - -u32 sys_fork(struct regs *r) -{ - cli(); - struct page_directory *dir = paging_copy_user_directory(current_proc->cr3); - struct process *proc = process_make_new(); - proc->cr3 = dir; - memcpy(&proc->registers, r, sizeof(struct regs)); - proc->registers.eax = proc->pid; - proc->pid = current_proc->pid + 1; - - sti(); - process_spawn(proc); - - return 0; -}
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_spawn.c b/src/kernel/syscall/actions/sys_spawn.c new file mode 100644 index 0000000..e3fa790 --- /dev/null +++ b/src/kernel/syscall/actions/sys_spawn.c @@ -0,0 +1,7 @@ +#include <stdint.h> +#include <tasks/process.h> + +u32 sys_spawn(char *path) +{ + return uspawn(path); +}
\ No newline at end of file diff --git a/src/kernel/syscall/syscall.c b/src/kernel/syscall/syscall.c index 2ae60b2..d46d3fa 100644 --- a/src/kernel/syscall/syscall.c +++ b/src/kernel/syscall/syscall.c @@ -11,10 +11,10 @@ typedef u32 (*syscall_func)(u32, ...); u32 (*syscalls[])() = { [SYS_HALT] = (u32(*)())halt_loop, // DEBUG! [SYS_EXIT] = sys_exit, - [SYS_FORK] = sys_fork, [SYS_READ] = sys_read, [SYS_WRITE] = sys_write, [SYS_EXEC] = sys_exec, + [SYS_SPAWN] = sys_spawn, [SYS_WAIT] = sys_wait, [SYS_GET_PID] = sys_get_pid, [SYS_MALLOC] = sys_malloc, @@ -36,10 +36,7 @@ void syscall_handler(struct regs *r) log("[SYSCALL] %d at [0x%x] with 0x%x 0x%x 0x%x 0x%x", r->eax, location, r->ebx, r->ecx, r->edx, r->esi, r->edi); - if (r->eax == SYS_FORK) // TODO: Fix hardcoded fork parameters - r->eax = location(r); - else - r->eax = location(r->ebx, r->ecx, r->edx, r->esi, r->edi); + r->eax = location(r->ebx, r->ecx, r->edx, r->esi, r->edi); sti(); } diff --git a/src/kernel/syscall/syscall.h b/src/kernel/syscall/syscall.h index 7e37c81..d2c50c0 100644 --- a/src/kernel/syscall/syscall.h +++ b/src/kernel/syscall/syscall.h @@ -10,14 +10,14 @@ void syscalls_install(); u32 sys_exit(u32 code); -u32 sys_fork(struct regs *r); - u32 sys_read(char *path, u32 offset, u32 count, u8 *buf); u32 sys_write(char *path, u32 offset, u32 count, u8 *buf); u32 sys_exec(char *path); +u32 sys_spawn(char *path); + u32 sys_wait(u32 pid, u32 *status, u32 options); u32 sys_get_pid(); diff --git a/src/kernel/tasks/process.c b/src/kernel/tasks/process.c index 431f1fb..576190b 100644 --- a/src/kernel/tasks/process.c +++ b/src/kernel/tasks/process.c @@ -31,13 +31,15 @@ void scheduler(struct regs *regs) current_proc = root; } - debug("Max pid: %d", pid); - debug("Task switch to %s with pid %d", current_proc->name, current_proc->pid); + /* debug("Max pid: %d", pid); */ + /* debug("Task switch to %s with pid %d", current_proc->name, current_proc->pid); */ + serial_put('.'); while (current_proc->state == PROC_ASLEEP) { current_proc = current_proc->next; if (current_proc == NULL) { warn("No next process!"); + halt_loop(); current_proc = root; } } @@ -49,7 +51,7 @@ void scheduler(struct regs *regs) void process_init(struct process *proc) { - log("Initializing process %d", pid + 1); + log("Initializing process %d", pid); cli(); root = proc; root->pid = pid++; @@ -70,8 +72,6 @@ void process_kill(u32 pid) if (proc == PID_NOT_FOUND) panic("Can't kill unknown PID"); - // flush(proc->stdout); - // flush(proc->stderr); proc->state = PROC_ASLEEP; if (proc->parent != NULL) { @@ -82,6 +82,7 @@ void process_kill(u32 pid) u32 process_spawn(struct process *process) { + cli(); debug("Spawning process %d", process->pid); process->next = root->next; root->next = process; @@ -91,6 +92,7 @@ u32 process_spawn(struct process *process) //process_suspend(current_proc->pid); + sti(); return process->pid; } @@ -225,10 +227,6 @@ u32 kexec(char *path) if (proc == NULL) return -1; - // TODO: Add stdin etc support (?) - proc->stdin = NULL; - proc->stdout = NULL; - proc->stderr = NULL; process_init(proc); return 0; } @@ -242,12 +240,24 @@ u32 uexec(char *path) if (proc == NULL) return -1; - proc->stdin = NULL; - proc->stdout = NULL; - proc->stderr = NULL; + proc->parent = current_proc; + proc->gid = current_proc->pid; + process_spawn(proc); + log("Spawned"); + return 0; +} + +u32 uspawn(char *path) +{ + debug("Spawning user process %s", path); + + struct process *proc = elf_load(path); + if (proc == NULL) + return -1; proc->parent = current_proc; proc->gid = current_proc->pid; process_spawn(proc); + log("Spawned"); return 0; }
\ No newline at end of file diff --git a/src/kernel/tasks/process.h b/src/kernel/tasks/process.h index 17467c4..8221a85 100644 --- a/src/kernel/tasks/process.h +++ b/src/kernel/tasks/process.h @@ -23,10 +23,6 @@ struct process { int state; int thread; - u32 stdin; - u32 stdout; - u32 stderr; - u32 brk; u32 handlers[6]; @@ -57,6 +53,7 @@ struct process *process_make_new(); u32 kexec(char *path); u32 uexec(char *path); +u32 uspawn(char *path); extern struct process *current_proc; diff --git a/src/kernel/tasks/userspace.c b/src/kernel/tasks/userspace.c index 375e7bb..52eb528 100644 --- a/src/kernel/tasks/userspace.c +++ b/src/kernel/tasks/userspace.c @@ -50,9 +50,6 @@ void single_yield(struct process *proc, struct regs *regs) u32 single_exit(struct regs *regs) { - //close(current_proc->stdout); - //close(current_proc->stderr); - u32 hold = regs->ebx; proc_bottom = proc_bottom->next; diff --git a/src/shared/common.h b/src/shared/common.h index 3f2904c..4f6814c 100644 --- a/src/shared/common.h +++ b/src/shared/common.h @@ -4,10 +4,10 @@ // Syscalls #define SYS_HALT 0 // Halt (debug) #define SYS_EXIT 1 // Exit process -#define SYS_FORK 2 // Fork process -#define SYS_READ 3 // Read file -#define SYS_WRITE 4 // Write file -#define SYS_EXEC 5 // Execute file +#define SYS_READ 2 // Read file +#define SYS_WRITE 3 // Write file +#define SYS_EXEC 4 // Execute file and kill parent +#define SYS_SPAWN 5 // Execute file and let parent alive #define SYS_WAIT 6 // Wait for PID #define SYS_GET_PID 7 // Get process id #define SYS_MALLOC 8 // Allocate memory diff --git a/src/userspace/libc/syscall.c b/src/userspace/libc/syscall.c index 5a9fda8..fb00a12 100644 --- a/src/userspace/libc/syscall.c +++ b/src/userspace/libc/syscall.c @@ -9,14 +9,14 @@ DEFN_SYSCALL0(halt, SYS_HALT); DEFN_SYSCALL1(exit, SYS_EXIT, u32); -DEFN_SYSCALL0(fork, SYS_FORK); - DEFN_SYSCALL4(read, SYS_READ, char *, u32, u32, u8 *); DEFN_SYSCALL4(write, SYS_WRITE, char *, u32, u32, u8 *); DEFN_SYSCALL1(exec, SYS_EXEC, char *); +DEFN_SYSCALL1(spawn, SYS_SPAWN, char *); + DEFN_SYSCALL3(wait, SYS_WAIT, u32, u32 *, u32); DEFN_SYSCALL0(get_pid, SYS_GET_PID); diff --git a/src/userspace/libc/syscall.h b/src/userspace/libc/syscall.h index bd9d38d..cb1551a 100644 --- a/src/userspace/libc/syscall.h +++ b/src/userspace/libc/syscall.h @@ -80,6 +80,8 @@ DECL_SYSCALL4(write, char *, u32, u32, u8 *); DECL_SYSCALL1(exec, char *); +DECL_SYSCALL1(spawn, char *); + DECL_SYSCALL3(wait, u32, u32 *, u32); DECL_SYSCALL0(get_pid); diff --git a/src/userspace/libc/unistd.h b/src/userspace/libc/unistd.h index 876f35c..7610eb5 100644 --- a/src/userspace/libc/unistd.h +++ b/src/userspace/libc/unistd.h @@ -5,9 +5,9 @@ u32 exec(char *path); -void exit(u32 code); +u32 spawn(char *path); -u32 fork(); +void exit(u32 code); u32 get_pid(); diff --git a/src/userspace/libc/unistd/fork.c b/src/userspace/libc/unistd/fork.c deleted file mode 100644 index 6ca054a..0000000 --- a/src/userspace/libc/unistd/fork.c +++ /dev/null @@ -1,7 +0,0 @@ -#include <stdint.h> -#include <syscall.h> - -u32 fork() -{ - return syscall_fork(); -}
\ No newline at end of file diff --git a/src/userspace/libc/unistd/spawn.c b/src/userspace/libc/unistd/spawn.c new file mode 100644 index 0000000..91ed340 --- /dev/null +++ b/src/userspace/libc/unistd/spawn.c @@ -0,0 +1,7 @@ +#include <stdint.h> +#include <syscall.h> + +u32 spawn(char *path) +{ + return syscall_spawn(path); +}
\ No newline at end of file diff --git a/src/userspace/programs/init.c b/src/userspace/programs/init.c index 6cb80ce..1f6d0a3 100644 --- a/src/userspace/programs/init.c +++ b/src/userspace/programs/init.c @@ -5,14 +5,9 @@ #include <syscall.h> #include <unistd.h> -void test(u8 *data) -{ - printf("."); -} - void main() { - if (get_pid() != 2) { + if (get_pid() != 1) { printf("Wrong PID!\n"); exit(1); } @@ -20,22 +15,13 @@ void main() // TODO: Fix page fault when mallocing printf("Initializing userspace...\n"); - // TODO: Find out, why init gets PID 1 and stops - syscall_map(MAP_KEYBOARD, (u32)&test); - + // TODO: Find out, why processes change pid randomly // TODO: Fix occasional race conditions with cli/sti - // TODO: Fix scheduler turning off randomly.. - /* u32 x; */ - /* u32 f = fork(); */ - /* if (f == 0) { */ - /* printf("Waiting...\n"); */ - /* wait(&x); */ - /* } else { */ - /* printf("Executing...\n"); */ - /* exec("/bin/sh"); */ - /* } */ + // TODO: Fix scheduler turning off at some point + spawn("/bin/sh"); + printf("ok"); while (1) { //printf("B"); }; -} +}
\ No newline at end of file diff --git a/src/userspace/programs/root.c b/src/userspace/programs/root.c deleted file mode 100644 index 86b8bf7..0000000 --- a/src/userspace/programs/root.c +++ /dev/null @@ -1,15 +0,0 @@ -#include <stdio.h> -#include <syscall.h> -#include <unistd.h> - -// This process only exists because it can't crash -void main() -{ - if (get_pid() != 1) { - printf("Wrong PID!\n"); - exit(1); - } - - exec("/bin/init"); - printf("The init process crashed!"); -}
\ No newline at end of file diff --git a/src/userspace/programs/sh.c b/src/userspace/programs/sh.c index f8f93f3..ba6ba8f 100644 --- a/src/userspace/programs/sh.c +++ b/src/userspace/programs/sh.c @@ -6,6 +6,11 @@ #include <syscall.h> #include <unistd.h> +void test(u8 *data) +{ + printf("."); +} + void main() { printf("[~] "); @@ -13,6 +18,7 @@ void main() /* while (1) { */ /* putch(getch()); */ /* } */ + //syscall_map(MAP_KEYBOARD, (u32)&test); /* syscall_halt(); */ while (1) { |