Paging problem (doesn't work at all)

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

Paging problem (doesn't work at all)

Post 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   
User avatar
liquid.silver
Member
Member
Posts: 46
Joined: Sat Jun 30, 2007 12:07 pm
Location: South Africa
Contact:

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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.
User avatar
liquid.silver
Member
Member
Posts: 46
Joined: Sat Jun 30, 2007 12:07 pm
Location: South Africa
Contact:

Post by liquid.silver »

Ya that's it. What nonsense i wrote. (didn't see the align directive)
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

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

Post 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.
Post Reply