Skip to contentSkip to conversation

x86/hvm: Extend HVM cpuid leaf with vcpu id

PATCH Superseded Paul Durrant 5 participants 13 messages Nov 6, 2014 – Dec 11, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm: Extend HVM cpuid leaf with vcpu id

To perform certain hypercalls HVM guests need to use Xen's idea of vcpu id, which may well not match the guest OS idea of CPU id. This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
---
 xen/arch/x86/hvm/hvm.c              |    4 ++++
 xen/include/public/arch-x86/cpuid.h |    5 +++--
 2 files changed, 7 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/hvm.c +4 -0
index 78f519d..d9a5706 100644
@@ -4189,6 +4189,10 @@ void hvm_hypervisor_cpuid_leaf(uint32_t sub_idx,
          * foreign pages) has valid IOMMU entries.
          */
         *eax |= XEN_HVM_CPUID_IOMMU_MAPPINGS;
+
+        /* Indicate presence of vcpu id and set it in ebx */
+        *eax |= XEN_HVM_CPUID_VCPU_ID_PRESENT;
+        *ebx = current->vcpu_id;
     }
 }
 
xen/include/public/arch-x86/cpuid.h +3 -2
index 6005dfe..8ccb6e1 100644
@@ -76,13 +76,14 @@
 /*
  * Leaf 5 (0x40000x04)
  * HVM-specific features
+ * EAX: Features
+ * EBX: VCPU ID
  */
-
-/* EAX Features */
 #define XEN_HVM_CPUID_APIC_ACCESS_VIRT (1u << 0) /* Virtualized APIC registers */
 #define XEN_HVM_CPUID_X2APIC_VIRT      (1u << 1) /* Virtualized x2APIC accesses */
 /* Memory mapped from other domains has valid IOMMU entries */
 #define XEN_HVM_CPUID_IOMMU_MAPPINGS   (1u << 2)
+#define XEN_HVM_CPUID_VCPU_ID_PRESENT  (1u << 3) /* vcpu is present in EBX */
 
 #define XEN_CPUID_MAX_NUM_LEAVES 4
 
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 33 lines · Paul Durrant

To perform certain hypercalls HVM guests need to use Xen's idea of vcpu id, which may well not match the guest OS idea of CPU id. This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
---
 xen/arch/x86/hvm/hvm.c              |    4 ++++
 xen/include/public/arch-x86/cpuid.h |    5 +++--
 2 files changed, 7 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/hvm.c +4 -0
index 78f519d..d9a5706 100644
@@ -4189,6 +4189,10 @@ void hvm_hypervisor_cpuid_leaf(uint32_t sub_idx,
          * foreign pages) has valid IOMMU entries.
          */
         *eax |= XEN_HVM_CPUID_IOMMU_MAPPINGS;
+
+        /* Indicate presence of vcpu id and set it in ebx */
+        *eax |= XEN_HVM_CPUID_VCPU_ID_PRESENT;
+        *ebx = current->vcpu_id;
     }
 }
 
xen/include/public/arch-x86/cpuid.h +0 -0
index 6005dfe..8ccb6e1 100644

xen/include/public/arch-x86/cpuid.h line 80

Quoted 5 lines
xen/include/public/arch-x86/cpuid.h +2 -0
@@ -76,13 +76,14 @@
 /*
  * Leaf 5 (0x40000x04)
  * HVM-specific features
+ * EAX: Features
+ * EBX: VCPU ID

Probably want "iff EAX & VCPU_ID_PRESENT" in this comment.

xen/include/public/arch-x86/cpuid.h line 81

Quoted 3 lines
xen/include/public/arch-x86/cpuid.h +0 -2
  */
-
-/* EAX Features */

Spurious delete?

xen/include/public/arch-x86/cpuid.h line 86

Quoted 5 lines
xen/include/public/arch-x86/cpuid.h +1 -0
 #define XEN_HVM_CPUID_APIC_ACCESS_VIRT (1u << 0) /* Virtualized APIC registers */
 #define XEN_HVM_CPUID_X2APIC_VIRT      (1u << 1) /* Virtualized x2APIC accesses */
 /* Memory mapped from other domains has valid IOMMU entries */
 #define XEN_HVM_CPUID_IOMMU_MAPPINGS   (1u << 2)
