Skip to contentSkip to conversation

x86: add a new SMP bring up way for tboot case

PATCH In review Qiaowei Ren 4 participants 8 messages May 14, 2013 – May 15, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: add a new SMP bring up way for tboot case

tboot provides a better AP wakeup mechanism based on cpu MWAIT feature for OS/VMM. With this mechanism, system will boot faster and will NOT require VT to be enabled. But it requires that OS/VMM must have support it, otherwise system can never boot up.

Once this mechanism is enabled, tboot will put APs waiting in MWAIT loops before launching kernel. kernel can check the new flag field in v6 tboot shared page for the hint. If the bit TB_FLAG_AP_WAKE_SUPPORT in flag field is set, kernel BSP has to write the monitored memory (tboot->ap_wake_trigger) to bring APs out of MWAIT loops. The sipi vector should be written in tboot->ap_wake_addr before waking up APs.

---
 arch/x86/kernel/smpboot.c |   21 ++++++++++-----------
 arch/x86/kernel/tboot.c   |   36 ++++++++++++++++++++++++++++++++++++
 include/linux/tboot.h     |   17 +++++++++++++++++
 3 files changed, 63 insertions(+), 11 deletions(-)
arch/x86/kernel/smpboot.c +10 -11
index 9f190a2..07f979e 100644
@@ -787,17 +787,16 @@ static int __cpuinit do_boot_cpu(int apicid, int cpu, struct task_struct *idle)
 		}
 	}
 
