2 * handling privileged instructions
4 * Copyright IBM Corp. 2008, 2013
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
14 #include <linux/kvm.h>
15 #include <linux/gfp.h>
16 #include <linux/errno.h>
17 #include <linux/compat.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/pgtable.h>
25 #include <asm/pgalloc.h>
27 #include <asm/ptrace.h>
28 #include <asm/compat.h>
33 /* Handle SCK (SET CLOCK) interception */
34 static int handle_set_clock(struct kvm_vcpu *vcpu)
36 struct kvm_vcpu *cpup;
41 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
42 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
44 op2 = kvm_s390_get_base_disp_s(vcpu);
45 if (op2 & 7) /* Operand must be on a doubleword boundary */
46 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
47 rc = read_guest(vcpu, op2, &val, sizeof(val));
49 return kvm_s390_inject_prog_cond(vcpu, rc);
51 if (store_tod_clock(&hostclk)) {
52 kvm_s390_set_psw_cc(vcpu, 3);
55 val = (val - hostclk) & ~0x3fUL;
57 mutex_lock(&vcpu->kvm->lock);
58 kvm_for_each_vcpu(i, cpup, vcpu->kvm)
59 cpup->arch.sie_block->epoch = val;
60 mutex_unlock(&vcpu->kvm->lock);
62 kvm_s390_set_psw_cc(vcpu, 0);
66 static int handle_set_prefix(struct kvm_vcpu *vcpu)
72 vcpu->stat.instruction_spx++;
74 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
75 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
77 operand2 = kvm_s390_get_base_disp_s(vcpu);
79 /* must be word boundary */
81 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
84 rc = read_guest(vcpu, operand2, &address, sizeof(address));
86 return kvm_s390_inject_prog_cond(vcpu, rc);
88 address &= 0x7fffe000u;
91 * Make sure the new value is valid memory. We only need to check the
92 * first page, since address is 8k aligned and memory pieces are always
93 * at least 1MB aligned and have at least a size of 1MB.
95 if (kvm_is_error_gpa(vcpu->kvm, address))
96 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
98 kvm_s390_set_prefix(vcpu, address);
100 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
101 trace_kvm_s390_handle_prefix(vcpu, 1, address);
105 static int handle_store_prefix(struct kvm_vcpu *vcpu)
111 vcpu->stat.instruction_stpx++;
113 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
114 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
116 operand2 = kvm_s390_get_base_disp_s(vcpu);
118 /* must be word boundary */
120 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
122 address = kvm_s390_get_prefix(vcpu);
125 rc = write_guest(vcpu, operand2, &address, sizeof(address));
127 return kvm_s390_inject_prog_cond(vcpu, rc);
129 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
130 trace_kvm_s390_handle_prefix(vcpu, 0, address);
134 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
136 u16 vcpu_id = vcpu->vcpu_id;
140 vcpu->stat.instruction_stap++;
142 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
143 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
145 ga = kvm_s390_get_base_disp_s(vcpu);
148 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
150 rc = write_guest(vcpu, ga, &vcpu_id, sizeof(vcpu_id));
152 return kvm_s390_inject_prog_cond(vcpu, rc);
154 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", ga);
155 trace_kvm_s390_handle_stap(vcpu, ga);
159 static int __skey_check_enable(struct kvm_vcpu *vcpu)
162 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
165 rc = s390_enable_skey();
166 trace_kvm_s390_skey_related_inst(vcpu);
167 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
172 static int handle_skey(struct kvm_vcpu *vcpu)
174 int rc = __skey_check_enable(vcpu);
178 vcpu->stat.instruction_storage_key++;
180 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
181 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
183 kvm_s390_rewind_psw(vcpu, 4);
184 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
188 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
190 vcpu->stat.instruction_ipte_interlock++;
191 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
192 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
193 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
194 kvm_s390_rewind_psw(vcpu, 4);
195 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
199 static int handle_test_block(struct kvm_vcpu *vcpu)
204 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
205 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
207 kvm_s390_get_regs_rre(vcpu, NULL, ®2);
208 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
209 addr = kvm_s390_logical_to_effective(vcpu, addr);
210 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
211 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
212 addr = kvm_s390_real_to_abs(vcpu, addr);
214 if (kvm_is_error_gpa(vcpu->kvm, addr))
215 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
217 * We don't expect errors on modern systems, and do not care
218 * about storage keys (yet), so let's just clear the page.
220 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
222 kvm_s390_set_psw_cc(vcpu, 0);
223 vcpu->run->s.regs.gprs[0] = 0;
227 static int handle_tpi(struct kvm_vcpu *vcpu)
229 struct kvm_s390_interrupt_info *inti;
235 addr = kvm_s390_get_base_disp_s(vcpu);
237 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
239 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
241 kvm_s390_set_psw_cc(vcpu, 0);
245 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
246 tpi_data[1] = inti->io.io_int_parm;
247 tpi_data[2] = inti->io.io_int_word;
250 * Store the two-word I/O interruption code into the
253 len = sizeof(tpi_data) - 4;
254 rc = write_guest(vcpu, addr, &tpi_data, len);
256 rc = kvm_s390_inject_prog_cond(vcpu, rc);
257 goto reinject_interrupt;
261 * Store the three-word I/O interruption code into
262 * the appropriate lowcore area.
264 len = sizeof(tpi_data);
265 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
266 /* failed writes to the low core are not recoverable */
268 goto reinject_interrupt;
272 /* irq was successfully handed to the guest */
274 kvm_s390_set_psw_cc(vcpu, 1);
278 * If we encounter a problem storing the interruption code, the
279 * instruction is suppressed from the guest's view: reinject the
282 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
286 /* don't set the cc, a pgm irq was injected or we drop to user space */
287 return rc ? -EFAULT : 0;
290 static int handle_tsch(struct kvm_vcpu *vcpu)
292 struct kvm_s390_interrupt_info *inti;
294 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
295 vcpu->run->s.regs.gprs[1]);
298 * Prepare exit to userspace.
299 * We indicate whether we dequeued a pending I/O interrupt
300 * so that userspace can re-inject it if the instruction gets
301 * a program check. While this may re-order the pending I/O
302 * interrupts, this is no problem since the priority is kept
305 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
306 vcpu->run->s390_tsch.dequeued = !!inti;
308 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
309 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
310 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
311 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
313 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
318 static int handle_io_inst(struct kvm_vcpu *vcpu)
320 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
322 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
323 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
325 if (vcpu->kvm->arch.css_support) {
327 * Most I/O instructions will be handled by userspace.
328 * Exceptions are tpi and the interrupt portion of tsch.
330 if (vcpu->arch.sie_block->ipa == 0xb236)
331 return handle_tpi(vcpu);
332 if (vcpu->arch.sie_block->ipa == 0xb235)
333 return handle_tsch(vcpu);
334 /* Handle in userspace. */
338 * Set condition code 3 to stop the guest from issuing channel
341 kvm_s390_set_psw_cc(vcpu, 3);
346 static int handle_stfl(struct kvm_vcpu *vcpu)
351 vcpu->stat.instruction_stfl++;
353 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
354 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
357 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
358 * into a u32 memory representation. They will remain bits 0-31.
360 fac = *vcpu->kvm->arch.model.fac->list >> 32;
361 rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
365 VCPU_EVENT(vcpu, 5, "store facility list value %x", fac);
366 trace_kvm_s390_handle_stfl(vcpu, fac);
370 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
371 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
372 #define PSW_ADDR_24 0x0000000000ffffffUL
373 #define PSW_ADDR_31 0x000000007fffffffUL
375 int is_valid_psw(psw_t *psw)
377 if (psw->mask & PSW_MASK_UNASSIGNED)
379 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
380 if (psw->addr & ~PSW_ADDR_31)
383 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
385 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
392 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
394 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
395 psw_compat_t new_psw;
399 if (gpsw->mask & PSW_MASK_PSTATE)
400 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
402 addr = kvm_s390_get_base_disp_s(vcpu);
404 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
406 rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
408 return kvm_s390_inject_prog_cond(vcpu, rc);
409 if (!(new_psw.mask & PSW32_MASK_BASE))
410 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
411 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
412 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
413 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
414 if (!is_valid_psw(gpsw))
415 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
419 static int handle_lpswe(struct kvm_vcpu *vcpu)
425 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
426 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
428 addr = kvm_s390_get_base_disp_s(vcpu);
430 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
431 rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
433 return kvm_s390_inject_prog_cond(vcpu, rc);
434 vcpu->arch.sie_block->gpsw = new_psw;
435 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
436 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
440 static int handle_stidp(struct kvm_vcpu *vcpu)
442 u64 stidp_data = vcpu->arch.stidp_data;
446 vcpu->stat.instruction_stidp++;
448 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
449 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
451 operand2 = kvm_s390_get_base_disp_s(vcpu);
454 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
456 rc = write_guest(vcpu, operand2, &stidp_data, sizeof(stidp_data));
458 return kvm_s390_inject_prog_cond(vcpu, rc);
460 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
464 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
469 cpus = atomic_read(&vcpu->kvm->online_vcpus);
471 /* deal with other level 3 hypervisors */
472 if (stsi(mem, 3, 2, 2))
476 for (n = mem->count - 1; n > 0 ; n--)
477 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
479 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
480 mem->vm[0].cpus_total = cpus;
481 mem->vm[0].cpus_configured = cpus;
482 mem->vm[0].cpus_standby = 0;
483 mem->vm[0].cpus_reserved = 0;
484 mem->vm[0].caf = 1000;
485 memcpy(mem->vm[0].name, "KVMguest", 8);
486 ASCEBC(mem->vm[0].name, 8);
487 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
488 ASCEBC(mem->vm[0].cpi, 16);
491 static int handle_stsi(struct kvm_vcpu *vcpu)
493 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
494 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
495 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
496 unsigned long mem = 0;
500 vcpu->stat.instruction_stsi++;
501 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
503 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
504 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
507 kvm_s390_set_psw_cc(vcpu, 3);
511 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
512 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
513 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
516 vcpu->run->s.regs.gprs[0] = 3 << 28;
517 kvm_s390_set_psw_cc(vcpu, 0);
521 operand2 = kvm_s390_get_base_disp_s(vcpu);
523 if (operand2 & 0xfff)
524 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
527 case 1: /* same handling for 1 and 2 */
529 mem = get_zeroed_page(GFP_KERNEL);
532 if (stsi((void *) mem, fc, sel1, sel2))
536 if (sel1 != 2 || sel2 != 2)
538 mem = get_zeroed_page(GFP_KERNEL);
541 handle_stsi_3_2_2(vcpu, (void *) mem);
545 rc = write_guest(vcpu, operand2, (void *)mem, PAGE_SIZE);
547 rc = kvm_s390_inject_prog_cond(vcpu, rc);
550 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
552 kvm_s390_set_psw_cc(vcpu, 0);
553 vcpu->run->s.regs.gprs[0] = 0;
556 kvm_s390_set_psw_cc(vcpu, 3);
562 static const intercept_handler_t b2_handlers[256] = {
563 [0x02] = handle_stidp,
564 [0x04] = handle_set_clock,
565 [0x10] = handle_set_prefix,
566 [0x11] = handle_store_prefix,
567 [0x12] = handle_store_cpu_address,
568 [0x21] = handle_ipte_interlock,
569 [0x29] = handle_skey,
570 [0x2a] = handle_skey,
571 [0x2b] = handle_skey,
572 [0x2c] = handle_test_block,
573 [0x30] = handle_io_inst,
574 [0x31] = handle_io_inst,
575 [0x32] = handle_io_inst,
576 [0x33] = handle_io_inst,
577 [0x34] = handle_io_inst,
578 [0x35] = handle_io_inst,
579 [0x36] = handle_io_inst,
580 [0x37] = handle_io_inst,
581 [0x38] = handle_io_inst,
582 [0x39] = handle_io_inst,
583 [0x3a] = handle_io_inst,
584 [0x3b] = handle_io_inst,
585 [0x3c] = handle_io_inst,
586 [0x50] = handle_ipte_interlock,
587 [0x5f] = handle_io_inst,
588 [0x74] = handle_io_inst,
589 [0x76] = handle_io_inst,
590 [0x7d] = handle_stsi,
591 [0xb1] = handle_stfl,
592 [0xb2] = handle_lpswe,
595 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
597 intercept_handler_t handler;
600 * A lot of B2 instructions are priviledged. Here we check for
601 * the privileged ones, that we can handle in the kernel.
602 * Anything else goes to userspace.
604 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
606 return handler(vcpu);
611 static int handle_epsw(struct kvm_vcpu *vcpu)
615 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
617 /* This basically extracts the mask half of the psw. */
618 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
619 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
621 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
622 vcpu->run->s.regs.gprs[reg2] |=
623 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
628 #define PFMF_RESERVED 0xfffc0101UL
629 #define PFMF_SK 0x00020000UL
630 #define PFMF_CF 0x00010000UL
631 #define PFMF_UI 0x00008000UL
632 #define PFMF_FSC 0x00007000UL
633 #define PFMF_NQ 0x00000800UL
634 #define PFMF_MR 0x00000400UL
635 #define PFMF_MC 0x00000200UL
636 #define PFMF_KEY 0x000000feUL
638 static int handle_pfmf(struct kvm_vcpu *vcpu)
641 unsigned long start, end;
643 vcpu->stat.instruction_pfmf++;
645 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
647 if (!MACHINE_HAS_PFMF)
648 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
650 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
651 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
653 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
654 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
656 /* Only provide non-quiescing support if the host supports it */
657 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
658 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
660 /* No support for conditional-SSKE */
661 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
662 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
664 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
665 start = kvm_s390_logical_to_effective(vcpu, start);
667 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
669 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
672 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
674 /* We dont support EDAT2
676 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
679 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
682 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
683 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
684 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
687 while (start < end) {
688 unsigned long useraddr, abs_addr;
690 /* Translate guest address to host address */
691 if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
692 abs_addr = kvm_s390_real_to_abs(vcpu, start);
695 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
696 if (kvm_is_error_hva(useraddr))
697 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
699 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
700 if (clear_user((void __user *)useraddr, PAGE_SIZE))
701 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
704 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
705 int rc = __skey_check_enable(vcpu);
709 if (set_guest_storage_key(current->mm, useraddr,
710 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
711 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
712 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
717 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
718 vcpu->run->s.regs.gprs[reg2] = end;
722 static int handle_essa(struct kvm_vcpu *vcpu)
724 /* entries expected to be 1FF */
725 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
726 unsigned long *cbrlo, cbrle;
730 VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
731 gmap = vcpu->arch.gmap;
732 vcpu->stat.instruction_essa++;
733 if (!kvm_s390_cmma_enabled(vcpu->kvm))
734 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
736 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
737 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
739 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
740 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
742 /* Rewind PSW to repeat the ESSA instruction */
743 kvm_s390_rewind_psw(vcpu, 4);
744 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
745 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
746 down_read(&gmap->mm->mmap_sem);
747 for (i = 0; i < entries; ++i) {
749 if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
752 /* try to free backing */
753 __gmap_zap(gmap, cbrle);
755 up_read(&gmap->mm->mmap_sem);
757 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
761 static const intercept_handler_t b9_handlers[256] = {
762 [0x8a] = handle_ipte_interlock,
763 [0x8d] = handle_epsw,
764 [0x8e] = handle_ipte_interlock,
765 [0x8f] = handle_ipte_interlock,
766 [0xab] = handle_essa,
767 [0xaf] = handle_pfmf,
770 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
772 intercept_handler_t handler;
774 /* This is handled just as for the B2 instructions. */
775 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
777 return handler(vcpu);
782 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
784 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
785 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
786 int reg, rc, nr_regs;
790 vcpu->stat.instruction_lctl++;
792 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
793 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
795 ga = kvm_s390_get_base_disp_rs(vcpu);
798 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
800 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
801 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
803 nr_regs = ((reg3 - reg1) & 0xf) + 1;
804 rc = read_guest(vcpu, ga, ctl_array, nr_regs * sizeof(u32));
806 return kvm_s390_inject_prog_cond(vcpu, rc);
810 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
811 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
814 reg = (reg + 1) % 16;
816 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
820 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
822 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
823 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
824 int reg, rc, nr_regs;
828 vcpu->stat.instruction_stctl++;
830 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
831 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
833 ga = kvm_s390_get_base_disp_rs(vcpu);
836 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
838 VCPU_EVENT(vcpu, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
839 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
844 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
847 reg = (reg + 1) % 16;
849 rc = write_guest(vcpu, ga, ctl_array, nr_regs * sizeof(u32));
850 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
853 static int handle_lctlg(struct kvm_vcpu *vcpu)
855 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
856 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
857 int reg, rc, nr_regs;
861 vcpu->stat.instruction_lctlg++;
863 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
864 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
866 ga = kvm_s390_get_base_disp_rsy(vcpu);
869 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
871 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
872 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
874 nr_regs = ((reg3 - reg1) & 0xf) + 1;
875 rc = read_guest(vcpu, ga, ctl_array, nr_regs * sizeof(u64));
877 return kvm_s390_inject_prog_cond(vcpu, rc);
881 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
884 reg = (reg + 1) % 16;
886 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
890 static int handle_stctg(struct kvm_vcpu *vcpu)
892 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
893 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
894 int reg, rc, nr_regs;
898 vcpu->stat.instruction_stctg++;
900 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
901 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
903 ga = kvm_s390_get_base_disp_rsy(vcpu);
906 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
908 VCPU_EVENT(vcpu, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
909 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
914 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
917 reg = (reg + 1) % 16;
919 rc = write_guest(vcpu, ga, ctl_array, nr_regs * sizeof(u64));
920 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
923 static const intercept_handler_t eb_handlers[256] = {
924 [0x2f] = handle_lctlg,
925 [0x25] = handle_stctg,
928 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
930 intercept_handler_t handler;
932 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
934 return handler(vcpu);
938 static int handle_tprot(struct kvm_vcpu *vcpu)
940 u64 address1, address2;
941 unsigned long hva, gpa;
945 vcpu->stat.instruction_tprot++;
947 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
948 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
950 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
952 /* we only handle the Linux memory detection case:
954 * everything else goes to userspace. */
957 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
959 ret = guest_translate_address(vcpu, address1, &gpa, 1);
960 if (ret == PGM_PROTECTION) {
961 /* Write protected? Try again with read-only... */
963 ret = guest_translate_address(vcpu, address1, &gpa, 0);
966 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
967 ret = kvm_s390_inject_program_int(vcpu, ret);
968 } else if (ret > 0) {
969 /* Translation not available */
970 kvm_s390_set_psw_cc(vcpu, 3);
976 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
977 if (kvm_is_error_hva(hva)) {
978 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
981 cc = 1; /* Write not permitted ==> read-only */
982 kvm_s390_set_psw_cc(vcpu, cc);
983 /* Note: CC2 only occurs for storage keys (not supported yet) */
986 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
991 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
993 /* For e5xx... instructions we only handle TPROT */
994 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
995 return handle_tprot(vcpu);
999 static int handle_sckpf(struct kvm_vcpu *vcpu)
1003 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1004 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1006 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1007 return kvm_s390_inject_program_int(vcpu,
1010 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1011 vcpu->arch.sie_block->todpr = value;
1016 static const intercept_handler_t x01_handlers[256] = {
1017 [0x07] = handle_sckpf,
1020 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1022 intercept_handler_t handler;
1024 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1026 return handler(vcpu);