diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index f82fe63..3d374ae 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -8,9 +8,9 @@ ], "defines": [], "compilerPath": "/usr/bin/gcc", - "cStandard": "c17", - "cppStandard": "c++20", - "intelliSenseMode": "gcc-x64", + "cStandard": "c11", + "cppStandard": "c++11", + "intelliSenseMode": "linux-gcc-x64", "browse": { "path": [ "${workspaceFolder}/**", diff --git a/.vscode/compile_commands.json b/.vscode/compile_commands.json deleted file mode 100644 index d098bc2..0000000 --- a/.vscode/compile_commands.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - "directory": "/path/to/project", - "command": "gcc -c -o file1.o file1.c", - "file": "/path/to/project/file1.c" - }, - { - "directory": "/path/to/project", - "command": "g++ -c -o file2.o file2.cpp", - "file": "/path/to/project/file2.cpp" - } - ] \ No newline at end of file diff --git a/src/kernel/arch/x86/interrupt_utils.h b/src/kernel/arch/x86/interrupt_utils.h new file mode 100644 index 0000000..0a46a52 --- /dev/null +++ b/src/kernel/arch/x86/interrupt_utils.h @@ -0,0 +1,8 @@ +#ifndef INTERRUPT_UTILS_H +#define INTERRUPT_UTILS_H + +#include + +uint32_t read_interrupt_type(void); + +#endif /* INTERRUPT_UTILS_H */ diff --git a/src/kernel/arch/x86/isr.c b/src/kernel/arch/x86/isr.c index 36ea722..482ad76 100644 --- a/src/kernel/arch/x86/isr.c +++ b/src/kernel/arch/x86/isr.c @@ -1,182 +1,190 @@ -#include "isr.h" -#include "idt.h" -#include -#include - -enum -{ - TIMER_INTERRUPT = 0x20, - KEYBOARD_INTERRUPT = 0x21, - NETWORK_INTERRUPT = 0x22, - DISK_INTERRUPT = 0x23, - SERIAL_PORT_INTERRUPT = 0x24 -}; - -void dummy_isr(struct isr_regs *regs) -{ - printf("Timer interrupt occurred!\n"); -} - -// ISR table -void (*isr_table[256])(struct isr_regs *regs) = {0}; - -// Register an ISR -extern void isr_register(uint8_t num, void (*handler)(struct isr_regs *regs)) -{ - isr_table[num] = handler; -} - -// ISR handler -void isr_handler(struct idt_regs *regs) -{ - switch (regs->int_no) - { - case 0x00: - divide_error(regs); - break; - case 0x06: - invalid_opcode(); - break; - case 0x0D: - general_protection_fault(regs); - break; - case 0x0E: - page_fault(regs); - break; - case 0x08: - double_fault(); - break; - case 0x80: - system_call(regs); - break; - case 0x20: - timer(); - break; - case 0x21: - keyboard(); - break; - case 0x2F: - device(); - break; - case 0x28: - network(); - break; - case 0x2E: - disk(); - break; - case 0x24: - serial_port(); - break; - default: - printf("Unhandled interrupt: %d\n", regs->int_no); - break; - } -} - -// Exception handlers -void divide_error(struct idt_regs *regs) -{ - printf("Divide by zero error!\n"); - // Additional actions can be taken as needed -} - -void page_fault(struct idt_regs *regs) -{ - uint32_t faulting_address; - - // Read the CR2 register to get the faulting address - __asm__ volatile("mov %%cr2, %0" : "=r"(faulting_address)); - - // Print an error message with the faulting address - printf("Page fault at 0x%x, virtual address 0x%x\n", regs->eip, - faulting_address); - - // Additional actions can be taken as needed -} - -void general_protection_fault(struct idt_regs *regs) -{ - printf("General protection fault occurred!\n"); - // Additional actions can be taken as needed -} - -void double_fault() -{ - // Handle double fault exception -} - -// Interrupt handlers -void system_call(struct idt_regs *regs) -{ - // Get the system call number from the eax register - uint32_t syscall_number = regs->eax; - - // Handle different system call numbers - switch (syscall_number) - { - case 1: - // Handle open() system call - // ... - break; - case 2: - // Handle read() system call - // ... - break; - case 3: - // Handle write() system call - // ... - break; - case 4: - // Handle close() system call - // ... - break; - // Add more cases for other system calls - - default: - // Unknown system call number - printf("Unknown system call number: %d\n", syscall_number); - break; - } -} - -void timer() -{ - // Handle timer interrupt -} - -void keyboard() -{ - // Handle keyboard interrupt -} - -void device() -{ - // Determine the type of device interrupt - uint32_t interrupt_type = read_interrupt_type(); - - // Call the appropriate interrupt handler - switch (interrupt_type) - { - case TIMER_INTERRUPT: - timer(); - break; - case KEYBOARD_INTERRUPT: - keyboard(); - break; - case NETWORK_INTERRUPT: - network(); - break; - case DISK_INTERRUPT: - disk(); - break; - case SERIAL_PORT_INTERRUPT: - serial_port(); - break; - // Add more cases for other types of device interrupts - - default: - // Unknown interrupt type - printf("Unknown device interrupt type: %d\n", interrupt_type); - break; - } -} +#include "isr.h" +#include "idt.h" +#include +#include +#include +#include +#include + +#include "./interrupt_utils.h" + +void invalid_opcode(void); +void general_protection_fault(struct idt_regs *regs); +void page_fault(struct idt_regs *regs); +void system_call(struct idt_regs *regs); + +enum +{ + TIMER_INTERRUPT = 0x20, + KEYBOARD_INTERRUPT = 0x21, + NETWORK_INTERRUPT = 0x22, + DISK_INTERRUPT = 0x23, + SERIAL_PORT_INTERRUPT = 0x24 +}; + +void dummy_isr(struct isr_regs *regs) +{ + printf("Timer interrupt occurred!\n"); +} + +// Register an ISR +extern void isr_register(uint8_t num, void (*handler)(struct isr_regs *regs)) +{ + isr_table[num] = handler; +} + +// ISR handler +void isr_handler(struct idt_regs *regs) +{ + switch (regs->int_no) + { + case 0x00: + divide_error(regs); + break; + case 0x06: + invalid_opcode(); + break; + case 0x0D: + general_protection_fault(regs); + break; + case 0x0E: + page_fault(regs); + break; + case 0x08: + double_fault(); + break; + case 0x80: + system_call(regs); + break; + case 0x20: + timer(); + break; + case 0x21: + keyboard(); + break; + case 0x2F: + device(); + break; + case 0x28: + network(); + break; + case 0x2E: + disk(); + break; + case 0x24: + serial_port(); + break; + default: + printf("Unhandled interrupt: %d\n", regs->int_no); + break; + } +} + +// Exception handlers +void divide_error(struct idt_regs *regs) +{ + printf("Divide by zero error!\n"); + // Additional actions can be taken as needed +} + +void page_fault(struct idt_regs *regs) +{ + uint32_t faulting_address; + + // Read the CR2 register to get the faulting address + __asm__ volatile("mov %%cr2, %0" : "=r"(faulting_address)); + + // Print an error message with the faulting address + printf("Page fault at 0x%x, virtual address 0x%x\n", regs->eip, + faulting_address); + + // Additional actions can be taken as needed +} + +void general_protection_fault(struct idt_regs *regs) +{ + printf("General protection fault occurred!\n"); + // Additional actions can be taken as needed +} + +void double_fault() +{ + // Handle double fault exception +} + +// Interrupt handlers +void system_call(struct idt_regs *regs) +{ + // Get the system call number from the eax register + uint32_t syscall_number = regs->eax; + + // Handle different system call numbers + switch (syscall_number) + { + case 1: + // Handle open() system call + // ... + break; + case 2: + // Handle read() system call + // ... + break; + case 3: + // Handle write() system call + // ... + break; + case 4: + // Handle close() system call + // ... + break; + // Add more cases for other system calls + + default: + // Unknown system call number + printf("Unknown system call number: %d\n", syscall_number); + break; + } +} + +void timer() +{ + static int count = 0; + printf ("timer expired %d times\n", ++count); +} + +void keyboard() +{ + // Handle keyboard interrupt +} + +void device() +{ + // Determine the type of device interrupt + uint32_t interrupt_type = read_interrupt_type(); + + // Call the appropriate interrupt handler + switch (interrupt_type) + { + case TIMER_INTERRUPT: + timer(); + break; + case KEYBOARD_INTERRUPT: + keyboard(); + break; + case NETWORK_INTERRUPT: + network(); + break; + case DISK_INTERRUPT: + disk(); + break; + case SERIAL_PORT_INTERRUPT: + serial_port(); + break; + // Add more cases for other types of device interrupts + + default: + // Unknown interrupt type + printf("Unknown device interrupt type: %d\n", interrupt_type); + break; + } +}