Page 1 of 1

Can't jump to 32 bits, gdt problem?

Posted: Sun Oct 11, 2009 9:03 am
by Tjaalie
Dear thread reader,

Seeing that the tutorials have progressed quite far since the last time I checked them I finally began with my own os. Using the tutorials as a guide. I got the bootloader done and I'm working on the second stage right now. But when I try to jump to the 32 bit code my os triple faults. Here is what bochs tells me:
00015706189e[CPU0 ] jump_protected: gate type 0 unsupported
00015706189i[CPU0 ] CPU is in protected mode (active)
....
00015706189i[CPU0 ] >> jmp far 0008:0679 : EA79060800
I searched the web with this error (jump_protected: gate type 0 unsuporrted) and got a bunch of results of people who are trying to jump to 32 bit code, all reactions seem to indicate that its a gdt problem. I tried all the solutions posted on those forms but they didn't solve it.
So here is the code, in the hope that some bright mind can solve my error.

The gdt table:

Code: Select all

;;gdt table
gdt_start:
    dd 0
    dd 0
code equ $-gdt_start
    dw 0xFFFF
    dw 0
    db 0
    db 10011010b
    db 11001111b
    db 0
data equ $-gdt_start
    dw 0xFFFF
    dw 0
    db 0
    db 10010010b
    db 11001111b
    db 0
gdt_table:
    dw gdt_table - gdt_start - 1
    dw gdt_start
The gdt init code:

Code: Select all

;;setup gdt
gdt_init:
    ;;got this from one of the forums. But it doesn't solve anything. It sets the base of the code descriptor to the contents of cs
    ;;mov eax, 0
    ;;mov ax, cs
    ;;and eax, 0xFFFF
    ;;shl eax, 4
    ;;mov word [code+2], ax
    ;;shr eax, 16
    ;;mov byte [code+4], al
    ;;mov byte [code+7], ah

    ;;set gdt
    cli
    pusha
    lgdt [gdt_table]
    popa
    sti
    ret
And finally the pmode code:

Code: Select all

;;the 32 bit setup part
bits 16
bits32_init:
    ;;disable interupts and enable pmode
    cli
    mov eax, cr0
    or  eax, 1
    mov cr0, eax

    ;;jump to the 32 bit part
    jmp code:xboot2_32

;;the 32 bit part
bits 32
xboot2_32:
    jmp $
    ;;set registers
    mov ax, data
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov esp, 0xFFFF

    ;;hang
    jmp $
If I hang the os with 'jmp $' just after I enabled pmode there is no crash. So the jump (just as bochs indicated) is the problem. There is a other thread on this forum regarding a similar problem. Although the fix proposed there doesn't work for me. It keeps giving the exact same error msg.

If I change 'jmp code:xboot2_32' into 'jmp xboot2_32' it works. But something tells me that I need to make a far jump into the 32 bit code section. But since the other jump works I suspect there is something wrong with the gdt (just as indicated by google).

Thanks in advance for any time someone may spend on helping me out!

Re: Can't jump to 32 bits, gdt problem?

Posted: Sun Oct 11, 2009 10:39 am
by earlz

Code: Select all

    ;;jump to the 32 bit part
    jmp code:xboot2_32
`code` here is something that resolves to a constant right and not a variable location?

Re: Can't jump to 32 bits, gdt problem?

Posted: Sun Oct 11, 2009 11:36 am
by Combuster
Looks like you got an offset problem. Given the disassembled far jump it looks like you told the assembler your code gets executed at something like 0000:0600 or 0000:0000. You'll get problems when you try to run this code with CS/DS being nonzero as the 32-bit part expects it to be that way.

Also nasty is that you are missing 16 bits in your GDTR definition...

Re: Can't jump to 32 bits, gdt problem?

Posted: Sun Oct 11, 2009 11:44 am
by neon
Hello,

Code: Select all

gdt_table:
    dw gdt_table - gdt_start - 1
    dw gdt_start
gdt_start needs to be a dword not word.