Page 1 of 1

[solved] Pagefault when I switch to usermode

Posted: Tue Oct 07, 2008 11:31 am
by Craze Frog
When I switch to usermode I get a pagefault instantly. Obviously I don't want that, but I can't see what's wrong with my code.

The code that I want to run is simply a while loop in a function, starting at $1006D0.
Page table entry before and after the problem happens: $100023 (means: $100000 -> $101000 is read-write-accessed-usermode)
Error code: $5 (means:: read from usermode)
es, gs, fs, es: $23 (my user data segment selectors, kernel is $10)
cs: $1B (my user code segment selector, kernel is $8)
CR2: $1006D0 (that's where the code is)

Why do I get read errors when the memory is readable? :?

Re: Pagefault when I switch to usermode

Posted: Tue Oct 07, 2008 11:40 am
by i586coder
can you post some code in order trying to help you :shock:

Re: Pagefault when I switch to usermode

Posted: Tue Oct 07, 2008 11:51 am
by Craze Frog
I don't know what part of the code would be relevant.

The code I try to run in usermode (it works fine when I call it from kernel mode):

Code: Select all

procedure myThread();
var
    i: integer;
begin
    while true do begin
    end;
end;
The code where I go to user-mode:

Code: Select all

procedure Run(Thread: TTID);
var
    regs: IrqRegs;
    entry: pcardinal; { just for debugging }
begin
    { The next line sets up the regs structure with the correct register values, and
    switches to the correct page directory (note that even before this "switch" I still used the same page directory.)
    RunThread(PThread(Thread), @regs);
    { This code prints the page table entry for debugging }
    entry := PageEntryGet($100000);
    writestr('changed? ');
    writehexln(entry^);
    { Here I go to user-mode }
    asm
        lea esp, regs
        pop gs
        pop fs
        pop es
        pop ds
        popa
        add esp, 8
        iret
    end;
end;

Re: Pagefault when I switch to usermode

Posted: Tue Oct 07, 2008 3:11 pm
by Combuster
is both the page directory entry and the page table entry marked as at least Present+RW+User

Re: Pagefault when I switch to usermode

Posted: Tue Oct 07, 2008 11:14 pm
by egos
Craze Frog wrote:$100023
$100007?

Re: Pagefault when I switch to usermode

Posted: Wed Oct 08, 2008 5:58 am
by Craze Frog
Combuster wrote:is both the page directory entry and the page table entry marked as at least Present+RW+User
Thank you, it seems like the page directory entry was marked as supervisor. Which is a bit strange since I don't pass that flag when I'm allocating memory for it, but I guess it's added somewhere down the road...

egos, $100007 is supervisor mode and would definetely not work.

Re: Pagefault when I switch to usermode

Posted: Wed Oct 08, 2008 6:02 am
by AJ
Craze Frog wrote:Thank you, it seems like the page directory entry was marked as supervisor. Which is a bit strange since I don't pass that flag...
Of course, a page is "supervisor" by default - it's more a case of not passing the user flag.

Cheers,
Adam

Re: Pagefault when I switch to usermode

Posted: Wed Oct 08, 2008 6:10 am
by Craze Frog
Thanks guys, everything works perfectly now. I got a bit confused about all the bits.