[PATCH v5 06/31] cpu: Add generic cpu_list()

Gavin Shan posted 31 patches 1 year ago
Maintainers: Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>, Richard Henderson <richard.henderson@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, Peter Maydell <peter.maydell@linaro.org>, Beniamino Galvani <b.galvani@gmail.com>, Strahinja Jankovic <strahinja.p.jankovic@gmail.com>, Subbaraya Sundeep <sundeep.lkml@gmail.com>, Tyrone Ting <kfting@nuvoton.com>, Hao Wu <wuhaotsh@google.com>, Niek Linnenbank <nieklinnenbank@gmail.com>, Radoslaw Biernacki <rad@semihalf.com>, Leif Lindholm <quic_llindhol@quicinc.com>, Marcin Juszkiewicz <marcin.juszkiewicz@linaro.org>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Yanan Wang <wangyanan55@huawei.com>, Laurent Vivier <laurent@vivier.eu>, Vijai Kumar K <vijai@behindbytes.com>, Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Bin Meng <bin.meng@windriver.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <dbarboza@ventanamicro.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Michael Rolnik <mrolnik@gmail.com>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Brian Cain <bcain@quicinc.com>, Song Gao <gaosong@loongson.cn>, Aurelien Jarno <aurelien@aurel32.net>, Jiaxun Yang <jiaxun.yang@flygoat.com>, Aleksandar Rikalo <aleksandar.rikalo@syrmia.com>, Huacai Chen <chenhuacai@kernel.org>, Stafford Horne <shorne@gmail.com>, Nicholas Piggin <npiggin@gmail.com>, "Cédric Le Goater" <clg@kaod.org>, Yoshinori Sato <ysato@users.sourceforge.jp>, Bastian Koppelmann <kbastian@mail.uni-paderborn.de>, Max Filippov <jcmvbkbc@gmail.com>
There is a newer version of this series
[PATCH v5 06/31] cpu: Add generic cpu_list()
Posted by Gavin Shan 1 year ago
Add generic cpu_list() to replace the individual target's implementation
in the subsequent commits. Currently, there are 3 targets with no cpu_list()
implementation: microblaze and nios2. With this applied, those two targets
switch to the generic cpu_list().

[gshan@gshan q]$ ./build/qemu-system-microblaze -cpu ?
Available CPUs:
  microblaze-cpu

[gshan@gshan q]$ ./build/qemu-system-nios2 -cpu ?
Available CPUs:
  nios2-cpu

Suggested-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Gavin Shan <gshan@redhat.com>
---
 bsd-user/main.c |  5 +----
 cpu-target.c    | 29 ++++++++++++++++++++++++++---
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index e6014f517e..4de226d211 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -378,10 +378,7 @@ int main(int argc, char **argv)
         } else if (!strcmp(r, "cpu")) {
             cpu_model = argv[optind++];
             if (is_help_option(cpu_model)) {
-                /* XXX: implement xxx_cpu_list for targets that still miss it */
-#if defined(cpu_list)
-                cpu_list();
-#endif
+                list_cpus();
                 exit(1);
             }
         } else if (!strcmp(r, "B")) {
diff --git a/cpu-target.c b/cpu-target.c
index c078c0e91b..acfc654b95 100644
--- a/cpu-target.c
+++ b/cpu-target.c
@@ -24,6 +24,7 @@
 #include "hw/qdev-core.h"
 #include "hw/qdev-properties.h"
 #include "qemu/error-report.h"
+#include "qemu/qemu-print.h"
 #include "migration/vmstate.h"
 #ifdef CONFIG_USER_ONLY
 #include "qemu.h"
@@ -283,12 +284,34 @@ const char *parse_cpu_option(const char *cpu_option)
     return cpu_type;
 }
 
+#ifndef cpu_list
+static void cpu_list_entry(gpointer data, gpointer user_data)
+{
+    CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    g_autofree char *model = cpu_model_from_type(typename);
+
+    if (cc->deprecation_note) {
+        qemu_printf("  %s (deprecated)\n", model);
+    } else {
+        qemu_printf("  %s\n", model);
+    }
+}
+
+static void cpu_list(void)
+{
+    GSList *list;
+
+    list = object_class_get_list_sorted(TYPE_CPU, false);
+    qemu_printf("Available CPUs:\n");
+    g_slist_foreach(list, cpu_list_entry, NULL);
+    g_slist_free(list);
+}
+#endif
+
 void list_cpus(void)
 {
-    /* XXX: implement xxx_cpu_list for targets that still miss it */
-#if defined(cpu_list)
     cpu_list();
-#endif
 }
 
 #if defined(CONFIG_USER_ONLY)
-- 
2.41.0
Re: [PATCH v5 06/31] cpu: Add generic cpu_list()
Posted by Philippe Mathieu-Daudé 1 year ago
Hi Gavin,

