Why does switching to protected restarts the machine?-Collection of common programming errors
I’m trying to create very simple operating system in 64 bits. I’m trying to enter protected mode first, but I’m failing at this point.
When I do the far jump into 32 bits, the machine restarts.
My code is loaded into memory with another assembly program at the address 0x100.
The code is compiled with nasm and I’m running the program using qemu -fda
.
Here is the code I got so far:
[BITS 16]
jmp _start
_start:
cli
lgdt [GDT64]
; Switch to protected mode
mov eax, cr0
or al, 1b
mov cr0, eax
; Desactivate pagination
mov eax, cr0
and eax, 01111111111111111111111111111111b
mov cr0, eax
jmp (CODE_SELECTOR-GDT64):pm_start
[BITS 32]
pm_start:
jmp $
GDT64:
NULL_SELECTOR:
dw GDT_LENGTH ; limit of GDT
dw GDT64 ; linear address of GDT
dd 0x0
CODE_SELECTOR: ; 32-bit code selector (ring 0)
dw 0x0FFFF
db 0x0, 0x0, 0x0
db 10011010b
db 11001111b
db 0x0
DATA_SELECTOR: ; flat data selector (ring 0)
dw 0x0FFFF
db 0x0, 0x0, 0x0
db 10010010b
db 10001111b
db 0x0
LONG_SELECTOR: ; 64-bit code selector (ring 0)
dw 0x0FFFF
db 0x0, 0x0, 0x0
db 10011010b ;
db 10101111b
db 0x0
GDT_LENGTH:
If I do a jmp $
before the long jump, it works, the program is halted correctly, but when the long jump is done, it reboots the machine.
Did I forgot to setup something like a segment or something like that?