From ce16fe524c14ccaae67fb634105da5aef08ead48 Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 4 Oct 2018 00:53:30 +0200 Subject: moving framebuffer access completely to userspace --- kernel/apic.c | 1 + kernel/interrupts.c | 27 ++++++++-- kernel/kernel.c | 58 +++++++++++++++----- kernel/kernel.h | 7 ++- kernel/kmalloc.c | 2 +- kernel/log.c | 14 +++-- kernel/log.h | 10 ++-- kernel/ringbuffer.c | 58 +++++++++++--------- kernel/ringbuffer.h | 2 +- kernel/scheduler.c | 151 +++++++++++++++++++++++++++------------------------- kernel/smp.c | 20 +++---- kernel/vmem.c | 11 ++-- kernel/vmem.h | 1 + 13 files changed, 218 insertions(+), 144 deletions(-) (limited to 'kernel') diff --git a/kernel/apic.c b/kernel/apic.c index 75837f7..509fd3b 100644 --- a/kernel/apic.c +++ b/kernel/apic.c @@ -71,6 +71,7 @@ static uint32_t apic_read(uint32_t offset) uint32_t apic_id() { + if(local_apic_addr==0)return 0; return apic_read(APIC_APICID)>>24; } diff --git a/kernel/interrupts.c b/kernel/interrupts.c index 49c0191..4aa9db3 100644 --- a/kernel/interrupts.c +++ b/kernel/interrupts.c @@ -64,13 +64,32 @@ void interrupt_register(uint32_t irq, uint32_t func_addr) */ uint32_t interrupt_handler(uint32_t esp, uint32_t irq) { - uint32_t *stack; - if(handlers[irq]!=0) { uint32_t (*f)(uint32_t esp)=handlers[irq]; esp=f(esp); apic_eoi(); + return esp; + } + + if(irq==INTERRUPT_APIC_TIMER) + { + klog ("tick"); + esp=scheduler_run(esp,-1); + apic_eoi(); + return esp; + } + + kpanic("unhandled interrupt %d",irq); + + + uint32_t *stack; + + if(handlers[irq]!=0) + { + //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) @@ -83,7 +102,7 @@ uint32_t interrupt_handler(uint32_t esp, uint32_t irq) { case INTERRUPT_SYSCALL: stack=esp; - task_syscall(stack[11],stack[8],stack[10],stack[9]); //eax,ebx,ecx,edx + // task_syscall(stack[11],stack[8],stack[10],stack[9]); //eax,ebx,ecx,edx break; @@ -105,7 +124,7 @@ 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) { - scheduler_wake_worker(esp); + // scheduler_wake_worker(esp); esp=scheduler_run(esp,-1); } diff --git a/kernel/kernel.c b/kernel/kernel.c index 70b1913..3e07fde 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -41,7 +41,32 @@ #include "driver/vesa.h" #include "asm_pit.h" -/* F00L 0S Entry point (called directly from asm/multiboot.asm */ +/** F00L 0S Entry point (called directly from asm/multiboot.asm) + * + * After this procedure completes we are in a well defined state. + * + * * All Processors are up and running in 32bit protected mode. + * * Interrupts are installed and enabled: + * + * - PS/2 Keyboard CPU0 + * - PS/2 Mouse CPU0 + * - PIT Timer (20Hz) (DISABLED in apic.c) CPU0 + * - APIC Timer (Frequency defined in kernel.h) ALL CPUS + * - E1000 CPU0 + * + * * Software Interrupts: + * + * - Syscalls + * - IPI + * + * * Framebuffer is in a known state. + * * Paging is enabled. + * * Each CPU runs on its own stack at VMEM_CPU_STACK + * * Each CPU has its own private page at VMEM_CPU_PRIVATE + * * We are ready to start scheduling on the next interrupt. + * + */ + void kernel_main(uint32_t eax,uint32_t ebx) { // -- COM1 -- // @@ -60,7 +85,7 @@ void kernel_main(uint32_t eax,uint32_t ebx) klog("The Kernel was loaded at: 0x%08X - 0x%08X",get_kernel_start(),get_kernel_end()); klog("0x00000000 - 0x%08X will get identity mapped", VMEM_KERNEL_PAGES*4096); if(kernel_end>=top_kernel_vmem)kpanic("kernel to big. increase VMEM_KERNEL_PAGES"); - fixme("fear overrun of the initial buffer!"); + fixme("still fear overrun of stack"); // -- DISABLE LEGACY PIC -- // klog("Remapping & Disabling Programmable Interrupt Controller (PIC) ..."); @@ -87,7 +112,6 @@ void kernel_main(uint32_t eax,uint32_t ebx) klog("Interrupt Vector Table (IVT) init ..."); interrupts_init(0x08); interrupts_install(); - fixme("register interrupt callback funcs (instead hardcoded dispatcher)"); // -- PCI SCAN --/ klog("PCI init ..."); @@ -120,14 +144,16 @@ void kernel_main(uint32_t eax,uint32_t ebx) ioapic_config(); // -- VESA -- // + /* fixme("tell terminal syscall somehow if we are vga or textmode"); klog("Video Electronics Standards Association (VESA) init ... "); // binfont has to fit in ONE ext2 block // + fixme("support binfonts spanning multiple blocks?"); uint32_t inode= ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,VESA_FONT_PATH); uint32_t addr= ext2_inode_blockstart( VMEM_EXT2_RAMIMAGE,inode,0); - vesa_init(cfg_multiboot,addr); + vesa_init(cfg_multiboot,addr); // this only sets some internal static variables klog("Compositor init ..."); compositor_init(cfg_multiboot->framebuffer_width,cfg_multiboot->framebuffer_height,cfg_multiboot->framebuffer_pitch); @@ -136,29 +162,35 @@ void kernel_main(uint32_t eax,uint32_t ebx) // -- STD STREAMS -- // klog("Standard Streams init ..."); fd_init_std_streams(0,cfg_multiboot->framebuffer_type!=2); + */ // -- KB -- // klog("Keyboard init ..."); - keyboard_init(0); + keyboard_init(); // -- MOUSE -- // klog("Mouse init ..."); mouse_init(); - // we wait till almost the end since the time will only start ticking after we - // enable interrupts - klog("Programmable Interval Timer (PIT) init ..."); - uint64_t unixtime=timer_init(); - klog("Unix Time = %u seconds",unixtime); - // -- E1000 INIT (TODO: only if present!) --/ if(e1000_addr) { + #ifndef DISABLE_E1000 klog("E1000 init ..."); - // e1000_init(e1000_addr); + e1000_init(e1000_addr); + #endif } + // we wait until the end since the time will only start ticking once + // we enable interrupts. + klog("Programmable Interval Timer (PIT) init ..."); + klog("Reading CMOS Clock ..."); + uint64_t unixtime=timer_init(); + klog("Unix Time = %u seconds",unixtime); + klog("Symmetric Multi Processing (SMP) start ... "); // for(int i=1;i #include "spinlock.h" +#include "smp.h" +#include "apic.h" #include "kernel/kernel.h" #include "driver/serial.h" @@ -23,8 +25,10 @@ static void log_string(char *str) } } -void log(bool color,char *module_name, int prio, char *format_string, ...) +void log(char *module_name, int prio, char *format_string, ...) { + uint32_t cpu=apic_id(); + #ifdef FOOLOS_LOG_OFF return; #endif @@ -44,10 +48,10 @@ void log(bool color,char *module_name, int prio, char *format_string, ...) tfp_vsprintf(buf_info,format_string,va); va_end(va); - if(color) tfp_sprintf(buf_log,"\033[36;40m%s\033[31;40m %s:\033[37;40m %s\n",buf_time,module_name,buf_info); - else tfp_sprintf(buf_log,"%s %s: %s\n",buf_time,module_name,buf_info); + tfp_sprintf(buf_log,"\033[36;40m%s\033[33;40mCPU %02d:\033[31;40m%s:\033[37;40m %s\n",buf_time,cpu,module_name,buf_info); +// tfp_sprintf(buf_log,"%sCPU %02d:%s: %s\n",buf_time,cpu,module_name,buf_info); -// spinlock_spin(SPINLOCK_LOG); + spinlock_spin(SPINLOCK_LOG); log_string(buf_log); -// spinlock_release(SPINLOCK_LOG); + spinlock_release(SPINLOCK_LOG); } diff --git a/kernel/log.h b/kernel/log.h index 4e25bef..d834efd 100644 --- a/kernel/log.h +++ b/kernel/log.h @@ -11,7 +11,7 @@ #define FOOLOS_LOG_DEBUG 2 #define FOOLOS_LOG_FINE 1 -void log(bool color,char *module_name, int prio, char *format_string, ...); +void log(char *module_name, int prio, char *format_string, ...); // __FUNCTION__ ? #ifndef FOOLOS_LOG_OFF @@ -35,16 +35,16 @@ void log(bool color,char *module_name, int prio, char *format_string, ...); #define S1(x) #x #define S2(x) S1(x) -#define klog(...) log(FOOLOS_LOG_COLOR,__FILE__ ":" S2(__LINE__), 10, LOG_LABEL_INFO __VA_ARGS__) -#define kpanic(...) {log(FOOLOS_LOG_COLOR,__FILE__ ":" S2(__LINE__) ,0, LOG_LABEL_PANIC __VA_ARGS__ ); while(1);} +#define klog(...) log(__FILE__ ":" S2(__LINE__), 10, LOG_LABEL_INFO __VA_ARGS__) +#define kpanic(...) {log(__FILE__ ":" S2(__LINE__) ,0, LOG_LABEL_PANIC __VA_ARGS__ ); while(1);} #ifdef HIDE_FIXME #define fixme(...) {} #else -#define fixme(...) log(FOOLOS_LOG_COLOR,__FILE__ ":" S2(__LINE__) , 10, LOG_LABEL_FIX __VA_ARGS__) +#define fixme(...) log(__FILE__ ":" S2(__LINE__) , 10, LOG_LABEL_FIX __VA_ARGS__) #endif -#define testlog(...) log(FOOLOS_LOG_COLOR,__FILE__ ":" S2(__LINE__) , 10,LOG_LABEL_TEST __VA_ARGS__) +#define testlog(...) log(__FILE__ ":" S2(__LINE__) , 10,LOG_LABEL_TEST __VA_ARGS__) #endif diff --git a/kernel/ringbuffer.c b/kernel/ringbuffer.c index 43d0e33..3886340 100644 --- a/kernel/ringbuffer.c +++ b/kernel/ringbuffer.c @@ -16,48 +16,58 @@ void ringbuffer_free(ringbuffer *f) kbfree(f->data); } -bool ringbuffer_put(ringbuffer* f,uint8_t c) +bool ringbuffer_full(ringbuffer* f) { - if(ringbuffer_full(f))return false; - - f->data[f->back]=c; - f->back--; - f->back+=f->size; - f->back%=f->size; - return true; + if((f->back-1+f->size)%f->size==f->front)return true; + return false; } -bool ringbuffer_full(ringbuffer* f) +bool ringbuffer_empty(ringbuffer* f) { - if((f->back-1+f->size)%f->size==f->front) - { - return true; - } + if(f->front==f->back)return true; return false; } bool ringbuffer_has(ringbuffer* f) { - bool res=true; + return !ringbuffer_empty(f); +} + +// + +bool ringbuffer_put(ringbuffer* f,uint8_t c) +{ + if(ringbuffer_full(f))return false; + + f->data[f->back]=c; + f->back--; + f->back+=f->size; + f->back%=f->size; - if(f->front==f->back) - res=false; - return res; + return true; } uint8_t ringbuffer_get(ringbuffer* f) { - char c; - - if(f->front==f->back) - { - return 0; - } + if(ringbuffer_empty(f))return 0; // indistinguishable from value 0 :( // TODO - c=f->data[f->front]; + uint8_t c = f->data[f->front]; f->front--; f->front+=f->size; f->front%=f->size; + return c; } + +// TODO // + +uint32_t ringbuffer_write(ringbuffer* f, uint8_t *buf, uint32_t size) +{ + return 0; +} + +uint32_t ringbuffer_read(ringbuffer* f, uint8_t *buf, uint32_t size) +{ + return 0; +} diff --git a/kernel/ringbuffer.h b/kernel/ringbuffer.h index 6ad9537..f68f766 100644 --- a/kernel/ringbuffer.h +++ b/kernel/ringbuffer.h @@ -27,7 +27,7 @@ #include /** Ringbuffer sturcutre */ -typedef volatile struct ringbuffer_struct +typedef struct ringbuffer_struct { uint32_t size; uint32_t front; diff --git a/kernel/scheduler.c b/kernel/scheduler.c index a4e3743..9fdff7d 100644 --- a/kernel/scheduler.c +++ b/kernel/scheduler.c @@ -74,10 +74,10 @@ volatile void scheduler_init(uint32_t cpu, void *dir) } current_task[cpu]=0; - last_task[cpu]=0; +// last_task[cpu]=0; // need to make space on the esp stacks for pushing vals vias task_pusha - + // this is our main kernel task at slot 0 (per cpu) task_list[cpu][0].parent=0; task_list[cpu][0].pid=nextPID(); @@ -85,10 +85,10 @@ 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-8; - task_list[cpu][0].esp0 = 0; // esp0 not needed by kernel space tasks - strcpy(task_list[cpu][0].name,"kernel_worker"); - fd_init_std_streams(task_list[cpu][0].pid,0); + task_list[cpu][0].esp = kballoc(4)+4*4096-0x200-8; // 4 pages stack & prealign + task_list[cpu][0].esp0 = 0; // esp0 not required by kernel space tasks + strcpy(task_list[cpu][0].name,"kernel worker"); + //fd_init_std_streams(task_list[cpu][0].pid,0); // this will go to userspace task_list[cpu][1].parent=0; @@ -97,11 +97,11 @@ 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-8; // 4 pages stack & prealign - task_list[cpu][1].esp0 =kballoc(4)+4*4096; // esp0 not needed by kernel space tasks - strcpy(task_list[cpu][1].name,"init"); - fd_init_std_streams(task_list[cpu][1].pid,0); - + task_list[cpu][1].esp = kballoc(4)+4*4096-0x200-8; // 4 pages stack & prealign + task_list[cpu][1].esp = VMEM_USER_STACK_TOP-0x200-8; + task_list[cpu][1].esp0 = kballoc(4)+4*4096; // esp0 needed by user space tasks + strcpy(task_list[cpu][1].name,"userspace init"); + //fd_init_std_streams(task_list[cpu][1].pid,0); // sleeper task_list[cpu][2].parent=0; @@ -111,9 +111,9 @@ volatile void scheduler_init(uint32_t cpu, void *dir) task_list[cpu][2].syscall=false; task_list[cpu][2].vmem=dir; 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 + task_list[cpu][2].esp0 =0; // esp0 not needed by kernel space tasks strcpy(task_list[cpu][2].name,"idle process"); - fd_init_std_streams(task_list[cpu][2].pid,0); + //fd_init_std_streams(task_list[cpu][2].pid,0); // stacks task_pusha(task_list[cpu][0].esp); @@ -128,8 +128,8 @@ static uint32_t scheduler_schedule(uint32_t idx) if(task_list[cpu][idx].active && !task_list[cpu][idx].syscall) { - if(current_task[cpu]!=0)last_task[cpu]=current_task[cpu]; - if(current_task[cpu]==idx)return task_list[cpu][idx].esp; + //if(current_task[cpu]!=0)last_task[cpu]=current_task[cpu]; + //if(current_task[cpu]==idx)return task_list[cpu][idx].esp; current_task[cpu]=idx; @@ -162,49 +162,26 @@ volatile uint32_t scheduler_run(uint32_t oldesp,uint32_t preference) uint32_t cpu=smp_get(SMP_APIC_ID); uint32_t init=smp_get(SMP_SCHEDULER_INIT); - if(init){ + if(init) + { scheduler_init(cpu,x86_get_page_directory()); smp_set(SMP_SCHEDULER_INIT,0); + klog("Scheduler initialized for cpu %d",cpu); + return task_list[cpu][current_task[cpu]].esp; } - else task_list[cpu][current_task[cpu]].esp=oldesp; - uint32_t esp; - - if(preference!=-1) - { - esp=scheduler_schedule(preference); // try preference - if(esp)return esp; - - if(current_task[cpu]==0)// we have interrupted a task with ring1 work - { - esp=scheduler_schedule(last_task[cpu]); // try preference - if(esp)return esp; - } - } - else - { - //klog("preempt %d", last_task[cpu]); - } - for(int i=0;i