Paging
Posted: Mon Dec 12, 2011 1:58 am
I have a question about basic paging (no swapping). I am using QEMU to create my first OS. Right now I am working on enabling basic paging. So far, my OS gets stuck in 'restart'. Here is the current code:
I added in an if(i == 1022) halt(); with some arbitrary print statements, which seemed to work properly. Although at i==1023, restart mode kicks in. I am not sure why, it is almost like I have a memory location set too low and it cannot access and/or fill an array correctly (I ran into this problem implementing file systems, QEMU seems to hit restart when accessing UN-existant memory in an array). I also added in this code after the else statement:
This seems to give the result (on bot tables) of the first 31 entries having values, while the rest are all 0's.
Help/Tips/Etc. are greatly appreciated.
--Troubled Dev
Code: Select all
#define MEMORY_SIZE 128
unsigned ptablepool1[1024+1024];
unsigned ptablepool2[MEMORY_SIZE/4 * 1024];
unsigned* optable;
unsigned* iptable[MEMORY_SIZE/4];
void paging_init(void)
{ //outter page table set up
int j;
unsigned i; //page aligned pointer
i = (unsigned) ptablepool1;
i += 4096;
i &= ~4095;
optable = (unsigned*) i; //optable now points to i
//inner page table set up
i = (unsigned) ptablepool2;
i += 4096;
i &= ~4095;
for(j=0;j<(MEMORY_SIZE/4);++j,i+=4096)
iptable[j]=(unsigned*)i;
for(i=0;i<1023;i++){
if(i>= MEMORY_SIZE/4){
//address past end of RAM
optable[i]=0; //present bit to 0
}
else{ //make outer point to inner
unsigned p = (unsigned) iptable[i];
p |= 3; //set W and P to 1
optable[i] = p; //optable[i] points to inner table i
//***********Fill inner page table
for(j=0;j<1024;++j){
p = i*1024+j; //frame number
p <<=12; //move to upper 20 bits
p |= 3; //set w and p bits
iptable[i][j] = p;
}
}
}
i = (unsigned) optable; //activate paging
asm volatile ("mov %%eax,%%cr3\n"
"mov %%cr0,%%eax\n"
"or $0x80000000,%%eax\n"
"mov %%eax,%%cr0\n"
"jmp flushqueue\n"
"flushqueue:\n"
"nop" : "+a" (i) );
}
Code: Select all
kprintf("%d at iteration %d\n", iptable[i], i);
kprintf("%d at iteration %d\n", optable[i], i);
if(i == 1022)
halt();
Help/Tips/Etc. are greatly appreciated.
--Troubled Dev