diff mbox series

[v4,2/2] target/arm: kvm: Handle potential issue with dabt injection

Message ID 20200323113227.3169-3-beata.michalska@linaro.org
State Superseded
Headers show
Series target/arm: kvm: Support for KVM DABT with no valid ISS | expand

Commit Message

Beata Michalska March 23, 2020, 11:32 a.m. UTC
Injecting external data abort through KVM might trigger
an issue on kernels that do not get updated to include the KVM fix.
For those and aarch32 guests, the injected abort gets misconfigured
to be an implementation defined exception. This leads to the guest
repeatedly re-running the faulting instruction.

Add support for handling that case.
[
  Fixed-by: 018f22f95e8a
	('KVM: arm: Fix DFSR setting for non-LPAE aarch32 guests')
  Fixed-by: 21aecdbd7f3a
	('KVM: arm: Make inject_abt32() inject an external abort instead')
]

Signed-off-by: Beata Michalska <beata.michalska@linaro.org>

---
 target/arm/cpu.h     |  1 +
 target/arm/kvm.c     | 30 +++++++++++++++++++++++++++++-
 target/arm/kvm32.c   | 25 +++++++++++++++++++++++++
 target/arm/kvm64.c   | 34 ++++++++++++++++++++++++++++++++++
 target/arm/kvm_arm.h | 10 ++++++++++
 5 files changed, 99 insertions(+), 1 deletion(-)

-- 
2.7.4

Comments

Richard Henderson March 23, 2020, 6:44 p.m. UTC | #1
On 3/23/20 4:32 AM, Beata Michalska wrote:
>      uint8_t ext_dabt_pending; /* Request for injecting ext DABT */

> +    uint8_t ext_dabt_raised; /* Tracking/verifying injection of ext DABT */


Is there a reason these are uint8_t and not bool?


r~
Beata Michalska March 25, 2020, 3:16 p.m. UTC | #2
On Mon, 23 Mar 2020 at 18:44, Richard Henderson
<richard.henderson@linaro.org> wrote:
>

> On 3/23/20 4:32 AM, Beata Michalska wrote:

> >      uint8_t ext_dabt_pending; /* Request for injecting ext DABT */

> > +    uint8_t ext_dabt_raised; /* Tracking/verifying injection of ext DABT */

>

> Is there a reason these are uint8_t and not bool?

>

>

The ext_dabt_pending is reflecting the KVM type.
The ext_dabt_raised is following that one.

BR
Beata
> r~
Andrew Jones April 3, 2020, 8:44 a.m. UTC | #3
On Mon, Mar 23, 2020 at 11:32:27AM +0000, Beata Michalska wrote:
> Injecting external data abort through KVM might trigger

> an issue on kernels that do not get updated to include the KVM fix.

> For those and aarch32 guests, the injected abort gets misconfigured

> to be an implementation defined exception. This leads to the guest

> repeatedly re-running the faulting instruction.

> 

> Add support for handling that case.

> [

>   Fixed-by: 018f22f95e8a

> 	('KVM: arm: Fix DFSR setting for non-LPAE aarch32 guests')

>   Fixed-by: 21aecdbd7f3a

> 	('KVM: arm: Make inject_abt32() inject an external abort instead')

> ]

> 

> Signed-off-by: Beata Michalska <beata.michalska@linaro.org>

> ---

>  target/arm/cpu.h     |  1 +

>  target/arm/kvm.c     | 30 +++++++++++++++++++++++++++++-

>  target/arm/kvm32.c   | 25 +++++++++++++++++++++++++

>  target/arm/kvm64.c   | 34 ++++++++++++++++++++++++++++++++++

>  target/arm/kvm_arm.h | 10 ++++++++++

>  5 files changed, 99 insertions(+), 1 deletion(-)

> 

> diff --git a/target/arm/cpu.h b/target/arm/cpu.h

> index 4f834c1..868afc6 100644

> --- a/target/arm/cpu.h

> +++ b/target/arm/cpu.h

> @@ -561,6 +561,7 @@ typedef struct CPUARMState {

>      } serror;

>  

>      uint8_t ext_dabt_pending; /* Request for injecting ext DABT */

> +    uint8_t ext_dabt_raised; /* Tracking/verifying injection of ext DABT */

>  

>      /* State of our input IRQ/FIQ/VIRQ/VFIQ lines */

>      uint32_t irq_line_state;

> diff --git a/target/arm/kvm.c b/target/arm/kvm.c

> index c088589..58ad734 100644

> --- a/target/arm/kvm.c

> +++ b/target/arm/kvm.c

> @@ -721,7 +721,12 @@ int kvm_put_vcpu_events(ARMCPU *cpu)

>      ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events);

>      if (ret) {

>          error_report("failed to put vcpu events");

> -    } else {

> +    } else if (env->ext_dabt_pending) {

> +        /*

> +         * Mark that the external DABT has been injected,

> +         * if one has been requested

> +         */

> +        env->ext_dabt_raised = env->ext_dabt_pending;

>          /* Clear instantly if the call was successful */

>          env->ext_dabt_pending = 0;

>      }

> @@ -755,6 +760,29 @@ int kvm_get_vcpu_events(ARMCPU *cpu)

>  

>  void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)

>  {

> +    ARMCPU *cpu = ARM_CPU(cs);

> +    CPUARMState *env = &cpu->env;

> +

> +    if (unlikely(env->ext_dabt_raised)) {

> +        /*

> +         * Verifying that the ext DABT has been properly injected,

> +         * otherwise risking indefinitely re-running the faulting instruction

> +         * Covering a very narrow case for kernels 5.5..5.5.4

> +         * when injected abort was misconfigured to be

> +         * an IMPLEMENTATION DEFINED exception (for 32-bit EL1)

> +         */

> +        if (!arm_feature(env, ARM_FEATURE_AARCH64) &&

> +            unlikely(!kvm_arm_verify_ext_dabt_pending(cs))) {

> +

> +            error_report("Data abort exception with no valid ISS generated by "

> +                   "guest memory access. KVM unable to emulate faulting "

> +                   "instruction. Failed to inject an external data abort "

> +                   "into the guest.");

> +            abort();

> +       }

> +       /* Clear the status */

> +       env->ext_dabt_raised = 0;

> +    }

>  }

