I'm confusing with the use of GDT at Entering Long Mode Directly(http://wiki.osdev.org/Entering_Long_Mode_Directly), it was said that in long mode, all segment registers(except gs,fs) are 0.
#Why the code lgdt?
#What the role of GDT at long mode (Segment Disabled)?
#Can I enter the long mode without GDT?
Thanks!
[Solved]GDT in long mode
[Solved]GDT in long mode
Last edited by leyley on Mon Mar 25, 2013 9:25 pm, edited 1 time in total.
Code: Select all
#rm -rf /
Re: GDT in long mode
Hi,
Segment registers have a hidden part (base, limit, attributes) and a visible part (their actual value).
In protected mode or long mode, loading a value into the visible part of a segment register causes data (base, limit and attributes) to be loaded from the GDT or LDT into the hidden part of the segment register. Of course there are protection checks before this happens (e.g. is the visible part zero, is the GDT or LDT entry sane, etc).
In protected mode you can run 16-bit code or 32-bit code; and there's a bit in the attributes for CS that determine which.
In long mode you can run 16-bit code or 32-bit code or 64-bit code; and there's 2 bits in the attributes for CS that determine which.
If you're running 16-bit code or 32-bit code (in protected mode or long mode) the CPU uses the hidden base and limit information. If you're running 64-bit code (which is only possible in long mode) the CPU ignores the hidden base and limit information (and only uses the attributes).
You can enter long mode without a GDT. However; in this case you can't load any segment registers (as there's nowhere to load them from) and you'd be stuck with the base, limit and attributes that happened to be left in the hidden parts of the segment registers (e.g. if you were in real mode beforehand, you'd be stuck with 16-bit code and 64 KiB segment limits in long mode). Note that all interrupts (including exceptions) load CS from somewhere, so being unable to load any segment register also means that you can't have any interrupts or exceptions.
Normally you want to run 64-bit code in long mode (there's no reason to bother with long mode if you only want to run 16-bit or 32-bit code); and to run 64-bit code you must have a GDT so that you can load a "64-bit code descriptor" into CS.
Cheers,
Brendan
Segment registers have a hidden part (base, limit, attributes) and a visible part (their actual value).
In protected mode or long mode, loading a value into the visible part of a segment register causes data (base, limit and attributes) to be loaded from the GDT or LDT into the hidden part of the segment register. Of course there are protection checks before this happens (e.g. is the visible part zero, is the GDT or LDT entry sane, etc).
In protected mode you can run 16-bit code or 32-bit code; and there's a bit in the attributes for CS that determine which.
In long mode you can run 16-bit code or 32-bit code or 64-bit code; and there's 2 bits in the attributes for CS that determine which.
If you're running 16-bit code or 32-bit code (in protected mode or long mode) the CPU uses the hidden base and limit information. If you're running 64-bit code (which is only possible in long mode) the CPU ignores the hidden base and limit information (and only uses the attributes).
You can enter long mode without a GDT. However; in this case you can't load any segment registers (as there's nowhere to load them from) and you'd be stuck with the base, limit and attributes that happened to be left in the hidden parts of the segment registers (e.g. if you were in real mode beforehand, you'd be stuck with 16-bit code and 64 KiB segment limits in long mode). Note that all interrupts (including exceptions) load CS from somewhere, so being unable to load any segment register also means that you can't have any interrupts or exceptions.
Normally you want to run 64-bit code in long mode (there's no reason to bother with long mode if you only want to run 16-bit or 32-bit code); and to run 64-bit code you must have a GDT so that you can load a "64-bit code descriptor" into CS.
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Re: GDT in long mode
Note that some combination of the attributes are reserved (e.g. CS.L=1 & CS.D=1), so the cpu may not enter long mode if the leftover values are not sane.Brendan wrote:You can enter long mode without a GDT. However; in this case you can't load any segment registers (as there's nowhere to load them from) and you'd be stuck with the base, limit and attributes that happened to be left in the hidden parts of the segment registers
Furthermore, most attribute values must be sane in order for the cpu to continue to run, this demand either exceptional luck (without GDT) or just a proper initialization (with GDT).
Re: [Solved]GDT in long mode
The most accurate and clear replies I've seem in this forum.
I really appreciate your helps!
I really appreciate your helps!
Code: Select all
#rm -rf /