Skip to contentSkip to conversation

target-i386: Simplify APIC ID initialization, move compat code to pc.c

PATCH Superseded Eduardo Habkost 2 participants 13 messages Feb 26, 2015 – Mar 4, 2015

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover target-i386: Simplify APIC ID initialization, move compat code to pc.c

This series removes the APIC ID initialization code from x86_cpu_initfn() (getting us one step closer to making object_new() of X86CPU have no dependency on cpu_exec_init() and other global QEMU state), and moves the APIC ID compatibility logic from target-i386/cpu.c to hw/i386/pc.c.

Changes v1 -> v2:
* Move topology.h to include/hw/i386 instead of hw/i386
* Instead of adding an apic_id_set field, make apic_id a int64_t field and set it to -1 by default
* Check for cpu_init() errors on linux-user

Changes v2 -> v3:
* Removed patches:
* [PATCH v2 2/9] target-i386: Rename cpu_x86_init() to cpu_x86_init_user()
* [PATCH v2 3/9] target-i386: Eliminate cpu_init() function
* [PATCH v2 4/9] target-i386: Simplify error handling on cpu_x86_init_user()
* Redone patch "target-i386: Set APIC ID using cpu_index on CONFIG_USER" using the existing error handling style

Eduardo Habkost (5):
target-i386: Move topology.h to include/hw/i386
target-i386: Move CPUX86State.cpuid_apic_id to X86CPU.apic_id
target-i386: Set APIC ID using cpu_index on legacy cpu_x86_init()
target-i386: Require APIC ID to be explicitly set before CPU realize
target-i386: Move APIC ID compatibility code to pc.c

 hw/i386/pc.c                                | 35 ++++++++++++++++
 {target-i386 => include/hw/i386}/topology.h |  6 +--
 target-i386/cpu-qom.h                       |  1 +
 target-i386/cpu.c                           | 62 +++++++++--------------------
 target-i386/cpu.h                           |  1 -
 target-i386/kvm.c                           |  2 +-
 tests/Makefile                              |  2 -
 tests/test-x86-cpuid.c                      |  2 +-
 8 files changed, 60 insertions(+), 51 deletions(-)
 rename {target-i386 => include/hw/i386}/topology.h (97%)
Signature · 1 line
2.1.0
#3 lore raw

PATCH v3 2/5 target-i386: Move CPUX86State.cpuid_apic_id to X86CPU.apic_id

The field doesn't need to be inside CPUState, and it is not specific for the CPUID instruction, so move and rename it.

---
 target-i386/cpu-qom.h |  1 +
 target-i386/cpu.c     | 17 ++++++++---------
 target-i386/cpu.h     |  1 -
 target-i386/kvm.c     |  2 +-
 4 files changed, 10 insertions(+), 11 deletions(-)
