diff --git a/src/kernel/gen_isr_asm.sh b/src/kernel/gen_isr_asm.sh index 0329ee6..e5c0aa8 100755 --- a/src/kernel/gen_isr_asm.sh +++ b/src/kernel/gen_isr_asm.sh @@ -3,14 +3,15 @@ test -f isr.S && rm isr.S cp isr.S.base isr.S -for i ({0..255}); do - cat >> isr.S << EOF -isr$i: - push ebp - mov ebp, esp - push $i - jmp catchall -EOF +for i ({0..255}); do + echo "isr$i:" >> isr.S + echo " push ebp" >> isr.S + echo " mov ebp, esp" >> isr.S + if (( !(i == 8 || i == 17 || (i >= 10 && i <= 14) ) )); then + echo " push 0" >> isr.S + fi + echo " push $i" >> isr.S + echo " jmp catchall" >> isr.S done x="" diff --git a/src/kernel/idt.cpp b/src/kernel/idt.cpp index bfd3bd8..076b65b 100644 --- a/src/kernel/idt.cpp +++ b/src/kernel/idt.cpp @@ -21,37 +21,36 @@ void set_entry(uint8_t interrupt_number, uint16_t code_segment, void(*handler)() }; } -void page_fault(frame_struct* frame, uint32_t err_code) { +void page_fault(frame_struct* frame) { + // Clear interrupts, we don't want to perform a context switch during a page fault. + asm ("cli"); uint32_t problem_address; asm("mov %%cr2, %0" : "=a" (problem_address) :); - Global::kernel->terminal->printf("(EIP %x): Page Fault at %x\n", frame->eip, problem_address); - if (frame->cs & 3 == 0) { + Global::kernel->terminal->printf("(CS %x EIP %x): Page Fault at %x Error Code: %x Gate: %d\n", frame->cs, frame->eip, problem_address, frame->errcode, frame->gate); + if (!(frame->cs & 3)) { Global::kernel->terminal->printf("[FATAL] Kernel Page Fault!!!\n"); while (1) asm("hlt"); } else { // Print an error message. Global::kernel->terminal->printf("PID %d Terminated due to page fault!\n", Global::currentProc->PID); - // We are currently in the kernel stack for the current process so we need to load the main kernel stack in to esp. - //Global::kernel->loadPrimaryStack(); - - asm volatile ("mov %0, %%esp" ::"m"(Global::kernel->stack)); + asm volatile ("mov %0, %%esp" ::"m"(Global::kernel->globalISRStack)); + Global::kernel->PD->select(); // We can now safely delete the current process Global::kernel->destroyProcess(Global::currentProc); + + Global::currentProcValid = false; - // We want to load the kernel's page directory too. - //Global::kernel->PD->select(); - - //Global::currentProcValid = false; - asm ("int $0x20"); // Call context switch. - while (1) asm("hlt"); + // Go in to an infinite loop + asm ("sti"); + while (1) asm ("hlt"); } } void ignore_interrupt(frame_struct* frame) {} -void gpf(frame_struct* frame, uint32_t err_code) { - printf("General Protection Fault %x\n", err_code); +void gpf(frame_struct* frame) { + printf("(EIP %x) General Protection Fault %x\n", frame->eip, frame->errcode); while (1) asm("hlt"); } diff --git a/src/kernel/idt.h b/src/kernel/idt.h index 9d5f48b..761e143 100644 --- a/src/kernel/idt.h +++ b/src/kernel/idt.h @@ -16,6 +16,11 @@ struct __attribute__((packed)) frame_struct { uint32_t edx; uint32_t ecx; uint32_t eax; + + uint32_t gate; + uint32_t __ignored2; + uint32_t errcode; + uint32_t eip; uint16_t cs; uint16_t _ignored0; diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 5fedaed..c30c93e 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -16,6 +16,7 @@ Kernel::Kernel(PageDirectory* page_directory, PageMap* phys, PageMap* virt, uint void Kernel::init_kernel() { this->pid_map = new xnoe::hashtable(); + this->globalISRStack = (new uint8_t[0x8000]) + 0x8000; } Process* Kernel::createProcess(char* filename) { diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h index 3cbd618..d597748 100644 --- a/src/kernel/kernel.h +++ b/src/kernel/kernel.h @@ -10,6 +10,7 @@ class Kernel : public Process { public: uint32_t currentPID; uint32_t stack; + uint32_t globalISRStack; Terminal* terminal; xnoe::hashtable* pid_map; // Map of PIDs -> Process*s diff --git a/src/kernel/process.cpp b/src/kernel/process.cpp index a0dd0ad..7735dac 100644 --- a/src/kernel/process.cpp +++ b/src/kernel/process.cpp @@ -185,5 +185,4 @@ uint32_t Process::count_allocations(uint32_t address) { return alloc_tracker.get()->elem.alloc_count; else return 0; -} - +} \ No newline at end of file