Sign in to follow this  
  • entries
    72
  • comments
    51
  • views
    59830

GDT Descriptor Codes

Sign in to follow this  
Crypter

146 views

Hey everyone,

I have had several readers being confused at what the "0x8" descriptor code was for.

The memory addressing mode used in protected mode follows the descriptor:address model, NOT the segment:offset model.

Because of this, everything relies on the descriptors set up within the GDT.

In setting up the GDT, We need to load it using a base address of the table, and store it within the GDTR processor register...

; Global Descriptor Table. Protected Mode requires this, as it allows us to
; enter different protection modes (Such as Ring 0 and Ring 3)

gdt_data:

; All descriptors are 8 bytes in size. The null descriptor is from GDT offset 0.
; so its descriptor code is 0

dd 0 ; null descriptor
dd 0

; This is the code descriptor. It specifies that this segment (from 0-0xfffff)
; contains executable code at ring 0. This is our kernel code.

; Notice that this is exactally 16 bytes from the start of the GDT. Hence, its
; offset from the base address if 0x8, so its code descriptor is 0x8

; 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

; The same applies here. Because each descriptor is 8 bytes, the data
; descriptor is 8 bytes after the code descriptor, which is offset 0x10.

; gdt data: ; data descriptor
dw 0FFFFh ; limit low (Same as code)
dw 0 ; base low
db 0 ; base middle
db 10010010b ; access
db 11001111b ; granularity
db 0 ; base high

; pointer to GDT
end_of_gdt:
toc:
dw end_of_gdt - gdt_data - 1 ; limit (Size of GDT)
dd gdt_data ; base of GDT



Notice that we assign the pointer to point to the base of the GDT (gdt_data).

So, now we load the GDT's base pointer into GDTR:

lgdt [toc]




PMode uses CS to store the current descriptor to use. When entering protected mode from real mode, real mode does not know anything about the GDT. Because of this, the value of CS is incorrect.

To fix this, we have to set CS to point to our correct code descriptor, at offset 0x8, so we far jump:

; 16 bit code

jmp 0x8:PMode

[bits 32]

PMode:

; continue execution here...




The GDT is very important when setting up an interface between user mode applications and the kernel.

Tutorial 7 and 6 have both been updated to provide additional information.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now