target-i386/cpu-qom.h +1 -0
index b557b61..4a6f48a 100644
@@ -93,6 +93,7 @@ typedef struct X86CPU {
     bool expose_kvm;
     bool migratable;
     bool host_features;
+    uint32_t apic_id;
 
     /* if true the CPUID code directly forward host cache leaves to the guest */
     bool cache_info_passthrough;
target-i386/cpu.c +8 -9
index 8fc5727..079ce60 100644
@@ -1690,7 +1690,7 @@ static void x86_cpuid_get_apic_id(Object *obj, Visitor *v, void *opaque,
                                   const char *name, Error **errp)
 {
     X86CPU *cpu = X86_CPU(obj);
-    int64_t value = cpu->env.cpuid_apic_id;
+    int64_t value = cpu->apic_id;
 
     visit_type_int(v, &value, name, errp);
 }
@@ -1723,11 +1723,11 @@ static void x86_cpuid_set_apic_id(Object *obj, Visitor *v, void *opaque,
         return;
     }
 
-    if ((value != cpu->env.cpuid_apic_id) && cpu_exists(value)) {
+    if ((value != cpu->apic_id) && cpu_exists(value)) {
         error_setg(errp, "CPU with APIC ID %" PRIi64 " exists", value);
         return;
     }
-    cpu->env.cpuid_apic_id = value;
+    cpu->apic_id = value;
 }
 
 /* Generic getter for "feature-words" and "filtered-features" properties */
@@ -2272,7 +2272,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,
         break;
     case 1:
         *eax = env->cpuid_version;
-        *ebx = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
+        *ebx = (cpu->apic_id << 24) |
+               8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
         *ecx = env->features[FEAT_1_ECX];
         *edx = env->features[FEAT_1_EDX];
         if (cs->nr_cores * cs->nr_threads > 1) {
@@ -2721,7 +2722,6 @@ static void mce_init(X86CPU *cpu)
 #ifndef CONFIG_USER_ONLY
 static void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
 {
-    CPUX86State *env = &cpu->env;
     DeviceState *dev = DEVICE(cpu);
     APICCommonState *apic;
     const char *apic_type = "apic";
@@ -2740,7 +2740,7 @@ static void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
 
     object_property_add_child(OBJECT(cpu), "apic",
                               OBJECT(cpu->apic_state), NULL);
-    qdev_prop_set_uint8(cpu->apic_state, "id", env->cpuid_apic_id);
+    qdev_prop_set_uint8(cpu->apic_state, "id", cpu->apic_id);
     /* TODO: convert to link<> */
     apic = APIC_COMMON(cpu->apic_state);
     apic->cpu = cpu;
@@ -2923,7 +2923,7 @@ static void x86_cpu_initfn(Object *obj)
                         NULL, NULL, (void *)cpu->filtered_features, NULL);
 
     cpu->hyperv_spinlock_attempts = HYPERV_SPINLOCK_NEVER_RETRY;
-    env->cpuid_apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);
+    cpu->apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);
 
     x86_cpu_load_def(cpu, xcc->cpu_def, &error_abort);
 
@@ -2937,9 +2937,8 @@ static void x86_cpu_initfn(Object *obj)
 static int64_t x86_cpu_get_arch_id(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
-    CPUX86State *env = &cpu->env;
 
-    return env->cpuid_apic_id;
+    return cpu->apic_id;
 }
 
 static bool x86_cpu_get_paging_enabled(const CPUState *cs)
target-i386/cpu.h +0 -1
index 478450c..38bedc2 100644
@@ -944,7 +944,6 @@ typedef struct CPUX86State {
     uint32_t cpuid_version;
     FeatureWordArray features;
     uint32_t cpuid_model[12];
-    uint32_t cpuid_apic_id;
 
     /* MTRRs */
     uint64_t mtrr_fixed[11];
target-i386/kvm.c +1 -1
index 40d6a14..27fe2be 100644
@@ -430,7 +430,7 @@ static void cpu_update_state(void *opaque, int running, RunState state)
 unsigned long kvm_arch_vcpu_id(CPUState *cs)
 {
     X86CPU *cpu = X86_CPU(cs);
-    return cpu->env.cpuid_apic_id;
+    return cpu->apic_id;
 }
 
 #ifndef KVM_CPUID_SIGNATURE_NEXT
Signature · 1 line
2.1.0
#8 lore raw

PATCH v3 4/5 target-i386: Require APIC ID to be explicitly set before CPU realize

In reply to Eduardo Habkost

Instead of setting APIC ID automatically when creating a X86CPU, require the property to be set before realizing the object (which all callers of cpu_x86_create() already do).

---
 target-i386/cpu-qom.h | 2 +-
 target-i386/cpu.c     | 7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
target-i386/cpu-qom.h +1 -1
index 4a6f48a..31a0c1e 100644
@@ -93,7 +93,7 @@ typedef struct X86CPU {
     bool expose_kvm;
     bool migratable;
     bool host_features;
-    uint32_t apic_id;
+    int64_t apic_id;
 
     /* if true the CPUID code directly forward host cache leaves to the guest */
     bool cache_info_passthrough;
target-i386/cpu.c +6 -1
index c4a96b6..9819c47 100644
@@ -2786,6 +2786,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
     Error *local_err = NULL;
     static bool ht_warned;
 
+    if (cpu->apic_id < 0) {
+        error_setg(errp, "apic-id property was not initialized properly");
+        return;
+    }
+
     if (env->features[FEAT_7_0_EBX] && env->cpuid_level < 7) {
         env->cpuid_level = 7;
     }
@@ -2929,7 +2934,7 @@ static void x86_cpu_initfn(Object *obj)
                         NULL, NULL, (void *)cpu->filtered_features, NULL);
 
     cpu->hyperv_spinlock_attempts = HYPERV_SPINLOCK_NEVER_RETRY;
-    cpu->apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);
+    cpu->apic_id = -1;
 
     x86_cpu_load_def(cpu, xcc->cpu_def, &error_abort);
 