+#define XEN_HVM_CPUID_VCPU_ID_PRESENT  (1u << 3) /* vcpu is present in EBX */

vcpu id?

~Andrew

Quoted 3 lines
xen/include/public/arch-x86/cpuid.h +0 -0
 #define XEN_CPUID_MAX_NUM_LEAVES 4
 
#2 lore raw
Quoted 40 lines

-----Original Message-----
From: Andrew Cooper
Sent: 06 November 2014 15:14
To: Paul Durrant; [email protected]
Cc: Keir (Xen.org); Jan Beulich
Subject: Re: [Xen-devel] [PATCH] x86/hvm: Extend HVM cpuid leaf with vcpu id

xen/arch/x86/hvm/hvm.c void hvm_hypervisor_cpuid_leaf(uint32_t

Quoted 18 lines · Paul Durrant

To perform certain hypercalls HVM guests need to use Xen's idea of vcpu id, which may well not match the guest OS idea of CPU id. This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
---
 xen/arch/x86/hvm/hvm.c              |    4 ++++
 xen/include/public/arch-x86/cpuid.h |    5 +++--
 2 files changed, 7 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/hvm.c +0 -0
index 78f519d..d9a5706 100644
@@ -4189,6 +4189,10 @@ void hvm_hypervisor_cpuid_leaf(uint32_t

sub_idx,

xen/include/public/arch-

Quoted 11 lines
xen/arch/x86/hvm/hvm.c +4 -0
          * foreign pages) has valid IOMMU entries.
          */
         *eax |= XEN_HVM_CPUID_IOMMU_MAPPINGS;
+
+        /* Indicate presence of vcpu id and set it in ebx */
+        *eax |= XEN_HVM_CPUID_VCPU_ID_PRESENT;
+        *ebx = current->vcpu_id;
     }
 }
 
xen/include/public/arch- renamed from xen/include/public/arch-x86/cpuid.h +0 -0

x86/cpuid.h

Quoted 12 lines

xen/include/public/arch-x86/cpuid.h line 80

Quoted 9 lines

index 6005dfe..8ccb6e1 100644

xen/include/public/arch-x86/cpuid.h +2 -0
@@ -76,13 +76,14 @@
 /*
  * Leaf 5 (0x40000x04)
  * HVM-specific features
+ * EAX: Features
+ * EBX: VCPU ID

Probably want "iff EAX & VCPU_ID_PRESENT" in this comment.

Yes, I guess.

Quoted 6 lines

xen/include/public/arch-x86/cpuid.h line 81

Quoted 3 lines
xen/include/public/arch-x86/cpuid.h +0 -2
  */
-
-/* EAX Features */

Spurious delete?

Nope - it moved up into the above block.

Quoted 10 lines

xen/include/public/arch-x86/cpuid.h line 82

Quoted 1 line
xen/include/public/arch-x86/cpuid.h +0 -0
 #define XEN_HVM_CPUID_APIC_ACCESS_VIRT (1u << 0) /* Virtualized

APIC registers */

xen/include/public/arch-x86/cpuid.h line 83

Quoted 1 line
xen/include/public/arch-x86/cpuid.h +0 -0
 #define XEN_HVM_CPUID_X2APIC_VIRT      (1u << 1) /* Virtualized x2APIC

accesses */

xen/include/public/arch-x86/cpuid.h

Quoted 3 lines
xen/include/public/arch-x86/cpuid.h +1 -0
 /* Memory mapped from other domains has valid IOMMU entries */
 #define XEN_HVM_CPUID_IOMMU_MAPPINGS   (1u << 2)
+#define XEN_HVM_CPUID_VCPU_ID_PRESENT  (1u << 3) /* vcpu is

present in EBX */

vcpu id?

Yes. Typo.

Paul

Quoted 6 lines

~Andrew

Quoted 3 lines
xen/include/public/arch-x86/cpuid.h +0 -0
 #define XEN_CPUID_MAX_NUM_LEAVES 4
 
#3 lore raw
Quoted 51 lines · Paul Durrant
Quoted 40 lines

-----Original Message-----
From: Andrew Cooper
Sent: 06 November 2014 15:14
To: Paul Durrant; [email protected]
Cc: Keir (Xen.org); Jan Beulich
Subject: Re: [Xen-devel] [PATCH] x86/hvm: Extend HVM cpuid leaf with vcpu id

