All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alistair Francis <alistair23@gmail.com>
To: Anup Patel <anup@brainfault.org>
Cc: Peter Maydell <peter.maydell@linaro.org>,
	"open list:RISC-V" <qemu-riscv@nongnu.org>,
	Sagar Karandikar <sagark@eecs.berkeley.edu>,
	Anup Patel <anup.patel@wdc.com>,
	"qemu-devel@nongnu.org Developers" <qemu-devel@nongnu.org>,
	Atish Patra <atish.patra@wdc.com>,
	Alistair Francis <Alistair.Francis@wdc.com>,
	Palmer Dabbelt <palmer@dabbelt.com>
Subject: Re: [PATCH v2 04/22] target/riscv: Improve fidelity of guest external interrupts
Date: Tue, 19 Oct 2021 08:55:08 +1000	[thread overview]
Message-ID: <CAKmqyKOSNQeWbDAocDKGV3heeqdx7CfAGtz4_vyv_VrJ7c7_OQ@mail.gmail.com> (raw)
In-Reply-To: <CAAhSdy36BLy4QVALv0SOk37XacpT6q-BYOVA-KiXxtuMey0Qog@mail.gmail.com>

On Mon, Oct 18, 2021 at 10:55 PM Anup Patel <anup@brainfault.org> wrote:
>
> On Fri, Oct 15, 2021 at 11:54 AM Alistair Francis <alistair23@gmail.com> wrote:
> >
> > On Thu, Sep 16, 2021 at 11:42 PM Anup Patel <anup@brainfault.org> wrote:
> > >
> > > On Wed, Sep 15, 2021 at 6:19 AM Alistair Francis <alistair23@gmail.com> wrote:
> > > >
> > > > On Tue, Sep 14, 2021 at 2:33 AM Anup Patel <anup@brainfault.org> wrote:
> > > > >
> > > > > On Thu, Sep 9, 2021 at 12:14 PM Alistair Francis <alistair23@gmail.com> wrote:
> > > > > >
> > > > > > On Thu, Sep 2, 2021 at 9:26 PM Anup Patel <anup.patel@wdc.com> wrote:
> > > > > > >
> > > > > > > The guest external interrupts for external interrupt controller are
> > > > > > > not delivered to the guest running under hypervisor on time. This
> > > > > > > results in a guest having sluggish response to serial console input
> > > > > > > and other I/O events. To improve timely delivery of guest external
> > > > > > > interrupts, we check and inject interrupt upon every sret instruction.
> > > > > > >
> > > > > > > Signed-off-by: Anup Patel <anup.patel@wdc.com>
> > > > > > > ---
> > > > > > >  target/riscv/op_helper.c | 9 +++++++++
> > > > > > >  1 file changed, 9 insertions(+)
> > > > > > >
> > > > > > > diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c
> > > > > > > index ee7c24efe7..4c995c239e 100644
> > > > > > > --- a/target/riscv/op_helper.c
> > > > > > > +++ b/target/riscv/op_helper.c
> > > > > > > @@ -129,6 +129,15 @@ target_ulong helper_sret(CPURISCVState *env, target_ulong cpu_pc_deb)
> > > > > > >
> > > > > > >      riscv_cpu_set_mode(env, prev_priv);
> > > > > > >
> > > > > > > +    /*
> > > > > > > +     * QEMU does not promptly deliver guest external interrupts
> > > > > > > +     * to a guest running on a hypervisor which in-turn is running
> > > > > > > +     * on QEMU. We make dummy call to riscv_cpu_update_mip() upon
> > > > > > > +     * every sret instruction so that QEMU pickup guest external
> > > > > > > +     * interrupts sooner.
> > > > > > > +     */
> > > > > > > +     riscv_cpu_update_mip(env_archcpu(env), 0, 0);
> > > > > >
> > > > > > This doesn't seem right. I don't understand why we need this?
> > > > > >
> > > > > > riscv_cpu_update_mip() is called when an interrupt is delivered to the
> > > > > > CPU, if we are missing interrupts then that is a bug somewhere else.
> > > > >
> > > > > I have finally figured out the cause of guest external interrupts being
> > > > > missed by Guest/VM.
> > > > >
> > > > > The riscv_cpu_set_irq() which handles guest external interrupt lines
> > > > > of each CPU is called asynchronously. This function in-turn calls
> > > > > riscv_cpu_update_mip() but the CPU might be in host mode (V=0)
> > > > > or in Guest/VM mode (V=1). If the CPU is in host mode (V=0) when
> > > >
> > > > The IRQ being raised should just directly call riscv_cpu_update_mip()
> > > > so the IRQ should happen straight away.
> > >
> > > That's not true for guest external interrupts. The target Guest/VM might
> > > not be running on the receiving HART.
> > >
> > > Let say IMSIC injected guest external IRQ1 to HARTx which is meant
> > > for a Guest/VM, so the riscv_cpu_set_irq() will call riscv_cpu_update_mip().
> > > If HARTx might be in HS-mode or HARTx might be running some
> > > other Guest/VM then cpu_interrupt() request queued by riscv_cpu_update_mip()
> > > will not result in any interrupt being injected. This further means that
> > > QEMU has to check and inject guest external interrupts to target
> > > Guest/VM when HARTx makes a switch from HS-mode to VS-mode. By
> > > calling riscv_cpu_update_mip() upon SRET instruction we are ensuring
> > > that if any guest external interrupt was missed then it is injected ot
> > > VS-mode.
> >
> > Ah ok.
> >
> > So the problem is that an interrupt can occur for a guest, while that
> > guest isn't executing.
>
> Yes, that's right.
>
> >
> > So for example a CPU is executing with V=0. `riscv_cpu_update_mip()`
> > is called, which triggers a hard interrupt. That in turn calls
> > `riscv_cpu_exec_interrupt()` and `riscv_cpu_local_irq_pending()`.
>
> In this case, the hard interrupt is actually a guest external interrupt
> which is tracked via hgeip CSR. The hgeip CSR is updated immediately
> but `riscv_cpu_local_irq_pending()` might be called while V=0 hence
> no interrupt.
>
> >
> > This results in the guest Hypervisor receiving the interrupt, which it
> > then doesn't act on? Or is MIP set but `riscv_cpu_local_irq_pending()`
> > returns false due to the enable checks?
>
> Here, hgeip CSR will be set and it will be reflected in mip.VSEIP
> bit only when hypervisor schedules/runs the Guest (i.e. V=1 and
> hstatus.VGEIN pointing to the appropriate bit of hgeip csr).
>
> >
> > That either seems like a guest bug or that we need some functionality
> > in `riscv_cpu_swap_hypervisor_regs()` to trigger an interrupt on
> > context swap.
>
> This certainly is not a bug with Guest or Hypervisor but rather an
> issue of invoking `riscv_cpu_exec_interrupt()` and
> `riscv_cpu_local_irq_pending()` at the right time.
>
> Your suggestion of checking and triggering guest external interrupt
> in `riscv_cpu_swap_hypervisor_regs()` is a better approach. If you
> are okay then I will update this patch in the next revision.

