x86: Add ap_online() to consolidate final "AP is alive!" code

Add ap_online() to consolidate the last stage of onlining an AP CPU.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20220615232943.1465490-14-seanjc@google.com
diff --git a/lib/x86/setup.c b/lib/x86/setup.c
index 00d6e9c..7df0256 100644
--- a/lib/x86/setup.c
+++ b/lib/x86/setup.c
@@ -392,12 +392,5 @@
 	save_id();
 	enable_apic();
 	enable_x2apic();
-	sti();
-	asm volatile ("nop");
-	printf("setup: AP %d online\n", apic_id());
-	atomic_inc(&cpu_online_count);
-
-	/* Only the BSP runs the test's main(), APs are given work via IPIs. */
-	for (;;)
-		asm volatile("hlt");
+	ap_online();
 }
diff --git a/lib/x86/smp.c b/lib/x86/smp.c
index dd4eb8c..feaab7a 100644
--- a/lib/x86/smp.c
+++ b/lib/x86/smp.c
@@ -82,6 +82,18 @@
 	this_cpu_write_smp_id(apic_id());
 }
 
+void ap_online(void)
+{
+	irq_enable();
+
+	printf("setup: CPU %d online\n", apic_id());
+	atomic_inc(&cpu_online_count);
+
+	/* Only the BSP runs the test's main(), APs are given work via IPIs. */
+	for (;;)
+		asm volatile("hlt");
+}
+
 static void __on_cpu(int cpu, void (*function)(void *data), void *data, int wait)
 {
 	const u32 ipi_icr = APIC_INT_ASSERT | APIC_DEST_PHYSICAL | APIC_DM_FIXED | IPI_VECTOR;
diff --git a/lib/x86/smp.h b/lib/x86/smp.h
index 3ddc39e..08a440b 100644
--- a/lib/x86/smp.h
+++ b/lib/x86/smp.h
@@ -85,6 +85,7 @@
 void on_cpus(void (*function)(void *data), void *data);
 void smp_reset_apic(void);
 void bringup_aps(void);
+void ap_online(void);
 
 extern atomic_t cpu_online_count;
 extern unsigned char online_cpus[(MAX_TEST_CPUS + 7) / 8];
diff --git a/x86/cstart.S b/x86/cstart.S
index fdbe343..e82bed7 100644
--- a/x86/cstart.S
+++ b/x86/cstart.S
@@ -101,12 +101,10 @@
 	call save_id
 	call enable_apic
 	call enable_x2apic
-	sti
-	nop
-	lock incw cpu_online_count
+	call ap_online
 
-1:	hlt
-	jmp 1b
+	/* ap_online() should never return */
+	ud2
 
 start32:
 	setup_tr_and_percpu