On 15/11/23 00:56, Gavin Shan wrote:
> Add generic cpu_list() to replace the individual target's implementation
> in the subsequent commits. Currently, there are 3 targets with no cpu_list()
> implementation: microblaze and nios2. With this applied, those two targets
> switch to the generic cpu_list().
> 
> [gshan@gshan q]$ ./build/qemu-system-microblaze -cpu ?
> Available CPUs:
>    microblaze-cpu
> 
> [gshan@gshan q]$ ./build/qemu-system-nios2 -cpu ?
> Available CPUs:
>    nios2-cpu
> 
> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
> Signed-off-by: Gavin Shan <gshan@redhat.com>
> ---
>   bsd-user/main.c |  5 +----
>   cpu-target.c    | 29 ++++++++++++++++++++++++++---
>   2 files changed, 27 insertions(+), 7 deletions(-)


> diff --git a/cpu-target.c b/cpu-target.c
> index c078c0e91b..acfc654b95 100644
> --- a/cpu-target.c
> +++ b/cpu-target.c
> @@ -24,6 +24,7 @@
>   #include "hw/qdev-core.h"
>   #include "hw/qdev-properties.h"
>   #include "qemu/error-report.h"
> +#include "qemu/qemu-print.h"
>   #include "migration/vmstate.h"
>   #ifdef CONFIG_USER_ONLY
>   #include "qemu.h"
> @@ -283,12 +284,34 @@ const char *parse_cpu_option(const char *cpu_option)
>       return cpu_type;
>   }
>   
> +#ifndef cpu_list
> +static void cpu_list_entry(gpointer data, gpointer user_data)
> +{
> +    CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
> +    const char *typename = object_class_get_name(OBJECT_CLASS(data));
> +    g_autofree char *model = cpu_model_from_type(typename);
> +
> +    if (cc->deprecation_note) {
> +        qemu_printf("  %s (deprecated)\n", model);
> +    } else {
> +        qemu_printf("  %s\n", model);
> +    }
> +}
> +
> +static void cpu_list(void)
> +{
> +    GSList *list;
> +
> +    list = object_class_get_list_sorted(TYPE_CPU, false);
> +    qemu_printf("Available CPUs:\n");

Since this output will likely be displayed a lot, IMHO it is worth
doing a first pass to get the number of available CPUs. If it is 1,
print using singular but even better smth like:

        "This machine can only be used with the following CPU:"

That said, this can be done later on top, so:
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>

> +    g_slist_foreach(list, cpu_list_entry, NULL);
> +    g_slist_free(list);
> +}
> +#endif



Re: [PATCH v5 06/31] cpu: Add generic cpu_list()
Posted by Philippe Mathieu-Daudé 1 year ago
On 16/11/23 08:39, Philippe Mathieu-Daudé wrote:
> Hi Gavin,
> 
> On 15/11/23 00:56, Gavin Shan wrote:
>> Add generic cpu_list() to replace the individual target's implementation
>> in the subsequent commits. Currently, there are 3 targets with no 
>> cpu_list()
>> implementation: microblaze and nios2. With this applied, those two 
>> targets
>> switch to the generic cpu_list().
>>
>> [gshan@gshan q]$ ./build/qemu-system-microblaze -cpu ?
>> Available CPUs:
>>    microblaze-cpu
>>
>> [gshan@gshan q]$ ./build/qemu-system-nios2 -cpu ?
>> Available CPUs:
>>    nios2-cpu
>>
>> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
>> Signed-off-by: Gavin Shan <gshan@redhat.com>
>> ---
>>   bsd-user/main.c |  5 +----
>>   cpu-target.c    | 29 ++++++++++++++++++++++++++---
>>   2 files changed, 27 insertions(+), 7 deletions(-)
> 
> 
>> diff --git a/cpu-target.c b/cpu-target.c
>> index c078c0e91b..acfc654b95 100644
>> --- a/cpu-target.c
>> +++ b/cpu-target.c
>> @@ -24,6 +24,7 @@
>>   #include "hw/qdev-core.h"
>>   #include "hw/qdev-properties.h"
>>   #include "qemu/error-report.h"
>> +#include "qemu/qemu-print.h"
>>   #include "migration/vmstate.h"
>>   #ifdef CONFIG_USER_ONLY
>>   #include "qemu.h"
>> @@ -283,12 +284,34 @@ const char *parse_cpu_option(const char 
>> *cpu_option)
>>       return cpu_type;
>>   }
>> +#ifndef cpu_list
>> +static void cpu_list_entry(gpointer data, gpointer user_data)
>> +{
>> +    CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
>> +    const char *typename = object_class_get_name(OBJECT_CLASS(data));
>> +    g_autofree char *model = cpu_model_from_type(typename);
>> +
>> +    if (cc->deprecation_note) {
>> +        qemu_printf("  %s (deprecated)\n", model);
>> +    } else {
>> +        qemu_printf("  %s\n", model);
>> +    }
>> +}
>> +
>> +static void cpu_list(void)
>> +{
>> +    GSList *list;
>> +
>> +    list = object_class_get_list_sorted(TYPE_CPU, false);
>> +    qemu_printf("Available CPUs:\n");
> 
> Since this output will likely be displayed a lot, IMHO it is worth
> doing a first pass to get the number of available CPUs. If it is 1,
> print using singular but even better smth like:
> 
>         "This machine can only be used with the following CPU:"

Hmm I missed this code is common to user/system emulation.

System helper could be clever by using the intersection of cpu_list()
and MachineClass::valid_cpu_types[] sets.

> That said, this can be done later on top, so:
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> 
>> +    g_slist_foreach(list, cpu_list_entry, NULL);
>> +    g_slist_free(list);
>> +}
>> +#endif
> 
> 


