qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [RFC 00/25] QOMify accelerator code
@ 2014-07-09 22:03 Eduardo Habkost
  2014-07-09 22:03 ` [Qemu-devel] [RFC 01/25] vl.c: Small coding style fix Eduardo Habkost
                   ` (24 more replies)
  0 siblings, 25 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:03 UTC (permalink / raw)
  To: qemu-devel
  Cc: Michael Mueller, Marcel Apfelbaum, Alexander Graf,
	Christian Borntraeger, Jason J. Herne, Paolo Bonzini,
	Andreas Färber

Git tree:
  https://github.com/ehabkost/qemu-hacks/tree/accel-qom.v1

This is an attempt to convert the accel initialization and registration code
to be QOM-based. I see two use cases for this:

 * KVM-specific CPU compat bits;
 * Probing for accelerator-specific CPU model "runnable" information.

= KVM-specific compat bits =

Currently, the PC compat code that deals with KVM-specific CPUID compat bits
can't use compat_props to control compatibility, because it needs to affect the
X86CPU CPUID initialization behavior only when KVM is enabled. The solution I
propose is to have a "x86-kvm-accel" class which would be responsible for the
KVM-specific CPU initialization behavior. Any compatibility properties for that
behavior could then be tuned using "x86-kvm-accel" compat properties.

To be able to implement x86-specific accelerator methods, a TYPE_X86_ACCEL
interface was introduced.

Patches 19-24 implements this (except for the actual compat properties).

= Accel-specific runnable information =

There are plans to eventually make the "query-cpu-definitions" QMP command
return "runnable" information, which is accelerator-specific (e.g. some CPU
models may be runnable when using KVM, but not when using TCG). The existing
accelerator code maintains lots of global state, and the QOM-based approach will
allow us to eventually make query-cpu-definitions instantiate short-lived
accelerator objects (without affecting any machine and/or global state) to query
for accelerator-specific information.

Cc: Michael Mueller <mimu@linux.vnet.ibm.com>
Cc: Christian Borntraeger <borntraeger@de.ibm.com>
Cc: Alexander Graf <agraf@suse.de>
Cc: "Jason J. Herne" <jjherne@linux.vnet.ibm.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Andreas Färber <afaerber@suse.de>
Cc: Marcel Apfelbaum <marcel.a@redhat.com>

Eduardo Habkost (25):
  vl.c: Small coding style fix
  accel: Move accel code to accel.c
  accel: Create struct AccelType
  accel: Simplify configure_accelerator() using AccelType *acc variable
  accel: Move accel name lookup to separate function
  accel: Use QOM classes for accel types
  accel: Make AccelClass.available() optional
  accel: Move KVM accel registration to kvm-all.c
  accel: Move Xen registration code to xen-common.c
  accel: Move qtest accel registration to qtest.c
  accel: Remove tcg_available() function
  accel: Move accel init/allowed code to separate function
  accel: Rename 'init' method to 'init_machine'
  accel: Pass MachineState object to accel init functions
  accel: Create accel object when initializing machine
  accel: Save AccelState on MachineState when initializing
  kvm: Make KVMState be the TYPE_KVM_ACCEL instance struct
  accel: Get target name as argument when initializing accelerator
  accel: Use target-specific accel class if available
  accel: TYPE_X86_ACCEL interface
  kvm: x86 KVM accelerator subclass
  target-i386: Add AccelState parameter to cpu_x86_create()
  target-i386: Move accelerator-specific code outside
    X86CPU.instance_init
  target-i386: Accept "host" as value for CPU vendor
  target-i386: Move KVM CPUID hacking to accelerator cpu_post_init hook

 arch_init.c                |  11 +--
 hw/core/Makefile.objs      |   1 +
 hw/core/accel.c            | 171 +++++++++++++++++++++++++++++++++++++++++++++
 hw/i386/pc.c               |   3 +-
 include/hw/accel.h         |  60 ++++++++++++++++
 include/hw/boards.h        |   1 +
 include/hw/i386/accel.h    |  42 +++++++++++
 include/hw/xen/xen.h       |   1 -
 include/qemu/typedefs.h    |   5 ++
 include/sysemu/arch_init.h |   1 -
 include/sysemu/kvm.h       |   5 +-
 include/sysemu/qtest.h     |   1 -
 kvm-all.c                  |  40 +++++++++--
 kvm-stub.c                 |   5 --
 qtest.c                    |  27 ++++++-
 target-i386/Makefile.objs  |   1 +
 target-i386/accel.c        |  38 ++++++++++
 target-i386/cpu.c          | 101 +++++++++++---------------
 target-i386/cpu.h          |   2 +-
 target-i386/kvm.c          |  70 +++++++++++++++++++
 vl.c                       |  83 +---------------------
 xen-common-stub.c          |   6 --
 xen-common.c               |  25 ++++++-
 23 files changed, 528 insertions(+), 172 deletions(-)
 create mode 100644 hw/core/accel.c
 create mode 100644 include/hw/accel.h
 create mode 100644 include/hw/i386/accel.h
 create mode 100644 target-i386/accel.c

-- 
1.9.3

^ permalink raw reply	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 01/25] vl.c: Small coding style fix
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
@ 2014-07-09 22:03 ` Eduardo Habkost
  2014-07-09 22:03 ` [Qemu-devel] [RFC 02/25] accel: Move accel code to accel.c Eduardo Habkost
                   ` (23 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:03 UTC (permalink / raw)
  To: qemu-devel

Just to make checkpatch.pl happy when moving the code.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 vl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/vl.c b/vl.c
index 6e084c2..8da895f 100644
--- a/vl.c
+++ b/vl.c
@@ -2692,7 +2692,7 @@ static int configure_accelerator(MachineClass *mc)
         if (*p == ':') {
             p++;
         }
-        p = get_opt_name(buf, sizeof (buf), p, ':');
+        p = get_opt_name(buf, sizeof(buf), p, ':');
         for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
             if (strcmp(accel_list[i].opt_name, buf) == 0) {
                 if (!accel_list[i].available()) {
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 02/25] accel: Move accel code to accel.c
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
  2014-07-09 22:03 ` [Qemu-devel] [RFC 01/25] vl.c: Small coding style fix Eduardo Habkost
@ 2014-07-09 22:03 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 03/25] accel: Create struct AccelType Eduardo Habkost
                   ` (22 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:03 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/Makefile.objs |   1 +
 hw/core/accel.c       | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/accel.h    |  32 ++++++++++++++
 vl.c                  |  81 +-----------------------------------
 4 files changed, 147 insertions(+), 80 deletions(-)
 create mode 100644 hw/core/accel.c
 create mode 100644 include/hw/accel.h

diff --git a/hw/core/Makefile.objs b/hw/core/Makefile.objs
index 5377d05..0e082d2 100644
--- a/hw/core/Makefile.objs
+++ b/hw/core/Makefile.objs
@@ -13,3 +13,4 @@ common-obj-$(CONFIG_SOFTMMU) += machine.o
 common-obj-$(CONFIG_SOFTMMU) += null-machine.o
 common-obj-$(CONFIG_SOFTMMU) += loader.o
 common-obj-$(CONFIG_SOFTMMU) += qdev-properties-system.o
+common-obj-$(CONFIG_SOFTMMU) += accel.o
diff --git a/hw/core/accel.c b/hw/core/accel.c
new file mode 100644
index 0000000..04da696
--- /dev/null
+++ b/hw/core/accel.c
@@ -0,0 +1,113 @@
+/*
+ * QEMU System Emulator, accelerator interfaces
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ * Copyright (c) 2014 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 "hw/accel.h"
+#include "qemu-common.h"
+#include "sysemu/arch_init.h"
+#include "sysemu/sysemu.h"
+#include "sysemu/kvm.h"
+#include "sysemu/qtest.h"
+#include "hw/xen/xen.h"
+
+int tcg_tb_size;
+static bool tcg_allowed = true;
+
+static int tcg_init(MachineClass *mc)
+{
+    tcg_exec_init(tcg_tb_size * 1024 * 1024);
+    return 0;
+}
+
+static struct {
+    const char *opt_name;
+    const char *name;
+    int (*available)(void);
+    int (*init)(MachineClass *mc);
+    bool *allowed;
+} accel_list[] = {
+    { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
+    { "xen", "Xen", xen_available, xen_init, &xen_allowed },
+    { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
+    { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
+};
+
+int configure_accelerator(MachineClass *mc)
+{
+    const char *p;
+    char buf[10];
+    int i, ret;
+    bool accel_initialised = false;
+    bool init_failed = false;
+
+    p = qemu_opt_get(qemu_get_machine_opts(), "accel");
+    if (p == NULL) {
+        /* Use the default "accelerator", tcg */
+        p = "tcg";
+    }
+
+    while (!accel_initialised && *p != '\0') {
+        if (*p == ':') {
+            p++;
+        }
+        p = get_opt_name(buf, sizeof(buf), p, ':');
+        for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
+            if (strcmp(accel_list[i].opt_name, buf) == 0) {
+                if (!accel_list[i].available()) {
+                    printf("%s not supported for this target\n",
+                           accel_list[i].name);
+                    break;
+                }
+                *(accel_list[i].allowed) = true;
+                ret = accel_list[i].init(mc);
+                if (ret < 0) {
+                    init_failed = true;
+                    fprintf(stderr, "failed to initialize %s: %s\n",
+                            accel_list[i].name,
+                            strerror(-ret));
+                    *(accel_list[i].allowed) = false;
+                } else {
+                    accel_initialised = true;
+                }
+                break;
+            }
+        }
+        if (i == ARRAY_SIZE(accel_list)) {
+            fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
+        }
+    }
+
+    if (!accel_initialised) {
+        if (!init_failed) {
+            fprintf(stderr, "No accelerator found!\n");
+        }
+        exit(1);
+    }
+
+    if (init_failed) {
+        fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
+    }
+
+    return !accel_initialised;
+}
diff --git a/include/hw/accel.h b/include/hw/accel.h
new file mode 100644
index 0000000..5537d74
--- /dev/null
+++ b/include/hw/accel.h
@@ -0,0 +1,32 @@
+/* QEMU accelerator interfaces
+ *
+ * Copyright (c) 2014 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 HW_ACCEL_H
+#define HW_ACCEL_H
+
+#include "qemu/typedefs.h"
+
+extern int tcg_tb_size;
+
+int configure_accelerator(MachineClass *mc);
+
+#endif
diff --git a/vl.c b/vl.c
index 8da895f..d6e5c4e 100644
--- a/vl.c
+++ b/vl.c
@@ -61,6 +61,7 @@ int main(int argc, char **argv)
 #include "qemu/sockets.h"
 #include "hw/hw.h"
 #include "hw/boards.h"
+#include "hw/accel.h"
 #include "hw/usb.h"
 #include "hw/pcmcia.h"
 #include "hw/i386/pc.h"
@@ -211,11 +212,9 @@ static NotifierList exit_notifiers =
 static NotifierList machine_init_done_notifiers =
     NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
 
-static bool tcg_allowed = true;
 bool xen_allowed;
 uint32_t xen_domid;
 enum xen_mode xen_mode = XEN_EMULATE;
-static int tcg_tb_size;
 
 static int has_defaults = 1;
 static int default_serial = 1;
@@ -2655,84 +2654,6 @@ static MachineClass *machine_parse(const char *name)
     exit(!name || !is_help_option(name));
 }
 
-static int tcg_init(MachineClass *mc)
-{
-    tcg_exec_init(tcg_tb_size * 1024 * 1024);
-    return 0;
-}
-
-static struct {
-    const char *opt_name;
-    const char *name;
-    int (*available)(void);
-    int (*init)(MachineClass *mc);
-    bool *allowed;
-} accel_list[] = {
-    { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
-    { "xen", "Xen", xen_available, xen_init, &xen_allowed },
-    { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
-    { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
-};
-
-static int configure_accelerator(MachineClass *mc)
-{
-    const char *p;
-    char buf[10];
-    int i, ret;
-    bool accel_initialised = false;
-    bool init_failed = false;
-
-    p = qemu_opt_get(qemu_get_machine_opts(), "accel");
-    if (p == NULL) {
-        /* Use the default "accelerator", tcg */
-        p = "tcg";
-    }
-
-    while (!accel_initialised && *p != '\0') {
-        if (*p == ':') {
-            p++;
-        }
-        p = get_opt_name(buf, sizeof(buf), p, ':');
-        for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
-            if (strcmp(accel_list[i].opt_name, buf) == 0) {
-                if (!accel_list[i].available()) {
-                    printf("%s not supported for this target\n",
-                           accel_list[i].name);
-                    break;
-                }
-                *(accel_list[i].allowed) = true;
-                ret = accel_list[i].init(mc);
-                if (ret < 0) {
-                    init_failed = true;
-                    fprintf(stderr, "failed to initialize %s: %s\n",
-                            accel_list[i].name,
-                            strerror(-ret));
-                    *(accel_list[i].allowed) = false;
-                } else {
-                    accel_initialised = true;
-                }
-                break;
-            }
-        }
-        if (i == ARRAY_SIZE(accel_list)) {
-            fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
-        }
-    }
-
-    if (!accel_initialised) {
-        if (!init_failed) {
-            fprintf(stderr, "No accelerator found!\n");
-        }
-        exit(1);
-    }
-
-    if (init_failed) {
-        fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
-    }
-
-    return !accel_initialised;
-}
-
 void qemu_add_exit_notifier(Notifier *notify)
 {
     notifier_list_add(&exit_notifiers, notify);
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 03/25] accel: Create struct AccelType
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
  2014-07-09 22:03 ` [Qemu-devel] [RFC 01/25] vl.c: Small coding style fix Eduardo Habkost
  2014-07-09 22:03 ` [Qemu-devel] [RFC 02/25] accel: Move accel code to accel.c Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 04/25] accel: Simplify configure_accelerator() using AccelType *acc variable Eduardo Habkost
                   ` (21 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 04da696..c23c04b 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -40,13 +40,15 @@ static int tcg_init(MachineClass *mc)
     return 0;
 }
 
