On Sat, 15 May 2021 06:42:52 -0700 (PDT)
Post by Joe MonkLet me make it simple for you...
https://www.amd.com/system/files/TechDocs/24593.pdf?print=anzwix&show=1
page 14...
"Virtual-8086 mode is not supported when the processor is operating
in long mode. When long mode is enabled, any attempt to enable
virtual-8086 mode is silently ignored."
That means it ignores attempts to set RFLAGS.VM=1.
CR4.VME and TSS32.IRB are probably ignored too.
Of course, he should be able to disable long.mode by clearing
EFER.LMA=0. This should drop him into one of PM32, PM16, VM16E1,
VM16E0, or VM16 depending on the state of RFLAGS.VM, CR4.VME,
TSS32.IRB, CS.ar.D. He would need to turn off PAE too.
https://www.sandpile.org/x86/mode.htm
Intel "9.8.5.4 Switching Out of IA-32e Mode Operation" says it's a bit
more complicated to exit long-mode than simply clearing EFER.LMA which
is read-only: (rough summary) switch to CM16/CM32, clear CR0.PG=0 which
clears EFER.LMA, then clear EFER.LME, set CR0.PG=1.
Post by Joe MonkSo, on an AMD processor, there is no switching between "real mode"
and long mode. Thus, is it simpler to write code that doesn't do
this, and runs on any processor, or do you want to exclude AMD
processors from your software?
1) CM16 is the "equivalent" in long-mode (LM64) to virtual-mode (VM16,
VM16E0, VM16E1) for protected mode (PM16, PM32). He should only need
to jump to a code segment from long-mode with the CS.ar.D and CS.ar.L
descriptor bits set correctly to select CM16 or CM32 (equivalent to
PM32).
https://www.sandpile.org/x86/mode.htm
(You made note of using CM16/CM32 later in the thread, but only after
you told him "virtual" mode wasn't available ... What are CM16 and
CM32 if not "virtual" modes for LM64? Were you being pedantic here, or
just unaware of CM16/CM32 equivalence to PM16/PM32?)
2) Unless there is a hypervisor or microkernel as part of UEFI which
forces long-mode upon any guest OSes, by maintaining control of Ring-0,
as soon as his OS code takes control from UEFI, he can switch the
processor into any processor mode of his choice as his OS has
Ring-0 control. This includes switching out of long-mode or
compatibility mode and into either real-mode or protected-mode, the
latter from which he can access virtual-mode. In other words, as long
as his OS was entered in Ring 0 from UEFI, which it seems to be
according to one of the UEFI sections mentioned below, then his OS has
total control over which processor mode is executing. (Admittedly, I
didn't read the entire UEFI specs, just a quick search and skim. So, I
have no idea if an bootx68.efi or some other .efi loader file etc acts
as a Ring-0 shim.)
A) Are we allowed to exit long-mode once entered?
According to the Intel processor manuals, you can exit IA-32e mode
(long-mode) after it has been entered. See "9.8.5.4 Switching Out of
IA-32e Mode Operation". According to the AMD processor manuals, you can
also exit long mode after is has been entered. See "14.7 Leaving Long
Mode".
So, long-mode (LM64) can be switched out of by the OS at the processor
level, and the processor can then be switched into other processor
modes. I.e., once long-mode is entered the OS isn't forced to stay in
long-mode. The OS may switch out of long-mode and into another mode,
such as real-mode.
(This is contradictory to what you said.)
B) Is there any reason an OS couldn't switch the processor into the
mode of it's choice once control from UEFI is transferred to it?
If there is a hypervisor or microkernel controlling Ring-0 for UEFI,
that would prevent his OS from switching processor modes. This would
prevent exiting long-mode to enter protected-mode or real-mode, or
virtual mode via protected-mode. If such a hypervisor or microkernel
exists for UEFI, it is not mentioned in the UEFI sections regarding the
processor state upon transfer to the OS kernel. These sections state
that the processor mode can be changed for IA-32, but there is no
mention of this ability for x64. It appears to me, that this is an
oversight, as all the other processors in the specification have the
exact same statement on changing processor modes, and also have some
more notes on ACPI etc, all of which are missing from the x64 section.
If not, why would x64 be the only processor not capable of "altering
the processor execution mode" when both AMD and Intel manuals say x64
is capable of doing so? ... I.e., there should be an explicit
statement in the x64 section explaining why the processor can't be
switched out of long-mode (LM64), if it can't be for some reason with
UEFI. In other words, what's the difference for UEFI with x64
long-mode (LM64) from the other setups?
#1) UEFI 32-bit
UEFI "2.3.2 IA-32 Platforms"
"An application written to this specification may alter the processor
execution mode, ..."
The spec says that UEFI transfers to the OS in protected mode (assuming
32-bit), optional paging, PAE recommended for paging, memory map must
be identify mapped if paging is enabled, selectors are flat, interrupts
enabled but no interrupt services are available except timer functions,
direction flag is clear, (more stuff) ...
UEFI "2.3.2.1 Handoff State"
"When a 32-bit UEFI OS is loaded, the system firmware hands off control
to the OS in flat 32-bit mode. All descriptors are set to the 4GiB
limits so that all of memory is accessible from all segments.
"... in the image's PE32+ header ..."
#2) UEFI 64-bit
UEFI "2.3.4 x64 Platforms"
The spec says that UEFI transfers to the OS in long-mode, 64-bit mode,
paging is enabled, memory define by UEFI memory map is identity mapped,
selectors are flat, interrupts are enabled but no interrupt services
are available except timer functions, direction flag is clear, (more
stuff) ...
UEFI "2.3.4.1 Handoff State"
RCX - EFI_HANDLE
RDX - EFI_SYSTEM_TABLE *
RSP - <return address>
The spec doesn't mention the 64-bit processor state here for OS entry,
but one must assume that it is just like 2.3.2.1 but for 64-bit, as
2.3.2.1 matches 2.3.2, so 2.3.4.1 should match 2.3.4.
--
"There's the abyss. We're not in the abyss. We're in the car park and
snack area adjacent to the abyss." - Bill Bailey