All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eduardo Habkost <ehabkost@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Jiri Denemark" <jdenemar@redhat.com>,
	"Andreas Färber" <afaerber@suse.de>,
	"Igor Mammedov" <imammedo@redhat.com>,
	libvir-list@redhat.com
Subject: [Qemu-devel] [PATCH 4/9] target-i386: List CPU models using subclass list
Date: Fri,  6 May 2016 15:11:27 -0300	[thread overview]
Message-ID: <1462558292-2126-5-git-send-email-ehabkost@redhat.com> (raw)
In-Reply-To: <1462558292-2126-1-git-send-email-ehabkost@redhat.com>

Instead of using the builtin_x86_defs array, use the QOM subclass list
to list CPU models on "-cpu ?" and "query-cpu-definitions".

Signed-off-by: Andreas Färber <afaerber@suse.de>
[ehabkost: copied code from a patch by Andreas:
 "target-i386: QOM'ify CPU", from March 2012]
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 target-i386/cpu-qom.h |   4 ++
 target-i386/cpu.c     | 111 +++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 86 insertions(+), 29 deletions(-)

diff --git a/target-i386/cpu-qom.h b/target-i386/cpu-qom.h
index cb75017..c23b634 100644
--- a/target-i386/cpu-qom.h
+++ b/target-i386/cpu-qom.h
@@ -64,6 +64,10 @@ typedef struct X86CPUClass {
 
     bool kvm_required;
 
+    /* Optional description of CPU model.
+     * If unavailable, cpu_def->model_id is used */
+    const char *model_description;
+
     DeviceRealize parent_realize;
     void (*parent_reset)(CPUState *cpu);
 } X86CPUClass;
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 26a5a6b..037b602 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -676,6 +676,14 @@ static ObjectClass *x86_cpu_class_by_name(const char *cpu_model)
     return oc;
 }
 
+static char *x86_cpu_class_get_model_name(X86CPUClass *cc)
+{
+    const char *class_name = object_class_get_name(OBJECT_CLASS(cc));
+    assert(g_str_has_suffix(class_name, X86_CPU_TYPE_SUFFIX));
+    return g_strndup(class_name,
+                     strlen(class_name) - strlen(X86_CPU_TYPE_SUFFIX));
+}
+
 struct X86CPUDefinition {
     const char *name;
     uint32_t level;
@@ -1484,6 +1492,9 @@ static void host_x86_cpu_class_init(ObjectClass *oc, void *data)
     cpu_x86_fill_model_id(host_cpudef.model_id);
 
     xcc->cpu_def = &host_cpudef;
+    xcc->model_description =
+        "KVM processor with all supported host features "
+        "(only available in KVM mode)";
 
     /* level, xlevel, xlevel2, and the feature words are initialized on
      * instance_init, because they require KVM to be initialized.
@@ -2009,23 +2020,62 @@ static void listflags(FILE *f, fprintf_function print, const char **featureset)
     }
 }
 
-/* generate CPU information. */
+/* Sort alphabetically by type name, listing kvm_required models last. */
+static gint x86_cpu_list_compare(gconstpointer a, gconstpointer b)
+{
+    ObjectClass *class_a = (ObjectClass *)a;
+    ObjectClass *class_b = (ObjectClass *)b;
+    X86CPUClass *cc_a = X86_CPU_CLASS(class_a);
+    X86CPUClass *cc_b = X86_CPU_CLASS(class_b);
+    const char *name_a, *name_b;
+
+    if (cc_a->kvm_required != cc_b->kvm_required) {
+        /* kvm_required items go last */
+        return cc_a->kvm_required ? 1 : -1;
+    } else {
+        name_a = object_class_get_name(class_a);
+        name_b = object_class_get_name(class_b);
+        return strcmp(name_a, name_b);
+    }
+}
+
+static GSList *get_sorted_cpu_model_list(void)
+{
+    GSList *list = object_class_get_list(TYPE_X86_CPU, false);
+    list = g_slist_sort(list, x86_cpu_list_compare);
+    return list;
+}
+
+static void x86_cpu_list_entry(gpointer data, gpointer user_data)
+{
+    ObjectClass *oc = data;
+    X86CPUClass *cc = X86_CPU_CLASS(oc);
+    CPUListState *s = user_data;
+    char *name = x86_cpu_class_get_model_name(cc);
+    const char *desc = cc->model_description;
+    if (!desc) {
+        desc = cc->cpu_def->model_id;
+    }
+
+    (*s->cpu_fprintf)(s->file, "x86 %16s  %-48s\n",
+                      name, desc);
+    g_free(name);
+}
+
+/* list available CPU models and flags */
 void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf)
 {
-    X86CPUDefinition *def;
-    char buf[256];
     int i;
+    CPUListState s = {
+        .file = f,
+        .cpu_fprintf = cpu_fprintf,
+    };
+    GSList *list;
 
-    for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
-        def = &builtin_x86_defs[i];
-        snprintf(buf, sizeof(buf), "%s", def->name);
-        (*cpu_fprintf)(f, "x86 %16s  %-48s\n", buf, def->model_id);
-    }
-#ifdef CONFIG_KVM
-    (*cpu_fprintf)(f, "x86 %16s  %-48s\n", "host",
-                   "KVM processor with all supported host features "
-                   "(only available in KVM mode)");
-#endif
+    (*cpu_fprintf)(f, "Available CPUs:\n");
+    list = get_sorted_cpu_model_list();
+    g_slist_foreach(list, x86_cpu_list_entry, &s);
+    g_slist_free(list);
 
     (*cpu_fprintf)(f, "\nRecognized CPUID flags:\n");
     for (i = 0; i < ARRAY_SIZE(feature_word_info); i++) {
@@ -2037,26 +2087,29 @@ void x86_cpu_list(FILE *f, fprintf_function cpu_fprintf)
     }
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+static void x86_cpu_definition_entry(gpointer data, gpointer user_data)
 {
-    CpuDefinitionInfoList *cpu_list = NULL;
-    X86CPUDefinition *def;
-    int i;
-
-    for (i = 0; i < ARRAY_SIZE(builtin_x86_defs); i++) {
-        CpuDefinitionInfoList *entry;
-        CpuDefinitionInfo *info;
+    ObjectClass *oc = data;
+    X86CPUClass *cc = X86_CPU_CLASS(oc);
+    CpuDefinitionInfoList **cpu_list = user_data;
+    CpuDefinitionInfoList *entry;
+    CpuDefinitionInfo *info;
 
-        def = &builtin_x86_defs[i];
-        info = g_malloc0(sizeof(*info));
-        info->name = g_strdup(def->name);
+    info = g_malloc0(sizeof(*info));
+    info->name = x86_cpu_class_get_model_name(cc);
 
-        entry = g_malloc0(sizeof(*entry));
-        entry->value = info;
-        entry->next = cpu_list;
-        cpu_list = entry;
-    }
+    entry = g_malloc0(sizeof(*entry));
+    entry->value = info;
+    entry->next = *cpu_list;
+    *cpu_list = entry;
+}
 
+CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+{
+    CpuDefinitionInfoList *cpu_list = NULL;
+    GSList *list = get_sorted_cpu_model_list();
+    g_slist_foreach(list, x86_cpu_definition_entry, &cpu_list);
+    g_slist_free(list);
     return cpu_list;
 }
 
-- 
2.5.5

  parent reply	other threads:[~2016-05-06 18:12 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-06 18:11 [Qemu-devel] [PATCH 0/9] Add runnability info to query-cpu-definitions Eduardo Habkost
2016-05-06 18:11 ` [Qemu-devel] [PATCH 1/9] target-i386: Move TCG initialization check to tcg_x86_init() Eduardo Habkost
2016-05-10 14:58   ` Igor Mammedov
2016-05-06 18:11 ` [Qemu-devel] [PATCH 2/9] target-i386: Move TCG initialization to realize time Eduardo Habkost
2016-05-10 15:10   ` Igor Mammedov
2016-05-06 18:11 ` [Qemu-devel] [PATCH 3/9] target-i386: Call cpu_exec_init() on realize Eduardo Habkost
2016-05-10 15:15   ` Igor Mammedov
2016-05-06 18:11 ` Eduardo Habkost [this message]
2016-05-06 18:11 ` [Qemu-devel] [PATCH 5/9] target-i386: Move warning code outside x86_cpu_filter_features() Eduardo Habkost
2016-05-06 18:11 ` [Qemu-devel] [PATCH 6/9] target-i386: Define CPUID filtering functions before x86_cpu_list() Eduardo Habkost
2016-05-06 18:11 ` [Qemu-devel] [PATCH 7/9] qmp: Add runnability information to query-cpu-definitions Eduardo Habkost
2016-05-09  6:04   ` Markus Armbruster
2016-05-09  8:54   ` David Hildenbrand
2016-05-09 12:00     ` Eduardo Habkost
2016-05-09 12:05       ` David Hildenbrand
2016-05-09 12:36         ` Eduardo Habkost
2016-05-09 13:06           ` David Hildenbrand
2016-05-09 19:45             ` Eduardo Habkost
2016-05-10  6:32               ` David Hildenbrand
2016-05-10 12:03                 ` Eduardo Habkost
2016-05-09 15:20   ` [Qemu-devel] [libvirt] " Eric Blake
2016-05-09 19:25     ` Eduardo Habkost
2016-05-10  8:23       ` Markus Armbruster
2016-05-10  8:31         ` Jiri Denemark
2016-05-10 11:57         ` Eduardo Habkost
2016-05-11  7:11           ` Markus Armbruster
2016-05-11 19:35             ` Eduardo Habkost
2016-05-12  6:46               ` David Hildenbrand
2016-05-12  7:19               ` Jiri Denemark
2016-05-12 11:07                 ` Eduardo Habkost
2016-05-12  7:46               ` Markus Armbruster
2016-05-27 20:19                 ` Eduardo Habkost
2016-05-30  9:33                   ` Markus Armbruster
2016-05-31 12:01                     ` Eduardo Habkost
2016-05-31 13:24                       ` Markus Armbruster
2016-05-31 14:51                         ` Eduardo Habkost
2016-06-03 11:38                           ` David Hildenbrand
2016-05-06 18:11 ` [Qemu-devel] [PATCH 8/9] target-i386: Use "-" instead of "_" on all feature names Eduardo Habkost
2016-05-10 15:19   ` Igor Mammedov
2016-05-10 17:36     ` Jiri Denemark
2016-05-24 12:17   ` Igor Mammedov
2016-05-24 12:34     ` Eduardo Habkost
2016-05-24 13:22       ` Igor Mammedov
2016-05-27 20:32         ` Eduardo Habkost
2016-05-30  8:43           ` Igor Mammedov
2016-05-06 18:11 ` [Qemu-devel] [PATCH 9/9] target-i386: Return runnability information on query-cpu-definitions Eduardo Habkost
2016-05-09  7:24 ` [Qemu-devel] [PATCH 0/9] Add runnability info to query-cpu-definitions David Hildenbrand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1462558292-2126-5-git-send-email-ehabkost@redhat.com \
    --to=ehabkost@redhat.com \
    --cc=afaerber@suse.de \
    --cc=imammedo@redhat.com \
    --cc=jdenemar@redhat.com \
    --cc=libvir-list@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.