diff --git a/apps/Makefile b/apps/Makefile index 97b138c..b763425 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -1,5 +1,5 @@ all: make -C hash - nasm -f bin test1.asm -o test1.app + nasm -f bin -l test1.lst test1.asm -o test1.app nasm -f bin test2.asm -o test2.app diff --git a/apps/test1.app b/apps/test1.app index a06f044..f530636 100644 Binary files a/apps/test1.app and b/apps/test1.app differ diff --git a/apps/test1.asm b/apps/test1.asm index 34245f7..dee5ccc 100644 --- a/apps/test1.asm +++ b/apps/test1.asm @@ -10,8 +10,11 @@ org 0x0 ; dd 0 ; reserved start: - mov eax, 1 - mov ebx, 'h' + mov ebx, astring + mov eax, 2 int 0x30 jmp start ret + +astring: + db "Hello there from test1!", 10, 0 diff --git a/apps/test1.lst b/apps/test1.lst new file mode 100644 index 0000000..6fe7001 --- /dev/null +++ b/apps/test1.lst @@ -0,0 +1,22 @@ + 1 ; test app 1 + 2 + 3 [bits 32] + 4 org 0x0 + 5 + 6 ;header: + 7 ; dd 0x4D534F48 ; magic identifier "HOSM" + 8 ; dd 2 ; test app + 9 ; dd start ; start address + 10 ; dd 0 ; reserved + 11 + 12 start: + 13 00000000 BB[0F000000] mov ebx, astring + 14 00000005 B802000000 mov eax, 2 + 15 0000000A CD30 int 0x30 + 16 0000000C EBF2 jmp start + 17 0000000E C3 ret + 18 + 19 astring: + 20 0000000F 48656C6C6F20746865- db "Hello there from test1!", 10, 0 + 21 00000018 72652066726F6D2074- + 22 00000021 65737431210A00 diff --git a/initrd/bin/init b/initrd/bin/init index a06f044..f530636 100644 Binary files a/initrd/bin/init and b/initrd/bin/init differ diff --git a/kernel/Makefile b/kernel/Makefile index 78110af..6074b82 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -22,7 +22,8 @@ LDFLAGS=-nodefaultlibs -nostdlib --no-demangle -T link.ld OBJS=boot.o lang/lang_a.o \ kernel.o mm/mm.o mm/vmm.o lang/conv.o display/kout.o \ display/display.o sys/pic.o char/keyboard.o lang/lang.o \ - sys/pci_classes.o proc/proc.o proc/hash.o \ + sys/pci_classes.o syscall.o \ + proc/proc.o proc/hash.o \ lang/string.o lang/new.o char/misc_char.o char/vconsole.o \ devices.o block/ramdisk.o fs/vfs.o fs/FileSystem.o fs/VFSMount.o \ fs/ext2/ext2.o fs/sysfs/sysfs.o fs/sysfs/sysfs_entry.o \ @@ -31,7 +32,7 @@ OBJS=boot.o lang/lang_a.o \ fs/ext2/Ext2BlockCache.o CSRC=kernel.c mm/mm.c mm/vmm.c lang/conv.c display/kout.c \ display/display.c sys/pic.c char/keyboard.c lang/lang.c \ - sys/pci_classes.c + sys/pci_classes.c syscall.c CXXSRC=lang/string.cpp lang/new.cpp char/misc_char.cpp char/vconsole.cpp \ block/ramdisk.cpp devices.cpp fs/vfs.cpp fs/FileSystem.o fs/VFSMount.o\ fs/ext2/ext2.cpp fs/sysfs/sysfs.cpp fs/sysfs/sysfs_entry.cpp \ @@ -95,13 +96,12 @@ block/ramdisk.o: lang/lang.h block/ramdisk.h devices.h devices.o: hos_defines.h devices.h char/misc_char.h char/misc_char.h devices.o: char/vconsole.h block/ramdisk.h fs/vfs.o: hos_defines.h display/kout.h functions.h sys/io.h lang/lang.h -fs/vfs.o: fs/vfs.h devices.h fs/FileSystem.h fs/OpenDirectory.h fs/OpenFile.h -fs/vfs.o: lang/string.h fs/VFSMount.h fs/FileSystem.h fs/ext2/ext2.h fs/vfs.h +fs/vfs.o: fs/vfs.h devices.h fs/FileSystem.h fs/OpenDirectory.h lang/string.h +fs/vfs.o: fs/OpenFile.h fs/VFSMount.h fs/FileSystem.h fs/ext2/ext2.h fs/vfs.h fs/vfs.o: lang/vector.h fs/ext2/ext2.o: display/kout.h hos_defines.h mm/vmm.h multiboot.h lang/lang.h fs/ext2/ext2.o: fs/ext2/ext2.h fs/vfs.h devices.h fs/ext2/Ext2OpenDirectory.h -fs/ext2/ext2.o: fs/OpenDirectory.h fs/vfs.h fs/OpenFile.h lang/string.h -fs/ext2/ext2.o: fs/ext2/Ext2OpenFile.h fs/OpenFile.h +fs/ext2/ext2.o: fs/ext2/Ext2BlockCache.h fs/ext2/Ext2OpenFile.h fs/OpenFile.h fs/sysfs/sysfs.o: display/kout.h hos_defines.h fs/vfs.h devices.h fs/sysfs/sysfs.o: fs/sysfs/sysfs.h fs/sysfs/sysfs_entry.h lang/vector.h fs/sysfs/sysfs.o: lang/string.h @@ -115,8 +115,14 @@ proc/hash.o: hos_defines.h proc/hash.h lang/vector.h display/kout.h mm/vmm.h proc/hash.o: multiboot.h fs/OpenFile.o: fs/OpenFile.h hos_defines.h fs/OpenDirectory.o: fs/vfs.h hos_defines.h devices.h fs/OpenDirectory.h -fs/OpenDirectory.o: fs/OpenFile.h lang/string.h -fs/ext2/Ext2OpenDirectory.o: display/kout.h hos_defines.h lang/lang.h -fs/ext2/Ext2OpenDirectory.o: fs/vfs.h devices.h fs/ext2/Ext2OpenDirectory.h -fs/ext2/Ext2OpenDirectory.o: fs/OpenDirectory.h fs/vfs.h fs/OpenFile.h -fs/ext2/Ext2OpenDirectory.o: lang/string.h fs/ext2/ext2.h +fs/OpenDirectory.o: lang/string.h +fs/ext2/Ext2OpenDirectory.o: lang/lang.h hos_defines.h fs/vfs.h devices.h +fs/ext2/Ext2OpenDirectory.o: fs/ext2/Ext2OpenDirectory.h fs/ext2/ext2.h +fs/ext2/Ext2OpenDirectory.o: fs/ext2/Ext2BlockCache.h fs/ext2/Ext2OpenFile.h +fs/ext2/Ext2OpenDirectory.o: fs/OpenFile.h +fs/ext2/Ext2OpenFile.o: fs/ext2/Ext2OpenFile.h fs/ext2/ext2.h fs/vfs.h +fs/ext2/Ext2OpenFile.o: hos_defines.h devices.h fs/OpenFile.h +fs/ext2/Ext2OpenFile.o: fs/ext2/Ext2BlockCache.h lang/lang.h functions.h +fs/ext2/Ext2OpenFile.o: sys/io.h +fs/ext2/Ext2BlockCache.o: fs/vfs.h hos_defines.h devices.h +fs/ext2/Ext2BlockCache.o: fs/ext2/Ext2BlockCache.h fs/ext2/ext2.h diff --git a/kernel/Makefile.bak b/kernel/Makefile.bak index 55f81ef..75c9309 100644 --- a/kernel/Makefile.bak +++ b/kernel/Makefile.bak @@ -22,7 +22,8 @@ LDFLAGS=-nodefaultlibs -nostdlib --no-demangle -T link.ld OBJS=boot.o lang/lang_a.o \ kernel.o mm/mm.o mm/vmm.o lang/conv.o display/kout.o \ display/display.o sys/pic.o char/keyboard.o lang/lang.o \ - sys/pci_classes.o proc/proc.o proc/hash.o \ + sys/pci_classes.o syscall.o \ + proc/proc.o proc/hash.o \ lang/string.o lang/new.o char/misc_char.o char/vconsole.o \ devices.o block/ramdisk.o fs/vfs.o fs/FileSystem.o fs/VFSMount.o \ fs/ext2/ext2.o fs/sysfs/sysfs.o fs/sysfs/sysfs_entry.o \ @@ -31,7 +32,7 @@ OBJS=boot.o lang/lang_a.o \ fs/ext2/Ext2BlockCache.o CSRC=kernel.c mm/mm.c mm/vmm.c lang/conv.c display/kout.c \ display/display.c sys/pic.c char/keyboard.c lang/lang.c \ - sys/pci_classes.c + sys/pci_classes.c syscall.c CXXSRC=lang/string.cpp lang/new.cpp char/misc_char.cpp char/vconsole.cpp \ block/ramdisk.cpp devices.cpp fs/vfs.cpp fs/FileSystem.o fs/VFSMount.o\ fs/ext2/ext2.cpp fs/sysfs/sysfs.cpp fs/sysfs/sysfs_entry.cpp \ @@ -95,15 +96,16 @@ block/ramdisk.o: lang/lang.h block/ramdisk.h devices.h devices.o: hos_defines.h devices.h char/misc_char.h char/misc_char.h devices.o: char/vconsole.h block/ramdisk.h fs/vfs.o: hos_defines.h display/kout.h functions.h sys/io.h lang/lang.h -fs/vfs.o: fs/vfs.h devices.h lang/string.h lang/vector.h fs/FileSystem.h -fs/vfs.o: fs/OpenFile.h fs/OpenDirectory.h fs/VFSMount.h fs/FileSystem.h -fs/vfs.o: fs/vfs.h fs/ext2/ext2.h +fs/vfs.o: fs/vfs.h devices.h fs/FileSystem.h fs/OpenDirectory.h fs/OpenFile.h +fs/vfs.o: lang/string.h fs/VFSMount.h fs/FileSystem.h fs/ext2/ext2.h fs/vfs.h +fs/vfs.o: lang/vector.h fs/ext2/ext2.o: display/kout.h hos_defines.h mm/vmm.h multiboot.h lang/lang.h -fs/ext2/ext2.o: fs/ext2/ext2.h fs/vfs.h devices.h +fs/ext2/ext2.o: fs/ext2/ext2.h fs/vfs.h devices.h fs/ext2/Ext2OpenDirectory.h +fs/ext2/ext2.o: fs/OpenDirectory.h fs/vfs.h fs/OpenFile.h lang/string.h +fs/ext2/ext2.o: fs/ext2/Ext2OpenFile.h fs/OpenFile.h fs/sysfs/sysfs.o: display/kout.h hos_defines.h fs/vfs.h devices.h -fs/sysfs/sysfs.o: fs/sysfs/sysfs.h fs/FileSystem.h fs/OpenFile.h -fs/sysfs/sysfs.o: fs/OpenDirectory.h fs/vfs.h lang/string.h lang/vector.h -fs/sysfs/sysfs.o: fs/VFSMount.h fs/FileSystem.h fs/sysfs/sysfs_entry.h +fs/sysfs/sysfs.o: fs/sysfs/sysfs.h fs/sysfs/sysfs_entry.h lang/vector.h +fs/sysfs/sysfs.o: lang/string.h fs/sysfs/sysfs_entry.o: fs/sysfs/sysfs_entry.h lang/vector.h hos_defines.h fs/sysfs/sysfs_entry.o: lang/string.h sys/pci.o: hos_defines.h display/kout.h sys/io.h sys/pci.h lang/vector.h @@ -113,6 +115,9 @@ proc/proc.o: lang/vector.h proc/hash.o: hos_defines.h proc/hash.h lang/vector.h display/kout.h mm/vmm.h proc/hash.o: multiboot.h fs/OpenFile.o: fs/OpenFile.h hos_defines.h -fs/OpenDirectory.o: fs/OpenDirectory.h fs/vfs.h hos_defines.h devices.h -fs/OpenDirectory.o: lang/string.h lang/vector.h fs/FileSystem.h fs/OpenFile.h -fs/OpenDirectory.o: fs/VFSMount.h fs/FileSystem.h fs/vfs.h +fs/OpenDirectory.o: fs/vfs.h hos_defines.h devices.h fs/OpenDirectory.h +fs/OpenDirectory.o: fs/OpenFile.h lang/string.h +fs/ext2/Ext2OpenDirectory.o: display/kout.h hos_defines.h lang/lang.h +fs/ext2/Ext2OpenDirectory.o: fs/vfs.h devices.h fs/ext2/Ext2OpenDirectory.h +fs/ext2/Ext2OpenDirectory.o: fs/OpenDirectory.h fs/vfs.h fs/OpenFile.h +fs/ext2/Ext2OpenDirectory.o: lang/string.h fs/ext2/ext2.h diff --git a/kernel/idt.inc b/kernel/idt.inc index 22f0b25..745799f 100644 --- a/kernel/idt.inc +++ b/kernel/idt.inc @@ -1,7 +1,7 @@ ;idt.inc ;Author: Josh Holtrop ;Date: 10/30/03 -;Modified: 07/06/04 +;Modified: 01/02/06 idtr: dw 50*8-1 ;size of idt @@ -67,6 +67,23 @@ isr_label 48 isr_label 49 isr_main: + ; ok, here, we need to duplicate the top item on the stack + ; (the old eax) if the interrupt number is not 8 or 10-14 + ; this is to properly align the stack for both exceptions + ; having and not having error codes + cmp eax, 8 + jz isr_main_nodup ; if int=8, no dup + cmp eax, 10 + jb isr_main_dup ; if int<10, dup + cmp eax, 14 + jbe isr_main_nodup ; if int<=14, no dup +isr_main_dup: ; else dup + sub esp, 4 + push eax + mov eax, [esp+8] + mov [esp+4], eax + pop eax +isr_main_nodup: push ebx push ecx push edx @@ -93,6 +110,7 @@ isr_main: pop edx pop ecx pop ebx + add esp, 4 ;bypass error code pop eax ;original saved eax iret diff --git a/kernel/kernel.c b/kernel/kernel.c index 18f9eaf..19a101a 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -24,6 +24,7 @@ #include "fs/ext2/ext2.h" #include "sys/pci.h" #include "proc/proc.h" +#include "syscall.h" mb_info_t mb_info_block; mb_mmap_t mb_mmap[MAX_MMAP]; @@ -38,6 +39,7 @@ u32_t timer; // number of IRQ 0's extern u32_t mm_freepages; extern u32_t proc_new_esp; +extern u32_t cur_task; /* This function runs in segmented memory - 0xC000_0000 is mapped to 0x0 but 0x0 itself is an invalid linear address. Therefore, the multiboot information addresses @@ -151,35 +153,6 @@ void k_init() } } - char *buffer = kmalloc(2000); - void *ulab; - vfs_stat_t ulabstat; - - vfs_stat("/ulab", &ulabstat); - ulab = vfs_open_file("/ulab", VFS_MODE_READ); - vfs_read_file_block(ulab, buffer, ulabstat.size); - for (i = 0; i < ulabstat.size; i++) - putc(buffer[i]); - kprintf("\n"); - vfs_close_file(ulab); - - ulab = vfs_open_file("/ulab", VFS_MODE_WRITE | VFS_MODE_APPEND); - vfs_write_file_block(ulab, "String!!!", 9); - vfs_write_file(ulab, 'H'); - vfs_write_file_block(ulab, "String!!!", 9); - vfs_write_file(ulab, 'I'); - vfs_write_file_block(ulab, "String!!!", 9); - vfs_write_file(ulab, '!'); - vfs_write_file_block(ulab, "String!!!", 9); - vfs_close_file(ulab); - - vfs_stat("/ulab", &ulabstat); - ulab = vfs_open_file("/ulab", VFS_MODE_READ); - vfs_read_file_block(ulab, buffer, ulabstat.size); - for (i = 0; i < ulabstat.size; i++) - putc(buffer[i]); - kprintf("\n"); - vfs_close_file(ulab); void *root = vfs_open_dir("///"); char name[VFS_MAX_PATH_LENGTH]; @@ -224,7 +197,7 @@ void k_init() kprintf("Error: Could not open directory\n"); /* Create the initial task */ -/* vfs_stat_t stat; + vfs_stat_t stat; if (!vfs_stat(HOS_INIT_TASK, &stat)) { if (stat.permissions & @@ -237,7 +210,7 @@ void k_init() create_task(buf, stat.size, 0, 0); } } -*/ + criticalCounter--; } @@ -246,6 +219,9 @@ void isr(u32_t num, int_stack_t *int_stack) criticalCounter++; switch (num) { + case 0x0: /* divide by zero */ + kprintf("divide by zero, current process: %u\n", cur_task); + break; case 0x20: // timer timer++; (*(u16_t *)CONSOLE_MEMORY)++; @@ -257,9 +233,29 @@ void isr(u32_t num, int_stack_t *int_stack) pic_eoi(); break; case 0x30: - if (int_stack->eax == 1) - putc(int_stack->ebx); + syscall(cur_task, int_stack); break; + case 0x0D: /* general protection (error code) */ + kprintf("General protection fault process %u, error #%u\n", cur_task, int_stack->error); + break; + case 0x0E: /* Page fault (error code) */ + kprintf("Page fault process %u, error #%u\n", cur_task, int_stack->error); + break; + case 0x01: /* debug exception */ + case 0x02: /* non-maskable interrupt */ + case 0x03: /* breakpoint */ + case 0x04: /* overflow */ + case 0x05: /* bound exception */ + case 0x06: /* invalid opcode */ + case 0x07: /* FPU not available */ + case 0x08: /* Double fault (error code) */ + case 0x09: /* coprocessor segment overrun */ + case 0x0A: /* invalid TSS (error code) */ + case 0x0B: /* segment not present (error code) */ + case 0x0C: /* stack exception (error code) */ + case 0x10: /* floating point error */ + case 0x11: /* alignment check */ + case 0x12: /* machine check */ default: kprintf("Unhandled interrupt #%d, CR2 = 0x%x, int_stack at 0x%x!\n", num, read_cr2(), int_stack); halt(); diff --git a/kernel/kernel.h b/kernel/kernel.h index 3426f55..eacc302 100644 --- a/kernel/kernel.h +++ b/kernel/kernel.h @@ -32,11 +32,12 @@ typedef struct u32_t ecx; u32_t ebx; u32_t eax; - u32_t eip; + u32_t error; /* valid on exceptions 8, 10-14 */ + u32_t eip; u32_t cs; u32_t eflags; - u32_t esp; /* present if ring3->ring0 transition ?? */ + u32_t esp; /* present if privilege transition */ u32_t ss; } int_stack_t; diff --git a/kernel/proc/proc.cpp b/kernel/proc/proc.cpp index f19a893..e7bff65 100644 --- a/kernel/proc/proc.cpp +++ b/kernel/proc/proc.cpp @@ -22,11 +22,11 @@ extern u32_t mm_freepages; #include "proc/hash.h" extern "C" { -u32_t cur_task = 0; -u32_t n_processes = 0; +u32_t cur_task = 0; /* PID of currently executing process */ +u32_t n_processes = 0; /* total number of processes */ tss_t tss0; u32_t pid_base = 1024; -u32_t pid_index = 0; +u32_t pid_index = 0; /* index of running process in 'pids' vector */ } hash *processes; @@ -48,6 +48,11 @@ int proc_init() return 0; } +process_t *proc_get_struct(u32_t pid) +{ + return (process_t *) processes->get(pid); +} + void proc_sched(int_stack_t *int_stack) { u32_t new_pid_index = (pid_index + 1) % pids->size(); @@ -125,7 +130,7 @@ void create_address_space(process_t *p) u32_t *ptr32 = p->page_dir; for (i = 0; i < 768; i++) /* zero 3 gigs */ *ptr32++ = 0; - memcpyd(ptr32, (void *)0xFFFFFC00, 256); /* 1 gig kernel mem */ + memcpyd(ptr32, (void *)0xFFFFFC00, 256); /* 1 gig kernel mem */ } void create_process_stack(process_t *p, void *entry) @@ -146,7 +151,7 @@ void create_process_stack(process_t *p, void *entry) p->int_stack.fs = SEG_USER_DATA | 0x3; p->int_stack.gs = SEG_USER_DATA | 0x3; p->int_stack.ss = SEG_USER_DATA | 0x3; - p->int_stack.esp = 0x20000000; + p->int_stack.esp = 0x80000000; /* esp at 2GB */ p->int_stack.eflags = 0x0202; p->int_stack.cs = SEG_USER_CODE | 0x3; p->int_stack.eip = (u32_t)entry; diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index 84c2af5..df4e4c0 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -68,6 +68,8 @@ typedef struct } __attribute__((packed)) tss_t; int proc_init(); +process_t *proc_get_struct(u32_t pid); + void proc_sched(int_stack_t *int_stack); u32_t get_pid(); void switch_task(int_stack_t *int_stack, u32_t new_task); diff --git a/kernel/syscall.c b/kernel/syscall.c new file mode 100644 index 0000000..4002755 --- /dev/null +++ b/kernel/syscall.c @@ -0,0 +1,25 @@ +// syscall.c +// Author: Josh Holtrop +// Date; 01/02/06 +// Modified: 01/02/06 + +#include "syscall.h" +#include "display/kout.h" +#include "proc/proc.h" + +void syscall(u32_t cur_task, int_stack_t *int_stack) +{ + static u32_t times = 0; + process_t *proc = proc_get_struct(cur_task); + switch (int_stack->eax) + { + case 1: + times++; + kprintf("\033[sTimes Called: %d\033[u", times); + break; + case 2: + kprintf("%s", int_stack->ebx); + break; + } +} + diff --git a/kernel/syscall.h b/kernel/syscall.h new file mode 100644 index 0000000..08171a0 --- /dev/null +++ b/kernel/syscall.h @@ -0,0 +1,14 @@ +// syscall.h +// Author: Josh Holtrop +// Date; 01/02/06 +// Modified: 01/02/06 + +#ifndef __HOS_SYSCALL_H__ +#define __HOS_SYSCALL_H__ __HOS_SYSCALL_H__ + +#include "hos_defines.h" +#include "kernel.h" + +void syscall(u32_t cur_task, int_stack_t *int_stack); + +#endif