Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * handling privileged instructions
0004  *
0005  * Copyright IBM Corp. 2008, 2020
0006  *
0007  *    Author(s): Carsten Otte <cotte@de.ibm.com>
0008  *               Christian Borntraeger <borntraeger@de.ibm.com>
0009  */
0010 
0011 #include <linux/kvm.h>
0012 #include <linux/gfp.h>
0013 #include <linux/errno.h>
0014 #include <linux/mm_types.h>
0015 #include <linux/pgtable.h>
0016 
0017 #include <asm/asm-offsets.h>
0018 #include <asm/facility.h>
0019 #include <asm/current.h>
0020 #include <asm/debug.h>
0021 #include <asm/ebcdic.h>
0022 #include <asm/sysinfo.h>
0023 #include <asm/page-states.h>
0024 #include <asm/gmap.h>
0025 #include <asm/io.h>
0026 #include <asm/ptrace.h>
0027 #include <asm/sclp.h>
0028 #include <asm/ap.h>
0029 #include "gaccess.h"
0030 #include "kvm-s390.h"
0031 #include "trace.h"
0032 
0033 static int handle_ri(struct kvm_vcpu *vcpu)
0034 {
0035     vcpu->stat.instruction_ri++;
0036 
0037     if (test_kvm_facility(vcpu->kvm, 64)) {
0038         VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
0039         vcpu->arch.sie_block->ecb3 |= ECB3_RI;
0040         kvm_s390_retry_instr(vcpu);
0041         return 0;
0042     } else
0043         return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
0044 }
0045 
0046 int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
0047 {
0048     if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
0049         return handle_ri(vcpu);
0050     else
0051         return -EOPNOTSUPP;
0052 }
0053 
0054 static int handle_gs(struct kvm_vcpu *vcpu)
0055 {
0056     vcpu->stat.instruction_gs++;
0057 
0058     if (test_kvm_facility(vcpu->kvm, 133)) {
0059         VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
0060         preempt_disable();
0061         __ctl_set_bit(2, 4);
0062         current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
0063         restore_gs_cb(current->thread.gs_cb);
0064         preempt_enable();
0065         vcpu->arch.sie_block->ecb |= ECB_GS;
0066         vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
0067         vcpu->arch.gs_enabled = 1;
0068         kvm_s390_retry_instr(vcpu);
0069         return 0;
0070     } else
0071         return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
0072 }
0073 
0074 int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
0075 {
0076     int code = vcpu->arch.sie_block->ipb & 0xff;
0077 
0078     if (code == 0x49 || code == 0x4d)
0079         return handle_gs(vcpu);
0080     else
0081         return -EOPNOTSUPP;
0082 }
0083 /* Handle SCK (SET CLOCK) interception */
0084 static int handle_set_clock(struct kvm_vcpu *vcpu)
0085 {
0086     struct kvm_s390_vm_tod_clock gtod = { 0 };
0087     int rc;
0088     u8 ar;
0089     u64 op2;
0090 
0091     vcpu->stat.instruction_sck++;
0092 
0093     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0094         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0095 
0096     op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
0097     if (op2 & 7)    /* Operand must be on a doubleword boundary */
0098         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0099     rc = read_guest(vcpu, op2, ar, &gtod.tod, sizeof(gtod.tod));
0100     if (rc)
0101         return kvm_s390_inject_prog_cond(vcpu, rc);
0102 
0103     VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod);
0104     /*
0105      * To set the TOD clock the kvm lock must be taken, but the vcpu lock
0106      * is already held in handle_set_clock. The usual lock order is the
0107      * opposite.  As SCK is deprecated and should not be used in several
0108      * cases, for example when the multiple epoch facility or TOD clock
0109      * steering facility is installed (see Principles of Operation),  a
0110      * slow path can be used.  If the lock can not be taken via try_lock,
0111      * the instruction will be retried via -EAGAIN at a later point in
0112      * time.
0113      */
0114     if (!kvm_s390_try_set_tod_clock(vcpu->kvm, &gtod)) {
0115         kvm_s390_retry_instr(vcpu);
0116         return -EAGAIN;
0117     }
0118 
0119     kvm_s390_set_psw_cc(vcpu, 0);
0120     return 0;
0121 }
0122 
0123 static int handle_set_prefix(struct kvm_vcpu *vcpu)
0124 {
0125     u64 operand2;
0126     u32 address;
0127     int rc;
0128     u8 ar;
0129 
0130     vcpu->stat.instruction_spx++;
0131 
0132     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0133         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0134 
0135     operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
0136 
0137     /* must be word boundary */
0138     if (operand2 & 3)
0139         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0140 
0141     /* get the value */
0142     rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
0143     if (rc)
0144         return kvm_s390_inject_prog_cond(vcpu, rc);
0145 
0146     address &= 0x7fffe000u;
0147 
0148     /*
0149      * Make sure the new value is valid memory. We only need to check the
0150      * first page, since address is 8k aligned and memory pieces are always
0151      * at least 1MB aligned and have at least a size of 1MB.
0152      */
0153     if (kvm_is_error_gpa(vcpu->kvm, address))
0154         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
0155 
0156     kvm_s390_set_prefix(vcpu, address);
0157     trace_kvm_s390_handle_prefix(vcpu, 1, address);
0158     return 0;
0159 }
0160 
0161 static int handle_store_prefix(struct kvm_vcpu *vcpu)
0162 {
0163     u64 operand2;
0164     u32 address;
0165     int rc;
0166     u8 ar;
0167 
0168     vcpu->stat.instruction_stpx++;
0169 
0170     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0171         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0172 
0173     operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
0174 
0175     /* must be word boundary */
0176     if (operand2 & 3)
0177         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0178 
0179     address = kvm_s390_get_prefix(vcpu);
0180 
0181     /* get the value */
0182     rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
0183     if (rc)
0184         return kvm_s390_inject_prog_cond(vcpu, rc);
0185 
0186     VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
0187     trace_kvm_s390_handle_prefix(vcpu, 0, address);
0188     return 0;
0189 }
0190 
0191 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
0192 {
0193     u16 vcpu_id = vcpu->vcpu_id;
0194     u64 ga;
0195     int rc;
0196     u8 ar;
0197 
0198     vcpu->stat.instruction_stap++;
0199 
0200     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0201         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0202 
0203     ga = kvm_s390_get_base_disp_s(vcpu, &ar);
0204 
0205     if (ga & 1)
0206         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0207 
0208     rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
0209     if (rc)
0210         return kvm_s390_inject_prog_cond(vcpu, rc);
0211 
0212     VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
0213     trace_kvm_s390_handle_stap(vcpu, ga);
0214     return 0;
0215 }
0216 
0217 int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu)
0218 {
0219     int rc;
0220 
0221     trace_kvm_s390_skey_related_inst(vcpu);
0222     /* Already enabled? */
0223     if (vcpu->arch.skey_enabled)
0224         return 0;
0225 
0226     rc = s390_enable_skey();
0227     VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
0228     if (rc)
0229         return rc;
0230 
0231     if (kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS))
0232         kvm_s390_clear_cpuflags(vcpu, CPUSTAT_KSS);
0233     if (!vcpu->kvm->arch.use_skf)
0234         vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
0235     else
0236         vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
0237     vcpu->arch.skey_enabled = true;
0238     return 0;
0239 }
0240 
0241 static int try_handle_skey(struct kvm_vcpu *vcpu)
0242 {
0243     int rc;
0244 
0245     rc = kvm_s390_skey_check_enable(vcpu);
0246     if (rc)
0247         return rc;
0248     if (vcpu->kvm->arch.use_skf) {
0249         /* with storage-key facility, SIE interprets it for us */
0250         kvm_s390_retry_instr(vcpu);
0251         VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
0252         return -EAGAIN;
0253     }
0254     return 0;
0255 }
0256 
0257 static int handle_iske(struct kvm_vcpu *vcpu)
0258 {
0259     unsigned long gaddr, vmaddr;
0260     unsigned char key;
0261     int reg1, reg2;
0262     bool unlocked;
0263     int rc;
0264 
0265     vcpu->stat.instruction_iske++;
0266 
0267     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0268         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0269 
0270     rc = try_handle_skey(vcpu);
0271     if (rc)
0272         return rc != -EAGAIN ? rc : 0;
0273 
0274     kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
0275 
0276     gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
0277     gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
0278     gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
0279     vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
0280     if (kvm_is_error_hva(vmaddr))
0281         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
0282 retry:
0283     unlocked = false;
0284     mmap_read_lock(current->mm);
0285     rc = get_guest_storage_key(current->mm, vmaddr, &key);
0286 
0287     if (rc) {
0288         rc = fixup_user_fault(current->mm, vmaddr,
0289                       FAULT_FLAG_WRITE, &unlocked);
0290         if (!rc) {
0291             mmap_read_unlock(current->mm);
0292             goto retry;
0293         }
0294     }
0295     mmap_read_unlock(current->mm);
0296     if (rc == -EFAULT)
0297         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
0298     if (rc < 0)
0299         return rc;
0300     vcpu->run->s.regs.gprs[reg1] &= ~0xff;
0301     vcpu->run->s.regs.gprs[reg1] |= key;
0302     return 0;
0303 }
0304 
0305 static int handle_rrbe(struct kvm_vcpu *vcpu)
0306 {
0307     unsigned long vmaddr, gaddr;
0308     int reg1, reg2;
0309     bool unlocked;
0310     int rc;
0311 
0312     vcpu->stat.instruction_rrbe++;
0313 
0314     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0315         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0316 
0317     rc = try_handle_skey(vcpu);
0318     if (rc)
0319         return rc != -EAGAIN ? rc : 0;
0320 
0321     kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
0322 
0323     gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
0324     gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
0325     gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
0326     vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
0327     if (kvm_is_error_hva(vmaddr))
0328         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
0329 retry:
0330     unlocked = false;
0331     mmap_read_lock(current->mm);
0332     rc = reset_guest_reference_bit(current->mm, vmaddr);
0333     if (rc < 0) {
0334         rc = fixup_user_fault(current->mm, vmaddr,
0335                       FAULT_FLAG_WRITE, &unlocked);
0336         if (!rc) {
0337             mmap_read_unlock(current->mm);
0338             goto retry;
0339         }
0340     }
0341     mmap_read_unlock(current->mm);
0342     if (rc == -EFAULT)
0343         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
0344     if (rc < 0)
0345         return rc;
0346     kvm_s390_set_psw_cc(vcpu, rc);
0347     return 0;
0348 }
0349 
0350 #define SSKE_NQ 0x8
0351 #define SSKE_MR 0x4
0352 #define SSKE_MC 0x2
0353 #define SSKE_MB 0x1
0354 static int handle_sske(struct kvm_vcpu *vcpu)
0355 {
0356     unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
0357     unsigned long start, end;
0358     unsigned char key, oldkey;
0359     int reg1, reg2;
0360     bool unlocked;
0361     int rc;
0362 
0363     vcpu->stat.instruction_sske++;
0364 
0365     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0366         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0367 
0368     rc = try_handle_skey(vcpu);
0369     if (rc)
0370         return rc != -EAGAIN ? rc : 0;
0371 
0372     if (!test_kvm_facility(vcpu->kvm, 8))
0373         m3 &= ~SSKE_MB;
0374     if (!test_kvm_facility(vcpu->kvm, 10))
0375         m3 &= ~(SSKE_MC | SSKE_MR);
0376     if (!test_kvm_facility(vcpu->kvm, 14))
0377         m3 &= ~SSKE_NQ;
0378 
0379     kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
0380 
0381     key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
0382     start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
0383     start = kvm_s390_logical_to_effective(vcpu, start);
0384     if (m3 & SSKE_MB) {
0385         /* start already designates an absolute address */
0386         end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
0387     } else {
0388         start = kvm_s390_real_to_abs(vcpu, start);
0389         end = start + PAGE_SIZE;
0390     }
0391 
0392     while (start != end) {
0393         unsigned long vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
0394         unlocked = false;
0395 
0396         if (kvm_is_error_hva(vmaddr))
0397             return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
0398 
0399         mmap_read_lock(current->mm);
0400         rc = cond_set_guest_storage_key(current->mm, vmaddr, key, &oldkey,
0401                         m3 & SSKE_NQ, m3 & SSKE_MR,
0402                         m3 & SSKE_MC);
0403 
0404         if (rc < 0) {
0405             rc = fixup_user_fault(current->mm, vmaddr,
0406                           FAULT_FLAG_WRITE, &unlocked);
0407             rc = !rc ? -EAGAIN : rc;
0408         }
0409         mmap_read_unlock(current->mm);
0410         if (rc == -EFAULT)
0411             return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
0412         if (rc == -EAGAIN)
0413             continue;
0414         if (rc < 0)
0415             return rc;
0416         start += PAGE_SIZE;
0417     }
0418 
0419     if (m3 & (SSKE_MC | SSKE_MR)) {
0420         if (m3 & SSKE_MB) {
0421             /* skey in reg1 is unpredictable */
0422             kvm_s390_set_psw_cc(vcpu, 3);
0423         } else {
0424             kvm_s390_set_psw_cc(vcpu, rc);
0425             vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
0426             vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
0427         }
0428     }
0429     if (m3 & SSKE_MB) {
0430         if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
0431             vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
0432         else
0433             vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
0434         end = kvm_s390_logical_to_effective(vcpu, end);
0435         vcpu->run->s.regs.gprs[reg2] |= end;
0436     }
0437     return 0;
0438 }
0439 
0440 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
0441 {
0442     vcpu->stat.instruction_ipte_interlock++;
0443     if (psw_bits(vcpu->arch.sie_block->gpsw).pstate)
0444         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0445     wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu->kvm));
0446     kvm_s390_retry_instr(vcpu);
0447     VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
0448     return 0;
0449 }
0450 
0451 static int handle_test_block(struct kvm_vcpu *vcpu)
0452 {
0453     gpa_t addr;
0454     int reg2;
0455 
0456     vcpu->stat.instruction_tb++;
0457 
0458     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0459         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0460 
0461     kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
0462     addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
0463     addr = kvm_s390_logical_to_effective(vcpu, addr);
0464     if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
0465         return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
0466     addr = kvm_s390_real_to_abs(vcpu, addr);
0467 
0468     if (kvm_is_error_gpa(vcpu->kvm, addr))
0469         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
0470     /*
0471      * We don't expect errors on modern systems, and do not care
0472      * about storage keys (yet), so let's just clear the page.
0473      */
0474     if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
0475         return -EFAULT;
0476     kvm_s390_set_psw_cc(vcpu, 0);
0477     vcpu->run->s.regs.gprs[0] = 0;
0478     return 0;
0479 }
0480 
0481 static int handle_tpi(struct kvm_vcpu *vcpu)
0482 {
0483     struct kvm_s390_interrupt_info *inti;
0484     unsigned long len;
0485     u32 tpi_data[3];
0486     int rc;
0487     u64 addr;
0488     u8 ar;
0489 
0490     vcpu->stat.instruction_tpi++;
0491 
0492     addr = kvm_s390_get_base_disp_s(vcpu, &ar);
0493     if (addr & 3)
0494         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0495 
0496     inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
0497     if (!inti) {
0498         kvm_s390_set_psw_cc(vcpu, 0);
0499         return 0;
0500     }
0501 
0502     tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
0503     tpi_data[1] = inti->io.io_int_parm;
0504     tpi_data[2] = inti->io.io_int_word;
0505     if (addr) {
0506         /*
0507          * Store the two-word I/O interruption code into the
0508          * provided area.
0509          */
0510         len = sizeof(tpi_data) - 4;
0511         rc = write_guest(vcpu, addr, ar, &tpi_data, len);
0512         if (rc) {
0513             rc = kvm_s390_inject_prog_cond(vcpu, rc);
0514             goto reinject_interrupt;
0515         }
0516     } else {
0517         /*
0518          * Store the three-word I/O interruption code into
0519          * the appropriate lowcore area.
0520          */
0521         len = sizeof(tpi_data);
0522         if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
0523             /* failed writes to the low core are not recoverable */
0524             rc = -EFAULT;
0525             goto reinject_interrupt;
0526         }
0527     }
0528 
0529     /* irq was successfully handed to the guest */
0530     kfree(inti);
0531     kvm_s390_set_psw_cc(vcpu, 1);
0532     return 0;
0533 reinject_interrupt:
0534     /*
0535      * If we encounter a problem storing the interruption code, the
0536      * instruction is suppressed from the guest's view: reinject the
0537      * interrupt.
0538      */
0539     if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
0540         kfree(inti);
0541         rc = -EFAULT;
0542     }
0543     /* don't set the cc, a pgm irq was injected or we drop to user space */
0544     return rc ? -EFAULT : 0;
0545 }
0546 
0547 static int handle_tsch(struct kvm_vcpu *vcpu)
0548 {
0549     struct kvm_s390_interrupt_info *inti = NULL;
0550     const u64 isc_mask = 0xffUL << 24; /* all iscs set */
0551 
0552     vcpu->stat.instruction_tsch++;
0553 
0554     /* a valid schid has at least one bit set */
0555     if (vcpu->run->s.regs.gprs[1])
0556         inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
0557                        vcpu->run->s.regs.gprs[1]);
0558 
0559     /*
0560      * Prepare exit to userspace.
0561      * We indicate whether we dequeued a pending I/O interrupt
0562      * so that userspace can re-inject it if the instruction gets
0563      * a program check. While this may re-order the pending I/O
0564      * interrupts, this is no problem since the priority is kept
0565      * intact.
0566      */
0567     vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
0568     vcpu->run->s390_tsch.dequeued = !!inti;
0569     if (inti) {
0570         vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
0571         vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
0572         vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
0573         vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
0574     }
0575     vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
0576     kfree(inti);
0577     return -EREMOTE;
0578 }
0579 
0580 static int handle_io_inst(struct kvm_vcpu *vcpu)
0581 {
0582     VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
0583 
0584     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0585         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0586 
0587     if (vcpu->kvm->arch.css_support) {
0588         /*
0589          * Most I/O instructions will be handled by userspace.
0590          * Exceptions are tpi and the interrupt portion of tsch.
0591          */
0592         if (vcpu->arch.sie_block->ipa == 0xb236)
0593             return handle_tpi(vcpu);
0594         if (vcpu->arch.sie_block->ipa == 0xb235)
0595             return handle_tsch(vcpu);
0596         /* Handle in userspace. */
0597         vcpu->stat.instruction_io_other++;
0598         return -EOPNOTSUPP;
0599     } else {
0600         /*
0601          * Set condition code 3 to stop the guest from issuing channel
0602          * I/O instructions.
0603          */
0604         kvm_s390_set_psw_cc(vcpu, 3);
0605         return 0;
0606     }
0607 }
0608 
0609 /*
0610  * handle_pqap: Handling pqap interception
0611  * @vcpu: the vcpu having issue the pqap instruction
0612  *
0613  * We now support PQAP/AQIC instructions and we need to correctly
0614  * answer the guest even if no dedicated driver's hook is available.
0615  *
0616  * The intercepting code calls a dedicated callback for this instruction
0617  * if a driver did register one in the CRYPTO satellite of the
0618  * SIE block.
0619  *
0620  * If no callback is available, the queues are not available, return this
0621  * response code to the caller and set CC to 3.
0622  * Else return the response code returned by the callback.
0623  */
0624 static int handle_pqap(struct kvm_vcpu *vcpu)
0625 {
0626     struct ap_queue_status status = {};
0627     crypto_hook pqap_hook;
0628     unsigned long reg0;
0629     int ret;
0630     uint8_t fc;
0631 
0632     /* Verify that the AP instruction are available */
0633     if (!ap_instructions_available())
0634         return -EOPNOTSUPP;
0635     /* Verify that the guest is allowed to use AP instructions */
0636     if (!(vcpu->arch.sie_block->eca & ECA_APIE))
0637         return -EOPNOTSUPP;
0638     /*
0639      * The only possibly intercepted functions when AP instructions are
0640      * available for the guest are AQIC and TAPQ with the t bit set
0641      * since we do not set IC.3 (FIII) we currently will only intercept
0642      * the AQIC function code.
0643      * Note: running nested under z/VM can result in intercepts for other
0644      * function codes, e.g. PQAP(QCI). We do not support this and bail out.
0645      */
0646     reg0 = vcpu->run->s.regs.gprs[0];
0647     fc = (reg0 >> 24) & 0xff;
0648     if (fc != 0x03)
0649         return -EOPNOTSUPP;
0650 
0651     /* PQAP instruction is allowed for guest kernel only */
0652     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0653         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0654 
0655     /* Common PQAP instruction specification exceptions */
0656     /* bits 41-47 must all be zeros */
0657     if (reg0 & 0x007f0000UL)
0658         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0659     /* APFT not install and T bit set */
0660     if (!test_kvm_facility(vcpu->kvm, 15) && (reg0 & 0x00800000UL))
0661         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0662     /* APXA not installed and APID greater 64 or APQI greater 16 */
0663     if (!(vcpu->kvm->arch.crypto.crycbd & 0x02) && (reg0 & 0x0000c0f0UL))
0664         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0665 
0666     /* AQIC function code specific exception */
0667     /* facility 65 not present for AQIC function code */
0668     if (!test_kvm_facility(vcpu->kvm, 65))
0669         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0670 
0671     /*
0672      * If the hook callback is registered, there will be a pointer to the
0673      * hook function pointer in the kvm_s390_crypto structure. Lock the
0674      * owner, retrieve the hook function pointer and call the hook.
0675      */
0676     down_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
0677     if (vcpu->kvm->arch.crypto.pqap_hook) {
0678         pqap_hook = *vcpu->kvm->arch.crypto.pqap_hook;
0679         ret = pqap_hook(vcpu);
0680         if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
0681             kvm_s390_set_psw_cc(vcpu, 3);
0682         up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
0683         return ret;
0684     }
0685     up_read(&vcpu->kvm->arch.crypto.pqap_hook_rwsem);
0686     /*
0687      * A vfio_driver must register a hook.
0688      * No hook means no driver to enable the SIE CRYCB and no queues.
0689      * We send this response to the guest.
0690      */
0691     status.response_code = 0x01;
0692     memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
0693     kvm_s390_set_psw_cc(vcpu, 3);
0694     return 0;
0695 }
0696 
0697 static int handle_stfl(struct kvm_vcpu *vcpu)
0698 {
0699     int rc;
0700     unsigned int fac;
0701 
0702     vcpu->stat.instruction_stfl++;
0703 
0704     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0705         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0706 
0707     /*
0708      * We need to shift the lower 32 facility bits (bit 0-31) from a u64
0709      * into a u32 memory representation. They will remain bits 0-31.
0710      */
0711     fac = *vcpu->kvm->arch.model.fac_list >> 32;
0712     rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
0713                 &fac, sizeof(fac));
0714     if (rc)
0715         return rc;
0716     VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
0717     trace_kvm_s390_handle_stfl(vcpu, fac);
0718     return 0;
0719 }
0720 
0721 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
0722 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
0723 #define PSW_ADDR_24 0x0000000000ffffffUL
0724 #define PSW_ADDR_31 0x000000007fffffffUL
0725 
0726 int is_valid_psw(psw_t *psw)
0727 {
0728     if (psw->mask & PSW_MASK_UNASSIGNED)
0729         return 0;
0730     if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
0731         if (psw->addr & ~PSW_ADDR_31)
0732             return 0;
0733     }
0734     if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
0735         return 0;
0736     if ((psw->mask & PSW_MASK_ADDR_MODE) ==  PSW_MASK_EA)
0737         return 0;
0738     if (psw->addr & 1)
0739         return 0;
0740     return 1;
0741 }
0742 
0743 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
0744 {
0745     psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
0746     psw_compat_t new_psw;
0747     u64 addr;
0748     int rc;
0749     u8 ar;
0750 
0751     vcpu->stat.instruction_lpsw++;
0752 
0753     if (gpsw->mask & PSW_MASK_PSTATE)
0754         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0755 
0756     addr = kvm_s390_get_base_disp_s(vcpu, &ar);
0757     if (addr & 7)
0758         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0759 
0760     rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
0761     if (rc)
0762         return kvm_s390_inject_prog_cond(vcpu, rc);
0763     if (!(new_psw.mask & PSW32_MASK_BASE))
0764         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0765     gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
0766     gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
0767     gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
0768     if (!is_valid_psw(gpsw))
0769         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0770     return 0;
0771 }
0772 
0773 static int handle_lpswe(struct kvm_vcpu *vcpu)
0774 {
0775     psw_t new_psw;
0776     u64 addr;
0777     int rc;
0778     u8 ar;
0779 
0780     vcpu->stat.instruction_lpswe++;
0781 
0782     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0783         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0784 
0785     addr = kvm_s390_get_base_disp_s(vcpu, &ar);
0786     if (addr & 7)
0787         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0788     rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
0789     if (rc)
0790         return kvm_s390_inject_prog_cond(vcpu, rc);
0791     vcpu->arch.sie_block->gpsw = new_psw;
0792     if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
0793         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0794     return 0;
0795 }
0796 
0797 static int handle_stidp(struct kvm_vcpu *vcpu)
0798 {
0799     u64 stidp_data = vcpu->kvm->arch.model.cpuid;
0800     u64 operand2;
0801     int rc;
0802     u8 ar;
0803 
0804     vcpu->stat.instruction_stidp++;
0805 
0806     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0807         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0808 
0809     operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
0810 
0811     if (operand2 & 7)
0812         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0813 
0814     rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
0815     if (rc)
0816         return kvm_s390_inject_prog_cond(vcpu, rc);
0817 
0818     VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
0819     return 0;
0820 }
0821 
0822 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
0823 {
0824     int cpus = 0;
0825     int n;
0826 
0827     cpus = atomic_read(&vcpu->kvm->online_vcpus);
0828 
0829     /* deal with other level 3 hypervisors */
0830     if (stsi(mem, 3, 2, 2))
0831         mem->count = 0;
0832     if (mem->count < 8)
0833         mem->count++;
0834     for (n = mem->count - 1; n > 0 ; n--)
0835         memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
0836 
0837     memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
0838     mem->vm[0].cpus_total = cpus;
0839     mem->vm[0].cpus_configured = cpus;
0840     mem->vm[0].cpus_standby = 0;
0841     mem->vm[0].cpus_reserved = 0;
0842     mem->vm[0].caf = 1000;
0843     memcpy(mem->vm[0].name, "KVMguest", 8);
0844     ASCEBC(mem->vm[0].name, 8);
0845     memcpy(mem->vm[0].cpi, "KVM/Linux       ", 16);
0846     ASCEBC(mem->vm[0].cpi, 16);
0847 }
0848 
0849 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
0850                  u8 fc, u8 sel1, u16 sel2)
0851 {
0852     vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
0853     vcpu->run->s390_stsi.addr = addr;
0854     vcpu->run->s390_stsi.ar = ar;
0855     vcpu->run->s390_stsi.fc = fc;
0856     vcpu->run->s390_stsi.sel1 = sel1;
0857     vcpu->run->s390_stsi.sel2 = sel2;
0858 }
0859 
0860 static int handle_stsi(struct kvm_vcpu *vcpu)
0861 {
0862     int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
0863     int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
0864     int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
0865     unsigned long mem = 0;
0866     u64 operand2;
0867     int rc = 0;
0868     u8 ar;
0869 
0870     vcpu->stat.instruction_stsi++;
0871     VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
0872 
0873     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
0874         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
0875 
0876     /* Bailout forbidden function codes */
0877     if (fc > 3 && fc != 15)
0878         goto out_no_data;
0879 
0880     /*
0881      * fc 15 is provided only with
0882      *   - PTF/CPU topology support through facility 15
0883      *   - KVM_CAP_S390_USER_STSI
0884      */
0885     if (fc == 15 && (!test_kvm_facility(vcpu->kvm, 11) ||
0886              !vcpu->kvm->arch.user_stsi))
0887         goto out_no_data;
0888 
0889     if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
0890         || vcpu->run->s.regs.gprs[1] & 0xffff0000)
0891         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0892 
0893     if (fc == 0) {
0894         vcpu->run->s.regs.gprs[0] = 3 << 28;
0895         kvm_s390_set_psw_cc(vcpu, 0);
0896         return 0;
0897     }
0898 
0899     operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
0900 
0901     if (!kvm_s390_pv_cpu_is_protected(vcpu) && (operand2 & 0xfff))
0902         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
0903 
0904     switch (fc) {
0905     case 1: /* same handling for 1 and 2 */
0906     case 2:
0907         mem = get_zeroed_page(GFP_KERNEL_ACCOUNT);
0908         if (!mem)
0909             goto out_no_data;
0910         if (stsi((void *) mem, fc, sel1, sel2))
0911             goto out_no_data;
0912         break;
0913     case 3:
0914         if (sel1 != 2 || sel2 != 2)
0915             goto out_no_data;
0916         mem = get_zeroed_page(GFP_KERNEL_ACCOUNT);
0917         if (!mem)
0918             goto out_no_data;
0919         handle_stsi_3_2_2(vcpu, (void *) mem);
0920         break;
0921     case 15: /* fc 15 is fully handled in userspace */
0922         insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
0923         trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
0924         return -EREMOTE;
0925     }
0926     if (kvm_s390_pv_cpu_is_protected(vcpu)) {
0927         memcpy((void *)sida_origin(vcpu->arch.sie_block), (void *)mem,
0928                PAGE_SIZE);
0929         rc = 0;
0930     } else {
0931         rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
0932     }
0933     if (rc) {
0934         rc = kvm_s390_inject_prog_cond(vcpu, rc);
0935         goto out;
0936     }
0937     if (vcpu->kvm->arch.user_stsi) {
0938         insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
0939         rc = -EREMOTE;
0940     }
0941     trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
0942     free_page(mem);
0943     kvm_s390_set_psw_cc(vcpu, 0);
0944     vcpu->run->s.regs.gprs[0] = 0;
0945     return rc;
0946 out_no_data:
0947     kvm_s390_set_psw_cc(vcpu, 3);
0948 out:
0949     free_page(mem);
0950     return rc;
0951 }
0952 
0953 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
0954 {
0955     switch (vcpu->arch.sie_block->ipa & 0x00ff) {
0956     case 0x02:
0957         return handle_stidp(vcpu);
0958     case 0x04:
0959         return handle_set_clock(vcpu);
0960     case 0x10:
0961         return handle_set_prefix(vcpu);
0962     case 0x11:
0963         return handle_store_prefix(vcpu);
0964     case 0x12:
0965         return handle_store_cpu_address(vcpu);
0966     case 0x14:
0967         return kvm_s390_handle_vsie(vcpu);
0968     case 0x21:
0969     case 0x50:
0970         return handle_ipte_interlock(vcpu);
0971     case 0x29:
0972         return handle_iske(vcpu);
0973     case 0x2a:
0974         return handle_rrbe(vcpu);
0975     case 0x2b:
0976         return handle_sske(vcpu);
0977     case 0x2c:
0978         return handle_test_block(vcpu);
0979     case 0x30:
0980     case 0x31:
0981     case 0x32:
0982     case 0x33:
0983     case 0x34:
0984     case 0x35:
0985     case 0x36:
0986     case 0x37:
0987     case 0x38:
0988     case 0x39:
0989     case 0x3a:
0990     case 0x3b:
0991     case 0x3c:
0992     case 0x5f:
0993     case 0x74:
0994     case 0x76:
0995         return handle_io_inst(vcpu);
0996     case 0x56:
0997         return handle_sthyi(vcpu);
0998     case 0x7d:
0999         return handle_stsi(vcpu);
1000     case 0xaf:
1001         return handle_pqap(vcpu);
1002     case 0xb1:
1003         return handle_stfl(vcpu);
1004     case 0xb2:
1005         return handle_lpswe(vcpu);
1006     default:
1007         return -EOPNOTSUPP;
1008     }
1009 }
1010 
1011 static int handle_epsw(struct kvm_vcpu *vcpu)
1012 {
1013     int reg1, reg2;
1014 
1015     vcpu->stat.instruction_epsw++;
1016 
1017     kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
1018 
1019     /* This basically extracts the mask half of the psw. */
1020     vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
1021     vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
1022     if (reg2) {
1023         vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
1024         vcpu->run->s.regs.gprs[reg2] |=
1025             vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
1026     }
1027     return 0;
1028 }
1029 
1030 #define PFMF_RESERVED   0xfffc0101UL
1031 #define PFMF_SK         0x00020000UL
1032 #define PFMF_CF         0x00010000UL
1033 #define PFMF_UI         0x00008000UL
1034 #define PFMF_FSC        0x00007000UL
1035 #define PFMF_NQ         0x00000800UL
1036 #define PFMF_MR         0x00000400UL
1037 #define PFMF_MC         0x00000200UL
1038 #define PFMF_KEY        0x000000feUL
1039 
1040 static int handle_pfmf(struct kvm_vcpu *vcpu)
1041 {
1042     bool mr = false, mc = false, nq;
1043     int reg1, reg2;
1044     unsigned long start, end;
1045     unsigned char key;
1046 
1047     vcpu->stat.instruction_pfmf++;
1048 
1049     kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
1050 
1051     if (!test_kvm_facility(vcpu->kvm, 8))
1052         return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1053 
1054     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1055         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1056 
1057     if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
1058         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1059 
1060     /* Only provide non-quiescing support if enabled for the guest */
1061     if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
1062         !test_kvm_facility(vcpu->kvm, 14))
1063         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1064 
1065     /* Only provide conditional-SSKE support if enabled for the guest */
1066     if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
1067         test_kvm_facility(vcpu->kvm, 10)) {
1068         mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
1069         mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
1070     }
1071 
1072     nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
1073     key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
1074     start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
1075     start = kvm_s390_logical_to_effective(vcpu, start);
1076 
1077     if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
1078         if (kvm_s390_check_low_addr_prot_real(vcpu, start))
1079             return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
1080     }
1081 
1082     switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
1083     case 0x00000000:
1084         /* only 4k frames specify a real address */
1085         start = kvm_s390_real_to_abs(vcpu, start);
1086         end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1087         break;
1088     case 0x00001000:
1089         end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
1090         break;
1091     case 0x00002000:
1092         /* only support 2G frame size if EDAT2 is available and we are
1093            not in 24-bit addressing mode */
1094         if (!test_kvm_facility(vcpu->kvm, 78) ||
1095             psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT)
1096             return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1097         end = (start + _REGION3_SIZE) & ~(_REGION3_SIZE - 1);
1098         break;
1099     default:
1100         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1101     }
1102 
1103     while (start != end) {
1104         unsigned long vmaddr;
1105         bool unlocked = false;
1106 
1107         /* Translate guest address to host address */
1108         vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
1109         if (kvm_is_error_hva(vmaddr))
1110             return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1111 
1112         if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
1113             if (kvm_clear_guest(vcpu->kvm, start, PAGE_SIZE))
1114                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1115         }
1116 
1117         if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
1118             int rc = kvm_s390_skey_check_enable(vcpu);
1119 
1120             if (rc)
1121                 return rc;
1122             mmap_read_lock(current->mm);
1123             rc = cond_set_guest_storage_key(current->mm, vmaddr,
1124                             key, NULL, nq, mr, mc);
1125             if (rc < 0) {
1126                 rc = fixup_user_fault(current->mm, vmaddr,
1127                               FAULT_FLAG_WRITE, &unlocked);
1128                 rc = !rc ? -EAGAIN : rc;
1129             }
1130             mmap_read_unlock(current->mm);
1131             if (rc == -EFAULT)
1132                 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1133             if (rc == -EAGAIN)
1134                 continue;
1135             if (rc < 0)
1136                 return rc;
1137         }
1138         start += PAGE_SIZE;
1139     }
1140     if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
1141         if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
1142             vcpu->run->s.regs.gprs[reg2] = end;
1143         } else {
1144             vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
1145             end = kvm_s390_logical_to_effective(vcpu, end);
1146             vcpu->run->s.regs.gprs[reg2] |= end;
1147         }
1148     }
1149     return 0;
1150 }
1151 
1152 /*
1153  * Must be called with relevant read locks held (kvm->mm->mmap_lock, kvm->srcu)
1154  */
1155 static inline int __do_essa(struct kvm_vcpu *vcpu, const int orc)
1156 {
1157     int r1, r2, nappended, entries;
1158     unsigned long gfn, hva, res, pgstev, ptev;
1159     unsigned long *cbrlo;
1160 
1161     /*
1162      * We don't need to set SD.FPF.SK to 1 here, because if we have a
1163      * machine check here we either handle it or crash
1164      */
1165 
1166     kvm_s390_get_regs_rre(vcpu, &r1, &r2);
1167     gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
1168     hva = gfn_to_hva(vcpu->kvm, gfn);
1169     entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1170 
1171     if (kvm_is_error_hva(hva))
1172         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1173 
1174     nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
1175     if (nappended < 0) {
1176         res = orc ? 0x10 : 0;
1177         vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
1178         return 0;
1179     }
1180     res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
1181     /*
1182      * Set the block-content state part of the result. 0 means resident, so
1183      * nothing to do if the page is valid. 2 is for preserved pages
1184      * (non-present and non-zero), and 3 for zero pages (non-present and
1185      * zero).
1186      */
1187     if (ptev & _PAGE_INVALID) {
1188         res |= 2;
1189         if (pgstev & _PGSTE_GPS_ZERO)
1190             res |= 1;
1191     }
1192     if (pgstev & _PGSTE_GPS_NODAT)
1193         res |= 0x20;
1194     vcpu->run->s.regs.gprs[r1] = res;
1195     /*
1196      * It is possible that all the normal 511 slots were full, in which case
1197      * we will now write in the 512th slot, which is reserved for host use.
1198      * In both cases we let the normal essa handling code process all the
1199      * slots, including the reserved one, if needed.
1200      */
1201     if (nappended > 0) {
1202         cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
1203         cbrlo[entries] = gfn << PAGE_SHIFT;
1204     }
1205 
1206     if (orc) {
1207         struct kvm_memory_slot *ms = gfn_to_memslot(vcpu->kvm, gfn);
1208 
1209         /* Increment only if we are really flipping the bit */
1210         if (ms && !test_and_set_bit(gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
1211             atomic64_inc(&vcpu->kvm->arch.cmma_dirty_pages);
1212     }
1213 
1214     return nappended;
1215 }
1216 
1217 static int handle_essa(struct kvm_vcpu *vcpu)
1218 {
1219     /* entries expected to be 1FF */
1220     int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1221     unsigned long *cbrlo;
1222     struct gmap *gmap;
1223     int i, orc;
1224 
1225     VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
1226     gmap = vcpu->arch.gmap;
1227     vcpu->stat.instruction_essa++;
1228     if (!vcpu->kvm->arch.use_cmma)
1229         return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1230 
1231     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1232         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1233     /* Check for invalid operation request code */
1234     orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
1235     /* ORCs 0-6 are always valid */
1236     if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT
1237                         : ESSA_SET_STABLE_IF_RESIDENT))
1238         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1239 
1240     if (!vcpu->kvm->arch.migration_mode) {
1241         /*
1242          * CMMA is enabled in the KVM settings, but is disabled in
1243          * the SIE block and in the mm_context, and we are not doing
1244          * a migration. Enable CMMA in the mm_context.
1245          * Since we need to take a write lock to write to the context
1246          * to avoid races with storage keys handling, we check if the
1247          * value really needs to be written to; if the value is
1248          * already correct, we do nothing and avoid the lock.
1249          */
1250         if (vcpu->kvm->mm->context.uses_cmm == 0) {
1251             mmap_write_lock(vcpu->kvm->mm);
1252             vcpu->kvm->mm->context.uses_cmm = 1;
1253             mmap_write_unlock(vcpu->kvm->mm);
1254         }
1255         /*
1256          * If we are here, we are supposed to have CMMA enabled in
1257          * the SIE block. Enabling CMMA works on a per-CPU basis,
1258          * while the context use_cmma flag is per process.
1259          * It's possible that the context flag is enabled and the
1260          * SIE flag is not, so we set the flag always; if it was
1261          * already set, nothing changes, otherwise we enable it
1262          * on this CPU too.
1263          */
1264         vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
1265         /* Retry the ESSA instruction */
1266         kvm_s390_retry_instr(vcpu);
1267     } else {
1268         int srcu_idx;
1269 
1270         mmap_read_lock(vcpu->kvm->mm);
1271         srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1272         i = __do_essa(vcpu, orc);
1273         srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1274         mmap_read_unlock(vcpu->kvm->mm);
1275         if (i < 0)
1276             return i;
1277         /* Account for the possible extra cbrl entry */
1278         entries += i;
1279     }
1280     vcpu->arch.sie_block->cbrlo &= PAGE_MASK;   /* reset nceo */
1281     cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
1282     mmap_read_lock(gmap->mm);
1283     for (i = 0; i < entries; ++i)
1284         __gmap_zap(gmap, cbrlo[i]);
1285     mmap_read_unlock(gmap->mm);
1286     return 0;
1287 }
1288 
1289 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
1290 {
1291     switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1292     case 0x8a:
1293     case 0x8e:
1294     case 0x8f:
1295         return handle_ipte_interlock(vcpu);
1296     case 0x8d:
1297         return handle_epsw(vcpu);
1298     case 0xab:
1299         return handle_essa(vcpu);
1300     case 0xaf:
1301         return handle_pfmf(vcpu);
1302     default:
1303         return -EOPNOTSUPP;
1304     }
1305 }
1306 
1307 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
1308 {
1309     int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1310     int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1311     int reg, rc, nr_regs;
1312     u32 ctl_array[16];
1313     u64 ga;
1314     u8 ar;
1315 
1316     vcpu->stat.instruction_lctl++;
1317 
1318     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1319         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1320 
1321     ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1322 
1323     if (ga & 3)
1324         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1325 
1326     VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1327     trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
1328 
1329     nr_regs = ((reg3 - reg1) & 0xf) + 1;
1330     rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1331     if (rc)
1332         return kvm_s390_inject_prog_cond(vcpu, rc);
1333     reg = reg1;
1334     nr_regs = 0;
1335     do {
1336         vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
1337         vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
1338         if (reg == reg3)
1339             break;
1340         reg = (reg + 1) % 16;
1341     } while (1);
1342     kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1343     return 0;
1344 }
1345 
1346 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1347 {
1348     int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1349     int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1350     int reg, rc, nr_regs;
1351     u32 ctl_array[16];
1352     u64 ga;
1353     u8 ar;
1354 
1355     vcpu->stat.instruction_stctl++;
1356 
1357     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1358         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1359 
1360     ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1361 
1362     if (ga & 3)
1363         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1364 
1365     VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1366     trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1367 
1368     reg = reg1;
1369     nr_regs = 0;
1370     do {
1371         ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1372         if (reg == reg3)
1373             break;
1374         reg = (reg + 1) % 16;
1375     } while (1);
1376     rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1377     return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1378 }
1379 
1380 static int handle_lctlg(struct kvm_vcpu *vcpu)
1381 {
1382     int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1383     int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1384     int reg, rc, nr_regs;
1385     u64 ctl_array[16];
1386     u64 ga;
1387     u8 ar;
1388 
1389     vcpu->stat.instruction_lctlg++;
1390 
1391     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1392         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1393 
1394     ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1395 
1396     if (ga & 7)
1397         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1398 
1399     VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1400     trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
1401 
1402     nr_regs = ((reg3 - reg1) & 0xf) + 1;
1403     rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1404     if (rc)
1405         return kvm_s390_inject_prog_cond(vcpu, rc);
1406     reg = reg1;
1407     nr_regs = 0;
1408     do {
1409         vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
1410         if (reg == reg3)
1411             break;
1412         reg = (reg + 1) % 16;
1413     } while (1);
1414     kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1415     return 0;
1416 }
1417 
1418 static int handle_stctg(struct kvm_vcpu *vcpu)
1419 {
1420     int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1421     int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1422     int reg, rc, nr_regs;
1423     u64 ctl_array[16];
1424     u64 ga;
1425     u8 ar;
1426 
1427     vcpu->stat.instruction_stctg++;
1428 
1429     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1430         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1431 
1432     ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1433 
1434     if (ga & 7)
1435         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1436 
1437     VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1438     trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1439 
1440     reg = reg1;
1441     nr_regs = 0;
1442     do {
1443         ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1444         if (reg == reg3)
1445             break;
1446         reg = (reg + 1) % 16;
1447     } while (1);
1448     rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1449     return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1450 }
1451 
1452 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
1453 {
1454     switch (vcpu->arch.sie_block->ipb & 0x000000ff) {
1455     case 0x25:
1456         return handle_stctg(vcpu);
1457     case 0x2f:
1458         return handle_lctlg(vcpu);
1459     case 0x60:
1460     case 0x61:
1461     case 0x62:
1462         return handle_ri(vcpu);
1463     default:
1464         return -EOPNOTSUPP;
1465     }
1466 }
1467 
1468 static int handle_tprot(struct kvm_vcpu *vcpu)
1469 {
1470     u64 address, operand2;
1471     unsigned long gpa;
1472     u8 access_key;
1473     bool writable;
1474     int ret, cc;
1475     u8 ar;
1476 
1477     vcpu->stat.instruction_tprot++;
1478 
1479     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1480         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1481 
1482     kvm_s390_get_base_disp_sse(vcpu, &address, &operand2, &ar, NULL);
1483     access_key = (operand2 & 0xf0) >> 4;
1484 
1485     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1486         ipte_lock(vcpu->kvm);
1487 
1488     ret = guest_translate_address_with_key(vcpu, address, ar, &gpa,
1489                            GACC_STORE, access_key);
1490     if (ret == 0) {
1491         gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1492     } else if (ret == PGM_PROTECTION) {
1493         writable = false;
1494         /* Write protected? Try again with read-only... */
1495         ret = guest_translate_address_with_key(vcpu, address, ar, &gpa,
1496                                GACC_FETCH, access_key);
1497     }
1498     if (ret >= 0) {
1499         cc = -1;
1500 
1501         /* Fetching permitted; storing permitted */
1502         if (ret == 0 && writable)
1503             cc = 0;
1504         /* Fetching permitted; storing not permitted */
1505         else if (ret == 0 && !writable)
1506             cc = 1;
1507         /* Fetching not permitted; storing not permitted */
1508         else if (ret == PGM_PROTECTION)
1509             cc = 2;
1510         /* Translation not available */
1511         else if (ret != PGM_ADDRESSING && ret != PGM_TRANSLATION_SPEC)
1512             cc = 3;
1513 
1514         if (cc != -1) {
1515             kvm_s390_set_psw_cc(vcpu, cc);
1516             ret = 0;
1517         } else {
1518             ret = kvm_s390_inject_program_int(vcpu, ret);
1519         }
1520     }
1521 
1522     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1523         ipte_unlock(vcpu->kvm);
1524     return ret;
1525 }
1526 
1527 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1528 {
1529     switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1530     case 0x01:
1531         return handle_tprot(vcpu);
1532     default:
1533         return -EOPNOTSUPP;
1534     }
1535 }
1536 
1537 static int handle_sckpf(struct kvm_vcpu *vcpu)
1538 {
1539     u32 value;
1540 
1541     vcpu->stat.instruction_sckpf++;
1542 
1543     if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1544         return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1545 
1546     if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1547         return kvm_s390_inject_program_int(vcpu,
1548                            PGM_SPECIFICATION);
1549 
1550     value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1551     vcpu->arch.sie_block->todpr = value;
1552 
1553     return 0;
1554 }
1555 
1556 static int handle_ptff(struct kvm_vcpu *vcpu)
1557 {
1558     vcpu->stat.instruction_ptff++;
1559 
1560     /* we don't emulate any control instructions yet */
1561     kvm_s390_set_psw_cc(vcpu, 3);
1562     return 0;
1563 }
1564 
1565 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1566 {
1567     switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1568     case 0x04:
1569         return handle_ptff(vcpu);
1570     case 0x07:
1571         return handle_sckpf(vcpu);
1572     default:
1573         return -EOPNOTSUPP;
1574     }
1575 }