I am trying to implement paging. Here is the code that I use
Code: Select all
/*For enabling the paging . This function just calls the init paging tables and paging directory functions and then sets cr3 register to the physical address of the page directory and sets bit 31 of the cr0 register*/
void __NOINLINE __REGPARM init_paging()
{
init_paging_tables();
init_paging_directory();
write_cr3((uint32_t*)&pg_dir);
uint32_t loc = (uint32_t)read_cr0();
write_cr0(loc|0x80000000);
monitor_write("Paging is enabled now\n");
return ;
}
Code: Select all
/*The page table is declared statically as a global variable*/
struct page_table pg_tbl ;
void __NOINLINE __REGPARM init_paging_tables()
{
int i ;
uint32_t address = 0;
for(i=0;i<1024;i++)
{
pg_tbl.m_entry[i]=address|2 ; /*present and in supervisor mode*/
address+=4096 ;
}
return ;
}
Code: Select all
void __NOINLINE __REGPARM init_paging_directory()
{
int i =0;
uint32_t address = (uint32_t)&pg_dir.m_entry[0];
/*Populate the fist table only. As we identity map the first 4 MB of
* space*/
pg_dir.m_entry[0]=address|3;
for(i=1;i<1024;i++)
{
pg_dir.m_entry[i]=0|3; //attribute set to not present, supervisor mode
}
return ;
}
I tried debugging the code with bochs debugger. And in the diassembly, the function init_paging_directories() is not called at all. The remote gdb also showed that the function was never called when I set a breakpoint on it.
I got the paging working fine if I try something like this
Code: Select all
struct page_directory * ptr_dir =(struct page_directory *)0x9C00 ;
struct page_table * ptr = (struct page_table *)0x9D00; // just after the page directory
I cannot explain the behavior at all. But when I remotely debug the kernel , it seems to have removed a lot of function calls and stuff as a result of optimizatoins . I removed all of the optimizations and still not the desired result . This is the command line I use for building the code
Code: Select all
gcc -c -g -march=i686 -ffreestanding -Wall -Werror -I. -fno-toplevel-reorder -o paging.o paging.c
I know these are very noob questions but my operating system project cannot continue without paging and I am stuck .
Edit : I was making a silly error in the code. Now , paging works fine. Sorry for spamming.