Page 1 of 1

Paging problem

Posted: Tue May 13, 2003 11:00 pm
by pepito
I wrote a little kernel that enable A20, map IRQ 0-15 to INT 0x20-0x2D, and switch to PM. I have just a very basic console drivers (keybord & VGA). This little OS runs well, but now I like add 'paging' to it.

I write this rutine:

-----------------------

; Initialize a page directory -->

mov ax, 0x0100 ; Page directory address at 0x1000
mov fs, ax ; Load 'fs'

mov bx, 0 ; First entry
mov cx, 0x400 ; Set a counter to 1024 entries

.load_pd:

mov [fs:bx], WORD 0x0002 ; Insert a no-present page table
inc bx
inc bx ; Go next entry

loop .load_pd

; Insert first page directory entry -->

mov eax, 0x2000 ; Address of the page table
or eax, 0x0003 ; Level S, R/W, present

mov [fs:0], eax ; Insert the entry

; Load the page table with the address first 4MB of memory -->

mov ax, 0x0200 ; Page table address at 0x2000
mov fs, ax ; Load 'fs'

mov bx, 0 ; First entry
mov edx, 0 ; First address

mov cx, 0x400 ; Set a counter to 1024 entries

.load_pt

or edx, 0x0003 ; Level S, R/W, present
mov [fs:bx], edx ; Insert entry
inc bx
inc bx ; Next entry
add edx, 0x800 ; Get next address

loop .load_pt

; Load CR3 with the page directory address -->

xor eax, eax
mov eax, 0x1000
mov cr3, eax

; Set the cr0 PG bit -->

mov eax, cr0               
or eax, 0x80000000
mov cr0, eax; <-- Here the computer crash (reset)

-----------------------

My problem is that the computer reset as soon as I set the PG bit!

Furthermore, I like to know if I need to change some of my code, to run in 'paging' mode, or I can use my code as it is now?

Thank you very much!

PD. I am not an ASM expert... Is my code right?

JAER Mex2003

RE:Paging problem

Posted: Sat May 17, 2003 11:00 pm
by Adek336
1. is it possible to setup paging bit under rmode?
2. each page dir/table entry is 4 bytes long, you just do BX += 2 and it should be BX += 4.

Cheers,
Adrian.

RE:Paging problem

Posted: Mon May 19, 2003 11:00 pm
by pepito
Yes Cheers, that was my problem, I correct it and a get it:

***

activar_PAG:

; ------------------------------------
; Inicializar el directorio de páginas
; ------------------------------------

mov ax, 0
mov gs, ax

mov ebx, 0x1000 ; Cargar 'bx' con el primer desplazamiento
mov ecx, 0x400 ; Establecer un contador en 1024 entradas

.llenar_dp

mov [gs:ebx], DWORD 0 ; Insertar entrada nula
add ebx, 4 ; Siguiente entrada

loop .llenar_dp

; ----------------------------------------
; Primera entrada al directorio de páginas
; ----------------------------------------

mov eax, 0x2000 ; Dirección de la primera tabla de páginas
or eax, 0x0003 ; Nivel S, L/E, presente (puede ir en una línea)

mov [gs:0x1000], eax ; Insertar la entrada

; -------------------------------
; Cargar la tabla con las paginas
; -------------------------------

mov ebx, 0x2000 ; Cargar 'bx' con el primer desplazamiento
mov edx, 0 ; Cargar 'dx' con la primera dirección

mov ecx, 0x400 ; Establecer un contador en 1024 entradas

.llenar_tp

or edx, 0x0003 ; Nivel S, L/E, presente
mov [gs:ebx], edx ; Insertar entrada
add ebx, 4 ; Siguiente entrada
add edx, 0x1000 ; Siguiente dirección

loop .llenar_tp

; -----------------------------------------------------
; Cargar CR3 con la dirección del directorio de páginas
; -----------------------------------------------------

xor eax, eax
mov eax, 0x1000
mov cr3, eax

; -----------------------------------------------
; Establecer el bit PG (paginar) del registro cr0
; -----------------------------------------------

mov eax, cr0            
or eax, 0x80000000 ; Establacer el bit PG en CR0              
mov cr0, eax ; Actualizar cr0

***

Sorry, I have not time to traslate it to english, but now runs nice!

Thank you

JAER Mex2003