Triple fault when switch to protected mode
Posted: Mon Feb 08, 2016 7:11 pm
Hello. I'm writing a bootloader and at moment to switch to protected mode it cause a triple fault. Honestly I don't know why, I see everything "right". I've following tutorials, reading books and I don't understand why this doesn't work. Here is the code.
first stage
second stage
first stage
Code: Select all
org 0x7c00
bits 16
start:
mov [DriveBoot], dl
xor ax, ax
mov ds, ax ;00:0x7c00
cli
mov ss, ax
mov sp, 0x200
sti
call ClearScreen
cld
mov si, mensaje
call print
mov dl, 0x00
mov dh, 0x01
call UpdateCursor
call LoadSecondSector
cld
mov si, SecondSectorLoaded
call print
mov dl, 0x00
mov dh, 0x02
call UpdateCursor
jmp 0x1000:0x0 ;65536
hlt
ClearScreen:
mov ah, 0x00
mov al, 0x02
int 0x10
ret
UpdateCursor:
mov ah, 0x02
int 0x10
ret
LoadSecondSector:
.Reset:
mov ah, 0
mov dl, [DriveBoot]
int 0x13
jc .Reset
mov bx, 0x1000
mov es, bx
xor bx, bx ;offset = 0
.Load:
mov ah, 0x02
mov al, 1
mov ch, 0
mov cl, 2
mov dh, 0
mov dl, [DriveBoot]
int 0x13
jc .Load
ret
print:
lodsb
or al, al
jz .PrintDone
mov ah, 0eh
int 10h
jmp print
.PrintDone:
ret
mensaje: db "Bootloader cargado...",0
SecondSectorLoaded: db "Segundo sector cargado...",0
DriveBoot db 0
times 510 - ($-$$) db 0
dw 0xAA55
Code: Select all
org 0x0
bits 16
main:
mov ax, cs
mov ds, ax
mov ax, 0x21
cli
mov ss, ax
mov sp, 0x200
sti
cld
mov si, LoadingKernel
call Print
mov dl, 0x0
mov dh, 0x03
call updateCursor
call enable_A20_Line
cld
mov si, A20LineEnabled
call Print
cli
lgdt [toc]
mov eax, cr0
or eax, 0x01
mov cr0, eax
jmp 0x08:PMode_Entry ;Hear is the problem
hlt
updateCursor:
mov ah, 0x02
int 0x10
ret
Esperar_Controller:
in al, 0x64
test al, 2
jnz Esperar_Controller
ret
Esperar_Controller_Data:
in al, 0x64
test al, 1
jz Esperar_Controller_Data
ret
habilitar_A20_Line:
cli
call Esperar_Controller
mov al, 0xad
out 0x64, al
call Esperar_Controller
mov al, 0xd0
out 0x64, al
call Esperar_Controller_Data
in al, 0x60
push eax
call Esperar_Controller
mov al, 0xd1
out 0x64, al
call Esperar_Controller
pop eax
or al, 2
out 0x60, al
call Esperar_Controller
mov al, 0xae
out 0x64, al
sti
ret
Verificar_A20_Line:
pushf
push ds
push es
push di
push si
cli
xor ax, ax
mov es, ax
mov di, 0x500
mov ax, 0xffff
mov ds, ax
mov si, 0x510
mov al, byte [es:di]
push ax
mov al, byte [ds:si]
push ax
mov byte [es:di], 0x00
mov byte [ds:si], 0xff
cmp byte [es:di], 0xff
pop ax
mov byte [ds:si], al
pop ax
mov byte [es:di], al
mov ax, 0
je .Verificar_A20_Line_exit
mov ax, 1
.Verificar_A20_Line_exit:
pop si
pop di
pop es
pop ds
popf
sti
ret
enable_A20_Line:
call Verificar_A20_Line
cmp ax, 0
jne .A20_done
call habilitar_A20_Line
call Verificar_A20_Line
cmp ax, 0
jne .A20_done
cld
mov si, ErrorA20Line
call Print
.A20_done:
ret
Print:
lodsb
or al, al
jz .printDone
mov ah, 0eh
int 10h
jmp Print
.printDone:
ret
gdt_data:
;NULL DESCRIPTOR
dd 0
dd 0
;Code descriptor
dw 0xffff
dw 0
db 0
db 0x9a
db 0xcf
db 0
;Data descriptor
dw 0xffff
dw 0
db 0
db 0x92
db 0xcf
db 0
end_of_gdt:
toc:
dw end_of_gdt - gdt_data - 1
dd gdt_data
bits 32
PMode_Entry:
mov ax, 0x10
mov ds, ax
mov es, ax
;For the moment until hear
hlt
LoadingKernel: db "Cargando el kernel...",0
A20LineEnabled: db "A20 Line habilitado",0
ErrorA20Line: db "Error al habilitar A20 line", 0
times 512 - ($-$$) db 0