xen/arch/x86/hvm/hvm.c void hvm_hypervisor_cpuid_leaf(uint32_t

Quoted 18 lines · Paul Durrant

To perform certain hypercalls HVM guests need to use Xen's idea of vcpu id, which may well not match the guest OS idea of CPU id. This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
---
 xen/arch/x86/hvm/hvm.c              |    4 ++++
 xen/include/public/arch-x86/cpuid.h |    5 +++--
 2 files changed, 7 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/hvm.c +0 -0
index 78f519d..d9a5706 100644
@@ -4189,6 +4189,10 @@ void hvm_hypervisor_cpuid_leaf(uint32_t

sub_idx,

xen/include/public/arch-

Quoted 11 lines
xen/arch/x86/hvm/hvm.c +4 -0
          * foreign pages) has valid IOMMU entries.
          */
         *eax |= XEN_HVM_CPUID_IOMMU_MAPPINGS;
+
+        /* Indicate presence of vcpu id and set it in ebx */
+        *eax |= XEN_HVM_CPUID_VCPU_ID_PRESENT;
+        *ebx = current->vcpu_id;
     }
 }
 
xen/include/public/arch- renamed from xen/include/public/arch-x86/cpuid.h +0 -0

x86/cpuid.h

Quoted 11 lines

xen/include/public/arch-x86/cpuid.h line 80

Quoted 9 lines

index 6005dfe..8ccb6e1 100644

xen/include/public/arch-x86/cpuid.h +2 -0
@@ -76,13 +76,14 @@
 /*
  * Leaf 5 (0x40000x04)
  * HVM-specific features
+ * EAX: Features
+ * EBX: VCPU ID

Probably want "iff EAX & VCPU_ID_PRESENT" in this comment.

Quoted 8 lines

Yes, I guess.

Quoted 5 lines

xen/include/public/arch-x86/cpuid.h line 81

Quoted 3 lines
xen/include/public/arch-x86/cpuid.h +0 -2
  */
-
-/* EAX Features */

Spurious delete?

Nope - it moved up into the above block.

Ah - I see now. That looks ok.

With the other changes, Reviewed-by: Andrew Cooper <[email protected]>

~Andrew

#4 lore raw
In reply to Paul Durrant
Quoted 1 line · Paul Durrant

To perform certain hypercalls HVM guests need to use Xen's idea of

What are those 'certain'?

Quoted 1 line

vcpu id, which may well not match the guest OS idea of CPU id.

Can you elaborate more on the use case please? And why only restrict this to HVM guests? Why not PV?

Quoted 2 lines

This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Don't we have existing hypercalls to get this? Ah VCPUOP_get_physid is what I was thinking of and that does not seem to be what you want?

Quoted 53 lines
Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
---
 xen/arch/x86/hvm/hvm.c              |    4 ++++
 xen/include/public/arch-x86/cpuid.h |    5 +++--
 2 files changed, 7 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/hvm.c +4 -0
index 78f519d..d9a5706 100644
@@ -4189,6 +4189,10 @@ void hvm_hypervisor_cpuid_leaf(uint32_t sub_idx,
          * foreign pages) has valid IOMMU entries.
          */
         *eax |= XEN_HVM_CPUID_IOMMU_MAPPINGS;
+
+        /* Indicate presence of vcpu id and set it in ebx */
+        *eax |= XEN_HVM_CPUID_VCPU_ID_PRESENT;
+        *ebx = current->vcpu_id;
     }
 }
 
xen/include/public/arch-x86/cpuid.h +3 -2
index 6005dfe..8ccb6e1 100644
@@ -76,13 +76,14 @@
 /*
  * Leaf 5 (0x40000x04)
  * HVM-specific features
+ * EAX: Features
+ * EBX: VCPU ID
  */
-
-/* EAX Features */
 #define XEN_HVM_CPUID_APIC_ACCESS_VIRT (1u << 0) /* Virtualized APIC registers */
 #define XEN_HVM_CPUID_X2APIC_VIRT      (1u << 1) /* Virtualized x2APIC accesses */
 /* Memory mapped from other domains has valid IOMMU entries */
 #define XEN_HVM_CPUID_IOMMU_MAPPINGS   (1u << 2)
