Skip to contentSkip to conversation

target-i386: re-factor CPU creation/initialization to QOM

PATCH Superseded Igor Mammedov 4 participants 18 messages May 23, 2012 – May 24, 2012

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover target-i386: re-factor CPU creation/initialization to QOM

Moving code related to CPU creation and initialization internal parts from board level into apic and cpu objects will allow X86CPU to better model QOM object life-cycle.
It will allow to create X86CPU as any other object by creating it with object_new() then setting properties and then calling x86_cpu_realize() to make it running. Later x86_cpu_realize() should become realize property.

1 pc: Enable MSI support at APIC level
http://thread.gmane.org/gmane.comp.emulators.kvm.devel/91186
included here to prevent confilict since qom-next doesn't have it but patches 4, 5 are depending on it. I guess it would be best way to avoid confilicts, correct me if I'm wrong.

 2  target-i386: move cpu halted decision into x86_cpu_reset
 3  target-i386: add cpu-model property to x86_cpu
 4  pc: move apic_mapped initialization into common apic init code
 5  target-i386: make initialize CPU in QOM way
 6  target-i386: move reset callback to cpu.c

git tree for testing:
https://github.com/imammedo/qemu/tree/x86-cpu-realize-v2

Compile & Run tested:
target-i386: tcg and kvm mode
i386-linux-user: running of /bin/ls and /usr/bin/make on qemu tree

#1 lore raw

PATCH v2 1/6 pc: Enable MSI support at APIC level

From: Jan Kiszka <[email protected]>

Push msi_supported enabling to the APIC implementations where we can encapsulate the decision more cleanly, hiding the details from the generic code.

---
 hw/apic.c     |    3 +++
 hw/pc.c       |    9 ---------
 hw/xen.h      |   10 ----------
 hw/xen_apic.c |    5 +++++
 4 files changed, 8 insertions(+), 19 deletions(-)
hw/apic.c +3 -0
index 4eeaf88..5fbf01c 100644
@@ -19,6 +19,7 @@
 #include "apic_internal.h"
 #include "apic.h"
 #include "ioapic.h"
+#include "msi.h"
 #include "host-utils.h"
 #include "trace.h"
 #include "pc.h"
@@ -862,6 +863,8 @@ static void apic_init(APICCommonState *s)
 
     s->timer = qemu_new_timer_ns(vm_clock, apic_timer, s);
     local_apics[s->idx] = s;
+
+    msi_supported = true;
 }
 
 static void apic_class_init(ObjectClass *klass, void *data)
hw/pc.c +0 -9
index 4167782..ad27f36 100644
@@ -911,15 +911,6 @@ static DeviceState *apic_init(void *env, uint8_t apic_id)
         apic_mapped = 1;
     }
 
-    /* KVM does not support MSI yet. */
-    if (!kvm_irqchip_in_kernel()) {
-        msi_supported = true;
-    }
-
-    if (xen_msi_support()) {
-        msi_supported = true;
-    }
-
     return dev;
 }
 
hw/xen.h +0 -10
index 3ae4cd0..e5926b7 100644
@@ -57,14 +57,4 @@ void xen_register_framebuffer(struct MemoryRegion *mr);
 #  define HVM_MAX_VCPUS 32
 #endif
 
-static inline int xen_msi_support(void)
-{
-#if defined(CONFIG_XEN_CTRL_INTERFACE_VERSION) \
-    && CONFIG_XEN_CTRL_INTERFACE_VERSION >= 420
-    return xen_enabled();
-#else
-    return 0;
-#endif
-}
-
 #endif /* QEMU_HW_XEN_H */
hw/xen_apic.c +5 -0
index 1725ff6..a9e101f 100644
@@ -40,6 +40,11 @@ static void xen_apic_init(APICCommonState *s)
 {
     memory_region_init_io(&s->io_memory, &xen_apic_io_ops, s, "xen-apic-msi",
                           MSI_SPACE_SIZE);
+
+#if defined(CONFIG_XEN_CTRL_INTERFACE_VERSION) \
+    && CONFIG_XEN_CTRL_INTERFACE_VERSION >= 420
+    msi_supported = true;
+#endif
 }
 
 static void xen_apic_set_base(APICCommonState *s, uint64_t val)
Signature · 1 line
1.7.7.6
#2 lore raw

PATCH v2 2/6 target-i386: move cpu halted decision into x86_cpu_reset

In reply to Igor Mammedov

From: Igor Mammedov <[email protected]>

MP initialization protocol differs between cpu families, and for P6 and onward models it is up to CPU to decide if it will be BSP using this protocol, so try to model this. However there is no point in implementing MP initialization protocol in qemu. Thus first CPU is always marked as BSP.

This patch:
- moves decision to designate BSP from board into cpu, making cpu
self-sufficient in this regard. Later it will allow to cleanup hw/pc.c and remove cpu_reset and wrappers from there.
- stores flag that CPU is BSP in IA32_APIC_BASE to model behavior
described in Inted SDM vol 3a part 1 chapter 8.4.1
- uses MSR_IA32_APICBASE_BSP flag in apic_base for checking if cpu is BSP

patch is based on Jan Kiszka's proposal:

    http://thread.gmane.org/gmane.comp.emulators.qemu/100806

v2:
- fix build for i386-linux-user

---
 hw/apic.h            |    2 +-
 hw/apic_common.c     |   18 ++++++++++++------
 hw/pc.c              |    9 ---------
 target-i386/cpu.c    |    9 +++++++++
 target-i386/helper.c |    1 -
 target-i386/kvm.c    |    5 +++--
 6 files changed, 25 insertions(+), 19 deletions(-)
hw/apic.h +1 -1
index 62179ce..d961ed4 100644
@@ -20,9 +20,9 @@ void apic_init_reset(DeviceState *s);
 void apic_sipi(DeviceState *s);
 void apic_handle_tpr_access_report(DeviceState *d, target_ulong ip,
                                    TPRAccess access);
+void apic_designate_bsp(DeviceState *d);
 
 /* pc.c */
-int cpu_is_bsp(CPUX86State *env);
 DeviceState *cpu_get_current_apic(void);
 
 #endif
hw/apic_common.c +12 -6
index 60b8259..23d51e8 100644
@@ -43,8 +43,8 @@ uint64_t cpu_get_apic_base(DeviceState *d)
         trace_cpu_get_apic_base((uint64_t)s->apicbase);
         return s->apicbase;
     } else {
-        trace_cpu_get_apic_base(0);
-        return 0;
+        trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP);
+        return MSR_IA32_APICBASE_BSP;
     }
 }
 
@@ -201,22 +201,28 @@ void apic_init_reset(DeviceState *d)
     s->timer_expiry = -1;
 }
 