Signature · 1 line
2.1.0
#6 lore raw

PATCH v3 3/5 target-i386: Set APIC ID using cpu_index on legacy cpu_x86_init()

In reply to Eduardo Habkost

The PC CPU initialization code already sets apic-id based on the CPU topology, and CONFIG_USER doesn't need the topology-based APIC ID calculation code.

Make cpu_x86_init() (used only by CONFIG_USER) set apic-id before realizing the CPU (just like PC already does), so we can simplify x86_cpu_initfn later. As there is no CPU topology configuration in CONFIG_USER, just use cpu_index as the APIC ID.

---
Changes v1 -> v2: none
Changes v2 -> v3:
* Use "goto out" instead of "goto error" after patch changing error handling was removed from the series

---
 target-i386/cpu.c | 6 ++++++
 1 file changed, 6 insertions(+)
target-i386/cpu.c +6 -0
index 079ce60..c4a96b6 100644
@@ -2159,6 +2159,12 @@ X86CPU *cpu_x86_init(const char *cpu_model)
         goto out;
     }
 
+    object_property_set_int(OBJECT(cpu), CPU(cpu)->cpu_index, "apic-id",
+                            &error);
+    if (error) {
+        goto out;
+    }
+
     object_property_set_bool(OBJECT(cpu), true, "realized", &error);
 
 out:
Signature · 1 line
2.1.0
#11 lore raw

PATCH v3 5/5 target-i386: Move APIC ID compatibility code to pc.c

In reply to Eduardo Habkost

The APIC ID compatibility code is required only for PC, and now that x86_cpu_initfn() doesn't use x86_cpu_apic_id_from_index() anymore, that code can be moved to pc.c.

---
 hw/i386/pc.c      | 35 +++++++++++++++++++++++++++++++++++
 target-i386/cpu.c | 34 ----------------------------------
 2 files changed, 35 insertions(+), 34 deletions(-)
hw/i386/pc.c +35 -0
index 05008cb..1941d13 100644
@@ -25,6 +25,8 @@
 #include "hw/i386/pc.h"
 #include "hw/char/serial.h"
 #include "hw/i386/apic.h"
+#include "hw/i386/topology.h"
+#include "sysemu/cpus.h"
 #include "hw/block/fdc.h"
 #include "hw/ide.h"
 #include "hw/pci/pci.h"
@@ -628,6 +630,39 @@ bool e820_get_entry(int idx, uint32_t type, uint64_t *address, uint64_t *length)
     return false;
 }
 