-	/*
-	 * Wake up a CPU in difference cases:
-	 * - Use the method in the APIC driver if it's defined
-	 * Otherwise,
-	 * - Use an INIT boot APIC message for APs or NMI for BSP.
-	 */
-	if (apic->wakeup_secondary_cpu)
-		boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
-	else
-		boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
-						     &cpu0_nmi_registered);
+	if (!tboot_wake_up(apicid, start_ip)) {
+		/*
+		 * Kick the secondary CPU. Use the method in the APIC driver
+		 * if it's defined - or use an INIT boot APIC message otherwise:
+		 */
+		if (apic->wakeup_secondary_cpu)
+			boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
+		else
+			boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
+	}
 
 	if (!boot_error) {
 		/*
arch/x86/kernel/tboot.c +36 -0
index f84fe00..0ec1947 100644
@@ -101,6 +101,8 @@ void __init tboot_probe(void)
 	pr_debug("shutdown_entry: 0x%x\n", tboot->shutdown_entry);
 	pr_debug("tboot_base: 0x%08x\n", tboot->tboot_base);
 	pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
+	if (tboot->version >= 6)
+		pr_info("flags: 0x%08x\n", tboot->flags);
 }
 
 static pgd_t *tboot_pg_dir;
@@ -453,3 +455,37 @@ int tboot_force_iommu(void)
 
 	return 1;
 }
+
+/*
+ * tboot provides a better AP wakeup mechanism based on cpu MWAIT
+ * feature for OS/VMM. This mechanism is defaultly disabled, and
+ * could be enabled with tboot command line option:
+ *	ap_wake_mwait=true|false
+ *
+ * With this mechanism, system will boot faster and will NOT require
+ * VT to be enabled. But it requires that OS/VMM must have support
+ * it, otherwise system can never boot up.
+ *
+ * Once this mechanism is enabled, tboot will put APs waiting in
+ * MWAIT loops before launching kernel. kernel can check the new
+ * flag field in v6 tboot shared page for the hint. If the bit
+ * TB_FLAG_AP_WAKE_SUPPORT in flag field is set, kernel BSP has
+ * to write the monitored memory (tboot->ap_wake_trigger) to bring
+ * APs out of MWAIT loops. The sipi vector should be written in
+ * tboot->ap_wake_addr before waking up APs.
+ */
+bool tboot_wake_up(int apicid, unsigned long sipi_vec)
+{
+	if (!tboot_enabled())
+		return false;
+
+	if ((tboot->version < 6) ||
+		!(tboot->flags & TB_FLAG_AP_WAKE_SUPPORT))
+		return false;
+
+	tboot->ap_wake_addr = sipi_vec;
+	tboot->ap_wake_trigger = apicid;
+
+	return true;
+}
+
include/linux/tboot.h +17 -0
index c75128b..163d779 100644
@@ -124,8 +124,23 @@ struct tboot {
 
 	/* number of processors in wait-for-SIPI */
 	u32 num_in_wfs;
+
+	/*
+	 * version 6+ fields:
+	 */
+
+	u32 flags;
+
+	/* phys addr of kernel/VMM SIPI vector */
+	u64 ap_wake_addr;
+
+	/* kernel/VMM writes APIC ID to wake AP */
+	u32 ap_wake_trigger;
 } __packed;
 
+/* kernel/VMM use INIT-SIPI-SIPI if clear, ap_wake_* if set */
+#define TB_FLAG_AP_WAKE_SUPPORT 0X00000001
+
 /*
  * UUID for tboot data struct to facilitate matching
  * defined as {663C8DFF-E8B3-4b82-AABF-19EA4D057A08} by tboot, which is
@@ -146,6 +161,7 @@ extern void tboot_shutdown(u32 shutdown_type);
 extern struct acpi_table_header *tboot_get_dmar_table(
 				      struct acpi_table_header *dmar_tbl);
 extern int tboot_force_iommu(void);
+extern bool tboot_wake_up(int apicid, unsigned long sipi_vec);
 
 #else
 
@@ -156,6 +172,7 @@ extern int tboot_force_iommu(void);
 					do { } while (0)
 #define tboot_get_dmar_table(dmar_tbl)	(dmar_tbl)
 #define tboot_force_iommu()		0
+#define tboot_wake_up(apicid, sipi_vec) 0
 
 #endif /* !CONFIG_INTEL_TXT */
 
Signature · 1 line
1.7.9.5
maintainer
#1 lore raw
Quoted 10 lines · Qiaowei Ren

tboot provides a better AP wakeup mechanism based on cpu MWAIT feature for OS/VMM. With this mechanism, system will boot faster and will NOT require VT to be enabled. But it requires that OS/VMM must have support it, otherwise system can never boot up.

Once this mechanism is enabled, tboot will put APs waiting in MWAIT loops before launching kernel. kernel can check the new flag field in v6 tboot shared page for the hint. If the bit TB_FLAG_AP_WAKE_SUPPORT in flag field is set, kernel BSP has to write the monitored memory (tboot->ap_wake_trigger) to bring

Now this is a cool way to wake up an AP. It can't get any simpler. :-)

Quoted 49 lines

APs out of MWAIT loops. The sipi vector should be written in tboot->ap_wake_addr before waking up APs.

---
 arch/x86/kernel/smpboot.c |   21 ++++++++++-----------
 arch/x86/kernel/tboot.c   |   36 ++++++++++++++++++++++++++++++++++++
 include/linux/tboot.h     |   17 +++++++++++++++++
 3 files changed, 63 insertions(+), 11 deletions(-)
arch/x86/kernel/smpboot.c +10 -11
index 9f190a2..07f979e 100644
@@ -787,17 +787,16 @@ static int __cpuinit do_boot_cpu(int apicid, int cpu, struct task_struct *idle)
 		}
 	}
 