Yeah, let's do that instead.

Alistair


WARNING: multiple messages have this Message-ID (diff)
From: Alistair Francis <alistair23@gmail.com>
To: Anup Patel <anup@brainfault.org>
Cc: Anup Patel <anup.patel@wdc.com>,
	Peter Maydell <peter.maydell@linaro.org>,
	 Palmer Dabbelt <palmer@dabbelt.com>,
	Alistair Francis <Alistair.Francis@wdc.com>,
	 Sagar Karandikar <sagark@eecs.berkeley.edu>,
	Atish Patra <atish.patra@wdc.com>,
	"open list:RISC-V" <qemu-riscv@nongnu.org>,
	 "qemu-devel@nongnu.org Developers" <qemu-devel@nongnu.org>
Subject: Re: [PATCH v2 04/22] target/riscv: Improve fidelity of guest external interrupts
Date: Tue, 19 Oct 2021 08:55:08 +1000	[thread overview]
Message-ID: <CAKmqyKOSNQeWbDAocDKGV3heeqdx7CfAGtz4_vyv_VrJ7c7_OQ@mail.gmail.com> (raw)
In-Reply-To: <CAAhSdy36BLy4QVALv0SOk37XacpT6q-BYOVA-KiXxtuMey0Qog@mail.gmail.com>