+/* 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
+ * 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 x86_cpu_apic_id_from_index(unsigned int cpu_index)
+{
+    uint32_t correct_id;
+    static bool warned;
+
+    correct_id = x86_apicid_from_cpu_idx(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;
+    }
+}
+
 /* Calculates the limit to CPU APIC ID values
  *
  * This function returns the limit for the APIC ID value, so that all
target-i386/cpu.c +0 -34
index 9819c47..5af26a1 100644
@@ -25,7 +25,6 @@
 #include "sysemu/kvm.h"
 #include "sysemu/cpus.h"
 #include "kvm_i386.h"
-#include "hw/i386/topology.h"
 
 #include "qemu/option.h"
 #include "qemu/config-file.h"
@@ -2855,39 +2854,6 @@ out:
     }
 }
 
-/* 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
- * 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 x86_cpu_apic_id_from_index(unsigned int cpu_index)
-{
-    uint32_t correct_id;
-    static bool warned;
-
-    correct_id = x86_apicid_from_cpu_idx(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)
 {
     CPUState *cs = CPU(obj);
Signature · 1 line
2.1.0
#1 lore raw

PATCH v3 1/5 target-i386: Move topology.h to include/hw/i386

In reply to Eduardo Habkost

This will allow the PC code to use the header, and lets us eliminate the QEMU_INCLUDES hack inside tests/Makefile.

---
Changes v1 -> v2:
* Move it to include/hw/i386 instead of hw/i386

Changes v2 -> v3: none

---
 {target-i386 => include/hw/i386}/topology.h | 6 +++---
 target-i386/cpu.c                           | 2 +-
 tests/Makefile                              | 2 --
 tests/test-x86-cpuid.c                      | 2 +-
 4 files changed, 5 insertions(+), 7 deletions(-)
 rename {target-i386 => include/hw/i386}/topology.h (97%)
include/hw/i386/topology.h renamed from target-i386/topology.h +3 -3
similarity index 97%
rename from target-i386/topology.h
rename to include/hw/i386/topology.h
index 07a6c5f..9c6f3a9 100644
@@ -21,8 +21,8 @@
  * 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
+#ifndef HW_I386_TOPOLOGY_H
+#define HW_I386_TOPOLOGY_H
 
 /* This file implements the APIC-ID-based CPU topology enumeration logic,
  * documented at the following document:
@@ -131,4 +131,4 @@ static inline apic_id_t x86_apicid_from_cpu_idx(unsigned nr_cores,
     return apicid_from_topo_ids(nr_cores, nr_threads, pkg_id, core_id, smt_id);
 }
 
-#endif /* TARGET_I386_TOPOLOGY_H */
+#endif /* HW_I386_TOPOLOGY_H */
target-i386/cpu.c +1 -1
index d543e2b..8fc5727 100644
@@ -25,7 +25,7 @@
 #include "sysemu/kvm.h"
 #include "sysemu/cpus.h"
 #include "kvm_i386.h"
-#include "topology.h"
+#include "hw/i386/topology.h"
 
 #include "qemu/option.h"
 #include "qemu/config-file.h"
tests/Makefile +0 -2
index 307035c..7d4b96d 100644
@@ -239,8 +239,6 @@ $(test-obj-y): QEMU_INCLUDES += -Itests
 QEMU_CFLAGS += -I$(SRC_PATH)/tests
 qom-core-obj = qom/object.o qom/qom-qobject.o qom/container.o
 
-tests/test-x86-cpuid.o: QEMU_INCLUDES += -I$(SRC_PATH)/target-i386
-
 tests/check-qint$(EXESUF): tests/check-qint.o libqemuutil.a
 tests/check-qstring$(EXESUF): tests/check-qstring.o libqemuutil.a
 tests/check-qdict$(EXESUF): tests/check-qdict.o libqemuutil.a
tests/test-x86-cpuid.c +1 -1
index 8d9f96a..6cd20d4 100644
@@ -24,7 +24,7 @@
 
 #include <glib.h>
 
