diff mbox series

[v4,02/33] hw/cpu: Call object_class_is_abstract() once in cpu_class_by_name()

Message ID 20231102002500.1750692-3-gshan@redhat.com
State New
Headers show
Series [v4,01/33] target/alpha: Tidy up alpha_cpu_class_by_name() | expand

Commit Message

Gavin Shan Nov. 2, 2023, 12:24 a.m. UTC
From: Philippe Mathieu-Daudé <philmd@linaro.org>

Let CPUClass::class_by_name() handlers to return abstract classes,
and filter them once in the public cpu_class_by_name() method.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Gavin Shan <gshan@redhat.com>
---
 hw/core/cpu-common.c   | 8 +++++++-
 include/hw/core/cpu.h  | 7 ++++---
 target/alpha/cpu.c     | 2 +-
 target/arm/cpu.c       | 3 +--
 target/avr/cpu.c       | 3 +--
 target/cris/cpu.c      | 3 +--
 target/hexagon/cpu.c   | 3 +--
 target/loongarch/cpu.c | 3 +--
 target/m68k/cpu.c      | 3 +--
 target/openrisc/cpu.c  | 3 +--
 target/riscv/cpu.c     | 3 +--
 target/rx/cpu.c        | 5 +----
 target/sh4/cpu.c       | 3 ---
 target/tricore/cpu.c   | 3 +--
 target/xtensa/cpu.c    | 3 +--
 15 files changed, 23 insertions(+), 32 deletions(-)

Comments

Igor Mammedov Nov. 6, 2023, 2:40 p.m. UTC | #1
On Thu,  2 Nov 2023 10:24:29 +1000
Gavin Shan <gshan@redhat.com> wrote:

> From: Philippe Mathieu-Daudé <philmd@linaro.org>
> 
> Let CPUClass::class_by_name() handlers to return abstract classes,
> and filter them once in the public cpu_class_by_name() method.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Reviewed-by: Gavin Shan <gshan@redhat.com>
> ---
>  hw/core/cpu-common.c   | 8 +++++++-
>  include/hw/core/cpu.h  | 7 ++++---
>  target/alpha/cpu.c     | 2 +-
>  target/arm/cpu.c       | 3 +--
>  target/avr/cpu.c       | 3 +--
>  target/cris/cpu.c      | 3 +--
>  target/hexagon/cpu.c   | 3 +--
>  target/loongarch/cpu.c | 3 +--
>  target/m68k/cpu.c      | 3 +--
>  target/openrisc/cpu.c  | 3 +--
>  target/riscv/cpu.c     | 3 +--
>  target/rx/cpu.c        | 5 +----
>  target/sh4/cpu.c       | 3 ---
>  target/tricore/cpu.c   | 3 +--
>  target/xtensa/cpu.c    | 3 +--
>  15 files changed, 23 insertions(+), 32 deletions(-)
> 
> diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c
> index bab8942c30..bca0323e9f 100644
> --- a/hw/core/cpu-common.c
> +++ b/hw/core/cpu-common.c
> @@ -150,9 +150,15 @@ static bool cpu_common_has_work(CPUState *cs)
>  ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
>  {
>      CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
> +    ObjectClass *oc;
>  
>      assert(cpu_model && cc->class_by_name);
> -    return cc->class_by_name(cpu_model);
> +    oc = cc->class_by_name(cpu_model);
> +    if (oc && !object_class_is_abstract(oc)) {
> +        return oc;
> +    }
> +
> +    return NULL;
>  }
>  
>  static void cpu_common_parse_features(const char *typename, char *features,
> diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
> index 18593db5b2..ee85aafdf5 100644
> --- a/include/hw/core/cpu.h
> +++ b/include/hw/core/cpu.h
> @@ -102,7 +102,7 @@ struct SysemuCPUOps;
>  /**
>   * CPUClass:
>   * @class_by_name: Callback to map -cpu command line model name to an
> - * instantiatable CPU type.
> + *                 instantiatable CPU type.
>   * @parse_features: Callback to parse command line arguments.
>   * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
>   * @has_work: Callback for checking if there is work to do.
> @@ -772,9 +772,10 @@ void cpu_reset(CPUState *cpu);
>   * @typename: The CPU base type.
>   * @cpu_model: The model string without any parameters.
>   *
> - * Looks up a CPU #ObjectClass matching name @cpu_model.
> + * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
>   *
> - * Returns: A #CPUClass or %NULL if not matching class is found.
> + * Returns: A concrete #CPUClass or %NULL if no matching class is found
> + *          or if the matching class is abstract.
>   */
>  ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
>  
> diff --git a/target/alpha/cpu.c b/target/alpha/cpu.c
> index c7ae4d6a41..9436859c7b 100644
> --- a/target/alpha/cpu.c
> +++ b/target/alpha/cpu.c
> @@ -126,7 +126,7 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
>      int i;
>  
>      oc = object_class_by_name(cpu_model);
> -    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) != NULL &&
I'd split 'oc != NULL &&' into a separate patch 