+void apic_designate_bsp(DeviceState *d)
+{
+    if (d) {
+        APICCommonState *s = APIC_COMMON(d);
+        s->apicbase |= MSR_IA32_APICBASE_BSP;
+    }
+}
+
 static void apic_reset_common(DeviceState *d)
 {
     APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
     APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
-    bool bsp;
 
-    bsp = cpu_is_bsp(s->cpu_env);
     s->apicbase = 0xfee00000 |
-        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
+        (s->apicbase & MSR_IA32_APICBASE_BSP) | MSR_IA32_APICBASE_ENABLE;
 
     s->vapic_paddr = 0;
     info->vapic_base_update(s);
 
     apic_init_reset(d);
 
-    if (bsp) {
+    if (s->apicbase & MSR_IA32_APICBASE_BSP) {
         /*
          * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
          * time typically by BIOS, so PIC interrupt can be delivered to the
hw/pc.c +0 -9
index ad27f36..c609770 100644
@@ -870,12 +870,6 @@ void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
     nb_ne2k++;
 }
 
-int cpu_is_bsp(CPUX86State *env)
-{
-    /* We hard-wire the BSP to the first CPU. */
-    return env->cpu_index == 0;
-}
-
 DeviceState *cpu_get_current_apic(void)
 {
     if (cpu_single_env) {
@@ -926,10 +920,7 @@ void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
 static void pc_cpu_reset(void *opaque)
 {
     X86CPU *cpu = opaque;
-    CPUX86State *env = &cpu->env;
-
     cpu_reset(CPU(cpu));
-    env->halted = !cpu_is_bsp(env);
 }
 
 static X86CPU *pc_new_cpu(const char *cpu_model)
target-i386/cpu.c +9 -0
index 89b4ac7..14c0f64 100644
@@ -1704,6 +1704,15 @@ static void x86_cpu_reset(CPUState *s)
     env->dr[7] = DR7_FIXED_1;
     cpu_breakpoint_remove_all(env, BP_CPU);
     cpu_watchpoint_remove_all(env, BP_CPU);
+
+#if !defined(CONFIG_USER_ONLY)
+    /* We hard-wire the BSP to the first CPU. */
+    if (env->cpu_index == 0) {
+        apic_designate_bsp(env->apic_state);
+    }
+
+    env->halted = !(cpu_get_apic_base(env->apic_state) & MSR_IA32_APICBASE_BSP);
+#endif
 }
 
 static void mce_init(X86CPU *cpu)
target-i386/helper.c +0 -1
index 2cc8097..3ceefad 100644
@@ -1191,7 +1191,6 @@ void do_cpu_init(X86CPU *cpu)
     env->interrupt_request = sipi;
     env->pat = pat;
     apic_init_reset(env->apic_state);
-    env->halted = !cpu_is_bsp(env);
 }
 
 void do_cpu_sipi(X86CPU *cpu)
target-i386/kvm.c +3 -2
index 0d0d8f6..09621e5 100644
@@ -583,8 +583,9 @@ void kvm_arch_reset_vcpu(CPUX86State *env)
     env->interrupt_injected = -1;
     env->xcr0 = 1;
     if (kvm_irqchip_in_kernel()) {
-        env->mp_state = cpu_is_bsp(env) ? KVM_MP_STATE_RUNNABLE :
-                                          KVM_MP_STATE_UNINITIALIZED;
+        env->mp_state =
+            cpu_get_apic_base(env->apic_state) & MSR_IA32_APICBASE_BSP ?
+            KVM_MP_STATE_RUNNABLE : KVM_MP_STATE_UNINITIALIZED;
     } else {
         env->mp_state = KVM_MP_STATE_RUNNABLE;
     }
Signature · 1 line
1.7.7.6
#3 lore raw

PATCH v2 3/6 target-i386: add cpu-model property to x86_cpu

In reply to Igor Mammedov

it's probably intermidiate step till cpu modeled as sub-classes. After then we probably could drop it.

However it still could be used for overiding default cpu subclasses definition, and probably renamed to something like 'features'.

v2:
- remove accidential tcg_* init code move

---
 cpu-defs.h           |    2 +-
 hw/pc.c              |   10 ----------
 target-i386/cpu.c    |   24 ++++++++++++++++++++++++
 target-i386/helper.c |   16 ++++++++++++----
 4 files changed, 37 insertions(+), 15 deletions(-)
cpu-defs.h +1 -1
index f49e950..8f4623c 100644
@@ -221,7 +221,7 @@ typedef struct CPUWatchpoint {
     struct QemuCond *halt_cond;                                         \
     int thread_kicked;                                                  \
     struct qemu_work_item *queued_work_first, *queued_work_last;        \
-    const char *cpu_model_str;                                          \
+    char *cpu_model_str;                                                \
     struct KVMState *kvm_state;                                         \
     struct kvm_run *kvm_run;                                            \
     int kvm_fd;                                                         \
hw/pc.c +0 -10
index c609770..1aa90a2 100644
@@ -930,7 +930,6 @@ static X86CPU *pc_new_cpu(const char *cpu_model)
 
     cpu = cpu_x86_init(cpu_model);
     if (cpu == NULL) {
-        fprintf(stderr, "Unable to find x86 CPU definition\n");
         exit(1);
     }
     env = &cpu->env;
@@ -946,15 +945,6 @@ void pc_cpus_init(const char *cpu_model)
 {
     int i;
 
-    /* init CPUs */
-    if (cpu_model == NULL) {
-#ifdef TARGET_X86_64
-        cpu_model = "qemu64";
-#else
-        cpu_model = "qemu32";
-#endif
-    }
-
     for(i = 0; i < smp_cpus; i++) {
         pc_new_cpu(cpu_model);
     }
target-i386/cpu.c +24 -0
index 14c0f64..e655129 100644
@@ -1731,6 +1731,27 @@ static void mce_init(X86CPU *cpu)
     }
 }
 
+static char *x86_get_cpu_model(Object *obj, Error **errp)
+{
+    X86CPU *cpu = X86_CPU(obj);
+    CPUX86State *env = &cpu->env;
+    return g_strdup(env->cpu_model_str);
+}
+
+static void x86_set_cpu_model(Object *obj, const char *value, Error **errp)
+{
+    X86CPU *cpu = X86_CPU(obj);
+    CPUX86State *env = &cpu->env;
+
+    g_free((gpointer)env->cpu_model_str);
+    env->cpu_model_str = g_strdup(value);
+
+    if (cpu_x86_register(cpu, env->cpu_model_str) < 0) {
+        fprintf(stderr, "Unable to find x86 CPU definition\n");
+        error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
+    }
+}
+
 void x86_cpu_realize(Object *obj, Error **errp)
 {
     X86CPU *cpu = X86_CPU(obj);
@@ -1771,6 +1792,9 @@ static void x86_cpu_initfn(Object *obj)
                         x86_cpuid_get_tsc_freq,
                         x86_cpuid_set_tsc_freq, NULL, NULL, NULL);
 
+    object_property_add_str(obj, "cpu-model",
+        x86_get_cpu_model, x86_set_cpu_model, NULL);
+
     env->cpuid_apic_id = env->cpu_index;
 }
 
target-i386/helper.c +12 -4
index 3ceefad..fbaeeea 100644
@@ -1154,12 +1154,10 @@ int cpu_x86_get_descr_debug(CPUX86State *env, unsigned int selector,
 X86CPU *cpu_x86_init(const char *cpu_model)
 {
     X86CPU *cpu;
-    CPUX86State *env;
+    Error *errp = NULL;
     static int inited;
 
     cpu = X86_CPU(object_new(TYPE_X86_CPU));
-    env = &cpu->env;
-    env->cpu_model_str = cpu_model;
 
     /* init various static tables used in TCG mode */
     if (tcg_enabled() && !inited) {
@@ -1170,7 +1168,17 @@ X86CPU *cpu_x86_init(const char *cpu_model)
             cpu_set_debug_excp_handler(breakpoint_handler);
 #endif
     }
-    if (cpu_x86_register(cpu, cpu_model) < 0) {
+
+    if (cpu_model) {
+        object_property_set_str(OBJECT(cpu), cpu_model, "cpu-model", &errp);
+    } else {
+#ifdef TARGET_X86_64
+        object_property_set_str(OBJECT(cpu), "qemu64", "cpu-model", &errp);
+#else
+        object_property_set_str(OBJECT(cpu), "qemu32", "cpu-model", &errp);
+#endif
+    }
+    if (errp) {
         object_delete(OBJECT(cpu));
         return NULL;
     }
Signature · 1 line
1.7.7.6
#4 lore raw

PATCH v2 4/6 pc: move apic_mapped initialization into common apic init code

In reply to Igor Mammedov

Move from apic_init in pc.c, the code that belongs to apic_init_common.

---
 hw/apic_common.c |   11 +++++++++++
 hw/msi.h         |    2 ++
 hw/pc.c          |   12 ------------
 3 files changed, 13 insertions(+), 12 deletions(-)
hw/apic_common.c +11 -0
index 23d51e8..c3fa66b 100644
@@ -21,6 +21,7 @@
 #include "apic_internal.h"
 #include "trace.h"
 #include "kvm.h"
+#include "msi.h"
 
 static int apic_irq_delivered;
 bool apic_report_tpr_access;
@@ -284,6 +285,7 @@ static int apic_init_common(SysBusDevice *dev)
     APICCommonClass *info;
     static DeviceState *vapic;
     static int apic_no;
+    static int apic_mapped;
 
     if (apic_no >= MAX_APICS) {
         return -1;
@@ -295,6 +297,15 @@ static int apic_init_common(SysBusDevice *dev)
 
     sysbus_init_mmio(dev, &s->io_memory);
 
+    /* XXX: mapping more APICs at the same memory location */
+    if (apic_mapped == 0) {
+        /* NOTE: the APIC is directly connected to the CPU - it is not
+           on the global memory bus. */
+        /* XXX: what if the base changes? */
+        sysbus_mmio_map(sysbus_from_qdev(&s->busdev.qdev), 0, MSI_ADDR_BASE);
+        apic_mapped = 1;
+    }
+
     if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK) {
         vapic = sysbus_create_simple("kvmvapic", -1, NULL);
     }
hw/msi.h +2 -0
index 3040bb0..abd52b6 100644
@@ -40,4 +40,6 @@ static inline bool msi_present(const PCIDevice *dev)
     return dev->cap_present & QEMU_PCI_CAP_MSI;
 }
 
+#define MSI_ADDR_BASE 0xfee00000
+
 #endif /* QEMU_MSI_H */
hw/pc.c +0 -12
index 1aa90a2..1ccfc6e 100644
@@ -70,8 +70,6 @@
 #define FW_CFG_E820_TABLE (FW_CFG_ARCH_LOCAL + 3)
 #define FW_CFG_HPET (FW_CFG_ARCH_LOCAL + 4)
 
-#define MSI_ADDR_BASE 0xfee00000
-
 #define E820_NR_ENTRIES		16
 
 struct e820_entry {
@@ -882,7 +880,6 @@ DeviceState *cpu_get_current_apic(void)
 static DeviceState *apic_init(void *env, uint8_t apic_id)
 {
     DeviceState *dev;
-    static int apic_mapped;
 
     if (kvm_irqchip_in_kernel()) {
         dev = qdev_create(NULL, "kvm-apic");
@@ -896,15 +893,6 @@ static DeviceState *apic_init(void *env, uint8_t apic_id)
     qdev_prop_set_ptr(dev, "cpu_env", env);
     qdev_init_nofail(dev);
 
-    /* XXX: mapping more APICs at the same memory location */
-    if (apic_mapped == 0) {
-        /* NOTE: the APIC is directly connected to the CPU - it is not
-           on the global memory bus. */
-        /* XXX: what if the base changes? */
-        sysbus_mmio_map(sysbus_from_qdev(dev), 0, MSI_ADDR_BASE);
-        apic_mapped = 1;
-    }
-
     return dev;
 }
 
Signature · 1 line
1.7.7.6
#5 lore raw

Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

Quoted 6 lines · Igor Mammedov
hw/apic_common.c +2 -0
@@ -295,6 +297,15 @@ static int apic_init_common(SysBusDevice *dev)
 
     sysbus_init_mmio(dev, &s->io_memory);
 
+    /* XXX: mapping more APICs at the same memory location */
+    if (apic_mapped == 0) {

hw/apic_common.c static int apic_init_common(SysBusDevice *dev) · line 311

Quoted 10 lines
hw/apic_common.c +7 -0
+        /* NOTE: the APIC is directly connected to the CPU - it is not
+           on the global memory bus. */
+        /* XXX: what if the base changes? */
+        sysbus_mmio_map(sysbus_from_qdev(&s->busdev.qdev), 0, MSI_ADDR_BASE);
+        apic_mapped = 1;
+    }
+
     if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK) {
         vapic = sysbus_create_simple("kvmvapic", -1, NULL);
     }

This looks wrong -- sysbus device init functions shouldn't be mapping MMIO regions themselves, in general. They should expose MMIO regions to be mapped by whichever device or board model creates them. Which is what the code before this patch was doing -- why do you want to move this code?

-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

Quoted 15 lines · Peter Maydell
Quoted 14 lines · Igor Mammedov
hw/apic_common.c +9 -0
@@ -295,6 +297,15 @@ static int apic_init_common(SysBusDevice *dev)
 
    sysbus_init_mmio(dev, &s->io_memory);
 
+    /* XXX: mapping more APICs at the same memory location */
+    if (apic_mapped == 0) {
+        /* NOTE: the APIC is directly connected to the CPU - it is not
+           on the global memory bus. */
+        /* XXX: what if the base changes? */
+        sysbus_mmio_map(sysbus_from_qdev(&s->busdev.qdev), 0, MSI_ADDR_BASE);
+        apic_mapped = 1;
+    }
+
    if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK) {
Quoted 8 lines

hw/apic_common.c static int apic_init_common(SysBusDevice *dev) · line 311

Quoted 2 lines
hw/apic_common.c +0 -0
        vapic = sysbus_create_simple("kvmvapic", -1, NULL);
    }

This looks wrong -- sysbus device init functions shouldn't be mapping MMIO regions themselves, in general. They should expose MMIO regions to be mapped by whichever device or board model creates them. Which is what the code before this patch was doing -- why do you want to move this code?

I fact, the CPU normally decides about where the APIC is mapped, specifically when it is remapped via the MSR (which QEMU cannot support yet). Well, unless we are talking about an external APIC and a 486 CPU. Then the board decides. But I guess we could live with ignoring that corner case and stick the mapping setup into the CPU initialization code.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#7 lore raw

Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

In reply to Peter Maydell

----- Original Message -----

Quoted 26 lines

From: "Peter Maydell" <[email protected]>
To: "Igor Mammedov" <[email protected]>
Cc: [email protected], "wei liu2" <[email protected]>, [email protected], "stefano stabellini" <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], "jan kiszka" <[email protected]>, "anthony perard" <[email protected]>, [email protected], [email protected]
Sent: Wednesday, May 23, 2012 6:44:30 PM
Subject: Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

Quoted 16 lines · Igor Mammedov
hw/apic_common.c +3 -0
@@ -295,6 +297,15 @@ static int apic_init_common(SysBusDevice *dev)
 
     sysbus_init_mmio(dev, &s->io_memory);
 
+    /* XXX: mapping more APICs at the same memory location */
+    if (apic_mapped == 0) {
+        /* NOTE: the APIC is directly connected to the CPU - it is

not
+           on the global memory bus. */
+        /* XXX: what if the base changes? */
+        sysbus_mmio_map(sysbus_from_qdev(&s->busdev.qdev), 0, MSI_ADDR_BASE);
+        apic_mapped = 1;
+    }
+
    if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK) {

Quoted 8 lines
Quoted 2 lines

        vapic = sysbus_create_simple("kvmvapic", -1, NULL); }

This looks wrong -- sysbus device init functions shouldn't be mapping MMIO regions themselves, in general. They should expose MMIO regions to be mapped by whichever device or board model creates them. Which is what the code before this patch was doing -- why do you want to move this code?

Why:
For cpu-hotplug it was suggested to use device_add/del interface for it. To do so in a generalized way hot-plugged cpu should follow general QOM object creation sequence, i.e.
- create new cpu instance
- set properties
- realize instance
without creating precedent of special case for cpus in device_add/del if possible. So goal is to have a self-sufficient cpu object that doesn't require external hooks to create/initialize it. It looks possible do so for target-i386 at least.

Some thoughts why mapping should be inside of apic object initfn:

LAPIC is a part of cpu so it's created and initialized by cpu. (not true for 486 but for later cpus should be)
I've looked in Intel SDM and chapter "10.4.4 Local APIC Status and Location" says:

"APIC Base field, bits 12 through 35 ⎯ Specifies the base address of the APIC registers. ---cut--- Following a power-up or reset, the field is set to FEE0 0000H."

it suggests that apic base is mapped right after cpu power-on and power-on could be roughly mapped to object_new(),set_prop,realize sequence for cpu. So when cpu creates child object "apic", then mapping of default apic base inside apic/cpu objects looks more appropriate. (depending on cpu model, but we could chose a more convenient case to implement).

Thanks,
Igor

#8 lore raw

Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

Quoted 10 lines · Igor Mammedov

For cpu-hotplug it was suggested to use device_add/del interface for it. To do so in a generalized way hot-plugged cpu should follow general QOM object creation sequence, i.e.
 - create new cpu instance
 - set properties
 - realize instance
without creating precedent of special case for cpus in device_add/del if possible. So goal is to have a self-sufficient cpu object that doesn't require external hooks to create/initialize it. It looks possible do so for target-i386 at least.

I think your self-sufficient CPU object should probably be a container QOM object which contains the CPU core itself and the APIC device. Then the container object's initialisation can map the APIC device.

-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

Quoted 12 lines · Peter Maydell
Quoted 10 lines · Igor Mammedov

For cpu-hotplug it was suggested to use device_add/del interface for it. To do so in a generalized way hot-plugged cpu should follow general QOM object creation sequence, i.e.
- create new cpu instance
- set properties
- realize instance
without creating precedent of special case for cpus in device_add/del if possible. So goal is to have a self-sufficient cpu object that doesn't require external hooks to create/initialize it. It looks possible do so for target-i386 at least.

Quoted 4 lines

I think your self-sufficient CPU object should probably be a container QOM object which contains the CPU core itself and the APIC device. Then the container object's initialisation can map the APIC device.

For x86 it would be artificial thing without a real hardware to model after, that would needlessly complicate code and interface. I'd rather avoid this.

Meanwhile I've found only two devices that do mapping inside device's initfn :(

hw/lm32_sys.c
hw/bonito.c

and lm32_sys has comment:
/* Note: This device is not created in the board initialization,
* instead it has to be added with the -device parameter. Therefore,
* the device maps itself. */

So for some devices it's possible to do so.
And since APIC after power-on/reset should have it's regs mapped at initial APIC base, APIC's initfn looks like a legitimate place to do it.

However if you are strongly opposed to this idea, I'll try Jan's idea to put mapping into CPU code that creates APIC instance. It would be uglier than if mapping were in APIC itself, but at least it could be justified by fact that Integrated APIC is a part of cpu and manuals don't specify which of them is responsible for setting mapping (mapping just exists after cpu power-on/reset).

Signature · 3 lines
-----
Thanks,
  Igor
#10 lore raw

Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

Quoted 8 lines · Igor Mammedov
Quoted 4 lines · Peter Maydell

I think your self-sufficient CPU object should probably be a container QOM object which contains the CPU core itself and the APIC device. Then the container object's initialisation can map the APIC device.

For x86 it would be artificial thing without a real hardware to model after, that would needlessly complicate code and interface.

I think the real hardware looks exactly like a container which has both the cpu-core proper and the APIC on it...

-- PMM

#11 lore raw

Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

In reply to Igor Mammedov
Quoted 13 lines · Igor Mammedov
Quoted 12 lines · Peter Maydell
Quoted 10 lines · Igor Mammedov

For cpu-hotplug it was suggested to use device_add/del interface for it. To do so in a generalized way hot-plugged cpu should follow general QOM object creation sequence, i.e.
- create new cpu instance
- set properties
- realize instance
without creating precedent of special case for cpus in device_add/del if possible. So goal is to have a self-sufficient cpu object that doesn't require external hooks to create/initialize it. It looks possible do so for target-i386 at least.

Quoted 8 lines
Quoted 4 lines

I think your self-sufficient CPU object should probably be a container QOM object which contains the CPU core itself and the APIC device. Then the container object's initialisation can map the APIC device.

For x86 it would be artificial thing without a real hardware to model after, that would needlessly complicate code and interface. I'd rather avoid this.

No, letting the CPU map the APIC is really the proper way to deal with it, specifically once we will support remapping.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#12 lore raw

Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

Quoted 19 lines · Jan Kiszka
Quoted 18 lines · Igor Mammedov
Quoted 16 lines · Peter Maydell
Quoted 10 lines · Igor Mammedov

For cpu-hotplug it was suggested to use device_add/del interface for it. To do so in a generalized way hot-plugged cpu should follow general QOM object creation sequence, i.e.
- create new cpu instance
- set properties
- realize instance
without creating precedent of special case for cpus in device_add/del if possible. So goal is to have a self-sufficient cpu object that doesn't require external hooks to create/initialize it. It looks possible do so for target-i386 at least.

I think your self-sufficient CPU object should probably be a container QOM object which contains the CPU core itself and the APIC device. Then the container object's initialisation can map the APIC device.

Quoted 6 lines
Quoted 3 lines

For x86 it would be artificial thing without a real hardware to model after, that would needlessly complicate code and interface. I'd rather avoid this.

No, letting the CPU map the APIC is really the proper way to deal with it, specifically once we will support remapping.

Ok, I'll move mapping at cpu level.

Signature · 3 lines
-----
Thanks,
  Igor
#13 lore raw

Re: [Qemu-devel] [PATCH qom-next 4/6] pc: move apic_mapped initialization into common apic init code

In reply to Igor Mammedov

Am 24.05.2012 15:10, schrieb Igor Mammedov:

Quoted 28 lines · Peter Maydell
Quoted 12 lines · Igor Mammedov
Quoted 10 lines

For cpu-hotplug it was suggested to use device_add/del interface for it. To do so in a generalized way hot-plugged cpu should follow general QOM object creation sequence, i.e.
- create new cpu instance
- set properties
- realize instance
without creating precedent of special case for cpus in device_add/del if possible. So goal is to have a self-sufficient cpu object that doesn't require external hooks to create/initialize it. It looks possible do so for target-i386 at least.

Quoted 4 lines

I think your self-sufficient CPU object should probably be a container QOM object which contains the CPU core itself and the APIC device. Then the container object's initialisation can map the APIC device.

For x86 it would be artificial thing without a real hardware to model after, that would needlessly complicate code and interface. I'd rather avoid this.

Meanwhile I've found only two devices that do mapping inside device's initfn :(

hw/lm32_sys.c
hw/bonito.c

Quoted 6 lines

and lm32_sys has comment:
/* Note: This device is not created in the board initialization,
* instead it has to be added with the -device parameter. Therefore,
* the device maps itself. */

So for some devices it's possible to do so.

Simply put: Doing the mmio_map after qdev init is historical and no reason to keep doing so. It breaks the desired late-realize model.

I believe Anthony's controversial series on GitHub with the "boilerplate" accessors that Peter so disliked tried to fix this?

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
#14 lore raw

PATCH v2 5/6 target-i386: make initialize CPU in QOM way

In reply to Igor Mammedov

Make CPU creation/initialization consistent with QOM object behavior in this, by moving tcg and apic initialization from board level into CPU's initfn/realize calls and cpu_model property setter.

Which makes CPU object self-sufficient in respect of creation/initialization and matches a typical object creation sequence, i.e.:
- create CPU instance
- set properties
- realize object - (x86_cpu_realize will be converted into realize property setter, when it is implemented)

v2:
- fix moving of tcg_* initialization into cpu.c from helper.c

spotted-by <Jan Kiszka [email protected]>

- make it compile/work on i386-linux-user target

---
 hw/pc.c              |   45 ++++------------------------
 target-i386/cpu.c    |   81 ++++++++++++++++++++++++++++++++++++++++++++++++-
 target-i386/helper.c |   39 ------------------------
 3 files changed, 85 insertions(+), 80 deletions(-)
hw/pc.c +6 -39
index 1ccfc6e..d7845ea 100644
@@ -42,7 +42,6 @@
 #include "sysbus.h"
 #include "sysemu.h"
 #include "kvm.h"
-#include "xen.h"
 #include "blockdev.h"
 #include "ui/qemu-spice.h"
 #include "memory.h"
@@ -877,25 +876,6 @@ DeviceState *cpu_get_current_apic(void)
     }
 }
 
-static DeviceState *apic_init(void *env, uint8_t apic_id)
-{
-    DeviceState *dev;
-
-    if (kvm_irqchip_in_kernel()) {
-        dev = qdev_create(NULL, "kvm-apic");
-    } else if (xen_enabled()) {
-        dev = qdev_create(NULL, "xen-apic");
-    } else {
-        dev = qdev_create(NULL, "apic");
-    }
-
-    qdev_prop_set_uint8(dev, "id", apic_id);
-    qdev_prop_set_ptr(dev, "cpu_env", env);
-    qdev_init_nofail(dev);
-
-    return dev;
-}
-
 void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
 {
     CPUX86State *s = opaque;
@@ -911,30 +891,17 @@ static void pc_cpu_reset(void *opaque)
     cpu_reset(CPU(cpu));
 }
 
-static X86CPU *pc_new_cpu(const char *cpu_model)
-{
-    X86CPU *cpu;
-    CPUX86State *env;
-
-    cpu = cpu_x86_init(cpu_model);
-    if (cpu == NULL) {
-        exit(1);
-    }
-    env = &cpu->env;
-    if ((env->cpuid_features & CPUID_APIC) || smp_cpus > 1) {
-        env->apic_state = apic_init(env, env->cpuid_apic_id);
-    }
-    qemu_register_reset(pc_cpu_reset, cpu);
-    pc_cpu_reset(cpu);
-    return cpu;
-}
-
 void pc_cpus_init(const char *cpu_model)
 {
+    X86CPU *cpu;
     int i;
 
     for(i = 0; i < smp_cpus; i++) {
-        pc_new_cpu(cpu_model);
+        cpu = cpu_x86_init(cpu_model);
+        if (cpu == NULL) {
+            exit(1);
+        }
+        qemu_register_reset(pc_cpu_reset, cpu);
     }
 }
 
target-i386/cpu.c +79 -2

122 lines not on this page · raw message

target-i386/helper.c +0 -39
index fbaeeea..38ac25d 100644
@@ -941,34 +941,6 @@ int check_hw_breakpoints(CPUX86State *env, int force_dr6_update)
     return hit_enabled;
 }
 
