From 1ecb3fb821f49450ba4b08ad7d7a23d3acb75c47 Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Fri, 18 May 2012 13:42:09 +0200 Subject: Improvements. Next: initrd. --- src/kernel/core/kmain.cpp | 6 ++--- src/kernel/linker/elf.cpp | 2 +- src/kernel/linker/elf.h | 2 +- src/kernel/task/sched.cpp | 1 + src/user/lib/tce/syscall.c | 4 +-- src/user/test/main.c | 67 +++++++++++++++++++++++++++++----------------- 6 files changed, 51 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/kernel/core/kmain.cpp b/src/kernel/core/kmain.cpp index 314c033..9ec06df 100644 --- a/src/kernel/core/kmain.cpp +++ b/src/kernel/core/kmain.cpp @@ -77,15 +77,15 @@ extern "C" void kmain(multiboot_info_t* mbd, int32_t magic) { *ke_vt << "Loading modules :\n"; for (unsigned i = 0; i < mbd->mods_count; i++) { *ke_vt << " * " << (char*)mods[i].string; - if (elf_check((uint8_t*)mods[i].mod_start)) { - *ke_vt << " : Invalid ELF file\n"; - } else { + if (elf_check((uint8_t*)mods[i].mod_start) == 0) { process *pr = elf_exec((uint8_t*)mods[i].mod_start, PL_USER); if (pr == 0) { *ke_vt << " : Error loading\n"; } else { *ke_vt << " : OK, pid=" << (int)pr->pid << "\n"; } + } else { + *ke_vt << " : Invalid ELF file\n"; } } diff --git a/src/kernel/linker/elf.cpp b/src/kernel/linker/elf.cpp index c6aebaa..0375e40 100644 --- a/src/kernel/linker/elf.cpp +++ b/src/kernel/linker/elf.cpp @@ -7,7 +7,7 @@ int elf_check(uint8_t *data) { elf_ehdr *h = (elf_ehdr*)data; if (h->e_ident[0] == 0x7F && h->e_ident[1] == 'E' && h->e_ident[2] == 'L' && h->e_ident[3] == 'F') return 0; - return 1; + return -1; } thread_entry elf_load(uint8_t *data, process* process) { diff --git a/src/kernel/linker/elf.h b/src/kernel/linker/elf.h index bb61795..184f410 100644 --- a/src/kernel/linker/elf.h +++ b/src/kernel/linker/elf.h @@ -56,7 +56,7 @@ struct phdr { uint8_t* data; }; -int elf_check(uint8_t *data); //0 if ok, 1 if not a valid ELF +int elf_check(uint8_t *data); //0 if ok, -1 if not a valid ELF thread_entry elf_load(uint8_t *data, process* process); //Load an ELF to a process, return entry point process* elf_exec(uint8_t *data, int privilege); //Creates a new process and a thread for running ELF file diff --git a/src/kernel/task/sched.cpp b/src/kernel/task/sched.cpp index 2629a33..d7e16d2 100644 --- a/src/kernel/task/sched.cpp +++ b/src/kernel/task/sched.cpp @@ -1,6 +1,7 @@ #include "sched.h" #include #include +#include // Lower priority numbers have high priority. Priorities must start at 0. #define PRIORITIES 3 // we have 3 priority levels diff --git a/src/user/lib/tce/syscall.c b/src/user/lib/tce/syscall.c index 4e81475..3e8d75e 100644 --- a/src/user/lib/tce/syscall.c +++ b/src/user/lib/tce/syscall.c @@ -44,8 +44,8 @@ void thread_start(void *data) { mutex_lock(&_stack_freeing_mutex); if (_stack_to_free != 0) free(_stack_to_free); _stack_to_free = tsd->stack; - asm volatile("movl %0, (_stack_freeing_mutex); int $64;" :: - "a"(SC_THREAD_EXIT), "r"(MUTEX_UNLOCKED)); + asm volatile("movl %%ebx, (_stack_freeing_mutex); int $64;" :: + "a"(SC_THREAD_EXIT), "b"(MUTEX_UNLOCKED)); } void thread_new(void (*entry)(void*), void *data) { struct thread_start_data *tsd = malloc(sizeof(struct thread_start_data)); diff --git a/src/user/test/main.c b/src/user/test/main.c index e7a6b4f..c98a619 100644 --- a/src/user/test/main.c +++ b/src/user/test/main.c @@ -41,24 +41,31 @@ void list_dir(FILE f, int lv) { file_info info; while ((r = read(f, i, 256, buf)) > 0) { + if (strcmp(buf, ".") == 0 || strcmp(buf, "..") == 0) { + i++; + continue; + } for (k = 0; k < lv; k++) printk(" "); - printk(buf); printk("\t\t"); - + printk(buf); stat_relative(f, buf, &info); - printk_hex(info.type); + if (info.type & FT_DIR) printk("/"); + printk(" \t"); + + if (info.type & FT_FILE) printk("file "); + if (info.type & FT_DIR) printk("dir "); + if (info.type & FT_SYMLINK) printk("symlink "); + if (info.type & FT_DEV) printk("dev "); + if (info.type & FT_TERMINAL) printk("term "); + if (info.type & FT_DIR) { - printk("\t"); - if (strcmp(buf, ".") != 0 && strcmp(buf, "..") != 0) { - FILE ff = open_relative(f, buf, 0); - if (ff <= 0) { - printk("error: "); printk_int(ff); printk("\n"); - } else { - printk("fd: "); printk_int(ff); printk("\n"); - list_dir(ff, lv+1); - close(ff); - } + printk(" \t"); + FILE ff = open_relative(f, buf, 0); + if (ff <= 0) { + printk("error: "); printk_int(ff); printk("\n"); } else { - printk("\n"); + printk("fd: "); printk_int(ff); printk("\n"); + list_dir(ff, lv+1); + close(ff); } } else { printk("\n"); @@ -67,6 +74,10 @@ void list_dir(FILE f, int lv) { } } +void fprint(FILE f, char *s) { + write(f, 0, strlen(s), s); +} + int main() { printk("(test app) malloc(42) = "); printk_hex((uint32_t)malloc(42)); @@ -93,27 +104,35 @@ int main() { close(f); } - printk(" -> YOUR SHELL IS STARTING UP, SIR.\n"); f = open("/.ui/klog", 0); if (f <= 0) { printk(" -> Error #"); printk_int(f); printk(" - too bad. Exiting.\n"); } else { - char *s = "Hello, writing from your opened file!\n"; - write(f, 0, strlen(s), s); + fprint(f, " -> Now reading && writing from/to virtual terminal '/.ui/klog'\n"); while (1) { - char *s = " > "; - write(f, 0, strlen(s), s); + fprint(f, " > "); char buffer[256]; int l = read(f, 0, 255, buffer); buffer[l] = 0; if (buffer[l-1] == '\n') { - char *s = "You said: "; - write(f, 0, strlen(s), s); - write(f, 0, l, buffer); + buffer[l-1] = 0; l--; + + if (strcmp(buffer, "about") == 0) { + fprint(f, "Trivial/Computing Environment v0.1.0 - useless shell, first ediiton.\n"); + } else if (strcmp(buffer, "help") == 0) { + fprint(f, "Available commands: about, help, exit.\n"); + } else if (strcmp(buffer, "exit") == 0) { + fprint(f, "Exiting the shell. I think the system is pretty likely to panic.\n"); + break; + } else if (strcmp(buffer, "gotosleep") == 0) { + while (1) thread_sleep(1000); + } else { + fprint(f, "Unknown command. "); + fprint(f, buffer); + } } else { - char *s = "oops\n"; - write(f, 0, strlen(s), s); + fprint(f, " - - - oops\n"); } } } -- cgit v1.2.3