diff --git a/Makefile b/Makefile index b61c9375..6a83740c 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ ECHO = `which echo` -e MODULES = $(patsubst %.c,%.o,$(wildcard kernel/core/*.c)) FILESYSTEMS = $(patsubst %.c,%.o,$(wildcard kernel/core/fs/*.c)) VIDEODRIVERS = $(patsubst %.c,%.o,$(wildcard kernel/core/video/*.c)) -BINARIES = initrd/bin/hello initrd/bin/echo initrd/bin/yes initrd/bin/cat +BINARIES = initrd/bin/hello initrd/bin/echo initrd/bin/yes initrd/bin/cat initrd/bin/sh UTILITIES = util/bin/readelf util/bin/typewriter EMU = qemu GENEXT = genext2fs diff --git a/kernel/core/cmos.c b/kernel/core/cmos.c index c067be5d..737a72d7 100644 --- a/kernel/core/cmos.c +++ b/kernel/core/cmos.c @@ -68,6 +68,23 @@ get_time( *seconds = from_bcd(values[0]); } +int +gettimeofday(struct timeval * t, void *z) { + uint16_t values[128]; + cmos_dump(values); + + /* Math Time */ + uint32_t time = (from_bcd(values[9]) - 70) * 31556926 + + from_bcd(values[8]) * 2629743 + + from_bcd(values[7]) * 87400 + + from_bcd(values[4]) * 3600 + + from_bcd(values[2]) * 60 + + from_bcd(values[0]); + t->tv_sec = time; + t->tv_usec = 0; + return 0; +} + /* * vim:tabstop=4 * vim:noexpandtab diff --git a/kernel/core/panic.c b/kernel/core/panic.c index 39de3144..4fcfe157 100644 --- a/kernel/core/panic.c +++ b/kernel/core/panic.c @@ -12,7 +12,8 @@ void kernel_halt() { void halt_and_catch_fire(char * error_message, const char * file, int line, struct regs * regs) { __asm__ __volatile__("cli"); - settextcolor(14,4); + settextcolor(0,11); + kprintf("Process %d did a dumb.\n", getpid()); kprintf("PANIC! %s\n", error_message); kprintf("File: %s\n", file); kprintf("Line: %d\n", line); @@ -26,7 +27,9 @@ void halt_and_catch_fire(char * error_message, const char * file, int line, stru kprintf("User ESP: 0x%x\n", regs->useresp); kprintf("eip=0x%x\n", regs->eip); } - kernel_halt(); + kprintf("Killing process...\n"); + resettextcolor(); + kexit(0); } void assert_failed(const char *file, uint32_t line, const char *desc) { diff --git a/kernel/core/syscall.c b/kernel/core/syscall.c index 89d05e12..baaeac93 100644 --- a/kernel/core/syscall.c +++ b/kernel/core/syscall.c @@ -67,6 +67,20 @@ static int close(int fd) { return 0; } +static int execve(const char * filename, char *const argv[], char *const envp[]) { + int i = 0; + while (argv[i]) { + ++i; + } + /* Discard envp */ + exec((char *)filename, i, (char **)argv); + return -1; +} + +static int sys_fork() { + return fork(); +} + /* * System Call Internals */ @@ -78,9 +92,12 @@ static uintptr_t syscalls[] = { (uintptr_t)&open, (uintptr_t)&read, (uintptr_t)&write, - (uintptr_t)&close + (uintptr_t)&close, + (uintptr_t)&gettimeofday, + (uintptr_t)&execve, + (uintptr_t)&sys_fork, }; -uint32_t num_syscalls = 6; +uint32_t num_syscalls = 9; void syscalls_install() { diff --git a/kernel/core/task.c b/kernel/core/task.c index c5a0a9e4..a72acabb 100644 --- a/kernel/core/task.c +++ b/kernel/core/task.c @@ -157,7 +157,7 @@ switch_task() { __asm__ __volatile__ ("mov %%ebp, %0" : "=r" (ebp)); eip = read_eip(); if (eip == 0x10000) { - + __asm__ __volatile__ ("sti"); return; } current_task->eip = eip; @@ -190,7 +190,6 @@ void enter_user_mode() { set_kernel_stack(current_task->stack + KERNEL_STACK_SIZE); __asm__ __volatile__( - "cli\n" "mov $0x23, %ax\n" "mov %ax, %ds\n" "mov %ax, %es\n" @@ -214,7 +213,6 @@ void enter_user_jmp(uintptr_t location, int argc, char ** argv) { set_kernel_stack(current_task->stack + KERNEL_STACK_SIZE); __asm__ __volatile__( - "cli\n" "mov $0x23, %%ax\n" "mov %%ax, %%ds\n" "mov %%ax, %%es\n" diff --git a/kernel/core/video/bochs.c b/kernel/core/video/bochs.c index 00523099..c794a940 100644 --- a/kernel/core/video/bochs.c +++ b/kernel/core/video/bochs.c @@ -315,7 +315,6 @@ void draw_cursor() { } void bochs_write(char c) { - __asm__ __volatile__ ("cli"); cell_redraw(csr_x, csr_y); if (c == '\n') { for (uint16_t i = csr_x; i < TERM_WIDTH; ++i) { @@ -346,7 +345,6 @@ void bochs_write(char c) { csr_y = TERM_HEIGHT - 1; } draw_cursor(); - __asm__ __volatile__ ("sti"); } diff --git a/kernel/include/syscall.h b/kernel/include/syscall.h index 8a40df21..75a6237f 100644 --- a/kernel/include/syscall.h +++ b/kernel/include/syscall.h @@ -62,7 +62,7 @@ DECL_SYSCALL2(stat, const char *, void *); /* Process Control */ DECL_SYSCALL0(getpid); -DECL_SYSCALL3(exec, char *, char **, char **); +DECL_SYSCALL3(execve, char *, char **, char **); DECL_SYSCALL0(fork); DECL_SYSCALL2(kill, int, int); DECL_SYSCALL1(wait, int *); @@ -70,6 +70,8 @@ DECL_SYSCALL1(wait, int *); /* Memory management */ DECL_SYSCALL1(sbrk, int); +DECL_SYSCALL2(gettimeofday, void *, void *); + #endif /* diff --git a/kernel/include/system.h b/kernel/include/system.h index ab0f873f..d2046cf7 100644 --- a/kernel/include/system.h +++ b/kernel/include/system.h @@ -247,6 +247,14 @@ extern void parse_args(char * argv); extern void get_time(uint16_t * hours, uint16_t * minutes, uint16_t * seconds); extern void get_date(uint16_t * month, uint16_t * day); +struct timeval { + uint32_t tv_sec; + uint32_t tv_usec; +}; + +extern int gettimeofday(struct timeval * t, void * z); + + /* CPU Detect by Brynet */ extern int detect_cpu(); diff --git a/kernel/main.c b/kernel/main.c index 099aad4a..f48bb4c6 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -157,9 +157,7 @@ int main(struct multiboot *mboot, uint32_t mboot_mag, uintptr_t esp) start_shell(); - fork(); - - if (getpid() == 0) { + if (fork()) { char * args[] = { "/bin/yes", "\033[1;32mA\033[0m" diff --git a/loader/sh.c b/loader/sh.c new file mode 100644 index 00000000..794c21d1 --- /dev/null +++ b/loader/sh.c @@ -0,0 +1,12 @@ +#include + +int main(int argc, char ** argv) { + /* A Simple Shell */ + int i = syscall_fork(); + if (i == 0) { + syscall_print("Herpy derpy!\n"); + } else { + syscall_print("Hello World!\n"); + } + return 0; +} diff --git a/loader/syscall.c b/loader/syscall.c index 67cfcc4f..5d7831e8 100644 --- a/loader/syscall.c +++ b/loader/syscall.c @@ -6,3 +6,6 @@ DEFN_SYSCALL3(open, 2, const char *, int, int) DEFN_SYSCALL3(read, 3, int, char *, int) DEFN_SYSCALL3(write, 4, int, char *, int) DEFN_SYSCALL1(close, 5, int) +DEFN_SYSCALL2(gettimeofday, 6, void *, void *) +DEFN_SYSCALL3(execve, 7, char *, char **, char **) +DEFN_SYSCALL0(fork, 8)