>  

>  MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)

> diff --git a/target/arm/kvm32.c b/target/arm/kvm32.c

> index f271181..86c4fe7 100644

> --- a/target/arm/kvm32.c

> +++ b/target/arm/kvm32.c

> @@ -564,3 +564,28 @@ void kvm_arm_pmu_init(CPUState *cs)

>  {

>      qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);

>  }

> +

> +#define ARM_REG_DFSR  ARM_CP15_REG32(0, 5, 0, 0)

> +#define ARM_REG_TTBCR ARM_CP15_REG32(0, 2, 0, 2)

> +

> +#define DFSR_FSC(v)   (((v) >> 6 | (v)) & 0x1F)

> +#define DFSC_EXTABT(lpae) (lpae) ? 0x10 : 0x08


We should put () around the whole ?: expression when it's in a macro

> +

> +bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)

> +{

> +    uint32_t dfsr_val;

> +

> +    if (!kvm_get_one_reg(cs, ARM_REG_DFSR, &dfsr_val)) {

> +        ARMCPU *cpu = ARM_CPU(cs);

> +        CPUARMState *env = &cpu->env;

> +        uint32_t ttbcr;

> +        int lpae = 0;

> +

> +        if (!kvm_get_one_reg(cs, ARM_REG_TTBCR, &ttbcr)) {

> +            lpae = arm_feature(env, ARM_FEATURE_LPAE) && (ttbcr & TTBCR_EAE);

> +        }

> +        return !(DFSR_FSC(dfsr_val) != DFSC_EXTABT(lpae));


 !(a != b) is a convoluted way to write a == b

> +    }

> +    return false;

> +}

> +

> diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c

> index be5b31c..18594e9 100644

> --- a/target/arm/kvm64.c

> +++ b/target/arm/kvm64.c

> @@ -1430,3 +1430,37 @@ bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)

>  

>      return false;

>  }

> +

> +#define ARM64_REG_ESR_EL1 ARM64_SYS_REG(3, 0, 5, 2, 0)

> +#define ARM64_REG_TCR_EL1 ARM64_SYS_REG(3, 0, 2, 0, 2)

> +

> +#define ESR_DFSC(aarch64, v)    \

> +    ((aarch64) ? ((v) & 0x3F)   \

> +               : (((v) >> 6 | (v)) & 0x1F))

> +

> +#define ESR_DFSC_EXTABT(aarch64, lpae) \

> +    ((aarch64) ? 0x10 : (lpae) ? 0x10 : 0x8)

> +

> +bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)

> +{

> +    uint64_t dfsr_val;

> +

> +    if (!kvm_get_one_reg(cs, ARM64_REG_ESR_EL1, &dfsr_val)) {

> +        ARMCPU *cpu = ARM_CPU(cs);

> +        CPUARMState *env = &cpu->env;

> +        int aarch64_mode = arm_feature(env, ARM_FEATURE_AARCH64);

> +        int lpae = 0;

> +

> +        if (!aarch64_mode) {

> +            uint64_t ttbcr;

> +

> +            if (!kvm_get_one_reg(cs, ARM64_REG_TCR_EL1, &ttbcr)) {

> +                lpae = arm_feature(env, ARM_FEATURE_LPAE)

> +                        && (ttbcr & TTBCR_EAE);

> +            }

> +        }

> +        return !(ESR_DFSC(aarch64_mode, dfsr_val) !=

> +                ESR_DFSC_EXTABT(aarch64_mode, lpae));


a == b, please

> +    }

> +    return false;

> +}

> diff --git a/target/arm/kvm_arm.h b/target/arm/kvm_arm.h

> index 39472d5..f2dc6a2 100644

> --- a/target/arm/kvm_arm.h

> +++ b/target/arm/kvm_arm.h

> @@ -461,6 +461,16 @@ void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr);

>  int kvm_arm_handle_dabt_nisv(CPUState *cs, uint64_t esr_iss,

>                               uint64_t fault_ipa);

>  /**

> + * kvm_arm_verify_ext_dabt_pending:

> + * @cs: CPUState

> + *

> + * Verify the fault status code wrt the Ext DABT injection

> + *

> + * Returns: true if the fault status code is as expected, false otherwise

> + */

> +bool kvm_arm_verify_ext_dabt_pending(CPUState *cs);

> +

