Skip to contentSkip to conversation

tools/hvmloader: Decouple APIC IDs from vCPU IDs

PATCH In review Alejandro Vallejo 2 participants 5 messages Feb 4, 2025 – Mar 10, 2025

lore
Share & tools
lore
#0 lore raw

v3 cover tools/hvmloader: Decouple APIC IDs from vCPU IDs

v2->v3:
* Moved "extern uint32_t *cpu_to_apicid;" to patch1

v2: https://lore.kernel.org/xen-devel/[email protected]/T/#t
v1->v2:
* Dropped patch to skip writing the MP Tables if apicid >= 255

v1: https://lore.kernel.org/xen-devel/[email protected]/
source series: https://lore.kernel.org/xen-devel/[email protected]/

The hypervisor, hvmloader and the toolstack currently engage in a shared assumption that for every vCPU apicid == 2 * vcpuid. This series removes such assumption from hvmloader, by making it read the APIC ID of each vCPU and storing it for later use.

Alejandro Vallejo (2):
tools/hvmloader: Retrieve APIC IDs from the APs themselves tools/hvmloader: Replace LAPIC_ID() with cpu_to_apicid[]

 tools/firmware/hvmloader/config.h    |  3 +-
 tools/firmware/hvmloader/hvmloader.c |  6 ++--
 tools/firmware/hvmloader/mp_tables.c |  2 +-
 tools/firmware/hvmloader/smp.c       | 43 +++++++++++++++++++++++++++-
 tools/firmware/hvmloader/util.c      |  2 +-
 5 files changed, 49 insertions(+), 7 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/2 tools/hvmloader: Retrieve APIC IDs from the APs themselves

Make it so the APs expose their own APIC IDs in a lookup table (LUT). We can use that LUT to populate the MADT, decoupling the algorithm that relates CPU IDs and APIC IDs from hvmloader.

Modified the printf to also print the APIC ID of each CPU, as well as fixing a (benign) wrong specifier being used for the vcpu id.

---
v2->v3:
* Moved extern from patch2 into patch1 for (transient) MISRA compliance.

v1->v2:
* Removed "(x2)" from the comment of cpu_to_apicid.
* Added "APIC ID" to the printed string on AP boot up.

Changes from the v7 version of this patch in the longer topology series:
* s/cpu_to_x2apicid/cpu_to_apicid/
* Though, as I already stated, I don't think this is a good idea.
* Dynamically size cpu_to_apicid rather than using HVM_MAX_VCPUS.
* Got rid of the ap_callin removal. It's not as trivial if we don't want to assume cpu0 always has apicid=0. Part of the complaints on the previous versions involved the inability to do that.
* For debugging sanity, I've added the apicid to the CPU boot printf.
* Later on, toolstack will choose the APIC IDs and it's helpful to know the relationship in the logs.
* While at it, fix the vcpu specifier s/%d/%u/
* Check for leaf 0xb while probing for x2apic support.

---
 tools/firmware/hvmloader/config.h |  2 ++
 tools/firmware/hvmloader/smp.c    | 43 ++++++++++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 1 deletion(-)
tools/firmware/hvmloader/config.h +2 -0
index cd716bf39245..b32bcbf4a1f2 100644
@@ -48,6 +48,8 @@ extern uint8_t ioapic_version;
 
 #define IOAPIC_ID           0x01
 
+extern uint32_t *cpu_to_apicid;
+
 #define LAPIC_BASE_ADDRESS  0xfee00000
 #define LAPIC_ID(vcpu_id)   ((vcpu_id) * 2)
 
tools/firmware/hvmloader/smp.c +42 -1

59 lines not on this page · raw message

Signature · 1 line
2.48.1
#2 lore raw

PATCH v3 2/2 tools/hvmloader: Replace LAPIC_ID() with cpu_to_apicid[]

In reply to Alejandro Vallejo

Replace uses of the LAPIC_ID() macro with accesses to the cpu_to_apicid[] lookup table. This table contains the APIC IDs of each vCPU as probed at runtime rather than assuming a predefined relation.

Moved smp_initialise() ahead of apic_setup() in order to initialise cpu_to_apicid ASAP and avoid using it uninitialised. Note that bringing up the APs doesn't need the APIC in hvmloader becasue it always runs virtualized and uses the PV interface.

---
v2->v3:
* Moved extern in config.h to patch1 for (transient) MISRA compliance.

v1->v2:
* No changes

Changes wrt original series
* No changes (it was wrongly stated in v1 that something did. That was
part
of the following patch)

---
 tools/firmware/hvmloader/config.h    | 1 -
 tools/firmware/hvmloader/hvmloader.c | 6 +++---
 tools/firmware/hvmloader/mp_tables.c | 2 +-
 tools/firmware/hvmloader/util.c      | 2 +-
 4 files changed, 5 insertions(+), 6 deletions(-)
