GDT Tutorial: Difference between revisions

slight cleanup
[unchecked revision][unchecked revision]
m (Fixed link)
(slight cleanup)
Line 76:
=== Filling the table ===
 
You noticed that i didn't gave a real structure for <tt>GDT[]</tt>, didn't you? That's on purpose. The actual structure of descriptors is somehow a nightmarelittle messy for reasons unknown. Base address are split on 3 different fields and you cannot encode any limit you want. Plus, here and there, you have flags that you need to set up properly if you want things to work.
 
<source lang="c">
/**
encodeGdtEntry(unsigned char target[8], struct GDT source)
* \param target A pointer to the 8-byte GDT entry
* \param source An arbitrary structure describing the GDT entry
*/
void encodeGdtEntry(unsigned charuint8_t *target[8], struct GDT source)
{
// Check the limit to make sure that it can be encoded
if ((source.limit > 65536) && (source.limit & 0xFFF) != 0xFFF)) {
kerror("You can't do that!");
Line 92 ⟶ 97:
}
// Encode the limit
target[0] = source.limit & 0xFF;
target[1] = (source.limit >> 8) & 0xFF;
target[6] |= (source.limit >> 16) & 0xF;
// Encode the base
target[2] = source.base & 0xFF;
target[3] = (source.base >> 8) & 0xFF;
Line 101 ⟶ 108:
target[7] = (source.base >> 24) & 0xFF;
// And... Type
target[5] = source.type;
}
</source>
 
Okay, that's rather ugly, but it's the most 'for dummies' i can come with ... hope you know about masking and shifting. You can hard-code that rather than convert it at runtime, of course. ItThis code assumes that you only want 32-bit bits stuff, too, and it's probably not valid for gates and other things i didn't talk aboutsegments.
 
=== Telling the CPU where the table stands ===
Anonymous user