Search code examples
assemblyx86fasmbochs

X86: protected mode, GDT, IDT


I've tried to execute simple kernel with a kolibri bootloader. It's being loaded into 1000:0000. I don't understand, what's wrong in this part:

...
; switch to PM
mov eax, cr0
or  al, 1
mov cr0, eax

use32
PROTECTED_ENTRY:
mov  ax, 00010000b  ; DATA
mov  ds, ax
mov  ss, ax
mov  esp, 0xFFFF

jmp $

mov  ax, 00011000b  ; VIDEO
mov  es, ax
mov  edi, 0

mov  esi, string
int 1

jmp $

'cause in debugger it looks like this enter image description here

What's going on here? Why ES and DS aren't being changed?

P.S. i'm trying to get this kernel working with kolibri loader: http://wasm.ru/article.php?article=ia32int


Solution

  • The processor does not automatically enter protected mode when you set the protected bit in cr0. It enters protected mode when cs is changed after that. The easiest way to do this is to insert a far jump immediately after writing to cr0.

    mov cr0, eax
    .db 066h
    jmp CODE_SEGMENT:PROTECTED_ENTRY
    
    use32
    PROTECTED_ENTRY:
    

    Hopefully I got that right. (I'm used to AT&T syntax.) That .db is an operand size override to allow a 32 bit address.