From: David Gibson Date: Wed, 26 Sep 2018 05:46:35 +0000 Subject: Re: [RFC PATCH 22/32] KVM: PPC: Book3S HV: Handle hypercalls correctly when nested Message-Id: <20180926054635.GY30868@umbus.fritz.box> MIME-Version: 1 Content-Type: multipart/mixed; boundary="L+CNnuwY89phXYPb" List-Id: References: <1537524123-9578-23-git-send-email-paulus@ozlabs.org> In-Reply-To: <1537524123-9578-23-git-send-email-paulus@ozlabs.org> To: kvm-ppc@vger.kernel.org --L+CNnuwY89phXYPb Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Sep 21, 2018 at 08:01:53PM +1000, Paul Mackerras wrote: > When we are running as a nested hypervisor, we use a hypercall to > enter the guest rather than code in book3s_hv_rmhandlers.S. This means > that the hypercall handlers listed in hcall_real_table never get called. > There are some hypercalls that are handled there and not in > kvmppc_pseries_do_hcall(), which therefore won't get processed for > a nested guest. >=20 > To fix this, we add cases to kvmppc_pseries_do_hcall() to handle those > hypercalls, with the following exceptions: >=20 > - The HPT hypercalls (H_ENTER, H_REMOVE, etc.) are not handled because > we only support radix mode for nested guests. >=20 > - H_CEDE has to be handled specially because the cede logic in > kvmhv_run_single_vcpu assumes that it has been processed by the time > that kvmhv_p9_guest_entry() returns. Therefore we put a special > case for H_CEDE in kvmhv_p9_guest_entry(). >=20 > For the XICS hypercalls, if real-mode processing is enabled, then the > virtual-mode handlers assume that they are being called only to finish > up the operation. Therefore we turn off the real-mode flag in the XICS > code when running as a nested hypervisor. >=20 > Signed-off-by: Paul Mackerras Reviewed-by: David Gibson > --- > arch/powerpc/include/asm/asm-prototypes.h | 4 +++ > arch/powerpc/kvm/book3s_hv.c | 43 +++++++++++++++++++++++++= ++++++ > arch/powerpc/kvm/book3s_hv_rmhandlers.S | 2 ++ > arch/powerpc/kvm/book3s_xics.c | 3 ++- > 4 files changed, 51 insertions(+), 1 deletion(-) >=20 > diff --git a/arch/powerpc/include/asm/asm-prototypes.h b/arch/powerpc/inc= lude/asm/asm-prototypes.h > index 5c9b00c..c55ba3b 100644 > --- a/arch/powerpc/include/asm/asm-prototypes.h > +++ b/arch/powerpc/include/asm/asm-prototypes.h > @@ -167,4 +167,8 @@ void kvmhv_load_guest_pmu(struct kvm_vcpu *vcpu); > =20 > int __kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu); > =20 > +long kvmppc_h_set_dabr(struct kvm_vcpu *vcpu, unsigned long dabr); > +long kvmppc_h_set_xdabr(struct kvm_vcpu *vcpu, unsigned long dabr, > + unsigned long dabrx); > + > #endif /* _ASM_POWERPC_ASM_PROTOTYPES_H */ > diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c > index 9052a34..61de6ac 100644 > --- a/arch/powerpc/kvm/book3s_hv.c > +++ b/arch/powerpc/kvm/book3s_hv.c > @@ -50,6 +50,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -915,6 +916,19 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) > break; > } > return RESUME_HOST; > + case H_SET_DABR: > + ret =3D kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4)); > + break; > + case H_SET_XDABR: > + ret =3D kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4), > + kvmppc_get_gpr(vcpu, 5)); > + break; > + case H_GET_TCE: > + ret =3D kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4), > + kvmppc_get_gpr(vcpu, 5)); > + if (ret =3D=3D H_TOO_HARD) > + return RESUME_HOST; > + break; > case H_PUT_TCE: > ret =3D kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4), > kvmppc_get_gpr(vcpu, 5), > @@ -938,6 +952,10 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) > if (ret =3D=3D H_TOO_HARD) > return RESUME_HOST; > break; > + case H_RANDOM: > + if (!powernv_get_random_long(&vcpu->arch.regs.gpr[4])) > + ret =3D H_HARDWARE; > + break; > =20 > case H_SET_PARTITION_TABLE: > ret =3D H_FUNCTION; > @@ -963,6 +981,24 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) > return RESUME_GUEST; > } > =20 > +/* > + * Handle H_CEDE in the nested virtualization case where we haven't > + * called the real-mode hcall handlers in book3s_hv_rmhandlers.S. > + * This has to be done early, not in kvmppc_pseries_do_hcall(), so > + * that the cede logic in kvmppc_run_single_vcpu() works properly. > + */ > +static void kvmppc_nested_cede(struct kvm_vcpu *vcpu) > +{ > + vcpu->arch.shregs.msr |=3D MSR_EE; > + vcpu->arch.ceded =3D 1; > + smp_mb(); > + if (vcpu->arch.prodded) { > + vcpu->arch.prodded =3D 0; > + smp_mb(); > + vcpu->arch.ceded =3D 0; > + } > +} > + > static int kvmppc_hcall_impl_hv(unsigned long cmd) > { > switch (cmd) { > @@ -3420,6 +3456,13 @@ int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u6= 4 time_limit) > vcpu->arch.shregs.msr =3D vcpu->arch.regs.msr; > vcpu->arch.shregs.dar =3D mfspr(SPRN_DAR); > vcpu->arch.shregs.dsisr =3D mfspr(SPRN_DSISR); > + > + /* H_CEDE has to be handled now, not later */ > + if (trap =3D=3D BOOK3S_INTERRUPT_SYSCALL && > + kvmppc_get_gpr(vcpu, 3) =3D=3D H_CEDE) { > + kvmppc_nested_cede(vcpu); > + trap =3D 0; > + } > } else { > trap =3D kvmhv_load_hv_regs_and_go(vcpu, time_limit); > } > diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S b/arch/powerpc/kvm/b= ook3s_hv_rmhandlers.S > index 04fcaa4..a4780be 100644 > --- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S > +++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S > @@ -2462,6 +2462,7 @@ hcall_real_table: > hcall_real_table_end: > =20 > _GLOBAL(kvmppc_h_set_xdabr) > +EXPORT_SYMBOL_GPL(kvmppc_h_set_xdabr) > andi. r0, r5, DABRX_USER | DABRX_KERNEL > beq 6f > li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI > @@ -2471,6 +2472,7 @@ _GLOBAL(kvmppc_h_set_xdabr) > blr > =20 > _GLOBAL(kvmppc_h_set_dabr) > +EXPORT_SYMBOL_GPL(kvmppc_h_set_dabr) > li r5, DABRX_USER | DABRX_KERNEL > 3: > BEGIN_FTR_SECTION > diff --git a/arch/powerpc/kvm/book3s_xics.c b/arch/powerpc/kvm/book3s_xic= s.c > index d9ba1b0..b0b2bfc 100644 > --- a/arch/powerpc/kvm/book3s_xics.c > +++ b/arch/powerpc/kvm/book3s_xics.c > @@ -1390,7 +1390,8 @@ static int kvmppc_xics_create(struct kvm_device *de= v, u32 type) > } > =20 > #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE > - if (cpu_has_feature(CPU_FTR_ARCH_206)) { > + if (cpu_has_feature(CPU_FTR_ARCH_206) && > + cpu_has_feature(CPU_FTR_HVMODE)) { > /* Enable real mode support */ > xics->real_mode =3D ENABLE_REALMODE; > xics->real_mode_dbg =3D DEBUG_REALMODE; --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --L+CNnuwY89phXYPb Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEdfRlhq5hpmzETofcbDjKyiDZs5IFAlurHTsACgkQbDjKyiDZ s5LT1Q//QlMdTQihqNTcPwKZsWjv0uiOsYksrerSfNyJWMenEfUM7TAy+eqJr3+1 VrcFLjZdbNCtKovzMYR13RLXTkrp2MlMGTNYdSSainGeY2ac8M0lLWoc9DJO7Pv7 jzZq2sbPkx1JM6FU5Ne3LiqGr59bcwBhVLTrB646LjYrU0A/J5XuLEoi5vhrLRD/ 0kmTjeTdTnkTc0SQuK4GlMGuERlUABq2dm6Y8W6IsEnXIQ73oVNV4JB2uKzzAAwS gSqzOTEai/ze2SmUVpaqv6qYVIqNqA2CbiJVqre6JBvjh1qkvIMjViA/rAIOt4Sc /7BMX/wVzV+sGDIAzCmTg1fh7QP034CT51XhLBt0LgpPiItEm/ifhPYtuMp04gkU 7Rvgz1Fiq4SEvA/MZbQIjVbpgxPSrFmTX3I0+6oNp5RieGiEtL3K1CovWz52ei2C ZOLXendsNKdSOdBX3hKddsvRsABzUD+fHSeT/3vn5dHnIB72tnW0q2HhQjKhlhNV jijTJTI1eoGJFutHdfpjaS6U1bU87hotY/WV98P9jFDiW7M0naqGluN0GhHSz3P5 XzIjQIqt/eHLk3ddFVHf8z5xR591tdYnHcqFp+6D81/EPOEd6ztExsaBcqCWj9ZF 52YYqODp14FybGXebAR2SvQZ47sR9t1cahvYgMgx0BrAN8z+Sm8= =oMfA -----END PGP SIGNATURE----- --L+CNnuwY89phXYPb--