From 5348a94a6e7a16a070c502c29db30a08253a99a3 Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Tue, 2 Sep 2014 14:53:09 +0200 Subject: Debugging paging problem on VirtualBox (VT-x) --- kernel/console.c | 6 +++--- kernel/kernel.c | 64 +++++++++++++++++++++++++++++++------------------------- kernel/vesa.c | 12 ++++++++--- kernel/vmem.c | 55 +++++++++++++++++++++++++++++++++++------------- kernel/x86.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- kernel/x86.h | 2 +- 6 files changed, 149 insertions(+), 54 deletions(-) (limited to 'kernel') diff --git a/kernel/console.c b/kernel/console.c index 72ad066..16cf04f 100644 --- a/kernel/console.c +++ b/kernel/console.c @@ -1,6 +1,6 @@ #include "console.h" -//#define FOOLOS_CONSOLE +#define FOOLOS_CONSOLE //#undef FOOLOS_CONSOLE static int posx=0; @@ -10,11 +10,11 @@ static int posy=0; void print_char_col(int x, int y, char c, char col) { -#ifdef FOOLOS_CONSOLE +//#ifdef FOOLOS_CONSOLE char* video_mem=(char *)SCR_VIDEOMEM+(x+y*SCR_REAL_WIDTH)*2; video_mem[0]=c; video_mem[1]=col; -#endif +//#endif } void print_char(int x, int y, char c) diff --git a/kernel/kernel.c b/kernel/kernel.c index 7bbfcce..429a286 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -25,22 +25,17 @@ void kernel_main(uint32_t initial_stack, int mp) { + volatile static uint32_t cpu1_counter=0; + // catch the APs (Application Processors) + /* if(mp==1) { - uint16_t c3=0; - while(1) - { - - - c3++; - asm("cli"); - - PutString("cpu2: %03d", 200,560,0b1111100000000000, c3/100); - - asm("sti"); - } + //while(1) { static uint16_t c=0; PutString("cpu2: %03d", 200,560,0b1111100000000000, (c++)/100); } + while(1) { static uint16_t c=0; PutString("cpu1counter: %d", 200,560,0b1111100000000000, (cpu1_counter)); } } + */ + // // We want to get output to the screen as fast as possible! @@ -62,7 +57,6 @@ void kernel_main(uint32_t initial_stack, int mp) // our video memory // - uint32_t vesa_physbase=vesa_init(0x8300,0x8400,0x7200); @@ -72,8 +66,6 @@ void kernel_main(uint32_t initial_stack, int mp) log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"initial esp: 0x%08X",initial_stack); - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"mp: %d",mp); - // // Initialize other processors // @@ -81,9 +73,25 @@ void kernel_main(uint32_t initial_stack, int mp) // Should support APCI in future too. // + if(!init_mp()) panic(FOOLOS_MODULE_NAME,"Can not Find _MP_"); + // + // Memory Init + // + + // we know that here, the bootloader placed the mamory map! + mem_init(0x7c00+0x400,*((uint16_t *)(0x7c00+0x600))); + + + // paging (pass the vesa physbase address for identity mapping) + vmem_init(vesa_physbase); + + while(1) { + PutString("cpu1counter: %d", 10,560,0b1111100000000000, (cpu1_counter)); + cpu1_counter++; + } // // Setup PIC // @@ -102,17 +110,6 @@ void kernel_main(uint32_t initial_stack, int mp) timer_init(); - // - // Memory Init - // - - // we know that here, the bootloader placed the mamory map! - mem_init(0x7c00+0x400,*((uint16_t *)(0x7c00+0x600))); - - // paging (pass the vesa physbase address for identity mapping) - vmem_init(vesa_physbase); - - // // Interrupts // @@ -136,8 +133,19 @@ void kernel_main(uint32_t initial_stack, int mp) int_install_ir(38, 0b10001110, 0x08,&int_floppy_handler); // now we can enable interrupts back again - int_enable(); - +// int_enable(); + + while(1) { + cpu1_counter++; + /* + static uint16_t c=0; + PutString("cpu1: A",20,560,0xffffff,0); + for(int c2=0;c2<0xffff;c2++); + PutString("cpu1: B",20,560,0xffffff,0); + for(int c2=0;c2<0xffff;c2++); + //PutString("cpu1: %03d", 20,560,0b1111100000000000, (c++)/100); + */ + } // // Scan the PCI Bus diff --git a/kernel/vesa.c b/kernel/vesa.c index b7b4615..a3fc76c 100644 --- a/kernel/vesa.c +++ b/kernel/vesa.c @@ -57,6 +57,12 @@ static console_y; static console_lines; static console_cols; +void vesa_set_physbase(uint32_t addr) +{ + VbeModeInfoBlock->physbase=addr; +} + + uint32_t vesa_init(vbeinfo *info,vbemodeinfo *mode,foolfont *rawfont) { //the only functionallu important init lines! (rest is log) @@ -180,12 +186,12 @@ void PutConsoleNL() { console_x=0; console_y++; - if(console_y>console_lines)console_y=1; + if(console_y>=console_lines)console_y=0; + for(int i=0;im_entries [PAGE_TABLE_INDEX (virt) ] = page; + table2->m_entries [PAGE_TABLE_INDEX (virt) ] = page; } - //! 1st 4mb are idenitity mapped - for (int i=0, frame=0x0, virt=0x00000000; i<1024; i++, frame+=4096, virt+=4096) + //! map 1mb to 3gb (where we are at) + + uint32_t vesa_mapped=0x1000*1024; + + for (int i=0, frame=vesa_physbase, virt=vesa_mapped; i<1024; i++, frame+=4096, virt+=4096) { //! create a new page pt_entry page=0; pt_entry_add_attrib (&page, I86_PTE_PRESENT); +// pt_entry_add_attrib (&page, I86_PTE_WRITABLE); pt_entry_set_frame (&page, frame); //! ...and add it to the page table - table2->m_entries [PAGE_TABLE_INDEX (virt) ] = page; + table1->m_entries [PAGE_TABLE_INDEX (virt) ] = page; } + for(int i=0;i<1024;i++)dir->m_entries [i]=0; - pd_entry* entry = &dir->m_entries [PAGE_DIRECTORY_INDEX (vesa_physbase) ]; + pd_entry* entry = &dir->m_entries [PAGE_DIRECTORY_INDEX (vesa_mapped) ]; *entry=0; pd_entry_add_attrib (entry, I86_PDE_PRESENT); pd_entry_add_attrib (entry, I86_PDE_WRITABLE); @@ -252,16 +270,23 @@ void vmem_init(uint32_t vesa_physbase) pd_entry_add_attrib (entry2, I86_PDE_WRITABLE); pd_entry_set_frame (entry2, (physical_addr)table2); - - - x86_set_pdbr(dir); log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"enabling paging..."); + + show_cr(); + + // while(1); + + x86_paging_enable(); + x86_paging_disable(); x86_paging_enable(); + vesa_set_physbase(vesa_mapped); + + // x86_flush_tlb(0); + // x86_flush_tlb(vesa_physbase); - x86_flush_tlb(0); - x86_flush_tlb(vesa_physbase); + show_cr(); } diff --git a/kernel/x86.c b/kernel/x86.c index 94f4f89..c1113bf 100644 --- a/kernel/x86.c +++ b/kernel/x86.c @@ -1,5 +1,45 @@ #include "x86.h" +// get control registers (cr0-cr4) + +uint32_t x86_get_cr0() +{ + uint32_t cr; + asm volatile("mov %%cr0, %0": "=b"(cr)); + return cr; +} + +uint32_t x86_get_cr1() +{ + uint32_t cr=0; + + // reading the reserved cr1 register results in crash. + // (at least on emulators) + // asm volatile("mov %%cr1, %0": "=b"(cr)); + + return cr; +} + +uint32_t x86_get_cr2() +{ + uint32_t cr; + asm volatile("mov %%cr2, %0": "=b"(cr)); + return cr; +} + +uint32_t x86_get_cr3() +{ + uint32_t cr=0; + asm volatile("mov %%cr3, %0": "=b"(cr)); + return cr; +} + +uint32_t x86_get_cr4() +{ + uint32_t cr; + asm volatile("mov %%cr4, %0": "=b"(cr)); + return cr; +} void x86_outb(int port, uint8_t data) { __asm __volatile("outb %0,%w1" : : "a" (data), "d" (port)); @@ -43,15 +83,31 @@ void x86_set_pdbr(uint32_t addr) } +// enable PT bit in CR0 void x86_paging_enable() { - unsigned int cr0; - asm volatile("mov %%cr0, %0": "=b"(cr0)); - cr0 |= 0x80000000; + uint32_t cr0=x86_get_cr0(); + cr0 |= 0x80000000; // enable paging + +// cr0 |= 0x40000000; // cahce disable +// cr0 |= 0x20000000; // not-write-through +// cr0 |= 0x10000; // write to read-only pages + + asm volatile("mov %0, %%cr0":: "b"(cr0)); +} + + +// disable PT bit in CR0 +void x86_paging_disable() +{ + uint32_t cr0=x86_get_cr0(); + cr0 &= ~0x80000000; asm volatile("mov %0, %%cr0":: "b"(cr0)); } -x86_flush_tlb(uint32_t addr) +void x86_flush_tlb(uint32_t addr) { asm volatile("invlpg (%0)" ::"r" (addr) : "memory"); } + + diff --git a/kernel/x86.h b/kernel/x86.h index 434c28c..345162f 100644 --- a/kernel/x86.h +++ b/kernel/x86.h @@ -18,6 +18,6 @@ void x86_outl(int port, uint32_t data); uint32_t x86_inl(int port); void x86_set_pdbr(uint32_t addr); void x86_paging_enable(); -void x86_flush_tlb(void* m); +void x86_flush_tlb(uint32_t addr); #endif -- cgit v1.2.3