Skip to content

Commit e5282de

Browse files
Pierre MorelVasily Gorbik
authored andcommitted
s390: ap: kvm: add PQAP interception for AQIC
We prepare the interception of the PQAP/AQIC instruction for the case the AQIC facility is enabled in the guest. First of all we do not want to change existing behavior when intercepting AP instructions without the SIE allowing the guest to use AP instructions. In this patch we only handle the AQIC interception allowed by facility 65 which will be enabled when the complete interception infrastructure will be present. We add a callback inside the KVM arch structure for s390 for a VFIO driver to handle a specific response to the PQAP instruction with the AQIC command and only this command. But we want to be able to return a correct answer to the guest even there is no VFIO AP driver in the kernel. Therefor, we inject the correct exceptions from inside KVM for the case the callback is not initialized, which happens when the vfio_ap driver is not loaded. We do consider the responsibility of the driver to always initialize the PQAP callback if it defines queues by initializing the CRYCB for a guest. If the callback has been setup we call it. If not we setup an answer considering that no queue is available for the guest when no callback has been setup. Signed-off-by: Pierre Morel <[email protected]> Reviewed-by: Tony Krowiak <[email protected]> Acked-by: Harald Freudenberger <[email protected]> Acked-by: Christian Borntraeger <[email protected]> Signed-off-by: Halil Pasic <[email protected]> Signed-off-by: Vasily Gorbik <[email protected]>
1 parent da17767 commit e5282de

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

arch/s390/include/asm/kvm_host.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/kvm_host.h>
1919
#include <linux/kvm.h>
2020
#include <linux/seqlock.h>
21+
#include <linux/module.h>
2122
#include <asm/debug.h>
2223
#include <asm/cpu.h>
2324
#include <asm/fpu/api.h>
@@ -720,8 +721,14 @@ struct kvm_s390_cpu_model {
720721
unsigned short ibc;
721722
};
722723

724+
struct kvm_s390_module_hook {
725+
int (*hook)(struct kvm_vcpu *vcpu);
726+
struct module *owner;
727+
};
728+
723729
struct kvm_s390_crypto {
724730
struct kvm_s390_crypto_cb *crycb;
731+
struct kvm_s390_module_hook *pqap_hook;
725732
__u32 crycbd;
726733
__u8 aes_kw;
727734
__u8 dea_kw;

arch/s390/kvm/priv.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <asm/io.h>
2828
#include <asm/ptrace.h>
2929
#include <asm/sclp.h>
30+
#include <asm/ap.h>
3031
#include "gaccess.h"
3132
#include "kvm-s390.h"
3233
#include "trace.h"
@@ -592,6 +593,89 @@ static int handle_io_inst(struct kvm_vcpu *vcpu)
592593
}
593594
}
594595

596+
/*
597+
* handle_pqap: Handling pqap interception
598+
* @vcpu: the vcpu having issue the pqap instruction
599+
*
600+
* We now support PQAP/AQIC instructions and we need to correctly
601+
* answer the guest even if no dedicated driver's hook is available.
602+
*
603+
* The intercepting code calls a dedicated callback for this instruction
604+
* if a driver did register one in the CRYPTO satellite of the
605+
* SIE block.
606+
*
607+
* If no callback is available, the queues are not available, return this
608+
* response code to the caller and set CC to 3.
609+
* Else return the response code returned by the callback.
610+
*/
611+
static int handle_pqap(struct kvm_vcpu *vcpu)
612+
{
613+
struct ap_queue_status status = {};
614+
unsigned long reg0;
615+
int ret;
616+
uint8_t fc;
617+
618+
/* Verify that the AP instruction are available */
619+
if (!ap_instructions_available())
620+
return -EOPNOTSUPP;
621+
/* Verify that the guest is allowed to use AP instructions */
622+
if (!(vcpu->arch.sie_block->eca & ECA_APIE))
623+
return -EOPNOTSUPP;
624+
/*
625+
* The only possibly intercepted functions when AP instructions are
626+
* available for the guest are AQIC and TAPQ with the t bit set
627+
* since we do not set IC.3 (FIII) we currently will only intercept
628+
* the AQIC function code.
629+
*/
630+
reg0 = vcpu->run->s.regs.gprs[0];
631+
fc = (reg0 >> 24) & 0xff;
632+
if (WARN_ON_ONCE(fc != 0x03))
633+
return -EOPNOTSUPP;
634+
635+
/* PQAP instruction is allowed for guest kernel only */
636+
if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
637+
return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
638+
639+
/* Common PQAP instruction specification exceptions */
640+
/* bits 41-47 must all be zeros */
641+
if (reg0 & 0x007f0000UL)
642+
return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
643+
/* APFT not install and T bit set */
644+
if (!test_kvm_facility(vcpu->kvm, 15) && (reg0 & 0x00800000UL))
645+
return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
646+
/* APXA not installed and APID greater 64 or APQI greater 16 */
647+
if (!(vcpu->kvm->arch.crypto.crycbd & 0x02) && (reg0 & 0x0000c0f0UL))
648+
return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
649+
650+
/* AQIC function code specific exception */
651+
/* facility 65 not present for AQIC function code */
652+
if (!test_kvm_facility(vcpu->kvm, 65))
653+
return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
654+
655+
/*
656+
* Verify that the hook callback is registered, lock the owner
657+
* and call the hook.
658+
*/
659+
if (vcpu->kvm->arch.crypto.pqap_hook) {
660+
if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
661+
return -EOPNOTSUPP;
662+
ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
663+
module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
664+
if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
665+
kvm_s390_set_psw_cc(vcpu, 3);
666+
return ret;
667+
}
668+
/*
669+
* A vfio_driver must register a hook.
670+
* No hook means no driver to enable the SIE CRYCB and no queues.
671+
* We send this response to the guest.
672+
*/
673+
status.response_code = 0x01;
674+
memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
675+
kvm_s390_set_psw_cc(vcpu, 3);
676+
return 0;
677+
}
678+
595679
static int handle_stfl(struct kvm_vcpu *vcpu)
596680
{
597681
int rc;
@@ -878,6 +962,8 @@ int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
878962
return handle_sthyi(vcpu);
879963
case 0x7d:
880964
return handle_stsi(vcpu);
965+
case 0xaf:
966+
return handle_pqap(vcpu);
881967
case 0xb1:
882968
return handle_stfl(vcpu);
883969
case 0xb2:

drivers/s390/crypto/vfio_ap_private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/mdev.h>
1717
#include <linux/delay.h>
1818
#include <linux/mutex.h>
19+
#include <linux/kvm_host.h>
1920

2021
#include "ap_bus.h"
2122

@@ -81,6 +82,7 @@ struct ap_matrix_mdev {
8182
struct ap_matrix matrix;
8283
struct notifier_block group_notifier;
8384
struct kvm *kvm;
85+
struct kvm_s390_module_hook pqap_hook;
8486
};
8587

8688
extern int vfio_ap_mdev_register(void);

0 commit comments

Comments
 (0)