-static struct {
+typedef struct AccelType {
     const char *opt_name;
     const char *name;
     int (*available)(void);
     int (*init)(MachineClass *mc);
     bool *allowed;
-} accel_list[] = {
+} AccelType;
+
+static AccelType accel_list[] = {
     { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
     { "xen", "Xen", xen_available, xen_init, &xen_allowed },
     { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 04/25] accel: Simplify configure_accelerator() using AccelType *acc variable
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (2 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 03/25] accel: Create struct AccelType Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 05/25] accel: Move accel name lookup to separate function Eduardo Habkost
                   ` (20 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index c23c04b..00a71c0 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -62,6 +62,7 @@ int configure_accelerator(MachineClass *mc)
     int i, ret;
     bool accel_initialised = false;
     bool init_failed = false;
+    AccelType *acc = NULL;
 
     p = qemu_opt_get(qemu_get_machine_opts(), "accel");
     if (p == NULL) {
@@ -75,20 +76,21 @@ int configure_accelerator(MachineClass *mc)
         }
         p = get_opt_name(buf, sizeof(buf), p, ':');
         for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
-            if (strcmp(accel_list[i].opt_name, buf) == 0) {
-                if (!accel_list[i].available()) {
+            acc = &accel_list[i];
+            if (strcmp(acc->opt_name, buf) == 0) {
+                if (!acc->available()) {
                     printf("%s not supported for this target\n",
-                           accel_list[i].name);
+                           acc->name);
                     break;
                 }
-                *(accel_list[i].allowed) = true;
-                ret = accel_list[i].init(mc);
+                *(acc->allowed) = true;
+                ret = acc->init(mc);
                 if (ret < 0) {
                     init_failed = true;
                     fprintf(stderr, "failed to initialize %s: %s\n",
-                            accel_list[i].name,
+                            acc->name,
                             strerror(-ret));
-                    *(accel_list[i].allowed) = false;
+                    *(acc->allowed) = false;
                 } else {
                     accel_initialised = true;
                 }
@@ -108,7 +110,7 @@ int configure_accelerator(MachineClass *mc)
     }
 
     if (init_failed) {
-        fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
+        fprintf(stderr, "Back to %s accelerator.\n", acc->name);
     }
 
     return !accel_initialised;
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 05/25] accel: Move accel name lookup to separate function
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (3 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 04/25] accel: Simplify configure_accelerator() using AccelType *acc variable Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 06/25] accel: Use QOM classes for accel types Eduardo Habkost
                   ` (19 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c | 57 +++++++++++++++++++++++++++++++++------------------------
 1 file changed, 33 insertions(+), 24 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 00a71c0..7f9b715 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -55,11 +55,24 @@ static AccelType accel_list[] = {
     { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
 };
 
+/* Lookup AccelType from opt_name. Returns NULL if not found */
+static AccelType *accel_find(const char *opt_name)
+{
+    int i;
+    for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
+        AccelType *acc = &accel_list[i];
+        if (acc->opt_name && strcmp(acc->opt_name, opt_name) == 0) {
+            return acc;
+        }
+    }
+    return NULL;
+}
+
 int configure_accelerator(MachineClass *mc)
 {
     const char *p;
     char buf[10];
-    int i, ret;
+    int ret;
     bool accel_initialised = false;
     bool init_failed = false;
     AccelType *acc = NULL;
@@ -75,30 +88,26 @@ int configure_accelerator(MachineClass *mc)
             p++;
         }
         p = get_opt_name(buf, sizeof(buf), p, ':');
-        for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
-            acc = &accel_list[i];
-            if (strcmp(acc->opt_name, buf) == 0) {
-                if (!acc->available()) {
-                    printf("%s not supported for this target\n",
-                           acc->name);
-                    break;
-                }
-                *(acc->allowed) = true;
-                ret = acc->init(mc);
-                if (ret < 0) {
-                    init_failed = true;
-                    fprintf(stderr, "failed to initialize %s: %s\n",
-                            acc->name,
-                            strerror(-ret));
-                    *(acc->allowed) = false;
-                } else {
-                    accel_initialised = true;
-                }
-                break;
-            }
-        }
-        if (i == ARRAY_SIZE(accel_list)) {
+        acc = accel_find(buf);
+        if (!acc) {
             fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
+            continue;
+        }
+        if (!acc->available()) {
+            printf("%s not supported for this target\n",
+                   acc->name);
+            continue;
+        }
+        *(acc->allowed) = true;
+        ret = acc->init(mc);
+        if (ret < 0) {
+            init_failed = true;
+            fprintf(stderr, "failed to initialize %s: %s\n",
+                    acc->name,
+                    strerror(-ret));
+            *(acc->allowed) = false;
+        } else {
+            accel_initialised = true;
         }
     }
 
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 06/25] accel: Use QOM classes for accel types
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (4 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 05/25] accel: Move accel name lookup to separate function Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 07/25] accel: Make AccelClass.available() optional Eduardo Habkost
                   ` (18 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c    | 117 ++++++++++++++++++++++++++++++++++++++++++-----------
 include/hw/accel.h |  27 +++++++++++++
 2 files changed, 120 insertions(+), 24 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 7f9b715..b42335c 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -30,6 +30,7 @@
 #include "sysemu/kvm.h"
 #include "sysemu/qtest.h"
 #include "hw/xen/xen.h"
+#include "qom/object.h"
 
 int tcg_tb_size;
 static bool tcg_allowed = true;
@@ -40,32 +41,20 @@ static int tcg_init(MachineClass *mc)
     return 0;
 }
 
-typedef struct AccelType {
-    const char *opt_name;
-    const char *name;
-    int (*available)(void);
-    int (*init)(MachineClass *mc);
-    bool *allowed;
-} AccelType;
-
-static AccelType accel_list[] = {
-    { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
-    { "xen", "Xen", xen_available, xen_init, &xen_allowed },
-    { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
-    { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
+static const TypeInfo accel_type = {
+    .name = TYPE_ACCEL,
+    .parent = TYPE_OBJECT,
+    .class_size = sizeof(AccelClass),
+    .instance_size = sizeof(AccelState),
 };
 
-/* Lookup AccelType from opt_name. Returns NULL if not found */
-static AccelType *accel_find(const char *opt_name)
+/* Lookup AccelClass from opt_name. Returns NULL if not found */
+static AccelClass *accel_find(const char *opt_name)
 {
-    int i;
-    for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
-        AccelType *acc = &accel_list[i];
-        if (acc->opt_name && strcmp(acc->opt_name, opt_name) == 0) {
-            return acc;
-        }
-    }
-    return NULL;
+    char *class_name = g_strdup_printf("%s-accel", opt_name);
+    AccelClass *ac = ACCEL_CLASS(object_class_by_name(class_name));
+    g_free(class_name);
+    return ac;
 }
 
 int configure_accelerator(MachineClass *mc)
@@ -75,7 +64,7 @@ int configure_accelerator(MachineClass *mc)
     int ret;
     bool accel_initialised = false;
     bool init_failed = false;
-    AccelType *acc = NULL;
+    AccelClass *acc = NULL;
 
     p = qemu_opt_get(qemu_get_machine_opts(), "accel");
     if (p == NULL) {
@@ -124,3 +113,83 @@ int configure_accelerator(MachineClass *mc)
 
     return !accel_initialised;
 }
+
+
+static void tcg_accel_class_init(ObjectClass *oc, void *data)
+{
+    AccelClass *ac = ACCEL_CLASS(oc);
+    ac->name = "tcg";
+    ac->available = tcg_available;
+    ac->init = tcg_init;
+    ac->allowed = &tcg_allowed;
+}
+
+#define TYPE_TCG_ACCEL "tcg-accel"
+
+static const TypeInfo tcg_accel_type = {
+    .name = TYPE_TCG_ACCEL,
+    .parent = TYPE_ACCEL,
+    .class_init = tcg_accel_class_init,
+};
+
+static void xen_accel_class_init(ObjectClass *oc, void *data)
+{
+    AccelClass *ac = ACCEL_CLASS(oc);
+    ac->name = "Xen";
+    ac->available = xen_available;
+    ac->init = xen_init;
+    ac->allowed = &xen_allowed;
+}
+
+#define TYPE_XEN_ACCEL "xen-accel"
+
+static const TypeInfo xen_accel_type = {
+    .name = TYPE_XEN_ACCEL,
+    .parent = TYPE_ACCEL,
+    .class_init = xen_accel_class_init,
+};
+
+static void kvm_accel_class_init(ObjectClass *oc, void *data)
+{
+    AccelClass *ac = ACCEL_CLASS(oc);
+    ac->name = "KVM";
+    ac->available = kvm_available;
+    ac->init = kvm_init;
+    ac->allowed = &kvm_allowed;
+}
+
+#define TYPE_KVM_ACCEL "kvm-accel"
+
+static const TypeInfo kvm_accel_type = {
+    .name = TYPE_KVM_ACCEL,
+    .parent = TYPE_ACCEL,
+    .class_init = kvm_accel_class_init,
+};
+
+static void qtest_accel_class_init(ObjectClass *oc, void *data)
+{
+    AccelClass *ac = ACCEL_CLASS(oc);
+    ac->name = "QTest";
+    ac->available = qtest_available;
+    ac->init = qtest_init_accel;
+    ac->allowed = &qtest_allowed;
+}
+
+#define TYPE_QTEST_ACCEL "qtest-accel"
+
+static const TypeInfo qtest_accel_type = {
+    .name = TYPE_QTEST_ACCEL,
+    .parent = TYPE_ACCEL,
+    .class_init = qtest_accel_class_init,
+};
+
+static void register_accel_types(void)
+{
+    type_register_static(&accel_type);
+    type_register_static(&tcg_accel_type);
+    type_register_static(&xen_accel_type);
+    type_register_static(&kvm_accel_type);
+    type_register_static(&qtest_accel_type);
+}
+
+type_init(register_accel_types);
diff --git a/include/hw/accel.h b/include/hw/accel.h
index 5537d74..01f9831 100644
--- a/include/hw/accel.h
+++ b/include/hw/accel.h
@@ -24,6 +24,33 @@
 #define HW_ACCEL_H
 
 #include "qemu/typedefs.h"
+#include "qom/object.h"
+
+typedef struct AccelState {
+    /*< private >*/
+    Object parent_obj;
+} AccelState;
+
+typedef struct AccelClass {
+    /*< private >*/
+    ObjectClass parent_class;
+    /*< public >*/
+
+    const char *opt_name;
+    const char *name;
+    int (*available)(void);
+    int (*init)(MachineClass *mc);
+    bool *allowed;
+} AccelClass;
+
+#define TYPE_ACCEL "accel"
+
+#define ACCEL_CLASS(klass) \
+    OBJECT_CLASS_CHECK(AccelClass, (klass), TYPE_ACCEL)
+#define ACCEL(obj) \
+    OBJECT_CHECK(AccelState, (obj), TYPE_ACCEL)
+#define ACCEL_GET_CLASS(obj) \
+    OBJECT_GET_CLASS(AccelClass, (obj), TYPE_ACCEL)
 
 extern int tcg_tb_size;
 
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 07/25] accel: Make AccelClass.available() optional
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (5 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 06/25] accel: Use QOM classes for accel types Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 08/25] accel: Move KVM accel registration to kvm-all.c Eduardo Habkost
                   ` (17 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

When we move accel classes outside accel.c, the available() function
won't be necessary anymore, because the classes will be registered only
if the accelerator code is really enable at build time.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index b42335c..b2a92b6 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -82,7 +82,7 @@ int configure_accelerator(MachineClass *mc)
             fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
             continue;
         }