-#include "topology.h"
+#include "hw/i386/topology.h"
 
 static void test_topo_bits(void)
 {
Signature · 1 line
2.1.0
#7 lore raw

Re: [Qemu-devel] [PATCH v3 3/5] target-i386: Set APIC ID using cpu_index on legacy cpu_x86_init()

In reply to Eduardo Habkost

Am 26.02.2015 um 22:42 schrieb Eduardo Habkost:

Quoted 26 lines

The PC CPU initialization code already sets apic-id based on the CPU topology, and CONFIG_USER doesn't need the topology-based APIC ID calculation code.

Make cpu_x86_init() (used only by CONFIG_USER) set apic-id before realizing the CPU (just like PC already does), so we can simplify x86_cpu_initfn later. As there is no CPU topology configuration in CONFIG_USER, just use cpu_index as the APIC ID.

---
Changes v1 -> v2: none
Changes v2 -> v3:
* Use "goto out" instead of "goto error" after patch changing error handling was removed from the series

---
 target-i386/cpu.c | 6 ++++++
 1 file changed, 6 insertions(+)
target-i386/cpu.c +0 -0
index 079ce60..c4a96b6 100644
@@ -2159,6 +2159,12 @@ X86CPU *cpu_x86_init(const char *cpu_model)
         goto out;
     }

target-i386/cpu.c X86CPU *cpu_x86_init(const char *cpu_model) · line 2170

Quoted 10 lines
target-i386/cpu.c +6 -0
 
+    object_property_set_int(OBJECT(cpu), CPU(cpu)->cpu_index, "apic-id",
+                            &error);
+    if (error) {
+        goto out;
+    }
+
     object_property_set_bool(OBJECT(cpu), true, "realized", &error);
 
 out:

Why do this here? Cf. following patch.

Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#9 lore raw

Re: [Qemu-devel] [PATCH v3 4/5] target-i386: Require APIC ID to be explicitly set before CPU realize

In reply to Eduardo Habkost

Am 26.02.2015 um 22:42 schrieb Eduardo Habkost:

Quoted 29 lines

Instead of setting APIC ID automatically when creating a X86CPU, require the property to be set before realizing the object (which all callers of cpu_x86_create() already do).

---
 target-i386/cpu-qom.h | 2 +-
 target-i386/cpu.c     | 7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
target-i386/cpu-qom.h +1 -1
index 4a6f48a..31a0c1e 100644
@@ -93,7 +93,7 @@ typedef struct X86CPU {
     bool expose_kvm;
     bool migratable;
     bool host_features;
-    uint32_t apic_id;
+    int64_t apic_id;
 
     /* if true the CPUID code directly forward host cache leaves to the guest */
     bool cache_info_passthrough;
target-i386/cpu.c +0 -0
index c4a96b6..9819c47 100644

target-i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 2789

Quoted 4 lines
target-i386/cpu.c +1 -0
@@ -2786,6 +2786,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
     Error *local_err = NULL;
     static bool ht_warned;
 
+    if (cpu->apic_id < 0) {

Since you were setting it immediately before setting realized=true,

#ifdef CONFIG_USER_ONLY
    cpu->apic_id = cs->cpu_index;

#else

target-i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 2791

Quoted 2 lines
target-i386/cpu.c +2 -0
+        error_setg(errp, "apic-id property was not initialized properly");
+        return;

#endif

would keep any x86-specific logic out of cpu_init().

Regards,
Andreas

Quoted 14 lines
target-i386/cpu.c +3 -1
+    }
+
     if (env->features[FEAT_7_0_EBX] && env->cpuid_level < 7) {
         env->cpuid_level = 7;
     }
@@ -2929,7 +2934,7 @@ static void x86_cpu_initfn(Object *obj)
                         NULL, NULL, (void *)cpu->filtered_features, NULL);
 
     cpu->hyperv_spinlock_attempts = HYPERV_SPINLOCK_NEVER_RETRY;
-    cpu->apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);
+    cpu->apic_id = -1;
 
     x86_cpu_load_def(cpu, xcc->cpu_def, &error_abort);
 
Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
Reviewed-by on v3 2/5
#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] target-i386: Move CPUX86State.cpuid_apic_id to X86CPU.apic_id

In reply to Eduardo Habkost

Am 26.02.2015 um 22:42 schrieb Eduardo Habkost:

Quoted 5 lines

The field doesn't need to be inside CPUState, and it is not specific for the CPUID instruction, so move and rename it.

Really great,

Thanks,
Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
Reviewed-by on v3 5/5
#12 lore raw