> +    if (object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) &&
>          !object_class_is_abstract(oc)) {

stray abstract check leftover??

>          return oc;
>      }
> diff --git a/target/arm/cpu.c b/target/arm/cpu.c
> index 954328d72a..8c622d6b59 100644
> --- a/target/arm/cpu.c
> +++ b/target/arm/cpu.c
> @@ -2399,8 +2399,7 @@ static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
>      oc = object_class_by_name(typename);
>      g_strfreev(cpuname);
>      g_free(typename);
> -    if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
> -        object_class_is_abstract(oc)) {
> +    if (!object_class_dynamic_cast(oc, TYPE_ARM_CPU)) {
>          return NULL;
>      }
>      return oc;
> diff --git a/target/avr/cpu.c b/target/avr/cpu.c
> index 14d8b9d1f0..113d522f75 100644
> --- a/target/avr/cpu.c
> +++ b/target/avr/cpu.c
> @@ -157,8 +157,7 @@ static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
>      ObjectClass *oc;
>  
>      oc = object_class_by_name(cpu_model);
> -    if (object_class_dynamic_cast(oc, TYPE_AVR_CPU) == NULL ||
> -        object_class_is_abstract(oc)) {
> +    if (!object_class_dynamic_cast(oc, TYPE_AVR_CPU)) {
>          oc = NULL;
>      }
>      return oc;
> diff --git a/target/cris/cpu.c b/target/cris/cpu.c
> index be4a44c218..1cb431cd46 100644
> --- a/target/cris/cpu.c
> +++ b/target/cris/cpu.c
> @@ -95,8 +95,7 @@ static ObjectClass *cris_cpu_class_by_name(const char *cpu_model)
>      typename = g_strdup_printf(CRIS_CPU_TYPE_NAME("%s"), cpu_model);
>      oc = object_class_by_name(typename);
>      g_free(typename);
> -    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU) ||
> -                       object_class_is_abstract(oc))) {
> +    if (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU)) {
>          oc = NULL;
>      }
>      return oc;
> diff --git a/target/hexagon/cpu.c b/target/hexagon/cpu.c
> index 1adc11b713..bd5adb7acd 100644
> --- a/target/hexagon/cpu.c
> +++ b/target/hexagon/cpu.c
> @@ -63,8 +63,7 @@ static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
>      oc = object_class_by_name(typename);
>      g_strfreev(cpuname);
>      g_free(typename);
> -    if (!oc || !object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU) ||
> -        object_class_is_abstract(oc)) {
> +    if (!object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU)) {
>          return NULL;
>      }
>      return oc;
> diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
> index ef1bf89dac..06d1b9bb95 100644
> --- a/target/loongarch/cpu.c
> +++ b/target/loongarch/cpu.c
> @@ -648,8 +648,7 @@ static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
>          }
>      }
>  
> -    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)
> -        && !object_class_is_abstract(oc)) {
> +    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)) {
>          return oc;
>      }
>      return NULL;
> diff --git a/target/m68k/cpu.c b/target/m68k/cpu.c
> index 538d9473c2..fe381cc5d3 100644
> --- a/target/m68k/cpu.c
> +++ b/target/m68k/cpu.c
> @@ -111,8 +111,7 @@ static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
>      typename = g_strdup_printf(M68K_CPU_TYPE_NAME("%s"), cpu_model);
>      oc = object_class_by_name(typename);
>      g_free(typename);
> -    if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
> -                       object_class_is_abstract(oc))) {
> +    if (!object_class_dynamic_cast(oc, TYPE_M68K_CPU)) {
>          return NULL;
>      }
>      return oc;
> diff --git a/target/openrisc/cpu.c b/target/openrisc/cpu.c
> index f5a3d5273b..cc94f37e77 100644
> --- a/target/openrisc/cpu.c
> +++ b/target/openrisc/cpu.c
> @@ -164,8 +164,7 @@ static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
>      typename = g_strdup_printf(OPENRISC_CPU_TYPE_NAME("%s"), cpu_model);
>      oc = object_class_by_name(typename);
>      g_free(typename);
> -    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU) ||
> -                       object_class_is_abstract(oc))) {
> +    if (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {
>          return NULL;
>      }
>      return oc;
> diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
> index ac4a6c7eec..018bad6f82 100644
> --- a/target/riscv/cpu.c
> +++ b/target/riscv/cpu.c
> @@ -636,8 +636,7 @@ static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
>      oc = object_class_by_name(typename);
>      g_strfreev(cpuname);
>      g_free(typename);
> -    if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) ||
> -        object_class_is_abstract(oc)) {
> +    if (!object_class_dynamic_cast(oc, TYPE_RISCV_CPU)) {
>          return NULL;
>      }
>      return oc;
> diff --git a/target/rx/cpu.c b/target/rx/cpu.c
> index 4d0d3a0c8c..0063837e93 100644
> --- a/target/rx/cpu.c
> +++ b/target/rx/cpu.c
> @@ -111,16 +111,13 @@ static ObjectClass *rx_cpu_class_by_name(const char *cpu_model)
>      char *typename;
>  
>      oc = object_class_by_name(cpu_model);
> -    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL &&
> +    if (object_class_dynamic_cast(oc, TYPE_RX_CPU) &&
>          !object_class_is_abstract(oc)) {

ditto wrt strays

>          return oc;
>      }
>      typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model);
>      oc = object_class_by_name(typename);
>      g_free(typename);
> -    if (oc != NULL && object_class_is_abstract(oc)) {
> -        oc = NULL;
> -    }
>  
>      return oc;
>  }
> diff --git a/target/sh4/cpu.c b/target/sh4/cpu.c
> index 788e41fea6..a8ec98b134 100644
> --- a/target/sh4/cpu.c
> +++ b/target/sh4/cpu.c
> @@ -152,9 +152,6 @@ static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
>  
>      typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
>      oc = object_class_by_name(typename);
> -    if (oc != NULL && object_class_is_abstract(oc)) {
> -        oc = NULL;
> -    }