> +/**

>   * its_class_name:

>   *

>   * Return the ITS class name to use depending on whether KVM acceleration

> -- 

> 2.7.4

> 

>


I'll leave the decision to take this KVM bug workaround patch at all to Peter,
and I didn't actually review whether or not kvm_arm_verify_ext_dabt_pending
is doing what it claims it's doing, so I'm reluctant to give an r-b on
this patch. But, as far as the code goes, besides the comments above, it
looks fine to me.

Thanks,
drew
Peter Maydell April 7, 2020, 11:24 a.m. UTC | #4
On Fri, 3 Apr 2020 at 09:44, Andrew Jones <drjones@redhat.com> wrote:
>

> On Mon, Mar 23, 2020 at 11:32:27AM +0000, Beata Michalska wrote:

> > Injecting external data abort through KVM might trigger

> > an issue on kernels that do not get updated to include the KVM fix.

> > For those and aarch32 guests, the injected abort gets misconfigured

> > to be an implementation defined exception. This leads to the guest

> > repeatedly re-running the faulting instruction.

> >

> > Add support for handling that case.

> > [

> >   Fixed-by: 018f22f95e8a

> >       ('KVM: arm: Fix DFSR setting for non-LPAE aarch32 guests')

> >   Fixed-by: 21aecdbd7f3a

> >       ('KVM: arm: Make inject_abt32() inject an external abort instead')

> > ]

> >


> I'll leave the decision to take this KVM bug workaround patch at all to Peter,

> and I didn't actually review whether or not kvm_arm_verify_ext_dabt_pending

> is doing what it claims it's doing, so I'm reluctant to give an r-b on

> this patch. But, as far as the code goes, besides the comments above, it

> looks fine to me.


I think that having the workaround for the broken kernels is
reasonable (in fact it might have been my suggestion).

thanks
-- PMM
Beata Michalska April 7, 2020, 11:31 a.m. UTC | #5
On Fri, 3 Apr 2020 at 09:44, Andrew Jones <drjones@redhat.com> wrote:
>

> On Mon, Mar 23, 2020 at 11:32:27AM +0000, Beata Michalska wrote:

> > Injecting external data abort through KVM might trigger

> > an issue on kernels that do not get updated to include the KVM fix.

> > For those and aarch32 guests, the injected abort gets misconfigured

> > to be an implementation defined exception. This leads to the guest

> > repeatedly re-running the faulting instruction.

> >

> > Add support for handling that case.

> > [

> >   Fixed-by: 018f22f95e8a

> >       ('KVM: arm: Fix DFSR setting for non-LPAE aarch32 guests')

> >   Fixed-by: 21aecdbd7f3a

> >       ('KVM: arm: Make inject_abt32() inject an external abort instead')

> > ]

> >

> > Signed-off-by: Beata Michalska <beata.michalska@linaro.org>

> > ---

> >  target/arm/cpu.h     |  1 +

> >  target/arm/kvm.c     | 30 +++++++++++++++++++++++++++++-

> >  target/arm/kvm32.c   | 25 +++++++++++++++++++++++++

> >  target/arm/kvm64.c   | 34 ++++++++++++++++++++++++++++++++++

> >  target/arm/kvm_arm.h | 10 ++++++++++

> >  5 files changed, 99 insertions(+), 1 deletion(-)

> >

> > diff --git a/target/arm/cpu.h b/target/arm/cpu.h

> > index 4f834c1..868afc6 100644

> > --- a/target/arm/cpu.h

> > +++ b/target/arm/cpu.h

> > @@ -561,6 +561,7 @@ typedef struct CPUARMState {

> >      } serror;

> >

> >      uint8_t ext_dabt_pending; /* Request for injecting ext DABT */

> > +    uint8_t ext_dabt_raised; /* Tracking/verifying injection of ext

DABT */
> >

> >      /* State of our input IRQ/FIQ/VIRQ/VFIQ lines */

> >      uint32_t irq_line_state;

> > diff --git a/target/arm/kvm.c b/target/arm/kvm.c

> > index c088589..58ad734 100644

> > --- a/target/arm/kvm.c

> > +++ b/target/arm/kvm.c

> > @@ -721,7 +721,12 @@ int kvm_put_vcpu_events(ARMCPU *cpu)

> >      ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events);

> >      if (ret) {

> >          error_report("failed to put vcpu events");

> > -    } else {

> > +    } else if (env->ext_dabt_pending) {

> > +        /*

> > +         * Mark that the external DABT has been injected,

> > +         * if one has been requested

> > +         */

> > +        env->ext_dabt_raised = env->ext_dabt_pending;

> >          /* Clear instantly if the call was successful */

> >          env->ext_dabt_pending = 0;

> >      }

> > @@ -755,6 +760,29 @@ int kvm_get_vcpu_events(ARMCPU *cpu)

> >

> >  void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)

> >  {

> > +    ARMCPU *cpu = ARM_CPU(cs);

> > +    CPUARMState *env = &cpu->env;

> > +

> > +    if (unlikely(env->ext_dabt_raised)) {

> > +        /*

> > +         * Verifying that the ext DABT has been properly injected,

> > +         * otherwise risking indefinitely re-running the faulting

instruction
> > +         * Covering a very narrow case for kernels 5.5..5.5.4

> > +         * when injected abort was misconfigured to be

> > +         * an IMPLEMENTATION DEFINED exception (for 32-bit EL1)

> > +         */

> > +        if (!arm_feature(env, ARM_FEATURE_AARCH64) &&

> > +            unlikely(!kvm_arm_verify_ext_dabt_pending(cs))) {

> > +

> > +            error_report("Data abort exception with no valid ISS

generated by "
> > +                   "guest memory access. KVM unable to emulate

faulting "
> > +                   "instruction. Failed to inject an external data

abort "
> > +                   "into the guest.");

> > +            abort();

> > +       }

> > +       /* Clear the status */

> > +       env->ext_dabt_raised = 0;

> > +    }

> >  }

> >

> >  MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)

> > diff --git a/target/arm/kvm32.c b/target/arm/kvm32.c

> > index f271181..86c4fe7 100644

> > --- a/target/arm/kvm32.c

> > +++ b/target/arm/kvm32.c

> > @@ -564,3 +564,28 @@ void kvm_arm_pmu_init(CPUState *cs)

> >  {

> >      qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);

> >  }

> > +

> > +#define ARM_REG_DFSR  ARM_CP15_REG32(0, 5, 0, 0)

> > +#define ARM_REG_TTBCR ARM_CP15_REG32(0, 2, 0, 2)

> > +

> > +#define DFSR_FSC(v)   (((v) >> 6 | (v)) & 0x1F)

> > +#define DFSC_EXTABT(lpae) (lpae) ? 0x10 : 0x08

>

> We should put () around the whole ?: expression when it's in a macro

>

> > +

> > +bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)

