Page 2 of 2

Re: Page becomes not present when switching to ring 3

Posted: Wed Oct 08, 2014 12:43 am
by iocoder
iansjack wrote:The page is marked as user mode but not present (so says the page fault error code).
Oh weird. Could you please check the value of your page entry after the exception? If it is really not present then maybe someone has changed it to get you into trouble.

Re: Page becomes not present when switching to ring 3

Posted: Wed Oct 08, 2014 6:02 am
by Combuster
SlayterDev wrote:Changed to this (I think thats what you meant):

Code: Select all

asm volatile(" \
		mov $0x23, %%ax; \
		mov %%ax, %%ds; \
		mov %%ax, %%es; \
		mov %%ax, %%fs; \
		mov %%ax, %%gs; \
		push %0; \
		pushl $0x200; \
		pushl $0x1B; \
		push %1; \
		pushl $0x23; \
		iret; \
		":: "r" (mainThread->frame.esp), "m" (mainThread->frame.eip));
Now I get a general protection fault.
GCC is perfectly allowed to use eax (r) and esp-based references (m) in this construction. Both get clobbered before getting used.

I also don't trust the iret frame order - your probable eflags (0x200) are where I'd expect either SS or ESP.

Re: Page becomes not present when switching to ring 3

Posted: Wed Oct 08, 2014 7:35 am
by SlayterDev
Changing to this:

Code: Select all

asm volatile(" \
      pushl $0x23; \
      push %0; \
      pushl $0x200; \
      pushl $0x1B; \
      push %1; \
      mov $0x23, %%ax; \
      mov %%ax, %%ds; \
      mov %%ax, %%es; \
      mov %%ax, %%fs; \
      mov %%ax, %%gs; \
      iret; \
      ":: "r" (mainThread->frame.esp), "m" (mainThread->frame.eip));
Produces the same page fault. However, in the page fault handler, I checked the page from the faulting address and it comes up as present. I have no idea what the deal is here.

Re: Page becomes not present when switching to ring 3

Posted: Wed Oct 08, 2014 9:35 pm
by SlayterDev
Well I linked the executable higher up at 0x02000000. Now, if the program is just "jmp $" it runs. But if it is anything else, I get a read-only page fault on a page between where I mapped the program and the stack. I make adjustments to ensure that page is mapped and set as writable (present and user-mode of course) but then we get a "page not present" page fault again. Even stranger, the address loaded from cr2 is 0x2 (where the range we should be in is 0x02000000-0x2002000). I'm going to dig around some more in the morning and see if I can figure anything else out. Any ideas?