All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andre Przywara <andre.przywara@arm.com>
To: Stefano Stabellini <sstabellini@kernel.org>
Cc: xen-devel@lists.xenproject.org, Julien Grall <julien.grall@arm.com>
Subject: Re: [PATCH v3 05/39] ARM: timer: Handle level triggered IRQs correctly
Date: Tue, 27 Mar 2018 14:06:23 +0100	[thread overview]
Message-ID: <44ad82ff-1034-8ab1-a771-a86d5214be83@arm.com> (raw)
In-Reply-To: <alpine.DEB.2.10.1803261324350.12360@sstabellini-ThinkPad-X260>

Hi,

On 26/03/18 21:28, Stefano Stabellini wrote:
> On Wed, 21 Mar 2018, Andre Przywara wrote:
>> The ARM Generic Timer uses a level-sensitive interrupt semantic. We
>> easily catch when the line goes high, as this triggers the hardware IRQ.
>> However we also have to keep track of when the line lowers, as the
>> emulation depends on it: Upon entering the guest, the new VGIC will
>> *clear* the virtual interrupt line, so it needs to re-sample the actual
>> state after returning from the guest.
>> So we have to sync the state of the interrupt condition at certain
>> points to catch when the line goes low and we can remove the vtimer vIRQ
>> from the vGIC (and the LR).
>> The VGIC in Xen so far only implemented edge triggered vIRQs, really, so
>> we need to add new functionality to re-sample the interrupt state.
>> Do this only when the new VGIC is in use.
>>
>> Signed-off-by: Andre Przywara <andre.przywara@linaro.org>
>> ---
>> Changelog v2 ... v3:
>> - move vtimer_sync() from time.c into vtimer.c
>> - rename function to vtimer_update_irqs()
>> - refactor functionality into new static function, to ...
>> - handle physical timer as well
>> - extending comments
>>
>> Changelog v1 ... v2:
>> - restrict to new VGIC
>> - add TODO: comment
>>
>>  xen/arch/arm/traps.c         | 11 ++++++++++
>>  xen/arch/arm/vtimer.c        | 49 ++++++++++++++++++++++++++++++++++++++++++++
>>  xen/include/asm-arm/vtimer.h |  1 +
>>  3 files changed, 61 insertions(+)
>>
>> diff --git a/xen/arch/arm/traps.c b/xen/arch/arm/traps.c
>> index 7411bff7a7..2638446693 100644
>> --- a/xen/arch/arm/traps.c
>> +++ b/xen/arch/arm/traps.c
>> @@ -2024,6 +2024,17 @@ static void enter_hypervisor_head(struct cpu_user_regs *regs)
>>          if ( current->arch.hcr_el2 & HCR_VA )
>>              current->arch.hcr_el2 = READ_SYSREG(HCR_EL2);
>>  
>> +#ifdef CONFIG_NEW_VGIC
>> +        /*
>> +         * We need to update the state of our emulated devices using level
>> +         * triggered interrupts before syncing back the VGIC state.
>> +         *
>> +         * TODO: Investigate whether this is necessary to do on every
>> +         * trap and how it can be optimised.
>> +         */
>> +        vtimer_update_irqs(current);
>> +#endif
>> +
>>          vgic_sync_from_lrs(current);
>>      }
>>  }
>> diff --git a/xen/arch/arm/vtimer.c b/xen/arch/arm/vtimer.c
>> index 8164f6c7f1..c99dd237d1 100644
>> --- a/xen/arch/arm/vtimer.c
>> +++ b/xen/arch/arm/vtimer.c
>> @@ -334,6 +334,55 @@ bool vtimer_emulate(struct cpu_user_regs *regs, union hsr hsr)
>>      }
>>  }
>>  
>> +static void vtimer_update_irq(struct vcpu *v, struct vtimer *vtimer,
>> +                              uint32_t vtimer_ctl)
>> +{
>> +    bool level;
>> +
>> +    /* Filter for the three bits that determine the status of the timer */
>> +    vtimer_ctl &= (CNTx_CTL_ENABLE | CNTx_CTL_PENDING | CNTx_CTL_MASK);
>> +
>> +    /* The level is high if the timer is pending and enabled, but not masked. */
>> +    level = (vtimer_ctl == (CNTx_CTL_ENABLE | CNTx_CTL_PENDING));
>> +
>> +    /*
>> +     * This is mostly here to *lower* the virtual interrupt line if the timer
>> +     * is no longer pending.
>> +     * We would have injected an IRQ already via SOFTIRQ when the timer expired.
>> +     * Doing it here again is basically a NOP if the line was already high.
>> +     */
>> +    vgic_inject_irq(v->domain, v, vtimer->irq, level);
>> +}
>> +
>> +/**
>> + * vtimer_update_irqs() - update the virtual timers' IRQ lines after a guest run
>> + * @vcpu: The VCPU to sync the timer state
>> + *
>> + * After returning from a guest, update the state of the timers' virtual
>> + * interrupt lines, to model the level triggered interrupts correctly.
>> + * If the guest has handled a timer interrupt, the virtual interrupt line
>> + * needs to be lowered explicitly. vgic_inject_irq() takes care of that.
>> + */
>> +void vtimer_update_irqs(struct vcpu *v)
>> +{
>> +    /*
>> +     * For the virtual timer we read the current state from the hardware.
>> +     * Technically we should keep the CNTx_CTL_MASK bit here, to catch if
>> +     * the timer interrupt is masked. However Xen *always* masks the timer
>> +     * upon entering the hypervisor, leaving it up to the guest to un-mask it.
>> +     * So we would always read a "low" level, despite the condition being
>> +     * actually "high".  Ignoring the mask bit solves this (for now).
>> +     *
>> +     * TODO: The proper fix for this is to make vtimer vIRQ hardware mapped,
>> +     * but this requires reworking the arch timer to implement this.
>> +     */
>> +    vtimer_update_irq(v, &v->arch.virt_timer,
>> +                      READ_SYSREG32(CNTV_CTL_EL0) & ~CNTx_CTL_MASK);
> 
> Yes, but won't this have the opposite effect? Meaning that it will
> always read as "high" for the virtual timer (because we remove the MASK
> and that is the only thing that can cause a "low" read in
> vtimer_update_irq if it's enabled and pending)?

What we want to know here is the status of the interrupt line of the
virtual timer. We don't know if it's still pending or not. So we are
very much interested in the pending bit of CNTV_CTL_EL0.
We could read the distributor's ISPENDR register as well, but this is
more costly.

> It seems to me that it would be better to remove the update of the
> virtual timer -- this seems to have the potential of causing problems.

Removing this makes Dom0 hang very early. The reason is that in that
case we never clear the line_level in the vtimer's struct vgic_irq:
When the h/w IRQ fires, we set line_level by injecting the corresponding
virtual IRQ. But if the emulated line_level is still high,
vgic_inject_irq() will bail out early, as making an IRQ pending when it
is already pending is a NOP, so vgic_validate_injection() denies that case.

Properly emulating the actual state of a virtual level triggered
interrupt line is something we were totally ignoring so far, because we
only dealt with edge interrupts. In case of the timer and also the event
channel this is wrong, as both devices are actually using level
triggered interrupts semantics.

Cheers,
Andre.

>> +    /* For the physical timer we rely on our emulated state. */
>> +    vtimer_update_irq(v, &v->arch.phys_timer, v->arch.phys_timer.ctl);
>> +}
>> +
>>  /*
>>   * Local variables:
>>   * mode: C
>> diff --git a/xen/include/asm-arm/vtimer.h b/xen/include/asm-arm/vtimer.h
>> index 5aaddc6f63..91d88b377f 100644
>> --- a/xen/include/asm-arm/vtimer.h
>> +++ b/xen/include/asm-arm/vtimer.h
>> @@ -27,6 +27,7 @@ extern bool vtimer_emulate(struct cpu_user_regs *regs, union hsr hsr);
>>  extern int virt_timer_save(struct vcpu *v);
>>  extern int virt_timer_restore(struct vcpu *v);
>>  extern void vcpu_timer_destroy(struct vcpu *v);
>> +void vtimer_update_irqs(struct vcpu *v);
>>  
>>  #endif
>>  
>> -- 
>> 2.14.1
>>

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  reply	other threads:[~2018-03-27 13:06 UTC|newest]

Thread overview: 122+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-21 16:31 [PATCH v3 00/39] New VGIC(-v2) implementation Andre Przywara
2018-03-21 16:31 ` [PATCH v3 01/39] xen/arm: gic: Read unconditionally the source from the LRs Andre Przywara
2018-03-21 16:31 ` [PATCH v3 02/39] ARM: GIC: add GIC_INVALID to enum gic_version Andre Przywara
2018-03-22  1:39   ` Julien Grall
2018-03-26 20:08   ` Stefano Stabellini
2018-03-21 16:31 ` [PATCH v3 03/39] ARM: GIC: Allow tweaking the active and pending state of an IRQ Andre Przywara
2018-03-22  1:51   ` Julien Grall
2018-03-22 11:11     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 04/39] ARM: GIC: Allow reading pending state of a hardware IRQ Andre Przywara
2018-03-26 20:08   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 05/39] ARM: timer: Handle level triggered IRQs correctly Andre Przywara
2018-03-22  1:58   ` Julien Grall
2018-03-26 20:28   ` Stefano Stabellini
2018-03-27 13:06     ` Andre Przywara [this message]
2018-03-27 18:17       ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 06/39] ARM: evtchn: " Andre Przywara
2018-03-22  2:08   ` Julien Grall
2018-03-26 20:08   ` Stefano Stabellini
2018-03-28  0:01   ` Stefano Stabellini
2018-03-28 15:39     ` Andre Przywara
2018-03-28 17:46       ` Stefano Stabellini
2018-03-29  0:34         ` Julien Grall
2018-03-29 13:44         ` Andre Przywara
2018-04-03 13:34     ` Julien Grall
2018-03-21 16:32 ` [PATCH v3 07/39] ARM: vPL011: Use the VGIC's level triggered IRQs handling if available Andre Przywara
2018-03-26 20:20   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 08/39] ARM: new VGIC: Add data structure definitions Andre Przywara
2018-03-26 20:41   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 09/39] ARM: new VGIC: Add accessor to new struct vgic_irq instance Andre Przywara
2018-03-22  2:11   ` Julien Grall
2018-03-26 20:46     ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 10/39] ARM: new VGIC: Implement virtual IRQ injection Andre Przywara
2018-03-26 21:01   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 11/39] Add list_sort() routine from Linux Andre Przywara
2018-03-21 17:01   ` Jan Beulich
2018-03-22  2:14   ` Julien Grall
2018-03-21 16:32 ` [PATCH v3 12/39] ARM: new VGIC: Add IRQ sorting Andre Przywara
2018-03-26 21:16   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 13/39] ARM: new VGIC: Add IRQ sync/flush framework Andre Przywara
2018-03-22  2:16   ` Julien Grall
2018-03-26 21:30   ` Stefano Stabellini
2018-03-27 13:23     ` Andre Przywara
2018-03-27 18:55       ` Stefano Stabellini
2018-03-27 19:20         ` Stefano Stabellini
2018-03-27 22:13           ` André Przywara
2018-03-21 16:32 ` [PATCH v3 14/39] ARM: new VGIC: Add GICv2 world switch backend Andre Przywara
2018-03-22  3:48   ` Julien Grall
2018-03-22 11:04     ` Andre Przywara
2018-03-22 13:55       ` Julien Grall
2018-03-21 16:32 ` [PATCH v3 15/39] ARM: new VGIC: Implement vgic_vcpu_pending_irq Andre Przywara
2018-03-22  3:52   ` Julien Grall
2018-03-22 11:15     ` Andre Przywara
2018-03-26 23:34       ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 16/39] ARM: new VGIC: Add MMIO handling framework Andre Przywara
2018-03-27 20:07   ` Stefano Stabellini
2018-03-28  9:28     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 17/39] ARM: new VGIC: Add GICv2 " Andre Przywara
2018-03-27 20:28   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 18/39] ARM: new VGIC: Add CTLR, TYPER and IIDR handlers Andre Przywara
2018-03-22  7:33   ` Julien Grall
2018-03-27 20:38   ` Stefano Stabellini
2018-03-28 10:36     ` Andre Przywara
2018-03-28 17:20       ` Stefano Stabellini
2018-03-28 17:22       ` [PATCH v3 26/39] ARM: new VGIC: Add SGIPENDR register handlers Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 19/39] ARM: new VGIC: Add ENABLE registers handlers Andre Przywara
2018-03-27 21:06   ` Stefano Stabellini
2018-03-28  9:09     ` Andre Przywara
2018-03-28 17:19       ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 20/39] ARM: new VGIC: Add PENDING " Andre Przywara
2018-03-27 21:14   ` Stefano Stabellini
2018-03-28 14:10     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 21/39] ARM: new VGIC: Add ACTIVE " Andre Przywara
2018-03-27 21:21   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 22/39] ARM: new VGIC: Add PRIORITY " Andre Przywara
2018-03-27 21:24   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 23/39] ARM: new VGIC: Add CONFIG " Andre Przywara
2018-03-27 21:26   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 24/39] ARM: new VGIC: Add TARGET " Andre Przywara
2018-03-27  0:24   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 25/39] ARM: new VGIC: Add SGIR register handler Andre Przywara
2018-03-22  7:54   ` Julien Grall
2018-03-27 22:23   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 26/39] ARM: new VGIC: Add SGIPENDR register handlers Andre Przywara
2018-03-27 22:27   ` Stefano Stabellini
2018-03-28 10:37     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 27/39] ARM: new VGIC: Handle hardware mapped IRQs Andre Przywara
2018-03-27 22:31   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 28/39] ARM: new VGIC: Add event channel IRQ handling Andre Przywara
2018-03-27 22:33   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 29/39] ARM: new VGIC: Handle virtual IRQ allocation/reservation Andre Przywara
2018-03-27 22:38   ` Stefano Stabellini
2018-03-28  9:17     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 30/39] ARM: new VGIC: Dump virtual IRQ info Andre Przywara
2018-03-27 22:39   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 31/39] ARM: new VGIC: Provide system register emulation stub Andre Przywara
2018-03-27 22:40   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 32/39] ARM: new VGIC: Implement arch_move_irqs() Andre Przywara
2018-03-26 23:46   ` Stefano Stabellini
2018-03-28 18:47   ` Stefano Stabellini
2018-03-29 14:57     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 33/39] ARM: new VGIC: Add preliminary stub implementation Andre Przywara
2018-03-27 22:48   ` Stefano Stabellini
2018-04-03 13:22     ` Julien Grall
2018-03-21 16:32 ` [PATCH v3 34/39] ARM: new VGIC: vgic-init: register VGIC Andre Przywara
2018-03-22  8:00   ` Julien Grall
2018-03-22 11:18     ` Andre Przywara
2018-03-27 22:50     ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 35/39] ARM: new VGIC: Add vgic_v2_enable Andre Przywara
2018-03-27 22:51   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 36/39] ARM: new VGIC: vgic-init: implement vgic_init Andre Przywara
2018-03-22  8:01   ` Julien Grall
2018-03-27 23:16   ` Stefano Stabellini
2018-03-28  0:06     ` Stefano Stabellini
2018-03-28 10:49     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 37/39] ARM: new VGIC: vgic-init: implement map_resources Andre Przywara
2018-03-27 23:09   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 38/39] ARM: new VGIC: Allocate two pages for struct vcpu Andre Przywara
2018-03-22  8:11   ` Julien Grall
2018-03-27 23:07     ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 39/39] ARM: VGIC: wire new VGIC(-v2) files into Xen build system Andre Przywara
2018-03-22  8:16   ` Julien Grall
2018-03-22 10:39     ` Andre Przywara

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=44ad82ff-1034-8ab1-a771-a86d5214be83@arm.com \
    --to=andre.przywara@arm.com \
    --cc=julien.grall@arm.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.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 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.