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.
installing own GDT using gdt_flush() why jmp 0x08:flush2
The lgdt instruction loads a pointer to a GDT
Now lets say our GDT looks like this
Now, when you do a far jmp like
it loads the GDT-Entry from the gdt-pointer with the offset 0x08 and loads the base, limit, etc.. of the entry
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
Code: Select all
jmp 0x08:somelocation
-
- Member
- Posts: 2566
- Joined: Sun Jan 14, 2007 9:15 pm
- Libera.chat IRC: miselin
- Location: Sydney, Australia (I come from a land down under!)
- Contact:
First, use the "code" tag, it makes code easier to read.
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,
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.
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
Code: Select all
jmp 0x08:flush2