On Mon, Oct 18, 2021 at 10:55 PM Anup Patel <anup@brainfault.org> wrote:
>
> On Fri, Oct 15, 2021 at 11:54 AM Alistair Francis <alistair23@gmail.com> wrote:
> >
> > On Thu, Sep 16, 2021 at 11:42 PM Anup Patel <anup@brainfault.org> wrote:
> > >
> > > On Wed, Sep 15, 2021 at 6:19 AM Alistair Francis <alistair23@gmail.com> wrote:
> > > >
> > > > On Tue, Sep 14, 2021 at 2:33 AM Anup Patel <anup@brainfault.org> wrote:
> > > > >
> > > > > On Thu, Sep 9, 2021 at 12:14 PM Alistair Francis <alistair23@gmail.com> wrote:
> > > > > >
> > > > > > On Thu, Sep 2, 2021 at 9:26 PM Anup Patel <anup.patel@wdc.com> wrote:
> > > > > > >
> > > > > > > The guest external interrupts for external interrupt controller are
> > > > > > > not delivered to the guest running under hypervisor on time. This
> > > > > > > results in a guest having sluggish response to serial console input
> > > > > > > and other I/O events. To improve timely delivery of guest external
> > > > > > > interrupts, we check and inject interrupt upon every sret instruction.
> > > > > > >
> > > > > > > Signed-off-by: Anup Patel <anup.patel@wdc.com>
> > > > > > > ---
> > > > > > >  target/riscv/op_helper.c | 9 +++++++++
> > > > > > >  1 file changed, 9 insertions(+)
> > > > > > >
> > > > > > > diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c
> > > > > > > index ee7c24efe7..4c995c239e 100644
> > > > > > > --- a/target/riscv/op_helper.c
> > > > > > > +++ b/target/riscv/op_helper.c
> > > > > > > @@ -129,6 +129,15 @@ target_ulong helper_sret(CPURISCVState *env, target_ulong cpu_pc_deb)
> > > > > > >
> > > > > > >      riscv_cpu_set_mode(env, prev_priv);
> > > > > > >
> > > > > > > +    /*
> > > > > > > +     * QEMU does not promptly deliver guest external interrupts
> > > > > > > +     * to a guest running on a hypervisor which in-turn is running
> > > > > > > +     * on QEMU. We make dummy call to riscv_cpu_update_mip() upon
> > > > > > > +     * every sret instruction so that QEMU pickup guest external
> > > > > > > +     * interrupts sooner.
> > > > > > > +     */
> > > > > > > +     riscv_cpu_update_mip(env_archcpu(env), 0, 0);
> > > > > >
> > > > > > This doesn't seem right. I don't understand why we need this?
> > > > > >
> > > > > > riscv_cpu_update_mip() is called when an interrupt is delivered to the
> > > > > > CPU, if we are missing interrupts then that is a bug somewhere else.
> > > > >
> > > > > I have finally figured out the cause of guest external interrupts being
> > > > > missed by Guest/VM.
> > > > >
> > > > > The riscv_cpu_set_irq() which handles guest external interrupt lines
> > > > > of each CPU is called asynchronously. This function in-turn calls
> > > > > riscv_cpu_update_mip() but the CPU might be in host mode (V=0)
> > > > > or in Guest/VM mode (V=1). If the CPU is in host mode (V=0) when
> > > >
> > > > The IRQ being raised should just directly call riscv_cpu_update_mip()
> > > > so the IRQ should happen straight away.
> > >
> > > That's not true for guest external interrupts. The target Guest/VM might
> > > not be running on the receiving HART.
> > >
> > > Let say IMSIC injected guest external IRQ1 to HARTx which is meant
> > > for a Guest/VM, so the riscv_cpu_set_irq() will call riscv_cpu_update_mip().
> > > If HARTx might be in HS-mode or HARTx might be running some
> > > other Guest/VM then cpu_interrupt() request queued by riscv_cpu_update_mip()
> > > will not result in any interrupt being injected. This further means that
> > > QEMU has to check and inject guest external interrupts to target
> > > Guest/VM when HARTx makes a switch from HS-mode to VS-mode. By
> > > calling riscv_cpu_update_mip() upon SRET instruction we are ensuring
> > > that if any guest external interrupt was missed then it is injected ot
> > > VS-mode.
> >
> > Ah ok.
> >
> > So the problem is that an interrupt can occur for a guest, while that
> > guest isn't executing.
>
> Yes, that's right.
>
> >
> > So for example a CPU is executing with V=0. `riscv_cpu_update_mip()`
> > is called, which triggers a hard interrupt. That in turn calls
> > `riscv_cpu_exec_interrupt()` and `riscv_cpu_local_irq_pending()`.
>
> In this case, the hard interrupt is actually a guest external interrupt
> which is tracked via hgeip CSR. The hgeip CSR is updated immediately
> but `riscv_cpu_local_irq_pending()` might be called while V=0 hence
> no interrupt.
>
> >
> > This results in the guest Hypervisor receiving the interrupt, which it
> > then doesn't act on? Or is MIP set but `riscv_cpu_local_irq_pending()`
> > returns false due to the enable checks?
>
> Here, hgeip CSR will be set and it will be reflected in mip.VSEIP
> bit only when hypervisor schedules/runs the Guest (i.e. V=1 and
> hstatus.VGEIN pointing to the appropriate bit of hgeip csr).
>
> >
> > That either seems like a guest bug or that we need some functionality
> > in `riscv_cpu_swap_hypervisor_regs()` to trigger an interrupt on
> > context swap.
>
> This certainly is not a bug with Guest or Hypervisor but rather an
> issue of invoking `riscv_cpu_exec_interrupt()` and
> `riscv_cpu_local_irq_pending()` at the right time.
>
> Your suggestion of checking and triggering guest external interrupt
> in `riscv_cpu_swap_hypervisor_regs()` is a better approach. If you
> are okay then I will update this patch in the next revision.