> > +{

> > +    uint32_t dfsr_val;

> > +

> > +    if (!kvm_get_one_reg(cs, ARM_REG_DFSR, &dfsr_val)) {

> > +        ARMCPU *cpu = ARM_CPU(cs);

> > +        CPUARMState *env = &cpu->env;

> > +        uint32_t ttbcr;

> > +        int lpae = 0;

> > +

> > +        if (!kvm_get_one_reg(cs, ARM_REG_TTBCR, &ttbcr)) {

> > +            lpae = arm_feature(env, ARM_FEATURE_LPAE) && (ttbcr &

TTBCR_EAE);
> > +        }

> > +        return !(DFSR_FSC(dfsr_val) != DFSC_EXTABT(lpae));

>

>  !(a != b) is a convoluted way to write a == b

>

> > +    }

> > +    return false;

> > +}

> > +

> > diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c

> > index be5b31c..18594e9 100644

> > --- a/target/arm/kvm64.c

> > +++ b/target/arm/kvm64.c

> > @@ -1430,3 +1430,37 @@ bool kvm_arm_handle_debug(CPUState *cs, struct

kvm_debug_exit_arch *debug_exit)
> >

> >      return false;

> >  }

> > +

> > +#define ARM64_REG_ESR_EL1 ARM64_SYS_REG(3, 0, 5, 2, 0)

> > +#define ARM64_REG_TCR_EL1 ARM64_SYS_REG(3, 0, 2, 0, 2)

> > +

> > +#define ESR_DFSC(aarch64, v)    \

> > +    ((aarch64) ? ((v) & 0x3F)   \

> > +               : (((v) >> 6 | (v)) & 0x1F))

> > +

> > +#define ESR_DFSC_EXTABT(aarch64, lpae) \

> > +    ((aarch64) ? 0x10 : (lpae) ? 0x10 : 0x8)

> > +

> > +bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)

> > +{

> > +    uint64_t dfsr_val;

> > +

> > +    if (!kvm_get_one_reg(cs, ARM64_REG_ESR_EL1, &dfsr_val)) {

> > +        ARMCPU *cpu = ARM_CPU(cs);

> > +        CPUARMState *env = &cpu->env;

> > +        int aarch64_mode = arm_feature(env, ARM_FEATURE_AARCH64);

> > +        int lpae = 0;

> > +

> > +        if (!aarch64_mode) {

> > +            uint64_t ttbcr;

> > +

> > +            if (!kvm_get_one_reg(cs, ARM64_REG_TCR_EL1, &ttbcr)) {

> > +                lpae = arm_feature(env, ARM_FEATURE_LPAE)

> > +                        && (ttbcr & TTBCR_EAE);

> > +            }

> > +        }

> > +        return !(ESR_DFSC(aarch64_mode, dfsr_val) !=

> > +                ESR_DFSC_EXTABT(aarch64_mode, lpae));

>

> a == b, please

>

> > +    }

> > +    return false;

> > +}

> > diff --git a/target/arm/kvm_arm.h b/target/arm/kvm_arm.h

> > index 39472d5..f2dc6a2 100644

> > --- a/target/arm/kvm_arm.h

> > +++ b/target/arm/kvm_arm.h

> > @@ -461,6 +461,16 @@ void kvm_arm_copy_hw_debug_data(struct

kvm_guest_debug_arch *ptr);
> >  int kvm_arm_handle_dabt_nisv(CPUState *cs, uint64_t esr_iss,

> >                               uint64_t fault_ipa);

> >  /**

> > + * kvm_arm_verify_ext_dabt_pending:

> > + * @cs: CPUState

> > + *

> > + * Verify the fault status code wrt the Ext DABT injection

> > + *

> > + * Returns: true if the fault status code is as expected, false

otherwise
> > + */

> > +bool kvm_arm_verify_ext_dabt_pending(CPUState *cs);

> > +

