x86: Expose consistent topology to guests
v4 cover x86: Expose consistent topology to guests
I've added the patches most critical to get into 4.19 at the head. They are pretty well reviewed already and shouldn't be very contentious anymore.
v3 -> v4:
* Fixed cpuid() bug in hvmloader, causing UB in v3
* Fixed a bogus assert in hvmloader, also causing a crash in v3
* Used HVM contexts rather than sync'd algos between Xen and toolstack in order to initialise the per-vCPU LAPIC state.
* Formatting asjustments.
v2 -> v3:
(v2/patch2 and v2/patch4 are already committed)
* Moved the vlapic check hook addition to v3/patch1
* And created a check hook for the architectural state too for consistency.
* Fixed migrations from Xen <= 4.13 by reconstructing the previous topology.
* Correctly set the APIC ID after a policy change when vlapic is already in x2APIC mode.
* Removed bogus assumption introduced in v1 and v2 on hvmloader about which 8bit APIC IDs represent ids > 254. (it's "id % 0xff", not "min(id, 0xff)".
* Used an x2apic flag check instead.
* Various formatting adjustments.
v1 -> v2:
* v1/patch 4 replaced by a different strategy (See patches 4 and 5 in v2):
* Have hvmloader populate MADT with the real APIC IDs as read by the APs themselves rather than giving it knowledge on how to derive them.
* Removed patches 2 and 3 in v1, as no longer relevant.
* Split v1/patch6 in two parts ((a) creating the generator and (b) plugging it in) and use the generator in the unit tests of the vcpuid->apicid mapping function. Becomes patches 6 and 8 in v2.
Patch 1: Same as v1/patch1.
Patch 2: Header dependency cleanup in preparation for patch3. Patch 3: Adds vlapic_hidden check for the newly introduced reserved area. Patch 4: [hvmloader] Replaces INIT+SIPI+SIPI sequences with hypercalls. Patch 5: [hvmloader] Retrieve the per-CPU APIC IDs from the APs themselves. Patch 6: Split from v1/patch6.
Patch 7: Logically matching v1/patch5, but using v2/patch6 for testing. Patch 8: Split from v1/patch6.
=== Original cover letter ===
Current topology handling is close to non-existent. As things stand, APIC IDs are allocated through the apic_id=vcpu_id*2 relation without giving any hints to the OS on how to parse the x2APIC ID of a given CPU and assuming the guest will assume 2 threads per core.
This series involves bringing x2APIC IDs into the migration stream, so older guests keep operating as they used to and enhancing Xen+toolstack so new guests get topology information consistent with their x2APIC IDs. As a side effect of this, x2APIC IDs are now packed and don't have (unless under a pathological case) gaps.
Further work ought to allow combining this topology configurations with gang-scheduling of guest hyperthreads into affine physical hyperthreads. For the time being it purposefully keeps the configuration of "1 socket" + "1 thread per core" + "1 core per vCPU".
Patch 1: Includes x2APIC IDs in the migration stream. This allows Xen to
reconstruct the right x2APIC IDs on migrated-in guests, and future-proofs itself in the face of x2APIC ID derivation changes.
Patch 2: Minor refactor to expose xc_cpu_policy in libxl
Patch 3: Refactors xen/lib/x86 to work on non-Xen freestanding environments
(e.g: hvmloader)
Patch 4: Remove old assumptions about vcpu_id<->apic_id relationship in hvmloader Patch 5: Add logic to derive x2APIC IDs given a CPU policy and vCPU IDs Patch 6: Includes a simple topology generator for toolstack so new guests
have topologically consistent information in CPUID
*** BLURB HERE ***
Alejandro Vallejo (10):
tools/hvmloader: Fix non-deterministic cpuid() x86/vlapic: Move lapic migration checks to the check hooks xen/x86: Add initial x2APIC ID to the per-vLAPIC save area tools/hvmloader: Retrieve (x2)APIC IDs from the APs themselves xen/x86: Add supporting code for uploading LAPIC contexts during
domain create
tools/libguest: Make setting MTRR registers unconditional xen/lib: Add topology generator for x86
xen/x86: Derive topologically correct x2APIC IDs from the policy xen/x86: Synthesise domain topologies
tools/libguest: Set topologically correct x2APIC IDs for each vCPU
tools/firmware/hvmloader/config.h | 6 +- tools/firmware/hvmloader/hvmloader.c | 4 +- tools/firmware/hvmloader/smp.c | 54 ++++-- tools/firmware/hvmloader/util.c | 9 - tools/firmware/hvmloader/util.h | 27 ++- tools/include/xen-tools/common-macros.h | 5 + tools/libs/guest/xg_cpuid_x86.c | 24 ++- tools/libs/guest/xg_dom_x86.c | 114 ++++++++----- tools/tests/cpu-policy/test-cpu-policy.c | 201 +++++++++++++++++++++++ xen/arch/x86/cpu-policy.c | 9 +- xen/arch/x86/cpuid.c | 14 +- xen/arch/x86/hvm/vlapic.c | 127 ++++++++++---- xen/arch/x86/include/asm/hvm/vlapic.h | 1 + xen/include/public/arch-x86/hvm/save.h | 2 + xen/include/xen/lib/x86/cpu-policy.h | 27 +++ xen/lib/x86/policy.c | 164 ++++++++++++++++++ xen/lib/x86/private.h | 4 + 17 files changed, 687 insertions(+), 105 deletions(-)
Signature · 1 line
2.34.1
PATCH v4 1/10 tools/hvmloader: Fix non-deterministic cpuid()
hvmloader's cpuid() implementation deviates from Xen's in that the value passed on ecx is unspecified. This means that when used on leaves that implement subleaves it's unspecified which one you get; though it's more than likely an invalid one.
Import Xen's implementation so there are no surprises.
---
This is a fix for a latent bug. Should go into 4.19.
v4
* New patch
--- tools/firmware/hvmloader/util.c | 9 --------- tools/firmware/hvmloader/util.h | 27 ++++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 12 deletions(-)
tools/firmware/hvmloader/util.c +0 -9
@@ -267,15 +267,6 @@ memcmp(const void *s1, const void *s2, unsigned n)return 0 ; } -void -cpuid (uint32_t idx,uint32_t *eax,uint32_t *ebx,uint32_t *ecx,uint32_t *edx) -{ - asmvolatile ( -"cpuid" - :"=a" (*eax),"=b" (*ebx),"=c" (*ecx),"=d" (*edx) - :"0" (idx) ); -} -static const char hex_digits[] ="0123456789abcdef" ;/* Write a two-character hex representation of 'byte' to digits[].
tools/firmware/hvmloader/util.h +24 -3
@@ -184,9 +184,30 @@ int uart_exists(uint16_t uart_base);int lpt_exists (uint16_t lpt_base);int hpet_exists (unsigned long hpet_base); -/* Do cpuid instruction, with operation 'idx' */ -void cpuid (uint32_t idx,uint32_t *eax,uint32_t *ebx, -uint32_t *ecx,uint32_t *edx); +/* Some CPUID calls want 'count' to be placed in ecx */ +static inline void cpuid_count ( +uint32_t op, +uint32_t count, +uint32_t *eax, +uint32_t *ebx, +uint32_t *ecx, +uint32_t *edx) +{ + asmvolatile ("cpuid" + :"=a" (*eax),"=b" (*ebx),"=c" (*ecx),"=d" (*edx) + :"0" (op),"c" (count) ); +} + +/* Generic CPUID function (subleaf 0) */ +static inline void cpuid ( +uint32_t leaf, +uint32_t *eax, +uint32_t *ebx, +uint32_t *ecx, +uint32_t *edx) +{ +cpuid_count (leaf,0 , eax, ebx, ecx, edx); +}/* Read the TSC register. */ static inline uint64_t rdtsc (void )
Signature · 1 line
2.34.1
Re: [PATCH for 4.19 v4 01/10] tools/hvmloader: Fix non-deterministic cpuid()
Quoted 6 lines · Alejandro Vallejo
hvmloader's
cpuid()implementation deviates from Xen's in that the value passed on ecx is unspecified. This means that when used on leaves that implement subleaves it's unspecified which one you get; though it's more than likely an invalid one.Import Xen's implementation so there are no surprises.
hvmloader")
Quoted 16 lines
Signed-off-by Alejandro Vallejo <[email protected]>tools/firmware/hvmloader/util.h +2
-3@@ -184,9 +184,30 @@ int uart_exists(uint16_t uart_base);int lpt_exists (uint16_t lpt_base);int hpet_exists (unsigned long hpet_base); -/* Do cpuid instruction, with operation 'idx' */ -void cpuid (uint32_t idx,uint32_t *eax,uint32_t *ebx, -uint32_t *ecx,uint32_t *edx); +/* Some CPUID calls want 'count' to be placed in ecx */ +static inline void cpuid_count(
tools/firmware/hvmloader/util.h int uart_exists(uint16_t uart_base); · line 198
Quoted 10 lines
tools/firmware/hvmloader/util.h +10
-0+uint32_t op, + uint32_t count, + uint32_t *eax, + uint32_t *ebx, + uint32_t *ecx, + uint32_t *edx) +{ + asmvolatile ("cpuid" + :"=a" (*eax),"=b" (*ebx),"=c" (*ecx),"=d" (*edx) + :"0" (op),"c" (count) );
"a" to be consistent with "c".
Also it would be better to name the parameters as leaf and subleaf.
Both can be fixed on commit. However, there's no use in HVMLoader tickling this bug right now, so I'm not sure we want to rush this into 4.19 at this point.
~Andrew
Re: [PATCH for 4.19 v4 01/10] tools/hvmloader: Fix non-deterministic cpuid()
Quoted 40 lines · Andrew Cooper
Quoted 6 lines · Alejandro Vallejo
hvmloader's
cpuid()implementation deviates from Xen's in that the value passed on ecx is unspecified. This means that when used on leaves that implement subleaves it's unspecified which one you get; though it's more than likely an invalid one.Import Xen's implementation so there are no surprises.
Fixes 318ac791f9f9 ("Add utilities needed for SMBIOS generation tohvmloader")
Quoted 16 lines
Signed-off-by Alejandro Vallejo <[email protected]>tools/firmware/hvmloader/util.h +2
-3@@ -184,9 +184,30 @@ int uart_exists(uint16_t uart_base);int lpt_exists (uint16_t lpt_base);int hpet_exists (unsigned long hpet_base); -/* Do cpuid instruction, with operation 'idx' */ -void cpuid (uint32_t idx,uint32_t *eax,uint32_t *ebx, -uint32_t *ecx,uint32_t *edx); +/* Some CPUID calls want 'count' to be placed in ecx */ +static inline void cpuid_count(tools/firmware/hvmloader/util.h int uart_exists(uint16_t uart_base); · line 198
Quoted 10 lines
tools/firmware/hvmloader/util.h +10
-0+uint32_t op, + uint32_t count, + uint32_t *eax, + uint32_t *ebx, + uint32_t *ecx, + uint32_t *edx) +{ + asmvolatile ("cpuid" + :"=a" (*eax),"=b" (*ebx),"=c" (*ecx),"=d" (*edx) + :"0" (op),"c" (count) );"a" to be consistent with "c".
Quoted 7 lines
Also it would be better to name the parameters as leaf and subleaf.
Both can be fixed on commit. However, there's no use in HVMLoader tickling this bug right now, so I'm not sure we want to rush this into 4.19 at this point.
~Andrew
All sound good to me. For the record, the static inlines are copied verbatim from Xen so if you'd like these adjusted you probably also want to make a postit to change Xen's too.
Cheers,
Alejandro
Re: [PATCH for 4.19 v4 01/10] tools/hvmloader: Fix non-deterministic cpuid()
In reply to Andrew CooperQuoted 44 lines · Andrew Cooper
Quoted 9 lines · Alejandro Vallejo
hvmloader's
cpuid()implementation deviates from Xen's in that the value passed
on ecx is unspecified. This means that when used on leaves that implement
subleaves it's unspecified which one you get; though it's more than likely an
invalid one.Import Xen's implementation so there are no surprises.
Fixes 318ac791f9f9 ("Add utilities needed for SMBIOS generation tohvmloader")
Quoted 17 lines
Signed-off-by Alejandro Vallejo <[email protected]>tools/firmware/hvmloader/util.h +0
-0@@ -184,9 +184,30 @@ int uart_exists(uint16_t uart_base); int lpt_exists(uint16_t lpt_base); int hpet_exists(unsigned long hpet_base);tools/firmware/hvmloader/util.h +2
-3-/* Do cpuid instruction, with operation 'idx' */ -void cpuid (uint32_t idx,uint32_t *eax,uint32_t *ebx, - uint32_t *ecx, uint32_t *edx); +/* Some CPUID calls want 'count' to be placed in ecx */ +static inline void cpuid_count(tools/firmware/hvmloader/util.h int uart_exists(uint16_t uart_base); · line 198
Quoted 10 lines
tools/firmware/hvmloader/util.h +10
-0+ uint32_t op, + uint32_t count, + uint32_t *eax, + uint32_t *ebx, + uint32_t *ecx, + uint32_t *edx) +{ + asm volatile ( "cpuid" + : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) + : "0" (op), "c" (count) );"a" to be consistent with "c".
Quoted 6 lines
Also it would be better to name the parameters as leaf and subleaf.
Both can be fixed on commit. However, there's no use in HVMLoader tickling this bug right now, so I'm not sure we want to rush this into
4.19 at this point.
I agree, I think it would be better to postpone the patch until 4.20 branch.
~ Oleksii
PATCH v4 2/10 x86/vlapic: Move lapic migration checks to the check hooks
In reply to Alejandro VallejoWhile doing this, factor out checks common to architectural and hidden state.
---
This puts essential LAPIC information in the stream. It's technically a feature but it makes 4.19 guests a lot more future-proof. I think this should go on 4.19
v4:
* Replaced BUG() with ASSERT_UNREACHABLE(), and allow ret -EINVAL on release.
* Adjust printk() to be clearer
* Assign lapic_check_common() outside the "if" condition.
--- xen/arch/x86/hvm/vlapic.c | 85 ++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 27 deletions(-)
xen/arch/x86/hvm/vlapic.c +58 -27
@@ -1553,60 +1553,91 @@ static void lapic_load_fixup(struct vlapic *vlapic) v, vlapic->loaded.id, vlapic->loaded.ldr, good_ldr); } -static int cf_check lapic_load_hidden(struct domain *d, hvm_domain_context_t *h) -{ -unsigned int vcpuid =hvm_load_instance (h); -struct vcpu *v; -struct vlapic *s; +static int lapic_check_common (const struct domain *d,unsigned int vcpuid) +{if ( !has_vlapic (d) )return -ENODEV ;/* Which vlapic to load? */ -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) +if ( !domain_vcpu (d, vcpuid) ) { -dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no apic%u\n" , +dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no vCPU %u\n" , d->domain_id, vcpuid);return -EINVAL ; } - s =vcpu_vlapic (v); + +return 0 ; +} + +static int cf_check lapic_check_hidden(const struct domain *d, +hvm_domain_context_t *h) +{ +unsigned int vcpuid =hvm_load_instance (h); +struct hvm_hw_lapic s; +int rc =lapic_check_common (d, vcpuid); + +if ( rc ) +return rc; + +if (hvm_load_entry_zeroextend (LAPIC , h, &s) !=0 ) +return -ENODATA ; + +/* EN=0 with EXTD=1 is illegal */ +if ( (s.apic_base_msr & (APIC_BASE_ENABLE |APIC_BASE_EXTD )) == +APIC_BASE_EXTD ) +return -EINVAL ; + +return 0 ; +} + +static int cf_check lapic_load_hidden(struct domain *d, hvm_domain_context_t *h) +{ +unsigned int vcpuid =hvm_load_instance (h); +struct vcpu *v = d->vcpu[vcpuid]; +struct vlapic *s =vcpu_vlapic (v);if (hvm_load_entry_zeroextend (LAPIC , h, &s->hw) !=0 ) + { +ASSERT_UNREACHABLE ();return -EINVAL ; + } s->loaded.hw =1 ;if ( s->loaded.regs )lapic_load_fixup (s); -if ( !(s->hw.apic_base_msr &APIC_BASE_ENABLE ) && -unlikely (vlapic_x2apic_mode (s)) ) -return -EINVAL ; -hvm_update_vlapic_mode (v);return 0 ; } -static int cf_check lapic_load_regs(struct domain *d, hvm_domain_context_t *h) +static int cf_check lapic_check_regs(const struct domain *d, +hvm_domain_context_t *h) {unsigned int vcpuid =hvm_load_instance (h); -struct vcpu *v; -struct vlapic *s; +int rc; -if ( !has_vlapic (d) ) -return -ENODEV ; +if ( (rc =lapic_check_common (d, vcpuid)) ) +return rc; -/* Which vlapic to load? */ -if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) ==NULL ) - { -dprintk (XENLOG_G_ERR ,"HVM restore: dom%d has no apic%u\n" , - d->domain_id, vcpuid); -return -EINVAL ; - } - s =vcpu_vlapic (v); +if ( !hvm_get_entry (LAPIC_REGS , h) ) +return -ENODATA ; + +return 0 ; +} + +static int cf_check lapic_load_regs(struct domain *d, hvm_domain_context_t *h) +{ +unsigned int vcpuid =hvm_load_instance (h); +struct vcpu *v = d->vcpu[vcpuid]; +struct vlapic *s =vcpu_vlapic (v);if (hvm_load_entry (LAPIC_REGS , h, s->regs) !=0 ) + { +ASSERT_UNREACHABLE ();return -EINVAL ; + } s->loaded.id =vlapic_get_reg (s,APIC_ID ); s->loaded.ldr =vlapic_get_reg (s,APIC_LDR );
@@ -1623,9 +1654,9 @@ static int cf_check lapic_load_regs(struct domain *d, hvm_domain_context_t *h)return 0 ; } -HVM_REGISTER_SAVE_RESTORE (LAPIC , lapic_save_hidden,NULL , +HVM_REGISTER_SAVE_RESTORE (LAPIC , lapic_save_hidden, lapic_check_hidden, lapic_load_hidden,1 ,HVMSR_PER_VCPU ); -HVM_REGISTER_SAVE_RESTORE (LAPIC_REGS , lapic_save_regs,NULL , +HVM_REGISTER_SAVE_RESTORE (LAPIC_REGS , lapic_save_regs, lapic_check_regs, lapic_load_regs,1 ,HVMSR_PER_VCPU );int vlapic_init (struct vcpu *v)
Signature · 1 line
2.34.1
PATCH v4 3/10 xen/x86: Add initial x2APIC ID to the per-vLAPIC save area
In reply to Alejandro VallejoThis allows the initial x2APIC ID to be sent on the migration stream. This allows further changes to topology and APIC ID assignment without breaking existing hosts. Given the vlapic data is zero-extended on restore, fix up migrations from hosts without the field by setting it to the old convention if zero.
The hardcoded mapping x2apic_id=2*vcpu_id is kept for the time being, but it's meant to be overriden by toolstack on a later patch with appropriate values.
---
Same rationale as previous patch for inclusion in 4.19
Roger replied to v3 with an R-by for this patch. I didn't add it here because the patch has seen substantial changes and it's probably worth looking at again
All changes are removals. In particular...
v4:
* Removed hooks into cpu policy update events. They are no longer relevant.
* Remove the derivation (within Xen) of x2apic_id from vcpu_id via lib/x86.
* Rearranged for toolstack to provide those on hvmcontext blobs on a later patch. This still works out because the default is the legacy scheme of apicid=vcpuid*2
--- xen/arch/x86/cpuid.c | 14 +++++--------- xen/arch/x86/hvm/vlapic.c | 22 ++++++++++++++++++++-- xen/arch/x86/include/asm/hvm/vlapic.h | 1 + xen/include/public/arch-x86/hvm/save.h | 2 ++ 4 files changed, 28 insertions(+), 11 deletions(-)
xen/arch/x86/cpuid.c +5 -9
@@ -139,10 +139,9 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,const struct cpu_user_regs *regs;case 0x1 : -/* TODO: Rework topology logic. */ res->b &=0x00ffffffu ;if (is_hvm_domain (d) ) - res->b |= (v->vcpu_id *2 ) <<24 ; + res->b |=vlapic_x2apic_id (vcpu_vlapic (v)) <<24 ;/* TODO: Rework vPMU control in terms of toolstack choices. */ if ( vpmu_available(v) &&
@@ -312,18 +311,15 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,case 0xb :/* -* In principle, this leaf is Intel-only. In practice, it is tightly -* coupled with x2apic, and we offer an x2apic-capable APIC emulation -* to guests on AMD hardware as well. -* -* TODO: Rework topology logic. +* Don't expose topology information to PV guests. Exposed on HVM +* along with x2APIC because they are tightly coupled. */ -if ( p->basic.x2apic ) +if (is_hvm_domain (d) && p->basic.x2apic ) { *(uint8_t *)&res->c = subleaf;/* Fix the x2APIC identifier. */ - res->d = v->vcpu_id *2 ; + res->d =vlapic_x2apic_id (vcpu_vlapic (v)); }break ;
xen/arch/x86/hvm/vlapic.c +20 -2
@@ -1072,7 +1072,7 @@ static uint32_t x2apic_ldr_from_id(uint32_t id)static void set_x2apic_id (struct vlapic *vlapic) {const struct vcpu *v =vlapic_vcpu (vlapic); -uint32_t apic_id = v->vcpu_id *2 ; +uint32_t apic_id = vlapic->hw.x2apic_id;uint32_t apic_ldr =x2apic_ldr_from_id (apic_id);/*
@@ -1452,7 +1452,7 @@ void vlapic_reset(struct vlapic *vlapic)if ( v->vcpu_id ==0 ) vlapic->hw.apic_base_msr |=APIC_BASE_BSP ; -vlapic_set_reg (vlapic,APIC_ID , (v->vcpu_id *2 ) <<24 ); +vlapic_set_reg (vlapic,APIC_ID ,SET_xAPIC_ID (vlapic->hw.x2apic_id));vlapic_do_init (vlapic); }
@@ -1520,6 +1520,16 @@ static void lapic_load_fixup(struct vlapic *vlapic)const struct vcpu *v =vlapic_vcpu (vlapic);uint32_t good_ldr =x2apic_ldr_from_id (vlapic->loaded.id); +/* +* Loading record without hw.x2apic_id in the save stream, calculate using +* the traditional "vcpu_id * 2" relation. There's an implicit assumption +* that vCPU0 always has x2APIC0, which is true for the old relation, and +* still holds under the new x2APIC generation algorithm. While that case +* goes through the conditional it's benign because it still maps to zero. +*/ +if ( !vlapic->hw.x2apic_id ) + vlapic->hw.x2apic_id = v->vcpu_id *2 ; +/* Skip fixups on xAPIC mode, or if the x2APIC LDR is already correct */ if ( !vlapic_x2apic_mode (vlapic) || (vlapic->loaded.ldr == good_ldr) )
@@ -1588,6 +1598,13 @@ static int cf_check lapic_check_hidden(const struct domain *d, APIC_BASE_EXTD )return -EINVAL ; +/* +* Fail migrations from newer versions of Xen where +* rsvd_zero is interpreted as something else. +*/ +if ( s.rsvd_zero ) +return -EINVAL ; +return 0 ; }
@@ -1672,6 +1689,7 @@ int vlapic_init(struct vcpu *v) } vlapic->pt.source = PTSRC_lapic; + vlapic->hw.x2apic_id =2 * v->vcpu_id; vlapic->regs_page =alloc_domheap_page (v->domain, MEMF_no_owner);if ( !vlapic->regs_page )
xen/arch/x86/include/asm/hvm/vlapic.h +1 -0
@@ -44,6 +44,7 @@#define vlapic_xapic_mode (vlapic) \ (!vlapic_hw_disabled(vlapic) && \ !((vlapic)->hw.apic_base_msr & APIC_BASE_EXTD)) +#define vlapic_x2apic_id (vlapic) ((vlapic)->hw.x2apic_id)/* * Generic APIC bitmap vector update & search routines.
xen/include/public/arch-x86/hvm/save.h +2 -0
@@ -394,6 +394,8 @@ struct hvm_hw_lapic {uint32_t disabled;/* VLAPIC_xx_DISABLED */ uint32_t timer_divisor;uint64_t tdt_msr; +uint32_t x2apic_id; +uint32_t rsvd_zero; }; DECLARE_HVM_SAVE_TYPE(LAPIC, 5, struct hvm_hw_lapic);
Signature · 1 line
2.34.1
PATCH v4 4/10 tools/hvmloader: Retrieve (x2)APIC IDs from the APs themselves
In reply to Alejandro VallejoMake it so the APs expose their own APIC IDs in a LUT. We can use that LUT to populate the MADT, decoupling the algorithm that relates CPU IDs and APIC IDs from hvmloader.
While at this also remove ap_callin, as writing the APIC ID may serve the same purpose.
---
v4:
* Removed bogus ! in ASSERT() statement introduced in v3.
--- tools/firmware/hvmloader/config.h | 6 ++- tools/firmware/hvmloader/hvmloader.c | 4 +- tools/firmware/hvmloader/smp.c | 54 ++++++++++++++++++++----- tools/include/xen-tools/common-macros.h | 5 +++ 4 files changed, 56 insertions(+), 13 deletions(-)
tools/firmware/hvmloader/config.h +5 -1
@@ -4,6 +4,8 @@#include <stdint.h> #include <stdbool.h> +#include <xen/hvm/hvm_info_table.h> +enum virtual_vga { VGA_none, VGA_std, VGA_cirrus, VGA_pt };extern enum virtual_vga virtual_vga;
@@ -48,8 +50,10 @@ extern uint8_t ioapic_version;#define IOAPIC_ID 0x01 +extern uint32_t CPU_TO_X2APICID [HVM_MAX_VCPUS ]; +#define LAPIC_BASE_ADDRESS 0xfee00000 -#define LAPIC_ID (vcpu_id) ((vcpu_id) * 2) +#define LAPIC_ID (vcpu_id) (CPU_TO_X2APICID[(vcpu_id)])#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 +2 -2
@@ -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/smp.c +44 -10
90 lines not on this page · raw message
tools/include/xen-tools/common-macros.h +5 -0
@@ -108,4 +108,9 @@#define get_unaligned (ptr) get_unaligned_t(typeof(*(ptr)), ptr)#define put_unaligned (val, ptr) put_unaligned_t(typeof(*(ptr)), val, ptr) +#define __ACCESS_ONCE (x) ({ \ + (void)(typeof(x))0;/* Scalar typecheck. */ \ + (volatile typeof(x) *)&(x); }) +#define ACCESS_ONCE (x) (*__ACCESS_ONCE(x)) +#endif /* __XEN_TOOLS_COMMON_MACROS__ */
Signature · 1 line
2.34.1
PATCH v4 5/10 xen/x86: Add supporting code for uploading LAPIC contexts during domain create
In reply to Alejandro VallejoThis patch is a precondition for a later patch in which toolstack uses HVM contexts to upload LAPIC data to a newly constructed domain.
If toolstack were to upload LAPIC contexts as part of domain creation as-is it would encounter a problem were the architectural state does not reflect the APIC ID in the hidden state. This patch ensures updates to the hidden state trigger an update in the architectural registers so the APIC ID in both is consistent.
---
v4:
* New patch
--- xen/arch/x86/hvm/vlapic.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
xen/arch/x86/hvm/vlapic.c +20 -0
@@ -1622,7 +1622,27 @@ static int cf_check lapic_load_hidden(struct domain *d, hvm_domain_context_t *h) s->loaded.hw =1 ;if ( s->loaded.regs ) + { +/* +* We already processed architectural regs in lapic_load_regs(), so +* this must be a migration. Fix up inconsistencies from any older Xen. +*/ lapic_load_fixup (s); + } +else + { +/* +* We haven't seen architectural regs so this could be a migration or a +* plain domain create. In the domain create case it's fine to modify +* the architectural state to align it to the APIC ID that was just +* uploaded and in the migrate case it doesn't matter because the +* architectural state will be replaced by the LAPIC_REGS ctx later on. +*/ +if (vlapic_x2apic_mode (s) ) +set_x2apic_id (s); +else +vlapic_set_reg (s,APIC_ID ,SET_xAPIC_ID (s->hw.x2apic_id)); + }hvm_update_vlapic_mode (v);
Signature · 1 line
2.34.1
PATCH v4 6/10 tools/libguest: Make setting MTRR registers unconditional
In reply to Alejandro VallejoThis greatly simplifies a later patch that makes use of HVM contexts to upload LAPIC data. The idea is to reuse MTRR setting procedure to avoid code duplication. It's currently only used for PVH, but there's no real reason to overcomplicate the toolstack preventing them being set for HVM too when hvmloader will override them anyway.
While at it, add a missing "goto out" to what was the error condition in the loop.
---
v4:
* New patch
--- tools/libs/guest/xg_dom_x86.c | 83 ++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 40 deletions(-)
tools/libs/guest/xg_dom_x86.c +43 -40
@@ -989,6 +989,7 @@ const static void *hvm_get_save_record(const void *ctx, unsigned int type,static int vcpu_hvm (struct xc_dom_image *dom) { +/* Initialises the BSP */ struct {struct hvm_save_descriptor header_d;HVM_SAVE_TYPE (HEADER) header;
@@ -997,6 +998,18 @@ static int vcpu_hvm(struct xc_dom_image *dom)struct hvm_save_descriptor end_d;HVM_SAVE_TYPE (END) end; } bsp_ctx; +/* Initialises APICs and MTRRs of every vCPU */ +struct { +struct hvm_save_descriptor header_d; +HVM_SAVE_TYPE (HEADER) header; +struct hvm_save_descriptor mtrr_d; +HVM_SAVE_TYPE (MTRR) mtrr; +struct hvm_save_descriptor end_d; +HVM_SAVE_TYPE (END) end; + } vcpu_ctx; +/* Context from full_ctx */ +const HVM_SAVE_TYPE (MTRR) *mtrr_record; +/* Raw context as taken from Xen */ uint8_t *full_ctx =NULL ;int rc;
@@ -1083,51 +1096,41 @@ static int vcpu_hvm(struct xc_dom_image *dom) bsp_ctx.end_d.instance =0 ; bsp_ctx.end_d.length =HVM_SAVE_LENGTH (END ); -/* TODO: maybe this should be a firmware option instead? */ -if ( !dom->device_model ) +/* TODO: maybe setting MTRRs should be a firmware option instead? */ + mtrr_record =hvm_get_save_record (full_ctx,HVM_SAVE_CODE (MTRR ),0 ); + +if ( !mtrr_record) { -struct { -struct hvm_save_descriptor header_d; -HVM_SAVE_TYPE (HEADER) header; -struct hvm_save_descriptor mtrr_d; -HVM_SAVE_TYPE (MTRR) mtrr; -struct hvm_save_descriptor end_d; -HVM_SAVE_TYPE (END) end; - } mtrr = { - .header_d = bsp_ctx.header_d, - .header = bsp_ctx.header, - .mtrr_d.typecode =HVM_SAVE_CODE (MTRR ), - .mtrr_d.length =HVM_SAVE_LENGTH (MTRR ), - .end_d = bsp_ctx.end_d, - .end = bsp_ctx.end, - }; -const HVM_SAVE_TYPE (MTRR) *mtrr_record = -hvm_get_save_record (full_ctx,HVM_SAVE_CODE (MTRR ),0 ); -unsigned int i; - -if ( !mtrr_record ) - { -xc_dom_panic (dom->xch,XC_INTERNAL_ERROR , -"%s: unable to get MTRR save record" , __func__); - goto out; - } +xc_dom_panic (dom->xch,XC_INTERNAL_ERROR , +"%s: unable to get MTRR save record" , __func__); + goto out; + } -memcpy (&mtrr.mtrr, mtrr_record,sizeof (mtrr.mtrr)); + vcpu_ctx.header_d = bsp_ctx.header_d; + vcpu_ctx.header = bsp_ctx.header; + vcpu_ctx.mtrr_d.typecode =HVM_SAVE_CODE (MTRR ); + vcpu_ctx.mtrr_d.length =HVM_SAVE_LENGTH (MTRR ); + vcpu_ctx.mtrr = *mtrr_record; + vcpu_ctx.end_d = bsp_ctx.end_d; + vcpu_ctx.end = bsp_ctx.end; -/* -* Enable MTRR, set default type to WB. -* TODO: add MMIO areas as UC when passthrough is supported. -*/ - mtrr.mtrr.msr_mtrr_def_type =MTRR_TYPE_WRBACK |MTRR_DEF_TYPE_ENABLE ; +/* +* Enable MTRR, set default type to WB. +* TODO: add MMIO areas as UC when passthrough is supported in PVH +*/ + vcpu_ctx.mtrr.msr_mtrr_def_type =MTRR_TYPE_WRBACK |MTRR_DEF_TYPE_ENABLE ; -for ( i =0 ; i < dom->max_vcpus; i++ ) +for (unsigned int i =0 ; i < dom->max_vcpus; i++ ) + { + vcpu_ctx.mtrr_d.instance = i; + + rc =xc_domain_hvm_setcontext (dom->xch, dom->guest_domid, + (uint8_t *)&vcpu_ctx,sizeof (vcpu_ctx)); +if ( rc !=0 ) { - mtrr.mtrr_d.instance = i; - rc =xc_domain_hvm_setcontext (dom->xch, dom->guest_domid, - (uint8_t *)&mtrr,sizeof (mtrr)); -if ( rc !=0 ) -xc_dom_panic (dom->xch,XC_INTERNAL_ERROR , -"%s: SETHVMCONTEXT failed (rc=%d)" , __func__, rc); +xc_dom_panic (dom->xch,XC_INTERNAL_ERROR , +"%s: SETHVMCONTEXT failed (rc=%d)" , __func__, rc); + goto out; } }
Signature · 1 line
2.34.1
Re: [PATCH v4 06/10] tools/libguest: Make setting MTRR registers unconditional
Quoted 5 lines · Alejandro Vallejo
This greatly simplifies a later patch that makes use of HVM contexts to upload LAPIC data. The idea is to reuse MTRR setting procedure to avoid code duplication. It's currently only used for PVH, but there's no real reason to overcomplicate the toolstack preventing them being set for HVM too when hvmloader will override them anyway.
Yet then - why set them when hvmloader will do so again? Is it even guaranteed to be no change in (guest) behavior to do so?
Plus what about a guest which was configured to have the CPUID bit for MTRRs clear? I think we ought to document this as not supported for PVH (we may actually choose to refuse building such a guest), but in principle the MTRR save/load operations should simply fail for a HVM guest in said configuration. Making such a change in Xen now would, afaict, be benign to the tool stack. After this adjustment it would result in a perceived regression, when there shouldn't be any.
Thinking about it, even for PVH it may make sense to allow CPUID.MTRR=0, as long as CPUID.PAT=1, thus forcing it into PAT-only mode. I think we did even discuss this possible configuration before.
Jan
Re: [PATCH v4 06/10] tools/libguest: Make setting MTRR registers unconditional
Quoted 8 lines · Jan Beulich
Quoted 5 lines · Alejandro Vallejo
This greatly simplifies a later patch that makes use of HVM contexts to upload LAPIC data. The idea is to reuse MTRR setting procedure to avoid code duplication. It's currently only used for PVH, but there's no real reason to overcomplicate the toolstack preventing them being set for HVM too when hvmloader will override them anyway.
Yet then - why set them when hvmloader will do so again?
To keep the toolstack complexity tractable, essentially. This way I can send N hypercalls (for N vCPUs) rather than 2*N and have a single hvmcontext struct rather than several.
In truth though, I could simply write back the old MTRRs taken from bsp_ctx on HVM.
Quoted 2 lines
Is it even guaranteed
to be no change in (guest) behavior to do so?
hvmloader overrides those values, so there is no change by the time BIOS or OVMF start running. As I mentioned before though, I can actually upload back the old values in the HVM case.
Quoted 4 lines
Plus what about a guest which was configured to have the CPUID bit for MTRRs clear?
I think we ought to document this as not supported for PVH (we may
By "this" do you mean PVH _must_ have MTRR support? I would agree.
Quoted 2 lines
actually choose to refuse building such a guest), but in principle the MTRR save/load operations should simply fail for a HVM guest in said configuration.
What use cases does that cover? With the adjustment I mention at the top that should be sorted. I'm wondering why we allow !mtrr at all.
Quoted 3 lines
Making such a change in Xen now would, afaict, be benign to the tool stack. After this adjustment it would result in a perceived regression, when there shouldn't be any.
Fair point.
Quoted 6 lines
Thinking about it, even for PVH it may make sense to allow CPUID.MTRR=0, as long as CPUID.PAT=1, thus forcing it into PAT-only mode. I think we did even discuss this possible configuration before.
Jan
Is PAT-only an existing real HW configuration? Can't say I've seen any.
Cheers,
Alejandro
Re: [PATCH v4 06/10] tools/libguest: Make setting MTRR registers unconditional
Quoted 6 lines · Alejandro Vallejo
Quoted 3 lines · Jan Beulich
Plus what about a guest which was configured to have the CPUID bit for MTRRs clear?
I think we ought to document this as not supported for PVH (we mayBy "this" do you mean PVH _must_ have MTRR support? I would agree.
That was my first thought, yes. But then further down I adjusted my considerations.
Quoted 5 lines
Quoted 2 lines
actually choose to refuse building such a guest), but in principle the MTRR save/load operations should simply fail for a HVM guest in said configuration.
What use cases does that cover? With the adjustment I mention at the top that should be sorted. I'm wondering why we allow !mtrr at all.
Not allowing it would open up for a mess in what CPUID bits we allow to override and for which ones we'd deny overrides.
Quoted 4 lines
Quoted 3 lines
Making such a change in Xen now would, afaict, be benign to the tool stack. After this adjustment it would result in a perceived regression, when there shouldn't be any.
Quoted 8 lines
Fair point.
Quoted 4 lines
Thinking about it, even for PVH it may make sense to allow CPUID.MTRR=0, as long as CPUID.PAT=1, thus forcing it into PAT-only mode. I think we did even discuss this possible configuration before.
Is PAT-only an existing real HW configuration? Can't say I've seen any.
I don't think there are any, but the architecture doesn't preclude it, and that's a simpler model overall for an OS to work with. Hence why it was discussed (to some degree) before (if my memory doesn't fail me there).
Jan
PATCH v4 7/10 xen/lib: Add topology generator for x86
In reply to Alejandro VallejoAdd a helper to populate topology leaves in the cpu policy from threads/core and cores/package counts. It's unit-tested in test-cpu-policy.c, but it's not connected to the rest of the code yet.
Adds the ASSERT() macro to xen/lib/x86/private.h, as it was missing.
---
v4:
* v1->v2 introduced a bug. lppp must be MIN(0xff, threads_per_pkg).
* Add missing MIN() when setting p->extd.nc (should've been done in v2)
--- tools/tests/cpu-policy/test-cpu-policy.c | 133 +++++++++++++++++++++++ xen/include/xen/lib/x86/cpu-policy.h | 16 +++ xen/lib/x86/policy.c | 88 +++++++++++++++ xen/lib/x86/private.h | 4 + 4 files changed, 241 insertions(+)
tools/tests/cpu-policy/test-cpu-policy.c +133 -0
@@ -650,6 +650,137 @@ static void test_is_compatible_failure(void) } } +static void test_topo_from_parts (void ) +{ +static const struct test { +unsigned int threads_per_core; +unsigned int cores_per_pkg; +struct cpu_policy policy; + } tests[] = { + { + .threads_per_core =3 , .cores_per_pkg =1 , + .policy = { + .x86_vendor =X86_VENDOR_AMD , + .topo.subleaf = { + { .nr_logical =3 , .level =0 , .type =1 , .id_shift =2 , }, + { .nr_logical =1 , .level =1 , .type =2 , .id_shift =2 , }, + }, + }, + }, + { + .threads_per_core =1 , .cores_per_pkg =3 , + .policy = { + .x86_vendor =X86_VENDOR_AMD , + .topo.subleaf = { + { .nr_logical =1 , .level =0 , .type =1 , .id_shift =0 , }, + { .nr_logical =3 , .level =1 , .type =2 , .id_shift =2 , }, + }, + }, + }, + { + .threads_per_core =7 , .cores_per_pkg =5 , + .policy = { + .x86_vendor =X86_VENDOR_AMD , + .topo.subleaf = { + { .nr_logical =7 , .level =0 , .type =1 , .id_shift =3 , }, + { .nr_logical =5 , .level =1 , .type =2 , .id_shift =6 , }, + }, + }, + }, + { + .threads_per_core =2 , .cores_per_pkg =128 , + .policy = { + .x86_vendor =X86_VENDOR_AMD , + .topo.subleaf = { + { .nr_logical =2 , .level =0 , .type =1 , .id_shift =1 , }, + { .nr_logical =128 , .level =1 , .type =2 , + .id_shift =8 , }, + }, + }, + }, + { + .threads_per_core =3 , .cores_per_pkg =1 , + .policy = { + .x86_vendor =X86_VENDOR_INTEL , + .topo.subleaf = { + { .nr_logical =3 , .level =0 , .type =1 , .id_shift =2 , }, + { .nr_logical =3 , .level =1 , .type =2 , .id_shift =2 , }, + }, + }, + }, + { + .threads_per_core =1 , .cores_per_pkg =3 , + .policy = { + .x86_vendor =X86_VENDOR_INTEL , + .topo.subleaf = { + { .nr_logical =1 , .level =0 , .type =1 , .id_shift =0 , }, + { .nr_logical =3 , .level =1 , .type =2 , .id_shift =2 , }, + }, + }, + }, + { + .threads_per_core =7 , .cores_per_pkg =5 , + .policy = { + .x86_vendor =X86_VENDOR_INTEL , + .topo.subleaf = { + { .nr_logical =7 , .level =0 , .type =1 , .id_shift =3 , }, + { .nr_logical =35 , .level =1 , .type =2 , .id_shift =6 , }, + }, + }, + }, + { + .threads_per_core =2 , .cores_per_pkg =128 , + .policy = { + .x86_vendor =X86_VENDOR_INTEL , + .topo.subleaf = { + { .nr_logical =2 , .level =0 , .type =1 , .id_shift =1 , }, + { .nr_logical =256 , .level =1 , .type =2 , + .id_shift =8 , }, + }, + }, + }, + }; + +printf ("Testing topology synthesis from parts:\n" ); + +for (size_t i =0 ; i <ARRAY_SIZE (tests); ++i ) + { +const struct test *t = &tests[i]; +struct cpu_policy actual = { .x86_vendor = t->policy.x86_vendor }; +int rc =x86_topo_from_parts (&actual, t->threads_per_core, + t->cores_per_pkg); + +if ( rc ||memcmp (&actual.topo, &t->policy.topo,sizeof (actual.topo)) ) + { +#define TOPO (n, f) t->policy.topo.subleaf[(n)].f, actual.topo.subleaf[(n)].f +fail ("FAIL[%d] - '%s %u t/c, %u c/p'\n" , + rc, +x86_cpuid_vendor_to_str (t->policy.x86_vendor), + t->threads_per_core, t->cores_per_pkg); +printf (" subleaf=%u expected_n=%u actual_n=%u\n" +" expected_lvl=%u actual_lvl=%u\n" +" expected_type=%u actual_type=%u\n" +" expected_shift=%u actual_shift=%u\n" , +0 , +TOPO (0 , nr_logical), +TOPO (0 , level), +TOPO (0 , type), +TOPO (0 , id_shift)); + +printf (" subleaf=%u expected_n=%u actual_n=%u\n" +" expected_lvl=%u actual_lvl=%u\n" +" expected_type=%u actual_type=%u\n" +" expected_shift=%u actual_shift=%u\n" , +1 , +TOPO (1 , nr_logical), +TOPO (1 , level), +TOPO (1 , type), +TOPO (1 , id_shift)); +#undef TOPO + } + } +} +int main (int argc,char **argv) {printf ("CPU Policy unit tests\n" );
@@ -667,6 +798,8 @@ int main(int argc, char **argv)test_is_compatible_success ();test_is_compatible_failure (); +test_topo_from_parts (); +if ( nr_failures )printf ("Done: %u failures\n" , nr_failures);else
xen/include/xen/lib/x86/cpu-policy.h +16 -0
@@ -542,6 +542,22 @@ int x86_cpu_policies_are_compatible(const struct cpu_policy *host,const struct cpu_policy *guest, struct cpu_policy_errors *err); +/** +* Synthesise topology information in `p` given high-level constraints +* +* Topology is given in various fields accross several leaves, some of +* which are vendor-specific. This function uses the policy itself to +* derive such leaves from threads/core and cores/package. +* +* @param p CPU policy of the domain. +* @param threads_per_core threads/core. Doesn't need to be a power of 2. +* @param cores_per_package cores/package. Doesn't need to be a power of 2. +* @return 0 on success; -errno on failure +*/ +int x86_topo_from_parts (struct cpu_policy *p, +unsigned int threads_per_core, +unsigned int cores_per_pkg); +#endif /* !XEN_LIB_X86_POLICIES_H */ /*
xen/lib/x86/policy.c +88 -0
94 lines not on this page · raw message
xen/lib/x86/private.h +4 -0
@@ -4,6 +4,7 @@#ifdef __XEN__#include <xen/bitops.h> +#include <xen/bug.h> #include <xen/guest_access.h> #include <xen/kernel.h> #include <xen/lib.h>
@@ -17,6 +18,7 @@#else +#include <assert.h> #include <errno.h> #include <inttypes.h> #include <stdbool.h>
@@ -28,6 +30,8 @@#include <xen-tools/common-macros.h> +#define ASSERT (x) assert(x) +static inline bool test_bit (unsigned int bit,const void *vaddr) {const char *addr = vaddr;
Signature · 1 line
2.34.1
PATCH v4 8/10 xen/x86: Derive topologically correct x2APIC IDs from the policy
In reply to Alejandro VallejoImplements the helper for mapping vcpu_id to x2apic_id given a valid topology in a policy. The algo is written with the intention of extending it to leaves 0x1f and extended 0x26 in the future.
Toolstack doesn't set leaf 0xb and the HVM default policy has it cleared, so the leaf is not implemented. In that case, the new helper just returns the legacy mapping.
---v2->v4 (v3 was not reviewed):
* Rewrite eXX notation for CPUID leaves as "extended XX"
* Newlines and linewraps
* In the unit-test, reduce the scope of policy
* In the unit-test, fail if topology generation fails.
--- tools/tests/cpu-policy/test-cpu-policy.c | 68 +++++++++++++++++++++ xen/include/xen/lib/x86/cpu-policy.h | 11 ++++ xen/lib/x86/policy.c | 76 ++++++++++++++++++++++++ 3 files changed, 155 insertions(+)
tools/tests/cpu-policy/test-cpu-policy.c +68 -0
@@ -781,6 +781,73 @@ static void test_topo_from_parts(void) } } +static void test_x2apic_id_from_vcpu_id_success (void ) +{ +static const struct test { +unsigned int vcpu_id; +unsigned int threads_per_core; +unsigned int cores_per_pkg; +uint32_t x2apic_id; +uint8_t x86_vendor; + } tests[] = { + { + .vcpu_id =3 , .threads_per_core =3 , .cores_per_pkg =8 , + .x2apic_id =1 <<2 , + }, + { + .vcpu_id =6 , .threads_per_core =3 , .cores_per_pkg =8 , + .x2apic_id =2 <<2 , + }, + { + .vcpu_id =24 , .threads_per_core =3 , .cores_per_pkg =8 , + .x2apic_id =1 <<5 , + }, + { + .vcpu_id =35 , .threads_per_core =3 , .cores_per_pkg =8 , + .x2apic_id = (35 %3 ) | (((35 /3 ) %8 ) <<2 ) | ((35 /24 ) <<5 ), + }, + { + .vcpu_id =96 , .threads_per_core =7 , .cores_per_pkg =3 , + .x2apic_id = (96 %7 ) | (((96 /7 ) %3 ) <<3 ) | ((96 /21 ) <<5 ), + }, + }; + +const uint8_t vendors[] = { +X86_VENDOR_INTEL , +X86_VENDOR_AMD , +X86_VENDOR_CENTAUR , +X86_VENDOR_SHANGHAI , +X86_VENDOR_HYGON , + }; + +printf ("Testing x2apic id from vcpu id success:\n" ); + +/* Perform the test run on every vendor we know about */ +for (size_t i =0 ; i <ARRAY_SIZE (vendors); ++i ) + { +for (size_t j =0 ; j <ARRAY_SIZE (tests); ++j ) + { +struct cpu_policy policy = { .x86_vendor = vendors[i] }; +const struct test *t = &tests[j]; +uint32_t x2apic_id; +int rc =x86_topo_from_parts (&policy, t->threads_per_core, + t->cores_per_pkg); + +if ( rc ) { +fail ("FAIL[%d] - 'x86_topo_from_parts() failed" , rc); +continue ; + } + + x2apic_id =x86_x2apic_id_from_vcpu_id (&policy, t->vcpu_id); +if ( x2apic_id != t->x2apic_id ) +fail ("FAIL - '%s cpu%u %u t/c %u c/p'. bad x2apic_id: expected=%u actual=%u\n" , +x86_cpuid_vendor_to_str (policy.x86_vendor), + t->vcpu_id, t->threads_per_core, t->cores_per_pkg, + t->x2apic_id, x2apic_id); + } + } +} +int main (int argc,char **argv) {printf ("CPU Policy unit tests\n" );
@@ -799,6 +866,7 @@ int main(int argc, char **argv)test_is_compatible_failure ();test_topo_from_parts (); +test_x2apic_id_from_vcpu_id_success ();if ( nr_failures )printf ("Done: %u failures\n" , nr_failures);
xen/include/xen/lib/x86/cpu-policy.h +11 -0
@@ -542,6 +542,17 @@ int x86_cpu_policies_are_compatible(const struct cpu_policy *host,const struct cpu_policy *guest, struct cpu_policy_errors *err); +/** +* Calculates the x2APIC ID of a vCPU given a CPU policy +* +* If the policy lacks leaf 0xb falls back to legacy mapping of apic_id=cpu*2 +* +* @param p CPU policy of the domain. +* @param id vCPU ID of the vCPU. +* @returns x2APIC ID of the vCPU. +*/ +uint32_t x86_x2apic_id_from_vcpu_id (const struct cpu_policy *p,uint32_t id); +/** * Synthesise topology information in `p` given high-level constraints *
xen/lib/x86/policy.c +76 -0
82 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 9/10 xen/x86: Synthesise domain topologies
In reply to Alejandro VallejoExpose sensible topologies in leaf 0xb. At the moment it synthesises non-HT systems, in line with the previous code intent.
Leaf 0xb in the host policy is no longer zapped and the guest {max,def} policies have their topology leaves zapped instead. The intent is for toolstack to populate them. There's no current use for the topology information in the host policy, but it makes no harm.
---
This patch MUST NOT go in without the following intimately related one
"Set topologically correct x2APIC IDs for each vCPU"
Otherwise we expose one topology and then create APIC IDs that don't reflect it
v2->v4 (v3 was not reviewed):
* Adjustments to the commit message
* Various newline/linewrap fixes
* Also print error code in new ERROR() message
* Preserve old logic to recreate old CPUID policy to enable migrations from versions of Xen without policy information in the migration stream.
--- tools/libs/guest/xg_cpuid_x86.c | 24 +++++++++++++++++++++++- xen/arch/x86/cpu-policy.c | 9 ++++++--- 2 files changed, 29 insertions(+), 4 deletions(-)
tools/libs/guest/xg_cpuid_x86.c +23 -1
@@ -725,8 +725,16 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, p->policy.basic.htt =test_bit (X86_FEATURE_HTT , host_featureset); p->policy.extd.cmp_legacy =test_bit (X86_FEATURE_CMP_LEGACY , host_featureset); } -else +else if ( restore ) { +/* +* Reconstruct the topology exposed on Xen <= 4.13. It makes very little +* sense, but it's what those guests saw so it's set in stone now. +* +* Guests from Xen 4.14 onwards carry their own CPUID leaves in the +* migration stream so they don't need special treatment. +*/ +/* * Topology for HVM guests is entirely controlled by Xen. For now, we * hardcode APIC_ID = vcpu_id * 2 to give the illusion of no SMT.
@@ -782,6 +790,20 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore,break ; } } +else + { +/* TODO: Expose the ability to choose a custom topology for HVM/PVH */ +unsigned int threads_per_core =1 ; +unsigned int cores_per_pkg = di.max_vcpu_id +1 ; + + rc =x86_topo_from_parts (&p->policy, threads_per_core, cores_per_pkg); +if ( rc ) + { +ERROR ("Failed to generate topology: rc=%d t/c=%u c/p=%u" , + rc, threads_per_core, cores_per_pkg); + goto out; + } + } nr_leaves =ARRAY_SIZE (p->leaves); rc =x86_cpuid_copy_to_buffer (&p->policy, p->leaves, &nr_leaves);
xen/arch/x86/cpu-policy.c +6 -3
@@ -263,9 +263,6 @@ static void recalculate_misc(struct cpu_policy *p) p->basic.raw[0x8 ] =EMPTY_LEAF ; -/* TODO: Rework topology logic. */ -memset (p->topo.raw,0 ,sizeof (p->topo.raw)); - p->basic.raw[0xc ] =EMPTY_LEAF ; p->extd.e1d &= ~CPUID_COMMON_1D_FEATURES ;
@@ -613,6 +610,9 @@ static void __init calculate_pv_max_policy(void)recalculate_xstate (p); p->extd.raw[0xa ] =EMPTY_LEAF ;/* No SVM for PV guests. */ + +/* Wipe host topology. Populated by toolstack */ +memset (p->topo.raw,0 ,sizeof (p->topo.raw)); }static void __init calculate_pv_def_policy (void )
@@ -776,6 +776,9 @@ static void __init calculate_hvm_max_policy(void)/* It's always possible to emulate CPUID faulting for HVM guests */ p->platform_info.cpuid_faulting = true; + +/* Wipe host topology. Populated by toolstack */ +memset (p->topo.raw,0 ,sizeof (p->topo.raw)); }static void __init calculate_hvm_def_policy (void )
Signature · 1 line
2.34.1
PATCH v4 10/10 tools/libguest: Set topologically correct x2APIC IDs for each vCPU
In reply to Alejandro VallejoHave toolstack populate the new x2APIC ID in the LAPIC save record with the proper IDs intended for each vCPU.
---
v4:
* New patch. Replaced v3's method of letting Xen find out via the same algorithm toolstack uses.
--- tools/libs/guest/xg_dom_x86.c | 37 ++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
tools/libs/guest/xg_dom_x86.c +36 -1
@@ -1004,19 +1004,40 @@ static int vcpu_hvm(struct xc_dom_image *dom)HVM_SAVE_TYPE (HEADER) header;struct hvm_save_descriptor mtrr_d;HVM_SAVE_TYPE (MTRR) mtrr; +struct hvm_save_descriptor lapic_d; +HVM_SAVE_TYPE (LAPIC) lapic;struct hvm_save_descriptor end_d;HVM_SAVE_TYPE (END) end; } vcpu_ctx; -/* Context from full_ctx */ +/* Contexts from full_ctx */ const HVM_SAVE_TYPE (MTRR) *mtrr_record; +const HVM_SAVE_TYPE (LAPIC) *lapic_record;/* Raw context as taken from Xen */ uint8_t *full_ctx =NULL ; +xc_cpu_policy_t *policy =xc_cpu_policy_init ();int rc;DOMPRINTF_CALLED (dom->xch);assert (dom->max_vcpus); +/* +* Fetch the CPU policy of this domain. We need it to determine the APIC IDs +* each of vCPU in a manner consistent with the exported topology. +* +* TODO: It's silly to query a policy we have ourselves created. It should +* instead be part of xc_dom_image +*/ + + rc =xc_cpu_policy_get_domain (dom->xch, dom->guest_domid, policy); +if ( rc !=0 ) + { +xc_dom_panic (dom->xch,XC_INTERNAL_ERROR , +"%s: unable to fetch cpu policy for dom%u (rc=%d)" , + __func__, dom->guest_domid, rc); + goto out; + } +/* * Get the full HVM context in order to have the header, it is not * possible to get the header with getcontext_partial, and crafting one
@@ -1111,6 +1132,8 @@ static int vcpu_hvm(struct xc_dom_image *dom) vcpu_ctx.mtrr_d.typecode =HVM_SAVE_CODE (MTRR ); vcpu_ctx.mtrr_d.length =HVM_SAVE_LENGTH (MTRR ); vcpu_ctx.mtrr = *mtrr_record; + vcpu_ctx.lapic_d.typecode =HVM_SAVE_CODE (LAPIC ); + vcpu_ctx.lapic_d.length =HVM_SAVE_LENGTH (LAPIC ); vcpu_ctx.end_d = bsp_ctx.end_d; vcpu_ctx.end = bsp_ctx.end;
@@ -1124,6 +1147,17 @@ static int vcpu_hvm(struct xc_dom_image *dom) { vcpu_ctx.mtrr_d.instance = i; + lapic_record =hvm_get_save_record (full_ctx,HVM_SAVE_CODE (LAPIC ), i); +if ( !lapic_record ) + { +xc_dom_panic (dom->xch,XC_INTERNAL_ERROR , +"%s: unable to get LAPIC[%d] save record" , __func__, i); + goto out; + } + vcpu_ctx.lapic = *lapic_record; + vcpu_ctx.lapic.x2apic_id =x86_x2apic_id_from_vcpu_id (&policy->policy, i); + vcpu_ctx.lapic_d.instance = i; + rc =xc_domain_hvm_setcontext (dom->xch, dom->guest_domid, (uint8_t *)&vcpu_ctx,sizeof (vcpu_ctx));if ( rc !=0 )
@@ -1146,6 +1180,7 @@ static int vcpu_hvm(struct xc_dom_image *dom) out:free (full_ctx); +xc_cpu_policy_destroy (policy);return rc; }
Signature · 1 line
2.34.1