-static CPUDebugExcpHandler *prev_debug_excp_handler;
-
-static void breakpoint_handler(CPUX86State *env)
-{
-    CPUBreakpoint *bp;
-
-    if (env->watchpoint_hit) {
-        if (env->watchpoint_hit->flags & BP_CPU) {
-            env->watchpoint_hit = NULL;
-            if (check_hw_breakpoints(env, 0))
-                raise_exception_env(EXCP01_DB, env);
-            else
-                cpu_resume_from_signal(env, NULL);
-        }
-    } else {
-        QTAILQ_FOREACH(bp, &env->breakpoints, entry)
-            if (bp->pc == env->eip) {
-                if (bp->flags & BP_CPU) {
-                    check_hw_breakpoints(env, 1);
-                    raise_exception_env(EXCP01_DB, env);
-                }
-                break;
-            }
-    }
-    if (prev_debug_excp_handler)
-        prev_debug_excp_handler(env);
-}
-
 typedef struct MCEInjectionParams {
     Monitor *mon;
     CPUX86State *env;
@@ -1155,20 +1127,9 @@ X86CPU *cpu_x86_init(const char *cpu_model)
 {
     X86CPU *cpu;
     Error *errp = NULL;
-    static int inited;
 
     cpu = X86_CPU(object_new(TYPE_X86_CPU));
 
-    /* init various static tables used in TCG mode */
-    if (tcg_enabled() && !inited) {
-        inited = 1;
-        optimize_flags_init();
-#ifndef CONFIG_USER_ONLY
-        prev_debug_excp_handler =
-            cpu_set_debug_excp_handler(breakpoint_handler);
-#endif
-    }
-
     if (cpu_model) {
         object_property_set_str(OBJECT(cpu), cpu_model, "cpu-model", &errp);
     } else {
Signature · 1 line
1.7.7.6
#15 lore raw

Re: [Qemu-devel] [PATCH qom-next 5/6] target-i386: make initialize CPU in QOM way

Am 23.05.2012 18:39, schrieb Igor Mammedov:

Quoted 46 lines

Make CPU creation/initialization consistent with QOM object behavior in this, by moving tcg and apic initialization from board level into CPU's initfn/realize calls and cpu_model property setter.

Which makes CPU object self-sufficient in respect of creation/initialization and matches a typical object creation sequence, i.e.:
- create CPU instance
- set properties
- realize object - (x86_cpu_realize will be converted into realize property setter, when it is implemented)

v2:
- fix moving of tcg_* initialization into cpu.c from helper.c

spotted-by <Jan Kiszka [email protected]>

- make it compile/work on i386-linux-user target

---
 hw/pc.c              |   45 ++++------------------------
 target-i386/cpu.c    |   81 ++++++++++++++++++++++++++++++++++++++++++++++++-
 target-i386/helper.c |   39 ------------------------
 3 files changed, 85 insertions(+), 80 deletions(-)
hw/pc.c +0 -14
index 1ccfc6e..d7845ea 100644
@@ -911,30 +891,17 @@ static void pc_cpu_reset(void *opaque)
     cpu_reset(CPU(cpu));
 }
 
-static X86CPU *pc_new_cpu(const char *cpu_model)
-{
-    X86CPU *cpu;
-    CPUX86State *env;
-
-    cpu = cpu_x86_init(cpu_model);
-    if (cpu == NULL) {
-        exit(1);
-    }
-    env = &cpu->env;
-    if ((env->cpuid_features & CPUID_APIC) || smp_cpus > 1) {
-        env->apic_state = apic_init(env, env->cpuid_apic_id);
-    }
-    qemu_register_reset(pc_cpu_reset, cpu);

hw/pc.c static void pc_cpu_reset(void *opaque) · line 899

Quoted 10 lines
hw/pc.c +1 -4
-    pc_cpu_reset(cpu);
-    return cpu;
-}
-
 void pc_cpus_init(const char *cpu_model)
 {
+    X86CPU *cpu;
     int i;
 
     for(i = 0; i < smp_cpus; i++) {

If we do changes here, please add the missing space. :)

Quoted 25 lines
hw/pc.c +5 -1
-        pc_new_cpu(cpu_model);
+        cpu = cpu_x86_init(cpu_model);
+        if (cpu == NULL) {
+            exit(1);
+        }
+        qemu_register_reset(pc_cpu_reset, cpu);
     }
 }
 
target-i386/cpu.c +7 -0
index e655129..99ef891 100644
@@ -1749,24 +1753,89 @@ static void x86_set_cpu_model(Object *obj, const char *value, Error **errp)
     if (cpu_x86_register(cpu, env->cpu_model_str) < 0) {
         fprintf(stderr, "Unable to find x86 CPU definition\n");
         error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
+        return;
+    }
+
+#ifndef CONFIG_USER_ONLY
+    if (((env->cpuid_features & CPUID_APIC) || smp_cpus > 1)) {
+        if (kvm_irqchip_in_kernel()) {
+            env->apic_state = qdev_create(NULL, "kvm-apic");

target-i386/cpu.c static void x86_set_cpu_model(Object *obj, const char *value, Error **errp) · line 1772

Quoted 10 lines
target-i386/cpu.c +10 -0
+        } else if (xen_enabled()) {
+            env->apic_state = qdev_create(NULL, "xen-apic");
+        } else {
+            env->apic_state = qdev_create(NULL, "apic");
+        }
+        object_property_add_child(OBJECT(cpu), "apic",
+            OBJECT(env->apic_state), NULL);
+
+        qdev_prop_set_uint8(env->apic_state, "id", env->cpuid_apic_id);
+        qdev_prop_set_ptr(env->apic_state, "cpu_env", env);

I'd like to avoid re-adding this set_ptr(). We can cherry-pick my link<X86CPU> property from QOM CPUState part 4 series.

Quoted 44 lines
target-i386/cpu.c +38 -0
+    }
+#endif
+}
+
+#ifndef CONFIG_USER_ONLY
+static CPUDebugExcpHandler *prev_debug_excp_handler;
+
+static void breakpoint_handler(CPUX86State *env)
+{
+    CPUBreakpoint *bp;
+
+    if (env->watchpoint_hit) {
+        if (env->watchpoint_hit->flags & BP_CPU) {
+            env->watchpoint_hit = NULL;
+            if (check_hw_breakpoints(env, 0)) {
+                raise_exception_env(EXCP01_DB, env);
+            } else {
+                cpu_resume_from_signal(env, NULL);
+            }
+        }
+    } else {
+        QTAILQ_FOREACH(bp, &env->breakpoints, entry)
+            if (bp->pc == env->eip) {
+                if (bp->flags & BP_CPU) {
+                    check_hw_breakpoints(env, 1);
+                    raise_exception_env(EXCP01_DB, env);
+                }
+                break;
+            }
+    }
+    if (prev_debug_excp_handler) {
+        prev_debug_excp_handler(env);
     }
 }