tools/firmware/hvmloader/config.h +0 -1
index b32bcbf4a1f2..6e1da137d779 100644
@@ -51,7 +51,6 @@ extern uint8_t ioapic_version;
 extern uint32_t *cpu_to_apicid;
 
 #define LAPIC_BASE_ADDRESS  0xfee00000
-#define LAPIC_ID(vcpu_id)   ((vcpu_id) * 2)
 
 #define PCI_ISA_DEVFN       0x08    /* dev 1, fn 0 */
 #define PCI_ISA_IRQ_MASK    0x0c20U /* ISA IRQs 5,10,11 are PCI connected */
tools/firmware/hvmloader/hvmloader.c +3 -3
index f8af88fabf24..4e330fc1e241 100644
@@ -224,7 +224,7 @@ static void apic_setup(void)
 
     /* 8259A ExtInts are delivered through IOAPIC pin 0 (Virtual Wire Mode). */
     ioapic_write(0x10, APIC_DM_EXTINT);
-    ioapic_write(0x11, SET_APIC_ID(LAPIC_ID(0)));
+    ioapic_write(0x11, SET_APIC_ID(cpu_to_apicid[0]));
 }
 
 struct bios_info {
@@ -341,11 +341,11 @@ int main(void)
 
     printf("CPU speed is %u MHz\n", get_cpu_mhz());
 
+    smp_initialise();
+
     apic_setup();
     pci_setup();
 
-    smp_initialise();
-
     perform_tests();
 
     if ( bios->bios_info_setup )
tools/firmware/hvmloader/mp_tables.c +1 -1
index 77d3010406d0..3c93a5c947d9 100644
@@ -199,7 +199,7 @@ static void fill_mp_config_table(struct mp_config_table *mpct, int length)
 static void fill_mp_proc_entry(struct mp_proc_entry *mppe, int vcpu_id)
 {
     mppe->type = ENTRY_TYPE_PROCESSOR;
-    mppe->lapic_id = LAPIC_ID(vcpu_id);
+    mppe->lapic_id = cpu_to_apicid[vcpu_id];
     mppe->lapic_version = 0x11;
     mppe->cpu_flags = CPU_FLAG_ENABLED;
     if ( vcpu_id == 0 )
tools/firmware/hvmloader/util.c +1 -1
index d3b3f9038e64..2d07ce129013 100644
@@ -827,7 +827,7 @@ static void acpi_mem_free(struct acpi_ctxt *ctxt,
 
 static uint32_t acpi_lapic_id(unsigned cpu)
 {
-    return LAPIC_ID(cpu);
+    return cpu_to_apicid[cpu];
 }
 
 void hvmloader_acpi_build_tables(struct acpi_config *config,
Signature · 1 line
2.48.1
Acked-by on the v3 series
#3 lore raw
In reply to Alejandro Vallejo
Quoted 10 lines · Alejandro Vallejo

v2->v3:
* Moved "extern uint32_t *cpu_to_apicid;" to patch1

v2: https://lore.kernel.org/xen-devel/[email protected]/T/#t
v1->v2:
* Dropped patch to skip writing the MP Tables if apicid >= 255

v1: https://lore.kernel.org/xen-devel/[email protected]/
source series: https://lore.kernel.org/xen-devel/[email protected]/

Quoted 8 lines

The hypervisor, hvmloader and the toolstack currently engage in a shared assumption that for every vCPU apicid == 2 * vcpuid. This series removes such assumption from hvmloader, by making it read the APIC ID of each vCPU and storing it for later use.

Alejandro Vallejo (2):
tools/hvmloader: Retrieve APIC IDs from the APs themselves tools/hvmloader: Replace LAPIC_ID() with cpu_to_apicid[]

#4 lore raw
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Alejandro Vallejo

v2->v3:
* Moved "extern uint32_t *cpu_to_apicid;" to patch1

v2: https://lore.kernel.org/xen-devel/[email protected]/T/#t
v1->v2:
* Dropped patch to skip writing the MP Tables if apicid >= 255

v1: https://lore.kernel.org/xen-devel/[email protected]/
source series: https://lore.kernel.org/xen-devel/[email protected]/

The hypervisor, hvmloader and the toolstack currently engage in a shared assumption that for every vCPU apicid == 2 * vcpuid. This series removes such assumption from hvmloader, by making it read the APIC ID of each vCPU and storing it for later use.

Quoted 5 lines
Quoted 3 lines

Alejandro Vallejo (2):
tools/hvmloader: Retrieve APIC IDs from the APs themselves tools/hvmloader: Replace LAPIC_ID() with cpu_to_apicid[]

Cheers!

Alejandro