-	/*
-	 * Wake up a CPU in difference cases:
-	 * - Use the method in the APIC driver if it's defined
-	 * Otherwise,
-	 * - Use an INIT boot APIC message for APs or NMI for BSP.
-	 */
-	if (apic->wakeup_secondary_cpu)
-		boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
-	else
-		boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
-						     &cpu0_nmi_registered);
+	if (!tboot_wake_up(apicid, start_ip)) {
+		/*
+		 * Kick the secondary CPU. Use the method in the APIC driver
+		 * if it's defined - or use an INIT boot APIC message otherwise:
+		 */
+		if (apic->wakeup_secondary_cpu)
+			boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
+		else
+			boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
+	}
 
 	if (!boot_error) {
 		/*
arch/x86/kernel/tboot.c +0 -0
index f84fe00..0ec1947 100644

arch/x86/kernel/tboot.c void __init tboot_probe(void) · line 105

Quoted 5 lines
arch/x86/kernel/tboot.c +2 -0
@@ -101,6 +101,8 @@ void __init tboot_probe(void)
 	pr_debug("shutdown_entry: 0x%x\n", tboot->shutdown_entry);
 	pr_debug("tboot_base: 0x%08x\n", tboot->tboot_base);
 	pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
+	if (tboot->version >= 6)
+		pr_info("flags: 0x%08x\n", tboot->flags);

This should be pr_debug like the rest, no?

Signature · 5 lines
Regards/Gruss,
    Boris.

Sent from a fat crate under my desk. Formatting is fine.
--
#2 lore raw
Quoted 13 lines · Borislav Petkov

arch/x86/kernel/tboot.c void __init tboot_probe(void) · line 105

Quoted 10 lines · Qiaowei Ren
arch/x86/kernel/tboot.c index renamed from arch/x86/kernel/tboot.c +0 -0

f84fe00..0ec1947 100644

arch/x86/kernel/tboot.c +2 -0
@@ -101,6 +101,8 @@ void __init tboot_probe(void)
 	pr_debug("shutdown_entry: 0x%x\n", tboot->shutdown_entry);
 	pr_debug("tboot_base: 0x%08x\n", tboot->tboot_base);
 	pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
+	if (tboot->version >= 6)
+		pr_info("flags: 0x%08x\n", tboot->flags);

This should be pr_debug like the rest, no?

Well. pr_debug is ok.

Thanks,
Qiaowei ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#3 lore raw
In reply to Qiaowei Ren
Quoted 5 lines · Qiaowei Ren

tboot provides a better AP wakeup mechanism based on cpu MWAIT feature for OS/VMM. With this mechanism, system will boot faster and will NOT require VT to be enabled. But it requires that OS/VMM must have support it, otherwise system can never boot up.

Quoted 8 lines

Once this mechanism is enabled, tboot will put APs waiting in MWAIT loops before launching kernel. kernel can check the new flag field in v6 tboot shared page for the hint. If the bit TB_FLAG_AP_WAKE_SUPPORT in flag field is set, kernel BSP has to write the monitored memory (tboot->ap_wake_trigger) to bring APs out of MWAIT loops. The sipi vector should be written in tboot->ap_wake_addr before waking up APs.

This really needs a *detailed* specification about the state the CPU is parked in. Most BIOSes do in fact park the CPUs in an mwait loop, but we can't use it because the CPU state they are parked in is ill-defined.

This is a good idea, but please write (or point to) a spec about what the parked CPU state looks like and how the OS gets control. From the *looks* of the code I assume it is entered in 16-bit real mode but then it is important to know what parts of the register state are well-defined.

Quoted 1 line

+#define TB_FLAG_AP_WAKE_SUPPORT 0X00000001

Minor nit: please lower-case the hexadecimal x.

-hpa

#4 lore raw
Quoted 17 lines · H. Peter Anvin
Quoted 5 lines · Qiaowei Ren

tboot provides a better AP wakeup mechanism based on cpu MWAIT feature for OS/VMM. With this mechanism, system will boot faster and will NOT require VT to be enabled. But it requires that OS/VMM must have support it, otherwise system can never boot up.

Quoted 10 lines

Once this mechanism is enabled, tboot will put APs waiting in MWAIT loops before launching kernel. kernel can check the new flag field in
v6 tboot shared page for the hint. If the bit TB_FLAG_AP_WAKE_SUPPORT in flag field is set, kernel BSP has to write the monitored memory
(tboot->ap_wake_trigger) to bring APs out of MWAIT loops. The sipi vector should be written in
tboot->ap_wake_addr before waking up APs.

Quoted 8 lines

This really needs a *detailed* specification about the state the CPU is parked in. Most BIOSes do in fact park the CPUs in an mwait loop, but we can't use it because the CPU state they are parked in is ill-defined.

This is a good idea, but please write (or point to) a spec about what the parked CPU state looks like and how the OS gets control. From the *looks* of the code I assume it is entered in 16-bit real mode but then it is important to know what parts of the register state are well-defined.

The following is how to do mwait for tboot & kernel:

For bootstrap processor (BSP), "tboot TXT pre-launch" is executed after BIOS. In this stage, tboot will issue GETSEC[SENTER], which broadcasts messages to the chipset and other physical or logical processors in the platform. In response, other logical processors perform basic cleanup and other tasks, and then finally enter SENTER sleep state.

Next, for BSP, SINIT will run and then enter "tboot post-launch", which will start all sleeping APs. If tboot command line option " ap_wake_mwait=true" is set, APs will do some work and then enter mwait loop. Kernel will be launched in BSP by tboot post-launch, and bring APs out of mwait loop.

Tboot works in protected mode (but paging is disabled), and closes interrupt. For APs, MONITOR and MWAIT related code in tboot is as follows:

    while ( _tboot_shared.ap_wake_trigger != cpuid ) {
        cpu_monitor(&_tboot_shared.ap_wake_trigger, 0, 0);
        mb();
        if ( _tboot_shared.ap_wake_trigger == cpuid )
            break;
        cpu_mwait(0, 0);
    }

Their extension and hint are all 0. According Intel manual:
Extension=0: Treat interrupts as break events even if masked (e.g., even if EFLAGS.IF=0). Hint=0: the preferred optimized state the processor should enter is C0.
So, when "tboot->ap_wake_trigger" is set by kernel, APs can exit from mwait loop.

Peter, I don't know whether I explain your problem. What do you think about it?

Thanks,
Qiaowei

#5 lore raw

No, this does not really answer the question of what the CPU state looks like.

Quoted 67 lines · Qiaowei
Quoted 17 lines · H. Peter Anvin
Quoted 5 lines · Qiaowei Ren

tboot provides a better AP wakeup mechanism based on cpu MWAIT feature for OS/VMM. With this mechanism, system will boot faster and will NOT require VT to be enabled. But it requires that OS/VMM must have support it, otherwise system can never boot up.

Quoted 10 lines

Once this mechanism is enabled, tboot will put APs waiting in MWAIT loops before launching kernel. kernel can check the new flag field in
v6 tboot shared page for the hint. If the bit TB_FLAG_AP_WAKE_SUPPORT in flag field is set, kernel BSP has to write the monitored memory
(tboot->ap_wake_trigger) to bring APs out of MWAIT loops. The sipi vector should be written in
tboot->ap_wake_addr before waking up APs.

Quoted 1 line

This really needs a *detailed* specification about the state the CPU

is parked in.

Quoted 5 lines

Most BIOSes do in fact park the CPUs in an mwait loop, but we can't use it because the CPU state they are parked in is ill-defined.

This is a good idea, but please write (or point to) a spec about what the parked CPU state looks like and how the OS gets control. From

the

Quoted 2 lines

*looks* of the code I assume it is entered in 16-bit real mode but then it is important to know what parts of the register state are

well-defined.

The following is how to do mwait for tboot & kernel:

For bootstrap processor (BSP), "tboot TXT pre-launch" is executed after BIOS. In this stage, tboot will issue GETSEC[SENTER], which broadcasts messages to the chipset and other physical or logical processors in the platform. In response, other logical processors perform basic cleanup and other tasks, and then finally enter SENTER sleep state.

Next, for BSP, SINIT will run and then enter "tboot post-launch", which will start all sleeping APs. If tboot command line option " ap_wake_mwait=true" is set, APs will do some work and then enter mwait loop. Kernel will be launched in BSP by tboot post-launch, and bring APs out of mwait loop.

Tboot works in protected mode (but paging is disabled), and closes interrupt. For APs, MONITOR and MWAIT related code in tboot is as follows:

   while ( _tboot_shared.ap_wake_trigger != cpuid ) {
       cpu_monitor(&_tboot_shared.ap_wake_trigger, 0, 0);
       mb();
       if ( _tboot_shared.ap_wake_trigger == cpuid )
           break;
       cpu_mwait(0, 0);
   }

Their extension and hint are all 0. According Intel manual:
Extension=0: Treat interrupts as break events even if masked (e.g.,
even if EFLAGS.IF=0).
Hint=0: the preferred optimized state the processor should enter is
C0.
So, when "tboot->ap_wake_trigger" is set by kernel, APs can exit from mwait loop.

Peter, I don't know whether I explain your problem. What do you think about it?

Thanks,
Qiaowei

Signature · 1 line
Sent from my mobile phone. Please excuse brevity and lack of formatting.
#6 lore raw

Oh, the question should be in what states all the AP CPU registers are. Do you think we need to define the case like what Intel SDM defined for getsec[SENTER]?

Jimmy

H. Peter Anvin wrote on 2013-05-15:

Quoted 72 lines

No, this does not really answer the question of what the CPU state looks like.

Quoted 66 lines · Qiaowei
Quoted 22 lines · H. Peter Anvin
Quoted 5 lines · Qiaowei Ren

tboot provides a better AP wakeup mechanism based on cpu MWAIT feature for OS/VMM. With this mechanism, system will boot faster and will NOT require VT to be enabled. But it requires that OS/VMM must have support it, otherwise system can never boot up.

Quoted 10 lines

Once this mechanism is enabled, tboot will put APs waiting in MWAIT loops before launching kernel. kernel can check the new flag field in
v6 tboot shared page for the hint. If the bit TB_FLAG_AP_WAKE_SUPPORT in flag field is set, kernel BSP has to write the monitored memory
(tboot->ap_wake_trigger) to bring APs out of MWAIT loops. The sipi vector should be written in
tboot->ap_wake_addr before waking up APs.

This really needs a *detailed* specification about the state the CPU is parked in. Most BIOSes do in fact park the CPUs in an mwait loop, but we can't use it because the CPU state they are parked in is ill-defined.

Quoted 4 lines

This is a good idea, but please write (or point to) a spec about what the parked CPU state looks like and how the OS gets control. From the *looks* of the code I assume it is entered in 16-bit real mode but then it is important to know what parts of the register state are

well-defined.

The following is how to do mwait for tboot & kernel:

For bootstrap processor (BSP), "tboot TXT pre-launch" is executed after BIOS. In this stage, tboot will issue GETSEC[SENTER], which broadcasts messages to the chipset and other physical or logical processors in the platform. In response, other logical processors perform basic cleanup and other tasks, and then finally enter SENTER sleep state.

Next, for BSP, SINIT will run and then enter "tboot post-launch", which will start all sleeping APs. If tboot command line option " ap_wake_mwait=true" is set, APs will do some work and then enter mwait loop. Kernel will be launched in BSP by tboot post-launch, and bring APs out of mwait loop.

Tboot works in protected mode (but paging is disabled), and closes interrupt. For APs, MONITOR and MWAIT related code in tboot is as follows:

   while ( _tboot_shared.ap_wake_trigger != cpuid ) {
       cpu_monitor(&_tboot_shared.ap_wake_trigger, 0, 0);
       mb();
       if ( _tboot_shared.ap_wake_trigger == cpuid )
           break;
       cpu_mwait(0, 0);
   }

Their extension and hint are all 0. According Intel manual:
Extension=0: Treat interrupts as break events even if masked (e.g.,
even if EFLAGS.IF=0).
Hint=0: the preferred optimized state the processor should enter is
C0.
So, when "tboot->ap_wake_trigger" is set by kernel, APs can exit from mwait loop.

Peter, I don't know whether I explain your problem. What do you think about it?

Thanks,
Qiaowei

Jimmy

#7 lore raw
Quoted 5 lines · Gang

Oh, the question should be in what states all the AP CPU registers are. Do you think we need to define the case like what Intel SDM defined for getsec[SENTER]?

Jimmy

I'll take a look later today.

-hpa