summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/apic.c6
-rw-r--r--kernel/interrupts.c62
-rw-r--r--kernel/interrupts.h11
-rw-r--r--kernel/kernel.c16
-rw-r--r--kernel/kernel.h4
-rw-r--r--kernel/kmalloc.c43
-rw-r--r--kernel/scheduler.c6
-rw-r--r--kernel/smp.c6
-rw-r--r--kernel/syscalls.c2
9 files changed, 79 insertions, 77 deletions
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<INT_MAX; i++)
diff --git a/kernel/interrupts.h b/kernel/interrupts.h
index b3b8a93..31b0cc0 100644
--- a/kernel/interrupts.h
+++ b/kernel/interrupts.h
@@ -26,6 +26,10 @@
* -------------------
* * 0x81-0xA0
*
+ * Default
+ * -------
+ * * 0xff
+ *
* Usage
* -----
*
@@ -34,6 +38,10 @@
* interrupt_handler() and exception_handler() will be called accordingly from
* the interrupt handlers this functionality is backed by. You can find them
* in asm_int.h. The selector 0x08 is used.
+ *
+ * You can register handlers for specific interrupts via
+ * interrupt_register(). Remember that the interrupts are routed
+ * through the I/O APIC which has to be configured as well.
*/
#define INTERRUPT_SYSCALL 0x80 // can be called from user code / ring 3
@@ -46,9 +54,8 @@
#define INTERRUPT_E1000 0x93
#define INTERRUPT_APIC_TIMER 0x94
-
void interrupts_init();
void interrupts_install();
-void interrupt_handler_register(uint32_t irq, uint32_t func_addr);
+void interrupt_register(uint32_t irq, uint32_t func_addr);
#endif
diff --git a/kernel/kernel.c b/kernel/kernel.c
index e4d7eba..f926139 100644
--- a/kernel/kernel.c
+++ b/kernel/kernel.c
@@ -1,7 +1,8 @@
#include "kernel.h"
-#include "log.h"
+#include "log.h"
#include "serial.h"
+
#include "asm_pic.h"
#include "multiboot.h"
#include "acpi.h"
@@ -53,10 +54,6 @@ void kernel_main(uint32_t eax,uint32_t ebx)
klog("Version: git-commit: %s",GIT_REVISION);
klog("======================================");
- // -- UNIT TESTING -- //
- testing_kmalloc();
- testing_mount();
-
// -- DISABLE LEGACY PIC -- //
klog("Remapping & Disabling Programmable Interrupt Controller (PIC) ...");
fixme("io_wait & spurious interrupts");
@@ -65,7 +62,7 @@ void kernel_main(uint32_t eax,uint32_t ebx)
// -- GET CONFIGS -- //
klog("Read Multiboot Structures ...");
multiboot_information *cfg_multiboot;
- cfg_multiboot=multiboot_read(eax, ebx,true); // true-silent
+ cfg_multiboot=multiboot_read(eax, ebx,true); // true for silent
// elf_multiboot_read(cfg_multiboot); // just show kernel section headers
klog("Read Advanced Power Configuration Interface (ACPI) Structures ...");
@@ -84,7 +81,6 @@ void kernel_main(uint32_t eax,uint32_t ebx)
interrupts_install();
fixme("register interrupt callback funcs (instead hardcoded dispatcher)");
-
// -- PCI SCAN --/
klog("PCI init ...");
uint32_t e1000_addr=pci_init();
@@ -109,8 +105,7 @@ void kernel_main(uint32_t eax,uint32_t ebx)
klog("Mounting ... ");
ext2_dump_info(VMEM_EXT2_RAMIMAGE);
ext2_mount("/");
- sysfs_mount("/sys");
- pipe_mount("/pipes");
+ sysfs_mount("/sys/");
// -- APIC -- //
klog("Advanced Programmable Interrupt Controller (APIC) config ...");
@@ -145,6 +140,9 @@ void kernel_main(uint32_t eax,uint32_t ebx)
uint64_t unixtime=timer_init();
klog("Unix Time = %u seconds",unixtime);
+ // -- E1000 INIT (TODO: only if present!) --/
+ klog("E1000 init ...");
+ fixme("do not hardcode address and allow paging somehwere else");
e1000_init(e1000_addr);
klog("Symmetric Multi Processing (SMP) start ... ");
diff --git a/kernel/kernel.h b/kernel/kernel.h
index 07e5fda..d0c14f6 100644
--- a/kernel/kernel.h
+++ b/kernel/kernel.h
@@ -38,8 +38,8 @@ REFERENCES
#define MAX_TASKS 100
#define MEM_PRINT_MEMORYMAP
-#define KMALLOC_MEM_SIZE 1024*1024*8 // 8MB for in kernel-memory
-#define KMALLOC_BLOCK_SIZE 1024*4 // 4096 per block
+#define KMALLOC_MEM_SIZE (1024*1024*1) // 8MB for in kernel-memory
+#define KMALLOC_BLOCK_SIZE (1024*4) // 4096 per block
#define NUMBER_SPINLOCKS 16
#define SPINLOCK_LOG 0
diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c
index 061d68e..38db6fc 100644
--- a/kernel/kmalloc.c
+++ b/kernel/kmalloc.c
@@ -3,7 +3,7 @@
#include "spinlock.h"
#include "log.h"
-#define BLOCKS KMALLOC_MEM_SIZE/KMALLOC_BLOCK_SIZE
+#define BLOCKS (KMALLOC_MEM_SIZE/KMALLOC_BLOCK_SIZE)
// this is in .bss so we can assume it was zeroed!
static uint8_t data[KMALLOC_MEM_SIZE] __attribute__((aligned (4096))); // bytes
@@ -11,16 +11,12 @@ static uint8_t map[BLOCKS];
//
static uint32_t data_addr;
-static uint32_t next;
-static uint32_t first;
-static uint8_t init=0;
-
static uint32_t next_free(uint32_t start)
{
for(int i=start;i<BLOCKS;i++)
{
if(!map[i])return i;
- return next_free(i+map[i]);
+ i+=map[i]-1;
}
return BLOCKS; // out of mem
}
@@ -44,7 +40,7 @@ static uint32_t free_cont(uint32_t blocks)
{
pos=next_free(pos);
// klog("next_free:%d",pos);
- if(pos==BLOCKS)return BLOCKS; // out of mem
+ if(pos+blocks>=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<BLOCKS;i++)
{
- if(map[i]) used++;
- else free++;
+ if(map[i])
+ {
+ cmap[i]='X';
+ used++;
+ }
+ else
+ {
+ cmap[i]='_';
+ free++;
+ }
}
f(r,"kernel blocks allocation/deallocation");
f(r,"total 4096kb blocks: %d (%d bytes)",BLOCKS,BLOCKS*4096);
f(r,"used 4096kb blocks: %d (%d bytes)",used,used*4096);
f(r,"free 4096kb blocks: %d (%d bytes)",free,free*4096);
+// f(r,"%s",cmap); // TODO: watch out for buffer overflow in f
+
}
diff --git a/kernel/scheduler.c b/kernel/scheduler.c
index f6067e5..dfe1b25 100644
--- a/kernel/scheduler.c
+++ b/kernel/scheduler.c
@@ -81,7 +81,7 @@ volatile void scheduler_init(uint32_t cpu, void *dir)
task_list[cpu][0].syscall=false;
task_list[cpu][0].thread=false;
task_list[cpu][0].vmem=dir;
- task_list[cpu][0].esp = VMEM_CPU_STACK_TOP-0x200;
+ task_list[cpu][0].esp = VMEM_CPU_STACK_TOP-0x200-8;
task_list[cpu][0].esp0 = 0; // esp0 not needed by kernel space tasks
fd_init_std_streams(task_list[cpu][0].pid,0);
@@ -92,7 +92,7 @@ volatile void scheduler_init(uint32_t cpu, void *dir)
task_list[cpu][1].thread=false;
task_list[cpu][1].syscall=false;
task_list[cpu][1].vmem=dir;
- task_list[cpu][1].esp = kballoc(4)+4*4096-0x200; // 4 pages stack
+ task_list[cpu][1].esp = kballoc(4)+4*4096-0x200-8; // 4 pages stack & prealign
task_list[cpu][1].esp0 =kballoc(4)+4*4096; // esp0 not needed by kernel space tasks
fd_init_std_streams(task_list[cpu][1].pid,0);
@@ -104,7 +104,7 @@ volatile void scheduler_init(uint32_t cpu, void *dir)
task_list[cpu][2].thread=false;
task_list[cpu][2].syscall=false;
task_list[cpu][2].vmem=dir;
- task_list[cpu][2].esp = kballoc(4)+4*4096-0x200; // 4 pages stack
+ task_list[cpu][2].esp = kballoc(4)+4*4096-0x200-8; // 4 pages stack & prealign
task_list[cpu][2].esp0 =kballoc(4)+4*4096; // esp0 not needed by kernel space tasks
fd_init_std_streams(task_list[cpu][2].pid,0);
diff --git a/kernel/smp.c b/kernel/smp.c
index d9c994a..e204b32 100644
--- a/kernel/smp.c
+++ b/kernel/smp.c
@@ -16,6 +16,8 @@
#include "vesa.h"
#include "syscalls.h"
+void run_smp();
+
// set cpu private value
void smp_set(uint32_t offset, uint32_t value)
{
@@ -58,12 +60,12 @@ void smp_main()
smp_main_generic(false);
}
-static void run_smp()
+void run_smp()
{
apic_enable();
klog("Setup the LAPIC Timer on CPU with lapic_id=0x%x ...",apic_id());
- apic_init_timer(10);// freq 1HZ
+ apic_init_timer(2);// freq 2HZ
klog("Enable Interrupts on CPU with lapic_id=0x%x ...",apic_id());
diff --git a/kernel/syscalls.c b/kernel/syscalls.c
index 2952c11..d289121 100644
--- a/kernel/syscalls.c
+++ b/kernel/syscalls.c
@@ -461,5 +461,5 @@ uint32_t syscall_generic(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint
case SYSCALL_DUP2 :
return syscall_dup2(p1,p2,p3,pid);
}
- kpanic("unknown syscall");
+ kpanic("unknown syscall %d",nr);
}