GPF when I switch to a LDT descriptor using a far jmp

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
RodStewart
Posts: 2
Joined: Sun Jan 04, 2015 12:01 pm

GPF when I switch to a LDT descriptor using a far jmp

Post by RodStewart »

I am a beginner in the OS's world and I need your help please

I wrote a bare metal kernel which runs in the protected mode and I wanna do a jump to an LDT code segment, but when I tried to do that I got a general protection fault.

This is the GDT and the LDT entries:

Code: Select all

gdt_data:
        dd 0                ; null descriptor
        dd 0

; gdt code:                 ; code descriptor
        dw 0FFFFh           ; limit low
        dw 0                ; base low
        db 0                ; base middle
        db 10011010b        ; access
        db 11001111b        ; granularity
        db 0                ; base high

; gdt data:                 ; data descriptor
        dw 0FFFFh           ; limit low 
        dw 0                ; base low
        db 0                ; base middle
        db 10010010b        ; access
        db 11001111b        ; granularity
        db 0                ; base high

; ldt entry:                
        dw 00FFh            ; limit 
        dw ldt_data         ; base low
        db 0                ; base middle
        db 10000010b        ; access
        db 01000000b        ; granularity
        db 0                ; base high


; LDT ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ldt_data:
        dd 0                ; null descriptor
        dd 0
; ldt code:                 ; code descriptor
        dw 0x100            ; limit low 
        dw 0                ; base low
        db 0x80             ; base middle
        db 10011010b        ; access
        db 11000000b        ; granularity
        db 0                ; base high
; ldt data:                 ; data descriptor
        dw 0x100            ; limit low 
        dw 0                ; base low
        db 0x80             ; base middle
        db 10010010b        ; access
        db 11000000b        ; granularity
        db 0                ; base high



And I jumped in the Kernel using:

Code: Select all

jmp    dword 0x82 : 0x0000
Is it correct to jump directly to LDT descriptor or I have some kind of misunderstanding??
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: GPF when I switch to a LDT descriptor using a far jmp

Post by Combuster »

dw ldt_data ; base low
db 0 ; base middle
(...)
db 0 ; base high
Does your entire kernel fit within the first 64K of physical memory? How did you get it there?
jmp dword 0x82 : 0x000
Is the code executed actually at address 0 physical? Why did you have to destroy the IVT to get it there? Shouldn't the selector be 00001100 binary instead of 10000010 binary?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: GPF when I switch to a LDT descriptor using a far jmp

Post by alexfru »

RodStewart wrote: And I jumped in the Kernel using:

Code: Select all

jmp    dword 0x82 : 0x0000
So, where does this 0x82 come from? Your last night's dream?

Pull out your CPU manual (or download a copy if you haven't yet!) and see how selectors are defined:
bits 15 through 3: index of the respective GDT/LDT entry (I don't think you have 17 entries even in both tables combined)
bit 2: 0 for GDT segment, 1 for LDT segment (0x82 has this bit set to 0, so, how is this supposed to use LDT then?)
bits 1 through 0: RPL (0x82 has this set to 2? 2? Really?)
RodStewart
Posts: 2
Joined: Sun Jan 04, 2015 12:01 pm

Re: GPF when I switch to a LDT descriptor using a far jmp

Post by RodStewart »

Thank you Combuster and alexfru.

It was a fatal mistake :shock:

But I wonder, is it a meaningful to have an LDT without TSS ?
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: GPF when I switch to a LDT descriptor using a far jmp

Post by alexfru »

RodStewart wrote:But I wonder, is it a meaningful to have an LDT without TSS ?
You rarely need to have more than a handful of code/data segments. It must be something very special that would require having many of them, especially many per process. For example, Borland Pascal 7 could create 16-bit protected mode programs for i80286+. While real and protected mode programming differ quite a bit in terms of segmentation, Borland figured out a way to make the two similar. Just like in real mode adding 4096 to a selector advances the physical address by 64KB, they introduced a dedicated variable, SelectorInc, that could be used for the same purpose, but would have different values in real and protected mode. This made using buffers larger than 64KB the same in real and protected mode. Under the hood the compiler would need to allocate a number of GDT or LDT entries in order to support such a segmentation scheme (LDT is preferable).
Post Reply