After load my gdt, I need to update register cs, but when I put code segment in it, the CPU reset!
I feel confused, hope someone can take me out of this reset loop.
Here is my code:
Code: Select all
[bits 64] ;uefi should bring us to long mode
entry:
cli ;disable interrupt
lgdt [GDT_R] ;load gdt
mov rsp, STACK_BOTTOM
mov rax, 8
push rax ;push code seg
push new ;push where we want to go
retf ;far return
new:
mov ax, 16 ;update data seg
mov es, ax
mov ss, ax
mov ds, ax
mov fs, ax
mov gs, ax
call main ;call c code
jmp HALT
Code: Select all
GDT_R:
dw (GDT_END - GDT)
dq GDT
GDT:
NULL:
dw 0x0
dw 0x0
db 0x0
db 0x0
db 0x0
db 0x0
CODE:
dw 0xffff
dw 0x0
db 0x0
db 0x9a
db 0xaf
db 0x0
DATA:
dw 0xffff
dw 0x0
db 0x0
db 0x92
db 0xcf
db 0x0
GDT_END:
I fix it with:
Code: Select all
cli ;disable interrupt
lgdt [GDT_R]
mov eax, 0x10
push rax
mov rax, STACK_BOTTOM
push rax
push 0x202
mov eax, 0x8
push rax
mov rax, InNewGDT
push rax
iretq
InNewGDT:
mov ax, 0x10
mov es, ax
mov ss, ax
mov ds, ax
mov fs, ax
mov gs, ax
lea rax, [rel main]
call rax
jmp HALT
GDT_R:
dw GDT_END - GDT - 1
dq GDT
GDT:
NULL: equ $ - GDT
dq 0x0
CODE: equ $ - GDT
dw 0xffff
dw 0x0
db 0x0
db 0x9a
db 0xaf
db 0x0
DATA: equ $ - GDT
dw 0xffff
dw 0x0
db 0x0
db 0x92
db 0xcf
db 0x0
GDT_END:
sorry for my poor English.