-        if (!acc->available()) {
+        if (acc->available && !acc->available()) {
             printf("%s not supported for this target\n",
                    acc->name);
             continue;
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 08/25] accel: Move KVM accel registration to kvm-all.c
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (6 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 07/25] accel: Make AccelClass.available() optional Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 09/25] accel: Move Xen registration code to xen-common.c Eduardo Habkost
                   ` (16 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Note that this has an user-visible side-effect: instead of reporting
"KVM is not supported for this target", QEMU binaries not supporting KVM
will report "kvm accelerator does not exist".

As kvm_availble() always return 1 when CONFIG_KVM is enabled, we don't
need to set AccelClass.available anymore. kvm_enabled() is not being
completely removed yet only because qmp_query_kvm() still uses it.

This also allows us to make kvm_init() static.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c      | 18 ------------------
 include/sysemu/kvm.h |  2 --
 kvm-all.c            | 28 +++++++++++++++++++++++++++-
 kvm-stub.c           |  5 -----
 4 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index b2a92b6..2226c17 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -149,23 +149,6 @@ static const TypeInfo xen_accel_type = {
     .class_init = xen_accel_class_init,
 };
 
-static void kvm_accel_class_init(ObjectClass *oc, void *data)
-{
-    AccelClass *ac = ACCEL_CLASS(oc);
-    ac->name = "KVM";
-    ac->available = kvm_available;
-    ac->init = kvm_init;
-    ac->allowed = &kvm_allowed;
-}
-
-#define TYPE_KVM_ACCEL "kvm-accel"
-
-static const TypeInfo kvm_accel_type = {
-    .name = TYPE_KVM_ACCEL,
-    .parent = TYPE_ACCEL,
-    .class_init = kvm_accel_class_init,
-};
-
 static void qtest_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelClass *ac = ACCEL_CLASS(oc);
@@ -188,7 +171,6 @@ static void register_accel_types(void)
     type_register_static(&accel_type);
     type_register_static(&tcg_accel_type);
     type_register_static(&xen_accel_type);
-    type_register_static(&kvm_accel_type);
     type_register_static(&qtest_accel_type);
 }
 
diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
index 174ea36..7b95bfd 100644
--- a/include/sysemu/kvm.h
+++ b/include/sysemu/kvm.h
@@ -163,8 +163,6 @@ extern KVMState *kvm_state;
 
 /* external API */
 
-int kvm_init(MachineClass *mc);
-
 int kvm_has_sync_mmu(void);
 int kvm_has_vcpu_events(void);
 int kvm_has_robust_singlestep(void);
diff --git a/kvm-all.c b/kvm-all.c
index 3ae30ee..cd4f9fa 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -26,6 +26,7 @@
 #include "qemu/config-file.h"
 #include "sysemu/sysemu.h"
 #include "hw/hw.h"
+#include "hw/accel.h"
 #include "hw/pci/msi.h"
 #include "hw/s390x/adapter.h"
 #include "exec/gdbstub.h"
@@ -110,6 +111,8 @@ struct KVMState
 #endif
 };
 
+#define TYPE_KVM_ACCEL "kvm-accel"
+
 KVMState *kvm_state;
 bool kvm_kernel_irqchip;
 bool kvm_async_interrupts_allowed;
@@ -1368,7 +1371,7 @@ static int kvm_max_vcpus(KVMState *s)
     return (ret) ? ret : kvm_recommended_vcpus(s);
 }
 
-int kvm_init(MachineClass *mc)
+static int kvm_init(MachineClass *mc)
 {
     static const char upgrade_note[] =
         "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
@@ -2198,3 +2201,26 @@ int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target)
     }
     return r;
 }
+
+static void kvm_accel_class_init(ObjectClass *oc, void *data)
+{
+    AccelClass *ac = ACCEL_CLASS(oc);
+    ac->name = "KVM";
+    ac->init = kvm_init;
+    ac->allowed = &kvm_allowed;
+}
+
+#define TYPE_KVM_ACCEL "kvm-accel"
+
+static const TypeInfo kvm_accel_type = {
+    .name = TYPE_KVM_ACCEL,
+    .parent = TYPE_ACCEL,
+    .class_init = kvm_accel_class_init,
+};
+
+static void kvm_type_init(void)
+{
+    type_register_static(&kvm_accel_type);
+}
+
+type_init(kvm_type_init);
diff --git a/kvm-stub.c b/kvm-stub.c
index 8e7737c..43fc0dd 100644
--- a/kvm-stub.c
+++ b/kvm-stub.c
@@ -35,11 +35,6 @@ int kvm_init_vcpu(CPUState *cpu)
     return -ENOSYS;
 }
 
-int kvm_init(MachineClass *mc)
-{
-    return -ENOSYS;
-}
-
 void kvm_flush_coalesced_mmio_buffer(void)
 {
 }
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 09/25] accel: Move Xen registration code to xen-common.c
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (7 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 08/25] accel: Move KVM accel registration to kvm-all.c Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 10/25] accel: Move qtest accel registration to qtest.c Eduardo Habkost
                   ` (15 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Note that this has an user-visible side-effect: instead of reporting
"Xen is not supported for this target", QEMU binaries not supporting Xen
will report "xen accelerator does not exist".

As xen_available() always return 1 when CONFIG_XEN is enabled, we don't
need to set AccelClass.available anymore. xen_enabled() is not being is
not being removed yet, only because vl.c is still using it.

This also allows us to make xen_init() static.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c      | 18 ------------------
 include/hw/xen/xen.h |  1 -
 xen-common-stub.c    |  6 ------
 xen-common.c         | 25 ++++++++++++++++++++++++-
 4 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 2226c17..34995fd 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -132,23 +132,6 @@ static const TypeInfo tcg_accel_type = {
     .class_init = tcg_accel_class_init,
 };
 
-static void xen_accel_class_init(ObjectClass *oc, void *data)
-{
-    AccelClass *ac = ACCEL_CLASS(oc);
-    ac->name = "Xen";
-    ac->available = xen_available;
-    ac->init = xen_init;
-    ac->allowed = &xen_allowed;
-}
-
-#define TYPE_XEN_ACCEL "xen-accel"
-
-static const TypeInfo xen_accel_type = {
-    .name = TYPE_XEN_ACCEL,
-    .parent = TYPE_ACCEL,
-    .class_init = xen_accel_class_init,
-};
-
 static void qtest_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelClass *ac = ACCEL_CLASS(oc);
@@ -170,7 +153,6 @@ static void register_accel_types(void)
 {
     type_register_static(&accel_type);
     type_register_static(&tcg_accel_type);
-    type_register_static(&xen_accel_type);
     type_register_static(&qtest_accel_type);
 }
 
diff --git a/include/hw/xen/xen.h b/include/hw/xen/xen.h
index f71f2d8..b0ed04c 100644
--- a/include/hw/xen/xen.h
+++ b/include/hw/xen/xen.h
@@ -36,7 +36,6 @@ void xen_cmos_set_s3_resume(void *opaque, int irq, int level);
 
 qemu_irq *xen_interrupt_controller_init(void);
 
-int xen_init(MachineClass *mc);
 void xenstore_store_pv_console_info(int i, struct CharDriverState *chr);
 
 #if defined(NEED_CPU_H) && !defined(CONFIG_USER_ONLY)
diff --git a/xen-common-stub.c b/xen-common-stub.c
index bd56ca2..906f991 100644
--- a/xen-common-stub.c
+++ b/xen-common-stub.c
@@ -11,9 +11,3 @@
 void xenstore_store_pv_console_info(int i, CharDriverState *chr)
 {
 }
-
-int xen_init(MachineClass *mc)
-{
-    return -ENOSYS;
-}
-
diff --git a/xen-common.c b/xen-common.c
index f07b35e..ef853f6 100644
--- a/xen-common.c
+++ b/xen-common.c
@@ -9,6 +9,7 @@
  */
 
 #include "hw/xen/xen_backend.h"
+#include "hw/accel.h"
 #include "qmp-commands.h"
 #include "sysemu/char.h"
 
@@ -109,7 +110,7 @@ static void xen_change_state_handler(void *opaque, int running,
     }
 }
 
