qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: Isaku Yamahata <isaku.yamahata@intel.com>
Cc: peter.maydell@linaro.org, Peter Maydell <Peter.maydel@linaro.org>,
	f4bug@amsat.org, qemu-devel@nongnu.org, imammedo@redhat.com,
	isaku.yamahata@gmail.com
Subject: Re: [PATCH v2 2/3] pci: sprinkle assert in PCI pin number
Date: Wed, 24 Mar 2021 12:37:22 -0400	[thread overview]
Message-ID: <20210324123128-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <dc9c8d2aa7bfcd82ba812f1ff5630a4cc0ffbbe7.1616519655.git.isaku.yamahata@intel.com>

On Tue, Mar 23, 2021 at 10:24:30AM -0700, Isaku Yamahata wrote:
> If a device model
> (a) doesn't set the value to a correct interrupt number and then
> (b) triggers an interrupt for itself,
> it's device model bug. Add assert on interrupt pin number to catch
> this kind of bug more obviously.
> 
> Suggested-by: Peter Maydell <Peter.maydel@linaro.org>
> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
> ---
>  hw/pci/pci.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/pci/pci.c b/hw/pci/pci.c
> index ac9a24889c..cb6bab999b 100644
> --- a/hw/pci/pci.c
> +++ b/hw/pci/pci.c
> @@ -1450,6 +1450,8 @@ static void pci_irq_handler(void *opaque, int irq_num, int level)
>      PCIDevice *pci_dev = opaque;
>      int change;
>  
> +    assert(0 <= irq_num && irq_num < PCI_NUM_PINS);
> +    assert(level == 0 || level == 1);
>      change = level - pci_irq_state(pci_dev, irq_num);
>      if (!change)
>          return;
> @@ -1463,7 +1465,13 @@ static void pci_irq_handler(void *opaque, int irq_num, int level)
>  
>  static inline int pci_intx(PCIDevice *pci_dev)
>  {
> -    return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
> +    int intx = pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;




> +    /*
> +     * This function is used to setup/trigger irq.
> +     * So PIN = 0 (interrupt isn't used) doesn't make sense.

Well not really. It just returns it. But of course value -1 is not a
valid index.  Better: callers must make sure the device actually has an
interrupt pin, otherwise PCI_INTERRUPT_PIN is 0 and intx is negative
here.

> +     */
> +    assert(0 <= intx && intx < PCI_NUM_PINS);
> +    return intx;
>  }
>  
>  qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
> -- 
> 2.25.1



  parent reply	other threads:[~2021-03-24 16:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-23 17:24 [PATCH v2 0/3] reinitialize ACPI PM device on reset Isaku Yamahata
2021-03-23 17:24 ` [PATCH v2 1/3] vt82c686.c: don't raise SCI when PCI_INTERRUPT_PIN isn't setup Isaku Yamahata
2021-03-23 18:43   ` Philippe Mathieu-Daudé
2021-03-23 22:33     ` BALATON Zoltan
2021-03-23 17:24 ` [PATCH v2 2/3] pci: sprinkle assert in PCI pin number Isaku Yamahata
2021-03-23 17:50   ` Peter Maydell
2021-03-24 16:37   ` Michael S. Tsirkin [this message]
2021-03-23 17:24 ` [PATCH v2 3/3] acpi:piix4, vt82c686: reinitialize acpi PM device on reset Isaku Yamahata
2021-03-23 19:36   ` Michael S. Tsirkin

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=20210324123128-mutt-send-email-mst@kernel.org \
    --to=mst@redhat.com \
    --cc=Peter.maydel@linaro.org \
    --cc=f4bug@amsat.org \
    --cc=imammedo@redhat.com \
    --cc=isaku.yamahata@gmail.com \
    --cc=isaku.yamahata@intel.com \
    --cc=peter.maydell@linaro.org \
    --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).