From b86f48da7fc46934d576698bb4f16be9b2a7eaf9 Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 26 Sep 2018 23:58:14 +0200 Subject: some bugfixes --- Makefile | 9 ++++++-- driver/e1000.c | 15 +++++++------ driver/e1000.h | 1 + driver/keyboard.c | 12 +++++++++++ driver/mouse.c | 19 +++++++++++++++- driver/timer.c | 10 ++++++++- fs/mount.c | 2 +- fs/sysfs.c | 2 +- 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 +- net/arp.c | 2 +- net/icmp.c | 10 ++++----- net/ipv4.c | 6 +++--- 20 files changed, 146 insertions(+), 98 deletions(-) diff --git a/Makefile b/Makefile index e1ffbbe..56ba339 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ GIT_REVISION=$(shell git rev-parse HEAD) QEMU=/home/miguel/git/EXT/qemu-3.0.0/i386-softmmu/qemu-system-i386 +QEMU=qemu-system-i386 #use our cross compiler CC=i686-foolos-gcc @@ -21,11 +22,15 @@ AS=i686-elf-as ############ compiler flags ############ + +#https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html + CFLAGS= -CFLAGS=-DGIT_REVISION=\"$(GIT_REVISION)\" +#CFLAGS+=-fvar-tracking +CFLAGS+=-DGIT_REVISION=\"$(GIT_REVISION)\" CFLAGS+=-ffreestanding # do we need this if using own compiler? CFLAGS+=-nostdlib -CFLAGS+=-O0 +CFLAGS+=-Og CFLAGS+=-I. CFLAGS+=-I/home/miguel/temp/foolos/usr/i686-foolos/include/ CFLAGS+=-I./asm diff --git a/driver/e1000.c b/driver/e1000.c index 2f50df4..5d0cbe6 100644 --- a/driver/e1000.c +++ b/driver/e1000.c @@ -3,11 +3,12 @@ //https://github.com/torvalds/linux/blob/master/drivers/net/ethernet/intel/e1000/e1000_hw.c //https://github.com/qemu/qemu/blob/master/hw/net/e1000.c //registers etc. verified from pdf at https://pdos.csail.mit.edu/6.828/2006/readings/hardware/8254x_GBe_SDM.pdf +#include "e1000.h" +#include "interrupts.h" #include #include "log.h" -#include "e1000.h" #include "kmalloc.h" #include "netdev.h" #include "arp.h" @@ -427,8 +428,8 @@ void e1000_handleReceive() old_cur = rx_cur; rx_cur = (rx_cur + 1) % E1000_NUM_RX_DESC; writeCommand(REG_RDT, old_cur ); - klog("RDT %d",readCommand(REG_RDT)); - klog("RDH %d",readCommand(REG_RDH)); +// klog("RDT %d",readCommand(REG_RDT)); +// klog("RDH %d",readCommand(REG_RDH)); } } @@ -443,8 +444,8 @@ int e1000_sendPacket(const void * p_data, uint16_t p_len) uint8_t old_cur = tx_cur; tx_cur = (tx_cur + 1) % E1000_NUM_TX_DESC; writeCommand(REG_TDT, tx_cur); - klog("TDT %d",readCommand(REG_TDT)); - klog("TDH %d",readCommand(REG_TDH)); +// klog("TDT %d",readCommand(REG_TDT)); +// klog("TDH %d",readCommand(REG_TDH)); while(!(tx_descs[old_cur]->status & 0xff)); // TODO: seriously wait here!?!?!? return 0; } @@ -512,6 +513,7 @@ struct netdev e1000_init(uint32_t base) for(int i = 0; i < 0x80; i++)writeCommand(REG_MTA + i*4, 0); //e1000_linkup(); + interrupt_register(INTERRUPT_E1000,&e1000_interrupt); enableInterrupt(); @@ -523,7 +525,7 @@ struct netdev e1000_init(uint32_t base) return dev; } -void e1000_irq (int irq) +uint32_t e1000_interrupt (uint32_t esp) { // if ( p_interruptContext->getInteruptNumber() == pciConfigHeader->getIntLine()+IRQ0) // { @@ -555,4 +557,5 @@ void e1000_irq (int irq) */ if(status & 0x80)e1000_handleReceive(); //... + return esp; } diff --git a/driver/e1000.h b/driver/e1000.h index 27a052e..7eee572 100644 --- a/driver/e1000.h +++ b/driver/e1000.h @@ -5,3 +5,4 @@ int e1000_sendPacket(const void * p_data, uint16_t p_len); void e1000_irq (int irq); void e1000_linkup(); void e1000_linkdown(); +uint32_t e1000_interrupt (uint32_t esp); diff --git a/driver/keyboard.c b/driver/keyboard.c index b9a1dad..f667396 100644 --- a/driver/keyboard.c +++ b/driver/keyboard.c @@ -4,11 +4,14 @@ #include "log.h" #include "e1000.h" #include "kmalloc.h" +#include "interrupts.h" #include #include "inet.h" +ringbuffer kb_in; + static bool ctrl_l=false; static bool shift_l=false; static bool shift_r=false; @@ -31,8 +34,17 @@ static void put(uint8_t c) syscall_generic(SYSCALL_WRITE,kb_stream, (char *)&c , 1, 0); } +uint32_t keyboard_interrupt(uint32_t esp) +{ + + ringbuffer_put(&kb_in,x86_inb(0x60)); + return esp; +} + void keyboard_init(uint32_t s) { + kb_in=ringbuffer_init(1);// 4096 bytes ringbuffer; + interrupt_register(INTERRUPT_KEYBOARD,&keyboard_interrupt); kb_stream=s; } diff --git a/driver/mouse.c b/driver/mouse.c index 7ec5e68..cebafed 100644 --- a/driver/mouse.c +++ b/driver/mouse.c @@ -1,6 +1,9 @@ +#include "mouse.h" + +#include "ringbuffer.h" +#include "interrupts.h" #include "kernel/kernel.h" #include "log.h" -#include "mouse.h" #include "driver/vesa.h" //http://forum.osdev.org/viewtopic.php?t=10247 @@ -17,6 +20,8 @@ volatile int16_t mouse_x; volatile int16_t mouse_y; static volatile uint8_t mouse_a; +static ringbuffer mouse_in; + uint8_t mouse_read(); void mouse_wait(uint8_t a_type) //unsigned char @@ -67,8 +72,20 @@ int8_t mouse_get_y() return mouse_y; } +uint32_t mouse_interrupt(uint32_t esp) +{ + uint8_t b=x86_inb(0x60); + ringbuffer_put(&mouse_in,b); + //klog("%d",b); + return esp; +} + void mouse_init() { + + interrupt_register(INTERRUPT_MOUSE,&mouse_interrupt); + mouse_in=ringbuffer_init(1);// 4096 bytes ringbuffer; + mouse_x=mouse_y=0; mouse_cycle=0; diff --git a/driver/timer.c b/driver/timer.c index 30a30c6..4b8f050 100644 --- a/driver/timer.c +++ b/driver/timer.c @@ -1,6 +1,7 @@ +#include "timer.h" +#include "interrupts.h" #include "kernel.h" #include "log.h" -#include "timer.h" #include "asm_x86.h" #include "asm_pit.h" @@ -148,6 +149,11 @@ static uint64_t get_rtc_time() return epoch_seconds; } +uint32_t timer_interrupt(uint32_t esp) +{ + asm_pit_tick(); + return esp; +} // PIT uint64_t timer_init() @@ -156,7 +162,9 @@ uint64_t timer_init() task_system_clock_start=epoch_time*25; // since pit ticks 25times a second asm_pit_rate_40ms(); //tick at 25hz fixme("pit rate does only seem to work occasionally.. 1/25 seconds???" ); + interrupt_register(INTERRUPT_PIT_TIMER,&timer_interrupt); return epoch_time; + } uint64_t timer_get_ms() diff --git a/fs/mount.c b/fs/mount.c index 01a90b2..92f9b8a 100644 --- a/fs/mount.c +++ b/fs/mount.c @@ -79,7 +79,7 @@ static char* get_mount_for_path(char *path,mount *mnt) mount *m=&mounts[i]; uint32_t len=check_match(path,m->path); - if(len>best_len&&len==strlen(m->path)) + if(len>best_len)//&&len==strlen(m->path)) { best=i; best_len=len; diff --git a/fs/sysfs.c b/fs/sysfs.c index de679f2..5288a93 100644 --- a/fs/sysfs.c +++ b/fs/sysfs.c @@ -9,7 +9,7 @@ #include "lib/string/string.h" -static const char* names[] = {"mem","kmalloc","mount"}; +static const char* names[] = {"/mem","/kmalloc","/mount"}; static uint32_t map[]={mem_sysfs,mem_sysfs_set, kmalloc_sysfs,NULL, 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;iopcode) { case ARP_REQUEST: - klog("Arp Reply"); +// klog("Arp Reply"); arp_reply(netdev, hdr, arphdr); break; default: diff --git a/net/icmp.c b/net/icmp.c index 20ac203..d2e4de2 100644 --- a/net/icmp.c +++ b/net/icmp.c @@ -72,16 +72,16 @@ bool icmp_incoming(struct netdev *dev,struct eth_hdr *hdr) struct ipv4_hdr *ipv4=hdr->payload; struct icmp_v4 *data=(uint32_t *)ipv4+ipv4->ihl; - klog ("icmp type=%d",data->type); - klog ("icmp code=%d",data->code); - klog ("icmp csum=0x%04x",ntohs(data->csum)); +// klog ("icmp type=%d",data->type); +// klog ("icmp code=%d",data->code); +// klog ("icmp csum=0x%04x",ntohs(data->csum)); data->csum=0; - klog("expected checksum = 0x%04X",checksum(data,ntohs(ipv4->len)-ipv4->ihl*4)); +// klog("expected checksum = 0x%04X",checksum(data,ntohs(ipv4->len)-ipv4->ihl*4)); if(data->type==ICMP_ECHO_REQUEST) // echo request { struct icmp_v4_echo *echo=data->data; - klog ("received echo request id=%d, seq=%d, data=%d ",ntohs(echo->id),ntohs(echo->seq),echo->data); + //klog ("received echo request id=%d, seq=%d, data=%d ",ntohs(echo->id),ntohs(echo->seq),echo->data); icmp_reply(dev,hdr); /// TODO watchout that this is the memory managed by the network card we are dealing with!!. fix this later. } diff --git a/net/ipv4.c b/net/ipv4.c index b090a7b..c90733d 100644 --- a/net/ipv4.c +++ b/net/ipv4.c @@ -7,10 +7,10 @@ bool ipv4_incoming(struct netdev *dev,struct eth_hdr *hdr) { struct ipv4_hdr *ipv4=hdr->payload; - klog("ipv4 incoming with checksum: 0x%04X",ntohs(ipv4->csum)); +// klog("ipv4 incoming with checksum: 0x%04X",ntohs(ipv4->csum)); ipv4->csum=0; - klog("expected checksum: 0x%04X",checksum(ipv4,ntohs(ipv4->len))); - klog("ipv4 header len=%d",ipv4->ihl); +// klog("expected checksum: 0x%04X",checksum(ipv4,ntohs(ipv4->len))); +// klog("ipv4 header len=%d",ipv4->ihl); if(ipv4->proto==IPV4_P_ICMP) { -- cgit v1.2.3