qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Igor Mammedov <imammedo@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-devel@nongnu.org, xieyongji@bytedance.com, kwolf@redhat.com,
	hreitz@redhat.com, Coiby.Xu@gmail.com, pbonzini@redhat.com,
	berrange@redhat.com, eduardo@habkost.net,
	mark.cave-ayland@ilande.co.uk, michael.roth@amd.com,
	kkostiuk@redhat.com, qemu-block@nongnu.org, philmd@linaro.org
Subject: Re: [PATCH v2 4/7] target/i386/cpu: Avoid mixing signed and unsigned in property setters
Date: Fri, 11 Oct 2024 14:23:30 +0200	[thread overview]
Message-ID: <20241011142330.4d0bf59c@imammedo.users.ipa.redhat.com> (raw)
In-Reply-To: <20241010150144.986655-5-armbru@redhat.com>

On Thu, 10 Oct 2024 17:01:41 +0200
Markus Armbruster <armbru@redhat.com> wrote:

> Properties "family", "model", and "stepping" are visited as signed
> integers.  They are backed by bits in CPUX86State member
> @cpuid_version.  The code to extract and insert these bits mixes
> signed and unsigned.  Not actually wrong, but avoiding such mixing is
> good practice.
> 
> Visit them as unsigned integers instead.
> 
> This adds a few mildly ugly cast in arguments of error_setg().  The
> next commit will get rid of them again.
> 
> Property "tsc-frequency" is also visited as signed integer.  The value
> ultimately flows into the kernel, where it is 31 bits unsigned.  The
> QEMU code freely mixes int, uint32_t, int64_t.  I elect not to attempt
> draining this swamp today.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>

Reviewed-by: Igor Mammedov <imammedo@redhat.com>

