qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Zhao Liu <zhao1.liu@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Michael S . Tsirkin" <mst@redhat.com>, qemu-devel@nongnu.org
Subject: Re: [PATCH] hw/timer/hpet: Detect invalid access to TN registers
Date: Wed, 19 Feb 2025 14:12:51 +0800	[thread overview]
Message-ID: <Z7V2Y7jVs5jgIoMo@intel.com> (raw)
In-Reply-To: <53739259-69a5-4d7e-9178-f09e1d6ede89@redhat.com>

On Tue, Feb 18, 2025 at 09:53:00AM +0100, Paolo Bonzini wrote:
> Date: Tue, 18 Feb 2025 09:53:00 +0100
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: Re: [PATCH] hw/timer/hpet: Detect invalid access to TN registers
> 
> On 2/18/25 08:37, Zhao Liu wrote:
> > "addr & 0x18" ignores invalid address, so that the trace in default
> > branch (trace_hpet_ram_{read|write}_invalid()) doesn't work.
> > 
> > Mask addr by "0x1f & ~4", in which 0x1f means to get the complete TN
> > registers access and ~4 means to keep any invalid address offset.
> 
> I think this is less readable.
> 
> The reason to use !4 in the Rust code is because the initial AND is done
> in a separate function, timer_and_addr(). In C you don't have the same thing.

Yes.

> If anything you could do something like this:
 
> diff --git a/hw/timer/hpet.c b/hw/timer/hpet.c
> index ccb97b68066..7c011204971 100644
> --- a/hw/timer/hpet.c
> +++ b/hw/timer/hpet.c
> @@ -425,6 +425,7 @@ static uint64_t hpet_ram_read(void *opaque, hwaddr addr,
>      int shift = (addr & 4) * 8;
>      uint64_t cur_tick;
> +    addr &= ~4;
>      trace_hpet_ram_read(addr);
>      /*address range of all TN regs*/
> @@ -437,7 +438,7 @@ static uint64_t hpet_ram_read(void *opaque, hwaddr addr,
>              return 0;
>          }
> -        switch (addr & 0x18) {
> +        switch (addr & 0x1f) {
>          case HPET_TN_CFG: // including interrupt capabilities
>              return timer->config >> shift;
>          case HPET_TN_CMP: // comparator register
> @@ -449,7 +450,7 @@ static uint64_t hpet_ram_read(void *opaque, hwaddr addr,
>              break;
>          }
>      } else {
> -        switch (addr & ~4) {
> +        switch (addr) {
>          case HPET_ID: // including HPET_PERIOD
>              return s->capability >> shift;
>          case HPET_CFG:
> 
> and the same in the write function, 

Thanks! Your example is clearer!

> but that's also different from the Rust code.

At least, user could know the invalid access by trace in C side. Rust
hasn't supported trace, but it will be in the future.

There will be some differences in code between the two, and we can make
sure that the debug ability to be as consistent as possible.

Thanks,
Zhao



      parent reply	other threads:[~2025-02-19  5:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-18  7:37 [PATCH] hw/timer/hpet: Detect invalid access to TN registers Zhao Liu
2025-02-18  8:53 ` Paolo Bonzini
2025-02-18  9:07   ` Philippe Mathieu-Daudé
2025-02-19  3:34     ` Zhao Liu
2025-02-19  9:25     ` Paolo Bonzini
2025-02-19 14:38       ` Zhao Liu
2025-02-19  6:12   ` Zhao Liu [this message]

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=Z7V2Y7jVs5jgIoMo@intel.com \
    --to=zhao1.liu@intel.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@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 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).