flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Paging woes.

Author
Thread Post new topic Reply to topic
Coty



Joined: 17 May 2010
Posts: 554
Location: ␀
Coty
Hello guys, I'm tampering with making a basic kernel yet again. It's been a while since I've done anything of the sort so please excuse my confusion.

What I am doing is setting up and installing paging, what I want to do is create a virtual address of 0x0000_0000 at 0x0000_A000. That way I can start working on basic multitasking like I had in one of my old demos, however my code isn't working, and I need to verify what I remember and understand of paging is correct that way I can troubleshoot a littler easier....

So here is what I understand:

Just for giggles I'm going to put the page DIR a 0x0000_1000 and the first 4MB table at 0x0000_0000...

Code:
     Cr3
[0x0000_1000]
      |
   PageDir 
[0x0000_0003]
       |
       |        /|-------[0x0000_0003]  -- Maps First 4kb block as adress 0x0000_0000 with read/write
       + Table0| |-------[0x0001_0003]  -- Maps Second 4kb block as adress 0x0000_0000 with read/write
                \|-------[0x0002_0003]  -- Maps Third 4kb block as adress 0x0000_0000 with read/write
                  ...
    


Right? Okay... so... let's try to map 0x0000_A000 to 0x0000_0000 I would find block A

Code:
     Cr3
[0x0000_1000]
      |
   PageDir 
[0x0000_0003]
       |
       |    
       |          ...
       |        /|-------[0x0009_0003]  -- Maps the 9th 4kb block as adress 0x0009_0000 with read/write
       + Table0| |-------[0x000A_0003]  -- Maps the 10th 4kb block as adress 0x000A_0000 with read/write
                \|-------[0x000B_0003]  -- Maps the 11th 4kb block as adress 0x000B_0000 with read/write
                  ...    


and edit it so that it maps to 0x0000_0000 like this:

Code:
     Cr3
[0x0000_1000]
      |
   PageDir 
[0x0000_0003]
       |
       |    
       |          ...
       |        /|-------[0x0009_0003]  -- Maps the 9th 4kb block as adress 0x0009_0000 with read/write
       + Table0| |-------[0x0000_0003]  -- Maps the 10th 4kb block as adress 0x0000_0000 with read/write
                \|-------[0x000B_0003]  -- Maps the 11th 4kb block as adress 0x000B_0000 with read/write
                  ...    


Is that correct or am I thinking backwards? If that is correct then I'm going to move on to my technique for implementation.

I first load some code to 0x0000_A000, then I map the first 4MB and enable paging by pointing cr3 to our index, then oring the bit in cr0. Then I jump to 0x0000_A000, I'm assuming it's within implementation or I'm understanding wrong... QEmu doesn't throw any exceptions... nothing happing :/

Just fyi, in my actual implementation my IDT is located at 0x0000_0000 physical, any help would be nice Very Happy My minds just a little twisted up in a knot atm Wink

_________________
http://codercat.org/
Post 16 Aug 2013, 22:04
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Sorry in my original post I didn't spot you swapped directions.

To map VA 0x0000000 to PA 0x0000A000 you'd need:
Code:
; At 0x00001000 PageDirectory
dd 0x00000003 ; PageTable0 at 0x00000000

; At 0x00000000 PageTable0 PageEntry0
dd 0x0000A003    
(Your diagram was correct to map VA 0x0000A000 to PA 0x00000000)

Note that your IDT is going to conflict with the PageTable0 at 0x00000000, so I'd move one of these somewhere else.
Post 17 Aug 2013, 08:56
View user's profile Send private message Reply with quote
egos



Joined: 10 Feb 2009
Posts: 144
egos
I recommend to use recursive page dir mapping. I would write a tutorial but I have some problems with English.


Last edited by egos on 29 Aug 2013, 14:12; edited 1 time in total
Post 17 Aug 2013, 10:57
View user's profile Send private message Reply with quote
Coty



Joined: 17 May 2010
Posts: 554
Location: ␀
Coty
@cod3b453: Thanks! As soon as I read your post everything fell into place! Only took me a few mins to get it working Very Happy

@egos: What I think would be cool; would be to actually make tutorials in your native language. There are many nice tutorials in English as it is, granted some need some clarification... but it would be nice to see more things in more languages Wink

_________________
http://codercat.org/
Post 17 Aug 2013, 17:49
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Coty



Joined: 17 May 2010
Posts: 554
Location: ␀
Coty
Just for fun... Seems to work anyway Smile

Code:

        use16
        org     0x7C00
        format  binary as "img"
