From b86f48da7fc46934d576698bb4f16be9b2a7eaf9 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 26 Sep 2018 23:58:14 +0200 Subject: some bugfixes --- kernel/apic.c | 6 +++--- kernel/interrupts.c | 62 +++++++++++++++++++++-------------------------------- kernel/interrupts.h | 11 ++++++++-- kernel/kernel.c | 16 ++++++-------- kernel/kernel.h | 4 ++-- kernel/kmalloc.c | 43 ++++++++++++++++++++++--------------- kernel/scheduler.c | 6 +++--- kernel/smp.c | 6 ++++-- kernel/syscalls.c | 2 +- 9 files changed, 79 insertions(+), 77 deletions(-) (limited to 'kernel') diff --git a/kernel/apic.c b/kernel/apic.c index 4bc2bb3..e48b19b 100644 --- a/kernel/apic.c +++ b/kernel/apic.c @@ -156,10 +156,10 @@ void ioapic_config() // ioapic_config_entry(2,0x90|0xa000,0x3<<24); // level trigger on low // CPU - ioapic_config_entry(2, INTERRUPT_PIT_TIMER, 0x0<<24); // pit +// ioapic_config_entry(2, INTERRUPT_PIT_TIMER, 0x0<<24); // pit ioapic_config_entry(1, INTERRUPT_KEYBOARD, 0x0<<24); // kb - ioapic_config_entry(12, INTERRUPT_MOUSE, 0x0<<24); // mouse - ioapic_config_entry(11, INTERRUPT_E1000|0x8000, 0x0<<24); // e1000 (level trigger on high) +// ioapic_config_entry(12, INTERRUPT_MOUSE, 0x0<<24); // mouse +// ioapic_config_entry(11, INTERRUPT_E1000|0x8000, 0x0<<24); // e1000 (level trigger on high) } /** startup other cpus*/ diff --git a/kernel/interrupts.c b/kernel/interrupts.c index b0a473a..5a788c8 100644 --- a/kernel/interrupts.c +++ b/kernel/interrupts.c @@ -13,15 +13,11 @@ #include "apic.h" #include "ringbuffer.h" -ringbuffer mouse_in; -ringbuffer kb_in; - -// - /** The size of our interrupts table */ #define INT_MAX 256 // 0-255 -static uint32_t handlers[INT_MAX]; // addresses of interrupt handlers. +/** Addresses of the registered interrupt handlers */ +static uint32_t handlers[INT_MAX]; /** The interrupt descriptor table */ struct int_desc @@ -53,7 +49,8 @@ static void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr) idt[irq].sel=sel; } -void interrupt_handler_register(uint32_t irq, uint32_t func_addr) +/** register an interrupt handler for given irq number */ +void interrupt_register(uint32_t irq, uint32_t func_addr) { if(irq<128||irq>160)kpanic("irq number out of range!"); if(handlers[irq]!=0)kpanic("handler already registered!"); @@ -62,41 +59,38 @@ void interrupt_handler_register(uint32_t irq, uint32_t func_addr) /* * Interrupt dispatcher - * * Remeber that we are inside an interrupt here! - * */ uint32_t interrupt_handler(uint32_t esp, uint32_t irq) { + uint32_t *stack; - // process irq - switch(irq) + if(handlers[irq]!=0) { - case INTERRUPT_PIT_TIMER: - asm_pit_tick(); - break; - - case INTERRUPT_KEYBOARD: - ringbuffer_put(&kb_in,x86_inb(0x60)); - break; + uint32_t (*f)(uint32_t esp)=handlers[irq]; + esp=f(esp); + apic_eoi(); + } + else if(irq!=INTERRUPT_SYSCALL&&irq!=INTERRUPT_IPI&&irq!=INTERRUPT_APIC_TIMER) + { + kpanic("unhandled interrupt %d",irq); + } - case INTERRUPT_MOUSE: - ringbuffer_put(&mouse_in,x86_inb(0x60)); - break; + // process IRQ + switch(irq) + { case INTERRUPT_SYSCALL: stack=esp; task_syscall(stack[11],stack[8],stack[10],stack[9]); //eax,ebx,ecx,edx + esp=scheduler_wake_worker(esp); break; - case INTERRUPT_E1000: - e1000_irq(INTERRUPT_E1000); - break; - - case INTERRUPT_APIC_TIMER: - case INTERRUPT_IPI: + case INTERRUPT_APIC_TIMER: // frequency is configured in smp.c + case INTERRUPT_IPI: // inter process interrupt esp=scheduler_run(esp,0); + apic_eoi(); break; case 255: // default or spurious @@ -107,29 +101,21 @@ uint32_t interrupt_handler(uint32_t esp, uint32_t irq) // reschedule to kernel worker on these if(irq==INTERRUPT_SYSCALL||irq==INTERRUPT_KEYBOARD||irq==INTERRUPT_MOUSE) { - esp=scheduler_wake_worker(esp); } - // ack all to LAPIC, except software syscalls - if(irq!=INTERRUPT_SYSCALL) - apic_eoi(); + // Ack all to LAPIC, except software syscalls return esp; + } /** - * init interrupt descriptor table + * init interrupt descriptor table (TODO; do we seriously have to hardcode this?) */ void interrupts_init() { - // TODO???? - klog("Initializing Mouse and Kb input buffers"); - fixme("use a regular pipe-file"); - kb_in=ringbuffer_init(1);// 4096 bytes ringbuffer; - mouse_in=ringbuffer_init(1);// 4096 bytes ringbuffer; klog("Initializing. IDT: 0x%08x, IDTD: 0x%08X",&idt,&idtd); - // // Default interrupt handling for(int i=0; i=BLOCKS)return BLOCKS; // out of mem uint32_t end=next_used(pos,blocks); // klog("next_used:%d",end); if(end-pos>=blocks)return pos; @@ -83,28 +79,27 @@ static void mark_free(uint32_t start,uint32_t blocks) // will be initialized on first call to kballoc() // static void kmallocinit() { - next=&(data[0]); - data_addr=next; - first=next; - if(next%4096)kpanic("kmalloc data not aligned properly."); + data_addr=data; + if(data_addr%4096)kpanic("kmalloc data not aligned properly."); - klog("In-Kernel Block Memory Allocation Initialized at: 0x%08X (free: %d x 4096KB BLOCKS)",next,BLOCKS); - init=1; + klog("In-Kernel Block Memory Allocation Initialized at: 0x%08X (free: %d x 4096KB BLOCKS)",data_addr,BLOCKS); } // kernel block memory allocation // uint32_t kballoc(uint32_t size) { + static bool init=false; if(size>255)kpanic("max supported size 255 blocks"); spinlock_spin(SPINLOCK_ALLOC); - if(!init)kmallocinit(); + if(!init){kmallocinit(); init=true;} uint32_t blk=free_cont(size); if(blk==BLOCKS)kpanic("out of mem"); mark_used(blk,size); spinlock_release(SPINLOCK_ALLOC); + klog("allocated %d blocks at 0x%08X",size,data_addr+blk*4096); return data_addr+blk*4096; } @@ -112,7 +107,7 @@ void kbfree(uint32_t pos) { uint32_t blk=(pos-data_addr)/4096; spinlock_spin(SPINLOCK_ALLOC); - klog("freeing %d blocks ad 0x%08X",map[blk],pos); + klog("freeing %d blocks at 0x%08X",map[blk],pos); mark_free(blk,map[blk]); spinlock_release(SPINLOCK_ALLOC); } @@ -121,14 +116,28 @@ void kmalloc_sysfs(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...)) { uint32_t free=0; uint32_t used=0; + + char cmap[BLOCKS]; + cmap[200]=0; + for(int i=0;i