Why they do not do 
    object_class_dynamic_cast(oc, TYPE_ABSTRACT_FOO_CPU) 
here, do we really need this dynamic cast elsewhere at all?

>  out:
>      g_free(s);
> diff --git a/target/tricore/cpu.c b/target/tricore/cpu.c
> index 5ca666ee12..47e1c272cf 100644
> --- a/target/tricore/cpu.c
> +++ b/target/tricore/cpu.c
> @@ -132,8 +132,7 @@ static ObjectClass *tricore_cpu_class_by_name(const char *cpu_model)
>      typename = g_strdup_printf(TRICORE_CPU_TYPE_NAME("%s"), cpu_model);
>      oc = object_class_by_name(typename);
>      g_free(typename);
> -    if (!oc || !object_class_dynamic_cast(oc, TYPE_TRICORE_CPU) ||
> -        object_class_is_abstract(oc)) {
> +    if (!object_class_dynamic_cast(oc, TYPE_TRICORE_CPU)) {
>          return NULL;
>      }
>      return oc;
> diff --git a/target/xtensa/cpu.c b/target/xtensa/cpu.c
> index ea1dae7390..5d1c090467 100644
> --- a/target/xtensa/cpu.c
> +++ b/target/xtensa/cpu.c
> @@ -141,8 +141,7 @@ static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model)
>      typename = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), cpu_model);
>      oc = object_class_by_name(typename);
>      g_free(typename);
> -    if (oc == NULL || !object_class_dynamic_cast(oc, TYPE_XTENSA_CPU) ||
> -        object_class_is_abstract(oc)) {
> +    if (!object_class_dynamic_cast(oc, TYPE_XTENSA_CPU)) {
>          return NULL;
>      }
>      return oc;
Gavin Shan Nov. 13, 2023, 11:46 p.m. UTC | #2
On 11/7/23 00:40, Igor Mammedov wrote:
> On Thu,  2 Nov 2023 10:24:29 +1000
> Gavin Shan <gshan@redhat.com> wrote:
> 
>> From: Philippe Mathieu-Daudé <philmd@linaro.org>
>>
>> Let CPUClass::class_by_name() handlers to return abstract classes,
>> and filter them once in the public cpu_class_by_name() method.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> Reviewed-by: Gavin Shan <gshan@redhat.com>
>> ---
>>   hw/core/cpu-common.c   | 8 +++++++-
>>   include/hw/core/cpu.h  | 7 ++++---
>>   target/alpha/cpu.c     | 2 +-
>>   target/arm/cpu.c       | 3 +--
>>   target/avr/cpu.c       | 3 +--
>>   target/cris/cpu.c      | 3 +--
>>   target/hexagon/cpu.c   | 3 +--
>>   target/loongarch/cpu.c | 3 +--
>>   target/m68k/cpu.c      | 3 +--
>>   target/openrisc/cpu.c  | 3 +--
>>   target/riscv/cpu.c     | 3 +--
>>   target/rx/cpu.c        | 5 +----
>>   target/sh4/cpu.c       | 3 ---
>>   target/tricore/cpu.c   | 3 +--
>>   target/xtensa/cpu.c    | 3 +--
>>   15 files changed, 23 insertions(+), 32 deletions(-)
>>
>> diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c
>> index bab8942c30..bca0323e9f 100644
>> --- a/hw/core/cpu-common.c
>> +++ b/hw/core/cpu-common.c
>> @@ -150,9 +150,15 @@ static bool cpu_common_has_work(CPUState *cs)
>>   ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
>>   {
>>       CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
>> +    ObjectClass *oc;
>>   
>>       assert(cpu_model && cc->class_by_name);
>> -    return cc->class_by_name(cpu_model);
>> +    oc = cc->class_by_name(cpu_model);
>> +    if (oc && !object_class_is_abstract(oc)) {
>> +        return oc;
>> +    }
>> +
>> +    return NULL;
>>   }
>>   
>>   static void cpu_common_parse_features(const char *typename, char *features,
>> diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
>> index 18593db5b2..ee85aafdf5 100644
>> --- a/include/hw/core/cpu.h
>> +++ b/include/hw/core/cpu.h
>> @@ -102,7 +102,7 @@ struct SysemuCPUOps;
>>   /**
>>    * CPUClass:
>>    * @class_by_name: Callback to map -cpu command line model name to an
>> - * instantiatable CPU type.
>> + *                 instantiatable CPU type.
>>    * @parse_features: Callback to parse command line arguments.
>>    * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
>>    * @has_work: Callback for checking if there is work to do.
>> @@ -772,9 +772,10 @@ void cpu_reset(CPUState *cpu);
>>    * @typename: The CPU base type.
>>    * @cpu_model: The model string without any parameters.
>>    *
>> - * Looks up a CPU #ObjectClass matching name @cpu_model.
>> + * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
>>    *
>> - * Returns: A #CPUClass or %NULL if not matching class is found.
>> + * Returns: A concrete #CPUClass or %NULL if no matching class is found
>> + *          or if the matching class is abstract.
>>    */
>>   ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
>>   
>> diff --git a/target/alpha/cpu.c b/target/alpha/cpu.c
>> index c7ae4d6a41..9436859c7b 100644
>> --- a/target/alpha/cpu.c
>> +++ b/target/alpha/cpu.c
>> @@ -126,7 +126,7 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
>>       int i;
>>   
>>       oc = object_class_by_name(cpu_model);
>> -    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) != NULL &&
> I'd split 'oc != NULL &&' into a separate patch
> 

Agree. It's a good idea, but this patch has been merged as:

3a9d0d7b64 hw/cpu: Call object_class_is_abstract() once in cpu_class_by_name()

>> +    if (object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) &&
>>           !object_class_is_abstract(oc)) {
> 
> stray abstract check leftover??
> 

Nope, It's intentional. We will fall back to @alpha_cpu_aliases if the
CPU class corresponding to @cpu_model is abstract.

>>           return oc;
>>       }
>> diff --git a/target/arm/cpu.c b/target/arm/cpu.c
>> index 954328d72a..8c622d6b59 100644
>> --- a/target/arm/cpu.c
>> +++ b/target/arm/cpu.c
>> @@ -2399,8 +2399,7 @@ static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
>>       oc = object_class_by_name(typename);
>>       g_strfreev(cpuname);
>>       g_free(typename);
>> -    if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
>> -        object_class_is_abstract(oc)) {
>> +    if (!object_class_dynamic_cast(oc, TYPE_ARM_CPU)) {
>>           return NULL;
>>       }
>>       return oc;
>> diff --git a/target/avr/cpu.c b/target/avr/cpu.c
>> index 14d8b9d1f0..113d522f75 100644
>> --- a/target/avr/cpu.c
>> +++ b/target/avr/cpu.c
>> @@ -157,8 +157,7 @@ static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
>>       ObjectClass *oc;
>>   
>>       oc = object_class_by_name(cpu_model);
>> -    if (object_class_dynamic_cast(oc, TYPE_AVR_CPU) == NULL ||
>> -        object_class_is_abstract(oc)) {
>> +    if (!object_class_dynamic_cast(oc, TYPE_AVR_CPU)) {
>>           oc = NULL;
>>       }
>>       return oc;
>> diff --git a/target/cris/cpu.c b/target/cris/cpu.c
>> index be4a44c218..1cb431cd46 100644
>> --- a/target/cris/cpu.c
>> +++ b/target/cris/cpu.c
>> @@ -95,8 +95,7 @@ static ObjectClass *cris_cpu_class_by_name(const char *cpu_model)
>>       typename = g_strdup_printf(CRIS_CPU_TYPE_NAME("%s"), cpu_model);
>>       oc = object_class_by_name(typename);
>>       g_free(typename);
>> -    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU) ||
>> -                       object_class_is_abstract(oc))) {
>> +    if (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU)) {
>>           oc = NULL;
>>       }
>>       return oc;
>> diff --git a/target/hexagon/cpu.c b/target/hexagon/cpu.c
>> index 1adc11b713..bd5adb7acd 100644
>> --- a/target/hexagon/cpu.c
>> +++ b/target/hexagon/cpu.c
>> @@ -63,8 +63,7 @@ static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
>>       oc = object_class_by_name(typename);
>>       g_strfreev(cpuname);
>>       g_free(typename);
>> -    if (!oc || !object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU) ||
>> -        object_class_is_abstract(oc)) {
>> +    if (!object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU)) {
>>           return NULL;
>>       }
>>       return oc;
>> diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
>> index ef1bf89dac..06d1b9bb95 100644
>> --- a/target/loongarch/cpu.c
>> +++ b/target/loongarch/cpu.c
>> @@ -648,8 +648,7 @@ static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
>>           }
>>       }
>>   
>> -    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)
>> -        && !object_class_is_abstract(oc)) {
>> +    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)) {
>>           return oc;
>>       }
>>       return NULL;
>> diff --git a/target/m68k/cpu.c b/target/m68k/cpu.c
>> index 538d9473c2..fe381cc5d3 100644
>> --- a/target/m68k/cpu.c
>> +++ b/target/m68k/cpu.c
>> @@ -111,8 +111,7 @@ static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
>>       typename = g_strdup_printf(M68K_CPU_TYPE_NAME("%s"), cpu_model);
>>       oc = object_class_by_name(typename);
>>       g_free(typename);
>> -    if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
>> -                       object_class_is_abstract(oc))) {
>> +    if (!object_class_dynamic_cast(oc, TYPE_M68K_CPU)) {
>>           return NULL;
>>       }
>>       return oc;
>> diff --git a/target/openrisc/cpu.c b/target/openrisc/cpu.c
>> index f5a3d5273b..cc94f37e77 100644
>> --- a/target/openrisc/cpu.c
>> +++ b/target/openrisc/cpu.c
>> @@ -164,8 +164,7 @@ static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
>>       typename = g_strdup_printf(OPENRISC_CPU_TYPE_NAME("%s"), cpu_model);
>>       oc = object_class_by_name(typename);
>>       g_free(typename);
>> -    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU) ||
>> -                       object_class_is_abstract(oc))) {
>> +    if (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {
>>           return NULL;
>>       }
>>       return oc;
>> diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
>> index ac4a6c7eec..018bad6f82 100644
>> --- a/target/riscv/cpu.c
>> +++ b/target/riscv/cpu.c
>> @@ -636,8 +636,7 @@ static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
>>       oc = object_class_by_name(typename);
>>       g_strfreev(cpuname);
>>       g_free(typename);
>> -    if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) ||
>> -        object_class_is_abstract(oc)) {
>> +    if (!object_class_dynamic_cast(oc, TYPE_RISCV_CPU)) {
>>           return NULL;
>>       }
>>       return oc;
>> diff --git a/target/rx/cpu.c b/target/rx/cpu.c
>> index 4d0d3a0c8c..0063837e93 100644
>> --- a/target/rx/cpu.c
>> +++ b/target/rx/cpu.c
>> @@ -111,16 +111,13 @@ static ObjectClass *rx_cpu_class_by_name(const char *cpu_model)
>>       char *typename;
>>   
>>       oc = object_class_by_name(cpu_model);
>> -    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL &&
>> +    if (object_class_dynamic_cast(oc, TYPE_RX_CPU) &&
>>           !object_class_is_abstract(oc)) {
> 
> ditto wrt strays
> 

Same as above. It's intentional. We will fall back to @cpu_model-rx-cpu if
the CPU class corresponding to @cpu_model is abstract. The point is we don't
want rx_cpu_class_by_name() returns an abstract CPU class in the middle. The
last returned CPU class will be validated by cpu_class_by_name() like below.

ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
{
     :
     if (oc == NULL || object_class_is_abstract(oc)) {
         return NULL;
     }
     return oc;
}


>>           return oc;
>>       }
>>       typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model);
>>       oc = object_class_by_name(typename);
>>       g_free(typename);
>> -    if (oc != NULL && object_class_is_abstract(oc)) {
>> -        oc = NULL;
>> -    }
>>   
>>       return oc;
>>   }
>> diff --git a/target/sh4/cpu.c b/target/sh4/cpu.c
>> index 788e41fea6..a8ec98b134 100644
>> --- a/target/sh4/cpu.c
>> +++ b/target/sh4/cpu.c
>> @@ -152,9 +152,6 @@ static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
>>   
>>       typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
>>       oc = object_class_by_name(typename);
>> -    if (oc != NULL && object_class_is_abstract(oc)) {
>> -        oc = NULL;
>> -    }
> 
> Why they do not do
>      object_class_dynamic_cast(oc, TYPE_ABSTRACT_FOO_CPU)
> here, do we really need this dynamic cast elsewhere at all?
> 