> > +/**

> >   * its_class_name:

> >   *

> >   * Return the ITS class name to use depending on whether KVM

acceleration
> > --

> > 2.7.4

> >

> >

>

> I'll leave the decision to take this KVM bug workaround patch at all to

Peter,
> and I didn't actually review whether or not

kvm_arm_verify_ext_dabt_pending
> is doing what it claims it's doing, so I'm reluctant to give an r-b on

> this patch. But, as far as the code goes, besides the comments above, it

> looks fine to me.

>

Thanks for the feedback.
Will apply the changes for the next version.

BR
Beata
> Thanks,

> drew

>
<div><br>
<br>
On Fri, 3 Apr 2020 at 09:44, Andrew Jones &lt;<a href="mailto:drjones@redhat.com" target="_blank">drjones@redhat.com</a>&gt; wrote:<br>
&gt;<br></div><div>
&gt; On Mon, Mar 23, 2020 at 11:32:27AM +0000, Beata Michalska wrote:<br>
&gt; &gt; Injecting external data abort through KVM might trigger<br>
&gt; &gt; an issue on kernels that do not get updated to include the KVM fix.<br>
&gt; &gt; For those and aarch32 guests, the injected abort gets misconfigured<br>
&gt; &gt; to be an implementation defined exception. This leads to the guest<br>
&gt; &gt; repeatedly re-running the faulting instruction.<br>
&gt; &gt;<br>
&gt; &gt; Add support for handling that case.<br>
&gt; &gt; [<br>
&gt; &gt;   Fixed-by: 018f22f95e8a<br>
&gt; &gt;       (&#39;KVM: arm: Fix DFSR setting for non-LPAE aarch32 guests&#39;)<br>
&gt; &gt;   Fixed-by: 21aecdbd7f3a<br>
&gt; &gt;       (&#39;KVM: arm: Make inject_abt32() inject an external abort instead&#39;)<br>
&gt; &gt; ]<br>
&gt; &gt;<br>
&gt; &gt; Signed-off-by: Beata Michalska &lt;<a href="mailto:beata.michalska@linaro.org" target="_blank">beata.michalska@linaro.org</a>&gt;<br>
&gt; &gt; ---<br>
&gt; &gt;  target/arm/cpu.h     |  1 +<br>
&gt; &gt;  target/arm/kvm.c     | 30 +++++++++++++++++++++++++++++-<br>
&gt; &gt;  target/arm/kvm32.c   | 25 +++++++++++++++++++++++++<br>
&gt; &gt;  target/arm/kvm64.c   | 34 ++++++++++++++++++++++++++++++++++<br>
&gt; &gt;  target/arm/kvm_arm.h | 10 ++++++++++<br>
&gt; &gt;  5 files changed, 99 insertions(+), 1 deletion(-)<br>
&gt; &gt;<br>
&gt; &gt; diff --git a/target/arm/cpu.h b/target/arm/cpu.h<br>
&gt; &gt; index 4f834c1..868afc6 100644<br>
&gt; &gt; --- a/target/arm/cpu.h<br>
&gt; &gt; +++ b/target/arm/cpu.h<br>
&gt; &gt; @@ -561,6 +561,7 @@ typedef struct CPUARMState {<br>
&gt; &gt;      } serror;<br>
&gt; &gt;<br>
&gt; &gt;      uint8_t ext_dabt_pending; /* Request for injecting ext DABT */<br>
&gt; &gt; +    uint8_t ext_dabt_raised; /* Tracking/verifying injection of ext DABT */<br>
&gt; &gt;<br>
&gt; &gt;      /* State of our input IRQ/FIQ/VIRQ/VFIQ lines */<br>
&gt; &gt;      uint32_t irq_line_state;<br>
&gt; &gt; diff --git a/target/arm/kvm.c b/target/arm/kvm.c<br>
&gt; &gt; index c088589..58ad734 100644<br>
&gt; &gt; --- a/target/arm/kvm.c<br>
&gt; &gt; +++ b/target/arm/kvm.c<br>
&gt; &gt; @@ -721,7 +721,12 @@ int kvm_put_vcpu_events(ARMCPU *cpu)<br>
&gt; &gt;      ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &amp;events);<br>
&gt; &gt;      if (ret) {<br>
&gt; &gt;          error_report(&quot;failed to put vcpu events&quot;);<br>
&gt; &gt; -    } else {<br>
&gt; &gt; +    } else if (env-&gt;ext_dabt_pending) {<br>
&gt; &gt; +        /*<br>
&gt; &gt; +         * Mark that the external DABT has been injected,<br>
&gt; &gt; +         * if one has been requested<br>
&gt; &gt; +         */<br>
&gt; &gt; +        env-&gt;ext_dabt_raised = env-&gt;ext_dabt_pending;<br>
&gt; &gt;          /* Clear instantly if the call was successful */<br>
&gt; &gt;          env-&gt;ext_dabt_pending = 0;<br>
&gt; &gt;      }<br>
&gt; &gt; @@ -755,6 +760,29 @@ int kvm_get_vcpu_events(ARMCPU *cpu)<br>
&gt; &gt;<br>
&gt; &gt;  void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)<br>
&gt; &gt;  {<br>
&gt; &gt; +    ARMCPU *cpu = ARM_CPU(cs);<br>
&gt; &gt; +    CPUARMState *env = &amp;cpu-&gt;env;<br>
&gt; &gt; +<br>
&gt; &gt; +    if (unlikely(env-&gt;ext_dabt_raised)) {<br>
&gt; &gt; +        /*<br>
&gt; &gt; +         * Verifying that the ext DABT has been properly injected,<br>
&gt; &gt; +         * otherwise risking indefinitely re-running the faulting instruction<br>
&gt; &gt; +         * Covering a very narrow case for kernels 5.5..5.5.4<br>
&gt; &gt; +         * when injected abort was misconfigured to be<br>
&gt; &gt; +         * an IMPLEMENTATION DEFINED exception (for 32-bit EL1)<br>
&gt; &gt; +         */<br>
&gt; &gt; +        if (!arm_feature(env, ARM_FEATURE_AARCH64) &amp;&amp;<br>
&gt; &gt; +            unlikely(!kvm_arm_verify_ext_dabt_pending(cs))) {<br>
&gt; &gt; +<br>
&gt; &gt; +            error_report(&quot;Data abort exception with no valid ISS generated by &quot;<br>
&gt; &gt; +                   &quot;guest memory access. KVM unable to emulate faulting &quot;<br>
&gt; &gt; +                   &quot;instruction. Failed to inject an external data abort &quot;<br>
&gt; &gt; +                   &quot;into the guest.&quot;);<br>
&gt; &gt; +            abort();<br>
&gt; &gt; +       }<br>
&gt; &gt; +       /* Clear the status */<br>
&gt; &gt; +       env-&gt;ext_dabt_raised = 0;<br>
&gt; &gt; +    }<br>
&gt; &gt;  }<br>
&gt; &gt;<br>
&gt; &gt;  MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)<br>
&gt; &gt; diff --git a/target/arm/kvm32.c b/target/arm/kvm32.c<br>
&gt; &gt; index f271181..86c4fe7 100644<br>
&gt; &gt; --- a/target/arm/kvm32.c<br>
&gt; &gt; +++ b/target/arm/kvm32.c<br>
&gt; &gt; @@ -564,3 +564,28 @@ void kvm_arm_pmu_init(CPUState *cs)<br>
&gt; &gt;  {<br>
&gt; &gt;      qemu_log_mask(LOG_UNIMP, &quot;%s: not implemented\n&quot;, __func__);<br>
&gt; &gt;  }<br>
&gt; &gt; +<br>
&gt; &gt; +#define ARM_REG_DFSR  ARM_CP15_REG32(0, 5, 0, 0)<br>
&gt; &gt; +#define ARM_REG_TTBCR ARM_CP15_REG32(0, 2, 0, 2)<br>
&gt; &gt; +<br>
&gt; &gt; +#define DFSR_FSC(v)   (((v) &gt;&gt; 6 | (v)) &amp; 0x1F)<br>
&gt; &gt; +#define DFSC_EXTABT(lpae) (lpae) ? 0x10 : 0x08<br>
&gt;<br>
&gt; We should put () around the whole ?: expression when it&#39;s in a macro<br>
&gt;<br>
&gt; &gt; +<br>
&gt; &gt; +bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)<br>
&gt; &gt; +{<br>
&gt; &gt; +    uint32_t dfsr_val;<br>
&gt; &gt; +<br>
&gt; &gt; +    if (!kvm_get_one_reg(cs, ARM_REG_DFSR, &amp;dfsr_val)) {<br>
&gt; &gt; +        ARMCPU *cpu = ARM_CPU(cs);<br>
&gt; &gt; +        CPUARMState *env = &amp;cpu-&gt;env;<br>
&gt; &gt; +        uint32_t ttbcr;<br>
&gt; &gt; +        int lpae = 0;<br>
&gt; &gt; +<br>
&gt; &gt; +        if (!kvm_get_one_reg(cs, ARM_REG_TTBCR, &amp;ttbcr)) {<br>
&gt; &gt; +            lpae = arm_feature(env, ARM_FEATURE_LPAE) &amp;&amp; (ttbcr &amp; TTBCR_EAE);<br>
&gt; &gt; +        }<br>
&gt; &gt; +        return !(DFSR_FSC(dfsr_val) != DFSC_EXTABT(lpae));<br>
&gt;<br>
&gt;  !(a != b) is a convoluted way to write a == b<br>
&gt;<br>
&gt; &gt; +    }<br>
&gt; &gt; +    return false;<br>
&gt; &gt; +}<br>
&gt; &gt; +<br>
&gt; &gt; diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c<br>
&gt; &gt; index be5b31c..18594e9 100644<br>
&gt; &gt; --- a/target/arm/kvm64.c<br>
&gt; &gt; +++ b/target/arm/kvm64.c<br>
&gt; &gt; @@ -1430,3 +1430,37 @@ bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)<br>
&gt; &gt;<br>
&gt; &gt;      return false;<br>
&gt; &gt;  }<br>
&gt; &gt; +<br>
&gt; &gt; +#define ARM64_REG_ESR_EL1 ARM64_SYS_REG(3, 0, 5, 2, 0)<br>
&gt; &gt; +#define ARM64_REG_TCR_EL1 ARM64_SYS_REG(3, 0, 2, 0, 2)<br>
&gt; &gt; +<br>
&gt; &gt; +#define ESR_DFSC(aarch64, v)    \<br>
&gt; &gt; +    ((aarch64) ? ((v) &amp; 0x3F)   \<br>
&gt; &gt; +               : (((v) &gt;&gt; 6 | (v)) &amp; 0x1F))<br>
&gt; &gt; +<br>
&gt; &gt; +#define ESR_DFSC_EXTABT(aarch64, lpae) \<br>
&gt; &gt; +    ((aarch64) ? 0x10 : (lpae) ? 0x10 : 0x8)<br>
&gt; &gt; +<br>
&gt; &gt; +bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)<br>
&gt; &gt; +{<br>
&gt; &gt; +    uint64_t dfsr_val;<br>
&gt; &gt; +<br>
&gt; &gt; +    if (!kvm_get_one_reg(cs, ARM64_REG_ESR_EL1, &amp;dfsr_val)) {<br>
&gt; &gt; +        ARMCPU *cpu = ARM_CPU(cs);<br>
&gt; &gt; +        CPUARMState *env = &amp;cpu-&gt;env;<br>
&gt; &gt; +        int aarch64_mode = arm_feature(env, ARM_FEATURE_AARCH64);<br>
&gt; &gt; +        int lpae = 0;<br>
&gt; &gt; +<br>
&gt; &gt; +        if (!aarch64_mode) {<br>
&gt; &gt; +            uint64_t ttbcr;<br>
&gt; &gt; +<br>
&gt; &gt; +            if (!kvm_get_one_reg(cs, ARM64_REG_TCR_EL1, &amp;ttbcr)) {<br>
&gt; &gt; +                lpae = arm_feature(env, ARM_FEATURE_LPAE)<br>
&gt; &gt; +                        &amp;&amp; (ttbcr &amp; TTBCR_EAE);<br>
&gt; &gt; +            }<br>
&gt; &gt; +        }<br>
&gt; &gt; +        return !(ESR_DFSC(aarch64_mode, dfsr_val) !=<br>
&gt; &gt; +                ESR_DFSC_EXTABT(aarch64_mode, lpae));<br>
&gt;<br>
&gt; a == b, please<br>
&gt;<br>
&gt; &gt; +    }<br>
&gt; &gt; +    return false;<br>
&gt; &gt; +}<br>
&gt; &gt; diff --git a/target/arm/kvm_arm.h b/target/arm/kvm_arm.h<br>
&gt; &gt; index 39472d5..f2dc6a2 100644<br>
&gt; &gt; --- a/target/arm/kvm_arm.h<br>
&gt; &gt; +++ b/target/arm/kvm_arm.h<br>
&gt; &gt; @@ -461,6 +461,16 @@ void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr);<br>
&gt; &gt;  int kvm_arm_handle_dabt_nisv(CPUState *cs, uint64_t esr_iss,<br>
&gt; &gt;                               uint64_t fault_ipa);<br>
&gt; &gt;  /**<br>
&gt; &gt; + * kvm_arm_verify_ext_dabt_pending:<br>
&gt; &gt; + * @cs: CPUState<br>
&gt; &gt; + *<br>
&gt; &gt; + * Verify the fault status code wrt the Ext DABT injection<br>
&gt; &gt; + *<br>
&gt; &gt; + * Returns: true if the fault status code is as expected, false otherwise<br>
&gt; &gt; + */<br>
&gt; &gt; +bool kvm_arm_verify_ext_dabt_pending(CPUState *cs);<br>
&gt; &gt; +<br>
&gt; &gt; +/**<br>
&gt; &gt;   * its_class_name:<br>
&gt; &gt;   *<br>
&gt; &gt;   * Return the ITS class name to use depending on whether KVM acceleration<br>
&gt; &gt; --<br>
&gt; &gt; 2.7.4<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt;<br>
&gt; I&#39;ll leave the decision to take this KVM bug workaround patch at all to Peter,<br>
&gt; and I didn&#39;t actually review whether or not kvm_arm_verify_ext_dabt_pending<br>
&gt; is doing what it claims it&#39;s doing, so I&#39;m reluctant to give an r-b on<br>
&gt; this patch. But, as far as the code goes, besides the comments above, it<br>
&gt; looks fine to me.<br>
&gt;<br></div><div>
Thanks for the feedback.<br>
Will apply the changes for the next version.<br>
<br>
BR<br>
Beata<br>
&gt; Thanks,<br>
&gt; drew<br>
&gt;<br>
</div>
Beata Michalska April 7, 2020, 11:32 a.m. UTC | #6
On Tue, 7 Apr 2020 at 12:24, Peter Maydell <peter.maydell@linaro.org> wrote:
>

> On Fri, 3 Apr 2020 at 09:44, Andrew Jones <drjones@redhat.com> wrote:

> >

> > On Mon, Mar 23, 2020 at 11:32:27AM +0000, Beata Michalska wrote:

> > > Injecting external data abort through KVM might trigger

> > > an issue on kernels that do not get updated to include the KVM fix.

> > > For those and aarch32 guests, the injected abort gets misconfigured

> > > to be an implementation defined exception. This leads to the guest

> > > repeatedly re-running the faulting instruction.

> > >

> > > Add support for handling that case.

> > > [

> > >   Fixed-by: 018f22f95e8a

> > >       ('KVM: arm: Fix DFSR setting for non-LPAE aarch32 guests')

> > >   Fixed-by: 21aecdbd7f3a

> > >       ('KVM: arm: Make inject_abt32() inject an external abort

instead')
> > > ]

> > >

>

> > I'll leave the decision to take this KVM bug workaround patch at all to

Peter,
> > and I didn't actually review whether or not

kvm_arm_verify_ext_dabt_pending
> > is doing what it claims it's doing, so I'm reluctant to give an r-b on

> > this patch. But, as far as the code goes, besides the comments above, it

> > looks fine to me.

>

> I think that having the workaround for the broken kernels is

> reasonable (in fact it might have been my suggestion).

>


I will update the current version to cover the review feedback
and resend the patches soon.

Thanks a lot!

BR
Beata
> thanks

> -- PMM
<div><br>
<br>
On Tue, 7 Apr 2020 at 12:24, Peter Maydell &lt;<a href="mailto:peter.maydell@linaro.org" target="_blank">peter.maydell@linaro.org</a>&gt; wrote:<br>
&gt;<br>
&gt; On Fri, 3 Apr 2020 at 09:44, Andrew Jones &lt;<a href="mailto:drjones@redhat.com" target="_blank">drjones@redhat.com</a>&gt; wrote:<br>
&gt; &gt;<br>
&gt; &gt; On Mon, Mar 23, 2020 at 11:32:27AM +0000, Beata Michalska wrote:<br>
&gt; &gt; &gt; Injecting external data abort through KVM might trigger<br>
&gt; &gt; &gt; an issue on kernels that do not get updated to include the KVM fix.<br>
&gt; &gt; &gt; For those and aarch32 guests, the injected abort gets misconfigured<br>
&gt; &gt; &gt; to be an implementation defined exception. This leads to the guest<br>
&gt; &gt; &gt; repeatedly re-running the faulting instruction.<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Add support for handling that case.<br>
&gt; &gt; &gt; [<br>
&gt; &gt; &gt;   Fixed-by: 018f22f95e8a<br>
&gt; &gt; &gt;       (&#39;KVM: arm: Fix DFSR setting for non-LPAE aarch32 guests&#39;)<br>
&gt; &gt; &gt;   Fixed-by: 21aecdbd7f3a<br>
&gt; &gt; &gt;       (&#39;KVM: arm: Make inject_abt32() inject an external abort instead&#39;)<br>
&gt; &gt; &gt; ]<br>
&gt; &gt; &gt;<br>
&gt;<br>
&gt; &gt; I&#39;ll leave the decision to take this KVM bug workaround patch at all to Peter,<br>
&gt; &gt; and I didn&#39;t actually review whether or not kvm_arm_verify_ext_dabt_pending<br>
&gt; &gt; is doing what it claims it&#39;s doing, so I&#39;m reluctant to give an r-b on<br>
&gt; &gt; this patch. But, as far as the code goes, besides the comments above, it<br>
&gt; &gt; looks fine to me.<br>
&gt;<br>
&gt; I think that having the workaround for the broken kernels is<br>
&gt; reasonable (in fact it might have been my suggestion).<br>
&gt;<br>
<br></div><div>
I will update the current version to cover the review feedback<br>
and resend the patches soon.<br>
<br>
Thanks a lot!<br>
<br>
BR<br>
Beata<br>
&gt; thanks<br>
&gt; -- PMM<br>
</div>
diff mbox series

Patch

diff --git a/target/arm/cpu.h b/target/arm/cpu.h
index 4f834c1..868afc6 100644
--- a/target/arm/cpu.h
+++ b/target/arm/cpu.h
@@ -561,6 +561,7 @@  typedef struct CPUARMState {
     } serror;
 
     uint8_t ext_dabt_pending; /* Request for injecting ext DABT */
