diff --git a/src/bringelle.c b/src/bringelle.c index 167e6d5..dbd83af 100644 --- a/src/bringelle.c +++ b/src/bringelle.c @@ -8,29 +8,32 @@ extern void interrupt_enable(); void utask(){ - char *msg=(char*)4206592+10; + char *msg=(char*)4206592+300; msg[0]='A'; msg[1]='\0'; while(1){ asm("mov $0x1, %%eax;int $0x30"::"b"(msg)); - for(int i=0;i<10000;i++){ + for(int i=0;i<5000;i++){ } } } void utask2(){ - char *msg=(char*)4206592+10; + char *msg=(char*)4206592+300; msg[0]='B'; msg[1]='\0'; while(1){ asm("mov $0x1, %%eax;int $0x30"::"b"(msg)); - for(int i=0;i<10000;i++){ + for(int i=0;i<5000;i++){ } } } +/** + * Kernel entry point + */ void bringelle(){ clear(); printc("Booting Bringelle...\n",GREEN); @@ -51,6 +54,7 @@ void bringelle(){ int* page_dir=paging_allocate(2); task_create(page_dir, utask,100, 0xFF); + int* page_dir2=paging_allocate(2); task_create(page_dir2, utask2,100,0xFF); diff --git a/src/core/paging.c b/src/core/paging.c index ab853bb..0ab5498 100644 --- a/src/core/paging.c +++ b/src/core/paging.c @@ -1,5 +1,6 @@ #include "paging.h" #include "libc/stdio.h" +#include "libc/math.h" /// Use a bitmap to keep track of allocated pages char pages_status[PAGING_MAX_PAGES/8]; @@ -105,11 +106,24 @@ int *paging_allocate(int p){ // ----- Task table int *u_page_table=(int*)paging_allocate_next_page(); - page_dir[1]=(int)u_page_table|7; // 1024*1024*4096/4 - u_page_table[0]=(int)page_dir|7; + page_dir[1]=(int)u_page_table|7; + u_page_table[0]=(int)page_dir|7; // Virtual address is 1024*4096/4 u_page_table[1]=(int)k_page_table|7; u_page_table[2]=(int)u_page_table|7; - u_page_table[3]=(int)paging_allocate_next_page()|7; + + int dir_entry=1; + int pt_entry=3; + int p_current=max(1,p); // Allocate at least 1 page + while(p_current!=0){ + if(pt_entry%1024==0){ + dir_entry++; + pt_entry=0; + u_page_table=(int*)paging_allocate_next_page(); + page_dir[dir_entry]=(int)u_page_table|7; + } + u_page_table[pt_entry]=(int)paging_allocate_next_page()|7; + p_current--; + } return page_dir; } diff --git a/src/core/paging.h b/src/core/paging.h index 34eddff..53014db 100644 --- a/src/core/paging.h +++ b/src/core/paging.h @@ -13,30 +13,35 @@ #define PAGING_ENTRY_POINT_VIRT (1024*PAGING_PAGE_SIZE+3*PAGING_PAGE_SIZE) #define PAGING_ENTRY_POINT_PHY(page_dir) ((int*)(((int*)((((int*)page_dir)[1])&0xFFFFF000))[3]&0xFFFFF000)) - - - - /** * Configure and enable paging */ void paging_enable(); + /** * Allocate a new page and return its address */ char* paging_allocate_next_page(); + /** * Set usage status of a page */ void paging_set_usage(int addr,char state); + /** * Create a new page directory containing * p pages. */ int *paging_allocate(int p); + +/** + * Simple dump + */ void paging_dump(int min,int max); + /** * Handler of page fault */ void paging_page_fault(); + #endif \ No newline at end of file diff --git a/src/core/scheduler.c b/src/core/scheduler.c index d046298..224c20d 100644 --- a/src/core/scheduler.c +++ b/src/core/scheduler.c @@ -123,19 +123,25 @@ void task_create(int *page_dir, void *task, int task_size, int stack_offset){ void scheduler_start(){ if(nproc>0){ + // Disable interrupt to not be interrupted asm("cli"); - scheduler_on=1; // Enable scheduling + // Enable scheduling + scheduler_on=1; + + // Save kernel stack state u32 *stack; asm("mov %%ebp, %0":"=r" (stack)); - TSS.esp0=(u32)stack+1; // Remove ebp (c call convention) + // Remove ebp from the (c call convention) and return address (call) + TSS.esp0=(u32)stack+2; asm("mov %%ss, %0": "=m" (TSS.ss0)); - current_id=1; + // Get first stack + current_id=0; PROC *p=&procs[current_id]; - // Ensure interrupts are activated and NT flag is clear p->regs.eflags|=0x200; p->regs.eflags&=0xffffbfff; + // Switch to user task asm( "push %0 \n\t" "jmp task_switch" diff --git a/src/core/scheduler.h b/src/core/scheduler.h index ecb9428..62d6561 100644 --- a/src/core/scheduler.h +++ b/src/core/scheduler.h @@ -35,6 +35,7 @@ extern u16 nproc; // Number of active tasks * Must be called at each clock interrupt */ void clock(); + /** * Called by clock() and schedule the next task * Stack is a pointer pointing to the gs register on the stack. @@ -42,10 +43,12 @@ void clock(); * order: gs,fs,es,ds,edi,esi,ebp,UNUSED,edx,ecx,ebx,eax,eip,cs,eflags,esp,ss */ void schedule(u32 *stack); + /** * Create a new task to be schedule */ void task_create(int *page_dir, void *task, int task_size, int stack_offset); + /** * Stack the scheduler starting by task with PID 0 */ diff --git a/src/drivers/pic.h b/src/drivers/pic.h index 19e0f5c..3438131 100644 --- a/src/drivers/pic.h +++ b/src/drivers/pic.h @@ -2,7 +2,7 @@ #define PIC_H /** - * Configure + * Configure the PIC */ void pic_init(); diff --git a/src/libc/math.h b/src/libc/math.h index 9f9bd58..4cc37a9 100644 --- a/src/libc/math.h +++ b/src/libc/math.h @@ -5,4 +5,5 @@ int pow(int x,int n); int max(int x,int y); int min(int x,int y); int abs(int x); + #endif \ No newline at end of file diff --git a/src/libc/stdio.h b/src/libc/stdio.h index e2a35ad..d4bd6b6 100644 --- a/src/libc/stdio.h +++ b/src/libc/stdio.h @@ -3,8 +3,19 @@ #include "drivers/framebuffer.h" +/** + * Print a char* in the framebuffer + */ void print(char*); + +/** + * Print a char in the framebuffer + */ void printc(char*,VIDEO_COLORS c); + +/** + * Print an integer using itoa() + */ void printi(int i); #endif \ No newline at end of file diff --git a/src/libc/string.h b/src/libc/string.h index 82106b2..c3d3614 100644 --- a/src/libc/string.h +++ b/src/libc/string.h @@ -1,6 +1,9 @@ #ifndef STRING_H #define STRING_H +/** + * Convert int to char + */ void itoa(int i, char *a); #endif \ No newline at end of file