Re: [PATCH v5 06/31] cpu: Add generic cpu_list()
Posted by Gavin Shan 1 year ago
Hi Phil,

On 11/16/23 17:51, Philippe Mathieu-Daudé wrote:
> On 16/11/23 08:39, Philippe Mathieu-Daudé wrote:
>> On 15/11/23 00:56, Gavin Shan wrote:
>>> Add generic cpu_list() to replace the individual target's implementation
>>> in the subsequent commits. Currently, there are 3 targets with no cpu_list()
>>> implementation: microblaze and nios2. With this applied, those two targets
>>> switch to the generic cpu_list().
>>>
>>> [gshan@gshan q]$ ./build/qemu-system-microblaze -cpu ?
>>> Available CPUs:
>>>    microblaze-cpu
>>>
>>> [gshan@gshan q]$ ./build/qemu-system-nios2 -cpu ?
>>> Available CPUs:
>>>    nios2-cpu
>>>
>>> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
>>> Signed-off-by: Gavin Shan <gshan@redhat.com>
>>> ---
>>>   bsd-user/main.c |  5 +----
>>>   cpu-target.c    | 29 ++++++++++++++++++++++++++---
>>>   2 files changed, 27 insertions(+), 7 deletions(-)
>>
>>
>>> diff --git a/cpu-target.c b/cpu-target.c
>>> index c078c0e91b..acfc654b95 100644
>>> --- a/cpu-target.c
>>> +++ b/cpu-target.c
>>> @@ -24,6 +24,7 @@
>>>   #include "hw/qdev-core.h"
>>>   #include "hw/qdev-properties.h"
>>>   #include "qemu/error-report.h"
>>> +#include "qemu/qemu-print.h"
>>>   #include "migration/vmstate.h"
>>>   #ifdef CONFIG_USER_ONLY
>>>   #include "qemu.h"
>>> @@ -283,12 +284,34 @@ const char *parse_cpu_option(const char *cpu_option)
>>>       return cpu_type;
>>>   }
>>> +#ifndef cpu_list
>>> +static void cpu_list_entry(gpointer data, gpointer user_data)
>>> +{
>>> +    CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
>>> +    const char *typename = object_class_get_name(OBJECT_CLASS(data));
>>> +    g_autofree char *model = cpu_model_from_type(typename);
>>> +
>>> +    if (cc->deprecation_note) {
>>> +        qemu_printf("  %s (deprecated)\n", model);
>>> +    } else {
>>> +        qemu_printf("  %s\n", model);
>>> +    }
>>> +}
>>> +
>>> +static void cpu_list(void)
>>> +{
>>> +    GSList *list;
>>> +
>>> +    list = object_class_get_list_sorted(TYPE_CPU, false);
>>> +    qemu_printf("Available CPUs:\n");
>>
>> Since this output will likely be displayed a lot, IMHO it is worth
>> doing a first pass to get the number of available CPUs. If it is 1,
>> print using singular but even better smth like:
>>
>>         "This machine can only be used with the following CPU:"
> 
> Hmm I missed this code is common to user/system emulation.
> 
> System helper could be clever by using the intersection of cpu_list()
> and MachineClass::valid_cpu_types[] sets.
> 

When cpu_list() is called, it's possible the machine type option isn't
parsed yet. Besides, this function is usually used by "qemu-system-arm -cpu ?".
So I wouldn't connect to MachineClass::valid_cpu_types[] here if you agree.

Thanks,
Gavin

>> That said, this can be done later on top, so:
>> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>>
>>> +    g_slist_foreach(list, cpu_list_entry, NULL);
>>> +    g_slist_free(list);
>>> +}
>>> +#endif
>>
>>
> 