+    uint8_t ext_dabt_raised; /* Tracking/verifying injection of ext DABT */
 
     /* State of our input IRQ/FIQ/VIRQ/VFIQ lines */
     uint32_t irq_line_state;
diff --git a/target/arm/kvm.c b/target/arm/kvm.c
index c088589..58ad734 100644
--- a/target/arm/kvm.c
+++ b/target/arm/kvm.c
@@ -721,7 +721,12 @@  int kvm_put_vcpu_events(ARMCPU *cpu)
     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_SET_VCPU_EVENTS, &events);
     if (ret) {
         error_report("failed to put vcpu events");
-    } else {
+    } else if (env->ext_dabt_pending) {
+        /*
+         * Mark that the external DABT has been injected,
+         * if one has been requested
+         */
+        env->ext_dabt_raised = env->ext_dabt_pending;
         /* Clear instantly if the call was successful */
         env->ext_dabt_pending = 0;
     }
@@ -755,6 +760,29 @@  int kvm_get_vcpu_events(ARMCPU *cpu)
 
 void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
 {
+    ARMCPU *cpu = ARM_CPU(cs);
+    CPUARMState *env = &cpu->env;
+
+    if (unlikely(env->ext_dabt_raised)) {
+        /*
+         * Verifying that the ext DABT has been properly injected,
+         * otherwise risking indefinitely re-running the faulting instruction
+         * Covering a very narrow case for kernels 5.5..5.5.4
+         * when injected abort was misconfigured to be
+         * an IMPLEMENTATION DEFINED exception (for 32-bit EL1)
+         */
+        if (!arm_feature(env, ARM_FEATURE_AARCH64) &&
+            unlikely(!kvm_arm_verify_ext_dabt_pending(cs))) {
+
+            error_report("Data abort exception with no valid ISS generated by "
+                   "guest memory access. KVM unable to emulate faulting "
+                   "instruction. Failed to inject an external data abort "
+                   "into the guest.");
+            abort();
+       }
+       /* Clear the status */
+       env->ext_dabt_raised = 0;
+    }
 }
 
 MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