+#define XEN_HVM_CPUID_VCPU_ID_PRESENT  (1u << 3) /* vcpu is present in EBX */
 
 #define XEN_CPUID_MAX_NUM_LEAVES 4
 
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#5 lore raw
Quoted 3 lines · Konrad Rzeszutek Wilk
Quoted 1 line · Paul Durrant

To perform certain hypercalls HVM guests need to use Xen's idea of

What are those 'certain'?

Some HVM hypercalls take a vcpu_id as a parameter. See the functionally-unrelated-but-companion patch "x86/hvm: Add per-vcpu evtchn upcalls".

HVM guests currently have no way of obtaining a xen vcpu_id for a given vcpu, and therefore have no way of passing an appropriate parameter to the hypercalls.

As it currently happens, HVM guests can (and sadly do) take their APIC id, divide by 2, and end up with a number which happens to match the Xen vcpu_id. This only works because Xen's allocation of APIC IDs is wrong on AMD hardware and Intel Hardware with hyperthreading, and is an issue I plan to fix in the not too distant future. (It is one of the quagmire of issues relating to cpuid policies)

Quoted 4 lines
Quoted 2 lines

vcpu id, which may well not match the guest OS idea of CPU id.

Can you elaborate more on the use case please? And why only restrict this to HVM guests? Why not PV?

PV guests unconditionally know their vcpu_id's right from the start, and indeed need them to bring up APs. HVM guests currently only know APIC IDs.

Quoted 6 lines
Quoted 3 lines

This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Don't we have existing hypercalls to get this? Ah VCPUOP_get_physid is what I was thinking of and that does not seem to be what you want?

This hypercall is only valid for pinned vcpus (i.e. only dom0 with opt_dom0_vcpus_pin), and gives back the underlying APIC ID, and the ACPI processor ID for that APIC ID.

The APIC ID can be obtained from cpuid as the vcpus are pinned, and the ACPI ID can be obtained from the ACPI tables, as the domain is dom0. Ergo, this hypercall is quite redundant, has nothing at all to do with the problem Paul is trying to solve, and appears buggy as it hands back two 64bit values, shifted 32bits apart, in a 64bit integer.

~Andrew

#6 lore raw
Quoted 5 lines · Andrew Cooper
Quoted 3 lines · Konrad Rzeszutek Wilk
Quoted 1 line · Paul Durrant

To perform certain hypercalls HVM guests need to use Xen's idea of

What are those 'certain'?

Quoted 7 lines

Some HVM hypercalls take a vcpu_id as a parameter. See the functionally-unrelated-but-companion patch "x86/hvm: Add per-vcpu evtchn upcalls".

HVM guests currently have no way of obtaining a xen vcpu_id for a given vcpu, and therefore have no way of passing an appropriate parameter to the hypercalls.

Sorry for chiming in late...

That's not strictly true. You can use the processor index in ACPI.

Quoted 6 lines

As it currently happens, HVM guests can (and sadly do) take their APIC id, divide by 2, and end up with a number which happens to match the Xen vcpu_id. This only works because Xen's allocation of APIC IDs is wrong on AMD hardware and Intel Hardware with hyperthreading, and is an issue I plan to fix in the not too distant future. (It is one of the quagmire of issues relating to cpuid policies)

This is absolutely the wrong thing to do. If a guest OS is doing this, it is doing the wrong thing. FreeBSD went down this erroneous path...

See http://lists.xen.org/archives/html/xen-devel/2013-05/msg03156.html

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

vcpu id, which may well not match the guest OS idea of CPU id.

Can you elaborate more on the use case please? And why only restrict this to HVM guests? Why not PV?

PV guests unconditionally know their vcpu_id's right from the start, and indeed need them to bring up APs. HVM guests currently only know APIC IDs.

No, don't assume anything about APIC IDs mapping to Xen virtual processor index. This will break things on EC2 instances that expose proper CPU topology through CPUID, and this does *not* follow the "index * 2" formula.

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Don't we have existing hypercalls to get this? Ah VCPUOP_get_physid is what I was thinking of and that does not seem to be what you want?

This hypercall is only valid for pinned vcpus (i.e. only dom0 with opt_dom0_vcpus_pin), and gives back the underlying APIC ID, and the ACPI processor ID for that APIC ID.