Re: [PATCH v5 06/31] cpu: Add generic cpu_list()
Posted by Philippe Mathieu-Daudé 1 year ago
On 16/11/23 11:34, Gavin Shan wrote:
> Hi Phil,
> 
> On 11/16/23 17:51, Philippe Mathieu-Daudé wrote:
>> On 16/11/23 08:39, Philippe Mathieu-Daudé wrote:
>>> On 15/11/23 00:56, Gavin Shan wrote:
>>>> Add generic cpu_list() to replace the individual target's 
>>>> implementation
>>>> in the subsequent commits. Currently, there are 3 targets with no 
>>>> cpu_list()
>>>> implementation: microblaze and nios2. With this applied, those two 
>>>> targets
>>>> switch to the generic cpu_list().
>>>>
>>>> [gshan@gshan q]$ ./build/qemu-system-microblaze -cpu ?
>>>> Available CPUs:
>>>>    microblaze-cpu
>>>>
>>>> [gshan@gshan q]$ ./build/qemu-system-nios2 -cpu ?
>>>> Available CPUs:
>>>>    nios2-cpu
>>>>
>>>> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
>>>> Signed-off-by: Gavin Shan <gshan@redhat.com>
>>>> ---
>>>>   bsd-user/main.c |  5 +----
>>>>   cpu-target.c    | 29 ++++++++++++++++++++++++++---
>>>>   2 files changed, 27 insertions(+), 7 deletions(-)
>>>
>>>
>>>> diff --git a/cpu-target.c b/cpu-target.c
>>>> index c078c0e91b..acfc654b95 100644
>>>> --- a/cpu-target.c
>>>> +++ b/cpu-target.c
>>>> @@ -24,6 +24,7 @@
>>>>   #include "hw/qdev-core.h"
>>>>   #include "hw/qdev-properties.h"
>>>>   #include "qemu/error-report.h"
>>>> +#include "qemu/qemu-print.h"
>>>>   #include "migration/vmstate.h"
>>>>   #ifdef CONFIG_USER_ONLY
>>>>   #include "qemu.h"
>>>> @@ -283,12 +284,34 @@ const char *parse_cpu_option(const char 
>>>> *cpu_option)
>>>>       return cpu_type;
>>>>   }
>>>> +#ifndef cpu_list
>>>> +static void cpu_list_entry(gpointer data, gpointer user_data)
>>>> +{
>>>> +    CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
>>>> +    const char *typename = object_class_get_name(OBJECT_CLASS(data));
>>>> +    g_autofree char *model = cpu_model_from_type(typename);
>>>> +
>>>> +    if (cc->deprecation_note) {
>>>> +        qemu_printf("  %s (deprecated)\n", model);
>>>> +    } else {
>>>> +        qemu_printf("  %s\n", model);
>>>> +    }
>>>> +}
>>>> +
>>>> +static void cpu_list(void)
>>>> +{
>>>> +    GSList *list;
>>>> +
>>>> +    list = object_class_get_list_sorted(TYPE_CPU, false);
>>>> +    qemu_printf("Available CPUs:\n");
>>>
>>> Since this output will likely be displayed a lot, IMHO it is worth
>>> doing a first pass to get the number of available CPUs. If it is 1,
>>> print using singular but even better smth like:
>>>
>>>         "This machine can only be used with the following CPU:"
>>
>> Hmm I missed this code is common to user/system emulation.
>>
>> System helper could be clever by using the intersection of cpu_list()
>> and MachineClass::valid_cpu_types[] sets.
>>
> 
> When cpu_list() is called, it's possible the machine type option isn't
> parsed yet. Besides, this function is usually used by "qemu-system-arm 
> -cpu ?".

Not sure this is a good example :)

   $ qemu-system-arm -cpu ?
   qemu-system-arm: No machine specified, and there is no default
   Use -machine help to list supported machines

> So I wouldn't connect to MachineClass::valid_cpu_types[] here if you agree.

Agreed.