+#endif
 
 void x86_cpu_realize(Object *obj, Error **errp)
 {
     X86CPU *cpu = X86_CPU(obj);
+#ifndef CONFIG_USER_ONLY
+    CPUX86State *env = &cpu->env;
+
+    if (env->apic_state) {
+        if (qdev_init(env->apic_state) < 0) {

target-i386/cpu.c static void x86_set_cpu_model(Object *obj, const char *value, Error **errp) · line 1825

Quoted 10 lines
target-i386/cpu.c +7 -1
+            error_set(errp, QERR_DEVICE_INIT_FAILED,
+                object_get_typename(OBJECT(env->apic_state)));
+            return;
+        }
+    }
+#endif
 
     mce_init(cpu);
-    qemu_init_vcpu(&cpu->env);
+    qemu_init_vcpu(env);

This only works because currently qemu_init_vcpu() is a no-op macro that doesn't use the parameter. Please don't change it back, I guess it's a mismerge.

We can avoid the env variable if I finish merging Paolo's series - by realizing the CPU the APIC as its child would get realized, too. Is the ordering before mce_init() mandatory here or is it just to reduce the #ifndef'ery?

Quoted 20 lines
target-i386/cpu.c +4 -0
+    cpu_reset(CPU(cpu));
 }
 
 static void x86_cpu_initfn(Object *obj)
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
+    static int inited;
 
     cpu_exec_init(env);
 
