Paging

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.
Locked
Dohrann
Posts: 2
Joined: Mon Dec 12, 2011 1:47 am

Paging

Post by Dohrann »

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:

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) );
}
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:

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();
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
Dohrann
Posts: 2
Joined: Mon Dec 12, 2011 1:47 am

Re: Paging

Post by Dohrann »

Bump
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Paging

Post by Combuster »

In a hurry?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Paging

Post by DavidCooper »

Dohrann wrote:Bump
That's really going to annoy people - I hope you haven't just blown your chance of getting any help.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Paging

Post by AJ »

Dohrann wrote:Bump
Lock
Locked