diff --git a/target/arm/kvm32.c b/target/arm/kvm32.c
index f271181..86c4fe7 100644
--- a/target/arm/kvm32.c
+++ b/target/arm/kvm32.c
@@ -564,3 +564,28 @@  void kvm_arm_pmu_init(CPUState *cs)
 {
     qemu_log_mask(LOG_UNIMP, "%s: not implemented\n", __func__);
 }
+
+#define ARM_REG_DFSR  ARM_CP15_REG32(0, 5, 0, 0)
+#define ARM_REG_TTBCR ARM_CP15_REG32(0, 2, 0, 2)
+
+#define DFSR_FSC(v)   (((v) >> 6 | (v)) & 0x1F)
+#define DFSC_EXTABT(lpae) (lpae) ? 0x10 : 0x08
+
+bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)
+{
+    uint32_t dfsr_val;
+
+    if (!kvm_get_one_reg(cs, ARM_REG_DFSR, &dfsr_val)) {
+        ARMCPU *cpu = ARM_CPU(cs);
+        CPUARMState *env = &cpu->env;
+        uint32_t ttbcr;
+        int lpae = 0;
+
+        if (!kvm_get_one_reg(cs, ARM_REG_TTBCR, &ttbcr)) {
+            lpae = arm_feature(env, ARM_FEATURE_LPAE) && (ttbcr & TTBCR_EAE);
+        }
+        return !(DFSR_FSC(dfsr_val) != DFSC_EXTABT(lpae));
+    }
+    return false;
+}
+
diff --git a/target/arm/kvm64.c b/target/arm/kvm64.c
index be5b31c..18594e9 100644
--- a/target/arm/kvm64.c
+++ b/target/arm/kvm64.c
@@ -1430,3 +1430,37 @@  bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit)
 
     return false;
 }