+    env->cpuid_apic_id = env->cpu_index;
+
     object_property_add(obj, "family", "int",
                         x86_cpuid_version_get_family,
                         x86_cpuid_version_set_family, NULL, NULL, NULL);
@@ -1795,7 +1864,15 @@ static void x86_cpu_initfn(Object *obj)
     object_property_add_str(obj, "cpu-model",
         x86_get_cpu_model, x86_set_cpu_model, NULL);
 

target-i386/cpu.c static void x86_cpu_initfn(Object *obj) · line 1875

Quoted 10 lines
target-i386/cpu.c +9 -1
-    env->cpuid_apic_id = env->cpu_index;
+    /* init various static tables used in TCG mode */
+    if (tcg_enabled() && !inited) {
+        inited = 1;
+        optimize_flags_init();
+#ifndef CONFIG_USER_ONLY
+        prev_debug_excp_handler =
+            cpu_set_debug_excp_handler(breakpoint_handler);
+#endif
+    }

Did you forget to put that into its own patch or did that not work? My idea was to have it first in the series so that other changes here and elsewhere can be rebased onto it.

Also I wonder whether it would better be placed into the class_init? I'd tend towards initfn because that will not be invoked during type enumeration.

Quoted 29 lines
target-i386/cpu.c +0 -0
 }
 
 static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
