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.
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.
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
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 ]
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 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).