Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Note that GRUB already installs a GDT for you, but if we overwrite the area of memory that GRUB was loaded to, we will trash the GDT and this will cause what is called a 'triple fault'. In short, it'll reset the machine. What we should do to prevent that problem is to set up our own GDT in a place in memory that we know and can access.
To speed up the translation of logical addresses into linear addresses, the 80 x 86 processor provides an additional nonprogrammable registerthat is, a register that cannot be set by a programmerfor each of the six programmable segmentation registers. Each nonprogrammable register contains the 8-byte Segment Descriptor (described in the previous section) specified by the Segment Selector contained in the corresponding segmentation register. Every time a Segment Selector is loaded in a segmentation register, the corresponding Segment Descriptor is loaded from memory into the matching nonprogrammable CPU register. From then on, translations of logical addresses referring to that segment can be performed without accessing the GDT or LDT stored in main memory; the processor can refer only directly to the CPU register containing the Segment Descriptor. Accesses to the GDT or LDT are necessary only when the contents of the segmentation registers change (see Figure 2-4).
According to the second text it should be safe to use the segment selectors loaded by GRUB even if the actual memory with the segment descriptors is trashed. But is it safe in reality as well?
Note that GRUB already installs a GDT for you, but if we overwrite the area of memory that GRUB was loaded to, we will trash the GDT and this will cause what is called a 'triple fault'. In short, it'll reset the machine. What we should do to prevent that problem is to set up our own GDT in a place in memory that we know and can access.
To speed up the translation of logical addresses into linear addresses, the 80 x 86 processor provides an additional nonprogrammable registerthat is, a register that cannot be set by a programmerfor each of the six programmable segmentation registers. Each nonprogrammable register contains the 8-byte Segment Descriptor (described in the previous section) specified by the Segment Selector contained in the corresponding segmentation register. Every time a Segment Selector is loaded in a segmentation register, the corresponding Segment Descriptor is loaded from memory into the matching nonprogrammable CPU register. From then on, translations of logical addresses referring to that segment can be performed without accessing the GDT or LDT stored in main memory; the processor can refer only directly to the CPU register containing the Segment Descriptor. Accesses to the GDT or LDT are necessary only when the contents of the segmentation registers change (see Figure 2-4).
According to the second text it should be safe to use the segment selectors loaded by GRUB even if the actual memory with the segment descriptors is trashed. But is it safe in reality as well?
Yeah, of course it is. Just make sure to know *everything* that goes on that might push/pop a segment register since it'll break your assumptions.
It's the theory behind unreal mode by the way - you load the segment registers in pmode (loading the second unwritable half with the pmode-valid information) which is never changed back - so if you index with eax or ecx in realmode the offsets aren't invalid (since it checks the pmode-style register just without reloading them for realmode values, so you keep the old limit and base) and you can access 4GB of information. Which also explains the BIOS warning - if your BIOS functions reload the segment register, you're screwed. They also don't use 32-bit offsets.
Craze Frog wrote:According to the second text it should be safe to use the segment selectors loaded by GRUB even if the actual memory with the segment descriptors is trashed. But is it safe in reality as well?
Only if no one will ever touch the segment registers. But in reality, this doesn't happen. You will reload the segment registers during interrupts, when switching privilege levels, etc. It would be very difficult to make sure that the segment registers are never touched.