target-i386/helper.c +0 -18
index fbaeeea..38ac25d 100644
@@ -941,34 +941,6 @@ int check_hw_breakpoints(CPUX86State *env, int force_dr6_update)
     return hit_enabled;
 }
 
-static CPUDebugExcpHandler *prev_debug_excp_handler;
-
-static void breakpoint_handler(CPUX86State *env)
-{
-    CPUBreakpoint *bp;
-
-    if (env->watchpoint_hit) {
-        if (env->watchpoint_hit->flags & BP_CPU) {
-            env->watchpoint_hit = NULL;
-            if (check_hw_breakpoints(env, 0))
-                raise_exception_env(EXCP01_DB, env);
-            else
-                cpu_resume_from_signal(env, NULL);
-        }
-    } else {
-        QTAILQ_FOREACH(bp, &env->breakpoints, entry)
-            if (bp->pc == env->eip) {
-                if (bp->flags & BP_CPU) {

target-i386/helper.c int check_hw_breakpoints(CPUX86State *env, int force_dr6_update) · line 971

Quoted 10 lines
target-i386/helper.c +0 -10
-                    check_hw_breakpoints(env, 1);
-                    raise_exception_env(EXCP01_DB, env);
-                }
-                break;
-            }
-    }
-    if (prev_debug_excp_handler)
-        prev_debug_excp_handler(env);
-}
-

