1 // SPDX-License-Identifier: GPL-2.0
3 * handling privileged instructions
5 * Copyright IBM Corp. 2008, 2020
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
11 #include <linux/kvm.h>
12 #include <linux/gfp.h>
13 #include <linux/errno.h>
14 #include <linux/compat.h>
15 #include <linux/mm_types.h>
16 #include <linux/pgtable.h>
18 #include <asm/asm-offsets.h>
19 #include <asm/facility.h>
20 #include <asm/current.h>
21 #include <asm/debug.h>
22 #include <asm/ebcdic.h>
23 #include <asm/sysinfo.h>
24 #include <asm/page-states.h>
27 #include <asm/ptrace.h>
34 static int handle_ri(struct kvm_vcpu *vcpu)
36 vcpu->stat.instruction_ri++;
38 if (test_kvm_facility(vcpu->kvm, 64)) {
39 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (lazy)");
40 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
41 kvm_s390_retry_instr(vcpu);
44 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
47 int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
49 if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
50 return handle_ri(vcpu);
55 static int handle_gs(struct kvm_vcpu *vcpu)
57 vcpu->stat.instruction_gs++;
59 if (test_kvm_facility(vcpu->kvm, 133)) {
60 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (lazy)");
63 current->thread.gs_cb = (struct gs_cb *)&vcpu->run->s.regs.gscb;
64 restore_gs_cb(current->thread.gs_cb);
66 vcpu->arch.sie_block->ecb |= ECB_GS;
67 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
68 vcpu->arch.gs_enabled = 1;
69 kvm_s390_retry_instr(vcpu);
72 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
75 int kvm_s390_handle_e3(struct kvm_vcpu *vcpu)
77 int code = vcpu->arch.sie_block->ipb & 0xff;
79 if (code == 0x49 || code == 0x4d)
80 return handle_gs(vcpu);
84 /* Handle SCK (SET CLOCK) interception */
85 static int handle_set_clock(struct kvm_vcpu *vcpu)
87 struct kvm_s390_vm_tod_clock gtod = { 0 };
92 vcpu->stat.instruction_sck++;
94 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
95 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
97 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
98 if (op2 & 7) /* Operand must be on a doubleword boundary */
99 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
100 rc = read_guest(vcpu, op2, ar, >od.tod, sizeof(gtod.tod));
102 return kvm_s390_inject_prog_cond(vcpu, rc);
104 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", gtod.tod);
106 * To set the TOD clock the kvm lock must be taken, but the vcpu lock
107 * is already held in handle_set_clock. The usual lock order is the
108 * opposite. As SCK is deprecated and should not be used in several
109 * cases, for example when the multiple epoch facility or TOD clock
110 * steering facility is installed (see Principles of Operation), a
111 * slow path can be used. If the lock can not be taken via try_lock,
112 * the instruction will be retried via -EAGAIN at a later point in
115 if (!kvm_s390_try_set_tod_clock(vcpu->kvm, >od)) {
116 kvm_s390_retry_instr(vcpu);
120 kvm_s390_set_psw_cc(vcpu, 0);
124 static int handle_set_prefix(struct kvm_vcpu *vcpu)
131 vcpu->stat.instruction_spx++;
133 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
134 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
136 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
138 /* must be word boundary */
140 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
143 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
145 return kvm_s390_inject_prog_cond(vcpu, rc);
147 address &= 0x7fffe000u;
150 * Make sure the new value is valid memory. We only need to check the
151 * first page, since address is 8k aligned and memory pieces are always
152 * at least 1MB aligned and have at least a size of 1MB.
154 if (kvm_is_error_gpa(vcpu->kvm, address))
155 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
157 kvm_s390_set_prefix(vcpu, address);
158 trace_kvm_s390_handle_prefix(vcpu, 1, address);
162 static int handle_store_prefix(struct kvm_vcpu *vcpu)
169 vcpu->stat.instruction_stpx++;
171 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
172 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
174 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
176 /* must be word boundary */
178 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
180 address = kvm_s390_get_prefix(vcpu);
183 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
185 return kvm_s390_inject_prog_cond(vcpu, rc);
187 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
188 trace_kvm_s390_handle_prefix(vcpu, 0, address);
192 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
194 u16 vcpu_id = vcpu->vcpu_id;
199 vcpu->stat.instruction_stap++;
201 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
202 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
204 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
207 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
209 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
211 return kvm_s390_inject_prog_cond(vcpu, rc);
213 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
214 trace_kvm_s390_handle_stap(vcpu, ga);
218 int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu)
222 trace_kvm_s390_skey_related_inst(vcpu);
223 /* Already enabled? */
224 if (vcpu->arch.skey_enabled)
227 rc = s390_enable_skey();
228 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
232 if (kvm_s390_test_cpuflags(vcpu, CPUSTAT_KSS))
233 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_KSS);
234 if (!vcpu->kvm->arch.use_skf)
235 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
237 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
238 vcpu->arch.skey_enabled = true;
242 static int try_handle_skey(struct kvm_vcpu *vcpu)
246 rc = kvm_s390_skey_check_enable(vcpu);
249 if (vcpu->kvm->arch.use_skf) {
250 /* with storage-key facility, SIE interprets it for us */
251 kvm_s390_retry_instr(vcpu);
252 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
258 static int handle_iske(struct kvm_vcpu *vcpu)
260 unsigned long gaddr, vmaddr;
266 vcpu->stat.instruction_iske++;
268 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
269 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
271 rc = try_handle_skey(vcpu);
273 return rc != -EAGAIN ? rc : 0;
275 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
277 gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
278 gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
279 gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
280 vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
281 if (kvm_is_error_hva(vmaddr))
282 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
285 mmap_read_lock(current->mm);
286 rc = get_guest_storage_key(current->mm, vmaddr, &key);
289 rc = fixup_user_fault(current->mm, vmaddr,
290 FAULT_FLAG_WRITE, &unlocked);
292 mmap_read_unlock(current->mm);
296 mmap_read_unlock(current->mm);
298 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
301 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
302 vcpu->run->s.regs.gprs[reg1] |= key;
306 static int handle_rrbe(struct kvm_vcpu *vcpu)
308 unsigned long vmaddr, gaddr;
313 vcpu->stat.instruction_rrbe++;
315 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
316 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
318 rc = try_handle_skey(vcpu);
320 return rc != -EAGAIN ? rc : 0;
322 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
324 gaddr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
325 gaddr = kvm_s390_logical_to_effective(vcpu, gaddr);
326 gaddr = kvm_s390_real_to_abs(vcpu, gaddr);
327 vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(gaddr));
328 if (kvm_is_error_hva(vmaddr))
329 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
332 mmap_read_lock(current->mm);
333 rc = reset_guest_reference_bit(current->mm, vmaddr);
335 rc = fixup_user_fault(current->mm, vmaddr,
336 FAULT_FLAG_WRITE, &unlocked);
338 mmap_read_unlock(current->mm);
342 mmap_read_unlock(current->mm);
344 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
347 kvm_s390_set_psw_cc(vcpu, rc);
355 static int handle_sske(struct kvm_vcpu *vcpu)
357 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
358 unsigned long start, end;
359 unsigned char key, oldkey;
364 vcpu->stat.instruction_sske++;
366 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
367 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
369 rc = try_handle_skey(vcpu);
371 return rc != -EAGAIN ? rc : 0;
373 if (!test_kvm_facility(vcpu->kvm, 8))
375 if (!test_kvm_facility(vcpu->kvm, 10))
376 m3 &= ~(SSKE_MC | SSKE_MR);
377 if (!test_kvm_facility(vcpu->kvm, 14))
380 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
382 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
383 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
384 start = kvm_s390_logical_to_effective(vcpu, start);
386 /* start already designates an absolute address */
387 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
389 start = kvm_s390_real_to_abs(vcpu, start);
390 end = start + PAGE_SIZE;
393 while (start != end) {
394 unsigned long vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
397 if (kvm_is_error_hva(vmaddr))
398 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
400 mmap_read_lock(current->mm);
401 rc = cond_set_guest_storage_key(current->mm, vmaddr, key, &oldkey,
402 m3 & SSKE_NQ, m3 & SSKE_MR,
406 rc = fixup_user_fault(current->mm, vmaddr,
407 FAULT_FLAG_WRITE, &unlocked);
408 rc = !rc ? -EAGAIN : rc;
410 mmap_read_unlock(current->mm);
412 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
420 if (m3 & (SSKE_MC | SSKE_MR)) {
422 /* skey in reg1 is unpredictable */
423 kvm_s390_set_psw_cc(vcpu, 3);
425 kvm_s390_set_psw_cc(vcpu, rc);
426 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
427 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
431 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT)
432 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
434 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
435 end = kvm_s390_logical_to_effective(vcpu, end);
436 vcpu->run->s.regs.gprs[reg2] |= end;
441 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
443 vcpu->stat.instruction_ipte_interlock++;
444 if (psw_bits(vcpu->arch.sie_block->gpsw).pstate)
445 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
446 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
447 kvm_s390_retry_instr(vcpu);
448 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
452 static int handle_test_block(struct kvm_vcpu *vcpu)
457 vcpu->stat.instruction_tb++;
459 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
460 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
462 kvm_s390_get_regs_rre(vcpu, NULL, ®2);
463 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
464 addr = kvm_s390_logical_to_effective(vcpu, addr);
465 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
466 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
467 addr = kvm_s390_real_to_abs(vcpu, addr);
469 if (kvm_is_error_gpa(vcpu->kvm, addr))
470 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
472 * We don't expect errors on modern systems, and do not care
473 * about storage keys (yet), so let's just clear the page.
475 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
477 kvm_s390_set_psw_cc(vcpu, 0);
478 vcpu->run->s.regs.gprs[0] = 0;
482 static int handle_tpi(struct kvm_vcpu *vcpu)
484 struct kvm_s390_interrupt_info *inti;
491 vcpu->stat.instruction_tpi++;
493 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
495 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
497 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
499 kvm_s390_set_psw_cc(vcpu, 0);
503 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
504 tpi_data[1] = inti->io.io_int_parm;
505 tpi_data[2] = inti->io.io_int_word;
508 * Store the two-word I/O interruption code into the
511 len = sizeof(tpi_data) - 4;
512 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
514 rc = kvm_s390_inject_prog_cond(vcpu, rc);
515 goto reinject_interrupt;
519 * Store the three-word I/O interruption code into
520 * the appropriate lowcore area.
522 len = sizeof(tpi_data);
523 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
524 /* failed writes to the low core are not recoverable */
526 goto reinject_interrupt;
530 /* irq was successfully handed to the guest */
532 kvm_s390_set_psw_cc(vcpu, 1);
536 * If we encounter a problem storing the interruption code, the
537 * instruction is suppressed from the guest's view: reinject the
540 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
544 /* don't set the cc, a pgm irq was injected or we drop to user space */
545 return rc ? -EFAULT : 0;
548 static int handle_tsch(struct kvm_vcpu *vcpu)
550 struct kvm_s390_interrupt_info *inti = NULL;
551 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
553 vcpu->stat.instruction_tsch++;
555 /* a valid schid has at least one bit set */
556 if (vcpu->run->s.regs.gprs[1])
557 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
558 vcpu->run->s.regs.gprs[1]);
561 * Prepare exit to userspace.
562 * We indicate whether we dequeued a pending I/O interrupt
563 * so that userspace can re-inject it if the instruction gets
564 * a program check. While this may re-order the pending I/O
565 * interrupts, this is no problem since the priority is kept
568 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
569 vcpu->run->s390_tsch.dequeued = !!inti;
571 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
572 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
573 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
574 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
576 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
581 static int handle_io_inst(struct kvm_vcpu *vcpu)
583 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
585 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
586 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
588 if (vcpu->kvm->arch.css_support) {
590 * Most I/O instructions will be handled by userspace.
591 * Exceptions are tpi and the interrupt portion of tsch.
593 if (vcpu->arch.sie_block->ipa == 0xb236)
594 return handle_tpi(vcpu);
595 if (vcpu->arch.sie_block->ipa == 0xb235)
596 return handle_tsch(vcpu);
597 /* Handle in userspace. */
598 vcpu->stat.instruction_io_other++;
602 * Set condition code 3 to stop the guest from issuing channel
605 kvm_s390_set_psw_cc(vcpu, 3);
611 * handle_pqap: Handling pqap interception
612 * @vcpu: the vcpu having issue the pqap instruction
614 * We now support PQAP/AQIC instructions and we need to correctly
615 * answer the guest even if no dedicated driver's hook is available.
617 * The intercepting code calls a dedicated callback for this instruction
618 * if a driver did register one in the CRYPTO satellite of the
621 * If no callback is available, the queues are not available, return this
622 * response code to the caller and set CC to 3.
623 * Else return the response code returned by the callback.
625 static int handle_pqap(struct kvm_vcpu *vcpu)
627 struct ap_queue_status status = {};
632 /* Verify that the AP instruction are available */
633 if (!ap_instructions_available())
635 /* Verify that the guest is allowed to use AP instructions */
636 if (!(vcpu->arch.sie_block->eca & ECA_APIE))
639 * The only possibly intercepted functions when AP instructions are
640 * available for the guest are AQIC and TAPQ with the t bit set
641 * since we do not set IC.3 (FIII) we currently will only intercept
642 * the AQIC function code.
643 * Note: running nested under z/VM can result in intercepts for other
644 * function codes, e.g. PQAP(QCI). We do not support this and bail out.
646 reg0 = vcpu->run->s.regs.gprs[0];
647 fc = (reg0 >> 24) & 0xff;
651 /* PQAP instruction is allowed for guest kernel only */
652 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
653 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
655 /* Common PQAP instruction specification exceptions */
656 /* bits 41-47 must all be zeros */
657 if (reg0 & 0x007f0000UL)
658 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
659 /* APFT not install and T bit set */
660 if (!test_kvm_facility(vcpu->kvm, 15) && (reg0 & 0x00800000UL))
661 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
662 /* APXA not installed and APID greater 64 or APQI greater 16 */
663 if (!(vcpu->kvm->arch.crypto.crycbd & 0x02) && (reg0 & 0x0000c0f0UL))
664 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
666 /* AQIC function code specific exception */
667 /* facility 65 not present for AQIC function code */
668 if (!test_kvm_facility(vcpu->kvm, 65))
669 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
672 * Verify that the hook callback is registered, lock the owner
675 if (vcpu->kvm->arch.crypto.pqap_hook) {
676 if (!try_module_get(vcpu->kvm->arch.crypto.pqap_hook->owner))
678 ret = vcpu->kvm->arch.crypto.pqap_hook->hook(vcpu);
679 module_put(vcpu->kvm->arch.crypto.pqap_hook->owner);
680 if (!ret && vcpu->run->s.regs.gprs[1] & 0x00ff0000)
681 kvm_s390_set_psw_cc(vcpu, 3);
685 * A vfio_driver must register a hook.
686 * No hook means no driver to enable the SIE CRYCB and no queues.
687 * We send this response to the guest.
689 status.response_code = 0x01;
690 memcpy(&vcpu->run->s.regs.gprs[1], &status, sizeof(status));
691 kvm_s390_set_psw_cc(vcpu, 3);
695 static int handle_stfl(struct kvm_vcpu *vcpu)
700 vcpu->stat.instruction_stfl++;
702 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
703 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
706 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
707 * into a u32 memory representation. They will remain bits 0-31.
709 fac = *vcpu->kvm->arch.model.fac_list >> 32;
710 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
714 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
715 trace_kvm_s390_handle_stfl(vcpu, fac);
719 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
720 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
721 #define PSW_ADDR_24 0x0000000000ffffffUL
722 #define PSW_ADDR_31 0x000000007fffffffUL
724 int is_valid_psw(psw_t *psw)
726 if (psw->mask & PSW_MASK_UNASSIGNED)
728 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
729 if (psw->addr & ~PSW_ADDR_31)
732 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
734 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
741 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
743 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
744 psw_compat_t new_psw;
749 vcpu->stat.instruction_lpsw++;
751 if (gpsw->mask & PSW_MASK_PSTATE)
752 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
754 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
756 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
758 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
760 return kvm_s390_inject_prog_cond(vcpu, rc);
761 if (!(new_psw.mask & PSW32_MASK_BASE))
762 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
763 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
764 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
765 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
766 if (!is_valid_psw(gpsw))
767 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
771 static int handle_lpswe(struct kvm_vcpu *vcpu)
778 vcpu->stat.instruction_lpswe++;
780 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
781 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
783 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
785 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
786 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
788 return kvm_s390_inject_prog_cond(vcpu, rc);
789 vcpu->arch.sie_block->gpsw = new_psw;
790 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
791 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
795 static int handle_stidp(struct kvm_vcpu *vcpu)
797 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
802 vcpu->stat.instruction_stidp++;
804 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
805 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
807 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
810 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
812 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
814 return kvm_s390_inject_prog_cond(vcpu, rc);
816 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
820 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
825 cpus = atomic_read(&vcpu->kvm->online_vcpus);
827 /* deal with other level 3 hypervisors */
828 if (stsi(mem, 3, 2, 2))
832 for (n = mem->count - 1; n > 0 ; n--)
833 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
835 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
836 mem->vm[0].cpus_total = cpus;
837 mem->vm[0].cpus_configured = cpus;
838 mem->vm[0].cpus_standby = 0;
839 mem->vm[0].cpus_reserved = 0;
840 mem->vm[0].caf = 1000;
841 memcpy(mem->vm[0].name, "KVMguest", 8);
842 ASCEBC(mem->vm[0].name, 8);
843 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
844 ASCEBC(mem->vm[0].cpi, 16);
847 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, u8 ar,
848 u8 fc, u8 sel1, u16 sel2)
850 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
851 vcpu->run->s390_stsi.addr = addr;
852 vcpu->run->s390_stsi.ar = ar;
853 vcpu->run->s390_stsi.fc = fc;
854 vcpu->run->s390_stsi.sel1 = sel1;
855 vcpu->run->s390_stsi.sel2 = sel2;
858 static int handle_stsi(struct kvm_vcpu *vcpu)
860 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
861 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
862 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
863 unsigned long mem = 0;
868 vcpu->stat.instruction_stsi++;
869 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
871 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
872 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
875 kvm_s390_set_psw_cc(vcpu, 3);
879 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
880 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
881 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
884 vcpu->run->s.regs.gprs[0] = 3 << 28;
885 kvm_s390_set_psw_cc(vcpu, 0);
889 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
891 if (!kvm_s390_pv_cpu_is_protected(vcpu) && (operand2 & 0xfff))
892 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
895 case 1: /* same handling for 1 and 2 */
897 mem = get_zeroed_page(GFP_KERNEL);
900 if (stsi((void *) mem, fc, sel1, sel2))
904 if (sel1 != 2 || sel2 != 2)
906 mem = get_zeroed_page(GFP_KERNEL);
909 handle_stsi_3_2_2(vcpu, (void *) mem);
912 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
913 memcpy((void *)sida_origin(vcpu->arch.sie_block), (void *)mem,
917 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
920 rc = kvm_s390_inject_prog_cond(vcpu, rc);
923 if (vcpu->kvm->arch.user_stsi) {
924 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
927 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
929 kvm_s390_set_psw_cc(vcpu, 0);
930 vcpu->run->s.regs.gprs[0] = 0;
933 kvm_s390_set_psw_cc(vcpu, 3);
939 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
941 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
943 return handle_stidp(vcpu);
945 return handle_set_clock(vcpu);
947 return handle_set_prefix(vcpu);
949 return handle_store_prefix(vcpu);
951 return handle_store_cpu_address(vcpu);
953 return kvm_s390_handle_vsie(vcpu);
956 return handle_ipte_interlock(vcpu);
958 return handle_iske(vcpu);
960 return handle_rrbe(vcpu);
962 return handle_sske(vcpu);
964 return handle_test_block(vcpu);
981 return handle_io_inst(vcpu);
983 return handle_sthyi(vcpu);
985 return handle_stsi(vcpu);
987 return handle_pqap(vcpu);
989 return handle_stfl(vcpu);
991 return handle_lpswe(vcpu);
997 static int handle_epsw(struct kvm_vcpu *vcpu)
1001 vcpu->stat.instruction_epsw++;
1003 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
1005 /* This basically extracts the mask half of the psw. */
1006 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
1007 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
1009 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
1010 vcpu->run->s.regs.gprs[reg2] |=
1011 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
1016 #define PFMF_RESERVED 0xfffc0101UL
1017 #define PFMF_SK 0x00020000UL
1018 #define PFMF_CF 0x00010000UL
1019 #define PFMF_UI 0x00008000UL
1020 #define PFMF_FSC 0x00007000UL
1021 #define PFMF_NQ 0x00000800UL
1022 #define PFMF_MR 0x00000400UL
1023 #define PFMF_MC 0x00000200UL
1024 #define PFMF_KEY 0x000000feUL
1026 static int handle_pfmf(struct kvm_vcpu *vcpu)
1028 bool mr = false, mc = false, nq;
1030 unsigned long start, end;
1033 vcpu->stat.instruction_pfmf++;
1035 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
1037 if (!test_kvm_facility(vcpu->kvm, 8))
1038 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1040 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1041 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1043 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
1044 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1046 /* Only provide non-quiescing support if enabled for the guest */
1047 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
1048 !test_kvm_facility(vcpu->kvm, 14))
1049 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1051 /* Only provide conditional-SSKE support if enabled for the guest */
1052 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
1053 test_kvm_facility(vcpu->kvm, 10)) {
1054 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
1055 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
1058 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
1059 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
1060 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
1061 start = kvm_s390_logical_to_effective(vcpu, start);
1063 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
1064 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
1065 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
1068 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
1070 /* only 4k frames specify a real address */
1071 start = kvm_s390_real_to_abs(vcpu, start);
1072 end = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1075 end = (start + _SEGMENT_SIZE) & ~(_SEGMENT_SIZE - 1);
1078 /* only support 2G frame size if EDAT2 is available and we are
1079 not in 24-bit addressing mode */
1080 if (!test_kvm_facility(vcpu->kvm, 78) ||
1081 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_24BIT)
1082 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1083 end = (start + _REGION3_SIZE) & ~(_REGION3_SIZE - 1);
1086 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1089 while (start != end) {
1090 unsigned long vmaddr;
1091 bool unlocked = false;
1093 /* Translate guest address to host address */
1094 vmaddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
1095 if (kvm_is_error_hva(vmaddr))
1096 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1098 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
1099 if (kvm_clear_guest(vcpu->kvm, start, PAGE_SIZE))
1100 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1103 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
1104 int rc = kvm_s390_skey_check_enable(vcpu);
1108 mmap_read_lock(current->mm);
1109 rc = cond_set_guest_storage_key(current->mm, vmaddr,
1110 key, NULL, nq, mr, mc);
1112 rc = fixup_user_fault(current->mm, vmaddr,
1113 FAULT_FLAG_WRITE, &unlocked);
1114 rc = !rc ? -EAGAIN : rc;
1116 mmap_read_unlock(current->mm);
1118 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1126 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
1127 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_BITS_AMODE_64BIT) {
1128 vcpu->run->s.regs.gprs[reg2] = end;
1130 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
1131 end = kvm_s390_logical_to_effective(vcpu, end);
1132 vcpu->run->s.regs.gprs[reg2] |= end;
1139 * Must be called with relevant read locks held (kvm->mm->mmap_lock, kvm->srcu)
1141 static inline int __do_essa(struct kvm_vcpu *vcpu, const int orc)
1143 int r1, r2, nappended, entries;
1144 unsigned long gfn, hva, res, pgstev, ptev;
1145 unsigned long *cbrlo;
1148 * We don't need to set SD.FPF.SK to 1 here, because if we have a
1149 * machine check here we either handle it or crash
1152 kvm_s390_get_regs_rre(vcpu, &r1, &r2);
1153 gfn = vcpu->run->s.regs.gprs[r2] >> PAGE_SHIFT;
1154 hva = gfn_to_hva(vcpu->kvm, gfn);
1155 entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1157 if (kvm_is_error_hva(hva))
1158 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1160 nappended = pgste_perform_essa(vcpu->kvm->mm, hva, orc, &ptev, &pgstev);
1161 if (nappended < 0) {
1162 res = orc ? 0x10 : 0;
1163 vcpu->run->s.regs.gprs[r1] = res; /* Exception Indication */
1166 res = (pgstev & _PGSTE_GPS_USAGE_MASK) >> 22;
1168 * Set the block-content state part of the result. 0 means resident, so
1169 * nothing to do if the page is valid. 2 is for preserved pages
1170 * (non-present and non-zero), and 3 for zero pages (non-present and
1173 if (ptev & _PAGE_INVALID) {
1175 if (pgstev & _PGSTE_GPS_ZERO)
1178 if (pgstev & _PGSTE_GPS_NODAT)
1180 vcpu->run->s.regs.gprs[r1] = res;
1182 * It is possible that all the normal 511 slots were full, in which case
1183 * we will now write in the 512th slot, which is reserved for host use.
1184 * In both cases we let the normal essa handling code process all the
1185 * slots, including the reserved one, if needed.
1187 if (nappended > 0) {
1188 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo & PAGE_MASK);
1189 cbrlo[entries] = gfn << PAGE_SHIFT;
1193 struct kvm_memory_slot *ms = gfn_to_memslot(vcpu->kvm, gfn);
1195 /* Increment only if we are really flipping the bit */
1196 if (ms && !test_and_set_bit(gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
1197 atomic64_inc(&vcpu->kvm->arch.cmma_dirty_pages);
1203 static int handle_essa(struct kvm_vcpu *vcpu)
1205 /* entries expected to be 1FF */
1206 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
1207 unsigned long *cbrlo;
1211 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
1212 gmap = vcpu->arch.gmap;
1213 vcpu->stat.instruction_essa++;
1214 if (!vcpu->kvm->arch.use_cmma)
1215 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
1217 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1218 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1219 /* Check for invalid operation request code */
1220 orc = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
1221 /* ORCs 0-6 are always valid */
1222 if (orc > (test_kvm_facility(vcpu->kvm, 147) ? ESSA_SET_STABLE_NODAT
1223 : ESSA_SET_STABLE_IF_RESIDENT))
1224 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1226 if (!vcpu->kvm->arch.migration_mode) {
1228 * CMMA is enabled in the KVM settings, but is disabled in
1229 * the SIE block and in the mm_context, and we are not doing
1230 * a migration. Enable CMMA in the mm_context.
1231 * Since we need to take a write lock to write to the context
1232 * to avoid races with storage keys handling, we check if the
1233 * value really needs to be written to; if the value is
1234 * already correct, we do nothing and avoid the lock.
1236 if (vcpu->kvm->mm->context.uses_cmm == 0) {
1237 mmap_write_lock(vcpu->kvm->mm);
1238 vcpu->kvm->mm->context.uses_cmm = 1;
1239 mmap_write_unlock(vcpu->kvm->mm);
1242 * If we are here, we are supposed to have CMMA enabled in
1243 * the SIE block. Enabling CMMA works on a per-CPU basis,
1244 * while the context use_cmma flag is per process.
1245 * It's possible that the context flag is enabled and the
1246 * SIE flag is not, so we set the flag always; if it was
1247 * already set, nothing changes, otherwise we enable it
1250 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
1251 /* Retry the ESSA instruction */
1252 kvm_s390_retry_instr(vcpu);
1256 mmap_read_lock(vcpu->kvm->mm);
1257 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1258 i = __do_essa(vcpu, orc);
1259 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1260 mmap_read_unlock(vcpu->kvm->mm);
1263 /* Account for the possible extra cbrl entry */
1266 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
1267 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
1268 mmap_read_lock(gmap->mm);
1269 for (i = 0; i < entries; ++i)
1270 __gmap_zap(gmap, cbrlo[i]);
1271 mmap_read_unlock(gmap->mm);
1275 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
1277 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1281 return handle_ipte_interlock(vcpu);
1283 return handle_epsw(vcpu);
1285 return handle_essa(vcpu);
1287 return handle_pfmf(vcpu);
1293 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
1295 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1296 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1297 int reg, rc, nr_regs;
1302 vcpu->stat.instruction_lctl++;
1304 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1305 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1307 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1310 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1312 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1313 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
1315 nr_regs = ((reg3 - reg1) & 0xf) + 1;
1316 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1318 return kvm_s390_inject_prog_cond(vcpu, rc);
1322 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
1323 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
1326 reg = (reg + 1) % 16;
1328 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1332 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1334 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1335 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1336 int reg, rc, nr_regs;
1341 vcpu->stat.instruction_stctl++;
1343 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1344 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1346 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1349 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1351 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1352 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1357 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1360 reg = (reg + 1) % 16;
1362 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1363 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1366 static int handle_lctlg(struct kvm_vcpu *vcpu)
1368 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1369 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1370 int reg, rc, nr_regs;
1375 vcpu->stat.instruction_lctlg++;
1377 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1378 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1380 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1383 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1385 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1386 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
1388 nr_regs = ((reg3 - reg1) & 0xf) + 1;
1389 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1391 return kvm_s390_inject_prog_cond(vcpu, rc);
1395 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
1398 reg = (reg + 1) % 16;
1400 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1404 static int handle_stctg(struct kvm_vcpu *vcpu)
1406 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1407 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1408 int reg, rc, nr_regs;
1413 vcpu->stat.instruction_stctg++;
1415 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1416 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1418 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1421 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1423 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1424 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1429 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1432 reg = (reg + 1) % 16;
1434 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1435 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1438 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
1440 switch (vcpu->arch.sie_block->ipb & 0x000000ff) {
1442 return handle_stctg(vcpu);
1444 return handle_lctlg(vcpu);
1448 return handle_ri(vcpu);
1454 static int handle_tprot(struct kvm_vcpu *vcpu)
1456 u64 address1, address2;
1457 unsigned long hva, gpa;
1458 int ret = 0, cc = 0;
1462 vcpu->stat.instruction_tprot++;
1464 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1465 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1467 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
1469 /* we only handle the Linux memory detection case:
1471 * everything else goes to userspace. */
1472 if (address2 & 0xf0)
1474 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1476 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
1477 if (ret == PGM_PROTECTION) {
1478 /* Write protected? Try again with read-only... */
1480 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1484 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1485 ret = kvm_s390_inject_program_int(vcpu, ret);
1486 } else if (ret > 0) {
1487 /* Translation not available */
1488 kvm_s390_set_psw_cc(vcpu, 3);
1494 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1495 if (kvm_is_error_hva(hva)) {
1496 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1499 cc = 1; /* Write not permitted ==> read-only */
1500 kvm_s390_set_psw_cc(vcpu, cc);
1501 /* Note: CC2 only occurs for storage keys (not supported yet) */
1504 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1509 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1511 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1513 return handle_tprot(vcpu);
1519 static int handle_sckpf(struct kvm_vcpu *vcpu)
1523 vcpu->stat.instruction_sckpf++;
1525 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1526 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1528 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1529 return kvm_s390_inject_program_int(vcpu,
1532 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1533 vcpu->arch.sie_block->todpr = value;
1538 static int handle_ptff(struct kvm_vcpu *vcpu)
1540 vcpu->stat.instruction_ptff++;
1542 /* we don't emulate any control instructions yet */
1543 kvm_s390_set_psw_cc(vcpu, 3);
1547 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1549 switch (vcpu->arch.sie_block->ipa & 0x00ff) {
1551 return handle_ptff(vcpu);
1553 return handle_sckpf(vcpu);