Quoted 5 lines

The APIC ID can be obtained from cpuid as the vcpus are pinned, and the ACPI ID can be obtained from the ACPI tables, as the domain is dom0. Ergo, this hypercall is quite redundant, has nothing at all to do with the problem Paul is trying to solve, and appears buggy as it hands back two 64bit values, shifted 32bits apart, in a 64bit integer.

And the Xen vCPU index can be determined by the processor object IDs in DSDT.

--msw

#7 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 10.12.14 at 07:00, <[email protected]> wrote:
Quoted 9 lines · Andrew Cooper
Quoted 3 lines · Konrad Rzeszutek Wilk
Quoted 1 line · Paul Durrant

To perform certain hypercalls HVM guests need to use Xen's idea of

What are those 'certain'?

Some HVM hypercalls take a vcpu_id as a parameter. See the functionally-unrelated-but-companion patch "x86/hvm: Add per-vcpu evtchn upcalls".

Quoted 7 lines
Quoted 3 lines

HVM guests currently have no way of obtaining a xen vcpu_id for a given vcpu, and therefore have no way of passing an appropriate parameter to the hypercalls.

Sorry for chiming in late...

That's not strictly true. You can use the processor index in ACPI.

Not really, no. The ACPI tables get created by hvmloader, without regard to Xen vCPU numbering afaict.

Quoted 11 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

vcpu id, which may well not match the guest OS idea of CPU id.

Can you elaborate more on the use case please? And why only restrict this to HVM guests? Why not PV?

PV guests unconditionally know their vcpu_id's right from the start, and indeed need them to bring up APs. HVM guests currently only know APIC IDs.

No, don't assume anything about APIC IDs mapping to Xen virtual processor index. This will break things on EC2 instances that expose proper CPU topology through CPUID, and this does *not* follow the "index * 2" formula.

Hmm, you say "no" first, but the rest of your statement seems to agree with what Andrew was saying...

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Don't we have existing hypercalls to get this? Ah VCPUOP_get_physid is what I was thinking of and that does not seem to be what you want?

This hypercall is only valid for pinned vcpus (i.e. only dom0 with opt_dom0_vcpus_pin), and gives back the underlying APIC ID, and the ACPI processor ID for that APIC ID.

Quoted 8 lines
Quoted 5 lines

The APIC ID can be obtained from cpuid as the vcpus are pinned, and the ACPI ID can be obtained from the ACPI tables, as the domain is dom0. Ergo, this hypercall is quite redundant, has nothing at all to do with the problem Paul is trying to solve, and appears buggy as it hands back two 64bit values, shifted 32bits apart, in a 64bit integer.

And the Xen vCPU index can be determined by the processor object IDs in DSDT.

I hope you don't have code doing so, as such an association isn't pinned down anywhere in the ABI afaik.

Jan

#9 lore raw
In reply to Matt Wilson
Quoted 9 lines · Matt Wilson
Quoted 8 lines · Andrew Cooper
Quoted 3 lines · Konrad Rzeszutek Wilk
Quoted 1 line · Paul Durrant

To perform certain hypercalls HVM guests need to use Xen's idea of

What are those 'certain'?

Some HVM hypercalls take a vcpu_id as a parameter. See the functionally-unrelated-but-companion patch "x86/hvm: Add per-vcpu evtchn upcalls".

Quoted 6 lines
Quoted 3 lines

HVM guests currently have no way of obtaining a xen vcpu_id for a given vcpu, and therefore have no way of passing an appropriate parameter to the hypercalls.

Sorry for chiming in late...

That's not strictly true. You can use the processor index in ACPI.

The LAPIC objects in the MADT/APIC table? That is still constructed with the broken LAPIC_ID = 2 * processor_id logic.

Quoted 31 lines
Quoted 7 lines

As it currently happens, HVM guests can (and sadly do) take their APIC id, divide by 2, and end up with a number which happens to match the Xen vcpu_id. This only works because Xen's allocation of APIC IDs is wrong on AMD hardware and Intel Hardware with hyperthreading, and is an issue I plan to fix in the not too distant future. (It is one of the quagmire of issues relating to cpuid policies)

This is absolutely the wrong thing to do. If a guest OS is doing this, it is doing the wrong thing. FreeBSD went down this erroneous path...