I wonder if that could rather stay here as non-static?

Quoted 14 lines
target-i386/helper.c +0 -4
 typedef struct MCEInjectionParams {
     Monitor *mon;
     CPUX86State *env;
@@ -1155,20 +1127,9 @@ X86CPU *cpu_x86_init(const char *cpu_model)
 {
     X86CPU *cpu;
     Error *errp = NULL;
-    static int inited;
 
     cpu = X86_CPU(object_new(TYPE_X86_CPU));
 
-    /* init various static tables used in TCG mode */
-    if (tcg_enabled() && !inited) {
-        inited = 1;

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

Quoted 10 lines
target-i386/helper.c +0 -7
-        optimize_flags_init();
-#ifndef CONFIG_USER_ONLY
-        prev_debug_excp_handler =
-            cpu_set_debug_excp_handler(breakpoint_handler);
-#endif
-    }
-
     if (cpu_model) {
         object_property_set_str(OBJECT(cpu), cpu_model, "cpu-model", &errp);
     } else {

/-F

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
#16 lore raw

Re: [Qemu-devel] [PATCH qom-next 5/6] target-i386: make initialize CPU in QOM way

Quoted 56 lines · Andreas Färber

Am 23.05.2012 18:39, schrieb Igor Mammedov:

Quoted 55 lines

Make CPU creation/initialization consistent with QOM object behavior in this, by moving tcg and apic initialization from board level into CPU's initfn/realize calls and cpu_model property setter.

Which makes CPU object self-sufficient in respect of creation/initialization and matches a typical object creation sequence, i.e.:
- create CPU instance
- set properties
- realize object - (x86_cpu_realize will be converted into realize property setter, when it is implemented)

v2:
- fix moving of tcg_* initialization into cpu.c from helper.c

spotted-by <Jan Kiszka [email protected]>

- make it compile/work on i386-linux-user target

---
  hw/pc.c              |   45 ++++------------------------
  target-i386/cpu.c    |   81 ++++++++++++++++++++++++++++++++++++++++++++++++-
  target-i386/helper.c |   39 ------------------------
  3 files changed, 85 insertions(+), 80 deletions(-)
hw/pc.c +1 -18
index 1ccfc6e..d7845ea 100644
@@ -911,30 +891,17 @@ static void pc_cpu_reset(void *opaque)
      cpu_reset(CPU(cpu));
  }
 
-static X86CPU *pc_new_cpu(const char *cpu_model)
-{
-    X86CPU *cpu;
-    CPUX86State *env;
-
-    cpu = cpu_x86_init(cpu_model);
-    if (cpu == NULL) {
-        exit(1);
-    }
-    env =&cpu->env;
-    if ((env->cpuid_features&  CPUID_APIC) || smp_cpus>  1) {
-        env->apic_state = apic_init(env, env->cpuid_apic_id);
-    }
-    qemu_register_reset(pc_cpu_reset, cpu);
-    pc_cpu_reset(cpu);
-    return cpu;
-}
-
  void pc_cpus_init(const char *cpu_model)
  {
+    X86CPU *cpu;
      int i;
 
Quoted 3 lines

hw/pc.c static void pc_cpu_reset(void *opaque) · line 899

Quoted 1 line
hw/pc.c +0 -0
      for(i = 0; i<  smp_cpus; i++) {

If we do changes here, please add the missing space. :)

I'll fix it.

Quoted 31 lines
Quoted 31 lines
hw/pc.c +5 -1
-        pc_new_cpu(cpu_model);
+        cpu = cpu_x86_init(cpu_model);
+        if (cpu == NULL) {
+            exit(1);
+        }
+        qemu_register_reset(pc_cpu_reset, cpu);
      }
  }
 
target-i386/cpu.c +12 -0
index e655129..99ef891 100644
@@ -1749,24 +1753,89 @@ static void x86_set_cpu_model(Object *obj, const char *value, Error **errp)
      if (cpu_x86_register(cpu, env->cpu_model_str)<  0) {
          fprintf(stderr, "Unable to find x86 CPU definition\n");
          error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
+        return;
+    }
+
+#ifndef CONFIG_USER_ONLY
+    if (((env->cpuid_features&  CPUID_APIC) || smp_cpus>  1)) {
+        if (kvm_irqchip_in_kernel()) {
+            env->apic_state = qdev_create(NULL, "kvm-apic");
+        } else if (xen_enabled()) {
+            env->apic_state = qdev_create(NULL, "xen-apic");
+        } else {
+            env->apic_state = qdev_create(NULL, "apic");
+        }
Quoted 8 lines

target-i386/cpu.c static void x86_set_cpu_model(Object *obj, const char *value, Error **errp) · line 1772

Quoted 5 lines
target-i386/cpu.c +5 -0
+        object_property_add_child(OBJECT(cpu), "apic",
+            OBJECT(env->apic_state), NULL);
+
+        qdev_prop_set_uint8(env->apic_state, "id", env->cpuid_apic_id);
+        qdev_prop_set_ptr(env->apic_state, "cpu_env", env);

I'd like to avoid re-adding this set_ptr(). We can cherry-pick my link<X86CPU> property from QOM CPUState part 4 series.

sure, I'll add you link<X86CPU> patches and rebase on top of it

Quoted 52 lines
Quoted 52 lines
target-i386/cpu.c +44 -0
+    }
+#endif
+}
+
+#ifndef CONFIG_USER_ONLY
+static CPUDebugExcpHandler *prev_debug_excp_handler;
+
+static void breakpoint_handler(CPUX86State *env)
+{
+    CPUBreakpoint *bp;
+
+    if (env->watchpoint_hit) {
+        if (env->watchpoint_hit->flags&  BP_CPU) {
+            env->watchpoint_hit = NULL;
+            if (check_hw_breakpoints(env, 0)) {
+                raise_exception_env(EXCP01_DB, env);
+            } else {
+                cpu_resume_from_signal(env, NULL);
+            }
+        }
+    } else {
+        QTAILQ_FOREACH(bp,&env->breakpoints, entry)
+            if (bp->pc == env->eip) {
+                if (bp->flags&  BP_CPU) {
+                    check_hw_breakpoints(env, 1);
+                    raise_exception_env(EXCP01_DB, env);
+                }
+                break;
+            }
+    }
+    if (prev_debug_excp_handler) {
+        prev_debug_excp_handler(env);
      }
  }