;=============================================================================;
; Clear screen of all prior crap...                                           ;
;=============================================================================;
        push    dx
        push    0xb800
        pop     es
        xor     di, di
        mov     ax, 0
        mov     cx, 80*25
        rep     stosw
;=============================================================================;
; Set up P-Mode...                                                            ;
;=============================================================================;
        cli
        lgdt    [gdtr]
        mov     eax, cr0
        inc     eax
        mov     cr0, eax
        jmp     0x8:_PM
;-----------------------------------------------------------------------------;
gdtr:   dw GDT_end - GDT_start - 1     ; GDT size - 1  So that should be 23.
        dd GDT_start                   ; Were the GDT starts.
GDT_start:
        dq 0x0000000000000000          ; Null descriptor.
        dw 0x03FF                      ; 4MB limmit  1023 (1024-1)*4096 = 4MB)
        dw 0x0000                      ; Base adress = 0x0000
        dw 0x9A00                      ; code read/exec
        dw 0x00C0                      ; granularity

        dw 0x03FF                      ; 4MB limmit  1023 ((1024-1)*4096=2MB)
        dw 0x0000                      ; Base adress = 0x0000
        dw 0x9200                      ; code read/exec
        dw 0x00C0                      ; granularity
GDT_end:
;-----------------------------------------------------------------------------;
        use32
_PM:
        mov      ax, 0x0010            ; Move the data segment into AX then
        mov      ss, ax                ; into SS, DS, ES, GS, and FS.
        mov      ds, ax
        mov      es, ax
        mov      gs, ax
        mov      fs, ax
;-----------------------------------------------------------------------------;
; Install our basic paging...
;    This will map 4MB of "virtual" RAM corisponding to physical RAM.
;
; page_directory   = 0x0000_9000
; first_page_table = 0x0000_A000
;-----------------------------------------------------------------------------;
        mov     edi, 0x0009000         ; Paging starts at 0x9000 (at the PML4)
        mov     cr3, edi               ; place the location of PML4 into Cr3
        push    edi                    ; [!] put 0x00009000 on the stack. [!]
        ;'''''''''''''''''''''''''''''';
        ; Create the page directories. ;
        ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
        mov     eax, 0x00000002        ; NOT present
        mov     ecx, 1024              ;  1024 entries... This will point to our
                                       ; page table(s)
        push     cx                    ; {!} put 1024 onto the stack. {!}
        rep     stosd                  ; Clear all 1024 entries
        ;'''''''''''''''''''''''''''''';
        ; Create 1 page table, for 4MB ;
        ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
        pop      cx                    ; {!} Get 1024 from the stack. {!}
        mov      di, 0xA000
  .loop:
        or       al, 3                 ; <<< eax
        stosd
        add     eax, 4096
        loop    .loop
        ;'''''''''''''''''''''''''''''';
        ; Point page DIR to page table ;
        ;,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,;
        pop     edi                    ; [!] Get 0x00009000 from the stack [!]
        mov     eax, 0x0000A003        ; At location 0x0000_6000,
                                       ; Read/write bit = ture;
                                       ; Present bit = true;
        stosd
        mov     eax, cr0
        or      eax, 0x80000000        ; Enable paging bit.
        mov     cr0, eax
;-----------------------------------------------------------------------------;
; Put out dummy "spinner" app at 0x0000_1000
;-----------------------------------------------------------------------------;
        mov     edi, 0x00001000
        mov     esi, dummyApp
        mov     ecx, 14            ; Copy 14 bytes.
        rep     movsb              ; do it
;-----------------------------------------------------------------------------;
; Re-map 0x0000 to 0x1000, just for giggles...
;-----------------------------------------------------------------------------;
        mov     eax, [0xA000]      ; Get 0x0000_0000 entry
        xchg    eax, [0xA004]      ; swap it with 0x0000_1000
        mov     [0xA000], eax      ;   put 0x0000_1000 into 0x0000_0000 entry 
                                   ; thus fully swaping the entries...
;-----------------------------------------------------------------------------;
; Now goto 0x0000_0000 virtual, whitch is actually 0x0000_1000 physical!
;-----------------------------------------------------------------------------;
        jmp     0x00000000

;=============================================================================;
; Data area.                                                                  ;
;=============================================================================;

dummyApp:
        file "spinner.bin"

        times 510 - ($-$$) db 0x00
        dw 0xAA55

        times 1024*8 - ($-$$) db 0x00
    


Code:
        use32
org     0x00000000
        mov     edi, 0xB8000
        mov     eax, ": ) "
        stosd
        jmp     $
    

_________________
http://codercat.org/
Post 28 Aug 2013, 18:05
View user's profile Send private message Send e-mail Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.