-int xen_init(MachineClass *mc)
+static int xen_init(MachineClass *mc)
 {
     xen_xc = xen_xc_interface_open(0, 0, 0);
     if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
@@ -121,3 +122,25 @@ int xen_init(MachineClass *mc)
     return 0;
 }
 
+static void xen_accel_class_init(ObjectClass *oc, void *data)
+{
+    AccelClass *ac = ACCEL_CLASS(oc);
+    ac->name = "Xen";
+    ac->init = xen_init;
+    ac->allowed = &xen_allowed;
+}
+
+#define TYPE_XEN_ACCEL "xen-accel"
+
+static const TypeInfo xen_accel_type = {
+    .name = TYPE_XEN_ACCEL,
+    .parent = TYPE_ACCEL,
+    .class_init = xen_accel_class_init,
+};
+
+static void xen_type_init(void)
+{
+    type_register_static(&xen_accel_type);
+}
+
+type_init(xen_type_init);
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 10/25] accel: Move qtest accel registration to qtest.c
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (8 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 09/25] accel: Move Xen registration code to xen-common.c Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 11/25] accel: Remove tcg_available() function Eduardo Habkost
                   ` (14 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

As qtest_availble() returns 1 only when CONFIG_POSIX is set, keep
setting AccelClass.available to keep current behavior (this is different
from what we did for KVM and Xen).

This also allows us to make qtest_init_accel() static.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c        | 18 ------------------
 include/sysemu/qtest.h |  1 -
 qtest.c                | 27 ++++++++++++++++++++++++++-
 3 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 34995fd..95fbf1e 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -132,28 +132,10 @@ static const TypeInfo tcg_accel_type = {
     .class_init = tcg_accel_class_init,
 };
 
-static void qtest_accel_class_init(ObjectClass *oc, void *data)
-{
-    AccelClass *ac = ACCEL_CLASS(oc);
-    ac->name = "QTest";
-    ac->available = qtest_available;
-    ac->init = qtest_init_accel;
-    ac->allowed = &qtest_allowed;
-}
-
-#define TYPE_QTEST_ACCEL "qtest-accel"
-
-static const TypeInfo qtest_accel_type = {
-    .name = TYPE_QTEST_ACCEL,
-    .parent = TYPE_ACCEL,
-    .class_init = qtest_accel_class_init,
-};
-
 static void register_accel_types(void)
 {
     type_register_static(&accel_type);
     type_register_static(&tcg_accel_type);
-    type_register_static(&qtest_accel_type);
 }
 
 type_init(register_accel_types);
diff --git a/include/sysemu/qtest.h b/include/sysemu/qtest.h
index 95c9ade..05473b7 100644
--- a/include/sysemu/qtest.h
+++ b/include/sysemu/qtest.h
@@ -26,7 +26,6 @@ static inline bool qtest_enabled(void)
 
 bool qtest_driver(void);
 
-int qtest_init_accel(MachineClass *mc);
 void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp);
 
 static inline int qtest_available(void)
diff --git a/qtest.c b/qtest.c
index 04a6dc1..f7c0432 100644
--- a/qtest.c
+++ b/qtest.c
@@ -17,6 +17,7 @@
 #include "exec/ioport.h"
 #include "exec/memory.h"
 #include "hw/irq.h"
+#include "hw/accel.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/cpus.h"
 
@@ -509,7 +510,7 @@ static void qtest_event(void *opaque, int event)
     }
 }
 
-int qtest_init_accel(MachineClass *mc)
+static int qtest_init_accel(MachineClass *mc)
 {
     configure_icount("0");
 
@@ -548,3 +549,27 @@ bool qtest_driver(void)
 {
     return qtest_chr;
 }
+
+static void qtest_accel_class_init(ObjectClass *oc, void *data)
+{
+    AccelClass *ac = ACCEL_CLASS(oc);
+    ac->name = "QTest";
+    ac->available = qtest_available;
+    ac->init = qtest_init_accel;
+    ac->allowed = &qtest_allowed;
+}
+
+#define TYPE_QTEST_ACCEL "qtest-accel"
+
+static const TypeInfo qtest_accel_type = {
+    .name = TYPE_QTEST_ACCEL,
+    .parent = TYPE_ACCEL,
+    .class_init = qtest_accel_class_init,
+};
+
+static void qtest_type_init(void)
+{
+    type_register_static(&qtest_accel_type);
+}
+
+type_init(qtest_type_init);
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 11/25] accel: Remove tcg_available() function
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (9 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 10/25] accel: Move qtest accel registration to qtest.c Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 12/25] accel: Move accel init/allowed code to separate function Eduardo Habkost
                   ` (13 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

As the function always return 1, it is not needed anymore.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 arch_init.c                | 5 -----
 hw/core/accel.c            | 1 -
 include/sysemu/arch_init.h | 1 -
 3 files changed, 7 deletions(-)

diff --git a/arch_init.c b/arch_init.c
index 8ddaf35..7870c0c 100644
--- a/arch_init.c
+++ b/arch_init.c
@@ -1335,11 +1335,6 @@ void cpudef_init(void)
 #endif
 }
 
-int tcg_available(void)
-{
-    return 1;
-}
-
 int kvm_available(void)
 {
 #ifdef CONFIG_KVM
diff --git a/hw/core/accel.c b/hw/core/accel.c
index 95fbf1e..9aa853f 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -119,7 +119,6 @@ static void tcg_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelClass *ac = ACCEL_CLASS(oc);
     ac->name = "tcg";
-    ac->available = tcg_available;
     ac->init = tcg_init;
     ac->allowed = &tcg_allowed;
 }
diff --git a/include/sysemu/arch_init.h b/include/sysemu/arch_init.h
index 182d48d..5968837 100644
--- a/include/sysemu/arch_init.h
+++ b/include/sysemu/arch_init.h
@@ -32,7 +32,6 @@ void do_smbios_option(QemuOpts *opts);
 void ram_mig_init(void);
 void cpudef_init(void);
 void audio_init(void);
-int tcg_available(void);
 int kvm_available(void);
 int xen_available(void);
 
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 12/25] accel: Move accel init/allowed code to separate function
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (10 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 11/25] accel: Remove tcg_available() function Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 13/25] accel: Rename 'init' method to 'init_machine' Eduardo Habkost
                   ` (12 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 9aa853f..85e60eb 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -57,6 +57,17 @@ static AccelClass *accel_find(const char *opt_name)
     return ac;
 }
 
+static int accel_init(AccelClass *acc, MachineClass *mc)
+{
+    int ret;
+    *(acc->allowed) = true;
+    ret = acc->init(mc);
+    if (ret < 0) {
+        *(acc->allowed) = false;
+    }
+    return ret;
+}
+
 int configure_accelerator(MachineClass *mc)
 {
     const char *p;
@@ -87,14 +98,12 @@ int configure_accelerator(MachineClass *mc)
                    acc->name);
             continue;
         }
-        *(acc->allowed) = true;
-        ret = acc->init(mc);
+        ret = accel_init(acc, mc);
         if (ret < 0) {
             init_failed = true;
             fprintf(stderr, "failed to initialize %s: %s\n",
                     acc->name,
                     strerror(-ret));
-            *(acc->allowed) = false;
         } else {
             accel_initialised = true;
         }
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 13/25] accel: Rename 'init' method to 'init_machine'
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (11 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 12/25] accel: Move accel init/allowed code to separate function Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 14/25] accel: Pass MachineState object to accel init functions Eduardo Habkost
                   ` (11 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

This makes more explicit the fact that the method is for machine
initialization, not just for accelerator object initialization.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c    | 8 ++++----
 include/hw/accel.h | 2 +-
 kvm-all.c          | 2 +-
 qtest.c            | 2 +-
 xen-common.c       | 2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 85e60eb..9552690 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -57,11 +57,11 @@ static AccelClass *accel_find(const char *opt_name)
     return ac;
 }
 
-static int accel_init(AccelClass *acc, MachineClass *mc)
+static int accel_init_machine(AccelClass *acc, MachineClass *mc)
 {
     int ret;
     *(acc->allowed) = true;
-    ret = acc->init(mc);
+    ret = acc->init_machine(mc);
     if (ret < 0) {
         *(acc->allowed) = false;
     }
@@ -98,7 +98,7 @@ int configure_accelerator(MachineClass *mc)
                    acc->name);
             continue;
         }
