Skip to contentSkip to conversation

target-i386: Fix APIC-ID-based topology (v4)

PATCH Applied Eduardo Habkost 7 participants 43 messages Jan 17, 2013 – Jan 22, 2013 1 message not in the archive KVM

lore
Share & tools
lore
#0 lore raw

cover target-i386: Fix APIC-ID-based topology (v4)

I am hoping to get this bug fixed in 1.4. I didn't get much feedback on the RFC I sent last week, though.

Igor argued that APIC ID should be set by the board and not by the CPU itself, but I am not doing that because:
- I want to keep the bug fix simple and isolated as we are past soft freeze
- I believe the creator of the CPU object shouldn't be forced to provide the APIC ID, so the APIC ID is not unnecessarily exposed on the CPU hotplug device_add interface in the future
- The APIC ID _is_ set by the CPU itself (because each CPU package may have multiple core/threads, and each core/thread has a different APIC ID). What needs to be provided by the board to the CPU package in the future is the package ID and the bit width of the core/thread IDs.

Git tree for reference:
git://github.com/ehabkost/qemu-hacks.git apicid-topology.v5
https://github.com/ehabkost/qemu-hacks/tree/apicid-topology.v5

Eduardo Habkost (12):
kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou
KVM
target-i386: Don't set any KVM flag by default if KVM is disabled
pc: Reverse pc_init_pci() compatibility logic
kvm: Create kvm_arch_vcpu_id() function
target-i386: kvm: Set vcpu_id to APIC ID instead of CPU index
fw_cfg: Remove FW_CFG_MAX_CPUS from fw_cfg_init() target-i386/cpu: Introduce apic_id_for_cpu() function cpus.h: Make constant smp_cores/smp_threads available on *-user
pc: Set fw_cfg data based on APIC ID calculation
tests: Support target-specific unit tests
target-i386: Topology & APIC ID utility functions
pc: Generate APIC IDs according to CPU topology

 hw/fw_cfg.c            |   1 -
 hw/pc.c                |  44 +++++++++++++---
 hw/pc_piix.c           |  26 +++++++---
 hw/ppc_newworld.c      |   1 +
 hw/ppc_oldworld.c      |   1 +
 hw/sun4m.c             |   3 ++
 hw/sun4u.c             |   1 +
 include/sysemu/cpus.h  |   7 +++
 include/sysemu/kvm.h   |   4 ++
 kvm-all.c              |   2 +-
 target-i386/cpu.c      |  52 +++++++++++++++----
 target-i386/cpu.h      |   5 +-
 target-i386/kvm.c      |   6 +++
 target-i386/topology.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-ppc/kvm.c       |   5 ++
 target-s390x/kvm.c     |   5 ++
 tests/.gitignore       |   1 +
 tests/Makefile         |  21 +++++++-
 tests/test-x86-cpuid.c | 101 +++++++++++++++++++++++++++++++++++++
 19 files changed, 391 insertions(+), 28 deletions(-)
 create mode 100644 target-i386/topology.h
 create mode 100644 tests/test-x86-cpuid.c
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/12 kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou KVM

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

---
 include/sysemu/kvm.h | 1 +
 1 file changed, 1 insertion(+)
include/sysemu/kvm.h +1 -0
index 6bdd513..22acf91 100644
@@ -36,6 +36,7 @@
 #define KVM_FEATURE_ASYNC_PF     0
 #define KVM_FEATURE_STEAL_TIME   0
 #define KVM_FEATURE_PV_EOI       0
+#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 0
 #endif
 
 extern int kvm_allowed;
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [Qemu-devel] [PATCH for-1.4 01/12] kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou KVM

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 6 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

It appears as if Cc: below --- gets ignored? Please check and ping those people if necessary.

Personally, I adopted the convention of having to-be-persisted CCs above Sob and stripping trailing CCs when applying.

Andreas

Quoted 16 lines
---
 include/sysemu/kvm.h | 1 +
 1 file changed, 1 insertion(+)
include/sysemu/kvm.h +1 -0
index 6bdd513..22acf91 100644
@@ -36,6 +36,7 @@
 #define KVM_FEATURE_ASYNC_PF     0
 #define KVM_FEATURE_STEAL_TIME   0
 #define KVM_FEATURE_PV_EOI       0
+#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 0
 #endif
 
 extern int kvm_allowed;
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#3 lore raw

Re: [PATCH for-1.4 01/12] kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou KVM

Quoted 10 lines · Andreas Färber

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 6 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

It appears as if Cc: below --- gets ignored? Please check and ping those people if necessary.

I believe it depends on git-send-email suppress-cc option. At least I remember this format have worked for me before, I need to check what changed on my config.

Quoted 3 lines

Personally, I adopted the convention of having to-be-persisted CCs above Sob and stripping trailing CCs when applying.

