gcc erasing the function call from the code

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
richi18007
Member
Member
Posts: 35
Joined: Mon Mar 07, 2011 1:41 pm

gcc erasing the function call from the code

Post by richi18007 »

Hi ,
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 ;
}
Then I had the following two functions for setting page tables and page directories

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 ;
}
And for setting page directories

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 ;
}
The problem out here is that when I compile the code , everything is called correctly but the last debug statement after writing to cr0 register.
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
instead of statically allocating both the structures in the global area.
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
And then , link it all up. What's wrong in the entire setup ?
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.
Post Reply