-        ret = accel_init(acc, mc);
+        ret = accel_init_machine(acc, mc);
         if (ret < 0) {
             init_failed = true;
             fprintf(stderr, "failed to initialize %s: %s\n",
@@ -128,7 +128,7 @@ static void tcg_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelClass *ac = ACCEL_CLASS(oc);
     ac->name = "tcg";
-    ac->init = tcg_init;
+    ac->init_machine = tcg_init;
     ac->allowed = &tcg_allowed;
 }
 
diff --git a/include/hw/accel.h b/include/hw/accel.h
index 01f9831..20b0ec7 100644
--- a/include/hw/accel.h
+++ b/include/hw/accel.h
@@ -39,7 +39,7 @@ typedef struct AccelClass {
     const char *opt_name;
     const char *name;
     int (*available)(void);
-    int (*init)(MachineClass *mc);
+    int (*init_machine)(MachineClass *mc);
     bool *allowed;
 } AccelClass;
 
diff --git a/kvm-all.c b/kvm-all.c
index cd4f9fa..f96512f 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -2206,7 +2206,7 @@ static void kvm_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelClass *ac = ACCEL_CLASS(oc);
     ac->name = "KVM";
-    ac->init = kvm_init;
+    ac->init_machine = kvm_init;
     ac->allowed = &kvm_allowed;
 }
 
diff --git a/qtest.c b/qtest.c
index f7c0432..7bd81ea 100644
--- a/qtest.c
+++ b/qtest.c
@@ -555,7 +555,7 @@ static void qtest_accel_class_init(ObjectClass *oc, void *data)
     AccelClass *ac = ACCEL_CLASS(oc);
     ac->name = "QTest";
     ac->available = qtest_available;
-    ac->init = qtest_init_accel;
+    ac->init_machine = qtest_init_accel;
     ac->allowed = &qtest_allowed;
 }
 
diff --git a/xen-common.c b/xen-common.c
index ef853f6..3867a87 100644
--- a/xen-common.c
+++ b/xen-common.c
@@ -126,7 +126,7 @@ static void xen_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelClass *ac = ACCEL_CLASS(oc);
     ac->name = "Xen";
-    ac->init = xen_init;
+    ac->init_machine = xen_init;
     ac->allowed = &xen_allowed;
 }
 
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 14/25] accel: Pass MachineState object to accel init functions
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (12 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 13/25] accel: Rename 'init' method to 'init_machine' Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 15/25] accel: Create accel object when initializing machine Eduardo Habkost
                   ` (10 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Most of the machine options and machine state information is in the
MachineState object, not on the MachineClass. This will allow init
functions to use the MachineState object directly instead of the
qemu_get_machine_opts() or current_machine globals, and get the
AccelState object using MachineState.accelerator.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c         | 11 ++++++-----
 include/hw/accel.h      |  4 ++--
 include/qemu/typedefs.h |  1 +
 kvm-all.c               |  3 ++-
 qtest.c                 |  2 +-
 vl.c                    |  2 +-
 xen-common.c            |  2 +-
 7 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 9552690..4deb53f 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -24,6 +24,7 @@
  */
 
 #include "hw/accel.h"
+#include "hw/boards.h"
 #include "qemu-common.h"
 #include "sysemu/arch_init.h"
 #include "sysemu/sysemu.h"
@@ -35,7 +36,7 @@
 int tcg_tb_size;
 static bool tcg_allowed = true;
 
-static int tcg_init(MachineClass *mc)
+static int tcg_init(MachineState *ms)
 {
     tcg_exec_init(tcg_tb_size * 1024 * 1024);
     return 0;
@@ -57,18 +58,18 @@ static AccelClass *accel_find(const char *opt_name)
     return ac;
 }
 
-static int accel_init_machine(AccelClass *acc, MachineClass *mc)
+static int accel_init_machine(AccelClass *acc, MachineState *ms)
 {
     int ret;
     *(acc->allowed) = true;
-    ret = acc->init_machine(mc);
+    ret = acc->init_machine(ms);
     if (ret < 0) {
         *(acc->allowed) = false;
     }
     return ret;
 }
 
-int configure_accelerator(MachineClass *mc)
+int configure_accelerator(MachineState *ms)
 {
     const char *p;
     char buf[10];
@@ -98,7 +99,7 @@ int configure_accelerator(MachineClass *mc)
                    acc->name);
             continue;
         }
-        ret = accel_init_machine(acc, mc);
+        ret = accel_init_machine(acc, ms);
         if (ret < 0) {
             init_failed = true;
             fprintf(stderr, "failed to initialize %s: %s\n",
diff --git a/include/hw/accel.h b/include/hw/accel.h
index 20b0ec7..4f9909d 100644
--- a/include/hw/accel.h
+++ b/include/hw/accel.h
@@ -39,7 +39,7 @@ typedef struct AccelClass {
     const char *opt_name;
     const char *name;
     int (*available)(void);
-    int (*init_machine)(MachineClass *mc);
+    int (*init_machine)(MachineState *ms);
     bool *allowed;
 } AccelClass;
 
@@ -54,6 +54,6 @@ typedef struct AccelClass {
 
 extern int tcg_tb_size;
 
-int configure_accelerator(MachineClass *mc);
+int configure_accelerator(MachineState *ms);
 
 #endif
diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
index 5f20b0e..04df51b 100644
--- a/include/qemu/typedefs.h
+++ b/include/qemu/typedefs.h
@@ -32,6 +32,7 @@ typedef struct MemoryMappingList MemoryMappingList;
 
 typedef struct QEMUMachine QEMUMachine;
 typedef struct MachineClass MachineClass;
+typedef struct MachineState MachineState;
 typedef struct NICInfo NICInfo;
 typedef struct HCIInfo HCIInfo;
 typedef struct AudioState AudioState;
diff --git a/kvm-all.c b/kvm-all.c
index f96512f..fdb2fb5 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -1371,8 +1371,9 @@ static int kvm_max_vcpus(KVMState *s)
     return (ret) ? ret : kvm_recommended_vcpus(s);
 }
 
-static int kvm_init(MachineClass *mc)
+static int kvm_init(MachineState *ms)
 {
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
     static const char upgrade_note[] =
         "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
         "(see http://sourceforge.net/projects/kvm).\n";
diff --git a/qtest.c b/qtest.c
index 7bd81ea..b5fe486 100644
--- a/qtest.c
+++ b/qtest.c
@@ -510,7 +510,7 @@ static void qtest_event(void *opaque, int event)
     }
 }
 
-static int qtest_init_accel(MachineClass *mc)
+static int qtest_init_accel(MachineState *ms)
 {
     configure_icount("0");
 
diff --git a/vl.c b/vl.c
index d6e5c4e..e1d0502 100644
--- a/vl.c
+++ b/vl.c
@@ -4142,7 +4142,7 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
-    configure_accelerator(machine_class);
+    configure_accelerator(current_machine);
 
     if (qtest_chrdev) {
         Error *local_err = NULL;
diff --git a/xen-common.c b/xen-common.c
index 3867a87..d9c7abb 100644
--- a/xen-common.c
+++ b/xen-common.c
@@ -110,7 +110,7 @@ static void xen_change_state_handler(void *opaque, int running,
     }
 }
 
-static int xen_init(MachineClass *mc)
+static int xen_init(MachineState *ms)
 {
     xen_xc = xen_xc_interface_open(0, 0, 0);
     if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 15/25] accel: Create accel object when initializing machine
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (13 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 14/25] accel: Pass MachineState object to accel init functions Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 16/25] accel: Save AccelState on MachineState when initializing Eduardo Habkost
                   ` (9 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 4deb53f..b86773d 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -60,11 +60,15 @@ static AccelClass *accel_find(const char *opt_name)
 
 static int accel_init_machine(AccelClass *acc, MachineState *ms)
 {
+    ObjectClass *oc = OBJECT_CLASS(acc);
+    const char *cname = object_class_get_name(oc);
+    AccelState *accel = ACCEL(object_new(cname));
     int ret;
     *(acc->allowed) = true;
     ret = acc->init_machine(ms);
     if (ret < 0) {
         *(acc->allowed) = false;
+        object_unref(OBJECT(accel));
     }
     return ret;
 }
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 16/25] accel: Save AccelState on MachineState when initializing
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (14 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 15/25] accel: Create accel object when initializing machine Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 17/25] kvm: Make KVMState be the TYPE_KVM_ACCEL instance struct Eduardo Habkost
                   ` (8 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c         | 3 +++
 include/hw/boards.h     | 1 +
 include/qemu/typedefs.h | 2 ++
 3 files changed, 6 insertions(+)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index b86773d..268ecee 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -32,6 +32,7 @@
 #include "sysemu/qtest.h"
 #include "hw/xen/xen.h"
 #include "qom/object.h"
+#include "hw/boards.h"
 
 int tcg_tb_size;
 static bool tcg_allowed = true;
@@ -64,9 +65,11 @@ static int accel_init_machine(AccelClass *acc, MachineState *ms)
     const char *cname = object_class_get_name(oc);
     AccelState *accel = ACCEL(object_new(cname));
     int ret;
+    ms->accelerator = accel;
     *(acc->allowed) = true;
     ret = acc->init_machine(ms);
     if (ret < 0) {
+        ms->accelerator = NULL;
         *(acc->allowed) = false;
         object_unref(OBJECT(accel));
     }
diff --git a/include/hw/boards.h b/include/hw/boards.h
index 605a970..1cff2b4 100644
--- a/include/hw/boards.h
+++ b/include/hw/boards.h
@@ -132,6 +132,7 @@ struct MachineState {
     char *kernel_cmdline;
     char *initrd_filename;
     const char *cpu_model;
+    AccelState *accelerator;
 };
 
 #endif
diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
index 04df51b..446af93 100644
--- a/include/qemu/typedefs.h
+++ b/include/qemu/typedefs.h
@@ -30,6 +30,8 @@ typedef struct MemoryListener MemoryListener;
 
 typedef struct MemoryMappingList MemoryMappingList;
 
+typedef struct AccelState AccelState;
+
 typedef struct QEMUMachine QEMUMachine;
 typedef struct MachineClass MachineClass;
 typedef struct MachineState MachineState;
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 17/25] kvm: Make KVMState be the TYPE_KVM_ACCEL instance struct
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (15 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 16/25] accel: Save AccelState on MachineState when initializing Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 18/25] accel: Get target name as argument when initializing accelerator Eduardo Habkost
                   ` (7 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Now that we create an accel object before calling machine_init, we can
simply use the object to save all KVMState data.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 kvm-all.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/kvm-all.c b/kvm-all.c
index fdb2fb5..9185a62 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -75,8 +75,10 @@ typedef struct KVMSlot
 
 typedef struct kvm_dirty_log KVMDirtyLog;
 
-struct KVMState
+typedef struct KVMState
 {
+    AccelState parent_obj;
+
     KVMSlot *slots;
     int nr_slots;
     int fd;
@@ -109,10 +111,13 @@ struct KVMState
     QTAILQ_HEAD(msi_hashtab, KVMMSIRoute) msi_hashtab[KVM_MSI_HASHTAB_SIZE];
     bool direct_msi;
 #endif
-};
+} KVMState;
 
 #define TYPE_KVM_ACCEL "kvm-accel"
 
