Page 1 of 1

installing own GDT using gdt_flush() why jmp 0x08:flush2

Posted: Wed Jan 30, 2008 12:53 pm
by redDot
hello friends,
i have been following the OS tutorial by bran and have been currently studying the topic GDT. What i am finding difficult to understand is the use of the instruction jmp 0x08:flush2 to load the cs with the appropriate value. i understand that 0x08 is the location for the kernel code in the GDT created to be installed, but i don't understand how the jmp statement is loading the code segment with the base address of the kernel code.

the code is,
global _gdt_flush
extern _gp
_gdt_flush:
lgdt [_gp]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:flush2
flush2:
ret

thank you for your time. any help and suggestion will be extremely appreciated.

Posted: Wed Jan 30, 2008 1:25 pm
by cyr1x
The lgdt instruction loads a pointer to a GDT
Now lets say our GDT looks like this

Code: Select all

; null-descriptor
dq 0
;kernel-code-segment (this is offset 0x8)
dw 0xFFFF		;limit
dw 0x0000		;base low
db 0x00			;base middle
db 0x9A			;access
db 0xCF			;gran
db 0x00			;base high
;kernel-data-segment (this is offset 0x10)
dw 0xFFFF
dw 0x0000
db 0x00
db 0x92
db 0xCF
db 0x00	
Now, when you do a far jmp like

Code: Select all

 jmp 0x08:somelocation 
it loads the GDT-Entry from the gdt-pointer with the offset 0x08 and loads the base, limit, etc.. of the entry

Posted: Wed Jan 30, 2008 3:00 pm
by pcmattman
First, use the "code" tag, it makes code easier to read.

Code: Select all

global _gdt_flush
extern _gp
_gdt_flush:
lgdt [_gp]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:flush2
flush2:
ret
What happens here is the GDT is loaded into the CPU GDTR (see the intel manuals for information about htis) and then the segments are loaded with 0x10 - which is typically the kernel data segment. Then,

Code: Select all

jmp 0x08:flush2
is written to load CS from the GDT. You can't manually modify CS, and the only way the CPU will load it's attributes (limit, flags, etc...) is by performing this action to reset the code segment.