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)
139 vcpu->stat.instruction_stap++;
141 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
142 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
144 useraddr = kvm_s390_get_base_disp_s(vcpu);
147 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
149 if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr))
150 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
152 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
153 trace_kvm_s390_handle_stap(vcpu, useraddr);
157 static void __skey_check_enable(struct kvm_vcpu *vcpu)
159 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
163 trace_kvm_s390_skey_related_inst(vcpu);
164 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
168 static int handle_skey(struct kvm_vcpu *vcpu)
170 __skey_check_enable(vcpu);
172 vcpu->stat.instruction_storage_key++;
174 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
175 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
177 vcpu->arch.sie_block->gpsw.addr =
178 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
179 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
183 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
185 psw_t *psw = &vcpu->arch.sie_block->gpsw;
187 vcpu->stat.instruction_ipte_interlock++;
188 if (psw_bits(*psw).p)
189 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
190 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
191 psw->addr = __rewind_psw(*psw, 4);
192 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
196 static int handle_test_block(struct kvm_vcpu *vcpu)
202 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
203 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
205 kvm_s390_get_regs_rre(vcpu, NULL, ®2);
206 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
207 addr = kvm_s390_real_to_abs(vcpu, addr);
209 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
210 if (kvm_is_error_hva(hva))
211 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
213 * We don't expect errors on modern systems, and do not care
214 * about storage keys (yet), so let's just clear the page.
216 if (clear_user((void __user *)hva, PAGE_SIZE) != 0)
218 kvm_s390_set_psw_cc(vcpu, 0);
219 vcpu->run->s.regs.gprs[0] = 0;
223 static int handle_tpi(struct kvm_vcpu *vcpu)
225 struct kvm_s390_interrupt_info *inti;
229 addr = kvm_s390_get_base_disp_s(vcpu);
231 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
233 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
239 * Store the two-word I/O interruption code into the
242 if (put_guest(vcpu, inti->io.subchannel_id, (u16 __user *)addr)
243 || put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *)(addr + 2))
244 || put_guest(vcpu, inti->io.io_int_parm, (u32 __user *)(addr + 4)))
245 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
248 * Store the three-word I/O interruption code into
249 * the appropriate lowcore area.
251 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
252 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
253 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
254 put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
258 /* Set condition code and we're done. */
259 kvm_s390_set_psw_cc(vcpu, cc);
263 static int handle_tsch(struct kvm_vcpu *vcpu)
265 struct kvm_s390_interrupt_info *inti;
267 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
268 vcpu->run->s.regs.gprs[1]);
271 * Prepare exit to userspace.
272 * We indicate whether we dequeued a pending I/O interrupt
273 * so that userspace can re-inject it if the instruction gets
274 * a program check. While this may re-order the pending I/O
275 * interrupts, this is no problem since the priority is kept
278 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
279 vcpu->run->s390_tsch.dequeued = !!inti;
281 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
282 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
283 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
284 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
286 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
291 static int handle_io_inst(struct kvm_vcpu *vcpu)
293 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
295 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
296 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
298 if (vcpu->kvm->arch.css_support) {
300 * Most I/O instructions will be handled by userspace.
301 * Exceptions are tpi and the interrupt portion of tsch.
303 if (vcpu->arch.sie_block->ipa == 0xb236)
304 return handle_tpi(vcpu);
305 if (vcpu->arch.sie_block->ipa == 0xb235)
306 return handle_tsch(vcpu);
307 /* Handle in userspace. */
311 * Set condition code 3 to stop the guest from issuing channel
314 kvm_s390_set_psw_cc(vcpu, 3);
319 static int handle_stfl(struct kvm_vcpu *vcpu)
323 vcpu->stat.instruction_stfl++;
325 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
326 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
328 rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
332 VCPU_EVENT(vcpu, 5, "store facility list value %x",
333 *(unsigned int *) vfacilities);
334 trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities);
338 static void handle_new_psw(struct kvm_vcpu *vcpu)
340 /* Check whether the new psw is enabled for machine checks. */
341 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
342 kvm_s390_deliver_pending_machine_checks(vcpu);
345 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
346 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
347 #define PSW_ADDR_24 0x0000000000ffffffUL
348 #define PSW_ADDR_31 0x000000007fffffffUL
350 static int is_valid_psw(psw_t *psw) {
351 if (psw->mask & PSW_MASK_UNASSIGNED)
353 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
354 if (psw->addr & ~PSW_ADDR_31)
357 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
359 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
364 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
366 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
367 psw_compat_t new_psw;
370 if (gpsw->mask & PSW_MASK_PSTATE)
371 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
373 addr = kvm_s390_get_base_disp_s(vcpu);
375 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
376 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
377 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
378 if (!(new_psw.mask & PSW32_MASK_BASE))
379 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
380 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
381 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
382 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
383 if (!is_valid_psw(gpsw))
384 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
385 handle_new_psw(vcpu);
389 static int handle_lpswe(struct kvm_vcpu *vcpu)
394 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
395 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
397 addr = kvm_s390_get_base_disp_s(vcpu);
399 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
400 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
401 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
402 vcpu->arch.sie_block->gpsw = new_psw;
403 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
404 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
405 handle_new_psw(vcpu);
409 static int handle_stidp(struct kvm_vcpu *vcpu)
413 vcpu->stat.instruction_stidp++;
415 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
416 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
418 operand2 = kvm_s390_get_base_disp_s(vcpu);
421 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
423 if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
424 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
426 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
430 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
435 cpus = atomic_read(&vcpu->kvm->online_vcpus);
437 /* deal with other level 3 hypervisors */
438 if (stsi(mem, 3, 2, 2))
442 for (n = mem->count - 1; n > 0 ; n--)
443 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
445 mem->vm[0].cpus_total = cpus;
446 mem->vm[0].cpus_configured = cpus;
447 mem->vm[0].cpus_standby = 0;
448 mem->vm[0].cpus_reserved = 0;
449 mem->vm[0].caf = 1000;
450 memcpy(mem->vm[0].name, "KVMguest", 8);
451 ASCEBC(mem->vm[0].name, 8);
452 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
453 ASCEBC(mem->vm[0].cpi, 16);
456 static int handle_stsi(struct kvm_vcpu *vcpu)
458 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
459 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
460 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
461 unsigned long mem = 0;
465 vcpu->stat.instruction_stsi++;
466 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
468 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
469 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
472 kvm_s390_set_psw_cc(vcpu, 3);
476 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
477 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
478 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
481 vcpu->run->s.regs.gprs[0] = 3 << 28;
482 kvm_s390_set_psw_cc(vcpu, 0);
486 operand2 = kvm_s390_get_base_disp_s(vcpu);
488 if (operand2 & 0xfff)
489 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
492 case 1: /* same handling for 1 and 2 */
494 mem = get_zeroed_page(GFP_KERNEL);
497 if (stsi((void *) mem, fc, sel1, sel2))
501 if (sel1 != 2 || sel2 != 2)
503 mem = get_zeroed_page(GFP_KERNEL);
506 handle_stsi_3_2_2(vcpu, (void *) mem);
510 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
511 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
514 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
516 kvm_s390_set_psw_cc(vcpu, 0);
517 vcpu->run->s.regs.gprs[0] = 0;
520 kvm_s390_set_psw_cc(vcpu, 3);
526 static const intercept_handler_t b2_handlers[256] = {
527 [0x02] = handle_stidp,
528 [0x04] = handle_set_clock,
529 [0x10] = handle_set_prefix,
530 [0x11] = handle_store_prefix,
531 [0x12] = handle_store_cpu_address,
532 [0x21] = handle_ipte_interlock,
533 [0x29] = handle_skey,
534 [0x2a] = handle_skey,
535 [0x2b] = handle_skey,
536 [0x2c] = handle_test_block,
537 [0x30] = handle_io_inst,
538 [0x31] = handle_io_inst,
539 [0x32] = handle_io_inst,
540 [0x33] = handle_io_inst,
541 [0x34] = handle_io_inst,
542 [0x35] = handle_io_inst,
543 [0x36] = handle_io_inst,
544 [0x37] = handle_io_inst,
545 [0x38] = handle_io_inst,
546 [0x39] = handle_io_inst,
547 [0x3a] = handle_io_inst,
548 [0x3b] = handle_io_inst,
549 [0x3c] = handle_io_inst,
550 [0x50] = handle_ipte_interlock,
551 [0x5f] = handle_io_inst,
552 [0x74] = handle_io_inst,
553 [0x76] = handle_io_inst,
554 [0x7d] = handle_stsi,
555 [0xb1] = handle_stfl,
556 [0xb2] = handle_lpswe,
559 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
561 intercept_handler_t handler;
564 * A lot of B2 instructions are priviledged. Here we check for
565 * the privileged ones, that we can handle in the kernel.
566 * Anything else goes to userspace.
568 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
570 return handler(vcpu);
575 static int handle_epsw(struct kvm_vcpu *vcpu)
579 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
581 /* This basically extracts the mask half of the psw. */
582 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
583 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
585 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
586 vcpu->run->s.regs.gprs[reg2] |=
587 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
592 #define PFMF_RESERVED 0xfffc0101UL
593 #define PFMF_SK 0x00020000UL
594 #define PFMF_CF 0x00010000UL
595 #define PFMF_UI 0x00008000UL
596 #define PFMF_FSC 0x00007000UL
597 #define PFMF_NQ 0x00000800UL
598 #define PFMF_MR 0x00000400UL
599 #define PFMF_MC 0x00000200UL
600 #define PFMF_KEY 0x000000feUL
602 static int handle_pfmf(struct kvm_vcpu *vcpu)
605 unsigned long start, end;
607 vcpu->stat.instruction_pfmf++;
609 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
611 if (!MACHINE_HAS_PFMF)
612 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
614 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
615 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
617 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
618 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
620 /* Only provide non-quiescing support if the host supports it */
621 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
622 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
624 /* No support for conditional-SSKE */
625 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
626 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
628 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
629 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
631 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
634 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
636 /* We dont support EDAT2
638 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
641 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
643 while (start < end) {
644 unsigned long useraddr;
646 useraddr = gmap_translate(start, vcpu->arch.gmap);
647 if (IS_ERR((void *)useraddr))
648 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
650 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
651 if (clear_user((void __user *)useraddr, PAGE_SIZE))
652 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
655 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
656 __skey_check_enable(vcpu);
657 if (set_guest_storage_key(current->mm, useraddr,
658 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
659 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
660 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
665 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
666 vcpu->run->s.regs.gprs[reg2] = end;
670 static int handle_essa(struct kvm_vcpu *vcpu)
672 /* entries expected to be 1FF */
673 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
674 unsigned long *cbrlo, cbrle;
678 VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
679 gmap = vcpu->arch.gmap;
680 vcpu->stat.instruction_essa++;
681 if (!kvm_s390_cmma_enabled(vcpu->kvm))
682 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
684 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
685 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
687 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
688 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
690 /* Rewind PSW to repeat the ESSA instruction */
691 vcpu->arch.sie_block->gpsw.addr =
692 __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
693 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
694 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
695 down_read(&gmap->mm->mmap_sem);
696 for (i = 0; i < entries; ++i) {
698 if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
701 /* try to free backing */
702 __gmap_zap(cbrle, gmap);
704 up_read(&gmap->mm->mmap_sem);
706 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
710 static const intercept_handler_t b9_handlers[256] = {
711 [0x8a] = handle_ipte_interlock,
712 [0x8d] = handle_epsw,
713 [0x8e] = handle_ipte_interlock,
714 [0x8f] = handle_ipte_interlock,
715 [0xab] = handle_essa,
716 [0xaf] = handle_pfmf,
719 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
721 intercept_handler_t handler;
723 /* This is handled just as for the B2 instructions. */
724 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
726 return handler(vcpu);
731 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
733 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
734 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
739 vcpu->stat.instruction_lctl++;
741 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
742 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
744 useraddr = kvm_s390_get_base_disp_rs(vcpu);
747 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
749 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
751 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
755 rc = get_guest(vcpu, val, (u32 __user *) useraddr);
757 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
758 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
759 vcpu->arch.sie_block->gcr[reg] |= val;
763 reg = (reg + 1) % 16;
769 static int handle_lctlg(struct kvm_vcpu *vcpu)
771 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
772 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
776 vcpu->stat.instruction_lctlg++;
778 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
779 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
781 useraddr = kvm_s390_get_base_disp_rsy(vcpu);
784 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
788 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
790 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
793 rc = get_guest(vcpu, vcpu->arch.sie_block->gcr[reg],
794 (u64 __user *) useraddr);
796 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
800 reg = (reg + 1) % 16;
806 static const intercept_handler_t eb_handlers[256] = {
807 [0x2f] = handle_lctlg,
810 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
812 intercept_handler_t handler;
814 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
816 return handler(vcpu);
820 static int handle_tprot(struct kvm_vcpu *vcpu)
822 u64 address1, address2;
823 struct vm_area_struct *vma;
824 unsigned long user_address;
826 vcpu->stat.instruction_tprot++;
828 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
829 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
831 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
833 /* we only handle the Linux memory detection case:
836 * everything else goes to userspace. */
839 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
842 down_read(¤t->mm->mmap_sem);
843 user_address = __gmap_translate(address1, vcpu->arch.gmap);
844 if (IS_ERR_VALUE(user_address))
846 vma = find_vma(current->mm, user_address);
849 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
850 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
851 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
852 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
853 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
855 up_read(¤t->mm->mmap_sem);
859 up_read(¤t->mm->mmap_sem);
860 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
863 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
865 /* For e5xx... instructions we only handle TPROT */
866 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
867 return handle_tprot(vcpu);
871 static int handle_sckpf(struct kvm_vcpu *vcpu)
875 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
876 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
878 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
879 return kvm_s390_inject_program_int(vcpu,
882 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
883 vcpu->arch.sie_block->todpr = value;
888 static const intercept_handler_t x01_handlers[256] = {
889 [0x07] = handle_sckpf,
892 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
894 intercept_handler_t handler;
896 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
898 return handler(vcpu);