> ---
>  target/i386/cpu.c | 45 +++++++++++++++++++++------------------------
>  1 file changed, 21 insertions(+), 24 deletions(-)
> 
> diff --git a/target/i386/cpu.c b/target/i386/cpu.c
> index ff227a8c5c..4f8fa60432 100644
> --- a/target/i386/cpu.c
> +++ b/target/i386/cpu.c
> @@ -5433,13 +5433,13 @@ static void x86_cpuid_version_get_family(Object *obj, Visitor *v,
>  {
>      X86CPU *cpu = X86_CPU(obj);
>      CPUX86State *env = &cpu->env;
> -    int64_t value;
> +    uint64_t value;
>  
>      value = (env->cpuid_version >> 8) & 0xf;
>      if (value == 0xf) {
>          value += (env->cpuid_version >> 20) & 0xff;
>      }
> -    visit_type_int(v, name, &value, errp);
> +    visit_type_uint64(v, name, &value, errp);
>  }
>  
>  static void x86_cpuid_version_set_family(Object *obj, Visitor *v,
> @@ -5448,16 +5448,15 @@ static void x86_cpuid_version_set_family(Object *obj, Visitor *v,
>  {
>      X86CPU *cpu = X86_CPU(obj);
>      CPUX86State *env = &cpu->env;
> -    const int64_t min = 0;
> -    const int64_t max = 0xff + 0xf;
> -    int64_t value;
> +    const uint64_t max = 0xff + 0xf;
> +    uint64_t value;
>  
> -    if (!visit_type_int(v, name, &value, errp)) {
> +    if (!visit_type_uint64(v, name, &value, errp)) {
>          return;
>      }
> -    if (value < min || value > max) {
> +    if (value > max) {
>          error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
> -                   name ? name : "null", value, min, max);
> +                   name ? name : "null", value, (int64_t)0, (int64_t)max);
>          return;
>      }
>  
> @@ -5475,11 +5474,11 @@ static void x86_cpuid_version_get_model(Object *obj, Visitor *v,
>  {
>      X86CPU *cpu = X86_CPU(obj);
>      CPUX86State *env = &cpu->env;
> -    int64_t value;
> +    uint64_t value;
>  
>      value = (env->cpuid_version >> 4) & 0xf;
>      value |= ((env->cpuid_version >> 16) & 0xf) << 4;
> -    visit_type_int(v, name, &value, errp);
> +    visit_type_uint64(v, name, &value, errp);
>  }
>  
>  static void x86_cpuid_version_set_model(Object *obj, Visitor *v,
> @@ -5488,16 +5487,15 @@ static void x86_cpuid_version_set_model(Object *obj, Visitor *v,
>  {
>      X86CPU *cpu = X86_CPU(obj);
>      CPUX86State *env = &cpu->env;
> -    const int64_t min = 0;
> -    const int64_t max = 0xff;
> -    int64_t value;
> +    const uint64_t max = 0xff;
> +    uint64_t value;
>  
> -    if (!visit_type_int(v, name, &value, errp)) {
> +    if (!visit_type_uint64(v, name, &value, errp)) {
>          return;
>      }
> -    if (value < min || value > max) {
> +    if (value > max) {
>          error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
> -                   name ? name : "null", value, min, max);
> +                   name ? name : "null", value, (int64_t)0, (int64_t)max);
>          return;
>      }
>  
> @@ -5511,10 +5509,10 @@ static void x86_cpuid_version_get_stepping(Object *obj, Visitor *v,
>  {
>      X86CPU *cpu = X86_CPU(obj);
>      CPUX86State *env = &cpu->env;
> -    int64_t value;
> +    uint64_t value;
>  
>      value = env->cpuid_version & 0xf;
> -    visit_type_int(v, name, &value, errp);
> +    visit_type_uint64(v, name, &value, errp);
>  }
>  
>  static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
> @@ -5523,16 +5521,15 @@ static void x86_cpuid_version_set_stepping(Object *obj, Visitor *v,
>  {
>      X86CPU *cpu = X86_CPU(obj);
>      CPUX86State *env = &cpu->env;
> -    const int64_t min = 0;
> -    const int64_t max = 0xf;
> -    int64_t value;
> +    const uint64_t max = 0xf;
> +    uint64_t value;
>  
> -    if (!visit_type_int(v, name, &value, errp)) {
> +    if (!visit_type_uint64(v, name, &value, errp)) {
>          return;
>      }
> -    if (value < min || value > max) {
> +    if (value > max) {
>          error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, "",
> -                   name ? name : "null", value, min, max);
> +                   name ? name : "null", value, (int64_t)0, (int64_t)max);
>          return;
>      }
>  



  reply	other threads:[~2024-10-11 17:39 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-10 15:01 [PATCH v2 0/7] error: Eliminate QERR_PROPERTY_VALUE_OUT_OF_RANGE Markus Armbruster
2024-10-10 15:01 ` [PATCH v2 1/7] error: Drop superfluous #include "qapi/qmp/qerror.h" Markus Armbruster
2024-10-10 15:01 ` [PATCH v2 2/7] block: Improve errors about block sizes Markus Armbruster
2024-10-10 15:01 ` [PATCH v2 3/7] block: Adjust check_block_size() signature Markus Armbruster
2024-10-10 16:38   ` Philippe Mathieu-Daudé
2024-10-10 15:01 ` [PATCH v2 4/7] target/i386/cpu: Avoid mixing signed and unsigned in property setters Markus Armbruster
2024-10-11 12:23   ` Igor Mammedov [this message]
2024-10-10 15:01 ` [PATCH v2 5/7] target/i386/cpu: Improve errors for out of bounds property values Markus Armbruster
2024-10-10 17:38   ` Philippe Mathieu-Daudé
2024-10-10 19:25     ` Markus Armbruster
2024-10-11 15:11       ` Philippe Mathieu-Daudé
2024-10-15  4:45         ` Markus Armbruster
2024-10-11 12:24   ` Igor Mammedov
2024-10-10 15:01 ` [PATCH v2 6/7] hw/intc/openpic: " Markus Armbruster
2024-10-10 17:40   ` Philippe Mathieu-Daudé
2024-10-17  6:21     ` Markus Armbruster
2024-10-10 15:01 ` [PATCH v2 7/7] qerror: QERR_PROPERTY_VALUE_OUT_OF_RANGE is no longer used, drop Markus Armbruster
2024-10-10 17:41   ` Philippe Mathieu-Daudé

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=20241011142330.4d0bf59c@imammedo.users.ipa.redhat.com \
    --to=imammedo@redhat.com \
    --cc=Coiby.Xu@gmail.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=hreitz@redhat.com \
    --cc=kkostiuk@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=michael.roth@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=xieyongji@bytedance.com \
    /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 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).