I think it was missed at the beginning. We need to ensure the class is a CPU class
here. It's not harmful at least.

>>   out:
>>       g_free(s);
>> diff --git a/target/tricore/cpu.c b/target/tricore/cpu.c
>> index 5ca666ee12..47e1c272cf 100644
>> --- a/target/tricore/cpu.c
>> +++ b/target/tricore/cpu.c
>> @@ -132,8 +132,7 @@ static ObjectClass *tricore_cpu_class_by_name(const char *cpu_model)
>>       typename = g_strdup_printf(TRICORE_CPU_TYPE_NAME("%s"), cpu_model);
>>       oc = object_class_by_name(typename);
>>       g_free(typename);
>> -    if (!oc || !object_class_dynamic_cast(oc, TYPE_TRICORE_CPU) ||
>> -        object_class_is_abstract(oc)) {
>> +    if (!object_class_dynamic_cast(oc, TYPE_TRICORE_CPU)) {
>>           return NULL;
>>       }
>>       return oc;
>> diff --git a/target/xtensa/cpu.c b/target/xtensa/cpu.c
>> index ea1dae7390..5d1c090467 100644
>> --- a/target/xtensa/cpu.c
>> +++ b/target/xtensa/cpu.c
>> @@ -141,8 +141,7 @@ static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model)
>>       typename = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), cpu_model);
>>       oc = object_class_by_name(typename);
>>       g_free(typename);
>> -    if (oc == NULL || !object_class_dynamic_cast(oc, TYPE_XTENSA_CPU) ||
>> -        object_class_is_abstract(oc)) {
>> +    if (!object_class_dynamic_cast(oc, TYPE_XTENSA_CPU)) {
>>           return NULL;
>>       }
>>       return oc;
> 

Thanks,
Gavin
diff mbox series

Patch

diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c
index bab8942c30..bca0323e9f 100644
--- a/hw/core/cpu-common.c
+++ b/hw/core/cpu-common.c
@@ -150,9 +150,15 @@  static bool cpu_common_has_work(CPUState *cs)
 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
 {
     CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
+    ObjectClass *oc;
 
     assert(cpu_model && cc->class_by_name);
-    return cc->class_by_name(cpu_model);
+    oc = cc->class_by_name(cpu_model);
+    if (oc && !object_class_is_abstract(oc)) {
+        return oc;
+    }
+
+    return NULL;
 }
 
 static void cpu_common_parse_features(const char *typename, char *features,
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index 18593db5b2..ee85aafdf5 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -102,7 +102,7 @@  struct SysemuCPUOps;
 /**
  * CPUClass:
  * @class_by_name: Callback to map -cpu command line model name to an
- * instantiatable CPU type.
+ *                 instantiatable CPU type.
  * @parse_features: Callback to parse command line arguments.
  * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
  * @has_work: Callback for checking if there is work to do.
@@ -772,9 +772,10 @@  void cpu_reset(CPUState *cpu);
  * @typename: The CPU base type.
  * @cpu_model: The model string without any parameters.
  *
- * Looks up a CPU #ObjectClass matching name @cpu_model.
+ * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
  *
- * Returns: A #CPUClass or %NULL if not matching class is found.
+ * Returns: A concrete #CPUClass or %NULL if no matching class is found
+ *          or if the matching class is abstract.
  */
 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
 
diff --git a/target/alpha/cpu.c b/target/alpha/cpu.c
index c7ae4d6a41..9436859c7b 100644
--- a/target/alpha/cpu.c
+++ b/target/alpha/cpu.c
@@ -126,7 +126,7 @@  static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
     int i;
 
     oc = object_class_by_name(cpu_model);
-    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) != NULL &&
+    if (object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) &&
         !object_class_is_abstract(oc)) {
         return oc;
     }