See http://lists.xen.org/archives/html/xen-devel/2013-05/msg03156.html

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

vcpu id, which may well not match the guest OS idea of CPU id.

Can you elaborate more on the use case please? And why only restrict this to HVM guests? Why not PV?

PV guests unconditionally know their vcpu_id's right from the start, and indeed need them to bring up APs. HVM guests currently only know APIC IDs.

No, don't assume anything about APIC IDs mapping to Xen virtual processor index. This will break things on EC2 instances that expose proper CPU topology through CPUID, and this does *not* follow the "index * 2" formula.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Don't we have existing hypercalls to get this? Ah VCPUOP_get_physid is what I was thinking of and that does not seem to be what you want?

This hypercall is only valid for pinned vcpus (i.e. only dom0 with opt_dom0_vcpus_pin), and gives back the underlying APIC ID, and the ACPI processor ID for that APIC ID.

Quoted 7 lines
Quoted 5 lines

The APIC ID can be obtained from cpuid as the vcpus are pinned, and the ACPI ID can be obtained from the ACPI tables, as the domain is dom0. Ergo, this hypercall is quite redundant, has nothing at all to do with the problem Paul is trying to solve, and appears buggy as it hands back two 64bit values, shifted 32bits apart, in a 64bit integer.

And the Xen vCPU index can be determined by the processor object IDs in DSDT.

The processor object IDs have no requirement in the spec to be in order, or contiguous. A DSDT processor object refers back to the MADT, which in turn still has the broken logic.

Also, any solution using ACPI tables is going to be an issue for PVH guests.

~Andrew

#8 lore raw
In reply to Jan Beulich
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 10.12.14 at 07:00, <[email protected]> wrote:
Quoted 12 lines · Andrew Cooper
Quoted 3 lines · Konrad Rzeszutek Wilk
Quoted 1 line · Paul Durrant

To perform certain hypercalls HVM guests need to use Xen's idea of

What are those 'certain'?

Some HVM hypercalls take a vcpu_id as a parameter. See the functionally-unrelated-but-companion patch "x86/hvm: Add per-vcpu evtchn upcalls".

HVM guests currently have no way of obtaining a xen vcpu_id for a given vcpu, and therefore have no way of passing an appropriate parameter to the hypercalls.

Quoted 6 lines
Quoted 3 lines

Sorry for chiming in late...

That's not strictly true. You can use the processor index in ACPI.

Not really, no. The ACPI tables get created by hvmloader, without regard to Xen vCPU numbering afaict.

The ACPI tables created by hvmloader must take Xen vCPU numbering into regard, at last as a consideration as both hvmloader and the hypervisor share this "vcpu_id * 2" math for assigning APIC IDs.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

vcpu id, which may well not match the guest OS idea of CPU id.

Can you elaborate more on the use case please? And why only restrict this to HVM guests? Why not PV?

PV guests unconditionally know their vcpu_id's right from the start, and indeed need them to bring up APs. HVM guests currently only know APIC IDs.

Quoted 7 lines
Quoted 4 lines

No, don't assume anything about APIC IDs mapping to Xen virtual processor index. This will break things on EC2 instances that expose proper CPU topology through CPUID, and this does *not* follow the "index * 2" formula.

Hmm, you say "no" first, but the rest of your statement seems to agree with what Andrew was saying...

I say "no" because the guest has more to work on than APIC IDs. It has ACPI processor object IDs.

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 5 lines
Quoted 2 lines

This patch adds vcpu id to the HVM cpuid leaf allowing the guest to build a mapping.

Don't we have existing hypercalls to get this? Ah VCPUOP_get_physid is what I was thinking of and that does not seem to be what you want?

This hypercall is only valid for pinned vcpus (i.e. only dom0 with opt_dom0_vcpus_pin), and gives back the underlying APIC ID, and the ACPI processor ID for that APIC ID.

The APIC ID can be obtained from cpuid as the vcpus are pinned, and the ACPI ID can be obtained from the ACPI tables, as the domain is dom0. Ergo, this hypercall is quite redundant, has nothing at all to do with the problem Paul is trying to solve, and appears buggy as it hands back two 64bit values, shifted 32bits apart, in a 64bit integer.

