Changing CR3

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
pini

Changing CR3

Post by pini »

Consider the following code :

Code: Select all

        mov     edx,cr3
        mov     ebx,[edi + CONTEXT_KSTACK]
        mov     eax,[edx + (ADDRESS_KSTACK shr (MEM_PTE_BITS + MEM_OFFSET_BITS))]
        or      ebx,MEM_PAGE_PRESENT or MEM_PAGE_WRITEABLE
        and     eax,MEM_PAGE_MASK
        mov     [eax + (((ADDRESS_KSTACK and MEM_PTE_MASK) shr MEM_OFFSET_BITS) shl 2)],ebx
        invlpg  [ADDRESS_KSTACK]

; The code above is just used to map the process context in the kernel address space

        mov     esp,[edi + CONTEXT_OFFSET]
        pop     eax
        mov     [edi + CONTEXT_OFFSET],eax
        mov     eax,[edi + CONTEXT_PD]
        mov     cr3,eax
        pop_all
        iret
At the end, you can see this instruction :

Code: Select all

        mov     eax,[edi + CONTEXT_PD]
which loads the page directory of the next task to EAX, and then EAX is put in CR3.
This code works good with both bochs and qemu.

In fact, with my initial task example, the page directory is located at 0x1003000, which is correct (I printed this value out before writing to CR3).

But when I do the same with my real P4M, EAX contains the value 0x1003F80, which is false, and obviously the computer reboots when I write CR3.
I can't figure out why this is happening. Any idea ?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Changing CR3

Post by Pype.Clicker »

does EDI contains the same thing with both runs ?

Honnestly, there are soo many memory references in your code, the best thing to do is probably to run it step by step in a debugger and see if the state of the memory is what you expect to be.
pini

Re:Changing CR3

Post by pini »

I'll try to do what you suggest this evening.

Pype.Clicker wrote: Honnestly, there are soo many memory references in your code
fortunately, I'm not using C :)
pini

Re:Changing CR3

Post by pini »

I have finally figured it out : the error was in my memory allocator.
I have somehow forgotten to page-align the pages addresses in the allocator's structures. Anyway, it should not have been necessary to do an explicit alignment, because addresses calculation should already been correct.
Post Reply