I considered the CC lines not part of the commit message (just like the changelog isn't), so I decided to put them after "---".

Quoted 23 lines

Andreas

Quoted 16 lines
---
 include/sysemu/kvm.h | 1 +
 1 file changed, 1 insertion(+)
include/sysemu/kvm.h +1 -0
index 6bdd513..22acf91 100644
@@ -36,6 +36,7 @@
 #define KVM_FEATURE_ASYNC_PF     0
 #define KVM_FEATURE_STEAL_TIME   0
 #define KVM_FEATURE_PV_EOI       0
+#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 0
 #endif
 
 extern int kvm_allowed;
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Signature · 1 line
Eduardo
#4 lore raw

Re: [PATCH for-1.4 01/12] kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou KVM

Quoted 17 lines · Eduardo Habkost
Quoted 10 lines · Andreas Färber

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 6 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

It appears as if Cc: below --- gets ignored? Please check and ping those people if necessary.

I believe it depends on git-send-email suppress-cc option. At least I remember this format have worked for me before, I need to check what changed on my config.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Personally, I adopted the convention of having to-be-persisted CCs above Sob and stripping trailing CCs when applying.

I considered the CC lines not part of the commit message (just like the changelog isn't), so I decided to put them after "---".

This may be the reason git-send-email ignored it.

Quoted 18 lines
Quoted 18 lines

Andreas

Quoted 15 lines
---
 include/sysemu/kvm.h | 1 +
 1 file changed, 1 insertion(+)
include/sysemu/kvm.h +1 -0
index 6bdd513..22acf91 100644
@@ -36,6 +36,7 @@
 #define KVM_FEATURE_ASYNC_PF     0
 #define KVM_FEATURE_STEAL_TIME   0
 #define KVM_FEATURE_PV_EOI       0
+#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 0
 #endif
 
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
include/sysemu/kvm.h +0 -0
 extern int kvm_allowed;
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Signature · 1 line
Eduardo

--
Gleb.

#5 lore raw

Re: [PATCH for-1.4 01/12] kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou KVM

Quoted 18 lines · Gleb Natapov
Quoted 17 lines · Eduardo Habkost
Quoted 10 lines · Andreas Färber

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 6 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

It appears as if Cc: below --- gets ignored? Please check and ping those people if necessary.

I believe it depends on git-send-email suppress-cc option. At least I remember this format have worked for me before, I need to check what changed on my config.

Quoted 1 line
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

Personally, I adopted the convention of having to-be-persisted CCs above Sob and stripping trailing CCs when applying.

I considered the CC lines not part of the commit message (just like the changelog isn't), so I decided to put them after "---".

This may be the reason git-send-email ignored it.

I just tested it here, and it ignored it because I have suppress-cc=all enabled by default and (by mistake) I didn't use my script that explicitly disabled it. If I use --suppress-cc=author, git-send-email uses the CC lines before _and_ after "---".

Signature · 1 line
Eduardo

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Re: [PATCH for-1.4 01/12] kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou KVM

Am 22.01.2013 02:43, schrieb Marcelo Tosatti:

Quoted 22 lines · Eduardo Habkost
Quoted 21 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

---
 include/sysemu/kvm.h | 1 +
 1 file changed, 1 insertion(+)
include/sysemu/kvm.h +1 -0
index 6bdd513..22acf91 100644
@@ -36,6 +36,7 @@
 #define KVM_FEATURE_ASYNC_PF     0
 #define KVM_FEATURE_STEAL_TIME   0
 #define KVM_FEATURE_PV_EOI       0
+#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 0
 #endif
 
Quoted 6 lines
Quoted 4 lines
include/sysemu/kvm.h +0 -0
 extern int kvm_allowed;
Signature · 1 line
1.7.11.7

ACK

BTW is it the general strategy to add these as needed for new patches? Or should we add all current ones and mandate adding such dummy definitions when new ones get introduced via linux-headers/ update?

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#8 lore raw

Re: [Qemu-devel] [PATCH for-1.4 01/12] kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou KVM

Quoted 30 lines · Andreas Färber

Am 22.01.2013 02:43, schrieb Marcelo Tosatti:

Quoted 22 lines · Eduardo Habkost
Quoted 21 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

---
 include/sysemu/kvm.h | 1 +
 1 file changed, 1 insertion(+)
include/sysemu/kvm.h +1 -0
index 6bdd513..22acf91 100644
@@ -36,6 +36,7 @@
 #define KVM_FEATURE_ASYNC_PF     0
 #define KVM_FEATURE_STEAL_TIME   0
 #define KVM_FEATURE_PV_EOI       0
+#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 0
 #endif
 
Quoted 6 lines
Quoted 4 lines
include/sysemu/kvm.h +0 -0
 extern int kvm_allowed;
Signature · 1 line
1.7.11.7

ACK

Quoted 3 lines

BTW is it the general strategy to add these as needed for new patches? Or should we add all current ones and mandate adding such dummy definitions when new ones get introduced via linux-headers/ update?

I meant to include all existing bits in a single patch previously, but somehow I missed the KVM_FEATURE_CLOCKSOURCE_STABLE_BIT definition when looking at the kernel header.

It would be nice to automatically refresh the fake-defines list when updating linux-headers, but I won't mind if we update the list (pulling all existing bits again) only when QEMU starts using a define that is missing.

Signature · 1 line
Eduardo
#9 lore raw

Re: [PATCH for-1.4 01/12] kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou KVM

In reply to Andreas Färber
Quoted 30 lines · Andreas Färber

Am 22.01.2013 02:43, schrieb Marcelo Tosatti:

Quoted 22 lines · Eduardo Habkost
Quoted 21 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

---
 include/sysemu/kvm.h | 1 +
 1 file changed, 1 insertion(+)
include/sysemu/kvm.h +1 -0
index 6bdd513..22acf91 100644
@@ -36,6 +36,7 @@
 #define KVM_FEATURE_ASYNC_PF     0
 #define KVM_FEATURE_STEAL_TIME   0
 #define KVM_FEATURE_PV_EOI       0
+#define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 0
 #endif
 
Quoted 6 lines
Quoted 4 lines
include/sysemu/kvm.h +0 -0
 extern int kvm_allowed;
Signature · 1 line
1.7.11.7

ACK

Quoted 3 lines

BTW is it the general strategy to add these as needed for new patches? Or should we add all current ones and mandate adding such dummy definitions when new ones get introduced via linux-headers/ update?

Its a good idea to update the sync scripts to automatically create the dummy ones, i suppose (there is no proactive strategy).

#10 lore raw

PATCH 2/12 target-i386: Don't set any KVM flag by default if KVM is disabled

In reply to Eduardo Habkost

This is a cleanup that tries to solve two small issues:

- We don't need a separate kvm_pv_eoi_features variable just to keep a constant calculated at compile-time, and this style would require adding a separate variable (that's declared twice because of the CONFIG_KVM ifdef) for each feature that's going to be enabled/disable by machine-type compat code.
- The pc-1.3 code is setting the kvm_pv_eoi flag on cpuid_kvm_features even when KVM is disabled at runtime. This small incosistency in the cpuid_kvm_features field isn't a problem today because cpuid_kvm_features is ignored by the TCG code, but it may cause unexpected problems later when refactoring the CPUID handling code.

This patch eliminates the kvm_pv_eoi_features variable and simply uses kvm_enabled() inside the enable_kvm_pv_eoi() compat function, so it enables kvm_pv_eoi only if KVM is enabled. I believe this makes the behavior of enable_kvm_pv_eoi() clearer and easier to understand.

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

Changes v2:
- Coding style fix

Changes v3:
- Eliminate #ifdef by using the fake KVM_FEATURE_PV_EOI #define

Changes v4:
- Check kvm_enabled() when actually using kvm_default_features
- Eliminate Yet Another #ifdef by using the fake KVM_FEATURE_* #defines on kvm_default_features initialization

Changes v5:
- Rebase and move the kvm_enabled() check to cpu_x86_register()

---
 target-i386/cpu.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
target-i386/cpu.c +4 -8
index 333745b..754eb6f 100644
@@ -206,22 +206,16 @@ typedef struct model_features_t {
 int check_cpuid = 0;
 int enforce_cpuid = 0;
 
-#if defined(CONFIG_KVM)
 static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
         (1 << KVM_FEATURE_NOP_IO_DELAY) |
         (1 << KVM_FEATURE_CLOCKSOURCE2) |
         (1 << KVM_FEATURE_ASYNC_PF) |
         (1 << KVM_FEATURE_STEAL_TIME) |
         (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
-static const uint32_t kvm_pv_eoi_features = (0x1 << KVM_FEATURE_PV_EOI);
-#else
-static uint32_t kvm_default_features = 0;
-static const uint32_t kvm_pv_eoi_features = 0;
-#endif
 
 void enable_kvm_pv_eoi(void)
 {
-    kvm_default_features |= kvm_pv_eoi_features;
+    kvm_default_features |= (1UL << KVM_FEATURE_PV_EOI);
 }
 
 void host_cpuid(uint32_t function, uint32_t count,
@@ -1602,7 +1596,9 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
         goto out;
     }
 
-    def->kvm_features |= kvm_default_features;
+    if (kvm_enabled()) {
+        def->kvm_features |= kvm_default_features;
+    }
     def->ext_features |= CPUID_EXT_HYPERVISOR;
 
     if (cpu_x86_parse_featurestr(def, features) < 0) {
Signature · 1 line
1.7.11.7
#11 lore raw

Re: [Qemu-devel] [PATCH for-1.4 02/12] target-i386: Don't set any KVM flag by default if KVM is disabled

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 13 lines

This is a cleanup that tries to solve two small issues:

- We don't need a separate kvm_pv_eoi_features variable just to keep a constant calculated at compile-time, and this style would require adding a separate variable (that's declared twice because of the CONFIG_KVM ifdef) for each feature that's going to be enabled/disable by machine-type compat code.
- The pc-1.3 code is setting the kvm_pv_eoi flag on cpuid_kvm_features even when KVM is disabled at runtime. This small incosistency in the cpuid_kvm_features field isn't a problem today because cpuid_kvm_features is ignored by the TCG code, but it may cause unexpected problems later when refactoring the CPUID handling code.

Quoted 6 lines

This patch eliminates the kvm_pv_eoi_features variable and simply uses kvm_enabled() inside the enable_kvm_pv_eoi() compat function, so it enables kvm_pv_eoi only if KVM is enabled. I believe this makes the behavior of enable_kvm_pv_eoi() clearer and easier to understand.

Fine with me if KVM folks agree.

Andreas

Quoted 63 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

Changes v2:
- Coding style fix

Changes v3:
- Eliminate #ifdef by using the fake KVM_FEATURE_PV_EOI #define

Changes v4:
- Check kvm_enabled() when actually using kvm_default_features
- Eliminate Yet Another #ifdef by using the fake KVM_FEATURE_* #defines on kvm_default_features initialization

Changes v5:
- Rebase and move the kvm_enabled() check to cpu_x86_register()

---
 target-i386/cpu.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
target-i386/cpu.c +4 -8
index 333745b..754eb6f 100644
@@ -206,22 +206,16 @@ typedef struct model_features_t {
 int check_cpuid = 0;
 int enforce_cpuid = 0;
 
-#if defined(CONFIG_KVM)
 static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
         (1 << KVM_FEATURE_NOP_IO_DELAY) |
         (1 << KVM_FEATURE_CLOCKSOURCE2) |
         (1 << KVM_FEATURE_ASYNC_PF) |
         (1 << KVM_FEATURE_STEAL_TIME) |
         (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
-static const uint32_t kvm_pv_eoi_features = (0x1 << KVM_FEATURE_PV_EOI);
-#else
-static uint32_t kvm_default_features = 0;
-static const uint32_t kvm_pv_eoi_features = 0;
-#endif
 
 void enable_kvm_pv_eoi(void)
 {
-    kvm_default_features |= kvm_pv_eoi_features;
+    kvm_default_features |= (1UL << KVM_FEATURE_PV_EOI);
 }
 
 void host_cpuid(uint32_t function, uint32_t count,
@@ -1602,7 +1596,9 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
         goto out;
     }
 
-    def->kvm_features |= kvm_default_features;
+    if (kvm_enabled()) {
+        def->kvm_features |= kvm_default_features;
+    }
     def->ext_features |= CPUID_EXT_HYPERVISOR;
 
     if (cpu_x86_parse_featurestr(def, features) < 0) {
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Acked-by
#12 lore raw

Re: [PATCH for-1.4 02/12] target-i386: Don't set any KVM flag by default if KVM is disabled

Quoted 14 lines · Andreas Färber

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 13 lines

This is a cleanup that tries to solve two small issues:

- We don't need a separate kvm_pv_eoi_features variable just to keep a constant calculated at compile-time, and this style would require adding a separate variable (that's declared twice because of the CONFIG_KVM ifdef) for each feature that's going to be enabled/disable by machine-type compat code.
- The pc-1.3 code is setting the kvm_pv_eoi flag on cpuid_kvm_features even when KVM is disabled at runtime. This small incosistency in the cpuid_kvm_features field isn't a problem today because cpuid_kvm_features is ignored by the TCG code, but it may cause unexpected problems later when refactoring the CPUID handling code.

Quoted 9 lines
Quoted 6 lines

This patch eliminates the kvm_pv_eoi_features variable and simply uses kvm_enabled() inside the enable_kvm_pv_eoi() compat function, so it enables kvm_pv_eoi only if KVM is enabled. I believe this makes the behavior of enable_kvm_pv_eoi() clearer and easier to understand.

Fine with me if KVM folks agree.

Quoted 57 lines

Andreas

Quoted 55 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

Changes v2:
- Coding style fix

Changes v3:
- Eliminate #ifdef by using the fake KVM_FEATURE_PV_EOI #define

Changes v4:
- Check kvm_enabled() when actually using kvm_default_features
- Eliminate Yet Another #ifdef by using the fake KVM_FEATURE_* #defines on kvm_default_features initialization

Changes v5:
- Rebase and move the kvm_enabled() check to cpu_x86_register()

---
 target-i386/cpu.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
target-i386/cpu.c +1 -7
index 333745b..754eb6f 100644
@@ -206,22 +206,16 @@ typedef struct model_features_t {
 int check_cpuid = 0;
 int enforce_cpuid = 0;
 
-#if defined(CONFIG_KVM)
 static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
         (1 << KVM_FEATURE_NOP_IO_DELAY) |
         (1 << KVM_FEATURE_CLOCKSOURCE2) |
         (1 << KVM_FEATURE_ASYNC_PF) |
         (1 << KVM_FEATURE_STEAL_TIME) |
         (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
-static const uint32_t kvm_pv_eoi_features = (0x1 << KVM_FEATURE_PV_EOI);
-#else
-static uint32_t kvm_default_features = 0;
-static const uint32_t kvm_pv_eoi_features = 0;
-#endif
 
 void enable_kvm_pv_eoi(void)
 {
-    kvm_default_features |= kvm_pv_eoi_features;
+    kvm_default_features |= (1UL << KVM_FEATURE_PV_EOI);
 }
 
 void host_cpuid(uint32_t function, uint32_t count,
@@ -1602,7 +1596,9 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
         goto out;
     }
 
Quoted 13 lines
Quoted 8 lines
target-i386/cpu.c +3 -1
-    def->kvm_features |= kvm_default_features;
+    if (kvm_enabled()) {
+        def->kvm_features |= kvm_default_features;
+    }
     def->ext_features |= CPUID_EXT_HYPERVISOR;
 
     if (cpu_x86_parse_featurestr(def, features) < 0) {
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg

--
Gleb.

#13 lore raw

PATCH 3/12 pc: Reverse pc_init_pci() compatibility logic

In reply to Eduardo Habkost

Currently, the pc-1.4 machine init function enables PV EOI and then calls the pc-1.2 machine init function. The problem with this approach is that now we can't enable any additional compatibility code inside the pc-1.2 init function because it would end up enabling the compatibility behavior on pc-1.3 and pc-1.4 as well.

This reverses the logic so that the pc-1.2 machine init function will disable PV EOI, and then call the pc-1.4 machine init function.

This way we can change older machine-types to enable compatibility behavior, and the newer machine-types (pc-1.3, pc-q35-1.4 and pc-i440fx-1.4) would just use the default behavior.

(This means that one nice side-effect of this change is that pc-q35-1.4 will get PV EOI enabled by default, too)

It would be interesting to eventually change pc_init_pci_no_kvmclock() and pc_init_isa() to reuse pc_init_pci_1_2() as well (so we don't need to duplicate compatibility code on those two functions). But this will be probably much easier to do after we create a PCInitArgs struct for the PC initialization arguments, and/or after we use global-properties to implement the compatibility modes present in pc_init_pci_1_2().

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

---
 hw/pc_piix.c      | 22 +++++++++++++---------
 target-i386/cpu.c |  5 +++--
 target-i386/cpu.h |  2 +-
 3 files changed, 17 insertions(+), 12 deletions(-)
hw/pc_piix.c +13 -9
index 0a6923d..f9cfe78 100644
@@ -233,12 +233,14 @@ static void pc_init_pci(QEMUMachineInitArgs *args)
              initrd_filename, cpu_model, 1, 1);
 }
 
-static void pc_init_pci_1_3(QEMUMachineInitArgs *args)
+/* PC machine init function for pc-0.14 to pc-1.2 */
+static void pc_init_pci_1_2(QEMUMachineInitArgs *args)
 {
-    enable_kvm_pv_eoi();
+    disable_kvm_pv_eoi();
     pc_init_pci(args);
 }
 
+/* PC init function for pc-0.10 to pc-0.13, and reused by xenfv */
 static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs *args)
 {
     ram_addr_t ram_size = args->ram_size;
@@ -247,6 +249,7 @@ static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs *args)
     const char *kernel_cmdline = args->kernel_cmdline;
     const char *initrd_filename = args->initrd_filename;
     const char *boot_device = args->boot_device;
+    disable_kvm_pv_eoi();
     pc_init1(get_system_memory(),
              get_system_io(),
              ram_size, boot_device,
@@ -264,6 +267,7 @@ static void pc_init_isa(QEMUMachineInitArgs *args)
     const char *boot_device = args->boot_device;
     if (cpu_model == NULL)
         cpu_model = "486";
+    disable_kvm_pv_eoi();
     pc_init1(get_system_memory(),
              get_system_io(),
              ram_size, boot_device,
@@ -286,7 +290,7 @@ static QEMUMachine pc_i440fx_machine_v1_4 = {
     .name = "pc-i440fx-1.4",
     .alias = "pc",
     .desc = "Standard PC (i440FX + PIIX, 1996)",
-    .init = pc_init_pci_1_3,
+    .init = pc_init_pci,
     .max_cpus = 255,
     .is_default = 1,
     DEFAULT_MACHINE_OPTIONS,
@@ -302,7 +306,7 @@ static QEMUMachine pc_i440fx_machine_v1_4 = {
 static QEMUMachine pc_machine_v1_3 = {
     .name = "pc-1.3",
     .desc = "Standard PC",
-    .init = pc_init_pci_1_3,
+    .init = pc_init_pci,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_3,
@@ -342,7 +346,7 @@ static QEMUMachine pc_machine_v1_3 = {
 static QEMUMachine pc_machine_v1_2 = {
     .name = "pc-1.2",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_2,
@@ -386,7 +390,7 @@ static QEMUMachine pc_machine_v1_2 = {
 static QEMUMachine pc_machine_v1_1 = {
     .name = "pc-1.1",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_1,
@@ -422,7 +426,7 @@ static QEMUMachine pc_machine_v1_1 = {
 static QEMUMachine pc_machine_v1_0 = {
     .name = "pc-1.0",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_0,
@@ -438,7 +442,7 @@ static QEMUMachine pc_machine_v1_0 = {
 static QEMUMachine pc_machine_v0_15 = {
     .name = "pc-0.15",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_0_15,
@@ -471,7 +475,7 @@ static QEMUMachine pc_machine_v0_15 = {
 static QEMUMachine pc_machine_v0_14 = {
     .name = "pc-0.14",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_0_14, 
target-i386/cpu.c +3 -2
index 754eb6f..d1a14d5 100644
@@ -211,11 +211,12 @@ static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
         (1 << KVM_FEATURE_CLOCKSOURCE2) |
         (1 << KVM_FEATURE_ASYNC_PF) |
         (1 << KVM_FEATURE_STEAL_TIME) |
+        (1 << KVM_FEATURE_PV_EOI) |
         (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
 
-void enable_kvm_pv_eoi(void)
+void disable_kvm_pv_eoi(void)
 {
-    kvm_default_features |= (1UL << KVM_FEATURE_PV_EOI);
+    kvm_default_features &= ~(1UL << KVM_FEATURE_PV_EOI);
 }
 
 void host_cpuid(uint32_t function, uint32_t count,
target-i386/cpu.h +1 -1
index 4e091cd..9d4fcf9 100644
@@ -1250,7 +1250,7 @@ void do_smm_enter(CPUX86State *env1);
 
 void cpu_report_tpr_access(CPUX86State *env, TPRAccess access);
 
-void enable_kvm_pv_eoi(void);
+void disable_kvm_pv_eoi(void);
 
 /* Return name of 32-bit register, from a R_* constant */
 const char *get_register_name_32(unsigned int reg);
Signature · 1 line
1.7.11.7
#14 lore raw

Re: [Qemu-devel] [PATCH for-1.4 03/12] pc: Reverse pc_init_pci() compatibility logic

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 23 lines

Currently, the pc-1.4 machine init function enables PV EOI and then calls the pc-1.2 machine init function. The problem with this approach is that now we can't enable any additional compatibility code inside the pc-1.2 init function because it would end up enabling the compatibility behavior on pc-1.3 and pc-1.4 as well.

This reverses the logic so that the pc-1.2 machine init function will disable PV EOI, and then call the pc-1.4 machine init function.

This way we can change older machine-types to enable compatibility behavior, and the newer machine-types (pc-1.3, pc-q35-1.4 and pc-i440fx-1.4) would just use the default behavior.

(This means that one nice side-effect of this change is that pc-q35-1.4 will get PV EOI enabled by default, too)

It would be interesting to eventually change pc_init_pci_no_kvmclock() and pc_init_isa() to reuse pc_init_pci_1_2() as well (so we don't need to duplicate compatibility code on those two functions). But this will be probably much easier to do after we create a PCInitArgs struct for the PC initialization arguments, and/or after we use global-properties to implement the compatibility modes present in pc_init_pci_1_2().

Quoted 6 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

Ping! mst, you handled a previous PC machine compatibility patch - can you ack or nack?

Eduardo, which of the following patches depend on this one? Only 12/12?

Andreas

Quoted 141 lines
---
 hw/pc_piix.c      | 22 +++++++++++++---------
 target-i386/cpu.c |  5 +++--
 target-i386/cpu.h |  2 +-
 3 files changed, 17 insertions(+), 12 deletions(-)
hw/pc_piix.c +13 -9
index 0a6923d..f9cfe78 100644
@@ -233,12 +233,14 @@ static void pc_init_pci(QEMUMachineInitArgs *args)
              initrd_filename, cpu_model, 1, 1);
 }
 
-static void pc_init_pci_1_3(QEMUMachineInitArgs *args)
+/* PC machine init function for pc-0.14 to pc-1.2 */
+static void pc_init_pci_1_2(QEMUMachineInitArgs *args)
 {
-    enable_kvm_pv_eoi();
+    disable_kvm_pv_eoi();
     pc_init_pci(args);
 }
 
+/* PC init function for pc-0.10 to pc-0.13, and reused by xenfv */
 static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs *args)
 {
     ram_addr_t ram_size = args->ram_size;
@@ -247,6 +249,7 @@ static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs *args)
     const char *kernel_cmdline = args->kernel_cmdline;
     const char *initrd_filename = args->initrd_filename;
     const char *boot_device = args->boot_device;
+    disable_kvm_pv_eoi();
     pc_init1(get_system_memory(),
              get_system_io(),
              ram_size, boot_device,
@@ -264,6 +267,7 @@ static void pc_init_isa(QEMUMachineInitArgs *args)
     const char *boot_device = args->boot_device;
     if (cpu_model == NULL)
         cpu_model = "486";
+    disable_kvm_pv_eoi();
     pc_init1(get_system_memory(),
              get_system_io(),
              ram_size, boot_device,
@@ -286,7 +290,7 @@ static QEMUMachine pc_i440fx_machine_v1_4 = {
     .name = "pc-i440fx-1.4",
     .alias = "pc",
     .desc = "Standard PC (i440FX + PIIX, 1996)",
-    .init = pc_init_pci_1_3,
+    .init = pc_init_pci,
     .max_cpus = 255,
     .is_default = 1,
     DEFAULT_MACHINE_OPTIONS,
@@ -302,7 +306,7 @@ static QEMUMachine pc_i440fx_machine_v1_4 = {
 static QEMUMachine pc_machine_v1_3 = {
     .name = "pc-1.3",
     .desc = "Standard PC",
-    .init = pc_init_pci_1_3,
+    .init = pc_init_pci,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_3,
@@ -342,7 +346,7 @@ static QEMUMachine pc_machine_v1_3 = {
 static QEMUMachine pc_machine_v1_2 = {
     .name = "pc-1.2",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_2,
@@ -386,7 +390,7 @@ static QEMUMachine pc_machine_v1_2 = {
 static QEMUMachine pc_machine_v1_1 = {
     .name = "pc-1.1",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_1,
@@ -422,7 +426,7 @@ static QEMUMachine pc_machine_v1_1 = {
 static QEMUMachine pc_machine_v1_0 = {
     .name = "pc-1.0",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_0,
@@ -438,7 +442,7 @@ static QEMUMachine pc_machine_v1_0 = {
 static QEMUMachine pc_machine_v0_15 = {
     .name = "pc-0.15",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_0_15,
@@ -471,7 +475,7 @@ static QEMUMachine pc_machine_v0_15 = {
 static QEMUMachine pc_machine_v0_14 = {
     .name = "pc-0.14",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_0_14, 
target-i386/cpu.c +3 -2
index 754eb6f..d1a14d5 100644
@@ -211,11 +211,12 @@ static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
         (1 << KVM_FEATURE_CLOCKSOURCE2) |
         (1 << KVM_FEATURE_ASYNC_PF) |
         (1 << KVM_FEATURE_STEAL_TIME) |
+        (1 << KVM_FEATURE_PV_EOI) |
         (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
 
-void enable_kvm_pv_eoi(void)
+void disable_kvm_pv_eoi(void)
 {
-    kvm_default_features |= (1UL << KVM_FEATURE_PV_EOI);
+    kvm_default_features &= ~(1UL << KVM_FEATURE_PV_EOI);
 }
 
 void host_cpuid(uint32_t function, uint32_t count,
target-i386/cpu.h +1 -1
index 4e091cd..9d4fcf9 100644
@@ -1250,7 +1250,7 @@ void do_smm_enter(CPUX86State *env1);
 
 void cpu_report_tpr_access(CPUX86State *env, TPRAccess access);
 
-void enable_kvm_pv_eoi(void);
+void disable_kvm_pv_eoi(void);
 
 /* Return name of 32-bit register, from a R_* constant */
 const char *get_register_name_32(unsigned int reg);
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#15 lore raw

Re: [PATCH for-1.4 03/12] pc: Reverse pc_init_pci() compatibility logic

Quoted 31 lines · Andreas Färber

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 23 lines

Currently, the pc-1.4 machine init function enables PV EOI and then calls the pc-1.2 machine init function. The problem with this approach is that now we can't enable any additional compatibility code inside the pc-1.2 init function because it would end up enabling the compatibility behavior on pc-1.3 and pc-1.4 as well.

This reverses the logic so that the pc-1.2 machine init function will disable PV EOI, and then call the pc-1.4 machine init function.

This way we can change older machine-types to enable compatibility behavior, and the newer machine-types (pc-1.3, pc-q35-1.4 and pc-i440fx-1.4) would just use the default behavior.

(This means that one nice side-effect of this change is that pc-q35-1.4 will get PV EOI enabled by default, too)

It would be interesting to eventually change pc_init_pci_no_kvmclock() and pc_init_isa() to reuse pc_init_pci_1_2() as well (so we don't need to duplicate compatibility code on those two functions). But this will be probably much easier to do after we create a PCInitArgs struct for the PC initialization arguments, and/or after we use global-properties to implement the compatibility modes present in pc_init_pci_1_2().

Quoted 6 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

Quoted 4 lines

Ping! mst, you handled a previous PC machine compatibility patch - can you ack or nack?

Eduardo, which of the following patches depend on this one? Only 12/12?

Yes, only 12/12 depend on it (it is the patch that finally introduces the fix, on pc-1.4 only).

Signature · 1 line
Eduardo
Acked-by on v1 3/12
#16 lore raw

Re: [Qemu-devel] [PATCH for-1.4 03/12] pc: Reverse pc_init_pci() compatibility logic

In reply to Eduardo Habkost
Quoted 23 lines · Eduardo Habkost

Currently, the pc-1.4 machine init function enables PV EOI and then calls the pc-1.2 machine init function. The problem with this approach is that now we can't enable any additional compatibility code inside the pc-1.2 init function because it would end up enabling the compatibility behavior on pc-1.3 and pc-1.4 as well.

This reverses the logic so that the pc-1.2 machine init function will disable PV EOI, and then call the pc-1.4 machine init function.

This way we can change older machine-types to enable compatibility behavior, and the newer machine-types (pc-1.3, pc-q35-1.4 and pc-i440fx-1.4) would just use the default behavior.

(This means that one nice side-effect of this change is that pc-q35-1.4 will get PV EOI enabled by default, too)

It would be interesting to eventually change pc_init_pci_no_kvmclock() and pc_init_isa() to reuse pc_init_pci_1_2() as well (so we don't need to duplicate compatibility code on those two functions). But this will be probably much easier to do after we create a PCInitArgs struct for the PC initialization arguments, and/or after we use global-properties to implement the compatibility modes present in pc_init_pci_1_2().

Quoted 8 lines

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

I wasn't actually Cc'd :).
I don't see anything wrong with this patch.

Quoted 141 lines
---
 hw/pc_piix.c      | 22 +++++++++++++---------
 target-i386/cpu.c |  5 +++--
 target-i386/cpu.h |  2 +-
 3 files changed, 17 insertions(+), 12 deletions(-)
hw/pc_piix.c +13 -9
index 0a6923d..f9cfe78 100644
@@ -233,12 +233,14 @@ static void pc_init_pci(QEMUMachineInitArgs *args)
              initrd_filename, cpu_model, 1, 1);
 }
 
-static void pc_init_pci_1_3(QEMUMachineInitArgs *args)
+/* PC machine init function for pc-0.14 to pc-1.2 */
+static void pc_init_pci_1_2(QEMUMachineInitArgs *args)
 {
-    enable_kvm_pv_eoi();
+    disable_kvm_pv_eoi();
     pc_init_pci(args);
 }
 
+/* PC init function for pc-0.10 to pc-0.13, and reused by xenfv */
 static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs *args)
 {
     ram_addr_t ram_size = args->ram_size;
@@ -247,6 +249,7 @@ static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs *args)
     const char *kernel_cmdline = args->kernel_cmdline;
     const char *initrd_filename = args->initrd_filename;
     const char *boot_device = args->boot_device;
+    disable_kvm_pv_eoi();
     pc_init1(get_system_memory(),
              get_system_io(),
              ram_size, boot_device,
@@ -264,6 +267,7 @@ static void pc_init_isa(QEMUMachineInitArgs *args)
     const char *boot_device = args->boot_device;
     if (cpu_model == NULL)
         cpu_model = "486";
+    disable_kvm_pv_eoi();
     pc_init1(get_system_memory(),
              get_system_io(),
              ram_size, boot_device,
@@ -286,7 +290,7 @@ static QEMUMachine pc_i440fx_machine_v1_4 = {
     .name = "pc-i440fx-1.4",
     .alias = "pc",
     .desc = "Standard PC (i440FX + PIIX, 1996)",
-    .init = pc_init_pci_1_3,
+    .init = pc_init_pci,
     .max_cpus = 255,
     .is_default = 1,
     DEFAULT_MACHINE_OPTIONS,
@@ -302,7 +306,7 @@ static QEMUMachine pc_i440fx_machine_v1_4 = {
 static QEMUMachine pc_machine_v1_3 = {
     .name = "pc-1.3",
     .desc = "Standard PC",
-    .init = pc_init_pci_1_3,
+    .init = pc_init_pci,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_3,
@@ -342,7 +346,7 @@ static QEMUMachine pc_machine_v1_3 = {
 static QEMUMachine pc_machine_v1_2 = {
     .name = "pc-1.2",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_2,
@@ -386,7 +390,7 @@ static QEMUMachine pc_machine_v1_2 = {
 static QEMUMachine pc_machine_v1_1 = {
     .name = "pc-1.1",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_1,
@@ -422,7 +426,7 @@ static QEMUMachine pc_machine_v1_1 = {
 static QEMUMachine pc_machine_v1_0 = {
     .name = "pc-1.0",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_0,
@@ -438,7 +442,7 @@ static QEMUMachine pc_machine_v1_0 = {
 static QEMUMachine pc_machine_v0_15 = {
     .name = "pc-0.15",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_0_15,
@@ -471,7 +475,7 @@ static QEMUMachine pc_machine_v0_15 = {
 static QEMUMachine pc_machine_v0_14 = {
     .name = "pc-0.14",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_2,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_0_14, 
target-i386/cpu.c +3 -2
index 754eb6f..d1a14d5 100644
@@ -211,11 +211,12 @@ static uint32_t kvm_default_features = (1 << KVM_FEATURE_CLOCKSOURCE) |
         (1 << KVM_FEATURE_CLOCKSOURCE2) |
         (1 << KVM_FEATURE_ASYNC_PF) |
         (1 << KVM_FEATURE_STEAL_TIME) |
+        (1 << KVM_FEATURE_PV_EOI) |
         (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT);
 
-void enable_kvm_pv_eoi(void)
+void disable_kvm_pv_eoi(void)
 {
-    kvm_default_features |= (1UL << KVM_FEATURE_PV_EOI);
+    kvm_default_features &= ~(1UL << KVM_FEATURE_PV_EOI);
 }
 
 void host_cpuid(uint32_t function, uint32_t count,
target-i386/cpu.h +1 -1
index 4e091cd..9d4fcf9 100644
@@ -1250,7 +1250,7 @@ void do_smm_enter(CPUX86State *env1);
 
 void cpu_report_tpr_access(CPUX86State *env, TPRAccess access);
 
-void enable_kvm_pv_eoi(void);
+void disable_kvm_pv_eoi(void);
 
 /* Return name of 32-bit register, from a R_* constant */
 const char *get_register_name_32(unsigned int reg);
Signature · 1 line
1.7.11.7
#17 lore raw

PATCH 4/12 kvm: Create kvm_arch_vcpu_id() function

In reply to Eduardo Habkost

This will allow each architecture to define how the VCPU ID is set on the KVM_CREATE_VCPU ioctl call.

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

Changes v2:
- Get CPUState as argument instead of CPUArchState

---
 include/sysemu/kvm.h | 3 +++
 kvm-all.c            | 2 +-
 target-i386/kvm.c    | 5 +++++
 target-ppc/kvm.c     | 5 +++++
 target-s390x/kvm.c   | 5 +++++
 5 files changed, 19 insertions(+), 1 deletion(-)
include/sysemu/kvm.h +3 -0
index 22acf91..384ee66 100644
@@ -196,6 +196,9 @@ int kvm_arch_init(KVMState *s);
 
 int kvm_arch_init_vcpu(CPUState *cpu);
 
+/* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
+unsigned long kvm_arch_vcpu_id(CPUState *cpu);
+
 void kvm_arch_reset_vcpu(CPUState *cpu);
 
 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
kvm-all.c +1 -1
index 6278d61..995220d 100644
@@ -222,7 +222,7 @@ int kvm_init_vcpu(CPUState *cpu)
 
     DPRINTF("kvm_init_vcpu\n");
 
-    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, cpu->cpu_index);
+    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, kvm_arch_vcpu_id(cpu));
     if (ret < 0) {
         DPRINTF("kvm_create_vcpu failed\n");
         goto err;
target-i386/kvm.c +5 -0
index 3acff40..5f3f789 100644
@@ -411,6 +411,11 @@ static void cpu_update_state(void *opaque, int running, RunState state)
     }
 }
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     struct {
target-ppc/kvm.c +5 -0
index 19e9f25..1e544ae 100644
@@ -384,6 +384,11 @@ static inline void kvm_fixup_page_sizes(PowerPCCPU *cpu)
 
 #endif /* !defined (TARGET_PPC64) */
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     PowerPCCPU *cpu = POWERPC_CPU(cs);
target-s390x/kvm.c +5 -0
index 6ec5e6d..bd9864c 100644
@@ -72,6 +72,11 @@ int kvm_arch_init(KVMState *s)
     return 0;
 }
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cpu)
 {
     int ret = 0;
Signature · 1 line
1.7.11.7
#18 lore raw

Re: [Qemu-devel] [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 39 lines

This will allow each architecture to define how the VCPU ID is set on the KVM_CREATE_VCPU ioctl call.

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

Changes v2:
- Get CPUState as argument instead of CPUArchState

---
 include/sysemu/kvm.h | 3 +++
 kvm-all.c            | 2 +-
 target-i386/kvm.c    | 5 +++++
 target-ppc/kvm.c     | 5 +++++
 target-s390x/kvm.c   | 5 +++++
 5 files changed, 19 insertions(+), 1 deletion(-)
include/sysemu/kvm.h +3 -0
index 22acf91..384ee66 100644
@@ -196,6 +196,9 @@ int kvm_arch_init(KVMState *s);
 
 int kvm_arch_init_vcpu(CPUState *cpu);
 
+/* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
+unsigned long kvm_arch_vcpu_id(CPUState *cpu);
+
 void kvm_arch_reset_vcpu(CPUState *cpu);
 
 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
kvm-all.c +0 -0
index 6278d61..995220d 100644

kvm-all.c int kvm_init_vcpu(CPUState *cpu) · line 228

Quoted 8 lines
kvm-all.c +1 -1
@@ -222,7 +222,7 @@ int kvm_init_vcpu(CPUState *cpu)
 
     DPRINTF("kvm_init_vcpu\n");
 
-    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, cpu->cpu_index);
+    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, kvm_arch_vcpu_id(cpu));
     if (ret < 0) {
         DPRINTF("kvm_create_vcpu failed\n");
         goto err;

This is changing the vararg from int to unsigned long. I have no insights yet on how this is handled and whether that is okay; I would at least expect this change to be mentioned in the commit message.

Quoted 6 lines
target-i386/kvm.c +0 -0
index 3acff40..5f3f789 100644
@@ -411,6 +411,11 @@ static void cpu_update_state(void *opaque, int running, RunState state)
     }

target-i386/kvm.c static void cpu_update_state(void *opaque, int running, RunState state) · line 421

Quoted 10 lines
target-i386/kvm.c +5 -0
 }
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     struct {

Minor nit: If you change this to CPUState *cs you spare the renaming in 05/12. Alternatively use x86_cpu there (not much code affected so you can just ignore this, no need to respin just for that).

Otherwise looks okay to me.

Andreas

Quoted 32 lines
target-ppc/kvm.c +5 -0
index 19e9f25..1e544ae 100644
@@ -384,6 +384,11 @@ static inline void kvm_fixup_page_sizes(PowerPCCPU *cpu)
 
 #endif /* !defined (TARGET_PPC64) */
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     PowerPCCPU *cpu = POWERPC_CPU(cs);
target-s390x/kvm.c +5 -0
index 6ec5e6d..bd9864c 100644
@@ -72,6 +72,11 @@ int kvm_arch_init(KVMState *s)
     return 0;
 }
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cpu)
 {
     int ret = 0;
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#19 lore raw

Re: [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

On Fri, Jan 18, 2013 at 12:11:29PM +0100, Andreas Färber wrote:
[...]

Quoted 14 lines
Quoted 14 lines
include/sysemu/kvm.h +3 -0
+/* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
+unsigned long kvm_arch_vcpu_id(CPUState *cpu);
+
 void kvm_arch_reset_vcpu(CPUState *cpu);
 
 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
kvm-all.c +0 -0
index 6278d61..995220d 100644
@@ -222,7 +222,7 @@ int kvm_init_vcpu(CPUState *cpu)
 
     DPRINTF("kvm_init_vcpu\n");
 
Quoted 9 lines

kvm-all.c int kvm_init_vcpu(CPUState *cpu) · line 228

Quoted 5 lines
kvm-all.c +1 -1
-    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, cpu->cpu_index);
+    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, kvm_arch_vcpu_id(cpu));
     if (ret < 0) {
         DPRINTF("kvm_create_vcpu failed\n");
         goto err;

This is changing the vararg from int to unsigned long. I have no insights yet on how this is handled and whether that is okay; I would at least expect this change to be mentioned in the commit message.

It was an unexpected change (I didn't notice that cpu_index was int), but strictly speaking the previous code was incorrect (as ioctl() gets an unsigned long argument, not int). I doubt there are cases where it would really break, but it is a good thing to fix it.

I agree this should be mentioned in the commit message, though. Will you add it before committing, or should I resubmit?

Quoted 22 lines
Quoted 7 lines
target-i386/kvm.c +0 -0
index 3acff40..5f3f789 100644
@@ -411,6 +411,11 @@ static void cpu_update_state(void *opaque, int running, RunState state)
     }

target-i386/kvm.c static void cpu_update_state(void *opaque, int running, RunState state) · line 421

Quoted 10 lines
target-i386/kvm.c +5 -0
 }
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     struct {

Minor nit: If you change this to CPUState *cs you spare the renaming in 05/12. Alternatively use x86_cpu there (not much code affected so you can just ignore this, no need to respin just for that).

Quoted 1 line

Otherwise looks okay to me.

I actually wanted to rename the variable only when necessary, otherwise this patch would be confusing if all architectures used 'cpu' and i386 used 'cs'.

(And I like using "cpu" for the more specific CPU type in the function [e.g. CPUState or X86CPUState depending on the case] and abbreviations [like 'cs'] for the more generic types. I believe I have seen this style used in other parts of the code.)

Quoted 39 lines

Andreas

Quoted 32 lines
target-ppc/kvm.c +5 -0
index 19e9f25..1e544ae 100644
@@ -384,6 +384,11 @@ static inline void kvm_fixup_page_sizes(PowerPCCPU *cpu)
 
 #endif /* !defined (TARGET_PPC64) */
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     PowerPCCPU *cpu = POWERPC_CPU(cs);
target-s390x/kvm.c +5 -0
index 6ec5e6d..bd9864c 100644
@@ -72,6 +72,11 @@ int kvm_arch_init(KVMState *s)
     return 0;
 }
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cpu)
 {
     int ret = 0;
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Signature · 1 line
Eduardo
#20 lore raw

Re: [Qemu-devel] [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

Am 18.01.2013 13:53, schrieb Eduardo Habkost:

Quoted 26 lines · Andreas Färber

[...]

Quoted 14 lines
Quoted 14 lines
include/sysemu/kvm.h +3 -0
+/* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
+unsigned long kvm_arch_vcpu_id(CPUState *cpu);
+
 void kvm_arch_reset_vcpu(CPUState *cpu);
 
 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
kvm-all.c +0 -0
index 6278d61..995220d 100644
@@ -222,7 +222,7 @@ int kvm_init_vcpu(CPUState *cpu)
 
     DPRINTF("kvm_init_vcpu\n");
 
Quoted 9 lines

kvm-all.c int kvm_init_vcpu(CPUState *cpu) · line 228

Quoted 5 lines
kvm-all.c +1 -1
-    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, cpu->cpu_index);
+    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, kvm_arch_vcpu_id(cpu));
     if (ret < 0) {
         DPRINTF("kvm_create_vcpu failed\n");
         goto err;

This is changing the vararg from int to unsigned long. I have no insights yet on how this is handled and whether that is okay; I would at least expect this change to be mentioned in the commit message.

Quoted 7 lines

It was an unexpected change (I didn't notice that cpu_index was int), but strictly speaking the previous code was incorrect (as ioctl() gets an unsigned long argument, not int). I doubt there are cases where it would really break, but it is a good thing to fix it.

I agree this should be mentioned in the commit message, though. Will you add it before committing, or should I resubmit?

Could you suggest a text for me to add please?

Quoted 21 lines
Quoted 21 lines
Quoted 6 lines
target-i386/kvm.c +0 -0
index 3acff40..5f3f789 100644
@@ -411,6 +411,11 @@ static void cpu_update_state(void *opaque, int running, RunState state)
     }

target-i386/kvm.c static void cpu_update_state(void *opaque, int running, RunState state) · line 421

Quoted 10 lines
target-i386/kvm.c +5 -0
 }
 
+unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+{
+    return cpu->cpu_index;
+}
+
 int kvm_arch_init_vcpu(CPUState *cs)
 {
     struct {

Minor nit: If you change this to CPUState *cs you spare the renaming in 05/12. Alternatively use x86_cpu there (not much code affected so you can just ignore this, no need to respin just for that).

Quoted 5 lines
Quoted 1 line

Otherwise looks okay to me.

I actually wanted to rename the variable only when necessary, otherwise this patch would be confusing if all architectures used 'cpu' and i386 used 'cs'.

It's inconsistent anyway, 'cs' is relatively new and I see no reason to use it in the prototype.
But OK, once 03/12 gets an ack I'll start applying.

Quoted 5 lines

(And I like using "cpu" for the more specific CPU type in the function [e.g. CPUState or X86CPUState depending on the case] and abbreviations [like 'cs'] for the more generic types. I believe I have seen this style used in other parts of the code.)

Yes, I chose to use "cpu" for the more frequently used type. I don't really like "cs" but it seemed better than "base_cpu" or so. When changing something over from "env" something with three letters looks nicer though, easier to review. Can't have everything. ;)

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#21 lore raw

Re: [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

Quoted 32 lines · Andreas Färber

Am 18.01.2013 13:53, schrieb Eduardo Habkost:

Quoted 31 lines · Andreas Färber

[...]

Quoted 14 lines
Quoted 14 lines
include/sysemu/kvm.h +3 -0
+/* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */
+unsigned long kvm_arch_vcpu_id(CPUState *cpu);
+
 void kvm_arch_reset_vcpu(CPUState *cpu);
 
 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr);
kvm-all.c +0 -0
index 6278d61..995220d 100644
@@ -222,7 +222,7 @@ int kvm_init_vcpu(CPUState *cpu)
 
     DPRINTF("kvm_init_vcpu\n");
 
Quoted 9 lines

kvm-all.c int kvm_init_vcpu(CPUState *cpu) · line 228

Quoted 5 lines
kvm-all.c +1 -1
-    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, cpu->cpu_index);
+    ret = kvm_vm_ioctl(s, KVM_CREATE_VCPU, kvm_arch_vcpu_id(cpu));
     if (ret < 0) {
         DPRINTF("kvm_create_vcpu failed\n");
         goto err;

This is changing the vararg from int to unsigned long. I have no insights yet on how this is handled and whether that is okay; I would at least expect this change to be mentioned in the commit message.

It was an unexpected change (I didn't notice that cpu_index was int), but strictly speaking the previous code was incorrect (as ioctl() gets an unsigned long argument, not int). I doubt there are cases where it would really break, but it is a good thing to fix it.

Quoted 4 lines
Quoted 2 lines

I agree this should be mentioned in the commit message, though. Will you add it before committing, or should I resubmit?

Could you suggest a text for me to add please?

"The argument passed to KVM_CREATE_VCPU now has 'unsigned long' type instead of 'int', as expected by the Linux ioctl() syscall. Maybe an int works on most or all architectures supporting KVM, but it is safer to use an appropriate 'unsigned long' parameter."

To find out if 'int' breaks on any architecture, I would need to check the ABI specification for each architecture. I didn't do that, but I am sure we should pass an unsigned long instead, if that's the type expected by the kernel.

Signature · 1 line
Eduardo
#22 lore raw

Re: [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

Quoted 6 lines · Eduardo Habkost
Quoted 1 line

Could you suggest a text for me to add please?

"The argument passed to KVM_CREATE_VCPU now has 'unsigned long' type instead of 'int', as expected by the Linux ioctl() syscall. Maybe an int works on most or all architectures supporting KVM, but it is safer to use an appropriate 'unsigned long' parameter."

Interestingly enough, while the Linux syscall uses 'unsigned long', the POSIX definition of ioctl() uses 'int'; so the Linux kernel is already constrained to never use an ioctl value that doesn't fit within 'int', and glibc is already responsible for ensuring that argument promotion of an int doesn't change the behavior of ioctl() in libc when converting it over to the unsigned long syscall semantics expected by the kernel.

Quoted 6 lines

To find out if 'int' breaks on any architecture, I would need to check the ABI specification for each architecture. I didn't do that, but I am sure we should pass an unsigned long instead, if that's the type expected by the kernel.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#23 lore raw

Re: [Qemu-devel] [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

Quoted 11 lines · Eric Blake
Quoted 6 lines · Eduardo Habkost
Quoted 1 line

Could you suggest a text for me to add please?

"The argument passed to KVM_CREATE_VCPU now has 'unsigned long' type instead of 'int', as expected by the Linux ioctl() syscall. Maybe an int works on most or all architectures supporting KVM, but it is safer to use an appropriate 'unsigned long' parameter."

Interestingly enough, while the Linux syscall uses 'unsigned long', the POSIX definition of ioctl() uses 'int'; so the Linux kernel is already constrained to never use an ioctl value that doesn't fit within 'int',

Really? What about the ioctl()s that get a pointer as argument on architectures where pointers don't fit in an int?

Do you have a pointer to the POSIX definition you are talking about?

Note that I'm talking about the the extra ioctl() argument, not the ioctl() number (that is an unsigned int in the kernel code).

Quoted 15 lines

and glibc is already responsible for ensuring that argument promotion of an int doesn't change the behavior of ioctl() in libc when converting it over to the unsigned long syscall semantics expected by the kernel.

Quoted 6 lines

To find out if 'int' breaks on any architecture, I would need to check the ABI specification for each architecture. I didn't do that, but I am sure we should pass an unsigned long instead, if that's the type expected by the kernel.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Signature · 1 line
Eduardo
#24 lore raw

Re: [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

Quoted 13 lines · Eduardo Habkost
Quoted 11 lines · Eric Blake
Quoted 6 lines · Eduardo Habkost
Quoted 1 line

Could you suggest a text for me to add please?

"The argument passed to KVM_CREATE_VCPU now has 'unsigned long' type instead of 'int', as expected by the Linux ioctl() syscall. Maybe an int works on most or all architectures supporting KVM, but it is safer to use an appropriate 'unsigned long' parameter."

Interestingly enough, while the Linux syscall uses 'unsigned long', the POSIX definition of ioctl() uses 'int'; so the Linux kernel is already constrained to never use an ioctl value that doesn't fit within 'int',

Quoted 7 lines

Really? What about the ioctl()s that get a pointer as argument on architectures where pointers don't fit in an int?

Do you have a pointer to the POSIX definition you are talking about?

Note that I'm talking about the the extra ioctl() argument, not the ioctl() number (that is an unsigned int in the kernel code).

Okay, now you made me go back and check sources.

POSIX 2008 says:
#include <stropts.h>
int ioctl(int fildes, int request, ... /* arg */);

Gnulib says this about a bug that it works around:
@item
On glibc platforms, the second parameter is of type @code{unsigned long} rather than @code{int}.

But gnulib also suggests using <sys/ioctl.h> instead of the POSIX header <stropts.h> for getting ioctl(), because <stropts.h> was declared obsolete in POSIX 2008 and was never implemented in glibc.

Sure enough, looking at Fedora 18 /usr/include/sys/ioctl.h, I still see:
extern int ioctl (int __fd, unsigned long int __request, ...) __THROW;

Meanwhile, you are correct that the kernel defines request as 32 bits:
linux.git:include/uapi/asm-generic/ioctl.h
/* ioctl command encoding: 32 bits total, command in lower 16 bits,
* size of the parameter structure in the lower 14 bits of the
* upper 16 bits.
* Encoding the size of the parameter structure in the ioctl request
* is useful for catching programs compiled with old versions
* and to avoid overwriting user space outside the user buffer area.
* The highest 2 bits are reserved for indicating the ``access mode''.
* NOTE: This limits the max parameter size to 16kB -1 !
*/

Quoted 4 lines
Quoted 4 lines

and glibc is already responsible for ensuring that argument promotion of an int doesn't change the behavior of ioctl() in libc when converting it over to the unsigned long syscall semantics expected by the kernel.

So a more precise wording of this is:

glibc is already responsible from converting the 'unsigned long int' of the user declaration back into the 'unsigned int' that the kernel expects for the second argument. The third argument (when present), is generally treated as a pointer (of size appropriate for the architecture). Although there _might_ be an ioctl that uses it directly as an integer instead of dereferencing it as a pointer, those would be the exceptions to the rule.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#25 lore raw

Re: [Qemu-devel] [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 18.01.2013 18:46, schrieb Eric Blake:

Quoted 61 lines · Eduardo Habkost
Quoted 15 lines · Eric Blake
Quoted 9 lines · Eduardo Habkost
Quoted 7 lines
Quoted 1 line

Could you suggest a text for me to add please?

"The argument passed to KVM_CREATE_VCPU now has 'unsigned long' type instead of 'int', as expected by the Linux ioctl() syscall. Maybe an int works on most or all architectures supporting KVM, but it is safer to use an appropriate 'unsigned long' parameter."

Quoted 4 lines

Interestingly enough, while the Linux syscall uses 'unsigned long', the POSIX definition of ioctl() uses 'int'; so the Linux kernel is already constrained to never use an ioctl value that doesn't fit within 'int',

Quoted 8 lines

Really? What about the ioctl()s that get a pointer as argument on architectures where pointers don't fit in an int?

Do you have a pointer to the POSIX definition you are talking about?

Note that I'm talking about the the extra ioctl() argument, not the ioctl() number (that is an unsigned int in the kernel code).

Okay, now you made me go back and check sources.

POSIX 2008 says: #include <stropts.h> int ioctl(int fildes, int request, ... /* arg */);

Gnulib says this about a bug that it works around: @item On glibc platforms, the second parameter is of type @code{unsigned long} rather than @code{int}.

But gnulib also suggests using <sys/ioctl.h> instead of the POSIX header <stropts.h> for getting ioctl(), because <stropts.h> was declared obsolete in POSIX 2008 and was never implemented in glibc.

Sure enough, looking at Fedora 18 /usr/include/sys/ioctl.h, I still
see: extern int ioctl (int __fd, unsigned long int __request, ...) __THROW;

Meanwhile, you are correct that the kernel defines request as 32
bits: linux.git:include/uapi/asm-generic/ioctl.h /* ioctl command
encoding: 32 bits total, command in lower 16 bits, * size of the parameter structure in the lower 14 bits of the * upper 16 bits. * Encoding the size of the parameter structure in the ioctl request * is useful for catching programs compiled with old versions * and to avoid overwriting user space outside the user buffer area. * The highest 2 bits are reserved for indicating the ``access mode''. *
NOTE: This limits the max parameter size to 16kB -1 ! */

Quoted 5 lines
Quoted 5 lines

and glibc is already responsible for ensuring that argument promotion of an int doesn't change the behavior of ioctl() in libc when converting it over to the unsigned long syscall semantics expected by the kernel.

So a more precise wording of this is:

Quoted 7 lines

glibc is already responsible from converting the 'unsigned long int' of the user declaration back into the 'unsigned int' that the kernel expects for the second argument. The third argument (when present), is generally treated as a pointer (of size appropriate for the architecture). Although there _might_ be an ioctl that uses it directly as an integer instead of dereferencing it as a pointer, those would be the exceptions to the rule.

So ... do we have a conclusion what to put into the commit message? :)

It looks to me as if kvm-all.c:kvm_vm_ioctl() is using void*. I like unsigned long but maybe uintptr_t would be more correct then?

Or should kvm_vm_ioctl() be fixed to use something else instead? Eric's int would be a semantic change for the 64-bit platforms, no?

Andreas

- --
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg -----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)

iQIcBAEBAgAGBQJQ/T8lAAoJEPou0S0+fgE/gl8QALesZwG5q07W21mp2j4ikL8N jrBHjG2VZ9Kda+AIGMClVsWntGZSOzHdtriJ4gjxp90D71S/LQfsYAy6bj45FIwS kPbIQblLlL5Xc6ZiTS5yTzkwyEd7gUpDVouXyv3XxeyUxqhQKwgWxpiP4RftbBRI Z8wLbVFNpIoIsHfhKoNkT4M/Ucm1iZbChV6y4zqltAfdQhcl6Gq0jzhtkAfmN41t p3tCJYldRwayiKLsO2Y0BMNrKmCJisKCEGmkCQzye/3cuFoat/WUmpjV/65hLNtm ruzfn6pCqMTEGPC4YeDdUsxAhVzVX+Sd4mBKHBGItmvhhJMFYUtwTosRwX5bOrAJ mpVLAj5/XDYTm2/jQUEOJAqpxUr5oAVMQL3sNeWJPmXkk1kNaNWTNVHHDW1iJnRj ty0YIWOnuNabkwiDEjPCz6ghjfA3wOBWy8Gk3+F21MYgRQwDTFw4JZuroOIzy3iD 6Vs4MmiBUGnoLobSqw2dUZFmjL7a1500AxZG0MwBd+EqnbLHGqD33kvLrbUYT8+F eW+cqKV+ZXo3ux343rTxD6EFgmN7GmHSkknxJN5m6ldlw5wfFQ8KhdCiKjwSq3EP X0bVGmryEdIh+6w/RbhL75Vfb/Je0mr/GzhtijtXo+FORkF8ip2mlpVSl46r0AfI KvsZ0HZqZHsfoaSBC1js
=/cL3
-----END PGP SIGNATURE-----

#26 lore raw

Re: [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

Quoted 8 lines · Andreas Färber
Quoted 7 lines

glibc is already responsible from converting the 'unsigned long int' of the user declaration back into the 'unsigned int' that the kernel expects for the second argument. The third argument (when present), is generally treated as a pointer (of size appropriate for the architecture). Although there _might_ be an ioctl that uses it directly as an integer instead of dereferencing it as a pointer, those would be the exceptions to the rule.

Quoted 4 lines

So ... do we have a conclusion what to put into the commit message? :)

It looks to me as if kvm-all.c:kvm_vm_ioctl() is using void*. I like unsigned long but maybe uintptr_t would be more correct then?

uintptr_t feels more correct - the 3rd (vararg) argument through the ioctl() syscall is always retrieved using the same size as void*.

Quoted 3 lines

Or should kvm_vm_ioctl() be fixed to use something else instead? Eric's int would be a semantic change for the 64-bit platforms, no?

My discussion about 'int' vs. 'unsigned long' was in regards to the second argument KVM_CREATE_VCPU, which your patch does not change (perhaps my fault for jumping in on a conversation mid-thread without actually reading your original patch, which I have now done). That is, KVM_CREATE_VCPU as a constant is always 32 bits (kernel constraint), widened out to unsigned long when passed to the glibc function (due to the glibc signature disagreeing with POSIX), then narrowed back down to 32 bits when forwarded to the kernel syscall.

Meanwhile, your patch is fixing the third argument from 'int' to a wider type, which is necessary for passing that value through varargs when the receiving end will retrieve the same argument via a void* variable.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#27 lore raw

Re: [PATCH for-1.4 04/12] kvm: Create kvm_arch_vcpu_id() function

Quoted 9 lines · Eric Blake
Quoted 8 lines · Andreas Färber
Quoted 7 lines

glibc is already responsible from converting the 'unsigned long int' of the user declaration back into the 'unsigned int' that the kernel expects for the second argument. The third argument (when present), is generally treated as a pointer (of size appropriate for the architecture). Although there _might_ be an ioctl that uses it directly as an integer instead of dereferencing it as a pointer, those would be the exceptions to the rule.

Quoted 7 lines
Quoted 4 lines

So ... do we have a conclusion what to put into the commit message? :)

It looks to me as if kvm-all.c:kvm_vm_ioctl() is using void*. I like unsigned long but maybe uintptr_t would be more correct then?

uintptr_t feels more correct - the 3rd (vararg) argument through the ioctl() syscall is always retrieved using the same size as void*.

Actually, sys_ioctl() always retrieve it using "unsigned long", but nothing prevents the arch-specific syscall entry code to from translating something from a different type to "unsigned long" before calling sys_ioctl().

So I guess the only guarantee we have is the Linux ioctl(2) man page, that says: "The third argument is an untyped pointer to memory. It's traditionally char *argp (from the days before void * was valid C), and will be so named for this discussion."

That said, I plan to change the code to cast the argument to (void*) in the next version.

Quoted 14 lines
Quoted 4 lines

Or should kvm_vm_ioctl() be fixed to use something else instead? Eric's int would be a semantic change for the 64-bit platforms, no?

My discussion about 'int' vs. 'unsigned long' was in regards to the second argument KVM_CREATE_VCPU, which your patch does not change (perhaps my fault for jumping in on a conversation mid-thread without actually reading your original patch, which I have now done). That is, KVM_CREATE_VCPU as a constant is always 32 bits (kernel constraint), widened out to unsigned long when passed to the glibc function (due to the glibc signature disagreeing with POSIX), then narrowed back down to 32 bits when forwarded to the kernel syscall.

Quoted 3 lines

Meanwhile, your patch is fixing the third argument from 'int' to a wider type, which is necessary for passing that value through varargs when the receiving end will retrieve the same argument via a void* variable.

I am confident that "unsigned long" will work properly on all architectures we care about today, but I also don't know if this is documented and guaranteed to work on all architectures. Passing an argument of the documented type (void*) sounds like the right thing to do.

Signature · 1 line
Eduardo
#28 lore raw

PATCH 5/12 target-i386: kvm: Set vcpu_id to APIC ID instead of CPU index

In reply to Eduardo Habkost

The CPU ID in KVM is supposed to be the APIC ID, so change the KVM_CREATE_VCPU call to match it. The current behavior didn't break anything yet because today the APIC ID is assumed to be equal to the CPU index, but this won't be true in the future.

---
Cc: [email protected]
Cc: Michael S. Tsirkin <[email protected]>
Cc: Gleb Natapov <[email protected]>
Cc: Marcelo Tosatti <[email protected]>

Changes v2:
- Change only i386 code (kvm_arch_vcpu_id())

Changes v3:
- Get CPUState as argument instead of CPUArchState

---
 target-i386/kvm.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target-i386/kvm.c +3 -2
index 5f3f789..c440809 100644
@@ -411,9 +411,10 @@ static void cpu_update_state(void *opaque, int running, RunState state)
     }
 }
 
-unsigned long kvm_arch_vcpu_id(CPUState *cpu)
+unsigned long kvm_arch_vcpu_id(CPUState *cs)
 {
-    return cpu->cpu_index;
+    X86CPU *cpu = X86_CPU(cs);
+    return cpu->env.cpuid_apic_id;
 }
 
 int kvm_arch_init_vcpu(CPUState *cs)
Signature · 1 line
1.7.11.7
#29 lore raw

PATCH 6/12 fw_cfg: Remove FW_CFG_MAX_CPUS from fw_cfg_init()

In reply to Eduardo Habkost

PC will not use max_cpus for that field, so move it outside the common code so it can use a different value on PC.

---
 hw/fw_cfg.c       | 1 -
 hw/pc.c           | 2 +-
 hw/ppc_newworld.c | 1 +
 hw/ppc_oldworld.c | 1 +
 hw/sun4m.c        | 3 +++
 hw/sun4u.c        | 1 +
 6 files changed, 7 insertions(+), 2 deletions(-)
hw/fw_cfg.c +0 -1
index 7c9480c..2a31339 100644
@@ -519,7 +519,6 @@ FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
     fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
     fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
-    fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
     fw_cfg_bootsplash(s);
     fw_cfg_reboot(s);
hw/pc.c +1 -1
index ba1f19d..076398b 100644
@@ -560,7 +560,7 @@ static void *bochs_bios_init(void)
     int i, j;
 
     fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
-
+    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
     fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, (uint8_t *)acpi_tables,
hw/ppc_newworld.c +1 -0
index 7a465a7..85ba1c4 100644
@@ -397,6 +397,7 @@ static void ppc_core99_init(QEMUMachineInitArgs *args)
     /* No PCI init: the BIOS will do it */
 
     fw_cfg = fw_cfg_init(0, 0, CFG_ADDR, CFG_ADDR + 2);
+    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
     fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, machine_arch);
hw/ppc_oldworld.c +1 -0
index de34e75..e4eb444 100644
@@ -296,6 +296,7 @@ static void ppc_heathrow_init(QEMUMachineInitArgs *args)
     /* No PCI init: the BIOS will do it */
 
     fw_cfg = fw_cfg_init(0, 0, CFG_ADDR, CFG_ADDR + 2);
+    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
     fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, ARCH_HEATHROW);
hw/sun4m.c +3 -0
index 6f5de44..7474059 100644
@@ -1021,6 +1021,7 @@ static void sun4m_hw_init(const struct sun4m_hwdef *hwdef, ram_addr_t RAM_size,
                  hwdef->ecc_version);
 
     fw_cfg = fw_cfg_init(0, 0, CFG_ADDR, CFG_ADDR + 2);
+    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
     fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
@@ -1667,6 +1668,7 @@ static void sun4d_hw_init(const struct sun4d_hwdef *hwdef, ram_addr_t RAM_size,
                "Sun4d");
 
     fw_cfg = fw_cfg_init(0, 0, CFG_ADDR, CFG_ADDR + 2);
+    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
     fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
@@ -1869,6 +1871,7 @@ static void sun4c_hw_init(const struct sun4c_hwdef *hwdef, ram_addr_t RAM_size,
                "Sun4c");
 
     fw_cfg = fw_cfg_init(0, 0, CFG_ADDR, CFG_ADDR + 2);
+    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
     fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
hw/sun4u.c +1 -0
index cb75d03..95850d3 100644
@@ -878,6 +878,7 @@ static void sun4uv_init(MemoryRegion *address_space_mem,
                            (uint8_t *)&nd_table[0].macaddr);
 
     fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
+    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
     fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, hwdef->machine_id);
Signature · 1 line
1.7.11.7
#30 lore raw

PATCH 7/12 target-i386/cpu: Introduce apic_id_for_cpu() function

In reply to Eduardo Habkost

This function will be used by both the CPU initialization code and the fw_cfg table initialization code.

Later this function will be updated to generate APIC IDs according to the CPU topology.

---
 target-i386/cpu.c | 17 ++++++++++++++++-
 target-i386/cpu.h |  2 ++
 2 files changed, 18 insertions(+), 1 deletion(-)
target-i386/cpu.c +16 -1
index d1a14d5..d90789d 100644
@@ -2194,6 +2194,21 @@ void x86_cpu_realize(Object *obj, Error **errp)
     cpu_reset(CPU(cpu));
 }
 
+/* Calculates initial APIC ID for a specific CPU index
+ *
+ * Currently we need to be able to calculate the APIC ID from the CPU index
+ * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
+ * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
+ * all CPUs up to max_cpus.
+ */
+uint32_t apic_id_for_cpu(unsigned int cpu_index)
+{
+    /* right now APIC ID == CPU index. this will eventually change to use
+     * the CPU topology configuration properly
+     */
+    return cpu_index;
+}
+
 static void x86_cpu_initfn(Object *obj)
 {
     CPUState *cs = CPU(obj);
@@ -2228,7 +2243,7 @@ static void x86_cpu_initfn(Object *obj)
                         x86_cpuid_get_tsc_freq,
                         x86_cpuid_set_tsc_freq, NULL, NULL, NULL);
 
-    env->cpuid_apic_id = cs->cpu_index;
+    env->cpuid_apic_id = apic_id_for_cpu(cs->cpu_index);
 
     /* init various static tables used in TCG mode */
     if (tcg_enabled() && !inited) {
target-i386/cpu.h +2 -0
index 9d4fcf9..d86c0af 100644
@@ -1255,4 +1255,6 @@ void disable_kvm_pv_eoi(void);
 /* Return name of 32-bit register, from a R_* constant */
 const char *get_register_name_32(unsigned int reg);
 
+uint32_t apic_id_for_cpu(unsigned int cpu_index);
+
 #endif /* CPU_I386_H */
Signature · 1 line
1.7.11.7
#31 lore raw

Re: [Qemu-devel] [PATCH for-1.4 07/12] target-i386/cpu: Introduce apic_id_for_cpu() function

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 18 lines

This function will be used by both the CPU initialization code and the fw_cfg table initialization code.

Later this function will be updated to generate APIC IDs according to the CPU topology.

---
 target-i386/cpu.c | 17 ++++++++++++++++-
 target-i386/cpu.h |  2 ++
 2 files changed, 18 insertions(+), 1 deletion(-)
target-i386/cpu.c +0 -0
index d1a14d5..d90789d 100644
@@ -2194,6 +2194,21 @@ void x86_cpu_realize(Object *obj, Error **errp)
     cpu_reset(CPU(cpu));

target-i386/cpu.c void x86_cpu_realize(Object *obj, Error **errp) · line 2204

Quoted 10 lines
target-i386/cpu.c +8 -0
 }
 
+/* Calculates initial APIC ID for a specific CPU index
+ *
+ * Currently we need to be able to calculate the APIC ID from the CPU index
+ * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
+ * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
+ * all CPUs up to max_cpus.
+ */
+uint32_t apic_id_for_cpu(unsigned int cpu_index)

Can we rather make this x86_cpu_apic_id(X86CPU *cpu) to account for future changes to topology modelling?

Andreas

Quoted 30 lines
target-i386/cpu.c +8 -1
+{
+    /* right now APIC ID == CPU index. this will eventually change to use
+     * the CPU topology configuration properly
+     */
+    return cpu_index;
+}
+
 static void x86_cpu_initfn(Object *obj)
 {
     CPUState *cs = CPU(obj);
@@ -2228,7 +2243,7 @@ static void x86_cpu_initfn(Object *obj)
                         x86_cpuid_get_tsc_freq,
                         x86_cpuid_set_tsc_freq, NULL, NULL, NULL);
 
-    env->cpuid_apic_id = cs->cpu_index;
+    env->cpuid_apic_id = apic_id_for_cpu(cs->cpu_index);
 
     /* init various static tables used in TCG mode */
     if (tcg_enabled() && !inited) {
target-i386/cpu.h +2 -0
index 9d4fcf9..d86c0af 100644
@@ -1255,4 +1255,6 @@ void disable_kvm_pv_eoi(void);
 /* Return name of 32-bit register, from a R_* constant */
 const char *get_register_name_32(unsigned int reg);
 
+uint32_t apic_id_for_cpu(unsigned int cpu_index);
+
 #endif /* CPU_I386_H */
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#32 lore raw

Re: [Qemu-devel] [PATCH for-1.4 07/12] target-i386/cpu: Introduce apic_id_for_cpu() function

Quoted 21 lines · Andreas Färber

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 20 lines

This function will be used by both the CPU initialization code and the fw_cfg table initialization code.

Later this function will be updated to generate APIC IDs according to the CPU topology.

---
 target-i386/cpu.c | 17 ++++++++++++++++-
 target-i386/cpu.h |  2 ++
 2 files changed, 18 insertions(+), 1 deletion(-)
target-i386/cpu.c +0 -0
index d1a14d5..d90789d 100644
@@ -2194,6 +2194,21 @@ void x86_cpu_realize(Object *obj, Error **errp)
     cpu_reset(CPU(cpu));
 }
 
Quoted 11 lines

target-i386/cpu.c void x86_cpu_realize(Object *obj, Error **errp) · line 2204

Quoted 8 lines
target-i386/cpu.c +8 -0
+/* Calculates initial APIC ID for a specific CPU index
+ *
+ * Currently we need to be able to calculate the APIC ID from the CPU index
+ * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
+ * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
+ * all CPUs up to max_cpus.
+ */
+uint32_t apic_id_for_cpu(unsigned int cpu_index)

Can we rather make this x86_cpu_apic_id(X86CPU *cpu) to account for future changes to topology modelling?

We can't make it get a X86CPU as parameter, because the ACPI tables have to be built up to max_cpus, before the CPUs get actually created. But it can be renamed, yes.

Signature · 1 line
Eduardo
#33 lore raw

PATCH 8/12 cpus.h: Make constant smp_cores/smp_threads available on *-user

In reply to Eduardo Habkost

The code that calculates the APIC ID will use smp_cores/smp_threads, so just define them as 1 on *-user to avoid #ifdefs in the code.

---
 include/sysemu/cpus.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/sysemu/cpus.h +7 -0
index 81bd817..f7f6854 100644
@@ -13,9 +13,16 @@ void cpu_synchronize_all_post_init(void);
 
 void qtest_clock_warp(int64_t dest);
 
+#ifndef CONFIG_USER_ONLY
 /* vl.c */
 extern int smp_cores;
 extern int smp_threads;
+#else
+/* *-user doesn't have configurable SMP topology */
+#define smp_cores   1
+#define smp_threads 1
+#endif
+
 void set_numa_modes(void);
 void set_cpu_log(const char *optarg);
 void set_cpu_log_filename(const char *optarg);
Signature · 1 line
1.7.11.7
#34 lore raw

PATCH 9/12 pc: Set fw_cfg data based on APIC ID calculation

In reply to Eduardo Habkost

This changes FW_CFG_MAX_CPUS and FW_CFG_NUMA to use apic_id_for_cpu(), so the NUMA table can be based on the APIC IDs, instead of CPU index (SeaBIOS knows nothing about CPU indexes, just APIC IDs).

---
Changes v2:
- Get PC object as argument
- Add more detailed comments explaining the reason for FW_CFG_MAX_CPUS not being simply 'max_cpus'

Changes v3:
- Use PCInitArgs instead of PC object

Changes v4:
- Don't use PCInitArgs, just add the necessary data for apic_id_limit() as argument
- Rename function to pc_apic_id_limit()
- Rename max_apic_id to apic_id_limit

---
 hw/pc.c | 44 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 37 insertions(+), 7 deletions(-)
hw/pc.c +37 -7
index 076398b..655bbd3 100644
@@ -551,6 +551,18 @@ int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
     return index;
 }
 
+/* Calculates the limit to CPU APIC ID values
+ *
+ * This function returns the limit for the APIC ID value, so that all
+ * CPU APIC IDs are < pc_apic_id_limit().
+ *
+ * This is used for FW_CFG_MAX_CPUS. See comments on bochs_bios_init().
+ */
+static unsigned int pc_apic_id_limit(unsigned int max_cpus)
+{
+    return apic_id_for_cpu(max_cpus - 1) + 1;
+}
+
 static void *bochs_bios_init(void)
 {
     void *fw_cfg;
@@ -558,9 +570,24 @@ static void *bochs_bios_init(void)
     size_t smbios_len;
     uint64_t *numa_fw_cfg;
     int i, j;
+    unsigned int apic_id_limit = pc_apic_id_limit(max_cpus);
 
     fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
-    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
+    /* FW_CFG_MAX_CPUS is a bit confusing/problematic on x86:
+     *
+     * SeaBIOS needs FW_CFG_MAX_CPUS for CPU hotplug, but the CPU hotplug
+     * QEMU<->SeaBIOS interface is not based on the "CPU index", but on the APIC
+     * ID of hotplugged CPUs[1]. This means that FW_CFG_MAX_CPUS is not the
+     * "maximum number of CPUs", but the "limit to the APIC ID values SeaBIOS
+     * may see".
+     *
+     * So, this means we must not use max_cpus, here, but the maximum possible
+     * APIC ID value, plus one.
+     *
+     * [1] The only kind of "CPU identifier" used between SeaBIOS and QEMU is
+     *     the APIC ID, not the "CPU index"
+     */
+    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)apic_id_limit);
     fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
     fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, (uint8_t *)acpi_tables,
@@ -580,21 +607,24 @@ static void *bochs_bios_init(void)
      * of nodes, one word for each VCPU->node and one word for each node to
      * hold the amount of memory.
      */
-    numa_fw_cfg = g_malloc0((1 + max_cpus + nb_numa_nodes) * 8);
+    numa_fw_cfg = g_malloc0((1 + apic_id_limit + nb_numa_nodes) * 8);
     numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
-    for (i = 0; i < max_cpus; i++) {
+    unsigned int cpu_idx;
+    for (cpu_idx = 0; cpu_idx < max_cpus; cpu_idx++) {
+        unsigned int apic_id = apic_id_for_cpu(cpu_idx);
+        assert(apic_id < apic_id_limit);
         for (j = 0; j < nb_numa_nodes; j++) {
-            if (test_bit(i, node_cpumask[j])) {
-                numa_fw_cfg[i + 1] = cpu_to_le64(j);
+            if (test_bit(cpu_idx, node_cpumask[j])) {
+                numa_fw_cfg[apic_id + 1] = cpu_to_le64(j);
                 break;
             }
         }
     }
     for (i = 0; i < nb_numa_nodes; i++) {
-        numa_fw_cfg[max_cpus + 1 + i] = cpu_to_le64(node_mem[i]);
+        numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(node_mem[i]);
     }
     fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, (uint8_t *)numa_fw_cfg,
-                     (1 + max_cpus + nb_numa_nodes) * 8);
+                     (1 + apic_id_limit + nb_numa_nodes) * 8);
 
     return fw_cfg;
 }
Signature · 1 line
1.7.11.7
#35 lore raw

PATCH 10/12 tests: Support target-specific unit tests

In reply to Eduardo Habkost

To make unit tests that depend on target-specific files, use check-unit-<arch>-y and test-obj-<arch>-y.

---
 tests/Makefile | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tests/Makefile +13 -1
index d86e95a..41172d6 100644
@@ -112,9 +112,21 @@ tests/fdc-test$(EXESUF): tests/fdc-test.o
 tests/hd-geo-test$(EXESUF): tests/hd-geo-test.o
 tests/tmp105-test$(EXESUF): tests/tmp105-test.o
 
-# QTest rules
+# unit test rules:
 
 TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))
+
+# target-specific tests/objs:
+
+test-obj-y += $(foreach TARGET,$(TARGETS), $(test-obj-$(TARGET)-y))
+check-unit-y += $(foreach TARGET,$(TARGETS), $(check-unit-$(TARGET)-y))
+
+$(foreach TARGET,$(TARGETS),$(eval $(test-obj-$(TARGET)-y): QEMU_INCLUDES += -Itarget-$(TARGET)))
+
+$(test-obj-y): QEMU_INCLUDES += -Itests
+
+# QTest rules
+
 QTEST_TARGETS=$(foreach TARGET,$(TARGETS), $(if $(check-qtest-$(TARGET)-y), $(TARGET),))
 check-qtest-$(CONFIG_POSIX)=$(foreach TARGET,$(TARGETS), $(check-qtest-$(TARGET)-y))
 
Signature · 1 line
1.7.11.7
#36 lore raw

PATCH 11/12 target-i386: Topology & APIC ID utility functions

In reply to Eduardo Habkost

---
Changes v1 -> v2:
- Support 32-bit APIC IDs (in case x2APIC is going to be used)
- Coding style changes
- Use TARGET_I386_TOPOLOGY_H instead of __QEMU_X86_TOPOLOGY_H__
- Rename topo_make_apic_id() to topo_apicid_for_cpu()
- Rename __make_apicid() to topo_make_apicid()
- Spaces around operators on test-x86-cpuid.c, as requested by Blue Swirl
- Make test-x86-cpuid a target-specific test

Changes v2 -> v3:
- Add documentation pointers to the code
- Rename bits_for_count() to bitwidth_for_count()
- Remove unused apicid_*_id() functions

Changes v3 -> v4:
- Remove now-obsolete FIXME comment from test-x86-cpuid.c
- Change bitops.h include to qemu/bitops.h
- Add gcov file list to test-x86-cpuid

---
 target-i386/topology.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/.gitignore       |   1 +
 tests/Makefile         |   7 +++
 tests/test-x86-cpuid.c | 101 +++++++++++++++++++++++++++++++++++++
 4 files changed, 242 insertions(+)
 create mode 100644 target-i386/topology.h
 create mode 100644 tests/test-x86-cpuid.c
target-i386/topology.h new file +133 -0
new file mode 100644
index 0000000..833ab47
@@ -0,0 +1,133 @@
+/*
+ *  x86 CPU topology data structures and functions
+ *
+ *  Copyright (c) 2012 Red Hat Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef TARGET_I386_TOPOLOGY_H
+#define TARGET_I386_TOPOLOGY_H
+
+/* This file implements the APIC-ID-based CPU topology enumeration logic,
+ * documented at the following document:
+ *   Intel® 64 Architecture Processor Topology Enumeration
+ *   http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/
+ *
+ * This code should be compatible with AMD's "Extended Method" described at:
+ *   AMD CPUID Specification (Publication #25481)
+ *   Section 3: Multiple Core Calcuation
+ * as long as:
+ *  nr_threads is set to 1;
+ *  OFFSET_IDX is assumed to be 0;
+ *  CPUID Fn8000_0008_ECX[ApicIdCoreIdSize[3:0]] is set to apicid_core_width().
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+#include "qemu/bitops.h"
+
+/* APIC IDs can be 32-bit, but beware: APIC IDs > 255 require x2APIC support
+ */
+typedef uint32_t apic_id_t;
+
+/* Return the bit width needed for 'count' IDs
+ */
+static unsigned bitwidth_for_count(unsigned count)
+{
+    g_assert(count >= 1);
+    if (count == 1) {
+        return 0;
+    }
+    return bitops_flsl(count - 1) + 1;
+}
+
+/* Bit width of the SMT_ID (thread ID) field on the APIC ID
+ */
+static inline unsigned apicid_smt_width(unsigned nr_cores, unsigned nr_threads)
+{
+    return bitwidth_for_count(nr_threads);
+}
+
+/* Bit width of the Core_ID field
+ */
+static inline unsigned apicid_core_width(unsigned nr_cores, unsigned nr_threads)
+{
+    return bitwidth_for_count(nr_cores);
+}
+
+/* Bit offset of the Core_ID field
+ */
+static inline unsigned apicid_core_offset(unsigned nr_cores,
+                                          unsigned nr_threads)
+{
+    return apicid_smt_width(nr_cores, nr_threads);
+}
+
+/* Bit offset of the Pkg_ID (socket ID) field
+ */
+static inline unsigned apicid_pkg_offset(unsigned nr_cores, unsigned nr_threads)
+{
+    return apicid_core_offset(nr_cores, nr_threads) + \
+           apicid_core_width(nr_cores, nr_threads);
+}
+
+/* Make APIC ID for the CPU based on Pkg_ID, Core_ID, SMT_ID
+ *
+ * The caller must make sure core_id < nr_cores and smt_id < nr_threads.
+ */
+static inline apic_id_t topo_make_apicid(unsigned nr_cores,
+                                         unsigned nr_threads,
+                                         unsigned pkg_id, unsigned core_id,
+                                         unsigned smt_id)
+{
+    return (pkg_id  << apicid_pkg_offset(nr_cores, nr_threads)) | \
+           (core_id << apicid_core_offset(nr_cores, nr_threads)) | \
+           smt_id;
+}
+
+/* Calculate thread/core/package IDs for a specific topology,
+ * based on (contiguous) CPU index
+ */
+static inline void topo_ids_from_idx(unsigned nr_cores, unsigned nr_threads,
+                                     unsigned cpu_index,
+                                     unsigned *pkg_id, unsigned *core_id,
+                                     unsigned *smt_id)
+{
+    unsigned core_index = cpu_index / nr_threads;
+    *smt_id = cpu_index % nr_threads;
+    *core_id = core_index % nr_cores;
+    *pkg_id = core_index / nr_cores;
+}
+
+/* Make APIC ID for the CPU 'cpu_index'
+ *
+ * 'cpu_index' is a sequential, contiguous ID for the CPU.
+ */
+static inline apic_id_t topo_apicid_for_cpu(unsigned nr_cores,
+                                            unsigned nr_threads,
+                                            unsigned cpu_index)
+{
+    unsigned pkg_id, core_id, smt_id;
+    topo_ids_from_idx(nr_cores, nr_threads, cpu_index,
+                      &pkg_id, &core_id, &smt_id);
+    return topo_make_apicid(nr_cores, nr_threads, pkg_id, core_id, smt_id);
+}
+
+#endif /* TARGET_I386_TOPOLOGY_H */
tests/.gitignore +1 -0
index f9041f3..38c94ef 100644
@@ -10,4 +10,5 @@ test-qmp-commands.h
 test-qmp-commands
 test-qmp-input-strict
 test-qmp-marshal.c
+test-x86-cpuid
 *-test
tests/Makefile +7 -0
index 41172d6..d46e64c 100644
@@ -46,6 +46,11 @@ gcov-files-test-aio-$(CONFIG_POSIX) = aio-posix.c
 check-unit-y += tests/test-thread-pool$(EXESUF)
 gcov-files-test-thread-pool-y = thread-pool.c
 
+check-unit-i386-y += tests/test-x86-cpuid$(EXESUF)
+# all code tested by test-x86-cpuid is inside topology.h,
+# so add the test file itself to the gcov list
+gcov-files-test-x86-cpuid-y = tests/test-x86-cpuid.c
+
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
 # All QTests for now are POSIX-only, but the dependencies are
@@ -71,6 +76,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \
 	tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \
 	tests/test-qmp-commands.o tests/test-visitor-serialization.o
+test-obj-i386-y = tests/test-x86-cpuid.o
 
 test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o
 
@@ -86,6 +92,7 @@ tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(block-obj-y) libqemuutil
 tests/test-aio$(EXESUF): tests/test-aio.o $(block-obj-y) libqemuutil.a libqemustub.a
 tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(block-obj-y) libqemuutil.a libqemustub.a
 tests/test-iov$(EXESUF): tests/test-iov.o libqemuutil.a
+tests/test-x86-cpuid$(EXESUF): tests/test-x86-cpuid.o
 
 tests/test-qapi-types.c tests/test-qapi-types.h :\
 $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
tests/test-x86-cpuid.c new file +101 -0

101 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#37 lore raw

Re: [Qemu-devel] [PATCH for-1.4 11/12] target-i386: Topology & APIC ID utility functions

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 74 lines

---
Changes v1 -> v2:
- Support 32-bit APIC IDs (in case x2APIC is going to be used)
- Coding style changes
- Use TARGET_I386_TOPOLOGY_H instead of __QEMU_X86_TOPOLOGY_H__
- Rename topo_make_apic_id() to topo_apicid_for_cpu()
- Rename __make_apicid() to topo_make_apicid()
- Spaces around operators on test-x86-cpuid.c, as requested by Blue Swirl
- Make test-x86-cpuid a target-specific test

Changes v2 -> v3:
- Add documentation pointers to the code
- Rename bits_for_count() to bitwidth_for_count()
- Remove unused apicid_*_id() functions

Changes v3 -> v4:
- Remove now-obsolete FIXME comment from test-x86-cpuid.c
- Change bitops.h include to qemu/bitops.h
- Add gcov file list to test-x86-cpuid

---
 target-i386/topology.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/.gitignore       |   1 +
 tests/Makefile         |   7 +++
 tests/test-x86-cpuid.c | 101 +++++++++++++++++++++++++++++++++++++
 4 files changed, 242 insertions(+)
 create mode 100644 target-i386/topology.h
 create mode 100644 tests/test-x86-cpuid.c
target-i386/topology.h new file +38 -0
new file mode 100644
index 0000000..833ab47
@@ -0,0 +1,133 @@
+/*
+ *  x86 CPU topology data structures and functions
+ *
+ *  Copyright (c) 2012 Red Hat Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef TARGET_I386_TOPOLOGY_H
+#define TARGET_I386_TOPOLOGY_H
+
+/* This file implements the APIC-ID-based CPU topology enumeration logic,
+ * documented at the following document:
+ *   Intel® 64 Architecture Processor Topology Enumeration
+ *   http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/
+ *
+ * This code should be compatible with AMD's "Extended Method" described at:
+ *   AMD CPUID Specification (Publication #25481)
+ *   Section 3: Multiple Core Calcuation
+ * as long as:
+ *  nr_threads is set to 1;
+ *  OFFSET_IDX is assumed to be 0;
+ *  CPUID Fn8000_0008_ECX[ApicIdCoreIdSize[3:0]] is set to apicid_core_width().

target-i386/topology.h line 48

Quoted 10 lines
target-i386/topology.h new file +10 -0
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+#include "qemu/bitops.h"
+
+/* APIC IDs can be 32-bit, but beware: APIC IDs > 255 require x2APIC support
+ */
+typedef uint32_t apic_id_t;

Is this file imported from somewhere? There was a discussion some time ago about not using _t since reserved by POSIX...

Quoted 29 lines
target-i386/topology.h new file +29 -0
+
+/* Return the bit width needed for 'count' IDs
+ */
+static unsigned bitwidth_for_count(unsigned count)
+{
+    g_assert(count >= 1);
+    if (count == 1) {
+        return 0;
+    }
+    return bitops_flsl(count - 1) + 1;
+}
+
+/* Bit width of the SMT_ID (thread ID) field on the APIC ID
+ */
+static inline unsigned apicid_smt_width(unsigned nr_cores, unsigned nr_threads)
+{
+    return bitwidth_for_count(nr_threads);
+}
+
+/* Bit width of the Core_ID field
+ */
+static inline unsigned apicid_core_width(unsigned nr_cores, unsigned nr_threads)
+{
+    return bitwidth_for_count(nr_cores);
+}
+
+/* Bit offset of the Core_ID field
+ */
+static inline unsigned apicid_core_offset(unsigned nr_cores,

target-i386/topology.h line 87

Quoted 10 lines
target-i386/topology.h new file +10 -0
+                                          unsigned nr_threads)
+{
+    return apicid_smt_width(nr_cores, nr_threads);
+}
+
+/* Bit offset of the Pkg_ID (socket ID) field
+ */
+static inline unsigned apicid_pkg_offset(unsigned nr_cores, unsigned nr_threads)
+{
+    return apicid_core_offset(nr_cores, nr_threads) + \

Not a macro. :)

Quoted 4 lines
target-i386/topology.h new file +4 -0
+           apicid_core_width(nr_cores, nr_threads);
+}
+
+/* Make APIC ID for the CPU based on Pkg_ID, Core_ID, SMT_ID

target-i386/topology.h line 101

Quoted 10 lines
target-i386/topology.h new file +10 -0
+ *
+ * The caller must make sure core_id < nr_cores and smt_id < nr_threads.
+ */
+static inline apic_id_t topo_make_apicid(unsigned nr_cores,
+                                         unsigned nr_threads,
+                                         unsigned pkg_id, unsigned core_id,
+                                         unsigned smt_id)
+{
+    return (pkg_id  << apicid_pkg_offset(nr_cores, nr_threads)) | \
+           (core_id << apicid_core_offset(nr_cores, nr_threads)) | \

Ditto.

Quoted 22 lines
target-i386/topology.h new file +22 -0
+           smt_id;
+}
+
+/* Calculate thread/core/package IDs for a specific topology,
+ * based on (contiguous) CPU index
+ */
+static inline void topo_ids_from_idx(unsigned nr_cores, unsigned nr_threads,
+                                     unsigned cpu_index,
+                                     unsigned *pkg_id, unsigned *core_id,
+                                     unsigned *smt_id)
+{
+    unsigned core_index = cpu_index / nr_threads;
+    *smt_id = cpu_index % nr_threads;
+    *core_id = core_index % nr_cores;
+    *pkg_id = core_index / nr_cores;
+}
+
+/* Make APIC ID for the CPU 'cpu_index'
+ *
+ * 'cpu_index' is a sequential, contiguous ID for the CPU.
+ */
+static inline apic_id_t topo_apicid_for_cpu(unsigned nr_cores,

target-i386/topology.h line 133

Quoted 10 lines
target-i386/topology.h new file +10 -0
+                                            unsigned nr_threads,
+                                            unsigned cpu_index)
+{
+    unsigned pkg_id, core_id, smt_id;
+    topo_ids_from_idx(nr_cores, nr_threads, cpu_index,
+                      &pkg_id, &core_id, &smt_id);
+    return topo_make_apicid(nr_cores, nr_threads, pkg_id, core_id, smt_id);
+}
+
+#endif /* TARGET_I386_TOPOLOGY_H */

As a follow-up it would be nice to clean up the documentation gtk-doc style, e.g. @cpu_index: bla bla, and first function name, then parameters, then description.

Quoted 139 lines
tests/.gitignore +1 -0
index f9041f3..38c94ef 100644
@@ -10,4 +10,5 @@ test-qmp-commands.h
 test-qmp-commands
 test-qmp-input-strict
 test-qmp-marshal.c
+test-x86-cpuid
 *-test
tests/Makefile +7 -0
index 41172d6..d46e64c 100644
@@ -46,6 +46,11 @@ gcov-files-test-aio-$(CONFIG_POSIX) = aio-posix.c
 check-unit-y += tests/test-thread-pool$(EXESUF)
 gcov-files-test-thread-pool-y = thread-pool.c
 
+check-unit-i386-y += tests/test-x86-cpuid$(EXESUF)
+# all code tested by test-x86-cpuid is inside topology.h,
+# so add the test file itself to the gcov list
+gcov-files-test-x86-cpuid-y = tests/test-x86-cpuid.c
+
 check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
 # All QTests for now are POSIX-only, but the dependencies are
@@ -71,6 +76,7 @@ test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
 	tests/test-string-input-visitor.o tests/test-qmp-output-visitor.o \
 	tests/test-qmp-input-visitor.o tests/test-qmp-input-strict.o \
 	tests/test-qmp-commands.o tests/test-visitor-serialization.o
+test-obj-i386-y = tests/test-x86-cpuid.o
 
 test-qapi-obj-y = tests/test-qapi-visit.o tests/test-qapi-types.o
 
@@ -86,6 +92,7 @@ tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(block-obj-y) libqemuutil
 tests/test-aio$(EXESUF): tests/test-aio.o $(block-obj-y) libqemuutil.a libqemustub.a
 tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(block-obj-y) libqemuutil.a libqemustub.a
 tests/test-iov$(EXESUF): tests/test-iov.o libqemuutil.a
+tests/test-x86-cpuid$(EXESUF): tests/test-x86-cpuid.o
 
 tests/test-qapi-types.c tests/test-qapi-types.h :\
 $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
tests/test-x86-cpuid.c new file +91 -0
new file mode 100644
index 0000000..1fe9f30
@@ -0,0 +1,101 @@
+/*
+ *  Test code for x86 CPUID and Topology functions
+ *
+ *  Copyright (c) 2012 Red Hat Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <glib.h>
+
+#include "topology.h"
+
+static void test_topo_bits(void)
+{
+    /* simple tests for 1 thread per core, 1 core per socket */
+    g_assert_cmpuint(apicid_smt_width(1, 1), ==, 0);
+    g_assert_cmpuint(apicid_core_width(1, 1), ==, 0);
+
+    g_assert_cmpuint(topo_apicid_for_cpu(1, 1, 0), ==, 0);
+    g_assert_cmpuint(topo_apicid_for_cpu(1, 1, 1), ==, 1);
+    g_assert_cmpuint(topo_apicid_for_cpu(1, 1, 2), ==, 2);
+    g_assert_cmpuint(topo_apicid_for_cpu(1, 1, 3), ==, 3);
+
+
+    /* Test field width calculation for multiple values
+     */
+    g_assert_cmpuint(apicid_smt_width(1, 2), ==, 1);
+    g_assert_cmpuint(apicid_smt_width(1, 3), ==, 2);
+    g_assert_cmpuint(apicid_smt_width(1, 4), ==, 2);
+
+    g_assert_cmpuint(apicid_smt_width(1, 14), ==, 4);
+    g_assert_cmpuint(apicid_smt_width(1, 15), ==, 4);
+    g_assert_cmpuint(apicid_smt_width(1, 16), ==, 4);
+    g_assert_cmpuint(apicid_smt_width(1, 17), ==, 5);
+
+
+    g_assert_cmpuint(apicid_core_width(30, 2), ==, 5);
+    g_assert_cmpuint(apicid_core_width(31, 2), ==, 5);
+    g_assert_cmpuint(apicid_core_width(32, 2), ==, 5);
+    g_assert_cmpuint(apicid_core_width(33, 2), ==, 6);
+
+
+    /* build a weird topology and see if IDs are calculated correctly
+     */
+
+    /* This will use 2 bits for thread ID and 3 bits for core ID
+     */
+    g_assert_cmpuint(apicid_smt_width(6, 3), ==, 2);
+    g_assert_cmpuint(apicid_core_width(6, 3), ==, 3);
+    g_assert_cmpuint(apicid_pkg_offset(6, 3), ==, 5);
+
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 0), ==, 0);
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 1), ==, 1);
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 2), ==, 2);
+
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 1 * 3 + 0), ==, (1 << 2) | 0);
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 1 * 3 + 1), ==, (1 << 2) | 1);
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 1 * 3 + 2), ==, (1 << 2) | 2);
+
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 2 * 3 + 0), ==, (2 << 2) | 0);
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 2 * 3 + 1), ==, (2 << 2) | 1);
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 2 * 3 + 2), ==, (2 << 2) | 2);
+
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 5 * 3 + 0), ==, (5 << 2) | 0);
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 5 * 3 + 1), ==, (5 << 2) | 1);
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 5 * 3 + 2), ==, (5 << 2) | 2);
+
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 1 * 6 * 3 + 0 * 3 + 0), ==,
+                                         (1 << 5));
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 1 * 6 * 3 + 1 * 3 + 1), ==,
+                                         (1 << 5) | (1 << 2) | 1);
+    g_assert_cmpuint(topo_apicid_for_cpu(6, 3, 3 * 6 * 3 + 5 * 3 + 2), ==,
+                                         (3 << 5) | (5 << 2) | 2);
+}
+

tests/test-x86-cpuid.c line 101

Quoted 10 lines
tests/test-x86-cpuid.c new file +10 -0
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/cpuid/topology/basic", test_topo_bits);
+
+    g_test_run();
+
+    return 0;
+}

Otherwise looks good, thanks for adding tests!

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#38 lore raw

PATCH 12/12 pc: Generate APIC IDs according to CPU topology

In reply to Eduardo Habkost

This keeps compatibility on machine-types pc-1.2 and older, and prints a warning in case the requested configuration won't get the correct topology.

I couldn't think of a better way to warn about broken topology when in compat mode other than using error_report(). The warning message will be probably be buried in a log file somewhere, but it's better than nothing.

---
Changes v1 -> v2:
- Move code to cpu.c
- keep using cpu_index on *-user
- Use SMP.contiguous_apic_ids global property
- Prints warning in case the compatibility mode will expose incorrect topology

Changes v2 -> v3:
- Now all code is inside hw/pc.c
- Use a real "PC" class and a "contiguous_apic_ids" property

Changes v3 -> v4:
- Instead of using a global property, use a separate machine init function and a PCInitArgs field, to implement compatibility mode
- Use error_report() instead of fprintf(stderr) for the warning
- Use a field on PCInitArgs instead of a static variable to check if warning was already printed

Changes v4 -> v5:
- Don't use PCInitArgs: simply add a enable_compat_apic_id_mode() function and a static compat_apic_id_mode variable, to enable the compatibility mode
- Move APIC ID calculation code to cpu.c

---
 hw/pc_piix.c      | 12 ++++++++++--
 target-i386/cpu.c | 28 ++++++++++++++++++++++++----
 target-i386/cpu.h |  1 +
 3 files changed, 35 insertions(+), 6 deletions(-)
hw/pc_piix.c +10 -2
index f9cfe78..b9a9b2e 100644
@@ -233,11 +233,17 @@ static void pc_init_pci(QEMUMachineInitArgs *args)
              initrd_filename, cpu_model, 1, 1);
 }
 
+static void pc_init_pci_1_3(QEMUMachineInitArgs *args)
+{
+    enable_compat_apic_id_mode();
+    pc_init_pci(args);
+}
+
 /* PC machine init function for pc-0.14 to pc-1.2 */
 static void pc_init_pci_1_2(QEMUMachineInitArgs *args)
 {
     disable_kvm_pv_eoi();
-    pc_init_pci(args);
+    pc_init_pci_1_3(args);
 }
 
 /* PC init function for pc-0.10 to pc-0.13, and reused by xenfv */
@@ -250,6 +256,7 @@ static void pc_init_pci_no_kvmclock(QEMUMachineInitArgs *args)
     const char *initrd_filename = args->initrd_filename;
     const char *boot_device = args->boot_device;
     disable_kvm_pv_eoi();
+    enable_compat_apic_id_mode();
     pc_init1(get_system_memory(),
              get_system_io(),
              ram_size, boot_device,
@@ -268,6 +275,7 @@ static void pc_init_isa(QEMUMachineInitArgs *args)
     if (cpu_model == NULL)
         cpu_model = "486";
     disable_kvm_pv_eoi();
+    enable_compat_apic_id_mode();
     pc_init1(get_system_memory(),
              get_system_io(),
              ram_size, boot_device,
@@ -306,7 +314,7 @@ static QEMUMachine pc_i440fx_machine_v1_4 = {
 static QEMUMachine pc_machine_v1_3 = {
     .name = "pc-1.3",
     .desc = "Standard PC",
-    .init = pc_init_pci,
+    .init = pc_init_pci_1_3,
     .max_cpus = 255,
     .compat_props = (GlobalProperty[]) {
         PC_COMPAT_1_3,
target-i386/cpu.c +24 -4
index d90789d..4588f08 100644
@@ -23,6 +23,8 @@
 
 #include "cpu.h"
 #include "sysemu/kvm.h"
+#include "sysemu/cpus.h"
+#include "topology.h"
 
 #include "qemu/option.h"
 #include "qemu/config-file.h"
@@ -2194,6 +2196,14 @@ void x86_cpu_realize(Object *obj, Error **errp)
     cpu_reset(CPU(cpu));
 }
 
+/* Enables contiguous-apic-ID mode, for compatibility */
+static bool compat_apic_id_mode;
+
+void enable_compat_apic_id_mode(void)
+{
+    compat_apic_id_mode = true;
+}
+
 /* Calculates initial APIC ID for a specific CPU index
  *
  * Currently we need to be able to calculate the APIC ID from the CPU index
@@ -2203,10 +2213,20 @@ void x86_cpu_realize(Object *obj, Error **errp)
  */
 uint32_t apic_id_for_cpu(unsigned int cpu_index)
 {
-    /* right now APIC ID == CPU index. this will eventually change to use
-     * the CPU topology configuration properly
-     */
-    return cpu_index;
+    uint32_t correct_id;
+    static bool warned;
+
+    correct_id = topo_apicid_for_cpu(smp_cores, smp_threads, cpu_index);
+    if (compat_apic_id_mode) {
+        if (cpu_index != correct_id && !warned) {
+            error_report("APIC IDs set in compatibility mode, "
+                         "CPU topology won't match the configuration");
+            warned = true;
+        }
+        return cpu_index;
+    } else {
+        return correct_id;
+    }
 }
 
 static void x86_cpu_initfn(Object *obj)
target-i386/cpu.h +1 -0
index d86c0af..cf42d76 100644
@@ -1256,5 +1256,6 @@ void disable_kvm_pv_eoi(void);
 const char *get_register_name_32(unsigned int reg);
 
 uint32_t apic_id_for_cpu(unsigned int cpu_index);
+void enable_compat_apic_id_mode(void);
 
 #endif /* CPU_I386_H */
Signature · 1 line
1.7.11.7
#39 lore raw
In reply to Eduardo Habkost
Quoted 4 lines · 在 2013-01-17四的 18:59 -0200,Eduardo Habkost写道:

I am hoping to get this bug fixed in 1.4. I didn't get much feedback on the RFC I sent last week, though.

Igor argued that APIC ID should be set by the board and not by the CPU itself,

per Intel's SPEC, seems APIC ID really based on design of board. (refer to Intel® 64 and IA-32 Architectures
Software Developer’s Manual
Volume 3 (3A, 3B & 3C):
System Programming Guide
chapter 10.4.6)
but, actually, it maybe meaningless for emulation. after go though your patches,
I can't capture the purpose you do a topology map between APIC ID and cpu_index, (sorry for that)
can you help to clear that?

Quoted 52 lines

but I am not doing that because:
- I want to keep the bug fix simple and isolated as we are past soft freeze
- I believe the creator of the CPU object shouldn't be forced to provide the APIC ID, so the APIC ID is not unnecessarily exposed on the CPU hotplug device_add interface in the future
- The APIC ID _is_ set by the CPU itself (because each CPU package may have multiple core/threads, and each core/thread has a different APIC ID). What needs to be provided by the board to the CPU package in the future is the package ID and the bit width of the core/thread IDs.

Git tree for reference:
git://github.com/ehabkost/qemu-hacks.git apicid-topology.v5
https://github.com/ehabkost/qemu-hacks/tree/apicid-topology.v5

Eduardo Habkost (12):
kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou
KVM
target-i386: Don't set any KVM flag by default if KVM is disabled
pc: Reverse pc_init_pci() compatibility logic
kvm: Create kvm_arch_vcpu_id() function
target-i386: kvm: Set vcpu_id to APIC ID instead of CPU index
fw_cfg: Remove FW_CFG_MAX_CPUS from fw_cfg_init() target-i386/cpu: Introduce apic_id_for_cpu() function cpus.h: Make constant smp_cores/smp_threads available on *-user
pc: Set fw_cfg data based on APIC ID calculation
tests: Support target-specific unit tests
target-i386: Topology & APIC ID utility functions
pc: Generate APIC IDs according to CPU topology

 hw/fw_cfg.c            |   1 -
 hw/pc.c                |  44 +++++++++++++---
 hw/pc_piix.c           |  26 +++++++---
 hw/ppc_newworld.c      |   1 +
 hw/ppc_oldworld.c      |   1 +
 hw/sun4m.c             |   3 ++
 hw/sun4u.c             |   1 +
 include/sysemu/cpus.h  |   7 +++
 include/sysemu/kvm.h   |   4 ++
 kvm-all.c              |   2 +-
 target-i386/cpu.c      |  52 +++++++++++++++----
 target-i386/cpu.h      |   5 +-
 target-i386/kvm.c      |   6 +++
 target-i386/topology.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-ppc/kvm.c       |   5 ++
 target-s390x/kvm.c     |   5 ++
 tests/.gitignore       |   1 +
 tests/Makefile         |  21 +++++++-
 tests/test-x86-cpuid.c | 101 +++++++++++++++++++++++++++++++++++++
 19 files changed, 391 insertions(+), 28 deletions(-)
 create mode 100644 target-i386/topology.h
 create mode 100644 tests/test-x86-cpuid.c
Signature · 2 lines
regards!
li guang
#40 lore raw
Quoted 6 lines · li guang
Quoted 4 lines · 在 2013-01-17四的 18:59 -0200,Eduardo Habkost写道:

I am hoping to get this bug fixed in 1.4. I didn't get much feedback on the RFC I sent last week, though.

Igor argued that APIC ID should be set by the board and not by the CPU itself,

Quoted 11 lines

per Intel's SPEC, seems APIC ID really based on design of board. (refer to Intel® 64 and IA-32 Architectures
Software Developer’s Manual
Volume 3 (3A, 3B & 3C):
System Programming Guide
chapter 10.4.6)
but, actually, it maybe meaningless for emulation. after go though your patches,
I can't capture the purpose you do a topology map between APIC ID and cpu_index, (sorry for that)
can you help to clear that?

See the documents mentioned on PATCH 11/12:

+/* This file implements the APIC-ID-based CPU topology enumeration logic,
+ * documented at the following document:
+ * Intel® 64 Architecture Processor Topology Enumeration
+ * http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/
+ *
+ * This code should be compatible with AMD's "Extended Method" described at:
+ * AMD CPUID Specification (Publication #25481)
+ * Section 3: Multiple Core Calcuation
+ * as long as:

+ *  nr_threads is set to 1;
+ *  OFFSET_IDX is assumed to be 0;
+ *  CPUID Fn8000_0008_ECX[ApicIdCoreIdSize[3:0]] is set to apicid_core_width().

+ */

If we don't generate the APIC IDs properly, identification of CPU sockets/cores/threads is broken.

e.g. today -smp 12,cores=3,threads=2 currently ends up exposing 4 cores on the first socket, and 2 cores in the second one, because the APIC IDs are generated sequentially instead of being based on package/core/thread IDs.

Quoted 59 lines
Quoted 53 lines

but I am not doing that because:
- I want to keep the bug fix simple and isolated as we are past soft freeze
- I believe the creator of the CPU object shouldn't be forced to provide the APIC ID, so the APIC ID is not unnecessarily exposed on the CPU hotplug device_add interface in the future
- The APIC ID _is_ set by the CPU itself (because each CPU package may have multiple core/threads, and each core/thread has a different APIC ID). What needs to be provided by the board to the CPU package in the future is the package ID and the bit width of the core/thread IDs.

Git tree for reference:
git://github.com/ehabkost/qemu-hacks.git apicid-topology.v5
https://github.com/ehabkost/qemu-hacks/tree/apicid-topology.v5

Eduardo Habkost (12):
kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou
KVM
target-i386: Don't set any KVM flag by default if KVM is disabled
pc: Reverse pc_init_pci() compatibility logic
kvm: Create kvm_arch_vcpu_id() function
target-i386: kvm: Set vcpu_id to APIC ID instead of CPU index
fw_cfg: Remove FW_CFG_MAX_CPUS from fw_cfg_init() target-i386/cpu: Introduce apic_id_for_cpu() function cpus.h: Make constant smp_cores/smp_threads available on *-user
pc: Set fw_cfg data based on APIC ID calculation
tests: Support target-specific unit tests
target-i386: Topology & APIC ID utility functions
pc: Generate APIC IDs according to CPU topology

 hw/fw_cfg.c            |   1 -
 hw/pc.c                |  44 +++++++++++++---
 hw/pc_piix.c           |  26 +++++++---
 hw/ppc_newworld.c      |   1 +
 hw/ppc_oldworld.c      |   1 +
 hw/sun4m.c             |   3 ++
 hw/sun4u.c             |   1 +
 include/sysemu/cpus.h  |   7 +++
 include/sysemu/kvm.h   |   4 ++
 kvm-all.c              |   2 +-
 target-i386/cpu.c      |  52 +++++++++++++++----
 target-i386/cpu.h      |   5 +-
 target-i386/kvm.c      |   6 +++
 target-i386/topology.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-ppc/kvm.c       |   5 ++
 target-s390x/kvm.c     |   5 ++
 tests/.gitignore       |   1 +
 tests/Makefile         |  21 +++++++-
 tests/test-x86-cpuid.c | 101 +++++++++++++++++++++++++++++++++++++
 19 files changed, 391 insertions(+), 28 deletions(-)
 create mode 100644 target-i386/topology.h
 create mode 100644 tests/test-x86-cpuid.c
Signature · 2 lines
regards!
li guang
Signature · 1 line
Eduardo
#41 lore raw
Quoted 33 lines · 在 2013-01-18五的 12:49 -0200,Eduardo Habkost写道:
Quoted 6 lines · li guang
Quoted 4 lines · 在 2013-01-17四的 18:59 -0200,Eduardo Habkost写道:

I am hoping to get this bug fixed in 1.4. I didn't get much feedback on the RFC I sent last week, though.

Igor argued that APIC ID should be set by the board and not by the CPU itself,

Quoted 11 lines

per Intel's SPEC, seems APIC ID really based on design of board. (refer to Intel® 64 and IA-32 Architectures
Software Developer’s Manual
Volume 3 (3A, 3B & 3C):
System Programming Guide
chapter 10.4.6)
but, actually, it maybe meaningless for emulation. after go though your patches,
I can't capture the purpose you do a topology map between APIC ID and cpu_index, (sorry for that)
can you help to clear that?

See the documents mentioned on PATCH 11/12:

+/* This file implements the APIC-ID-based CPU topology enumeration logic,
+ * documented at the following document:
+ * Intel® 64 Architecture Processor Topology Enumeration
+ * http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/
+ *
+ * This code should be compatible with AMD's "Extended Method" described at:
+ * AMD CPUID Specification (Publication #25481)
+ * Section 3: Multiple Core Calcuation
+ * as long as:

+ *  nr_threads is set to 1;
+ *  OFFSET_IDX is assumed to be 0;
+ *  CPUID Fn8000_0008_ECX[ApicIdCoreIdSize[3:0]] is set to apicid_core_width().
Quoted 8 lines

+ */

If we don't generate the APIC IDs properly, identification of CPU sockets/cores/threads is broken.

e.g. today -smp 12,cores=3,threads=2 currently ends up exposing 4 cores on the first socket, and 2 cores in the second one, because the APIC IDs are generated sequentially instead of being based on package/core/thread IDs.

okay, Thanks!

Quoted 61 lines
Quoted 60 lines
Quoted 54 lines

but I am not doing that because:
- I want to keep the bug fix simple and isolated as we are past soft freeze
- I believe the creator of the CPU object shouldn't be forced to provide the APIC ID, so the APIC ID is not unnecessarily exposed on the CPU hotplug device_add interface in the future
- The APIC ID _is_ set by the CPU itself (because each CPU package may have multiple core/threads, and each core/thread has a different APIC ID). What needs to be provided by the board to the CPU package in the future is the package ID and the bit width of the core/thread IDs.

Git tree for reference:
git://github.com/ehabkost/qemu-hacks.git apicid-topology.v5
https://github.com/ehabkost/qemu-hacks/tree/apicid-topology.v5

Eduardo Habkost (12):
kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou
KVM
target-i386: Don't set any KVM flag by default if KVM is disabled
pc: Reverse pc_init_pci() compatibility logic
kvm: Create kvm_arch_vcpu_id() function
target-i386: kvm: Set vcpu_id to APIC ID instead of CPU index
fw_cfg: Remove FW_CFG_MAX_CPUS from fw_cfg_init() target-i386/cpu: Introduce apic_id_for_cpu() function cpus.h: Make constant smp_cores/smp_threads available on *-user
pc: Set fw_cfg data based on APIC ID calculation
tests: Support target-specific unit tests
target-i386: Topology & APIC ID utility functions
pc: Generate APIC IDs according to CPU topology

 hw/fw_cfg.c            |   1 -
 hw/pc.c                |  44 +++++++++++++---
 hw/pc_piix.c           |  26 +++++++---
 hw/ppc_newworld.c      |   1 +
 hw/ppc_oldworld.c      |   1 +
 hw/sun4m.c             |   3 ++
 hw/sun4u.c             |   1 +
 include/sysemu/cpus.h  |   7 +++
 include/sysemu/kvm.h   |   4 ++
 kvm-all.c              |   2 +-
 target-i386/cpu.c      |  52 +++++++++++++++----
 target-i386/cpu.h      |   5 +-
 target-i386/kvm.c      |   6 +++
 target-i386/topology.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-ppc/kvm.c       |   5 ++
 target-s390x/kvm.c     |   5 ++
 tests/.gitignore       |   1 +
 tests/Makefile         |  21 +++++++-
 tests/test-x86-cpuid.c | 101 +++++++++++++++++++++++++++++++++++++
 19 files changed, 391 insertions(+), 28 deletions(-)
 create mode 100644 target-i386/topology.h
 create mode 100644 tests/test-x86-cpuid.c
Signature · 2 lines
regards!
li guang
Signature · 2 lines
regards!
li guang
#42 lore raw
In reply to Eduardo Habkost

Adding kvm@vger, Gleb, Michael, Marcelo to Cc, as I forgot to disable suppress-cc when sending the patches.

Quoted 60 lines · Eduardo Habkost

I am hoping to get this bug fixed in 1.4. I didn't get much feedback on the RFC I sent last week, though.

Igor argued that APIC ID should be set by the board and not by the CPU itself, but I am not doing that because:
- I want to keep the bug fix simple and isolated as we are past soft freeze
- I believe the creator of the CPU object shouldn't be forced to provide the APIC ID, so the APIC ID is not unnecessarily exposed on the CPU hotplug device_add interface in the future
- The APIC ID _is_ set by the CPU itself (because each CPU package may have multiple core/threads, and each core/thread has a different APIC ID). What needs to be provided by the board to the CPU package in the future is the package ID and the bit width of the core/thread IDs.

Git tree for reference:
git://github.com/ehabkost/qemu-hacks.git apicid-topology.v5
https://github.com/ehabkost/qemu-hacks/tree/apicid-topology.v5

Eduardo Habkost (12):
kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou
KVM
target-i386: Don't set any KVM flag by default if KVM is disabled
pc: Reverse pc_init_pci() compatibility logic
kvm: Create kvm_arch_vcpu_id() function
target-i386: kvm: Set vcpu_id to APIC ID instead of CPU index
fw_cfg: Remove FW_CFG_MAX_CPUS from fw_cfg_init() target-i386/cpu: Introduce apic_id_for_cpu() function cpus.h: Make constant smp_cores/smp_threads available on *-user
pc: Set fw_cfg data based on APIC ID calculation
tests: Support target-specific unit tests
target-i386: Topology & APIC ID utility functions
pc: Generate APIC IDs according to CPU topology

 hw/fw_cfg.c            |   1 -
 hw/pc.c                |  44 +++++++++++++---
 hw/pc_piix.c           |  26 +++++++---
 hw/ppc_newworld.c      |   1 +
 hw/ppc_oldworld.c      |   1 +
 hw/sun4m.c             |   3 ++
 hw/sun4u.c             |   1 +
 include/sysemu/cpus.h  |   7 +++
 include/sysemu/kvm.h   |   4 ++
 kvm-all.c              |   2 +-
 target-i386/cpu.c      |  52 +++++++++++++++----
 target-i386/cpu.h      |   5 +-
 target-i386/kvm.c      |   6 +++
 target-i386/topology.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-ppc/kvm.c       |   5 ++
 target-s390x/kvm.c     |   5 ++
 tests/.gitignore       |   1 +
 tests/Makefile         |  21 +++++++-
 tests/test-x86-cpuid.c | 101 +++++++++++++++++++++++++++++++++++++
 19 files changed, 391 insertions(+), 28 deletions(-)
 create mode 100644 target-i386/topology.h
 create mode 100644 tests/test-x86-cpuid.c
Signature · 1 line
1.7.11.7
Signature · 1 line
Eduardo
#43 lore raw
In reply to Eduardo Habkost

Am 17.01.2013 21:59, schrieb Eduardo Habkost:

Quoted 18 lines

I am hoping to get this bug fixed in 1.4. I didn't get much feedback on the RFC I sent last week, though.

Igor argued that APIC ID should be set by the board and not by the CPU itself, but I am not doing that because:
- I want to keep the bug fix simple and isolated as we are past soft freeze
- I believe the creator of the CPU object shouldn't be forced to provide the APIC ID, so the APIC ID is not unnecessarily exposed on the CPU hotplug device_add interface in the future
- The APIC ID _is_ set by the CPU itself (because each CPU package may have multiple core/threads, and each core/thread has a different APIC ID). What needs to be provided by the board to the CPU package in the future is the package ID and the bit width of the core/thread IDs.

Git tree for reference:
git://github.com/ehabkost/qemu-hacks.git apicid-topology.v5
https://github.com/ehabkost/qemu-hacks/tree/apicid-topology.v5

Quoted 5 lines

Eduardo Habkost (12):
kvm: Add fake KVM_FEATURE_CLOCKSOURCE_STABLE_BIT for builds withou
KVM
target-i386: Don't set any KVM flag by default if KVM is disabled
pc: Reverse pc_init_pci() compatibility logic

Applied these to qom-cpu (for 1.4, with some typo fixes):
https://github.com/afaerber/qemu-cpu/commits/qom-cpu

For the rest there's either open questions or still ongoing review.

Andreas

Quoted 33 lines

kvm: Create kvm_arch_vcpu_id() function
target-i386: kvm: Set vcpu_id to APIC ID instead of CPU index
fw_cfg: Remove FW_CFG_MAX_CPUS from fw_cfg_init() target-i386/cpu: Introduce apic_id_for_cpu() function cpus.h: Make constant smp_cores/smp_threads available on *-user
pc: Set fw_cfg data based on APIC ID calculation
tests: Support target-specific unit tests
target-i386: Topology & APIC ID utility functions
pc: Generate APIC IDs according to CPU topology

 hw/fw_cfg.c            |   1 -
 hw/pc.c                |  44 +++++++++++++---
 hw/pc_piix.c           |  26 +++++++---
 hw/ppc_newworld.c      |   1 +
 hw/ppc_oldworld.c      |   1 +
 hw/sun4m.c             |   3 ++
 hw/sun4u.c             |   1 +
 include/sysemu/cpus.h  |   7 +++
 include/sysemu/kvm.h   |   4 ++
 kvm-all.c              |   2 +-
 target-i386/cpu.c      |  52 +++++++++++++++----
 target-i386/cpu.h      |   5 +-
 target-i386/kvm.c      |   6 +++
 target-i386/topology.h | 133 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-ppc/kvm.c       |   5 ++
 target-s390x/kvm.c     |   5 ++
 tests/.gitignore       |   1 +
 tests/Makefile         |  21 +++++++-
 tests/test-x86-cpuid.c | 101 +++++++++++++++++++++++++++++++++++++
 19 files changed, 391 insertions(+), 28 deletions(-)
 create mode 100644 target-i386/topology.h
 create mode 100644 tests/test-x86-cpuid.c
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg