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 = vcpu->arch.sie_block->prefix;
123 address = address & 0x7fffe000u;
126 rc = write_guest(vcpu, operand2, &address, sizeof(address));
128 return kvm_s390_inject_prog_cond(vcpu, rc);
130 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
131 trace_kvm_s390_handle_prefix(vcpu, 0, address);
135 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
137 u16 vcpu_id = vcpu->vcpu_id;
141 vcpu->stat.instruction_stap++;
143 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
144 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
146 ga = kvm_s390_get_base_disp_s(vcpu);
149 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
151 rc = write_guest(vcpu, ga, &vcpu_id, sizeof(vcpu_id));
153 return kvm_s390_inject_prog_cond(vcpu, rc);
155 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", ga);
156 trace_kvm_s390_handle_stap(vcpu, ga);
160 static void __skey_check_enable(struct kvm_vcpu *vcpu)
162 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
166 trace_kvm_s390_skey_related_inst(vcpu);
167 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
171 static int handle_skey(struct kvm_vcpu *vcpu)
173 __skey_check_enable(vcpu);
175 vcpu->stat.instruction_storage_key++;
177 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
178 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
180 vcpu->arch.sie_block->gpsw.addr =
181 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
182 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
186 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
188 psw_t *psw = &vcpu->arch.sie_block->gpsw;
190 vcpu->stat.instruction_ipte_interlock++;
191 if (psw_bits(*psw).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 psw->addr = __rewind_psw(*psw, 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_real_to_abs(vcpu, addr);
211 if (kvm_is_error_gpa(vcpu->kvm, addr))
212 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
214 * We don't expect errors on modern systems, and do not care
215 * about storage keys (yet), so let's just clear the page.
217 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
219 kvm_s390_set_psw_cc(vcpu, 0);
220 vcpu->run->s.regs.gprs[0] = 0;
224 static int handle_tpi(struct kvm_vcpu *vcpu)
226 struct kvm_s390_interrupt_info *inti;
233 addr = kvm_s390_get_base_disp_s(vcpu);
235 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
237 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
241 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
242 tpi_data[1] = inti->io.io_int_parm;
243 tpi_data[2] = inti->io.io_int_word;
246 * Store the two-word I/O interruption code into the
249 len = sizeof(tpi_data) - 4;
250 rc = write_guest(vcpu, addr, &tpi_data, len);
252 return kvm_s390_inject_prog_cond(vcpu, rc);
255 * Store the three-word I/O interruption code into
256 * the appropriate lowcore area.
258 len = sizeof(tpi_data);
259 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len))
263 * If we encounter a problem storing the interruption code, the
264 * instruction is suppressed from the guest's view: reinject the
270 kvm_s390_reinject_io_int(vcpu->kvm, inti);
272 /* Set condition code and we're done. */
274 kvm_s390_set_psw_cc(vcpu, cc);
275 return rc ? -EFAULT : 0;
278 static int handle_tsch(struct kvm_vcpu *vcpu)
280 struct kvm_s390_interrupt_info *inti;
282 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
283 vcpu->run->s.regs.gprs[1]);
286 * Prepare exit to userspace.
287 * We indicate whether we dequeued a pending I/O interrupt
288 * so that userspace can re-inject it if the instruction gets
289 * a program check. While this may re-order the pending I/O
290 * interrupts, this is no problem since the priority is kept
293 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
294 vcpu->run->s390_tsch.dequeued = !!inti;
296 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
297 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
298 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
299 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
301 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
306 static int handle_io_inst(struct kvm_vcpu *vcpu)
308 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
310 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
311 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
313 if (vcpu->kvm->arch.css_support) {
315 * Most I/O instructions will be handled by userspace.
316 * Exceptions are tpi and the interrupt portion of tsch.
318 if (vcpu->arch.sie_block->ipa == 0xb236)
319 return handle_tpi(vcpu);
320 if (vcpu->arch.sie_block->ipa == 0xb235)
321 return handle_tsch(vcpu);
322 /* Handle in userspace. */
326 * Set condition code 3 to stop the guest from issuing channel
329 kvm_s390_set_psw_cc(vcpu, 3);
334 static int handle_stfl(struct kvm_vcpu *vcpu)
338 vcpu->stat.instruction_stfl++;
340 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
341 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
343 rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
347 VCPU_EVENT(vcpu, 5, "store facility list value %x",
348 *(unsigned int *) vfacilities);
349 trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities);
353 static void handle_new_psw(struct kvm_vcpu *vcpu)
355 /* Check whether the new psw is enabled for machine checks. */
356 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
357 kvm_s390_deliver_pending_machine_checks(vcpu);
360 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
361 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
362 #define PSW_ADDR_24 0x0000000000ffffffUL
363 #define PSW_ADDR_31 0x000000007fffffffUL
365 static int is_valid_psw(psw_t *psw) {
366 if (psw->mask & PSW_MASK_UNASSIGNED)
368 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
369 if (psw->addr & ~PSW_ADDR_31)
372 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
374 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
379 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
381 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
382 psw_compat_t new_psw;
386 if (gpsw->mask & PSW_MASK_PSTATE)
387 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
389 addr = kvm_s390_get_base_disp_s(vcpu);
391 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
393 rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
395 return kvm_s390_inject_prog_cond(vcpu, rc);
396 if (!(new_psw.mask & PSW32_MASK_BASE))
397 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
398 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
399 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
400 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
401 if (!is_valid_psw(gpsw))
402 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
403 handle_new_psw(vcpu);
407 static int handle_lpswe(struct kvm_vcpu *vcpu)
413 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
414 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
416 addr = kvm_s390_get_base_disp_s(vcpu);
418 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
419 rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
421 return kvm_s390_inject_prog_cond(vcpu, rc);
422 vcpu->arch.sie_block->gpsw = new_psw;
423 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
424 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
425 handle_new_psw(vcpu);
429 static int handle_stidp(struct kvm_vcpu *vcpu)
431 u64 stidp_data = vcpu->arch.stidp_data;
435 vcpu->stat.instruction_stidp++;
437 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
438 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
440 operand2 = kvm_s390_get_base_disp_s(vcpu);
443 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
445 rc = write_guest(vcpu, operand2, &stidp_data, sizeof(stidp_data));
447 return kvm_s390_inject_prog_cond(vcpu, rc);
449 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
453 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
458 cpus = atomic_read(&vcpu->kvm->online_vcpus);
460 /* deal with other level 3 hypervisors */
461 if (stsi(mem, 3, 2, 2))
465 for (n = mem->count - 1; n > 0 ; n--)
466 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
468 mem->vm[0].cpus_total = cpus;
469 mem->vm[0].cpus_configured = cpus;
470 mem->vm[0].cpus_standby = 0;
471 mem->vm[0].cpus_reserved = 0;
472 mem->vm[0].caf = 1000;
473 memcpy(mem->vm[0].name, "KVMguest", 8);
474 ASCEBC(mem->vm[0].name, 8);
475 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
476 ASCEBC(mem->vm[0].cpi, 16);
479 static int handle_stsi(struct kvm_vcpu *vcpu)
481 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
482 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
483 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
484 unsigned long mem = 0;
488 vcpu->stat.instruction_stsi++;
489 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
491 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
492 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
495 kvm_s390_set_psw_cc(vcpu, 3);
499 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
500 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
501 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
504 vcpu->run->s.regs.gprs[0] = 3 << 28;
505 kvm_s390_set_psw_cc(vcpu, 0);
509 operand2 = kvm_s390_get_base_disp_s(vcpu);
511 if (operand2 & 0xfff)
512 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
515 case 1: /* same handling for 1 and 2 */
517 mem = get_zeroed_page(GFP_KERNEL);
520 if (stsi((void *) mem, fc, sel1, sel2))
524 if (sel1 != 2 || sel2 != 2)
526 mem = get_zeroed_page(GFP_KERNEL);
529 handle_stsi_3_2_2(vcpu, (void *) mem);
533 rc = write_guest(vcpu, operand2, (void *)mem, PAGE_SIZE);
535 rc = kvm_s390_inject_prog_cond(vcpu, rc);
538 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
540 kvm_s390_set_psw_cc(vcpu, 0);
541 vcpu->run->s.regs.gprs[0] = 0;
544 kvm_s390_set_psw_cc(vcpu, 3);
550 static const intercept_handler_t b2_handlers[256] = {
551 [0x02] = handle_stidp,
552 [0x04] = handle_set_clock,
553 [0x10] = handle_set_prefix,
554 [0x11] = handle_store_prefix,
555 [0x12] = handle_store_cpu_address,
556 [0x21] = handle_ipte_interlock,
557 [0x29] = handle_skey,
558 [0x2a] = handle_skey,
559 [0x2b] = handle_skey,
560 [0x2c] = handle_test_block,
561 [0x30] = handle_io_inst,
562 [0x31] = handle_io_inst,
563 [0x32] = handle_io_inst,
564 [0x33] = handle_io_inst,
565 [0x34] = handle_io_inst,
566 [0x35] = handle_io_inst,
567 [0x36] = handle_io_inst,
568 [0x37] = handle_io_inst,
569 [0x38] = handle_io_inst,
570 [0x39] = handle_io_inst,
571 [0x3a] = handle_io_inst,
572 [0x3b] = handle_io_inst,
573 [0x3c] = handle_io_inst,
574 [0x50] = handle_ipte_interlock,
575 [0x5f] = handle_io_inst,
576 [0x74] = handle_io_inst,
577 [0x76] = handle_io_inst,
578 [0x7d] = handle_stsi,
579 [0xb1] = handle_stfl,
580 [0xb2] = handle_lpswe,
583 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
585 intercept_handler_t handler;
588 * A lot of B2 instructions are priviledged. Here we check for
589 * the privileged ones, that we can handle in the kernel.
590 * Anything else goes to userspace.
592 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
594 return handler(vcpu);
599 static int handle_epsw(struct kvm_vcpu *vcpu)
603 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
605 /* This basically extracts the mask half of the psw. */
606 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
607 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
609 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
610 vcpu->run->s.regs.gprs[reg2] |=
611 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
616 #define PFMF_RESERVED 0xfffc0101UL
617 #define PFMF_SK 0x00020000UL
618 #define PFMF_CF 0x00010000UL
619 #define PFMF_UI 0x00008000UL
620 #define PFMF_FSC 0x00007000UL
621 #define PFMF_NQ 0x00000800UL
622 #define PFMF_MR 0x00000400UL
623 #define PFMF_MC 0x00000200UL
624 #define PFMF_KEY 0x000000feUL
626 static int handle_pfmf(struct kvm_vcpu *vcpu)
629 unsigned long start, end;
631 vcpu->stat.instruction_pfmf++;
633 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
635 if (!MACHINE_HAS_PFMF)
636 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
638 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
639 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
641 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
642 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
644 /* Only provide non-quiescing support if the host supports it */
645 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
646 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
648 /* No support for conditional-SSKE */
649 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
650 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
652 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
653 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
654 if (kvm_s390_check_low_addr_protection(vcpu, start))
655 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
658 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
660 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
663 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
665 /* We dont support EDAT2
667 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
670 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
672 while (start < end) {
673 unsigned long useraddr, abs_addr;
675 /* Translate guest address to host address */
676 if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
677 abs_addr = kvm_s390_real_to_abs(vcpu, start);
680 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
681 if (kvm_is_error_hva(useraddr))
682 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
684 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
685 if (clear_user((void __user *)useraddr, PAGE_SIZE))
686 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
689 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
690 __skey_check_enable(vcpu);
691 if (set_guest_storage_key(current->mm, useraddr,
692 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
693 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
694 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
699 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
700 vcpu->run->s.regs.gprs[reg2] = end;
704 static int handle_essa(struct kvm_vcpu *vcpu)
706 /* entries expected to be 1FF */
707 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
708 unsigned long *cbrlo, cbrle;
712 VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
713 gmap = vcpu->arch.gmap;
714 vcpu->stat.instruction_essa++;
715 if (!kvm_s390_cmma_enabled(vcpu->kvm))
716 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
718 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
719 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
721 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
722 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
724 /* Rewind PSW to repeat the ESSA instruction */
725 vcpu->arch.sie_block->gpsw.addr =
726 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
727 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
728 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
729 down_read(&gmap->mm->mmap_sem);
730 for (i = 0; i < entries; ++i) {
732 if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
735 /* try to free backing */
736 __gmap_zap(cbrle, gmap);
738 up_read(&gmap->mm->mmap_sem);
740 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
744 static const intercept_handler_t b9_handlers[256] = {
745 [0x8a] = handle_ipte_interlock,
746 [0x8d] = handle_epsw,
747 [0x8e] = handle_ipte_interlock,
748 [0x8f] = handle_ipte_interlock,
749 [0xab] = handle_essa,
750 [0xaf] = handle_pfmf,
753 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
755 intercept_handler_t handler;
757 /* This is handled just as for the B2 instructions. */
758 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
760 return handler(vcpu);
765 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
767 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
768 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
773 vcpu->stat.instruction_lctl++;
775 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
776 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
778 ga = kvm_s390_get_base_disp_rs(vcpu);
781 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
783 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
784 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
788 rc = read_guest(vcpu, ga, &val, sizeof(val));
790 return kvm_s390_inject_prog_cond(vcpu, rc);
791 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
792 vcpu->arch.sie_block->gcr[reg] |= val;
796 reg = (reg + 1) % 16;
802 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
804 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
805 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
810 vcpu->stat.instruction_stctl++;
812 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
813 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
815 ga = kvm_s390_get_base_disp_rs(vcpu);
818 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
820 VCPU_EVENT(vcpu, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
821 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
825 val = vcpu->arch.sie_block->gcr[reg] & 0x00000000fffffffful;
826 rc = write_guest(vcpu, ga, &val, sizeof(val));
828 return kvm_s390_inject_prog_cond(vcpu, rc);
832 reg = (reg + 1) % 16;
838 static int handle_lctlg(struct kvm_vcpu *vcpu)
840 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
841 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
845 vcpu->stat.instruction_lctlg++;
847 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
848 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
850 ga = kvm_s390_get_base_disp_rsy(vcpu);
853 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
857 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
858 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
861 rc = read_guest(vcpu, ga, &val, sizeof(val));
863 return kvm_s390_inject_prog_cond(vcpu, rc);
864 vcpu->arch.sie_block->gcr[reg] = val;
868 reg = (reg + 1) % 16;
874 static int handle_stctg(struct kvm_vcpu *vcpu)
876 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
877 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
881 vcpu->stat.instruction_stctg++;
883 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
884 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
886 ga = kvm_s390_get_base_disp_rsy(vcpu);
889 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
893 VCPU_EVENT(vcpu, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
894 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
897 val = vcpu->arch.sie_block->gcr[reg];
898 rc = write_guest(vcpu, ga, &val, sizeof(val));
900 return kvm_s390_inject_prog_cond(vcpu, rc);
904 reg = (reg + 1) % 16;
910 static const intercept_handler_t eb_handlers[256] = {
911 [0x2f] = handle_lctlg,
912 [0x25] = handle_stctg,
915 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
917 intercept_handler_t handler;
919 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
921 return handler(vcpu);
925 static int handle_tprot(struct kvm_vcpu *vcpu)
927 u64 address1, address2;
928 struct vm_area_struct *vma;
929 unsigned long user_address;
931 vcpu->stat.instruction_tprot++;
933 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
934 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
936 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
938 /* we only handle the Linux memory detection case:
941 * everything else goes to userspace. */
944 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
947 down_read(¤t->mm->mmap_sem);
948 user_address = __gmap_translate(address1, vcpu->arch.gmap);
949 if (IS_ERR_VALUE(user_address))
951 vma = find_vma(current->mm, user_address);
954 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
955 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
956 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
957 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
958 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
960 up_read(¤t->mm->mmap_sem);
964 up_read(¤t->mm->mmap_sem);
965 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
968 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
970 /* For e5xx... instructions we only handle TPROT */
971 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
972 return handle_tprot(vcpu);
976 static int handle_sckpf(struct kvm_vcpu *vcpu)
980 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
981 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
983 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
984 return kvm_s390_inject_program_int(vcpu,
987 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
988 vcpu->arch.sie_block->todpr = value;
993 static const intercept_handler_t x01_handlers[256] = {
994 [0x07] = handle_sckpf,
997 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
999 intercept_handler_t handler;
1001 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1003 return handler(vcpu);