diff --git a/target/arm/cpu.c b/target/arm/cpu.c
index 954328d72a..8c622d6b59 100644
--- a/target/arm/cpu.c
+++ b/target/arm/cpu.c
@@ -2399,8 +2399,7 @@  static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_ARM_CPU)) {
         return NULL;
     }
     return oc;
diff --git a/target/avr/cpu.c b/target/avr/cpu.c
index 14d8b9d1f0..113d522f75 100644
--- a/target/avr/cpu.c
+++ b/target/avr/cpu.c
@@ -157,8 +157,7 @@  static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
     ObjectClass *oc;
 
     oc = object_class_by_name(cpu_model);
-    if (object_class_dynamic_cast(oc, TYPE_AVR_CPU) == NULL ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_AVR_CPU)) {
         oc = NULL;
     }
     return oc;
diff --git a/target/cris/cpu.c b/target/cris/cpu.c
index be4a44c218..1cb431cd46 100644
--- a/target/cris/cpu.c
+++ b/target/cris/cpu.c
@@ -95,8 +95,7 @@  static ObjectClass *cris_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(CRIS_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU) ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU)) {
         oc = NULL;
     }
     return oc;
diff --git a/target/hexagon/cpu.c b/target/hexagon/cpu.c
index 1adc11b713..bd5adb7acd 100644
--- a/target/hexagon/cpu.c
+++ b/target/hexagon/cpu.c
@@ -63,8 +63,7 @@  static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU)) {
         return NULL;
     }
     return oc;
diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
index ef1bf89dac..06d1b9bb95 100644
--- a/target/loongarch/cpu.c
+++ b/target/loongarch/cpu.c
@@ -648,8 +648,7 @@  static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
         }
     }
 
-    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)
-        && !object_class_is_abstract(oc)) {
+    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)) {
         return oc;
     }
     return NULL;
diff --git a/target/m68k/cpu.c b/target/m68k/cpu.c
index 538d9473c2..fe381cc5d3 100644
--- a/target/m68k/cpu.c
+++ b/target/m68k/cpu.c
@@ -111,8 +111,7 @@  static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(M68K_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_M68K_CPU)) {
         return NULL;
     }
     return oc;
diff --git a/target/openrisc/cpu.c b/target/openrisc/cpu.c
index f5a3d5273b..cc94f37e77 100644
--- a/target/openrisc/cpu.c
+++ b/target/openrisc/cpu.c
@@ -164,8 +164,7 @@  static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(OPENRISC_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU) ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {
         return NULL;
     }
     return oc;
diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
index ac4a6c7eec..018bad6f82 100644
--- a/target/riscv/cpu.c
+++ b/target/riscv/cpu.c
@@ -636,8 +636,7 @@  static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_RISCV_CPU)) {
         return NULL;
     }
     return oc;