+#define KVM_STATE(obj) \
+    OBJECT_CHECK(KVMState, (obj), TYPE_KVM_ACCEL)
+
 KVMState *kvm_state;
 bool kvm_kernel_irqchip;
 bool kvm_async_interrupts_allowed;
@@ -1392,7 +1397,7 @@ static int kvm_init(MachineState *ms)
     int i, type = 0;
     const char *kvm_type;
 
-    s = g_malloc0(sizeof(KVMState));
+    s = KVM_STATE(ms->accelerator);
 
     /*
      * On systems where the kernel can support different base page
@@ -1581,7 +1586,6 @@ err:
         close(s->fd);
     }
     g_free(s->slots);
-    g_free(s);
 
     return ret;
 }
@@ -2217,6 +2221,7 @@ static const TypeInfo kvm_accel_type = {
     .name = TYPE_KVM_ACCEL,
     .parent = TYPE_ACCEL,
     .class_init = kvm_accel_class_init,
+    .instance_size = sizeof(KVMState),
 };
 
 static void kvm_type_init(void)
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 18/25] accel: Get target name as argument when initializing accelerator
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (16 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 17/25] kvm: Make KVMState be the TYPE_KVM_ACCEL instance struct Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 19/25] accel: Use target-specific accel class if available Eduardo Habkost
                   ` (6 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

We will look for target-specific accel subclasses, if available.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 arch_init.c        | 6 ++++++
 hw/core/accel.c    | 2 +-
 include/hw/accel.h | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch_init.c b/arch_init.c
index 7870c0c..79f3d6a 100644
--- a/arch_init.c
+++ b/arch_init.c
@@ -52,6 +52,7 @@
 #include "exec/ram_addr.h"
 #include "hw/acpi/acpi.h"
 #include "qemu/host-utils.h"
+#include "hw/accel.h"
 
 #ifdef DEBUG_ARCH_INIT
 #define DPRINTF(fmt, ...) \
@@ -155,6 +156,11 @@ int qemu_read_default_config_files(bool userconfig)
     return 0;
 }
 
+int configure_accelerator(MachineState *ms)
+{
+    return init_accelerator(ms, TARGET_NAME);
+}
+
 static inline bool is_zero_range(uint8_t *p, uint64_t size)
 {
     return buffer_find_nonzero_offset(p, size) == size;
diff --git a/hw/core/accel.c b/hw/core/accel.c
index 268ecee..78e7dfc 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -76,7 +76,7 @@ static int accel_init_machine(AccelClass *acc, MachineState *ms)
     return ret;
 }
 
-int configure_accelerator(MachineState *ms)
+int init_accelerator(MachineState *ms, const char *target_name)
 {
     const char *p;
     char buf[10];
diff --git a/include/hw/accel.h b/include/hw/accel.h
index 4f9909d..a369c61 100644
--- a/include/hw/accel.h
+++ b/include/hw/accel.h
@@ -55,5 +55,6 @@ typedef struct AccelClass {
 extern int tcg_tb_size;
 
 int configure_accelerator(MachineState *ms);
+int init_accelerator(MachineState *ms, const char *target_name);
 
 #endif
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 19/25] accel: Use target-specific accel class if available
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (17 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 18/25] accel: Get target name as argument when initializing accelerator Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 20/25] accel: TYPE_X86_ACCEL interface Eduardo Habkost
                   ` (5 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Target-specific accelerator subclasses are optional. If a given
accelerator type needs to make it mandatory, the base class can be made
abstract.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/core/accel.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/hw/core/accel.c b/hw/core/accel.c
index 78e7dfc..6efe36c 100644
--- a/hw/core/accel.c
+++ b/hw/core/accel.c
@@ -51,11 +51,25 @@ static const TypeInfo accel_type = {
 };
 
 /* Lookup AccelClass from opt_name. Returns NULL if not found */
-static AccelClass *accel_find(const char *opt_name)
+static AccelClass *accel_find(const char *opt_name, const char *target_name)
 {
     char *class_name = g_strdup_printf("%s-accel", opt_name);
-    AccelClass *ac = ACCEL_CLASS(object_class_by_name(class_name));
+    char *target_class_name = g_strdup_printf("%s-%s-accel",
+                                              target_name, opt_name);
+    ObjectClass *oc;
+    AccelClass *ac;
+    oc = object_class_by_name(target_class_name);
+    if (!oc) {
+        oc = object_class_by_name(class_name);
+    }
+    /* Must be a TYPE_ACCEL subclass, and not abstract */
+    oc = object_class_dynamic_cast(oc, TYPE_ACCEL);
+    if (oc && object_class_is_abstract(oc)) {
+        oc = NULL;
+    }
+    ac = ACCEL_CLASS(oc);
     g_free(class_name);
+    g_free(target_class_name);
     return ac;
 }
 
@@ -96,7 +110,7 @@ int init_accelerator(MachineState *ms, const char *target_name)
             p++;
         }
         p = get_opt_name(buf, sizeof(buf), p, ':');
-        acc = accel_find(buf);
+        acc = accel_find(buf, target_name);
         if (!acc) {
             fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
             continue;
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 20/25] accel: TYPE_X86_ACCEL interface
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (18 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 19/25] accel: Use target-specific accel class if available Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 21/25] kvm: x86 KVM accelerator subclass Eduardo Habkost
                   ` (4 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 include/hw/i386/accel.h   | 35 +++++++++++++++++++++++++++++++++++
 target-i386/Makefile.objs |  1 +
 target-i386/accel.c       | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+)
 create mode 100644 include/hw/i386/accel.h
 create mode 100644 target-i386/accel.c

diff --git a/include/hw/i386/accel.h b/include/hw/i386/accel.h
new file mode 100644
index 0000000..63d60c7
--- /dev/null
+++ b/include/hw/i386/accel.h
@@ -0,0 +1,35 @@
+/* TYPE_X86_ACCEL interface
+ */
+#ifndef HW_I386_ACCEL_H
+#define HW_I386_ACCEL_H
+
+#include "hw/accel.h"
+
+#define TYPE_X86_ACCEL "x86-accel"
+
+#define X86_ACCEL_CLASS(klass) \
+     OBJECT_CLASS_CHECK(X86AccelClass, (klass), TYPE_X86_ACCEL)
+#define X86_ACCEL_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(X86AccelClass, (obj), TYPE_X86_ACCEL)
+#define X86_ACCEL(obj) \
+     INTERFACE_CHECK(X86Accel, (obj), TYPE_X86_ACCEL)
+
+
+typedef struct X86Accel {
+    /* <private> */
+    Object Parent;
+} X86Accel;
+
+/**
+ * HotplugDeviceClass:
+ *
+ * Interface that may be implemented by target-specific accelerator
+ * classes.
+ */
+typedef struct X86AccelClass {
+    /* <private> */
+    InterfaceClass parent;
+} X86AccelClass;
+
+
+#endif
diff --git a/target-i386/Makefile.objs b/target-i386/Makefile.objs
index 027b94e..0537185 100644
--- a/target-i386/Makefile.objs
+++ b/target-i386/Makefile.objs
@@ -2,6 +2,7 @@ obj-y += translate.o helper.o cpu.o
 obj-y += excp_helper.o fpu_helper.o cc_helper.o int_helper.o svm_helper.o
 obj-y += smm_helper.o misc_helper.o mem_helper.o seg_helper.o
 obj-y += gdbstub.o
+obj-y += accel.o
 obj-$(CONFIG_SOFTMMU) += machine.o arch_memory_mapping.o arch_dump.o
 obj-$(CONFIG_KVM) += kvm.o
 obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o
diff --git a/target-i386/accel.c b/target-i386/accel.c
new file mode 100644
index 0000000..6ca1a48
--- /dev/null
+++ b/target-i386/accel.c
@@ -0,0 +1,38 @@
+/* TYPE_X86_ACCEL interface
+ *
+ * Copyright (c) 2014 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 "qemu/module.h"
+#include "hw/i386/accel.h"
+
+static const TypeInfo x86_accel_type = {
+    .name = TYPE_X86_ACCEL,
+    .parent = TYPE_INTERFACE,
+    .class_size = sizeof(X86AccelClass),
+};
+
+static void x86_accel_type_init(void)
+{
+    type_register_static(&x86_accel_type);
+}
+
+type_init(x86_accel_type_init);
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 21/25] kvm: x86 KVM accelerator subclass
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (19 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 20/25] accel: TYPE_X86_ACCEL interface Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 22/25] target-i386: Add AccelState parameter to cpu_x86_create() Eduardo Habkost
                   ` (3 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Implements TYPE_X86_ACCEL.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 include/sysemu/kvm.h |  3 +++
 kvm-all.c            |  2 --
 target-i386/kvm.c    | 19 +++++++++++++++++++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h
index 7b95bfd..34b4dfe 100644
--- a/include/sysemu/kvm.h
+++ b/include/sysemu/kvm.h
@@ -414,4 +414,7 @@ int kvm_set_one_reg(CPUState *cs, uint64_t id, void *source);
  * Returns: 0 on success, or a negative errno on failure.
  */
 int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target);
+
+#define TYPE_KVM_ACCEL "kvm-accel"
+
 #endif
diff --git a/kvm-all.c b/kvm-all.c
index 9185a62..c03b4e7 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -113,8 +113,6 @@ typedef struct KVMState
 #endif
 } KVMState;
 
-#define TYPE_KVM_ACCEL "kvm-accel"
-
 #define KVM_STATE(obj) \
     OBJECT_CHECK(KVMState, (obj), TYPE_KVM_ACCEL)
 
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index 097fe11..6221794 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -28,6 +28,7 @@
 #include "exec/gdbstub.h"
 #include "qemu/host-utils.h"
 #include "qemu/config-file.h"
+#include "hw/i386/accel.h"
 #include "hw/i386/pc.h"
 #include "hw/i386/apic.h"
 #include "hw/i386/apic_internal.h"