+#endif
 
  void x86_cpu_realize(Object *obj, Error **errp)
  {
      X86CPU *cpu = X86_CPU(obj);
+#ifndef CONFIG_USER_ONLY
+    CPUX86State *env =&cpu->env;
+
+    if (env->apic_state) {
+        if (qdev_init(env->apic_state)<  0) {
+            error_set(errp, QERR_DEVICE_INIT_FAILED,
+                object_get_typename(OBJECT(env->apic_state)));
+            return;
+        }
+    }
+#endif
 
Quoted 7 lines

target-i386/cpu.c static void x86_set_cpu_model(Object *obj, const char *value, Error **errp) · line 1825

Quoted 3 lines
target-i386/cpu.c +1 -1
      mce_init(cpu);
-    qemu_init_vcpu(&cpu->env);
+    qemu_init_vcpu(env);

This only works because currently qemu_init_vcpu() is a no-op macro that doesn't use the parameter. Please don't change it back, I guess it's a mismerge.

I'll fix it.

Quoted 5 lines

We can avoid the env variable if I finish merging Paolo's series - by realizing the CPU the APIC as its child would get realized, too. Is the ordering before mce_init() mandatory here or is it just to reduce the #ifndef'ery?

mce_init() ordering here is not important and it is less #ifndef-s this way. yep, with Paolo's realize patch whole #ifndef block should be thrown away. I can split it into a separate patch, that could be easily discarded when Paolo's realize is committed.

Quoted 35 lines
Quoted 20 lines
target-i386/cpu.c +4 -0
+    cpu_reset(CPU(cpu));
  }
 
  static void x86_cpu_initfn(Object *obj)
  {
      X86CPU *cpu = X86_CPU(obj);
      CPUX86State *env =&cpu->env;
+    static int inited;
 
      cpu_exec_init(env);
 
+    env->cpuid_apic_id = env->cpu_index;
+
      object_property_add(obj, "family", "int",
                          x86_cpuid_version_get_family,
                          x86_cpuid_version_set_family, NULL, NULL, NULL);
@@ -1795,7 +1864,15 @@ static void x86_cpu_initfn(Object *obj)
      object_property_add_str(obj, "cpu-model",
          x86_get_cpu_model, x86_set_cpu_model, NULL);
 

target-i386/cpu.c static void x86_cpu_initfn(Object *obj) · line 1875

Quoted 10 lines
target-i386/cpu.c +9 -1
-    env->cpuid_apic_id = env->cpu_index;
+    /* init various static tables used in TCG mode */
+    if (tcg_enabled()&&  !inited) {
+        inited = 1;
+        optimize_flags_init();
+#ifndef CONFIG_USER_ONLY
+        prev_debug_excp_handler =
+            cpu_set_debug_excp_handler(breakpoint_handler);
+#endif
+    }

Did you forget to put that into its own patch or did that not work? My idea was to have it first in the series so that other changes here and elsewhere can be rebased onto it.

Quoted 3 lines

Also I wonder whether it would better be placed into the class_init? I'd tend towards initfn because that will not be invoked during type enumeration.

Ok, I'll split it into separate patch.
On the second thought there is no compelling reason to move this hunk in cpu.c It could be left at board level, just moved in the beginning of pc_cpus_init() and called only once there.

Quoted 34 lines
Quoted 34 lines
target-i386/cpu.c +0 -0
  }
 
  static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
target-i386/helper.c +0 -22
index fbaeeea..38ac25d 100644
@@ -941,34 +941,6 @@ int check_hw_breakpoints(CPUX86State *env, int force_dr6_update)
      return hit_enabled;
  }
 
-static CPUDebugExcpHandler *prev_debug_excp_handler;
-
-static void breakpoint_handler(CPUX86State *env)
-{
-    CPUBreakpoint *bp;
-
-    if (env->watchpoint_hit) {
-        if (env->watchpoint_hit->flags&  BP_CPU) {
-            env->watchpoint_hit = NULL;
-            if (check_hw_breakpoints(env, 0))
-                raise_exception_env(EXCP01_DB, env);
-            else
-                cpu_resume_from_signal(env, NULL);
-        }
-    } else {
-        QTAILQ_FOREACH(bp,&env->breakpoints, entry)
-            if (bp->pc == env->eip) {
-                if (bp->flags&  BP_CPU) {
-                    check_hw_breakpoints(env, 1);
-                    raise_exception_env(EXCP01_DB, env);
-                }
-                break;
Quoted 8 lines

target-i386/helper.c int check_hw_breakpoints(CPUX86State *env, int force_dr6_update) · line 971

Quoted 6 lines
target-i386/helper.c +0 -6
-            }
-    }
-    if (prev_debug_excp_handler)
-        prev_debug_excp_handler(env);
-}
-

I wonder if that could rather stay here as non-static?

Any preference in what header file it should be?

Signature · 3 lines
-----
Thanks
  Igor
#17 lore raw

PATCH v2 6/6 target-i386: move reset callback to cpu.c

In reply to Igor Mammedov

Moving reset callback into cpu object from board level will allow properly create object during run-time (hotplug).

When reset over QOM hierarchy is implemented, this reset callback should be removed.

v2:
- fix build for i386-linux-target

---
 hw/pc.c           |    7 -------
 target-i386/cpu.c |   11 +++++++++++
 2 files changed, 11 insertions(+), 7 deletions(-)
hw/pc.c +0 -7
index d7845ea..868dbe7 100644
@@ -885,12 +885,6 @@ void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
     }
 }
 
-static void pc_cpu_reset(void *opaque)
-{
-    X86CPU *cpu = opaque;
-    cpu_reset(CPU(cpu));
-}
-
 void pc_cpus_init(const char *cpu_model)
 {
     X86CPU *cpu;
@@ -901,7 +895,6 @@ void pc_cpus_init(const char *cpu_model)
         if (cpu == NULL) {
             exit(1);
         }
-        qemu_register_reset(pc_cpu_reset, cpu);
     }
 }
 
target-i386/cpu.c +11 -0
index 99ef891..ad39f71 100644
@@ -1775,6 +1775,13 @@ static void x86_set_cpu_model(Object *obj, const char *value, Error **errp)
 }
 
 #ifndef CONFIG_USER_ONLY
+/* TODO: remove me, when reset over QOM tree is implemented */
+static void x86_cpu_machine_reset_cb(void *opaque)
+{
+    X86CPU *cpu = opaque;
+    cpu_reset(CPU(cpu));
+}
+
 static CPUDebugExcpHandler *prev_debug_excp_handler;
 
 static void breakpoint_handler(CPUX86State *env)
@@ -1823,6 +1830,10 @@ void x86_cpu_realize(Object *obj, Error **errp)
 
     mce_init(cpu);
     qemu_init_vcpu(env);
+
+#ifndef CONFIG_USER_ONLY
+    qemu_register_reset(x86_cpu_machine_reset_cb, cpu);
+#endif
     cpu_reset(CPU(cpu));
 }
 
Signature · 1 line
1.7.7.6