jueves, 15 de diciembre de 2011

Sistema Operativo pt.2: Modo Protegido

Siguiendo con esta guía sobre la programación de un Sistema Operativo desde cero, escribo ahora la segunda parte de esta travesía. Esta vez el tema es el Modo Protegido.
De por si, el paso al modo protegido puede ser dificultoso, sin embargo es bastante fácil, como yo ya lo pude comprobar. Antes de realizar el paso se deben apagar las interrupciones con cli y luego se debe declarar GDT e iniciarlo.
Esto se logra con la siguiente secuencia:

   cli
   lgdt [gdt_desc]


Por lo que para lograr un bootloader funcional se debería escribir:

;NuOS Kernel.
;Copyright (C) 2011  Felipe Cabrera

;This program is free software: you can redistribute it and/or modify
;it under the terms of the GNU General Public License as published by
;the Free Software Foundation, either version 3 of the License, or
;(at your option) any later version.

;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU General Public License for more details.

;You should have received a copy of the GNU General Public License
;along with this program.  If not, see <http://www.gnu.org/licenses/>.



;------------------------------boot.asm------------------------------;
[BITS 16]
[ORG 0x7c00]


jmp main

switch_protected:
   cli
   lgdt [gdt_desc]
   mov eax, cr0
   or eax, 1
   mov cr0, eax
   jmp 0x08:protected_main


main: 
   call switch_protected
   jmp main


[BITS 32]

protected_main:
   jmp protected_main

gdt:
 
gdt_null:
   dd 0
   dd 0
 
gdt_code:
   dw 0FFFFh
   dw 0
   db 0
   db 10011010b
   db 11001111b
   db 0
 
gdt_data:
   dw 0FFFFh
   dw 0
   db 0
   db 10010010b
   db 11001111b
   db 0
 
gdt_end:
 
gdt_desc:
   dw gdt_end - gdt - 1
   dd gdt


times 510-($-$$) db 0
db 0x55
db 0xAA


Pero claro, este bootloader no hace nada, y con nada me refiero visualmente nada, y esto no le agrada mucho a la gente. Además descubrí como escribir en el Modo Protegido, por lo que hay que aprovechar esto.


;NuOS Kernel.
;Copyright (C) 2011  Felipe Cabrera

;This program is free software: you can redistribute it and/or modify
;it under the terms of the GNU General Public License as published by
;the Free Software Foundation, either version 3 of the License, or
;(at your option) any later version.

;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU General Public License for more details.

;You should have received a copy of the GNU General Public License
;along with this program.  If not, see <http://www.gnu.org/licenses/>.


;------------------------------boot.asm------------------------------;
[BITS 16]
[ORG 0x7c00]

jmp main

bootmesg db "Loading... please wait... "

init:
   mov ax, cs
   mov ds, ax
   mov es, ax
   ret

print_mesg :
   mov ah,0x13
   mov al,0x00
   mov bx,0x0007
   mov cx,0x1A
   mov dx,0x0000
   int 0x10
   ret

clrscr :
   mov ax,0x0600
   mov cx,0x0000
   mov dx,0x174f
   mov bh,0
   int 0x10
   ret

switch_protected:
   cli
   lgdt [gdt_desc]
   mov eax, cr0
   or eax, 1
   mov cr0, eax
   jmp 0x08:protected_main

main:
   call init
   call clrscr
   mov bp,bootmesg
   call print_mesg
   call switch_protected
   jmp main

[BITS 32]
protected_main:
   mov ax, 10h
   mov ds, ax
   mov ss, ax
   mov esp, 090000h
   mov esi,words
   xor edx,edx
   loop0:
       mov word cx,[esi]
       mov word [ds:0B8000h+160d+edx],cx
       inc edx
       inc esi
       cmp byte [esi],0
       jnz loop0
   jmp protected_main

gdt:

gdt_null:
   dd 0
   dd 0

gdt_code:
   dw 0FFFFh
   dw 0
   db 0
   db 10011010b
   db 11001111b
   db 0

gdt_data:
   dw 0FFFFh
   dw 0
   db 0
   db 10010010b
   db 11001111b
   db 0

gdt_end:
 
gdt_desc:
   dw gdt_end - gdt - 1
   dd gdt

words db ' p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p pNpupOpSp pkpeprpnpeplp p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p p pPprpoptpepcptpepdp pMpopdpep pEpnpapbplpepdp.p p',0000

times 510-($-$$) db 0
db 0x55
db 0xAA


Esto da como resultado lo siguiente:

No hay comentarios.:

Publicar un comentario