Yeah, let's do that instead.

Alistair


  reply	other threads:[~2021-10-18 22:56 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-02 11:24 [PATCH v2 00/22] QEMU RISC-V AIA support Anup Patel
2021-09-02 11:24 ` Anup Patel
2021-09-02 11:24 ` [PATCH v2 01/22] target/riscv: Fix trap cause for RV32 HS-mode CSR access from RV64 HS-mode Anup Patel
2021-09-02 11:24   ` Anup Patel
2021-09-03  3:32   ` Alistair Francis
2021-09-03  3:32     ` Alistair Francis
2021-09-02 11:25 ` [PATCH v2 02/22] target/riscv: Implement SGEIP bit in hip and hie CSRs Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-15  1:03   ` Alistair Francis
2021-09-15  1:03     ` Alistair Francis
2021-09-02 11:25 ` [PATCH v2 03/22] target/riscv: Implement hgeie and hgeip CSRs Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-15  2:41   ` Alistair Francis
2021-09-15  2:41     ` Alistair Francis
2021-09-02 11:25 ` [PATCH v2 04/22] target/riscv: Improve fidelity of guest external interrupts Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-09  6:44   ` Alistair Francis
2021-09-09  6:44     ` Alistair Francis
2021-09-09 16:35     ` Bin Meng
2021-09-09 16:35       ` Bin Meng
2021-09-13 16:33     ` Anup Patel
2021-09-13 16:33       ` Anup Patel
2021-09-15  0:49       ` Alistair Francis
2021-09-15  0:49         ` Alistair Francis
2021-09-16 13:42         ` Anup Patel
2021-09-16 13:42           ` Anup Patel
2021-10-15  6:24           ` Alistair Francis
2021-10-15  6:24             ` Alistair Francis
2021-10-18 12:55             ` Anup Patel
2021-10-18 12:55               ` Anup Patel
2021-10-18 22:55               ` Alistair Francis [this message]
2021-10-18 22:55                 ` Alistair Francis
2021-09-02 11:25 ` [PATCH v2 05/22] target/riscv: Allow setting CPU feature from machine/device emulation Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-04 15:12   ` Bin Meng
2021-09-04 15:12     ` Bin Meng
2021-10-21 17:06     ` Anup Patel
2021-10-21 17:06       ` Anup Patel
2021-09-06  5:33   ` Alistair Francis
2021-09-06  5:33     ` Alistair Francis
2021-09-02 11:25 ` [PATCH v2 06/22] target/riscv: Add AIA cpu feature Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-04 15:13   ` Bin Meng
2021-09-04 15:13     ` Bin Meng
2021-09-06  5:33   ` Alistair Francis
2021-09-06  5:33     ` Alistair Francis
2021-09-02 11:25 ` [PATCH v2 07/22] target/riscv: Add defines for AIA CSRs Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 08/22] target/riscv: Allow AIA device emulation to set ireg rmw callback Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 09/22] target/riscv: Implement AIA local interrupt priorities Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 10/22] target/riscv: Implement AIA CSRs for 64 local interrupts on RV32 Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 11/22] target/riscv: Implement AIA hvictl and hviprioX CSRs Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 12/22] target/riscv: Implement AIA interrupt filtering CSRs Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 13/22] target/riscv: Implement AIA mtopi, stopi, and vstopi CSRs Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 14/22] target/riscv: Implement AIA xiselect and xireg CSRs Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 15/22] target/riscv: Implement AIA IMSIC interface CSRs Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 16/22] hw/riscv: virt: Use AIA INTC compatible string when available Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-15  1:17   ` Alistair Francis
2021-09-15  1:17     ` Alistair Francis
2021-09-02 11:25 ` [PATCH v2 17/22] target/riscv: Allow users to force enable AIA CSRs in HART Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-15  1:16   ` Alistair Francis
2021-09-15  1:16     ` Alistair Francis
2021-09-02 11:25 ` [PATCH v2 18/22] hw/intc: Add RISC-V AIA APLIC device emulation Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 19/22] hw/riscv: virt: Add optional AIA APLIC support to virt machine Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 20/22] hw/intc: Add RISC-V AIA IMSIC device emulation Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 21/22] hw/riscv: virt: Add optional AIA IMSIC support to virt machine Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-02 11:25 ` [PATCH v2 22/22] docs/system: riscv: Document AIA options for " Anup Patel
2021-09-02 11:25   ` Anup Patel
2021-09-09  6:51   ` Alistair Francis
2021-09-09  6:51     ` Alistair Francis
2021-09-04 13:51 ` [PATCH v2 00/22] QEMU RISC-V AIA support Bin Meng
2021-09-04 13:51   ` Bin Meng
2021-09-04 15:33   ` Anup Patel
2021-09-04 15:33     ` Anup Patel

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=CAKmqyKOSNQeWbDAocDKGV3heeqdx7CfAGtz4_vyv_VrJ7c7_OQ@mail.gmail.com \
    --to=alistair23@gmail.com \
    --cc=Alistair.Francis@wdc.com \
    --cc=anup.patel@wdc.com \
    --cc=anup@brainfault.org \
    --cc=atish.patra@wdc.com \
    --cc=palmer@dabbelt.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=sagark@eecs.berkeley.edu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.