Re: [PATCH v5 06/31] cpu: Add generic cpu_list()
Posted by Philippe Mathieu-Daudé 1 year ago
On 16/11/23 08:51, Philippe Mathieu-Daudé wrote:
> On 16/11/23 08:39, Philippe Mathieu-Daudé wrote:
>> Hi Gavin,
>>
>> On 15/11/23 00:56, Gavin Shan wrote:
>>> Add generic cpu_list() to replace the individual target's implementation
>>> in the subsequent commits. Currently, there are 3 targets with no 
>>> cpu_list()
>>> implementation: microblaze and nios2. With this applied, those two 
>>> targets
>>> switch to the generic cpu_list().
>>>
>>> [gshan@gshan q]$ ./build/qemu-system-microblaze -cpu ?
>>> Available CPUs:
>>>    microblaze-cpu
>>>
>>> [gshan@gshan q]$ ./build/qemu-system-nios2 -cpu ?
>>> Available CPUs:
>>>    nios2-cpu
>>>
>>> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
>>> Signed-off-by: Gavin Shan <gshan@redhat.com>
>>> ---
>>>   bsd-user/main.c |  5 +----
>>>   cpu-target.c    | 29 ++++++++++++++++++++++++++---
>>>   2 files changed, 27 insertions(+), 7 deletions(-)
>>
>>
>>> diff --git a/cpu-target.c b/cpu-target.c
>>> index c078c0e91b..acfc654b95 100644
>>> --- a/cpu-target.c
>>> +++ b/cpu-target.c
>>> @@ -24,6 +24,7 @@
>>>   #include "hw/qdev-core.h"
>>>   #include "hw/qdev-properties.h"
>>>   #include "qemu/error-report.h"
>>> +#include "qemu/qemu-print.h"
>>>   #include "migration/vmstate.h"
>>>   #ifdef CONFIG_USER_ONLY
>>>   #include "qemu.h"
>>> @@ -283,12 +284,34 @@ const char *parse_cpu_option(const char 
>>> *cpu_option)
>>>       return cpu_type;
>>>   }
>>> +#ifndef cpu_list
>>> +static void cpu_list_entry(gpointer data, gpointer user_data)
>>> +{
>>> +    CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
>>> +    const char *typename = object_class_get_name(OBJECT_CLASS(data));
>>> +    g_autofree char *model = cpu_model_from_type(typename);
>>> +
>>> +    if (cc->deprecation_note) {
>>> +        qemu_printf("  %s (deprecated)\n", model);
>>> +    } else {
>>> +        qemu_printf("  %s\n", model);

Wondering how this scale to heterogeneous emulation. Should we
display the architecture, i.e.:

-- >8 --
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index 76ef59de0a..aeff182a37 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -792,6 +792,8 @@ ObjectClass *cpu_class_by_name(const char *typename, 
const char *cpu_model);
   */
  char *cpu_model_from_type(const char *typename);

+char *cpu_arch_from_type(const char *typename);
+
  /**
   * cpu_create:
   * @typename: The CPU type.
diff --git a/cpu-target.c b/cpu-target.c
index acfc654b95..75412f902f 100644
--- a/cpu-target.c
+++ b/cpu-target.c
@@ -257,6 +257,12 @@ char *cpu_model_from_type(const char *typename)
      return g_strdup(typename);
  }

+char *cpu_arch_from_type(const char *typename)
+{
+    assert(g_str_has_suffix(CPU_RESOLVING_TYPE, "-cpu"));
+    return g_strndup(CPU_RESOLVING_TYPE, strlen(CPU_RESOLVING_TYPE) - 
strlen("-cpu"));
+}
+
  const char *parse_cpu_option(const char *cpu_option)
  {
      ObjectClass *oc;
@@ -290,12 +296,11 @@ static void cpu_list_entry(gpointer data, gpointer 
user_data)
      CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
      const char *typename = object_class_get_name(OBJECT_CLASS(data));
      g_autofree char *model = cpu_model_from_type(typename);
+    g_autofree char *arch = cpu_arch_from_type(typename);
+    g_autofree char *arch_up = g_ascii_strup(arch, -1);

-    if (cc->deprecation_note) {
-        qemu_printf("  %s (deprecated)\n", model);
-    } else {
-        qemu_printf("  %s\n", model);
-    }
+    qemu_printf("  %s (%s%s)\n", model, arch_up,
+                cc->deprecation_note ? ", deprecated" : "");
  }
---

Produces:

qemu-system-aarch64 -M mps2-an500 -cpu \?
Available CPUs:
   a64fx (ARM)
   arm1026 (ARM)
   arm1136 (ARM)
   arm1136-r2 (ARM)
   arm1176 (ARM)
   arm11mpcore (ARM)
   arm926 (ARM)
   arm946 (ARM)
   cortex-a15 (ARM)
   cortex-a35 (ARM)
   cortex-a53 (ARM)
   cortex-a55 (ARM)
   cortex-a57 (ARM)
   cortex-a7 (ARM)
   cortex-a710 (ARM)
   cortex-a72 (ARM)
   cortex-a76 (ARM)
   cortex-a8 (ARM)
   cortex-a9 (ARM)
   cortex-m0 (ARM)
   cortex-m3 (ARM)
   cortex-m33 (ARM)
   cortex-m4 (ARM)
   cortex-m55 (ARM)
   cortex-m7 (ARM)
   cortex-r5 (ARM)
   cortex-r52 (ARM)
   cortex-r5f (ARM)
   host (ARM)
   max (ARM)

Bah, host/max are meaningless here. We'll need to filter somehow,
or rename them.

Re: [PATCH v5 06/31] cpu: Add generic cpu_list()
Posted by Philippe Mathieu-Daudé 1 year ago
On 16/11/23 11:19, Philippe Mathieu-Daudé wrote:
> On 16/11/23 08:51, Philippe Mathieu-Daudé wrote:
>> On 16/11/23 08:39, Philippe Mathieu-Daudé wrote:
>>> Hi Gavin,
>>>
>>> On 15/11/23 00:56, Gavin Shan wrote:
>>>> Add generic cpu_list() to replace the individual target's 
>>>> implementation
>>>> in the subsequent commits. Currently, there are 3 targets with no 
>>>> cpu_list()
>>>> implementation: microblaze and nios2. With this applied, those two 
>>>> targets
>>>> switch to the generic cpu_list().
>>>>
>>>> [gshan@gshan q]$ ./build/qemu-system-microblaze -cpu ?
>>>> Available CPUs:
>>>>    microblaze-cpu
>>>>
>>>> [gshan@gshan q]$ ./build/qemu-system-nios2 -cpu ?
>>>> Available CPUs:
>>>>    nios2-cpu
>>>>
>>>> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
>>>> Signed-off-by: Gavin Shan <gshan@redhat.com>
>>>> ---
>>>>   bsd-user/main.c |  5 +----
>>>>   cpu-target.c    | 29 ++++++++++++++++++++++++++---
>>>>   2 files changed, 27 insertions(+), 7 deletions(-)
>>>
>>>
>>>> diff --git a/cpu-target.c b/cpu-target.c
>>>> index c078c0e91b..acfc654b95 100644
>>>> --- a/cpu-target.c
>>>> +++ b/cpu-target.c
>>>> @@ -24,6 +24,7 @@
>>>>   #include "hw/qdev-core.h"
>>>>   #include "hw/qdev-properties.h"
>>>>   #include "qemu/error-report.h"
>>>> +#include "qemu/qemu-print.h"
>>>>   #include "migration/vmstate.h"
>>>>   #ifdef CONFIG_USER_ONLY
>>>>   #include "qemu.h"
>>>> @@ -283,12 +284,34 @@ const char *parse_cpu_option(const char 
>>>> *cpu_option)
>>>>       return cpu_type;
>>>>   }
>>>> +#ifndef cpu_list
>>>> +static void cpu_list_entry(gpointer data, gpointer user_data)
>>>> +{
>>>> +    CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
>>>> +    const char *typename = object_class_get_name(OBJECT_CLASS(data));
>>>> +    g_autofree char *model = cpu_model_from_type(typename);
>>>> +
>>>> +    if (cc->deprecation_note) {
>>>> +        qemu_printf("  %s (deprecated)\n", model);
>>>> +    } else {
>>>> +        qemu_printf("  %s\n", model);
> 
> Wondering how this scale to heterogeneous emulation. Should we
> display the architecture, i.e.:
> 
> -- >8 --
> diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
> index 76ef59de0a..aeff182a37 100644
> --- a/include/hw/core/cpu.h
> +++ b/include/hw/core/cpu.h
> @@ -792,6 +792,8 @@ ObjectClass *cpu_class_by_name(const char *typename, 
> const char *cpu_model);
>    */
>   char *cpu_model_from_type(const char *typename);
> 
> +char *cpu_arch_from_type(const char *typename);
> +
>   /**
>    * cpu_create:
>    * @typename: The CPU type.
> diff --git a/cpu-target.c b/cpu-target.c
> index acfc654b95..75412f902f 100644
> --- a/cpu-target.c
> +++ b/cpu-target.c
> @@ -257,6 +257,12 @@ char *cpu_model_from_type(const char *typename)
>       return g_strdup(typename);
>   }
> 
> +char *cpu_arch_from_type(const char *typename)
> +{
> +    assert(g_str_has_suffix(CPU_RESOLVING_TYPE, "-cpu"));
> +    return g_strndup(CPU_RESOLVING_TYPE, strlen(CPU_RESOLVING_TYPE) - 
> strlen("-cpu"));

Not good enough:

$ ./qemu-system-mips64el -M mps2-an500 -cpu \?
Available CPUs:
   20Kc (MIPS64)
   24Kc (MIPS64)
   24KEc (MIPS64)
   24Kf (MIPS64)
   34Kf (MIPS64)
   4Kc (MIPS64)
   4KEc (MIPS64)
   4KEcR1 (MIPS64)
   4KEm (MIPS64)
   4KEmR1 (MIPS64)
   4Km (MIPS64)
   5Kc (MIPS64)
   5KEc (MIPS64)
   5KEf (MIPS64)
   5Kf (MIPS64)

Anyhow we can't use CPU_RESOLVING_TYPE in heterogeneous context,
so we'll probably have to add the arch as a CPUClass field.

> +}
> +
>   const char *parse_cpu_option(const char *cpu_option)
>   {
>       ObjectClass *oc;
> @@ -290,12 +296,11 @@ static void cpu_list_entry(gpointer data, gpointer 
> user_data)
>       CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
>       const char *typename = object_class_get_name(OBJECT_CLASS(data));
>       g_autofree char *model = cpu_model_from_type(typename);
> +    g_autofree char *arch = cpu_arch_from_type(typename);
> +    g_autofree char *arch_up = g_ascii_strup(arch, -1);
> 
> -    if (cc->deprecation_note) {
> -        qemu_printf("  %s (deprecated)\n", model);
> -    } else {
> -        qemu_printf("  %s\n", model);
> -    }
> +    qemu_printf("  %s (%s%s)\n", model, arch_up,
> +                cc->deprecation_note ? ", deprecated" : "");
>   }
> ---
> 
> Produces:
> 
> qemu-system-aarch64 -M mps2-an500 -cpu \?
> Available CPUs:
>    a64fx (ARM)
>    arm1026 (ARM)
>    arm1136 (ARM)
>    arm1136-r2 (ARM)
>    arm1176 (ARM)
>    arm11mpcore (ARM)
>    arm926 (ARM)
>    arm946 (ARM)
>    cortex-a15 (ARM)
>    cortex-a35 (ARM)
>    cortex-a53 (ARM)
>    cortex-a55 (ARM)
>    cortex-a57 (ARM)
>    cortex-a7 (ARM)
>    cortex-a710 (ARM)
>    cortex-a72 (ARM)
>    cortex-a76 (ARM)


Re: [PATCH v5 06/31] cpu: Add generic cpu_list()
Posted by Gavin Shan 1 year ago
Hi Phil,

On 11/16/23 20:25, Philippe Mathieu-Daudé wrote:
> On 16/11/23 11:19, Philippe Mathieu-Daudé wrote:
>> On 16/11/23 08:51, Philippe Mathieu-Daudé wrote:
>>> On 16/11/23 08:39, Philippe Mathieu-Daudé wrote:
>>>> On 15/11/23 00:56, Gavin Shan wrote:
>>>>> Add generic cpu_list() to replace the individual target's implementation
>>>>> in the subsequent commits. Currently, there are 3 targets with no cpu_list()
>>>>> implementation: microblaze and nios2. With this applied, those two targets
>>>>> switch to the generic cpu_list().
>>>>>
>>>>> [gshan@gshan q]$ ./build/qemu-system-microblaze -cpu ?
>>>>> Available CPUs:
>>>>>    microblaze-cpu
>>>>>
>>>>> [gshan@gshan q]$ ./build/qemu-system-nios2 -cpu ?
>>>>> Available CPUs:
>>>>>    nios2-cpu
>>>>>
>>>>> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
>>>>> Signed-off-by: Gavin Shan <gshan@redhat.com>
>>>>> ---
>>>>>   bsd-user/main.c |  5 +----
>>>>>   cpu-target.c    | 29 ++++++++++++++++++++++++++---
>>>>>   2 files changed, 27 insertions(+), 7 deletions(-)
>>>>
>>>>
>>>>> diff --git a/cpu-target.c b/cpu-target.c
>>>>> index c078c0e91b..acfc654b95 100644
>>>>> --- a/cpu-target.c
>>>>> +++ b/cpu-target.c
>>>>> @@ -24,6 +24,7 @@
>>>>>   #include "hw/qdev-core.h"
>>>>>   #include "hw/qdev-properties.h"
>>>>>   #include "qemu/error-report.h"
>>>>> +#include "qemu/qemu-print.h"
>>>>>   #include "migration/vmstate.h"
>>>>>   #ifdef CONFIG_USER_ONLY
>>>>>   #include "qemu.h"
>>>>> @@ -283,12 +284,34 @@ const char *parse_cpu_option(const char *cpu_option)
>>>>>       return cpu_type;
>>>>>   }
>>>>> +#ifndef cpu_list
>>>>> +static void cpu_list_entry(gpointer data, gpointer user_data)
>>>>> +{
>>>>> +    CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
>>>>> +    const char *typename = object_class_get_name(OBJECT_CLASS(data));
>>>>> +    g_autofree char *model = cpu_model_from_type(typename);
>>>>> +
>>>>> +    if (cc->deprecation_note) {
>>>>> +        qemu_printf("  %s (deprecated)\n", model);
>>>>> +    } else {
>>>>> +        qemu_printf("  %s\n", model);
>>
>> Wondering how this scale to heterogeneous emulation. Should we
>> display the architecture, i.e.:
>>
>> -- >8 --
>> diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
>> index 76ef59de0a..aeff182a37 100644
>> --- a/include/hw/core/cpu.h
>> +++ b/include/hw/core/cpu.h
>> @@ -792,6 +792,8 @@ ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
>>    */
>>   char *cpu_model_from_type(const char *typename);
>>
>> +char *cpu_arch_from_type(const char *typename);
>> +
>>   /**
>>    * cpu_create:
>>    * @typename: The CPU type.
>> diff --git a/cpu-target.c b/cpu-target.c
>> index acfc654b95..75412f902f 100644
>> --- a/cpu-target.c
>> +++ b/cpu-target.c
>> @@ -257,6 +257,12 @@ char *cpu_model_from_type(const char *typename)
>>       return g_strdup(typename);
>>   }
>>
>> +char *cpu_arch_from_type(const char *typename)
>> +{
>> +    assert(g_str_has_suffix(CPU_RESOLVING_TYPE, "-cpu"));
>> +    return g_strndup(CPU_RESOLVING_TYPE, strlen(CPU_RESOLVING_TYPE) - strlen("-cpu"));
> 
> Not good enough:
> 
> $ ./qemu-system-mips64el -M mps2-an500 -cpu \?
> Available CPUs:
>    20Kc (MIPS64)
>    24Kc (MIPS64)
>    24KEc (MIPS64)
>    24Kf (MIPS64)
>    34Kf (MIPS64)
>    4Kc (MIPS64)
>    4KEc (MIPS64)
>    4KEcR1 (MIPS64)
>    4KEm (MIPS64)
>    4KEmR1 (MIPS64)
>    4Km (MIPS64)
>    5Kc (MIPS64)
>    5KEc (MIPS64)
>    5KEf (MIPS64)
>    5Kf (MIPS64)
> 
> Anyhow we can't use CPU_RESOLVING_TYPE in heterogeneous context,
> so we'll probably have to add the arch as a CPUClass field.
> 

CPU_RESOLVING_TYPE has been pinned to one specific target. We need to
improve this to support heterogeneous scenario in the future if you
agree. How about to figure out the improvement in the future to support
heterogeneous case and leave it as what we had?

Thanks,
Gavin

>> +}
>> +
>>   const char *parse_cpu_option(const char *cpu_option)
>>   {
>>       ObjectClass *oc;
>> @@ -290,12 +296,11 @@ static void cpu_list_entry(gpointer data, gpointer user_data)
>>       CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
>>       const char *typename = object_class_get_name(OBJECT_CLASS(data));
>>       g_autofree char *model = cpu_model_from_type(typename);
>> +    g_autofree char *arch = cpu_arch_from_type(typename);
>> +    g_autofree char *arch_up = g_ascii_strup(arch, -1);
>>
>> -    if (cc->deprecation_note) {
>> -        qemu_printf("  %s (deprecated)\n", model);
>> -    } else {
>> -        qemu_printf("  %s\n", model);
>> -    }
>> +    qemu_printf("  %s (%s%s)\n", model, arch_up,
>> +                cc->deprecation_note ? ", deprecated" : "");
>>   }
>> ---
>>
>> Produces:
>>
>> qemu-system-aarch64 -M mps2-an500 -cpu \?
>> Available CPUs:
>>    a64fx (ARM)
>>    arm1026 (ARM)
>>    arm1136 (ARM)
>>    arm1136-r2 (ARM)
>>    arm1176 (ARM)
>>    arm11mpcore (ARM)
>>    arm926 (ARM)
>>    arm946 (ARM)
>>    cortex-a15 (ARM)
>>    cortex-a35 (ARM)
>>    cortex-a53 (ARM)
>>    cortex-a55 (ARM)
>>    cortex-a57 (ARM)
>>    cortex-a7 (ARM)
>>    cortex-a710 (ARM)
>>    cortex-a72 (ARM)
>>    cortex-a76 (ARM)
> 


Re: [PATCH v5 06/31] cpu: Add generic cpu_list()
Posted by Richard Henderson 1 year ago
On 11/14/23 15:56, Gavin Shan wrote:
> Add generic cpu_list() to replace the individual target's implementation
> in the subsequent commits. Currently, there are 3 targets with no cpu_list()
> implementation: microblaze and nios2. With this applied, those two targets
> switch to the generic cpu_list().
> 
> [gshan@gshan q]$ ./build/qemu-system-microblaze -cpu ?
> Available CPUs:
>    microblaze-cpu
> 
> [gshan@gshan q]$ ./build/qemu-system-nios2 -cpu ?
> Available CPUs:
>    nios2-cpu
> 
> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
> Signed-off-by: Gavin Shan <gshan@redhat.com>
> ---
>   bsd-user/main.c |  5 +----
>   cpu-target.c    | 29 ++++++++++++++++++++++++++---
>   2 files changed, 27 insertions(+), 7 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~