[solved] Pagefault when I switch to usermode

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
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

[solved] Pagefault when I switch to usermode

Post 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? :?
Last edited by Craze Frog on Wed Oct 08, 2008 7:27 am, edited 1 time in total.
User avatar
i586coder
Member
Member
Posts: 143
Joined: Sat Sep 20, 2008 6:43 am

Re: Pagefault when I switch to usermode

Post by i586coder »

can you post some code in order trying to help you :shock:
Distance doesn't make you any smaller,
but it does make you part of a larger picture.
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: Pagefault when I switch to usermode

Post 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;
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: Pagefault when I switch to usermode

Post by Combuster »

is both the page directory entry and the page table entry marked as at least Present+RW+User
"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 ]
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Pagefault when I switch to usermode

Post by egos »

Craze Frog wrote:$100023
$100007?
If you have seen bad English in my words, tell me what's wrong, please.
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: Pagefault when I switch to usermode

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Pagefault when I switch to usermode

Post 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
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: Pagefault when I switch to usermode

Post by Craze Frog »

Thanks guys, everything works perfectly now. I got a bit confused about all the bits.
Post Reply