aboutsummaryrefslogtreecommitdiffstats
path: root/arch/s390/kvm
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2016-06-15 03:21:46 -0400
committerPaolo Bonzini <pbonzini@redhat.com>2016-06-15 03:21:46 -0400
commitf26ed983264001f4e7b8f068adc76f19761c383d (patch)
treeb04d832d5740f3f4ee20313b6f04afec11b42612 /arch/s390/kvm
parentbb3541f175a977198d128f3a4e13534e019754a3 (diff)
parenta7e19ab55ffdd82f1a8d12694b9a0c0beeef534c (diff)
Merge tag 'kvm-s390-next-4.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/kvms390/linux into HEAD
KVM: s390: Features and fixes for 4.8 part1 Four bigger things: 1. The implementation of the STHYI opcode in the kernel. This is used in libraries like qclib [1] to provide enough information for a capacity and usage based software licence pricing. The STHYI content is defined by the related z/VM documentation [2]. Its data can be composed by accessing several other interfaces provided by LPAR or the machine. This information is partially sensitive or root-only so the kernel does the necessary filtering. 2. Preparation for nested virtualization (VSIE). KVM should query the proper sclp interfaces for the availability of some features before using it. In the past we have been sloppy and simply assumed that several features are available. With this we should be able to handle most cases of a missing feature. 3. CPU model interfaces extended by some additional features that are not covered by a facility bit in STFLE. For example all the crypto instructions of the coprocessor provide a query function. As reality tends to be more complex (e.g. export regulations might block some algorithms) we have to provide additional interfaces to query or set these non-stfle features. 4. Several fixes and changes detected and fixed when doing 1-3. All features change base s390 code. All relevant patches have an ACK from the s390 or component maintainers. The next pull request for 4.8 (part2) will contain the implementation of VSIE. [1] http://www.ibm.com/developerworks/linux/linux390/qclib.html [2] https://www.ibm.com/support/knowledgecenter/SSB27U_6.3.0/com.ibm.zvm.v630.hcpb4/hcpb4sth.htm
Diffstat (limited to 'arch/s390/kvm')
-rw-r--r--arch/s390/kvm/Makefile2
-rw-r--r--arch/s390/kvm/diag.c5
-rw-r--r--arch/s390/kvm/gaccess.c152
-rw-r--r--arch/s390/kvm/intercept.c15
-rw-r--r--arch/s390/kvm/interrupt.c26
-rw-r--r--arch/s390/kvm/kvm-s390.c255
-rw-r--r--arch/s390/kvm/kvm-s390.h9
-rw-r--r--arch/s390/kvm/priv.c217
-rw-r--r--arch/s390/kvm/sthyi.c471
-rw-r--r--arch/s390/kvm/trace.h49
10 files changed, 1055 insertions, 146 deletions
diff --git a/arch/s390/kvm/Makefile b/arch/s390/kvm/Makefile
index d42fa38c2429..82e73e2b953d 100644
--- a/arch/s390/kvm/Makefile
+++ b/arch/s390/kvm/Makefile
@@ -12,6 +12,6 @@ common-objs = $(KVM)/kvm_main.o $(KVM)/eventfd.o $(KVM)/async_pf.o $(KVM)/irqch
12ccflags-y := -Ivirt/kvm -Iarch/s390/kvm 12ccflags-y := -Ivirt/kvm -Iarch/s390/kvm
13 13
14kvm-objs := $(common-objs) kvm-s390.o intercept.o interrupt.o priv.o sigp.o 14kvm-objs := $(common-objs) kvm-s390.o intercept.o interrupt.o priv.o sigp.o
15kvm-objs += diag.o gaccess.o guestdbg.o 15kvm-objs += diag.o gaccess.o guestdbg.o sthyi.o
16 16
17obj-$(CONFIG_KVM) += kvm.o 17obj-$(CONFIG_KVM) += kvm.o
diff --git a/arch/s390/kvm/diag.c b/arch/s390/kvm/diag.c
index 1ea4095b67d7..ce865bd4f81d 100644
--- a/arch/s390/kvm/diag.c
+++ b/arch/s390/kvm/diag.c
@@ -212,6 +212,11 @@ static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
212 (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY)) 212 (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
213 return -EOPNOTSUPP; 213 return -EOPNOTSUPP;
214 214
215 VCPU_EVENT(vcpu, 4, "diag 0x500 schid 0x%8.8x queue 0x%x cookie 0x%llx",
216 (u32) vcpu->run->s.regs.gprs[2],
217 (u32) vcpu->run->s.regs.gprs[3],
218 vcpu->run->s.regs.gprs[4]);
219
215 /* 220 /*
216 * The layout is as follows: 221 * The layout is as follows:
217 * - gpr 2 contains the subchannel id (passed as addr) 222 * - gpr 2 contains the subchannel id (passed as addr)
diff --git a/arch/s390/kvm/gaccess.c b/arch/s390/kvm/gaccess.c
index 66938d283b77..8e245e764c21 100644
--- a/arch/s390/kvm/gaccess.c
+++ b/arch/s390/kvm/gaccess.c
@@ -476,18 +476,73 @@ enum {
476 FSI_FETCH = 2 /* Exception was due to fetch operation */ 476 FSI_FETCH = 2 /* Exception was due to fetch operation */
477}; 477};
478 478
479static int get_vcpu_asce(struct kvm_vcpu *vcpu, union asce *asce, 479enum prot_type {
480 ar_t ar, enum gacc_mode mode) 480 PROT_TYPE_LA = 0,
481 PROT_TYPE_KEYC = 1,
482 PROT_TYPE_ALC = 2,
483 PROT_TYPE_DAT = 3,
484};
485
486static int trans_exc(struct kvm_vcpu *vcpu, int code, unsigned long gva,
487 ar_t ar, enum gacc_mode mode, enum prot_type prot)
481{ 488{
482 int rc;
483 struct psw_bits psw = psw_bits(vcpu->arch.sie_block->gpsw);
484 struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm; 489 struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
485 struct trans_exc_code_bits *tec_bits; 490 struct trans_exc_code_bits *tec;
486 491
487 memset(pgm, 0, sizeof(*pgm)); 492 memset(pgm, 0, sizeof(*pgm));
488 tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code; 493 pgm->code = code;
489 tec_bits->fsi = mode == GACC_STORE ? FSI_STORE : FSI_FETCH; 494 tec = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
490 tec_bits->as = psw.as; 495
496 switch (code) {
497 case PGM_ASCE_TYPE:
498 case PGM_PAGE_TRANSLATION:
499 case PGM_REGION_FIRST_TRANS:
500 case PGM_REGION_SECOND_TRANS:
501 case PGM_REGION_THIRD_TRANS:
502 case PGM_SEGMENT_TRANSLATION:
503 /*
504 * op_access_id only applies to MOVE_PAGE -> set bit 61
505 * exc_access_id has to be set to 0 for some instructions. Both
506 * cases have to be handled by the caller. We can always store
507 * exc_access_id, as it is undefined for non-ar cases.
508 */
509 tec->addr = gva >> PAGE_SHIFT;
510 tec->fsi = mode == GACC_STORE ? FSI_STORE : FSI_FETCH;
511 tec->as = psw_bits(vcpu->arch.sie_block->gpsw).as;
512 /* FALL THROUGH */
513 case PGM_ALEN_TRANSLATION:
514 case PGM_ALE_SEQUENCE:
515 case PGM_ASTE_VALIDITY:
516 case PGM_ASTE_SEQUENCE:
517 case PGM_EXTENDED_AUTHORITY:
518 pgm->exc_access_id = ar;
519 break;
520 case PGM_PROTECTION:
521 switch (prot) {
522 case PROT_TYPE_ALC:
523 tec->b60 = 1;
524 /* FALL THROUGH */
525 case PROT_TYPE_DAT:
526 tec->b61 = 1;
527 tec->addr = gva >> PAGE_SHIFT;
528 tec->fsi = mode == GACC_STORE ? FSI_STORE : FSI_FETCH;
529 tec->as = psw_bits(vcpu->arch.sie_block->gpsw).as;
530 /* exc_access_id is undefined for most cases */
531 pgm->exc_access_id = ar;
532 break;
533 default: /* LA and KEYC set b61 to 0, other params undefined */
534 break;
535 }
536 break;
537 }
538 return code;
539}
540
541static int get_vcpu_asce(struct kvm_vcpu *vcpu, union asce *asce,
542 unsigned long ga, ar_t ar, enum gacc_mode mode)
543{
544 int rc;
545 struct psw_bits psw = psw_bits(vcpu->arch.sie_block->gpsw);
491 546
492 if (!psw.t) { 547 if (!psw.t) {
493 asce->val = 0; 548 asce->val = 0;
@@ -510,21 +565,8 @@ static int get_vcpu_asce(struct kvm_vcpu *vcpu, union asce *asce,
510 return 0; 565 return 0;
511 case PSW_AS_ACCREG: 566 case PSW_AS_ACCREG:
512 rc = ar_translation(vcpu, asce, ar, mode); 567 rc = ar_translation(vcpu, asce, ar, mode);
513 switch (rc) {
514 case PGM_ALEN_TRANSLATION:
515 case PGM_ALE_SEQUENCE:
516 case PGM_ASTE_VALIDITY:
517 case PGM_ASTE_SEQUENCE:
518 case PGM_EXTENDED_AUTHORITY:
519 vcpu->arch.pgm.exc_access_id = ar;
520 break;
521 case PGM_PROTECTION:
522 tec_bits->b60 = 1;
523 tec_bits->b61 = 1;
524 break;
525 }
526 if (rc > 0) 568 if (rc > 0)
527 pgm->code = rc; 569 return trans_exc(vcpu, rc, ga, ar, mode, PROT_TYPE_ALC);
528 return rc; 570 return rc;
529 } 571 }
530 return 0; 572 return 0;
@@ -729,40 +771,31 @@ static int low_address_protection_enabled(struct kvm_vcpu *vcpu,
729 return 1; 771 return 1;
730} 772}
731 773
732static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, 774static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, ar_t ar,
733 unsigned long *pages, unsigned long nr_pages, 775 unsigned long *pages, unsigned long nr_pages,
734 const union asce asce, enum gacc_mode mode) 776 const union asce asce, enum gacc_mode mode)
735{ 777{
736 struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
737 psw_t *psw = &vcpu->arch.sie_block->gpsw; 778 psw_t *psw = &vcpu->arch.sie_block->gpsw;
738 struct trans_exc_code_bits *tec_bits; 779 int lap_enabled, rc = 0;
739 int lap_enabled, rc;
740 780
741 tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
742 lap_enabled = low_address_protection_enabled(vcpu, asce); 781 lap_enabled = low_address_protection_enabled(vcpu, asce);
743 while (nr_pages) { 782 while (nr_pages) {
744 ga = kvm_s390_logical_to_effective(vcpu, ga); 783 ga = kvm_s390_logical_to_effective(vcpu, ga);
745 tec_bits->addr = ga >> PAGE_SHIFT; 784 if (mode == GACC_STORE && lap_enabled && is_low_address(ga))
746 if (mode == GACC_STORE && lap_enabled && is_low_address(ga)) { 785 return trans_exc(vcpu, PGM_PROTECTION, ga, ar, mode,
747 pgm->code = PGM_PROTECTION; 786 PROT_TYPE_LA);
748 return pgm->code;
749 }
750 ga &= PAGE_MASK; 787 ga &= PAGE_MASK;
751 if (psw_bits(*psw).t) { 788 if (psw_bits(*psw).t) {
752 rc = guest_translate(vcpu, ga, pages, asce, mode); 789 rc = guest_translate(vcpu, ga, pages, asce, mode);
753 if (rc < 0) 790 if (rc < 0)
754 return rc; 791 return rc;
755 if (rc == PGM_PROTECTION)
756 tec_bits->b61 = 1;
757 if (rc)
758 pgm->code = rc;
759 } else { 792 } else {
760 *pages = kvm_s390_real_to_abs(vcpu, ga); 793 *pages = kvm_s390_real_to_abs(vcpu, ga);
761 if (kvm_is_error_gpa(vcpu->kvm, *pages)) 794 if (kvm_is_error_gpa(vcpu->kvm, *pages))
762 pgm->code = PGM_ADDRESSING; 795 rc = PGM_ADDRESSING;
763 } 796 }
764 if (pgm->code) 797 if (rc)
765 return pgm->code; 798 return trans_exc(vcpu, rc, ga, ar, mode, PROT_TYPE_DAT);
766 ga += PAGE_SIZE; 799 ga += PAGE_SIZE;
767 pages++; 800 pages++;
768 nr_pages--; 801 nr_pages--;
@@ -783,7 +816,8 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, ar_t ar, void *data,
783 816
784 if (!len) 817 if (!len)
785 return 0; 818 return 0;
786 rc = get_vcpu_asce(vcpu, &asce, ar, mode); 819 ga = kvm_s390_logical_to_effective(vcpu, ga);
820 rc = get_vcpu_asce(vcpu, &asce, ga, ar, mode);
787 if (rc) 821 if (rc)
788 return rc; 822 return rc;
789 nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1; 823 nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1;
@@ -795,7 +829,7 @@ int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, ar_t ar, void *data,
795 need_ipte_lock = psw_bits(*psw).t && !asce.r; 829 need_ipte_lock = psw_bits(*psw).t && !asce.r;
796 if (need_ipte_lock) 830 if (need_ipte_lock)
797 ipte_lock(vcpu); 831 ipte_lock(vcpu);
798 rc = guest_page_range(vcpu, ga, pages, nr_pages, asce, mode); 832 rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode);
799 for (idx = 0; idx < nr_pages && !rc; idx++) { 833 for (idx = 0; idx < nr_pages && !rc; idx++) {
800 gpa = *(pages + idx) + (ga & ~PAGE_MASK); 834 gpa = *(pages + idx) + (ga & ~PAGE_MASK);
801 _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len); 835 _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
@@ -846,37 +880,28 @@ int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
846int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, ar_t ar, 880int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, ar_t ar,
847 unsigned long *gpa, enum gacc_mode mode) 881 unsigned long *gpa, enum gacc_mode mode)
848{ 882{
849 struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
850 psw_t *psw = &vcpu->arch.sie_block->gpsw; 883 psw_t *psw = &vcpu->arch.sie_block->gpsw;
851 struct trans_exc_code_bits *tec;
852 union asce asce; 884 union asce asce;
853 int rc; 885 int rc;
854 886
855 gva = kvm_s390_logical_to_effective(vcpu, gva); 887 gva = kvm_s390_logical_to_effective(vcpu, gva);
856 tec = (struct trans_exc_code_bits *)&pgm->trans_exc_code; 888 rc = get_vcpu_asce(vcpu, &asce, gva, ar, mode);
857 rc = get_vcpu_asce(vcpu, &asce, ar, mode);
858 tec->addr = gva >> PAGE_SHIFT;
859 if (rc) 889 if (rc)
860 return rc; 890 return rc;
861 if (is_low_address(gva) && low_address_protection_enabled(vcpu, asce)) { 891 if (is_low_address(gva) && low_address_protection_enabled(vcpu, asce)) {
862 if (mode == GACC_STORE) { 892 if (mode == GACC_STORE)
863 rc = pgm->code = PGM_PROTECTION; 893 return trans_exc(vcpu, PGM_PROTECTION, gva, 0,
864 return rc; 894 mode, PROT_TYPE_LA);
865 }
866 } 895 }
867 896
868 if (psw_bits(*psw).t && !asce.r) { /* Use DAT? */ 897 if (psw_bits(*psw).t && !asce.r) { /* Use DAT? */
869 rc = guest_translate(vcpu, gva, gpa, asce, mode); 898 rc = guest_translate(vcpu, gva, gpa, asce, mode);
870 if (rc > 0) { 899 if (rc > 0)
871 if (rc == PGM_PROTECTION) 900 return trans_exc(vcpu, rc, gva, 0, mode, PROT_TYPE_DAT);
872 tec->b61 = 1;
873 pgm->code = rc;
874 }
875 } else { 901 } else {
876 rc = 0;
877 *gpa = kvm_s390_real_to_abs(vcpu, gva); 902 *gpa = kvm_s390_real_to_abs(vcpu, gva);
878 if (kvm_is_error_gpa(vcpu->kvm, *gpa)) 903 if (kvm_is_error_gpa(vcpu->kvm, *gpa))
879 rc = pgm->code = PGM_ADDRESSING; 904 return trans_exc(vcpu, rc, gva, PGM_ADDRESSING, mode, 0);
880 } 905 }
881 906
882 return rc; 907 return rc;
@@ -915,20 +940,9 @@ int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, ar_t ar,
915 */ 940 */
916int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra) 941int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra)
917{ 942{
918 struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
919 psw_t *psw = &vcpu->arch.sie_block->gpsw;
920 struct trans_exc_code_bits *tec_bits;
921 union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]}; 943 union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]};
922 944
923 if (!ctlreg0.lap || !is_low_address(gra)) 945 if (!ctlreg0.lap || !is_low_address(gra))
924 return 0; 946 return 0;
925 947 return trans_exc(vcpu, PGM_PROTECTION, gra, 0, GACC_STORE, PROT_TYPE_LA);
926 memset(pgm, 0, sizeof(*pgm));
927 tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
928 tec_bits->fsi = FSI_STORE;
929 tec_bits->as = psw_bits(*psw).as;
930 tec_bits->addr = gra >> PAGE_SHIFT;
931 pgm->code = PGM_PROTECTION;
932
933 return pgm->code;
934} 948}
diff --git a/arch/s390/kvm/intercept.c b/arch/s390/kvm/intercept.c
index 2e6b54e4d3f9..9359f65c8634 100644
--- a/arch/s390/kvm/intercept.c
+++ b/arch/s390/kvm/intercept.c
@@ -349,6 +349,19 @@ static int handle_partial_execution(struct kvm_vcpu *vcpu)
349 return -EOPNOTSUPP; 349 return -EOPNOTSUPP;
350} 350}
351 351
352static int handle_operexc(struct kvm_vcpu *vcpu)
353{
354 vcpu->stat.exit_operation_exception++;
355 trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
356 vcpu->arch.sie_block->ipb);
357
358 if (vcpu->arch.sie_block->ipa == 0xb256 &&
359 test_kvm_facility(vcpu->kvm, 74))
360 return handle_sthyi(vcpu);
361
362 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
363}
364
352int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu) 365int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
353{ 366{
354 if (kvm_is_ucontrol(vcpu->kvm)) 367 if (kvm_is_ucontrol(vcpu->kvm))
@@ -370,6 +383,8 @@ int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
370 return handle_validity(vcpu); 383 return handle_validity(vcpu);
371 case 0x28: 384 case 0x28:
372 return handle_stop(vcpu); 385 return handle_stop(vcpu);
386 case 0x2c:
387 return handle_operexc(vcpu);
373 case 0x38: 388 case 0x38:
374 return handle_partial_execution(vcpu); 389 return handle_partial_execution(vcpu);
375 default: 390 default:
diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
index 5a80af740d3e..d72c4a877622 100644
--- a/arch/s390/kvm/interrupt.c
+++ b/arch/s390/kvm/interrupt.c
@@ -28,9 +28,6 @@
28#include "gaccess.h" 28#include "gaccess.h"
29#include "trace-s390.h" 29#include "trace-s390.h"
30 30
31#define IOINT_SCHID_MASK 0x0000ffff
32#define IOINT_SSID_MASK 0x00030000
33#define IOINT_CSSID_MASK 0x03fc0000
34#define PFAULT_INIT 0x0600 31#define PFAULT_INIT 0x0600
35#define PFAULT_DONE 0x0680 32#define PFAULT_DONE 0x0680
36#define VIRTIO_PARAM 0x0d00 33#define VIRTIO_PARAM 0x0d00
@@ -821,7 +818,14 @@ static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
821 struct kvm_s390_interrupt_info, 818 struct kvm_s390_interrupt_info,
822 list); 819 list);
823 if (inti) { 820 if (inti) {
824 VCPU_EVENT(vcpu, 4, "deliver: I/O 0x%llx", inti->type); 821 if (inti->type & KVM_S390_INT_IO_AI_MASK)
822 VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)");
823 else
824 VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x",
825 inti->io.subchannel_id >> 8,
826 inti->io.subchannel_id >> 1 & 0x3,
827 inti->io.subchannel_nr);
828
825 vcpu->stat.deliver_io_int++; 829 vcpu->stat.deliver_io_int++;
826 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, 830 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
827 inti->type, 831 inti->type,
@@ -1415,6 +1419,13 @@ static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1415 } 1419 }
1416 fi->counters[FIRQ_CNTR_IO] += 1; 1420 fi->counters[FIRQ_CNTR_IO] += 1;
1417 1421
1422 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1423 VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1424 else
1425 VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1426 inti->io.subchannel_id >> 8,
1427 inti->io.subchannel_id >> 1 & 0x3,
1428 inti->io.subchannel_nr);
1418 isc = int_word_to_isc(inti->io.io_int_word); 1429 isc = int_word_to_isc(inti->io.io_int_word);
1419 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc]; 1430 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1420 list_add_tail(&inti->list, list); 1431 list_add_tail(&inti->list, list);
@@ -1531,13 +1542,6 @@ int kvm_s390_inject_vm(struct kvm *kvm,
1531 inti->mchk.mcic = s390int->parm64; 1542 inti->mchk.mcic = s390int->parm64;
1532 break; 1543 break;
1533 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: 1544 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1534 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1535 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1536 else
1537 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1538 s390int->type & IOINT_CSSID_MASK,
1539 s390int->type & IOINT_SSID_MASK,
1540 s390int->type & IOINT_SCHID_MASK);
1541 inti->io.subchannel_id = s390int->parm >> 16; 1545 inti->io.subchannel_id = s390int->parm >> 16;
1542 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu; 1546 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1543 inti->io.io_int_parm = s390int->parm64 >> 32; 1547 inti->io.io_int_parm = s390int->parm64 >> 32;
diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
index 6d8ec3ac9dd8..49c60393a15c 100644
--- a/arch/s390/kvm/kvm-s390.c
+++ b/arch/s390/kvm/kvm-s390.c
@@ -26,6 +26,7 @@
26#include <linux/slab.h> 26#include <linux/slab.h>
27#include <linux/timer.h> 27#include <linux/timer.h>
28#include <linux/vmalloc.h> 28#include <linux/vmalloc.h>
29#include <linux/bitmap.h>
29#include <asm/asm-offsets.h> 30#include <asm/asm-offsets.h>
30#include <asm/lowcore.h> 31#include <asm/lowcore.h>
31#include <asm/etr.h> 32#include <asm/etr.h>
@@ -35,6 +36,8 @@
35#include <asm/switch_to.h> 36#include <asm/switch_to.h>
36#include <asm/isc.h> 37#include <asm/isc.h>
37#include <asm/sclp.h> 38#include <asm/sclp.h>
39#include <asm/cpacf.h>
40#include <asm/etr.h>
38#include "kvm-s390.h" 41#include "kvm-s390.h"
39#include "gaccess.h" 42#include "gaccess.h"
40 43
@@ -63,6 +66,7 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
63 { "exit_instruction", VCPU_STAT(exit_instruction) }, 66 { "exit_instruction", VCPU_STAT(exit_instruction) },
64 { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, 67 { "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
65 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, 68 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
69 { "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
66 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, 70 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
67 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) }, 71 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
68 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) }, 72 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
@@ -93,6 +97,7 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
93 { "instruction_stsi", VCPU_STAT(instruction_stsi) }, 97 { "instruction_stsi", VCPU_STAT(instruction_stsi) },
94 { "instruction_stfl", VCPU_STAT(instruction_stfl) }, 98 { "instruction_stfl", VCPU_STAT(instruction_stfl) },
95 { "instruction_tprot", VCPU_STAT(instruction_tprot) }, 99 { "instruction_tprot", VCPU_STAT(instruction_tprot) },
100 { "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
96 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, 101 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
97 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, 102 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
98 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, 103 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
@@ -130,6 +135,11 @@ unsigned long kvm_s390_fac_list_mask_size(void)
130 return ARRAY_SIZE(kvm_s390_fac_list_mask); 135 return ARRAY_SIZE(kvm_s390_fac_list_mask);
131} 136}
132 137
138/* available cpu features supported by kvm */
139static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
140/* available subfunctions indicated via query / "test bit" */
141static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
142
133static struct gmap_notifier gmap_notifier; 143static struct gmap_notifier gmap_notifier;
134debug_info_t *kvm_s390_dbf; 144debug_info_t *kvm_s390_dbf;
135 145
@@ -187,6 +197,61 @@ void kvm_arch_hardware_unsetup(void)
187 &kvm_clock_notifier); 197 &kvm_clock_notifier);
188} 198}
189 199
200static void allow_cpu_feat(unsigned long nr)
201{
202 set_bit_inv(nr, kvm_s390_available_cpu_feat);
203}
204
205static inline int plo_test_bit(unsigned char nr)
206{
207 register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
208 int cc = 3; /* subfunction not available */
209
210 asm volatile(
211 /* Parameter registers are ignored for "test bit" */
212 " plo 0,0,0,0(0)\n"
213 " ipm %0\n"
214 " srl %0,28\n"
215 : "=d" (cc)
216 : "d" (r0)
217 : "cc");
218 return cc == 0;
219}
220
221static void kvm_s390_cpu_feat_init(void)
222{
223 int i;
224
225 for (i = 0; i < 256; ++i) {
226 if (plo_test_bit(i))
227 kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
228 }
229
230 if (test_facility(28)) /* TOD-clock steering */
231 etr_ptff(kvm_s390_available_subfunc.ptff, ETR_PTFF_QAF);
232
233 if (test_facility(17)) { /* MSA */
234 __cpacf_query(CPACF_KMAC, kvm_s390_available_subfunc.kmac);
235 __cpacf_query(CPACF_KMC, kvm_s390_available_subfunc.kmc);
236 __cpacf_query(CPACF_KM, kvm_s390_available_subfunc.km);
237 __cpacf_query(CPACF_KIMD, kvm_s390_available_subfunc.kimd);
238 __cpacf_query(CPACF_KLMD, kvm_s390_available_subfunc.klmd);
239 }
240 if (test_facility(76)) /* MSA3 */
241 __cpacf_query(CPACF_PCKMO, kvm_s390_available_subfunc.pckmo);
242 if (test_facility(77)) { /* MSA4 */
243 __cpacf_query(CPACF_KMCTR, kvm_s390_available_subfunc.kmctr);
244 __cpacf_query(CPACF_KMF, kvm_s390_available_subfunc.kmf);
245 __cpacf_query(CPACF_KMO, kvm_s390_available_subfunc.kmo);
246 __cpacf_query(CPACF_PCC, kvm_s390_available_subfunc.pcc);
247 }
248 if (test_facility(57)) /* MSA5 */
249 __cpacf_query(CPACF_PPNO, kvm_s390_available_subfunc.ppno);
250
251 if (MACHINE_HAS_ESOP)
252 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
253}
254
190int kvm_arch_init(void *opaque) 255int kvm_arch_init(void *opaque)
191{ 256{
192 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 257 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
@@ -198,6 +263,8 @@ int kvm_arch_init(void *opaque)
198 return -ENOMEM; 263 return -ENOMEM;
199 } 264 }
200 265
266 kvm_s390_cpu_feat_init();
267
201 /* Register floating interrupt controller interface. */ 268 /* Register floating interrupt controller interface. */
202 return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 269 return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
203} 270}
@@ -250,8 +317,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
250 break; 317 break;
251 case KVM_CAP_NR_VCPUS: 318 case KVM_CAP_NR_VCPUS:
252 case KVM_CAP_MAX_VCPUS: 319 case KVM_CAP_MAX_VCPUS:
253 r = sclp.has_esca ? KVM_S390_ESCA_CPU_SLOTS 320 r = KVM_S390_BSCA_CPU_SLOTS;
254 : KVM_S390_BSCA_CPU_SLOTS; 321 if (sclp.has_esca && sclp.has_64bscao)
322 r = KVM_S390_ESCA_CPU_SLOTS;
255 break; 323 break;
256 case KVM_CAP_NR_MEMSLOTS: 324 case KVM_CAP_NR_MEMSLOTS:
257 r = KVM_USER_MEM_SLOTS; 325 r = KVM_USER_MEM_SLOTS;
@@ -417,9 +485,8 @@ static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *att
417 unsigned int idx; 485 unsigned int idx;
418 switch (attr->attr) { 486 switch (attr->attr) {
419 case KVM_S390_VM_MEM_ENABLE_CMMA: 487 case KVM_S390_VM_MEM_ENABLE_CMMA:
420 /* enable CMMA only for z10 and later (EDAT_1) */ 488 ret = -ENXIO;
421 ret = -EINVAL; 489 if (!sclp.has_cmma)
422 if (!MACHINE_IS_LPAR || !MACHINE_HAS_EDAT1)
423 break; 490 break;
424 491
425 ret = -EBUSY; 492 ret = -EBUSY;
@@ -432,6 +499,9 @@ static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *att
432 mutex_unlock(&kvm->lock); 499 mutex_unlock(&kvm->lock);
433 break; 500 break;
434 case KVM_S390_VM_MEM_CLR_CMMA: 501 case KVM_S390_VM_MEM_CLR_CMMA:
502 ret = -ENXIO;
503 if (!sclp.has_cmma)
504 break;
435 ret = -EINVAL; 505 ret = -EINVAL;
436 if (!kvm->arch.use_cmma) 506 if (!kvm->arch.use_cmma)
437 break; 507 break;
@@ -675,6 +745,39 @@ out:
675 return ret; 745 return ret;
676} 746}
677 747
748static int kvm_s390_set_processor_feat(struct kvm *kvm,
749 struct kvm_device_attr *attr)
750{
751 struct kvm_s390_vm_cpu_feat data;
752 int ret = -EBUSY;
753
754 if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
755 return -EFAULT;
756 if (!bitmap_subset((unsigned long *) data.feat,
757 kvm_s390_available_cpu_feat,
758 KVM_S390_VM_CPU_FEAT_NR_BITS))
759 return -EINVAL;
760
761 mutex_lock(&kvm->lock);
762 if (!atomic_read(&kvm->online_vcpus)) {
763 bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
764 KVM_S390_VM_CPU_FEAT_NR_BITS);
765 ret = 0;
766 }
767 mutex_unlock(&kvm->lock);
768 return ret;
769}
770
771static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
772 struct kvm_device_attr *attr)
773{
774 /*
775 * Once supported by kernel + hw, we have to store the subfunctions
776 * in kvm->arch and remember that user space configured them.
777 */
778 return -ENXIO;
779}
780
678static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 781static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
679{ 782{
680 int ret = -ENXIO; 783 int ret = -ENXIO;
@@ -683,6 +786,12 @@ static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
683 case KVM_S390_VM_CPU_PROCESSOR: 786 case KVM_S390_VM_CPU_PROCESSOR:
684 ret = kvm_s390_set_processor(kvm, attr); 787 ret = kvm_s390_set_processor(kvm, attr);
685 break; 788 break;
789 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
790 ret = kvm_s390_set_processor_feat(kvm, attr);
791 break;
792 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
793 ret = kvm_s390_set_processor_subfunc(kvm, attr);
794 break;
686 } 795 }
687 return ret; 796 return ret;
688} 797}
@@ -731,6 +840,50 @@ out:
731 return ret; 840 return ret;
732} 841}
733 842
843static int kvm_s390_get_processor_feat(struct kvm *kvm,
844 struct kvm_device_attr *attr)
845{
846 struct kvm_s390_vm_cpu_feat data;
847
848 bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
849 KVM_S390_VM_CPU_FEAT_NR_BITS);
850 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
851 return -EFAULT;
852 return 0;
853}
854
855static int kvm_s390_get_machine_feat(struct kvm *kvm,
856 struct kvm_device_attr *attr)
857{
858 struct kvm_s390_vm_cpu_feat data;
859
860 bitmap_copy((unsigned long *) data.feat,
861 kvm_s390_available_cpu_feat,
862 KVM_S390_VM_CPU_FEAT_NR_BITS);
863 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
864 return -EFAULT;
865 return 0;
866}
867
868static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
869 struct kvm_device_attr *attr)
870{
871 /*
872 * Once we can actually configure subfunctions (kernel + hw support),
873 * we have to check if they were already set by user space, if so copy
874 * them from kvm->arch.
875 */
876 return -ENXIO;
877}
878
879static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
880 struct kvm_device_attr *attr)
881{
882 if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
883 sizeof(struct kvm_s390_vm_cpu_subfunc)))
884 return -EFAULT;
885 return 0;
886}
734static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 887static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
735{ 888{
736 int ret = -ENXIO; 889 int ret = -ENXIO;
@@ -742,6 +895,18 @@ static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
742 case KVM_S390_VM_CPU_MACHINE: 895 case KVM_S390_VM_CPU_MACHINE:
743 ret = kvm_s390_get_machine(kvm, attr); 896 ret = kvm_s390_get_machine(kvm, attr);
744 break; 897 break;
898 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
899 ret = kvm_s390_get_processor_feat(kvm, attr);
900 break;
901 case KVM_S390_VM_CPU_MACHINE_FEAT:
902 ret = kvm_s390_get_machine_feat(kvm, attr);
903 break;
904 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
905 ret = kvm_s390_get_processor_subfunc(kvm, attr);
906 break;
907 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
908 ret = kvm_s390_get_machine_subfunc(kvm, attr);
909 break;
745 } 910 }
746 return ret; 911 return ret;
747} 912}
@@ -802,6 +967,8 @@ static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
802 switch (attr->attr) { 967 switch (attr->attr) {
803 case KVM_S390_VM_MEM_ENABLE_CMMA: 968 case KVM_S390_VM_MEM_ENABLE_CMMA:
804 case KVM_S390_VM_MEM_CLR_CMMA: 969 case KVM_S390_VM_MEM_CLR_CMMA:
970 ret = sclp.has_cmma ? 0 : -ENXIO;
971 break;
805 case KVM_S390_VM_MEM_LIMIT_SIZE: 972 case KVM_S390_VM_MEM_LIMIT_SIZE:
806 ret = 0; 973 ret = 0;
807 break; 974 break;
@@ -825,8 +992,13 @@ static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
825 switch (attr->attr) { 992 switch (attr->attr) {
826 case KVM_S390_VM_CPU_PROCESSOR: 993 case KVM_S390_VM_CPU_PROCESSOR:
827 case KVM_S390_VM_CPU_MACHINE: 994 case KVM_S390_VM_CPU_MACHINE:
995 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
996 case KVM_S390_VM_CPU_MACHINE_FEAT:
997 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
828 ret = 0; 998 ret = 0;
829 break; 999 break;
1000 /* configuring subfunctions is not supported yet */
1001 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
830 default: 1002 default:
831 ret = -ENXIO; 1003 ret = -ENXIO;
832 break; 1004 break;
@@ -857,7 +1029,6 @@ static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
857{ 1029{
858 uint8_t *keys; 1030 uint8_t *keys;
859 uint64_t hva; 1031 uint64_t hva;
860 unsigned long curkey;
861 int i, r = 0; 1032 int i, r = 0;
862 1033
863 if (args->flags != 0) 1034 if (args->flags != 0)
@@ -878,26 +1049,27 @@ static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
878 if (!keys) 1049 if (!keys)
879 return -ENOMEM; 1050 return -ENOMEM;
880 1051
1052 down_read(&current->mm->mmap_sem);
881 for (i = 0; i < args->count; i++) { 1053 for (i = 0; i < args->count; i++) {
882 hva = gfn_to_hva(kvm, args->start_gfn + i); 1054 hva = gfn_to_hva(kvm, args->start_gfn + i);
883 if (kvm_is_error_hva(hva)) { 1055 if (kvm_is_error_hva(hva)) {
884 r = -EFAULT; 1056 r = -EFAULT;
885 goto out; 1057 break;
886 } 1058 }
887 1059
888 curkey = get_guest_storage_key(current->mm, hva); 1060 r = get_guest_storage_key(current->mm, hva, &keys[i]);
889 if (IS_ERR_VALUE(curkey)) { 1061 if (r)
890 r = curkey; 1062 break;
891 goto out; 1063 }
892 } 1064 up_read(&current->mm->mmap_sem);
893 keys[i] = curkey; 1065
1066 if (!r) {
1067 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
1068 sizeof(uint8_t) * args->count);
1069 if (r)
1070 r = -EFAULT;
894 } 1071 }
895 1072
896 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
897 sizeof(uint8_t) * args->count);
898 if (r)
899 r = -EFAULT;
900out:
901 kvfree(keys); 1073 kvfree(keys);
902 return r; 1074 return r;
903} 1075}
@@ -934,24 +1106,25 @@ static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
934 if (r) 1106 if (r)
935 goto out; 1107 goto out;
936 1108
1109 down_read(&current->mm->mmap_sem);
937 for (i = 0; i < args->count; i++) { 1110 for (i = 0; i < args->count; i++) {
938 hva = gfn_to_hva(kvm, args->start_gfn + i); 1111 hva = gfn_to_hva(kvm, args->start_gfn + i);
939 if (kvm_is_error_hva(hva)) { 1112 if (kvm_is_error_hva(hva)) {
940 r = -EFAULT; 1113 r = -EFAULT;
941 goto out; 1114 break;
942 } 1115 }
943 1116
944 /* Lowest order bit is reserved */ 1117 /* Lowest order bit is reserved */
945 if (keys[i] & 0x01) { 1118 if (keys[i] & 0x01) {
946 r = -EINVAL; 1119 r = -EINVAL;
947 goto out; 1120 break;
948 } 1121 }
949 1122
950 r = set_guest_storage_key(current->mm, hva, 1123 r = set_guest_storage_key(current->mm, hva, keys[i], 0);
951 (unsigned long)keys[i], 0);
952 if (r) 1124 if (r)
953 goto out; 1125 break;
954 } 1126 }
1127 up_read(&current->mm->mmap_sem);
955out: 1128out:
956 kvfree(keys); 1129 kvfree(keys);
957 return r; 1130 return r;
@@ -1128,6 +1301,7 @@ static void sca_dispose(struct kvm *kvm)
1128 1301
1129int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 1302int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1130{ 1303{
1304 gfp_t alloc_flags = GFP_KERNEL;
1131 int i, rc; 1305 int i, rc;
1132 char debug_name[16]; 1306 char debug_name[16];
1133 static unsigned long sca_offset; 1307 static unsigned long sca_offset;
@@ -1149,9 +1323,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1149 1323
1150 rc = -ENOMEM; 1324 rc = -ENOMEM;
1151 1325
1326 ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500);
1327
1152 kvm->arch.use_esca = 0; /* start with basic SCA */ 1328 kvm->arch.use_esca = 0; /* start with basic SCA */
1329 if (!sclp.has_64bscao)
1330 alloc_flags |= GFP_DMA;
1153 rwlock_init(&kvm->arch.sca_lock); 1331 rwlock_init(&kvm->arch.sca_lock);
1154 kvm->arch.sca = (struct bsca_block *) get_zeroed_page(GFP_KERNEL); 1332 kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
1155 if (!kvm->arch.sca) 1333 if (!kvm->arch.sca)
1156 goto out_err; 1334 goto out_err;
1157 spin_lock(&kvm_lock); 1335 spin_lock(&kvm_lock);
@@ -1188,6 +1366,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1188 memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask, 1366 memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask,
1189 S390_ARCH_FAC_LIST_SIZE_BYTE); 1367 S390_ARCH_FAC_LIST_SIZE_BYTE);
1190 1368
1369 set_kvm_facility(kvm->arch.model.fac_mask, 74);
1370 set_kvm_facility(kvm->arch.model.fac_list, 74);
1371
1191 kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 1372 kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
1192 kvm->arch.model.ibc = sclp.ibc & 0x0fff; 1373 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
1193 1374
@@ -1395,7 +1576,7 @@ static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
1395 1576
1396 if (id < KVM_S390_BSCA_CPU_SLOTS) 1577 if (id < KVM_S390_BSCA_CPU_SLOTS)
1397 return true; 1578 return true;
1398 if (!sclp.has_esca) 1579 if (!sclp.has_esca || !sclp.has_64bscao)
1399 return false; 1580 return false;
1400 1581
1401 mutex_lock(&kvm->lock); 1582 mutex_lock(&kvm->lock);
@@ -1657,15 +1838,21 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1657 1838
1658 kvm_s390_vcpu_setup_model(vcpu); 1839 kvm_s390_vcpu_setup_model(vcpu);
1659 1840
1660 vcpu->arch.sie_block->ecb = 0x02; 1841 /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
1842 if (MACHINE_HAS_ESOP)
1843 vcpu->arch.sie_block->ecb |= 0x02;
1661 if (test_kvm_facility(vcpu->kvm, 9)) 1844 if (test_kvm_facility(vcpu->kvm, 9))
1662 vcpu->arch.sie_block->ecb |= 0x04; 1845 vcpu->arch.sie_block->ecb |= 0x04;
1663 if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73)) 1846 if (test_kvm_facility(vcpu->kvm, 73))
1664 vcpu->arch.sie_block->ecb |= 0x10; 1847 vcpu->arch.sie_block->ecb |= 0x10;
1665 1848
1666 if (test_kvm_facility(vcpu->kvm, 8)) 1849 if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi)
1667 vcpu->arch.sie_block->ecb2 |= 0x08; 1850 vcpu->arch.sie_block->ecb2 |= 0x08;
1668 vcpu->arch.sie_block->eca = 0xC1002000U; 1851 vcpu->arch.sie_block->eca = 0x1002000U;
1852 if (sclp.has_cei)
1853 vcpu->arch.sie_block->eca |= 0x80000000U;
1854 if (sclp.has_ib)
1855 vcpu->arch.sie_block->eca |= 0x40000000U;
1669 if (sclp.has_siif) 1856 if (sclp.has_siif)
1670 vcpu->arch.sie_block->eca |= 1; 1857 vcpu->arch.sie_block->eca |= 1;
1671 if (sclp.has_sigpif) 1858 if (sclp.has_sigpif)
@@ -1678,6 +1865,8 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1678 } 1865 }
1679 vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb; 1866 vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
1680 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 1867 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
1868 if (test_kvm_facility(vcpu->kvm, 74))
1869 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
1681 1870
1682 if (vcpu->kvm->arch.use_cmma) { 1871 if (vcpu->kvm->arch.use_cmma) {
1683 rc = kvm_s390_vcpu_setup_cmma(vcpu); 1872 rc = kvm_s390_vcpu_setup_cmma(vcpu);
@@ -1715,6 +1904,10 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1715 vcpu->arch.sie_block = &sie_page->sie_block; 1904 vcpu->arch.sie_block = &sie_page->sie_block;
1716 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; 1905 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
1717 1906
1907 /* the real guest size will always be smaller than msl */
1908 vcpu->arch.sie_block->mso = 0;
1909 vcpu->arch.sie_block->msl = sclp.hamax;
1910
1718 vcpu->arch.sie_block->icpua = id; 1911 vcpu->arch.sie_block->icpua = id;
1719 spin_lock_init(&vcpu->arch.local_int.lock); 1912 spin_lock_init(&vcpu->arch.local_int.lock);
1720 vcpu->arch.local_int.float_int = &kvm->arch.float_int; 1913 vcpu->arch.local_int.float_int = &kvm->arch.float_int;
@@ -2001,6 +2194,8 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2001 2194
2002 if (dbg->control & ~VALID_GUESTDBG_FLAGS) 2195 if (dbg->control & ~VALID_GUESTDBG_FLAGS)
2003 return -EINVAL; 2196 return -EINVAL;
2197 if (!sclp.has_gpere)
2198 return -EINVAL;
2004 2199
2005 if (dbg->control & KVM_GUESTDBG_ENABLE) { 2200 if (dbg->control & KVM_GUESTDBG_ENABLE) {
2006 vcpu->guest_debug = dbg->control; 2201 vcpu->guest_debug = dbg->control;
@@ -2597,6 +2792,8 @@ static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
2597 2792
2598static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 2793static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2599{ 2794{
2795 if (!sclp.has_ibs)
2796 return;
2600 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 2797 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
2601 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 2798 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
2602} 2799}
diff --git a/arch/s390/kvm/kvm-s390.h b/arch/s390/kvm/kvm-s390.h
index 8621ab00ec8e..52aa47e112d8 100644
--- a/arch/s390/kvm/kvm-s390.h
+++ b/arch/s390/kvm/kvm-s390.h
@@ -175,6 +175,12 @@ static inline int set_kvm_facility(u64 *fac_list, unsigned long nr)
175 return 0; 175 return 0;
176} 176}
177 177
178static inline int test_kvm_cpu_feat(struct kvm *kvm, unsigned long nr)
179{
180 WARN_ON_ONCE(nr >= KVM_S390_VM_CPU_FEAT_NR_BITS);
181 return test_bit_inv(nr, kvm->arch.cpu_feat);
182}
183
178/* are cpu states controlled by user space */ 184/* are cpu states controlled by user space */
179static inline int kvm_s390_user_cpu_state_ctrl(struct kvm *kvm) 185static inline int kvm_s390_user_cpu_state_ctrl(struct kvm *kvm)
180{ 186{
@@ -250,6 +256,9 @@ int kvm_s390_handle_eb(struct kvm_vcpu *vcpu);
250int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu); 256int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);
251int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu); 257int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
252 258
259/* implemented in sthyi.c */
260int handle_sthyi(struct kvm_vcpu *vcpu);
261
253/* implemented in kvm-s390.c */ 262/* implemented in kvm-s390.c */
254void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod); 263void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod);
255long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable); 264long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable);
diff --git a/arch/s390/kvm/priv.c b/arch/s390/kvm/priv.c
index 95916fa7c670..3db3be139992 100644
--- a/arch/s390/kvm/priv.c
+++ b/arch/s390/kvm/priv.c
@@ -27,6 +27,7 @@
27#include <asm/io.h> 27#include <asm/io.h>
28#include <asm/ptrace.h> 28#include <asm/ptrace.h>
29#include <asm/compat.h> 29#include <asm/compat.h>
30#include <asm/sclp.h>
30#include "gaccess.h" 31#include "gaccess.h"
31#include "kvm-s390.h" 32#include "kvm-s390.h"
32#include "trace.h" 33#include "trace.h"
@@ -152,30 +153,166 @@ static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
152static int __skey_check_enable(struct kvm_vcpu *vcpu) 153static int __skey_check_enable(struct kvm_vcpu *vcpu)
153{ 154{
154 int rc = 0; 155 int rc = 0;
156
157 trace_kvm_s390_skey_related_inst(vcpu);
155 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE))) 158 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
156 return rc; 159 return rc;
157 160
158 rc = s390_enable_skey(); 161 rc = s390_enable_skey();
159 VCPU_EVENT(vcpu, 3, "%s", "enabling storage keys for guest"); 162 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
160 trace_kvm_s390_skey_related_inst(vcpu); 163 if (!rc)
161 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE); 164 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
162 return rc; 165 return rc;
163} 166}
164 167
165 168static int try_handle_skey(struct kvm_vcpu *vcpu)
166static int handle_skey(struct kvm_vcpu *vcpu)
167{ 169{
168 int rc = __skey_check_enable(vcpu); 170 int rc;
169 171
172 vcpu->stat.instruction_storage_key++;
173 rc = __skey_check_enable(vcpu);
170 if (rc) 174 if (rc)
171 return rc; 175 return rc;
172 vcpu->stat.instruction_storage_key++; 176 if (sclp.has_skey) {
173 177 /* with storage-key facility, SIE interprets it for us */
178 kvm_s390_retry_instr(vcpu);
179 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
180 return -EAGAIN;
181 }
174 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE) 182 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
175 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP); 183 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
184 return 0;
185}
176 186
177 kvm_s390_retry_instr(vcpu); 187static int handle_iske(struct kvm_vcpu *vcpu)
178 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation"); 188{
189 unsigned long addr;
190 unsigned char key;
191 int reg1, reg2;
192 int rc;
193
194 rc = try_handle_skey(vcpu);
195 if (rc)
196 return rc != -EAGAIN ? rc : 0;
197
198 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
199
200 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
201 addr = kvm_s390_logical_to_effective(vcpu, addr);
202 addr = kvm_s390_real_to_abs(vcpu, addr);
203 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
204 if (kvm_is_error_hva(addr))
205 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
206
207 down_read(&current->mm->mmap_sem);
208 rc = get_guest_storage_key(current->mm, addr, &key);
209 up_read(&current->mm->mmap_sem);
210 if (rc)
211 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
212 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
213 vcpu->run->s.regs.gprs[reg1] |= key;
214 return 0;
215}
216
217static int handle_rrbe(struct kvm_vcpu *vcpu)
218{
219 unsigned long addr;
220 int reg1, reg2;
221 int rc;
222
223 rc = try_handle_skey(vcpu);
224 if (rc)
225 return rc != -EAGAIN ? rc : 0;
226
227 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
228
229 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
230 addr = kvm_s390_logical_to_effective(vcpu, addr);
231 addr = kvm_s390_real_to_abs(vcpu, addr);
232 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
233 if (kvm_is_error_hva(addr))
234 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
235
236 down_read(&current->mm->mmap_sem);
237 rc = reset_guest_reference_bit(current->mm, addr);
238 up_read(&current->mm->mmap_sem);
239 if (rc < 0)
240 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
241
242 kvm_s390_set_psw_cc(vcpu, rc);
243 return 0;
244}
245
246#define SSKE_NQ 0x8
247#define SSKE_MR 0x4
248#define SSKE_MC 0x2
249#define SSKE_MB 0x1
250static int handle_sske(struct kvm_vcpu *vcpu)
251{
252 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
253 unsigned long start, end;
254 unsigned char key, oldkey;
255 int reg1, reg2;
256 int rc;
257
258 rc = try_handle_skey(vcpu);
259 if (rc)
260 return rc != -EAGAIN ? rc : 0;
261
262 if (!test_kvm_facility(vcpu->kvm, 8))
263 m3 &= ~SSKE_MB;
264 if (!test_kvm_facility(vcpu->kvm, 10))
265 m3 &= ~(SSKE_MC | SSKE_MR);
266 if (!test_kvm_facility(vcpu->kvm, 14))
267 m3 &= ~SSKE_NQ;
268
269 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
270
271 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
272 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
273 start = kvm_s390_logical_to_effective(vcpu, start);
274 if (m3 & SSKE_MB) {
275 /* start already designates an absolute address */
276 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
277 } else {
278 start = kvm_s390_real_to_abs(vcpu, start);
279 end = start + PAGE_SIZE;
280 }
281
282 while (start != end) {
283 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
284
285 if (kvm_is_error_hva(addr))
286 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
287
288 down_read(&current->mm->mmap_sem);
289 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
290 m3 & SSKE_NQ, m3 & SSKE_MR,
291 m3 & SSKE_MC);
292 up_read(&current->mm->mmap_sem);
293 if (rc < 0)
294 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
295 start += PAGE_SIZE;
296 };
297
298 if (m3 & (SSKE_MC | SSKE_MR)) {
299 if (m3 & SSKE_MB) {
300 /* skey in reg1 is unpredictable */
301 kvm_s390_set_psw_cc(vcpu, 3);
302 } else {
303 kvm_s390_set_psw_cc(vcpu, rc);
304 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
305 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
306 }
307 }
308 if (m3 & SSKE_MB) {
309 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT)
310 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
311 else
312 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
313 end = kvm_s390_logical_to_effective(vcpu, end);
314 vcpu->run->s.regs.gprs[reg2] |= end;
315 }
179 return 0; 316 return 0;
180} 317}
181 318
@@ -583,9 +720,9 @@ static const intercept_handler_t b2_handlers[256] = {
583 [0x11] = handle_store_prefix, 720 [0x11] = handle_store_prefix,
584 [0x12] = handle_store_cpu_address, 721 [0x12] = handle_store_cpu_address,
585 [0x21] = handle_ipte_interlock, 722 [0x21] = handle_ipte_interlock,
586 [0x29] = handle_skey, 723 [0x29] = handle_iske,
587 [0x2a] = handle_skey, 724 [0x2a] = handle_rrbe,
588 [0x2b] = handle_skey, 725 [0x2b] = handle_sske,
589 [0x2c] = handle_test_block, 726 [0x2c] = handle_test_block,
590 [0x30] = handle_io_inst, 727 [0x30] = handle_io_inst,
591 [0x31] = handle_io_inst, 728 [0x31] = handle_io_inst,
@@ -654,8 +791,10 @@ static int handle_epsw(struct kvm_vcpu *vcpu)
654 791
655static int handle_pfmf(struct kvm_vcpu *vcpu) 792static int handle_pfmf(struct kvm_vcpu *vcpu)
656{ 793{
794 bool mr = false, mc = false, nq;
657 int reg1, reg2; 795 int reg1, reg2;
658 unsigned long start, end; 796 unsigned long start, end;
797 unsigned char key;
659 798
660 vcpu->stat.instruction_pfmf++; 799 vcpu->stat.instruction_pfmf++;
661 800
@@ -675,15 +814,27 @@ static int handle_pfmf(struct kvm_vcpu *vcpu)
675 !test_kvm_facility(vcpu->kvm, 14)) 814 !test_kvm_facility(vcpu->kvm, 14))
676 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 815 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
677 816
678 /* No support for conditional-SSKE */ 817 /* Only provide conditional-SSKE support if enabled for the guest */
679 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC)) 818 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
680 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 819 test_kvm_facility(vcpu->kvm, 10)) {
820 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
821 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
822 }
681 823
824 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
825 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
682 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK; 826 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
683 start = kvm_s390_logical_to_effective(vcpu, start); 827 start = kvm_s390_logical_to_effective(vcpu, start);
684 828
829 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
830 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
831 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
832 }
833
685 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) { 834 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
686 case 0x00000000: 835 case 0x00000000:
836 /* only 4k frames specify a real address */
837 start = kvm_s390_real_to_abs(vcpu, start);
687 end = (start + (1UL << 12)) & ~((1UL << 12) - 1); 838 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
688 break; 839 break;
689 case 0x00001000: 840 case 0x00001000:
@@ -701,20 +852,11 @@ static int handle_pfmf(struct kvm_vcpu *vcpu)
701 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); 852 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
702 } 853 }
703 854
704 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) { 855 while (start != end) {
705 if (kvm_s390_check_low_addr_prot_real(vcpu, start)) 856 unsigned long useraddr;
706 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
707 }
708
709 while (start < end) {
710 unsigned long useraddr, abs_addr;
711 857
712 /* Translate guest address to host address */ 858 /* Translate guest address to host address */
713 if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0) 859 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
714 abs_addr = kvm_s390_real_to_abs(vcpu, start);
715 else
716 abs_addr = start;
717 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
718 if (kvm_is_error_hva(useraddr)) 860 if (kvm_is_error_hva(useraddr))
719 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); 861 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
720 862
@@ -728,16 +870,25 @@ static int handle_pfmf(struct kvm_vcpu *vcpu)
728 870
729 if (rc) 871 if (rc)
730 return rc; 872 return rc;
731 if (set_guest_storage_key(current->mm, useraddr, 873 down_read(&current->mm->mmap_sem);
732 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY, 874 rc = cond_set_guest_storage_key(current->mm, useraddr,
733 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ)) 875 key, NULL, nq, mr, mc);
876 up_read(&current->mm->mmap_sem);
877 if (rc < 0)
734 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); 878 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
735 } 879 }
736 880
737 start += PAGE_SIZE; 881 start += PAGE_SIZE;
738 } 882 }
739 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) 883 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
740 vcpu->run->s.regs.gprs[reg2] = end; 884 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) {
885 vcpu->run->s.regs.gprs[reg2] = end;
886 } else {
887 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
888 end = kvm_s390_logical_to_effective(vcpu, end);
889 vcpu->run->s.regs.gprs[reg2] |= end;
890 }
891 }
741 return 0; 892 return 0;
742} 893}
743 894
diff --git a/arch/s390/kvm/sthyi.c b/arch/s390/kvm/sthyi.c
new file mode 100644
index 000000000000..bd98b7d25200
--- /dev/null
+++ b/arch/s390/kvm/sthyi.c
@@ -0,0 +1,471 @@
1/*
2 * store hypervisor information instruction emulation functions.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (version 2 only)
6 * as published by the Free Software Foundation.
7 *
8 * Copyright IBM Corp. 2016
9 * Author(s): Janosch Frank <frankja@linux.vnet.ibm.com>
10 */
11#include <linux/kvm_host.h>
12#include <linux/errno.h>
13#include <linux/pagemap.h>
14#include <linux/vmalloc.h>
15#include <linux/ratelimit.h>
16
17#include <asm/kvm_host.h>
18#include <asm/asm-offsets.h>
19#include <asm/sclp.h>
20#include <asm/diag.h>
21#include <asm/sysinfo.h>
22#include <asm/ebcdic.h>
23
24#include "kvm-s390.h"
25#include "gaccess.h"
26#include "trace.h"
27
28#define DED_WEIGHT 0xffff
29/*
30 * CP and IFL as EBCDIC strings, SP/0x40 determines the end of string
31 * as they are justified with spaces.
32 */
33#define CP 0xc3d7404040404040UL
34#define IFL 0xc9c6d34040404040UL
35
36enum hdr_flags {
37 HDR_NOT_LPAR = 0x10,
38 HDR_STACK_INCM = 0x20,
39 HDR_STSI_UNAV = 0x40,
40 HDR_PERF_UNAV = 0x80,
41};
42
43enum mac_validity {
44 MAC_NAME_VLD = 0x20,
45 MAC_ID_VLD = 0x40,
46 MAC_CNT_VLD = 0x80,
47};
48
49enum par_flag {
50 PAR_MT_EN = 0x80,
51};
52
53enum par_validity {
54 PAR_GRP_VLD = 0x08,
55 PAR_ID_VLD = 0x10,
56 PAR_ABS_VLD = 0x20,
57 PAR_WGHT_VLD = 0x40,
58 PAR_PCNT_VLD = 0x80,
59};
60
61struct hdr_sctn {
62 u8 infhflg1;
63 u8 infhflg2; /* reserved */
64 u8 infhval1; /* reserved */
65 u8 infhval2; /* reserved */
66 u8 reserved[3];
67 u8 infhygct;
68 u16 infhtotl;
69 u16 infhdln;
70 u16 infmoff;
71 u16 infmlen;
72 u16 infpoff;
73 u16 infplen;
74 u16 infhoff1;
75 u16 infhlen1;
76 u16 infgoff1;
77 u16 infglen1;
78 u16 infhoff2;
79 u16 infhlen2;
80 u16 infgoff2;
81 u16 infglen2;
82 u16 infhoff3;
83 u16 infhlen3;
84 u16 infgoff3;
85 u16 infglen3;
86 u8 reserved2[4];
87} __packed;
88
89struct mac_sctn {
90 u8 infmflg1; /* reserved */
91 u8 infmflg2; /* reserved */
92 u8 infmval1;
93 u8 infmval2; /* reserved */
94 u16 infmscps;
95 u16 infmdcps;
96 u16 infmsifl;
97 u16 infmdifl;
98 char infmname[8];
99 char infmtype[4];
100 char infmmanu[16];
101 char infmseq[16];
102 char infmpman[4];
103 u8 reserved[4];
104} __packed;
105
106struct par_sctn {
107 u8 infpflg1;
108 u8 infpflg2; /* reserved */
109 u8 infpval1;
110 u8 infpval2; /* reserved */
111 u16 infppnum;
112 u16 infpscps;
113 u16 infpdcps;
114 u16 infpsifl;
115 u16 infpdifl;
116 u16 reserved;
117 char infppnam[8];
118 u32 infpwbcp;
119 u32 infpabcp;
120 u32 infpwbif;
121 u32 infpabif;
122 char infplgnm[8];
123 u32 infplgcp;
124 u32 infplgif;
125} __packed;
126
127struct sthyi_sctns {
128 struct hdr_sctn hdr;
129 struct mac_sctn mac;
130 struct par_sctn par;
131} __packed;
132
133struct cpu_inf {
134 u64 lpar_cap;
135 u64 lpar_grp_cap;
136 u64 lpar_weight;
137 u64 all_weight;
138 int cpu_num_ded;
139 int cpu_num_shd;
140};
141
142struct lpar_cpu_inf {
143 struct cpu_inf cp;
144 struct cpu_inf ifl;
145};
146
147static inline u64 cpu_id(u8 ctidx, void *diag224_buf)
148{
149 return *((u64 *)(diag224_buf + (ctidx + 1) * DIAG204_CPU_NAME_LEN));
150}
151
152/*
153 * Scales the cpu capping from the lpar range to the one expected in
154 * sthyi data.
155 *
156 * diag204 reports a cap in hundredths of processor units.
157 * z/VM's range for one core is 0 - 0x10000.
158 */
159static u32 scale_cap(u32 in)
160{
161 return (0x10000 * in) / 100;
162}
163
164static void fill_hdr(struct sthyi_sctns *sctns)
165{
166 sctns->hdr.infhdln = sizeof(sctns->hdr);
167 sctns->hdr.infmoff = sizeof(sctns->hdr);
168 sctns->hdr.infmlen = sizeof(sctns->mac);
169 sctns->hdr.infplen = sizeof(sctns->par);
170 sctns->hdr.infpoff = sctns->hdr.infhdln + sctns->hdr.infmlen;
171 sctns->hdr.infhtotl = sctns->hdr.infpoff + sctns->hdr.infplen;
172}
173
174static void fill_stsi_mac(struct sthyi_sctns *sctns,
175 struct sysinfo_1_1_1 *sysinfo)
176{
177 if (stsi(sysinfo, 1, 1, 1))
178 return;
179
180 sclp_ocf_cpc_name_copy(sctns->mac.infmname);
181
182 memcpy(sctns->mac.infmtype, sysinfo->type, sizeof(sctns->mac.infmtype));
183 memcpy(sctns->mac.infmmanu, sysinfo->manufacturer, sizeof(sctns->mac.infmmanu));
184 memcpy(sctns->mac.infmpman, sysinfo->plant, sizeof(sctns->mac.infmpman));
185 memcpy(sctns->mac.infmseq, sysinfo->sequence, sizeof(sctns->mac.infmseq));
186
187 sctns->mac.infmval1 |= MAC_ID_VLD | MAC_NAME_VLD;
188}
189
190static void fill_stsi_par(struct sthyi_sctns *sctns,
191 struct sysinfo_2_2_2 *sysinfo)
192{
193 if (stsi(sysinfo, 2, 2, 2))
194 return;
195
196 sctns->par.infppnum = sysinfo->lpar_number;
197 memcpy(sctns->par.infppnam, sysinfo->name, sizeof(sctns->par.infppnam));
198
199 sctns->par.infpval1 |= PAR_ID_VLD;
200}
201
202static void fill_stsi(struct sthyi_sctns *sctns)
203{
204 void *sysinfo;
205
206 /* Errors are handled through the validity bits in the response. */
207 sysinfo = (void *)__get_free_page(GFP_KERNEL);
208 if (!sysinfo)
209 return;
210
211 fill_stsi_mac(sctns, sysinfo);
212 fill_stsi_par(sctns, sysinfo);
213
214 free_pages((unsigned long)sysinfo, 0);
215}
216
217static void fill_diag_mac(struct sthyi_sctns *sctns,
218 struct diag204_x_phys_block *block,
219 void *diag224_buf)
220{
221 int i;
222
223 for (i = 0; i < block->hdr.cpus; i++) {
224 switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
225 case CP:
226 if (block->cpus[i].weight == DED_WEIGHT)
227 sctns->mac.infmdcps++;
228 else
229 sctns->mac.infmscps++;
230 break;
231 case IFL:
232 if (block->cpus[i].weight == DED_WEIGHT)
233 sctns->mac.infmdifl++;
234 else
235 sctns->mac.infmsifl++;
236 break;
237 }
238 }
239 sctns->mac.infmval1 |= MAC_CNT_VLD;
240}
241
242/* Returns a pointer to the the next partition block. */
243static struct diag204_x_part_block *lpar_cpu_inf(struct lpar_cpu_inf *part_inf,
244 bool this_lpar,
245 void *diag224_buf,
246 struct diag204_x_part_block *block)
247{
248 int i, capped = 0, weight_cp = 0, weight_ifl = 0;
249 struct cpu_inf *cpu_inf;
250
251 for (i = 0; i < block->hdr.rcpus; i++) {
252 if (!(block->cpus[i].cflag & DIAG204_CPU_ONLINE))
253 continue;
254
255 switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
256 case CP:
257 cpu_inf = &part_inf->cp;
258 if (block->cpus[i].cur_weight < DED_WEIGHT)
259 weight_cp |= block->cpus[i].cur_weight;
260 break;
261 case IFL:
262 cpu_inf = &part_inf->ifl;
263 if (block->cpus[i].cur_weight < DED_WEIGHT)
264 weight_ifl |= block->cpus[i].cur_weight;
265 break;
266 default:
267 continue;
268 }
269
270 if (!this_lpar)
271 continue;
272
273 capped |= block->cpus[i].cflag & DIAG204_CPU_CAPPED;
274 cpu_inf->lpar_cap |= block->cpus[i].cpu_type_cap;
275 cpu_inf->lpar_grp_cap |= block->cpus[i].group_cpu_type_cap;
276
277 if (block->cpus[i].weight == DED_WEIGHT)
278 cpu_inf->cpu_num_ded += 1;
279 else
280 cpu_inf->cpu_num_shd += 1;
281 }
282
283 if (this_lpar && capped) {
284 part_inf->cp.lpar_weight = weight_cp;
285 part_inf->ifl.lpar_weight = weight_ifl;
286 }
287 part_inf->cp.all_weight += weight_cp;
288 part_inf->ifl.all_weight += weight_ifl;
289 return (struct diag204_x_part_block *)&block->cpus[i];
290}
291
292static void fill_diag(struct sthyi_sctns *sctns)
293{
294 int i, r, pages;
295 bool this_lpar;
296 void *diag204_buf;
297 void *diag224_buf = NULL;
298 struct diag204_x_info_blk_hdr *ti_hdr;
299 struct diag204_x_part_block *part_block;
300 struct diag204_x_phys_block *phys_block;
301 struct lpar_cpu_inf lpar_inf = {};
302
303 /* Errors are handled through the validity bits in the response. */
304 pages = diag204((unsigned long)DIAG204_SUBC_RSI |
305 (unsigned long)DIAG204_INFO_EXT, 0, NULL);
306 if (pages <= 0)
307 return;
308
309 diag204_buf = vmalloc(PAGE_SIZE * pages);
310 if (!diag204_buf)
311 return;
312
313 r = diag204((unsigned long)DIAG204_SUBC_STIB7 |
314 (unsigned long)DIAG204_INFO_EXT, pages, diag204_buf);
315 if (r < 0)
316 goto out;
317
318 diag224_buf = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
319 if (!diag224_buf || diag224(diag224_buf))
320 goto out;
321
322 ti_hdr = diag204_buf;
323 part_block = diag204_buf + sizeof(*ti_hdr);
324
325 for (i = 0; i < ti_hdr->npar; i++) {
326 /*
327 * For the calling lpar we also need to get the cpu
328 * caps and weights. The time information block header
329 * specifies the offset to the partition block of the
330 * caller lpar, so we know when we process its data.
331 */
332 this_lpar = (void *)part_block - diag204_buf == ti_hdr->this_part;
333 part_block = lpar_cpu_inf(&lpar_inf, this_lpar, diag224_buf,
334 part_block);
335 }
336
337 phys_block = (struct diag204_x_phys_block *)part_block;
338 part_block = diag204_buf + ti_hdr->this_part;
339 if (part_block->hdr.mtid)
340 sctns->par.infpflg1 = PAR_MT_EN;
341
342 sctns->par.infpval1 |= PAR_GRP_VLD;
343 sctns->par.infplgcp = scale_cap(lpar_inf.cp.lpar_grp_cap);
344 sctns->par.infplgif = scale_cap(lpar_inf.ifl.lpar_grp_cap);
345 memcpy(sctns->par.infplgnm, part_block->hdr.hardware_group_name,
346 sizeof(sctns->par.infplgnm));
347
348 sctns->par.infpscps = lpar_inf.cp.cpu_num_shd;
349 sctns->par.infpdcps = lpar_inf.cp.cpu_num_ded;
350 sctns->par.infpsifl = lpar_inf.ifl.cpu_num_shd;
351 sctns->par.infpdifl = lpar_inf.ifl.cpu_num_ded;
352 sctns->par.infpval1 |= PAR_PCNT_VLD;
353
354 sctns->par.infpabcp = scale_cap(lpar_inf.cp.lpar_cap);
355 sctns->par.infpabif = scale_cap(lpar_inf.ifl.lpar_cap);
356 sctns->par.infpval1 |= PAR_ABS_VLD;
357
358 /*
359 * Everything below needs global performance data to be
360 * meaningful.
361 */
362 if (!(ti_hdr->flags & DIAG204_LPAR_PHYS_FLG)) {
363 sctns->hdr.infhflg1 |= HDR_PERF_UNAV;
364 goto out;
365 }
366
367 fill_diag_mac(sctns, phys_block, diag224_buf);
368
369 if (lpar_inf.cp.lpar_weight) {
370 sctns->par.infpwbcp = sctns->mac.infmscps * 0x10000 *
371 lpar_inf.cp.lpar_weight / lpar_inf.cp.all_weight;
372 }
373
374 if (lpar_inf.ifl.lpar_weight) {
375 sctns->par.infpwbif = sctns->mac.infmsifl * 0x10000 *
376 lpar_inf.ifl.lpar_weight / lpar_inf.ifl.all_weight;
377 }
378 sctns->par.infpval1 |= PAR_WGHT_VLD;
379
380out:
381 kfree(diag224_buf);
382 vfree(diag204_buf);
383}
384
385static int sthyi(u64 vaddr)
386{
387 register u64 code asm("0") = 0;
388 register u64 addr asm("2") = vaddr;
389 int cc;
390
391 asm volatile(
392 ".insn rre,0xB2560000,%[code],%[addr]\n"
393 "ipm %[cc]\n"
394 "srl %[cc],28\n"
395 : [cc] "=d" (cc)
396 : [code] "d" (code), [addr] "a" (addr)
397 : "memory", "cc");
398 return cc;
399}
400
401int handle_sthyi(struct kvm_vcpu *vcpu)
402{
403 int reg1, reg2, r = 0;
404 u64 code, addr, cc = 0;
405 struct sthyi_sctns *sctns = NULL;
406
407 /*
408 * STHYI requires extensive locking in the higher hypervisors
409 * and is very computational/memory expensive. Therefore we
410 * ratelimit the executions per VM.
411 */
412 if (!__ratelimit(&vcpu->kvm->arch.sthyi_limit)) {
413 kvm_s390_retry_instr(vcpu);
414 return 0;
415 }
416
417 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
418 code = vcpu->run->s.regs.gprs[reg1];
419 addr = vcpu->run->s.regs.gprs[reg2];
420
421 vcpu->stat.instruction_sthyi++;
422 VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
423 trace_kvm_s390_handle_sthyi(vcpu, code, addr);
424
425 if (reg1 == reg2 || reg1 & 1 || reg2 & 1 || addr & ~PAGE_MASK)
426 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
427
428 if (code & 0xffff) {
429 cc = 3;
430 goto out;
431 }
432
433 /*
434 * If the page has not yet been faulted in, we want to do that
435 * now and not after all the expensive calculations.
436 */
437 r = write_guest(vcpu, addr, reg2, &cc, 1);
438 if (r)
439 return kvm_s390_inject_prog_cond(vcpu, r);
440
441 sctns = (void *)get_zeroed_page(GFP_KERNEL);
442 if (!sctns)
443 return -ENOMEM;
444
445 /*
446 * If we are a guest, we don't want to emulate an emulated
447 * instruction. We ask the hypervisor to provide the data.
448 */
449 if (test_facility(74)) {
450 cc = sthyi((u64)sctns);
451 goto out;
452 }
453
454 fill_hdr(sctns);
455 fill_stsi(sctns);
456 fill_diag(sctns);
457
458out:
459 if (!cc) {
460 r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
461 if (r) {
462 free_page((unsigned long)sctns);
463 return kvm_s390_inject_prog_cond(vcpu, r);
464 }
465 }
466
467 free_page((unsigned long)sctns);
468 vcpu->run->s.regs.gprs[reg2 + 1] = cc ? 4 : 0;
469 kvm_s390_set_psw_cc(vcpu, cc);
470 return r;
471}
diff --git a/arch/s390/kvm/trace.h b/arch/s390/kvm/trace.h
index 916834d7a73a..4fc9d4e5be89 100644
--- a/arch/s390/kvm/trace.h
+++ b/arch/s390/kvm/trace.h
@@ -41,7 +41,7 @@ TRACE_EVENT(kvm_s390_skey_related_inst,
41 TP_fast_assign( 41 TP_fast_assign(
42 VCPU_ASSIGN_COMMON 42 VCPU_ASSIGN_COMMON
43 ), 43 ),
44 VCPU_TP_PRINTK("%s", "first instruction related to skeys on vcpu") 44 VCPU_TP_PRINTK("%s", "storage key related instruction")
45 ); 45 );
46 46
47TRACE_EVENT(kvm_s390_major_guest_pfault, 47TRACE_EVENT(kvm_s390_major_guest_pfault,
@@ -185,8 +185,10 @@ TRACE_EVENT(kvm_s390_intercept_prog,
185 __entry->code = code; 185 __entry->code = code;
186 ), 186 ),
187 187
188 VCPU_TP_PRINTK("intercepted program interruption %04x", 188 VCPU_TP_PRINTK("intercepted program interruption %04x (%s)",
189 __entry->code) 189 __entry->code,
190 __print_symbolic(__entry->code,
191 icpt_prog_codes))
190 ); 192 );
191 193
192/* 194/*
@@ -412,6 +414,47 @@ TRACE_EVENT(kvm_s390_handle_stsi,
412 __entry->addr) 414 __entry->addr)
413 ); 415 );
414 416
417TRACE_EVENT(kvm_s390_handle_operexc,
418 TP_PROTO(VCPU_PROTO_COMMON, __u16 ipa, __u32 ipb),
419 TP_ARGS(VCPU_ARGS_COMMON, ipa, ipb),
420
421 TP_STRUCT__entry(
422 VCPU_FIELD_COMMON
423 __field(__u64, instruction)
424 ),
425
426 TP_fast_assign(
427 VCPU_ASSIGN_COMMON
428 __entry->instruction = ((__u64)ipa << 48) |
429 ((__u64)ipb << 16);
430 ),
431
432 VCPU_TP_PRINTK("operation exception on instruction %016llx (%s)",
433 __entry->instruction,
434 __print_symbolic(icpt_insn_decoder(__entry->instruction),
435 icpt_insn_codes))
436 );
437
438TRACE_EVENT(kvm_s390_handle_sthyi,
439 TP_PROTO(VCPU_PROTO_COMMON, u64 code, u64 addr),
440 TP_ARGS(VCPU_ARGS_COMMON, code, addr),
441
442 TP_STRUCT__entry(
443 VCPU_FIELD_COMMON
444 __field(u64, code)
445 __field(u64, addr)
446 ),
447
448 TP_fast_assign(
449 VCPU_ASSIGN_COMMON
450 __entry->code = code;
451 __entry->addr = addr;
452 ),
453
454 VCPU_TP_PRINTK("STHYI fc: %llu addr: %016llx",
455 __entry->code, __entry->addr)
456 );
457
415#endif /* _TRACE_KVM_H */ 458#endif /* _TRACE_KVM_H */
416 459
417/* This part must be outside protection */ 460/* This part must be outside protection */