@@ -2591,3 +2592,21 @@ int kvm_device_msix_deassign(KVMState *s, uint32_t dev_id)
     return kvm_deassign_irq_internal(s, dev_id, KVM_DEV_IRQ_GUEST_MSIX |
                                                 KVM_DEV_IRQ_HOST_MSIX);
 }
+
+#define TYPE_X86_KVM_ACCEL TARGET_NAME "-kvm-accel"
+
+static const TypeInfo x86_kvm_accel_type = {
+    .name = TYPE_X86_KVM_ACCEL,
+    .parent = TYPE_KVM_ACCEL,
+    .interfaces = (InterfaceInfo[]) {
+         { TYPE_X86_ACCEL },
+         { }
+    },
+};
+
+static void x86_kvm_type_init(void)
+{
+    type_register_static(&x86_kvm_accel_type);
+}
+
+type_init(x86_kvm_type_init);
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 22/25] target-i386: Add AccelState parameter to cpu_x86_create()
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (20 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 21/25] kvm: x86 KVM accelerator subclass Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 23/25] target-i386: Move accelerator-specific code outside X86CPU.instance_init Eduardo Habkost
                   ` (2 subsequent siblings)
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/i386/pc.c      | 3 ++-
 target-i386/cpu.c | 4 ++--
 target-i386/cpu.h | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index 2cf22b1..03a108c 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -960,7 +960,8 @@ static X86CPU *pc_new_cpu(const char *cpu_model, int64_t apic_id,
     X86CPU *cpu;
     Error *local_err = NULL;
 
-    cpu = cpu_x86_create(cpu_model, icc_bridge, &local_err);
+    cpu = cpu_x86_create(cpu_model, icc_bridge, current_machine->accelerator,
+                         &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
         return NULL;
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 45c662d..428ced3 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -2015,7 +2015,7 @@ static void x86_cpu_load_def(X86CPU *cpu, X86CPUDefinition *def, Error **errp)
 }
 
 X86CPU *cpu_x86_create(const char *cpu_model, DeviceState *icc_bridge,
-                       Error **errp)
+                       AccelState *accel, Error **errp)
 {
     X86CPU *cpu = NULL;
     X86CPUClass *xcc;
@@ -2077,7 +2077,7 @@ X86CPU *cpu_x86_init(const char *cpu_model)
     Error *error = NULL;
     X86CPU *cpu;
 
-    cpu = cpu_x86_create(cpu_model, NULL, &error);
+    cpu = cpu_x86_create(cpu_model, NULL, NULL, &error);
     if (error) {
         goto out;
     }
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
index e634d83..421859a 100644
--- a/target-i386/cpu.h
+++ b/target-i386/cpu.h
@@ -964,7 +964,7 @@ typedef struct CPUX86State {
 
 X86CPU *cpu_x86_init(const char *cpu_model);
 X86CPU *cpu_x86_create(const char *cpu_model, DeviceState *icc_bridge,
-                       Error **errp);
+                       AccelState *accel, Error **errp);
 int cpu_x86_exec(CPUX86State *s);
 void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf);
 void x86_cpudef_setup(void);
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 23/25] target-i386: Move accelerator-specific code outside X86CPU.instance_init
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (21 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 22/25] target-i386: Add AccelState parameter to cpu_x86_create() Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 24/25] target-i386: Accept "host" as value for CPU vendor Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 25/25] target-i386: Move KVM CPUID hacking to accelerator cpu_post_init hook Eduardo Habkost
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

The code will be changed to use the accelerator object, so it has to be
outside instance_init (which can't get any extra parameters).

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 target-i386/cpu.c | 61 +++++++++++++++++++++++++++++++------------------------
 1 file changed, 34 insertions(+), 27 deletions(-)

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 428ced3..406ce02 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1968,8 +1968,6 @@ static int x86_cpu_filter_features(X86CPU *cpu)
 static void x86_cpu_load_def(X86CPU *cpu, X86CPUDefinition *def, Error **errp)
 {
     CPUX86State *env = &cpu->env;
-    const char *vendor;
-    char host_vendor[CPUID_VENDOR_SZ + 1];
     FeatureWord w;
 
     object_property_set_int(OBJECT(cpu), def->level, "level", errp);
@@ -1977,6 +1975,7 @@ static void x86_cpu_load_def(X86CPU *cpu, X86CPUDefinition *def, Error **errp)
     object_property_set_int(OBJECT(cpu), def->model, "model", errp);
     object_property_set_int(OBJECT(cpu), def->stepping, "stepping", errp);
     object_property_set_int(OBJECT(cpu), def->xlevel, "xlevel", errp);
+    object_property_set_str(OBJECT(cpu), def->vendor, "vendor", errp);
     env->cpuid_xlevel2 = def->xlevel2;
     cpu->cache_info_passthrough = def->cache_info_passthrough;
     object_property_set_str(OBJECT(cpu), def->model_id, "model-id", errp);
@@ -1985,32 +1984,46 @@ static void x86_cpu_load_def(X86CPU *cpu, X86CPUDefinition *def, Error **errp)
     }
 
     /* Special cases not set in the X86CPUDefinition structs: */
+    env->features[FEAT_1_ECX] |= CPUID_EXT_HYPERVISOR;
+}
+
+/* Accelerator-specific initialization code, to be called immediately after
+ * creation of the X86CPU object, and before x86_cpu_parse_featurestr().
+ */
+static void x86_cpu_accel_init(X86CPU *cpu, Error **errp)
+{
+    CPUX86State *env = &cpu->env;
+    static int inited;
+
     if (kvm_enabled()) {
+        char host_vendor[CPUID_VENDOR_SZ + 1];
+        uint32_t  ebx = 0, ecx = 0, edx = 0;
         FeatureWord w;
         for (w = 0; w < FEATURE_WORDS; w++) {
             env->features[w] |= kvm_default_features[w];
             env->features[w] &= ~kvm_default_unset_features[w];
         }
-    }
 
-    env->features[FEAT_1_ECX] |= CPUID_EXT_HYPERVISOR;
-
-    /* sysenter isn't supported in compatibility mode on AMD,
-     * syscall isn't supported in compatibility mode on Intel.
-     * Normally we advertise the actual CPU vendor, but you can
-     * override this using the 'vendor' property if you want to use
-     * KVM's sysenter/syscall emulation in compatibility mode and
-     * when doing cross vendor migration
-     */
-    vendor = def->vendor;
-    if (kvm_enabled()) {
-        uint32_t  ebx = 0, ecx = 0, edx = 0;
+        /* sysenter isn't supported in compatibility mode on AMD,
+         * syscall isn't supported in compatibility mode on Intel.
+         * Normally we advertise the actual CPU vendor, but you can
+         * override this using the 'vendor' property if you want to use
+         * KVM's sysenter/syscall emulation in compatibility mode and
+         * when doing cross vendor migration
+         */
         host_cpuid(0, 0, NULL, &ebx, &ecx, &edx);
         x86_cpu_vendor_words2str(host_vendor, ebx, edx, ecx);
-        vendor = host_vendor;
+        object_property_set_str(OBJECT(cpu), host_vendor, "vendor", errp);
     }
 
-    object_property_set_str(OBJECT(cpu), vendor, "vendor", errp);
+    /* init various static tables used in TCG mode */
+    if (tcg_enabled() && !inited) {
+        inited = 1;
+        optimize_flags_init();
+#ifndef CONFIG_USER_ONLY
+        cpu_set_debug_excp_handler(breakpoint_handler);
+#endif
+    }
 
 }
 
@@ -2045,6 +2058,10 @@ X86CPU *cpu_x86_create(const char *cpu_model, DeviceState *icc_bridge,
     }
 
     cpu = X86_CPU(object_new(object_class_get_name(oc)));
+    x86_cpu_accel_init(cpu, &error);
+    if (error) {
+        goto out;
+    }
 
 #ifndef CONFIG_USER_ONLY
     if (icc_bridge == NULL) {
@@ -2777,7 +2794,6 @@ static void x86_cpu_initfn(Object *obj)
     X86CPU *cpu = X86_CPU(obj);
     X86CPUClass *xcc = X86_CPU_GET_CLASS(obj);
     CPUX86State *env = &cpu->env;
-    static int inited;
 
     cs->env_ptr = env;
     cpu_exec_init(env);
@@ -2820,15 +2836,6 @@ static void x86_cpu_initfn(Object *obj)
     env->cpuid_apic_id = x86_cpu_apic_id_from_index(cs->cpu_index);
 
     x86_cpu_load_def(cpu, xcc->cpu_def, &error_abort);
-
-    /* init various static tables used in TCG mode */
-    if (tcg_enabled() && !inited) {
-        inited = 1;
-        optimize_flags_init();
-#ifndef CONFIG_USER_ONLY
-        cpu_set_debug_excp_handler(breakpoint_handler);
-#endif
-    }
 }
 
 static int64_t x86_cpu_get_arch_id(CPUState *cs)
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 24/25] target-i386: Accept "host" as value for CPU vendor
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (22 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 23/25] target-i386: Move accelerator-specific code outside X86CPU.instance_init Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  2014-07-09 22:04 ` [Qemu-devel] [RFC 25/25] target-i386: Move KVM CPUID hacking to accelerator cpu_post_init hook Eduardo Habkost
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

