Hi,
Mike wrote:AFAIK, once in long mode, no 16-bit protected mode code is supported at all. The bit in the GDT entry used to specify 16-bit code is used under long mode to specify 64-bit code. Therefore, 16-bit protected mode cannot exist.
16-bit pmode _does_ exist on x64 machines before long mode is enabled.
How confused can you be? Let me count the ways....
Once upon a time there was no protected mode (8086, 80186, etc). Then Intel decided to introduce protected mode (probably due to 1 MB memory barriers, but maybe influenced by other factors). This was "16 bit Protected Mode" and was introduced with the
80286!
Read this carefully, because it can be hard to understand (and yes I do mean the 80
286).
16 bit Protected Mode used a GDT and had segment bases and limits, and also supported hardware task switching, but it was before anything was 32 bit (no EAX, no paging, no virtual 8086, etc). Intel also messed it up because they didn't provide a way to switch back into real mode. Instead you had to set a value in the CMOS and reset (using the keyboard controller) which was ugly and slow.
This 16 bit Protected Mode is what Microsoft used for old versions of Windows (e.g. Windows 3.0).
Anyway, one day Intel got bored and invented "32 bit Protected Mode", releasing it with the 80386. This is what we are all mostly familiar with
.
32 bit Protected Mode was much better, and allowed you to run 16 bit code and 32 bit code (and added paging, virtual 8086, etc). Microsoft eventually released Windows 95, and everything was good (or good enough), except for compatibility problems with earlier "16 bit Protected Mode" applications.
To solve this, Intel's 80386 "32 bit Protected Mode" supported barely enough old 16 bit Protected Mode to allow old software to work. This includes the 16 bit TSS structure and nothing else (because everything else can be done with 32 bit Protected Mode, including running 16 bit binaries). All Microsoft did was add the "80286 style" API to the new 32 bit OS (Windows 95). The 16 bit TSS structure still lingers on as a lonely reminder of what once was (like a fart in an elevator that still smells weeks later, or the gate A20 thing).
Anyway, when "16 bit Protected Mode" was first released no-one really bothered using it (it was crap and "32 bit Protected Mode" came soon after). I'm not surprised that after about 20 years Microsoft are dumping it (although I'm surprised it took so long).
Then came "Long Mode"...
Long Mode still has 2 "sub-modes", as explained above in my earlier post (in the quote from the AMD manual).
In the "64 bit" sub-mode the GDT isn't used much at all. Instead there's MSRs for the 64 bit base address for GS and FS (MSR 0xC0000100 and MSR 0xC0000101). Of course the base addresses for CS, DS, ES and SS are zero (and none of them have any limit at all).
In Long Mode, a code segment descriptor (in the GDT or LDT) uses bit 21 to determine if it's a "64 bit Long Mode" code segment or a "Compatibility Long Mode" code segment. If you do some cross-checking you'll find that this bit is reserved (must be zero) on older CPUs.
Basically, they've used this previously reserved bit 21 and called it "L", but the original default size bit (or bit 22, called 'D') still selects the default operand size. My AMD manual also says that if the 'L' bit is set, then the 'D' bit must be clear (that combination might be good for future 128 bit code ::)).
This gives 3 possible code types for long mode:
[tt] L = 1, D = 1 - A 64 bit code segment (the 64 bit sub-mode of long mode)
L = 0, D = 1 - A 32 bit code segment (the compatibility sub-mode of long mode)
L = 0, D = 0 - A 16 bit code segment (the compatibility sub-mode of long mode)[/tt]
Now, an operating system that is designed for Long Mode can switch between 64 bit "64 bit long mode" software and 32 bit "compatibility long mode" software and 16 bit "compatibility long mode" software just by loading CS. This means that an OS designed for Long Mode could even allow 64 bit code, 32 bit code and 16 bit code to be mixed in the same application
.
Of course all of the above is
Long Mode and should not be confused with
Legacy Mode. Legacy mode is the same as 32 bit Protected Mode, with virtual 8086 and everything else we're all used to.
Anyway, "Long Mode"
can run 16 bit software designed for 32 bit Protected Mode (and might run 16 bit software designed for 16 bit Protected Mode, but definately won't run real mode code designed 8086).
I hope this all doesn't confuse things more...
Cheers,
Brendan