diff options
| author | Miguel <m.i@gmx.at> | 2018-09-09 19:56:46 +0200 |
|---|---|---|
| committer | Miguel <m.i@gmx.at> | 2018-09-09 19:56:46 +0200 |
| commit | 074490c63dd09fc941b1162f62af1985ee9576d3 (patch) | |
| tree | cf20201cb188c556c8deb59d0eb6f5d145b04b72 /kernel | |
| parent | 4cda542d863839c5b0e026ccee297ca5ff3dd9cd (diff) | |
never ending cleanup
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/exceptions.c | 103 | ||||
| -rw-r--r-- | kernel/exceptions.h | 2 | ||||
| -rw-r--r-- | kernel/interrupts.c | 129 | ||||
| -rw-r--r-- | kernel/interrupts.h | 1 | ||||
| -rw-r--r-- | kernel/kernel.c | 73 | ||||
| -rw-r--r-- | kernel/mem.c | 3 | ||||
| -rw-r--r-- | kernel/mp.c | 178 | ||||
| -rw-r--r-- | kernel/mp.h | 2 |
8 files changed, 158 insertions, 333 deletions
diff --git a/kernel/exceptions.c b/kernel/exceptions.c new file mode 100644 index 0000000..3030b9c --- /dev/null +++ b/kernel/exceptions.c @@ -0,0 +1,103 @@ +#include "kernel.h" +#include "exceptions.h" + +#include "asm_x86.h" +#include "smp.h" + +static void defklog(uint32_t esp) +{ + klog("EXCEPTION: eip: 0x%08X",*(uint32_t *)esp); + esp+=4; + klog("EXCEPTION: segment: 0x%08X",*(uint16_t *)esp); + esp+=4; + klog("EXCPETION: eflags: 0x%08X",*(uint32_t *)esp); +} + +static void show_selector_error(uint32_t err) +{ + klog("Selector Error Details:"); + klog("External Event: %x",err&0b1); + klog("Location: 0x%x (0-GDT/1-IDT/2-LDT/3-IDT)",err&0b110); + klog("Selector: 0x%x",err&0b1111111111111000); +} + +static void show_page_fault_error(uint32_t error_code) +{ + klog("Page Fault Error Details:"); + klog("error_code_P (Present): %d",error_code&1?1:0); + klog("error_code_W/R (Write): %d",error_code&2?1:0); + klog("error_code_U/S (User): %d",error_code&4?1:0); + klog("error_code_RSVD (Reserved Write) : %d",error_code&8?1:0); + klog("error_code_I/D (Instruction Fetch): %d",error_code&16?1:0); + klog("at addr: 0x%08X",x86_get_cr(2)); +} + +void exception_handle(uint32_t esp, uint32_t irq) +{ + uint32_t error_code=0; + + klog("EXCEPTION: apicID: 0x%08X",apicID()); + klog("EXCEPTION: vector nr.: %d",irq); + + switch(irq){ //this interrupts push also an error_code + case 8: + case 10: + case 11: + case 12: + case 13: + case 14: + case 17: + case 30: + error_code = *(uint32_t *)esp; + esp+=4; + } + klog("EXCEPTION: error_code: %d",irq); + defklog(esp); + + switch(irq){ + case 0: + kpanic("Divide by 0"); + case 1: + kpanic("Single step (debugger)"); + case 2: + kpanic("Non Maskable Interrupt"); + case 3: + kpanic("Breakpoint (debugger)"); + case 4: + kpanic("Overflow"); + case 5: + kpanic("Bounds check"); + case 6: + kpanic("Undefined OP Code"); + case 7: + kpanic("No coprocessor"); + case 8: + kpanic("Double Fault"); + case 9: + kpanic("Coprocessor Segment Overrun"); + case 10: + show_selector_error(error_code); + kpanic("Invalid TSS"); + case 11: + show_selector_error(error_code); + kpanic("Segment Not Present"); + case 12: + show_selector_error(error_code); + kpanic("Stack Segment Overrun"); + case 13: + show_selector_error(error_code); + kpanic("Exception: Fault: General Protection Fault"); + case 14: + show_page_fault_error(error_code); + kpanic("Exception: Fault: Page Fault"); + case 15: + kpanic("RESERVED"); + case 16: + kpanic("Coprocessor error"); + case 17: + kpanic("Alignment Check"); + case 18: + kpanic("Machine Check"); + } +} + diff --git a/kernel/exceptions.h b/kernel/exceptions.h new file mode 100644 index 0000000..cfc5141 --- /dev/null +++ b/kernel/exceptions.h @@ -0,0 +1,2 @@ +#include <stdint.h> +void exception_handler(uint32_t esp, uint32_t irq); diff --git a/kernel/interrupts.c b/kernel/interrupts.c index 897c312..3ab0dd3 100644 --- a/kernel/interrupts.c +++ b/kernel/interrupts.c @@ -41,23 +41,12 @@ static void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr) idt[irq].sel=sel; } -/** Installs the interrupt table */ -void interrupts_install() -{ - idtd.size=sizeof(struct int_desc)*INT_MAX; - uint32_t addr=(uint32_t)&idt[0]; - idtd.baseHi=addr>>16; - idtd.baseLo=0xffff&addr; - __asm__("lidt %0"::"m" (idtd)); -} - /* * Interrupt dispatcher * * Remeber that we are inside an interrupt here! * */ - uint32_t interrupt_handler(uint32_t esp, uint32_t irq) { // DO NOT WRITE INSIDE INTERRUPTS!! COZ IT ACQUIRES LOCK AND WE WILL DEADLOCK @@ -101,111 +90,9 @@ uint32_t interrupt_handler(uint32_t esp, uint32_t irq) return esp; } -// log helpers // -void errlog(uint32_t error_code) -{ - klog("error_code: 0x%08X",error_code); -} - -void defklog(uint32_t esp) -{ - klog("EXCEPTION: eip: 0x%08X",*(uint32_t *)esp); - esp+=4; - klog("EXCEPTION: segment: 0x%08X",*(uint16_t *)esp); - esp+=4; - klog("EXCPETION: eflags: 0x%08X",*(uint32_t *)esp); -} - -void show_selector_error(uint32_t err) -{ - klog("Selector Error Details:"); - klog("External Event: %x",err&0b1); - klog("Location: 0x%x (0-GDT/1-IDT/2-LDT/3-IDT)",err&0b110); - klog("Selector: 0x%x",err&0b1111111111111000); -} - -void show_page_fault_error(uint32_t error_code) -{ - klog("Page Fault Error Details:"); - klog("error_code_P (Present): %d",error_code&1?1:0); - klog("error_code_W/R (Write): %d",error_code&2?1:0); - klog("error_code_U/S (User): %d",error_code&4?1:0); - klog("error_code_RSVD (Reserved Write) : %d",error_code&8?1:0); - klog("error_code_I/D (Instruction Fetch): %d",error_code&16?1:0); - klog("at addr: 0x%08X",x86_get_cr(2)); -} - -void exception_handle(uint32_t esp, uint32_t irq) -{ - uint32_t error_code=0; - - klog("EXCEPTION: apicID: 0x%08X",apicID()); - klog("EXCEPTION: vector nr.: %d",irq); - - switch(irq){ //this interrupts push also an error_code - case 8: - case 10: - case 11: - case 12: - case 13: - case 14: - case 17: - case 30: - error_code = *(uint32_t *)esp; - esp+=4; - } - klog("EXCEPTION: error_code: %d",irq); - defklog(esp); - - switch(irq){ - case 0: - kpanic("Divide by 0"); - case 1: - kpanic("Single step (debugger)"); - case 2: - kpanic("Non Maskable Interrupt"); - case 3: - kpanic("Breakpoint (debugger)"); - case 4: - kpanic("Overflow"); - case 5: - kpanic("Bounds check"); - case 6: - kpanic("Undefined OP Code"); - case 7: - kpanic("No coprocessor"); - case 8: - kpanic("Double Fault"); - case 9: - kpanic("Coprocessor Segment Overrun"); - case 10: - show_selector_error(error_code); - kpanic("Invalid TSS"); - case 11: - show_selector_error(error_code); - kpanic("Segment Not Present"); - case 12: - show_selector_error(error_code); - kpanic("Stack Segment Overrun"); - case 13: - show_selector_error(error_code); - kpanic("Exception: Fault: General Protection Fault"); - case 14: - show_page_fault_error(error_code); - kpanic("Exception: Fault: Page Fault"); - case 15: - kpanic("RESERVED"); - case 16: - kpanic("Coprocessor error"); - case 17: - kpanic("Alignment Check"); - case 18: - kpanic("Machine Check"); - } -} - - -// set default handler for all interrupts for a start +/** + * init interrupt descriptor table + */ void interrupts_init(uint16_t sel) { klog("initializing. IDT: 0x%08x, IDTD: 0x%08X",&idt,&idtd); @@ -256,3 +143,13 @@ void interrupts_init(uint16_t sel) int_install_ir(0x81, 0b10001110, 0x08,&int129); } + +/** Installs the interrupt table */ +void interrupts_install() +{ + idtd.size=sizeof(struct int_desc)*INT_MAX; + uint32_t addr=(uint32_t)&idt[0]; + idtd.baseHi=addr>>16; + idtd.baseLo=0xffff&addr; + __asm__("lidt %0"::"m" (idtd)); +} diff --git a/kernel/interrupts.h b/kernel/interrupts.h index b7f2760..3d28dc4 100644 --- a/kernel/interrupts.h +++ b/kernel/interrupts.h @@ -56,6 +56,5 @@ void interrupts_init(uint16_t sel); void interrupts_install(); uint32_t interrupt_handler(uint32_t esp, uint32_t irq); -void exception_handler(uint32_t esp, uint32_t irq); #endif diff --git a/kernel/kernel.c b/kernel/kernel.c index 50815d1..dfcab3f 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -1,14 +1,16 @@ #include "kernel.h" #include "serial.h" +#include "asm_pic.h" #include "multiboot.h" #include "acpi.h" +#include "gdt.h" +#include "interrupts.h" -// +#include "mem.h" +#include "vmem.h" -#include "kernel/mem.h" -#include "kernel/vmem.h" -#include "kernel/gdt.h" +//-- clean below headers --// #include "kernel/scheduler.h" #include "driver/timer.h" @@ -17,13 +19,10 @@ #include "syscalls.h" #include "fifo.h" -#include "mp.h" #include "asm_smp.h" #include "asm_x86.h" -#include "interrupts.h" #include "ringbuffer.h" #include "driver/screen.h" -#include "asm_pic.h" #include "smp.h" #include "fs/fs.h" @@ -33,8 +32,11 @@ /* F00L 0S Entry point (called directly from asm/multiboot.asm */ void kernel_main(uint32_t eax,uint32_t ebx) { + // -- COM1 -- // serial_init(); + klog("Communication Port (COM1) initialized."); + // -- PR & VERSION BANNER -- // klog("======================================"); klog("F00L- 0S / The Fool's Operating System"); klog("(C) 2018 / Michal Idziorek (m.i@gmx.at)"); @@ -42,67 +44,66 @@ void kernel_main(uint32_t eax,uint32_t ebx) klog("Version: git-commit: %s",GIT_REVISION); klog("======================================"); - fixme("Check if kernel size does not exceed memory limits!"); - - klog("Communication Port (COM1) initialized."); //delayed info + // -- DISABLE LEGACY PIC -- // + klog("Remapping & Disabling Programmable Interrupt Controller (PIC) ..."); + asm_pic_setup(); + // -- GET CONFIGS -- // klog("Read Multiboot Structures ..."); - multiboot_information *info; - info=multiboot_read(eax, ebx); + multiboot_information *cfg_multiboot; + cfg_multiboot=multiboot_read(eax, ebx); klog("Read Advanced Power Configuration Interface (ACPI) Structures ..."); - acpi_information procdata; - bool acpi_found=acpi_fill(&procdata); + acpi_information cfg_acpi; + bool acpi_found=acpi_fill(&cfg_acpi); + fixme("try to read (legacy) multiprocessor mp strucutres (see: xxx/mp.c)"); if(!acpi_found) kpanic("We Currently rely on ACPI Structures Sorry!"); - //klog("Read Multiprocessor (MP) Structures (Legacy) ... "); - //smp_processors procdata2; - //bool mp_found=mp_find(&procdata2); - + // -- GDT -- // klog("Global Descriptor Table (GDT) init ..."); gdt_init(); + // -- IVT -- // klog("Interrupt Vector Table (IVT) init ..."); interrupts_init(0x08); interrupts_install(); + fixme("register interrupt callback funcs"); - klog("Remapping & Disabling Programmable Interrupt Controller (PIC) ..."); - asm_pic_setup(); - - klog("Keyboard init ..."); - keyboard_init(0); - - klog("Mouse init ..."); - mouse_init(); - - klog("MEMORY MANAGEMENT"); - // memory management + // -- MEMORY MANAGEMENT -- // klog("Memory init ... "); - uint32_t kernel_blocks=mem_init(info); + uint32_t kernel_blocks=mem_init(cfg_multiboot); klog("Vritual Memory / Paging init ... "); - pdirectory *dir=vmem_init(kernel_blocks,(uint32_t)info->framebuffer_addr,procdata.local_apic_address,procdata.io_apic_address); - // - + pdirectory *dir=vmem_init(kernel_blocks,(uint32_t)cfg_multiboot->framebuffer_addr,cfg_acpi.local_apic_address,cfg_acpi.io_apic_address); + // -- RAM IMAGE -- // klog("Ram Filesystem init ... "); - fs_mount(info); + fs_mount(cfg_multiboot); + // -- VESA -- // klog("Video Electronics Standards Association (VESA) init ... "); // TODO check if text or fb? uint32_t addr=kballoc(1); fs_content("/binfont.bin",addr,0x100); // copy font (0x100 bytes) to memory. - vesa_init(info->vbe_control_info,info->vbe_mode_info,addr); + vesa_init(cfg_multiboot->vbe_control_info,cfg_multiboot->vbe_mode_info,addr); + // -- STDIN/STDOUT -- // klog("stdin/stdout init ..."); uint32_t sstdin = syscall_open("stdin",0,0); // stdin 0 uint32_t sstdout = syscall_open("term",0,0); // stdout 1 uint32_t sstderr = syscall_open("stderr",0,0); // stderr 2 + // -- ... -- // klog("Scheduler init ..."); scheduler_init(dir); + klog("Keyboard init ..."); + keyboard_init(0); + + klog("Mouse init ..."); + mouse_init(); + klog("Symmetric Multi Processing (SMP) start ... "); - smp_start_aps(&procdata); + smp_start_aps(&cfg_acpi); klog("Programmable Interval Timer (PIT) init ..."); uint64_t unixtime=timer_init(); diff --git a/kernel/mem.c b/kernel/mem.c index 8dfc6ea..7052e6c 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -146,6 +146,9 @@ void pmmngr_free_block (void* p) // returns index of first block outside of kerel-land uint32_t mem_init(multiboot_information *info) { + fixme("check if kernel size does not exceed memory limits!"); + fixme("move stack"); + if(info->flags&&1<<6) { klog("memory map of length %d provided by bootloader",info->mmap_length); diff --git a/kernel/mp.c b/kernel/mp.c deleted file mode 100644 index f7dcac6..0000000 --- a/kernel/mp.c +++ /dev/null @@ -1,178 +0,0 @@ -/* TEMPORARILY DISABLED - * -#include "kernel/kernel.h" - -#include <stdbool.h> - -#include "asm_x86.h" -#include "smp.h" -#include "lib/string/string.h" - -typedef struct mp_fps_struct -{ - uint32_t sig; //signature "_MP_" - uint32_t conf; //pointer to config struct - uint8_t length; //should be 1 - uint8_t version; // 1=1.1, 4=1.4 - - uint8_t checksum; - - uint8_t features1; - uint8_t features2; - - uint8_t res1; //reserved - uint8_t res2; - uint8_t res3; - -}mp_fps; - -typedef struct mp_config_struct -{ - uint32_t sig; //signature "PCMP" - uint16_t length; //base table length - uint8_t version; //revision 1=1.1 4=1.4 - uint8_t checksum; - - uint32_t oemid1; //OEM id (ascii) - uint32_t oemid2; - - uint32_t prodid1; //Product id (ascii) - uint32_t prodid2; - uint32_t prodid3; - - uint32_t oem_table; //pointer (optional) - uint16_t oem_size; //size of this table - uint16_t entries; //entry count (following the header) - uint32_t local_apic; //local apic address (same for every cpu) - uint16_t length_ext; //extended table length (optional) - uint8_t check_ext; //checksum for ext. table - -}mp_config; - -typedef struct proc_struct -{ - uint8_t type; //0=processor - uint8_t apic_id; - uint8_t apic_ver; - uint8_t cpu_bits; - uint32_t cpu_sig; - uint32_t cpu_flags; - -}proc_entry; - -//entries are sorted. (otherwise ignore. bochs!) -uint8_t *walk_mp_table(uint8_t *start_addr,smp_processors *smp) -{ - - if(*start_addr==0x0||*start_addr==0x2) - klog("entry type: %d",*start_addr); - - // that is a processor - if(*start_addr==0x00) - { - proc_entry *pro=start_addr; - klog("local apic id: %02X",pro->apic_id); - klog("cpu enabled bit: %s",pro->cpu_bits&1?"yes":"no"); - klog("bootstrap cpu bit: %s",pro->cpu_bits&2?"yes":"no"); - - // that is a enabled processor - if(pro->cpu_bits&1) - { - if(smp->processors>=SMP_MAX_PROC) - kpanic("we do not support that many processors. recompile with higher SMP_MAX_PROC."); - - smp->local_apic_id[smp->processors]=pro->apic_id; - // that is the bootstrap processor - if(pro->cpu_bits&2)smp->boot=smp->processors; - smp->processors++; - - } - - return start_addr+20; - } - - return start_addr+8; -} - -void do_mp_conf(mp_config *addr,smp_processors *procdata) -{ - char buf[]="XXXX"; - uint32_t *buf_addr=buf; - *buf_addr=addr->sig; - - klog("mp_config table addr: %08X",addr); - klog("mp_config signature: %s",buf); - klog("mp_config version: %02X",addr->version); - klog("mp_config # of entries: %d",addr->entries); - klog("mp_config local apic addr: 0x%08X",addr->local_apic); - klog("mp_config tabel length: %d",addr->length); - - uint8_t *start_addr=addr; - start_addr+=44; - - procdata->processors=0; - procdata->local_apic_address=addr->local_apic; - - - for(int i=0;i<addr->entries;i++) - { - start_addr=walk_mp_table(start_addr,procdata); - } - - - - -} -bool do_mp_fps(mp_fps *addr,smp_processors *procdata) -{ - - if(addr->length!=1)return false; - if(addr->version!=1&&addr->version!=4)return false; - - char buf[]="XXXX"; - uint32_t *buf_addr=buf; - *buf_addr=addr->sig; - - klog("signature: %s",buf); - klog("conf: %08X",addr->conf); - klog("ver: %02X",addr->version); - klog("f1: %02X",addr->features1); - - if(addr->features1!=0)kpanic("Intel default config not supported yet!"); - do_mp_conf(addr->conf,procdata); - - return true; -} - -// todo: check checksum,version etc. and narrow down search -bool mp_find(smp_processors *procdata) -{ - - klog("Looking for Mp Floating Ponter Struct..."); - uint8_t *addr=0x8000; - while((uint32_t)addr<=0xfffff) - { - if(!strcmp_l("_MP_",(char*)addr,4)) - { - // klog("Found at 0x%04X",addr); - if(do_mp_fps(addr,procdata))return true; - } - addr++; - } - - addr=0x190000-1025; - while((uint32_t)addr<=0x190000+1024) - { - if(!strcmp_l("_MP_",(char*)addr,4)) - { - // klog("Found at 0x%04X",addr); - if(do_mp_fps(addr,procdata))return true; - } - addr++; - } - - return false; - -} - -*/ diff --git a/kernel/mp.h b/kernel/mp.h deleted file mode 100644 index e6db125..0000000 --- a/kernel/mp.h +++ /dev/null @@ -1,2 +0,0 @@ -// MULTIBOOT SPEC parsing -//bool mp_find(smp_processors *procdata); |