diff --git a/target/rx/cpu.c b/target/rx/cpu.c
index 4d0d3a0c8c..0063837e93 100644
--- a/target/rx/cpu.c
+++ b/target/rx/cpu.c
@@ -111,16 +111,13 @@  static ObjectClass *rx_cpu_class_by_name(const char *cpu_model)
     char *typename;
 
     oc = object_class_by_name(cpu_model);
-    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL &&
+    if (object_class_dynamic_cast(oc, TYPE_RX_CPU) &&
         !object_class_is_abstract(oc)) {
         return oc;
     }
     typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
 
     return oc;
 }
diff --git a/target/sh4/cpu.c b/target/sh4/cpu.c
index 788e41fea6..a8ec98b134 100644
--- a/target/sh4/cpu.c
+++ b/target/sh4/cpu.c
@@ -152,9 +152,6 @@  static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
 
     typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
     oc = object_class_by_name(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
 
 out:
     g_free(s);
diff --git a/target/tricore/cpu.c b/target/tricore/cpu.c
index 5ca666ee12..47e1c272cf 100644
--- a/target/tricore/cpu.c
+++ b/target/tricore/cpu.c
@@ -132,8 +132,7 @@  static ObjectClass *tricore_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(TRICORE_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_TRICORE_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_TRICORE_CPU)) {
         return NULL;
     }
     return oc;
diff --git a/target/xtensa/cpu.c b/target/xtensa/cpu.c
index ea1dae7390..5d1c090467 100644
--- a/target/xtensa/cpu.c
+++ b/target/xtensa/cpu.c
@@ -141,8 +141,7 @@  static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc == NULL || !object_class_dynamic_cast(oc, TYPE_XTENSA_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_XTENSA_CPU)) {
         return NULL;
     }
     return oc;