Quoted 5 lines
Quoted 2 lines

And the Xen vCPU index can be determined by the processor object IDs in DSDT.

I hope you don't have code doing so, as such an association isn't pinned down anywhere in the ABI afaik.

I think that both Linux and FreeBSD are using the enumeration of CPUs in ACPI tables to derive the vCPU ID needed for HYPERVISOR_vcpu_op hypercalls. For Linux, we use smp_processor_id() to get the vCPU ID. This, as far as I know, will enumerate as CPUs are presented in ACPI tables (where CPU 0, the boot CPU, will return 0 for smp_processor_id() and so on.).

--msw

#10 lore raw
In reply to Andrew Cooper
Quoted 6 lines · Andrew Cooper
Quoted 2 lines · Matt Wilson

That's not strictly true. You can use the processor index in ACPI.

The LAPIC objects in the MADT/APIC table? That is still constructed with the broken LAPIC_ID = 2 * processor_id logic.

Have you checked on an EC2 instance (for example: C3, I2, R3)?

Quoted 5 lines
Quoted 2 lines

And the Xen vCPU index can be determined by the processor object IDs in DSDT.

The processor object IDs have no requirement in the spec to be in order, or contiguous.

True - this is a weak heuristic today, so having a CPUID based approach would be more robust. (To be clear, I'm not arguing against the proposed patch - I just want to make sure that the justification doesn't give people the wrong impression about the functional way to derive Xen vCPU ID without it.)

Quoted 2 lines

A DSDT processor object refers back to the MADT, which in turn still has the broken logic.

See above. :-)

Quoted 1 line

Also, any solution using ACPI tables is going to be an issue for PVH guests.

PVH guests have the same facilities available to them for vCPU enumeration as PV guests, no?

--msw

#11 lore raw
Quoted 6 lines · Matt Wilson
Quoted 4 lines · Andrew Cooper
Quoted 1 line · Matt Wilson

That's not strictly true. You can use the processor index in ACPI.

The LAPIC objects in the MADT/APIC table? That is still constructed with the broken LAPIC_ID = 2 * processor_id logic.

Have you checked on an EC2 instance (for example: C3, I2, R3)?

I meant in terms of upstream code. I will believe you when you say that those instances do it differently.

My point was that the processor index is only as accurate as hvmloader makes it, and upstream, this relies on the broken logic.

Even if hvmloader is altered and does things differently, it is still bogus to draw any inference of vcpu_id from processor id.

~Andrew

#12 lore raw
Quoted 8 lines · Andrew Cooper
Quoted 6 lines · Matt Wilson
Quoted 4 lines · Andrew Cooper
Quoted 1 line · Matt Wilson

That's not strictly true. You can use the processor index in ACPI.

The LAPIC objects in the MADT/APIC table? That is still constructed with the broken LAPIC_ID = 2 * processor_id logic.

Have you checked on an EC2 instance (for example: C3, I2, R3)?

Quoted 8 lines

I meant in terms of upstream code. I will believe you when you say that those instances do it differently.

My point was that the processor index is only as accurate as hvmloader makes it, and upstream, this relies on the broken logic.

Even if hvmloader is altered and does things differently, it is still bogus to draw any inference of vcpu_id from processor id.

hvmloader accurately sets the processor_id in the MADT LAPIC tables to the correct vcpu_id value (by chance, though things would not work if this was not so).

hvmloader/acpi/build.c:

Quoted 12 lines
   for ( i = 0; i < nr_processor_objects; i++ )
   {
       memset(lapic, 0, sizeof(*lapic));
       lapic->type    = ACPI_PROCESSOR_LOCAL_APIC;
       lapic->length  = sizeof(*lapic);
       /* Processor ID must match processor-object IDs in the DSDT. */
-->     lapic->acpi_processor_id = i;
       lapic->apic_id = LAPIC_ID(i);
       lapic->flags = ((i < hvm_info->nr_vcpus) &&
                       test_bit(i, hvm_info->vcpu_online)
                       ? ACPI_LOCAL_APIC_ENABLED : 0);
       lapic++;
Quoted 1 line

}

See also hvmloader/config.h:

Quoted 1 line

#define LAPIC_ID(vcpu_id) ((vcpu_id) * 2)

See how Keir called this "vcpu_id"?

--msw