Page Faults.....

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
Cjmovie

Page Faults.....

Post by Cjmovie »

Well, I made my kernel FINALLY support full tasks. It works fine....in Bochs!

You see, it maps blank new physical pages into the start of memory, depending on the size of an app. Then it copies the program into that space and schedules it.

The problem is, on a real computer, it page faults when it tries to write to the new area. I'm wondering, any basic/general things that could cause this, before I bore anyone with code?

I know it's not a cache thing, as the CPU should clear the page table/directory cache when I write to CR3, and I switch out CR3 right after I setup the new table with the 0x00000000 already mapped there.
pradeep

Re:Page Faults.....

Post by pradeep »

What does the CR2 contains? Print it and you would have a clue.Have you used E820? Does your computer supports it? Is your allocate free page function working correctly?
Cjmovie

Re:Page Faults.....

Post by Cjmovie »

My allocate and free page work fine. Also, I know it couldn't be my free-page function, because I've yet to even use it ^_^.

CR2 contains 0x00000000, otherwise I'd have no clue what the problem was/where it was coming from. Currently, all my page fault handler does is dump the registers and display what CR2 contains, and I've identified it to be erronous where the code first edits the area, with a memcpy() to put the code from the temporary buffer into the new memory location.

IDC about E820, as I already know the layout of my computers memory (no holes, such as at 1mb-2mb of 15-16mb), and I've yet to even rely on it. Currently, it assumes 32mb of RAM, and my computer has 256mb (which is actually pretty big waste for a x86_64 Computer, but oh well....). Also, my memory allocation tests work fine on my computer as well, so it's not a problem with usable memory.
Cjmovie

Re:Page Faults.....

Post by Cjmovie »

OK, heres a small subset of code, which is where the fault occurs.

Code: Select all

TaskList[a].esp3    = 0x7FFFFFFC; //Stack for process itself, VIRTUAL ADDRESS
 NewPage             = MMU_CreateTable(); //Create 'blank' table for mapping
 TaskList[a].cr3     = NewPage.Phys; //Set new CR3 Used by task
 
 if(!MMU_MapWith(NewPage, 0x7FFFF000, MMU_AllocPagePhys())){ //Map a 4K stack in place
  PrintString("ERROR! ERROR!\n");
 }
 
 for(i=0; i<(Size/4096)+1; i++){ //Loop to set up memory area for task code
  MMU_MapWith(NewPage, i*0x1000, MMU_AllocPagePhys()); //Get new page for code data
 }
 
 MMU_UseMap(NewPage.Phys, NewPage.Virt); //Use our new map
 
 memcpy((UCHAR*)0x00000000, (UCHAR*)Code, Size); //Copy code into beginning of virt. memory
 Stack = (UINT*)TaskList[a].esp3; //Get stack pointer for process
Cjmovie

Re:Page Faults.....

Post by Cjmovie »

Ha! I better stop asking questions on this forum :).
It seems most of the time I should have just took a little longer to debug, and looked at more than the obvious..... :-\

Turns out that the problem was in the function MMU_MapWith. It would allocate a physical page then map it into a static virtual address (as in, every call causes it to do the same mapping to the same virtual address). So what happened is that after the first call to it, the CPU would have the old data in its cache, and I have to invalidate it :). Bochs, apparently, does no caching....(emulated wise)

Also, I was forced to load and reload CR3, thus invalidating ALL pages. Anyone know the proper use of INVLPG? I keep getting an invalid operand error with NASM....
Warrior

Re:Page Faults.....

Post by Warrior »

I think it's in the "ThingYouCannotDoWithC" section of the Wiki.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Page Faults.....

Post by Candy »

Cjmovie wrote: Ha! I better stop asking questions on this forum :).
It seems most of the time I should have just took a little longer to debug, and looked at more than the obvious..... :-\
Of course, if you take infinite time you will find all the answers yourself. Do you have infinite time?

Which is why we're here. We took some time to figure it out with help from others as well, and now we know a few tricks and possible problems that you might not.

Of course, a fresh look always helps.
Turns out that the problem was in the function MMU_MapWith. It would allocate a physical page then map it into a static virtual address (as in, every call causes it to do the same mapping to the same virtual address). So what happened is that after the first call to it, the CPU would have the old data in its cache, and I have to invalidate it :). Bochs, apparently, does no caching....(emulated wise)
Also, I was forced to load and reload CR3, thus invalidating ALL pages. Anyone know the proper use of INVLPG? I keep getting an invalid operand error with NASM....
iirc INVLPG should attempt to access a memory operand on the page which you want to flush.

Try the instruction manuals for your favorite chip vendor, for AMD it's number 24594 and for AMD it's volume II of the series, with INVLPG in book 1.
INVLPG mem8

0F 01 /7

Invalidate the TLB entry for the page containing a specified memory location
So, that would come down to

Code: Select all

mov eax, 0xCAFEBABE 
INVLPG [eax]
for me.
Post Reply