+
+#define ARM64_REG_ESR_EL1 ARM64_SYS_REG(3, 0, 5, 2, 0)
+#define ARM64_REG_TCR_EL1 ARM64_SYS_REG(3, 0, 2, 0, 2)
+
+#define ESR_DFSC(aarch64, v)    \
+    ((aarch64) ? ((v) & 0x3F)   \
+               : (((v) >> 6 | (v)) & 0x1F))
+
+#define ESR_DFSC_EXTABT(aarch64, lpae) \
+    ((aarch64) ? 0x10 : (lpae) ? 0x10 : 0x8)
+
+bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)
+{
+    uint64_t dfsr_val;
+
+    if (!kvm_get_one_reg(cs, ARM64_REG_ESR_EL1, &dfsr_val)) {
+        ARMCPU *cpu = ARM_CPU(cs);
+        CPUARMState *env = &cpu->env;
+        int aarch64_mode = arm_feature(env, ARM_FEATURE_AARCH64);
+        int lpae = 0;
+
+        if (!aarch64_mode) {
+            uint64_t ttbcr;
+
+            if (!kvm_get_one_reg(cs, ARM64_REG_TCR_EL1, &ttbcr)) {
+                lpae = arm_feature(env, ARM_FEATURE_LPAE)
+                        && (ttbcr & TTBCR_EAE);
+            }
+        }
+        return !(ESR_DFSC(aarch64_mode, dfsr_val) !=
+                ESR_DFSC_EXTABT(aarch64_mode, lpae));
+    }
+    return false;
+}
diff --git a/target/arm/kvm_arm.h b/target/arm/kvm_arm.h
index 39472d5..f2dc6a2 100644
--- a/target/arm/kvm_arm.h
+++ b/target/arm/kvm_arm.h
@@ -461,6 +461,16 @@  void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr);
 int kvm_arm_handle_dabt_nisv(CPUState *cs, uint64_t esr_iss,
                              uint64_t fault_ipa);
 /**
+ * kvm_arm_verify_ext_dabt_pending:
+ * @cs: CPUState
+ *
+ * Verify the fault status code wrt the Ext DABT injection
+ *
+ * Returns: true if the fault status code is as expected, false otherwise
+ */
+bool kvm_arm_verify_ext_dabt_pending(CPUState *cs);
+
+/**
  * its_class_name:
  *
  * Return the ITS class name to use depending on whether KVM acceleration