本文以Intel SDM为主,以ACRN源码为辅来介绍Intel CPU operating modes。

1. IA-32 architecture

1.1 Protected mode

This mode is the native state of the processor. Among the capabilities of protected mode is the ability to directly execute “real-address mode” 8086 software in a protected, multi-tasking environment. This feature is called virtual-8086 mode, although it is not actually a processor mode. Virtual-8086 mode is actually a protected mode attribute that can be enabled for any task.

1.2 Real-address mode

This mode implements the programming environment of the Intel 8086 processor with extensions (such as the ability to switch to protected or system management mode). The processor is placed in real-address mode following power-up or a reset.

1.3 System management mode (SMM)

This mode provides an operating system or executive with a transparent mechanism for implementing platform-specific functions such as power management and system security. The processor enters SMM when the external SMM interrupt pin (SMI#) is activated or an SMI is received from the advanced programmable interrupt controller (APIC).

In SMM, the processor switches to a separate address space while saving the basic context of the currently running program or task. SMM-specific code may then be executed transparently. Upon returning from SMM, the processor is placed back into its state prior to the system management interrupt.

2. Intel® 64 Architecture

Intel 64 architecture adds IA-32e mode. IA-32e mode has two sub-modes.

2.1 Compatibility mode(sub-mode of IA-32e mode)

Compatibility mode permits most legacy 16-bit and 32-bit applications to run without re-compilation under a 64-bit operating system.

2.2 64-bit mode(sub-mode of IA-32e mode)

This mode enables a 64-bit operating system to run applications written to access 64-bit linear address space.

3. 状态机


4. Code

set_vcpu_mode

4.1 How to determine if protect mode?

4.2 How to determine if IA-32e mode?

注意:IA-32e mode还有一个叫法:long mode。

1
2
3
4
#define MSR_IA32_EFER_LME_BIT			(1UL << 8U)	/* IA32e mode enable */
#define MSR_IA32_EFER_LMA_BIT (1UL << 10U) /* IA32e mode active */
...
if ((ia32_efer & MSR_IA32_EFER_LMA_BIT) != 0UL)

4.3 How to determine if 64-bit mode?

1
2
3
4
5
6
if ((ia32_efer & MSR_IA32_EFER_LMA_BIT) != 0UL) {
if ((cs_attr & 0x2000U) != 0U) {
/* CS.L = 1 */
vcpu->arch.cpu_mode = CPU_MODE_64BIT;
}
}

4.4 How to determine if compatibility mode?

IA32e mode active and CS.L = 0


参考资料:

  1. x86-64处理器的几种运行模式
  2. X86 CPU的工作模式