Page 1 of 1

Page fault problem

Posted: Sat Aug 02, 2008 12:21 am
by huxuelei
Hi, I got a very strange problem when i enable paging.

there is some information i got from bochs:

00068859702i[CPU0 ] EFER = 0x00000000
00068859702i[CPU0 ] | RAX=0000000080000011 RBX=0000000000100000
00068859702i[CPU0 ] | RCX=00000000000b8efe RDX=00000000000003d5
00068859702i[CPU0 ] | RSP=0000000000067e38 RBP=0000000000067e38
00068859702i[CPU0 ] | RSI=0000000000000000 RDI=0000000000000001
00068859702i[CPU0 ] | R8=0000000000000000 R9=0000000000000000
00068859702i[CPU0 ] | R10=0000000000000000 R11=0000000000000000
00068859702i[CPU0 ] | R12=0000000000000000 R13=0000000000000000
00068859702i[CPU0 ] | R14=0000000000000000 R15=0000000000000000
00068859702i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf SF zf af PF cf
00068859702i[CPU0 ] | SEG selector base limit G D
00068859702i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00068859702i[CPU0 ] | CS:0008( 0001| 0| 0) 00000000 000fffff 1 1
00068859702i[CPU0 ] | DS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00068859702i[CPU0 ] | SS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00068859702i[CPU0 ] | ES:0010( 0002| 0| 0) 00000000 000fffff 1 1
00068859702i[CPU0 ] | FS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00068859702i[CPU0 ] | GS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00068859702i[CPU0 ] | MSR_FS_BASE:0000000000000000
00068859702i[CPU0 ] | MSR_GS_BASE:0000000000000000
00068859702i[CPU0 ] | RIP=00000000001042ee (00000000001042ee)
00068859702i[CPU0 ] | CR0=0x80000011 CR1=0x0 CR2=0x0000000080000011
00068859702i[CPU0 ] | CR3=0x00200000 CR4=0x00000000
00068859702i[CPU0 ] >> add byte ptr ds:[eax], al : 0000
00068859702e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting

it seems the bochs did not find the page fault handler.

but before this exception happend, i use the follow code to call my page fault handler:

Code: Select all

asm volatile ("int $0xe");
and this is work. i can see my page fault handler is executed.

but when i enable the paging , how could bochs can not find the correct handler?
here is the code i enable the paging:

Code: Select all

		asm volatile("movl %0, %%cr3\n\t"

					 "movl %%cr0, %%eax\n\t"

					 "or $0x80000000, %%eax\n\t"

					 "movl %%eax, %%cr0\n\t"

					 ::"r"(pageDirectory));
please give me some tips.

Re: Page fault problem

Posted: Sat Aug 02, 2008 1:18 am
by kmtdk
well, are you sure you have interrupts enabled, the idt entry is correct (rings ...) and do you point to a valid Dir table ... -> page table, -> a page.
and have you mapped the address, or is it 1:1 ?
But it seems as the next instruction is 0000, are you sure, you have done it right, because after enabling paging, the addrres is translated, and IF you have mapped it, and you don't have placed any code, there, it will crash. For me, it don't looks like a page fault. But can you give us some of the code, so we can see the error. ..


KMT dk

Re: Page fault problem

Posted: Sat Aug 02, 2008 1:21 am
by Brendan
Hi,

You've messed up the paging data structures, or the code you're using to enable paging isn't identity mapped.

This is the instruction that's causing the problem:

Code: Select all

00068859702i[CPU0 ] >> add byte ptr ds:[eax], al : 0000
The opcode for this instruction is "0x00, 0x00", and I expect that the page mapped at EIP is full of zeros.

It causes a page fault because EAX still contains 0x80000011 (from doing "mov cr0,eax" to enable paging), which is why Bochs is saying that the address that caused the page fault is 0x80000011 (or more specifically Bochs says "CR2=0x0000000080000011").
huxuelei wrote:but before this exception happend, i use the follow code to call my page fault handler:
Before you enable paging it works, but after you enable paging everything is messed up. It's fairly common IMHO...
huxuelei wrote:please give me some tips.
Sure - for fun, try using something like this when you enable paging:

Code: Select all

    mov eax,cr0
    or eax,0x80000000

    xor ecx,ecx
.wait:
    jecxz .wait

    mov cr0,eax
The will cause the computer to go into an "infinite" loop just before enabling paging.

Now run it, and when it gets to the "infinite" loop stop Bochs and enter the debugger (control + C).

Then break out of the "infinite" loop by changing ECX (e.g. "set ecx=1").

Then execute a few more instructions (single step with the "s" command) until the "mov cr0,eax" instruction is executed.

Now, Bochs should say the next instruction is "add byte ptr ds:[eax], al". This is immediately before an exception will occur (if you do "s" one more time it'll triple-fault). Look at your code by doing "x /100 eip". See how it's full of zeros?

Now trace your paging data structures, starting with CR3 to see if the page directory is right. Use the page directory entry to find the address of the page table, and use the page table entry to find the address of the page (just like the CPU would). This should make the mistake obvious.

Note: for Bochs "x /<count> <address>" shows you linear memory while "xp /<count> <address>" shows you physical memory. For paging structures you want to see physical memory.


Cheers,

Brendan