Re: [Qemu-devel] [PATCH v3 5/5] target-i386: Move APIC ID compatibility code to pc.c

In reply to Eduardo Habkost

Am 26.02.2015 um 22:42 schrieb Eduardo Habkost:

Quoted 10 lines

The APIC ID compatibility code is required only for PC, and now that x86_cpu_initfn() doesn't use x86_cpu_apic_id_from_index() anymore, that code can be moved to pc.c.

---
 hw/i386/pc.c      | 35 +++++++++++++++++++++++++++++++++++
 target-i386/cpu.c | 34 ----------------------------------
 2 files changed, 35 insertions(+), 34 deletions(-)

Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
Reviewed-by on v3 1/5
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/5] target-i386: Move topology.h to include/hw/i386

In reply to Eduardo Habkost

Am 26.02.2015 um 22:42 schrieb Eduardo Habkost:

Quoted 5 lines

This will allow the PC code to use the header, and lets us eliminate the QEMU_INCLUDES hack inside tests/Makefile.

Looks good,

Andreas

Signature · 3 lines
SUSE Linux GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Felix Imendörffer, Jane Smithard, Jennifer Guild, Dilip Upmanyu,
Graham Norton; HRB 21284 (AG Nürnberg)
#10 lore raw

Re: [Qemu-devel] [PATCH v3 4/5] target-i386: Require APIC ID to be explicitly set before CPU realize

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

Am 26.02.2015 um 22:42 schrieb Eduardo Habkost:

Quoted 29 lines

Instead of setting APIC ID automatically when creating a X86CPU, require the property to be set before realizing the object (which all callers of cpu_x86_create() already do).

---
 target-i386/cpu-qom.h | 2 +-
 target-i386/cpu.c     | 7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
target-i386/cpu-qom.h +1 -1
index 4a6f48a..31a0c1e 100644
@@ -93,7 +93,7 @@ typedef struct X86CPU {
     bool expose_kvm;
     bool migratable;
     bool host_features;
-    uint32_t apic_id;
+    int64_t apic_id;
 
     /* if true the CPUID code directly forward host cache leaves to the guest */
     bool cache_info_passthrough;
target-i386/cpu.c +0 -0
index c4a96b6..9819c47 100644

target-i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 2789

Quoted 4 lines
target-i386/cpu.c +1 -0
@@ -2786,6 +2786,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
     Error *local_err = NULL;
     static bool ht_warned;
 
+    if (cpu->apic_id < 0) {

Since you were setting it immediately before setting realized=true,

#ifdef CONFIG_USER_ONLY
    cpu->apic_id = cs->cpu_index;
Quoted 8 lines

#else

target-i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 2791

Quoted 2 lines
target-i386/cpu.c +2 -0
+        error_setg(errp, "apic-id property was not initialized properly");
+        return;

#endif

would keep any x86-specific logic out of cpu_init().

My initial intention was to make a x86-specific cpu_init() to avoid #ifdefs in the code, but considering that it would be interesting to finally start using cpu_generic_init(), it would make sense to have the #ifdef on realize.

Anyway, the good news is that we probably don't even need to set apic_id = cpu_index on *-user, making the #ifdef unnecessary. See my replies to Peter at:
[PULL 08/11] linux-user: Check for cpu_init() errors

Signature · 1 line
Eduardo
#5 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] target-i386: Move CPUX86State.cpuid_apic_id to X86CPU.apic_id

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

Am 26.02.2015 um 22:42 schrieb Eduardo Habkost:

Quoted 5 lines

The field doesn't need to be inside CPUState, and it is not specific for the CPUID instruction, so move and rename it.

Really great,

This has a funny side-effect visible to emulated code: the memcpy() at cpu_copy() will not copy the APIC ID anymore and the APIC ID will be different every time a new thread is created by *-user.

That's good news: it means we can already initialize apic_id to 0 on *-user and existing behavior will be kept. But I recommend applying a patch to do that (I will include one in v4 of this series) before this one.

Signature · 1 line
Eduardo