When using vendor=host, the host CPU vendor will be used.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 target-i386/cpu.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 406ce02..eaae1ce 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1528,8 +1528,16 @@ static void x86_cpuid_set_vendor(Object *obj, const char *value,
 {
     X86CPU *cpu = X86_CPU(obj);
     CPUX86State *env = &cpu->env;
+    char host_vendor[CPUID_VENDOR_SZ + 1];
     int i;
 
+    if (!strcmp(value, "host")) {
+        uint32_t  ebx = 0, ecx = 0, edx = 0;
+        host_cpuid(0, 0, NULL, &ebx, &ecx, &edx);
+        x86_cpu_vendor_words2str(host_vendor, ebx, edx, ecx);
+        value = host_vendor;
+    }
+
     if (strlen(value) != CPUID_VENDOR_SZ) {
         error_set(errp, QERR_PROPERTY_VALUE_BAD, "",
                   "vendor", value);
@@ -1996,8 +2004,6 @@ static void x86_cpu_accel_init(X86CPU *cpu, Error **errp)
     static int inited;
 
     if (kvm_enabled()) {
-        char host_vendor[CPUID_VENDOR_SZ + 1];
-        uint32_t  ebx = 0, ecx = 0, edx = 0;
         FeatureWord w;
         for (w = 0; w < FEATURE_WORDS; w++) {
             env->features[w] |= kvm_default_features[w];
@@ -2011,9 +2017,7 @@ static void x86_cpu_accel_init(X86CPU *cpu, Error **errp)
          * KVM's sysenter/syscall emulation in compatibility mode and
          * when doing cross vendor migration
          */
-        host_cpuid(0, 0, NULL, &ebx, &ecx, &edx);
-        x86_cpu_vendor_words2str(host_vendor, ebx, edx, ecx);
-        object_property_set_str(OBJECT(cpu), host_vendor, "vendor", errp);
+        object_property_set_str(OBJECT(cpu), "host", "vendor", errp);
     }
 
     /* init various static tables used in TCG mode */
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [Qemu-devel] [RFC 25/25] target-i386: Move KVM CPUID hacking to accelerator cpu_post_init hook
  2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
                   ` (23 preceding siblings ...)
  2014-07-09 22:04 ` [Qemu-devel] [RFC 24/25] target-i386: Accept "host" as value for CPU vendor Eduardo Habkost
@ 2014-07-09 22:04 ` Eduardo Habkost
  24 siblings, 0 replies; 26+ messages in thread
From: Eduardo Habkost @ 2014-07-09 22:04 UTC (permalink / raw)
  To: qemu-devel

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 include/hw/i386/accel.h |  7 +++++++
 include/qemu/typedefs.h |  2 ++
 target-i386/cpu.c       | 54 ++++++++++---------------------------------------
 target-i386/kvm.c       | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 71 insertions(+), 43 deletions(-)

diff --git a/include/hw/i386/accel.h b/include/hw/i386/accel.h
index 63d60c7..c294492 100644
--- a/include/hw/i386/accel.h
+++ b/include/hw/i386/accel.h
@@ -4,6 +4,7 @@
 #define HW_I386_ACCEL_H
 
 #include "hw/accel.h"
+#include "qemu/typedefs.h"
 
 #define TYPE_X86_ACCEL "x86-accel"
 
@@ -25,10 +26,16 @@ typedef struct X86Accel {
  *
  * Interface that may be implemented by target-specific accelerator
  * classes.
+ *
+ * @cpu_post_init: Hook called after creation of X86CPU objects. Can be used,
+ *                 to change CPUID data to match accelerator-specific
+ *                 requirements.
  */
 typedef struct X86AccelClass {
     /* <private> */
     InterfaceClass parent;
+    /* <public> */
+    void (*cpu_post_init)(AccelState *accel, X86CPU *cpu, Error **errp);
 } X86AccelClass;
 
 
diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
index 446af93..5879a47 100644
--- a/include/qemu/typedefs.h
+++ b/include/qemu/typedefs.h
@@ -79,4 +79,6 @@ typedef struct PcGuestInfo PcGuestInfo;
 typedef struct Range Range;
 typedef struct AdapterInfo AdapterInfo;
 
+typedef struct X86CPU X86CPU;
+
 #endif /* QEMU_TYPEDEFS_H */
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index eaae1ce..5fc5762 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -48,6 +48,7 @@
 #include "hw/xen/xen.h"
 #include "hw/i386/apic_internal.h"
 #endif
+#include "hw/i386/accel.h"
 
 
 /* Cache topology CPUID constants: */
@@ -444,31 +445,6 @@ typedef struct model_features_t {
     FeatureWord feat_word;
 } model_features_t;
 
-/* KVM-specific features that are automatically added to all CPU models
- * when KVM is enabled.
- */
-static uint32_t kvm_default_features[FEATURE_WORDS] = {
-    [FEAT_KVM] = (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_PV_EOI) |
-        (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT),
-    [FEAT_1_ECX] = CPUID_EXT_X2APIC,
-};
-
-/* Features that are not added by default to any CPU model when KVM is enabled.
- */
-static uint32_t kvm_default_unset_features[FEATURE_WORDS] = {
-    [FEAT_1_ECX] = CPUID_EXT_MONITOR,
-};
-
-void x86_cpu_compat_disable_kvm_features(FeatureWord w, uint32_t features)
-{
-    kvm_default_features[w] &= ~features;
-}
-
 /*
  * Returns the set of feature flags that are supported and migratable by
  * QEMU, for a given FeatureWord.
@@ -1998,26 +1974,18 @@ static void x86_cpu_load_def(X86CPU *cpu, X86CPUDefinition *def, Error **errp)
 /* Accelerator-specific initialization code, to be called immediately after
  * creation of the X86CPU object, and before x86_cpu_parse_featurestr().
  */
-static void x86_cpu_accel_init(X86CPU *cpu, Error **errp)
+static void x86_cpu_accel_init(X86CPU *cpu, AccelState *accel, Error **errp)
 {
-    CPUX86State *env = &cpu->env;
     static int inited;
+    X86Accel *xac = X86_ACCEL(object_dynamic_cast(OBJECT(accel),
+                                                  TYPE_X86_ACCEL));
+    X86AccelClass *xacc = NULL;
+    if (xac) {
+        xacc = X86_ACCEL_GET_CLASS(xac);
+    }
 
-    if (kvm_enabled()) {
-        FeatureWord w;
-        for (w = 0; w < FEATURE_WORDS; w++) {
-            env->features[w] |= kvm_default_features[w];
-            env->features[w] &= ~kvm_default_unset_features[w];
-        }
-
-        /* sysenter isn't supported in compatibility mode on AMD,
-         * syscall isn't supported in compatibility mode on Intel.
-         * Normally we advertise the actual CPU vendor, but you can
-         * override this using the 'vendor' property if you want to use
-         * KVM's sysenter/syscall emulation in compatibility mode and
-         * when doing cross vendor migration
-         */
-        object_property_set_str(OBJECT(cpu), "host", "vendor", errp);
+    if (xacc && xacc->cpu_post_init) {
+        xacc->cpu_post_init(accel, cpu, errp);
     }
 
     /* init various static tables used in TCG mode */
@@ -2062,7 +2030,7 @@ X86CPU *cpu_x86_create(const char *cpu_model, DeviceState *icc_bridge,
     }
 
     cpu = X86_CPU(object_new(object_class_get_name(oc)));
-    x86_cpu_accel_init(cpu, &error);
+    x86_cpu_accel_init(cpu, accel, &error);
     if (error) {
         goto out;
     }
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index 6221794..377e104 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -2593,11 +2593,62 @@ int kvm_device_msix_deassign(KVMState *s, uint32_t dev_id)
                                                 KVM_DEV_IRQ_HOST_MSIX);
 }
 
+/* KVM-specific features that are automatically added to all CPU models
+ * when KVM is enabled.
+ */
+static uint32_t kvm_default_features[FEATURE_WORDS] = {
+    [FEAT_KVM] = (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_PV_EOI) |
+        (1 << KVM_FEATURE_CLOCKSOURCE_STABLE_BIT),
+    [FEAT_1_ECX] = CPUID_EXT_X2APIC,
+};
+
+/* Features that are not added by default to any CPU model when KVM is enabled.
+ */
+static uint32_t kvm_default_unset_features[FEATURE_WORDS] = {
+    [FEAT_1_ECX] = CPUID_EXT_MONITOR,
+};
+
+void x86_cpu_compat_disable_kvm_features(FeatureWord w, uint32_t features)
+{
+    kvm_default_features[w] &= ~features;
+}
+
+static void x86_kvm_cpu_post_init(AccelState *accel, X86CPU *cpu, Error **errp)
+{
+    CPUX86State *env = &cpu->env;
+    FeatureWord w;
+    for (w = 0; w < FEATURE_WORDS; w++) {
+        env->features[w] |= kvm_default_features[w];
+        env->features[w] &= ~kvm_default_unset_features[w];
+    }
+
+    /* sysenter isn't supported in compatibility mode on AMD,
+     * syscall isn't supported in compatibility mode on Intel.
+     * Normally we advertise the actual CPU vendor, but you can
+     * override this using the 'vendor' property if you want to use
+     * KVM's sysenter/syscall emulation in compatibility mode and
+     * when doing cross vendor migration
+     */
+    object_property_set_str(OBJECT(cpu), "host", "vendor", errp);
+}
+
+static void x86_kvm_accel_class_init(ObjectClass *oc, void *data)
+{
+    X86AccelClass *xacc = X86_ACCEL_CLASS(oc);
+    xacc->cpu_post_init = x86_kvm_cpu_post_init;
+}
+
 #define TYPE_X86_KVM_ACCEL TARGET_NAME "-kvm-accel"
 
 static const TypeInfo x86_kvm_accel_type = {
     .name = TYPE_X86_KVM_ACCEL,
     .parent = TYPE_KVM_ACCEL,
+    .class_init = x86_kvm_accel_class_init,
     .interfaces = (InterfaceInfo[]) {
          { TYPE_X86_ACCEL },
          { }
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2014-07-09 22:05 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-07-09 22:03 [Qemu-devel] [RFC 00/25] QOMify accelerator code Eduardo Habkost
2014-07-09 22:03 ` [Qemu-devel] [RFC 01/25] vl.c: Small coding style fix Eduardo Habkost
2014-07-09 22:03 ` [Qemu-devel] [RFC 02/25] accel: Move accel code to accel.c Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 03/25] accel: Create struct AccelType Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 04/25] accel: Simplify configure_accelerator() using AccelType *acc variable Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 05/25] accel: Move accel name lookup to separate function Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 06/25] accel: Use QOM classes for accel types Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 07/25] accel: Make AccelClass.available() optional Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 08/25] accel: Move KVM accel registration to kvm-all.c Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 09/25] accel: Move Xen registration code to xen-common.c Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 10/25] accel: Move qtest accel registration to qtest.c Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 11/25] accel: Remove tcg_available() function Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 12/25] accel: Move accel init/allowed code to separate function Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 13/25] accel: Rename 'init' method to 'init_machine' Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 14/25] accel: Pass MachineState object to accel init functions Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 15/25] accel: Create accel object when initializing machine Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 16/25] accel: Save AccelState on MachineState when initializing Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 17/25] kvm: Make KVMState be the TYPE_KVM_ACCEL instance struct Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 18/25] accel: Get target name as argument when initializing accelerator Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 19/25] accel: Use target-specific accel class if available Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 20/25] accel: TYPE_X86_ACCEL interface Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 21/25] kvm: x86 KVM accelerator subclass Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 22/25] target-i386: Add AccelState parameter to cpu_x86_create() Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 23/25] target-i386: Move accelerator-specific code outside X86CPU.instance_init Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 24/25] target-i386: Accept "host" as value for CPU vendor Eduardo Habkost
2014-07-09 22:04 ` [Qemu-devel] [RFC 25/25] target-i386: Move KVM CPUID hacking to accelerator cpu_post_init hook Eduardo Habkost

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).