GDT Tutorial: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
m The limit field of the TSS segment descriptor should be equal to the size of the TSS minus one according to [IA32-v3 TSS Descriptor]
m Bot: Replace deprecated source tag with syntaxhighlight
Line 162: Line 162:
The linear address should here be computed as <tt>segment * 16 + offset</tt>. <tt>GDT</tt> and <tt>GDT_end</tt> are assumed to be symbols in the current data segment.
The linear address should here be computed as <tt>segment * 16 + offset</tt>. <tt>GDT</tt> and <tt>GDT_end</tt> are assumed to be symbols in the current data segment.


<source lang="asm">
<syntaxhighlight lang="asm">
gdtr DW 0 ; For limit storage
gdtr DW 0 ; For limit storage
DD 0 ; For base storage
DD 0 ; For base storage
Line 177: Line 177:
LGDT [gdtr]
LGDT [gdtr]
RET
RET
</syntaxhighlight>
</source>


==== Protected Mode, Flat Model ====
==== Protected Mode, Flat Model ====
Line 183: Line 183:
"Flat" meaning the base of your Data Segment is 0 (regardless of whether '''[[Paging]]''' is enabled). This is the case if your code has just been booted by [[GRUB]], for instance. In the '''[[System V ABI]]''', arguments are passed on reverse order in the stack, so a function that can be called as <tt>setGdt(limit, base)</tt> might look like the following example code.
"Flat" meaning the base of your Data Segment is 0 (regardless of whether '''[[Paging]]''' is enabled). This is the case if your code has just been booted by [[GRUB]], for instance. In the '''[[System V ABI]]''', arguments are passed on reverse order in the stack, so a function that can be called as <tt>setGdt(limit, base)</tt> might look like the following example code.


<source lang="asm">
<syntaxhighlight lang="asm">
gdtr DW 0 ; For limit storage
gdtr DW 0 ; For limit storage
DD 0 ; For base storage
DD 0 ; For base storage
Line 194: Line 194:
LGDT [gdtr]
LGDT [gdtr]
RET
RET
</syntaxhighlight>
</source>


==== Protected Mode, Non-Flat Model ====
==== Protected Mode, Non-Flat Model ====
Line 200: Line 200:
If your data segment has a non-zero base, you'll have to adjust the instructions of the sequence above to include the ability to add the base offset of your data segment, which should be a known value to you. You can pass it in as an argument and call this function as <tt>setGdt(limit, base, offset)</tt>.
If your data segment has a non-zero base, you'll have to adjust the instructions of the sequence above to include the ability to add the base offset of your data segment, which should be a known value to you. You can pass it in as an argument and call this function as <tt>setGdt(limit, base, offset)</tt>.


<source lang="asm">
<syntaxhighlight lang="asm">
gdtr DW 0 ; For limit storage
gdtr DW 0 ; For limit storage
DD 0 ; For base storage
DD 0 ; For base storage
Line 212: Line 212:
LGDT [gdtr]
LGDT [gdtr]
RET
RET
</syntaxhighlight>
</source>


==== Long Mode ====
==== Long Mode ====
Line 218: Line 218:
In '''[[Long Mode]]''', the length of the '''Base''' field is 8 bytes, rather than 4. As well, the '''[[System V ABI]]''' passes the first two arguments via the '''RDI''' and '''RSI''' registers. Thus, this example code can be called as <tt>setGdt(limit, base)</tt>. As well, only a flat model is possible in long mode, so no considerations have to be made otherwise.
In '''[[Long Mode]]''', the length of the '''Base''' field is 8 bytes, rather than 4. As well, the '''[[System V ABI]]''' passes the first two arguments via the '''RDI''' and '''RSI''' registers. Thus, this example code can be called as <tt>setGdt(limit, base)</tt>. As well, only a flat model is possible in long mode, so no considerations have to be made otherwise.


<source lang="asm">
<syntaxhighlight lang="asm">
gdtr DW 0 ; For limit storage
gdtr DW 0 ; For limit storage
DQ 0 ; For base storage
DQ 0 ; For base storage
Line 227: Line 227:
LGDT [gdtr]
LGDT [gdtr]
RET
RET
</syntaxhighlight>
</source>


=== Reload Segment Registers ===
=== Reload Segment Registers ===
Line 237: Line 237:
In this case, reloading '''CS''' is as simple as performing a far jump to the required segment, directly after the jump instruction:
In this case, reloading '''CS''' is as simple as performing a far jump to the required segment, directly after the jump instruction:


<source lang="asm">
<syntaxhighlight lang="asm">
reloadSegments:
reloadSegments:
; Reload CS register containing code selector:
; Reload CS register containing code selector:
Line 250: Line 250:
MOV SS, AX
MOV SS, AX
RET
RET
</syntaxhighlight>
</source>


An explanation of the above code can be found [http://stackoverflow.com/questions/23978486/far-jump-in-gdt-in-bootloader here].
An explanation of the above code can be found [http://stackoverflow.com/questions/23978486/far-jump-in-gdt-in-bootloader here].