Page 1 of 1

Paging problem (doesn't work at all)

Posted: Sun Dec 09, 2007 6:42 am
by Craze Frog
This is my paging code so far (fasm code). When I enable paging by writing to cr0 the cpu resets. I am trying to identity map the first 4 megabytes. Obviously I did something very wrong, but what?

Code: Select all

        ; setup pdir
        mov     eax, tbl
        or      eax, PG_PRESENT or PG_WRITE
        mov     [dir+0*4], eax
        ; setup ptbl
        mov     eax, PG_PRESENT or PG_WRITE
        mov     ecx, 0
        .pageloop:
        mov     [tbl+ecx*4], eax
        add     eax, PAGE_SIZE
        add     ecx, 1
        cmp     ecx, 1024
        jne     .pageloop

        ; enable paging
        mov     eax, tbl
        mov     cr3, eax
        mov     eax, cr0
        or      eax, PG_ENABLE_PAGING
        mov     cr0, eax

PAGE_SIZE =  4096
PG_ENABLE_PAGING = 80000000h
PG_PRESENT = 1
PG_WRITE = 2
PG_SUPERVISOR = 4

section '.bss' align 4096
        dir rd 1024
        tbl rd 1024   

Posted: Sun Dec 09, 2007 8:36 am
by liquid.silver
One thing i can think of is shouldn't the page directory be page aligned? You don't need to have space in your kernel code for it, just choose a area of unused memory.

Edit: also the same applies to each page table - they must be page aligned.

Posted: Sun Dec 09, 2007 8:48 am
by JamesM
Simple problem:

Code: Select all

; enable paging 
        mov     eax, tbl
Should be

Code: Select all

; enable paging 
        mov     eax, dir
You must put the page directory address into cr3 and not the first page table. Probably a typo.

Posted: Sun Dec 09, 2007 9:48 am
by liquid.silver
Ya that's it. What nonsense i wrote. (didn't see the align directive)

Posted: Sun Dec 09, 2007 10:11 am
by Craze Frog
Thanks for the help. I don't know what I was thinking. You know, when you look at the code for too long you see what you want to see and not what you typed...

Posted: Sun Dec 09, 2007 11:54 am
by Craze Frog
liquid.silver wrote:You don't need to have space in your kernel code for it, just choose a area of unused memory.
I just wanted to make things simple first (by putting them in the bss I made sure everything in them was zero before I started messing with them). I am now allocating the page directory and table dynamically from the page frame map.