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;
42 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
43 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
45 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
46 if (op2 & 7) /* Operand must be on a doubleword boundary */
47 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
48 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
50 return kvm_s390_inject_prog_cond(vcpu, rc);
52 if (store_tod_clock(&hostclk)) {
53 kvm_s390_set_psw_cc(vcpu, 3);
56 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
57 val = (val - hostclk) & ~0x3fUL;
59 mutex_lock(&vcpu->kvm->lock);
61 kvm_for_each_vcpu(i, cpup, vcpu->kvm)
62 cpup->arch.sie_block->epoch = val;
64 mutex_unlock(&vcpu->kvm->lock);
66 kvm_s390_set_psw_cc(vcpu, 0);
70 static int handle_set_prefix(struct kvm_vcpu *vcpu)
77 vcpu->stat.instruction_spx++;
79 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
80 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
82 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
84 /* must be word boundary */
86 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
89 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
91 return kvm_s390_inject_prog_cond(vcpu, rc);
93 address &= 0x7fffe000u;
96 * Make sure the new value is valid memory. We only need to check the
97 * first page, since address is 8k aligned and memory pieces are always
98 * at least 1MB aligned and have at least a size of 1MB.
100 if (kvm_is_error_gpa(vcpu->kvm, address))
101 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
103 kvm_s390_set_prefix(vcpu, address);
104 trace_kvm_s390_handle_prefix(vcpu, 1, address);
108 static int handle_store_prefix(struct kvm_vcpu *vcpu)
115 vcpu->stat.instruction_stpx++;
117 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
118 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
120 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
122 /* must be word boundary */
124 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
126 address = kvm_s390_get_prefix(vcpu);
129 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
131 return kvm_s390_inject_prog_cond(vcpu, rc);
133 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
134 trace_kvm_s390_handle_prefix(vcpu, 0, address);
138 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
140 u16 vcpu_id = vcpu->vcpu_id;
145 vcpu->stat.instruction_stap++;
147 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
148 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
150 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
153 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
155 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
157 return kvm_s390_inject_prog_cond(vcpu, rc);
159 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
160 trace_kvm_s390_handle_stap(vcpu, ga);
164 static int __skey_check_enable(struct kvm_vcpu *vcpu)
167 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
170 rc = s390_enable_skey();
171 VCPU_EVENT(vcpu, 3, "%s", "enabling storage keys for guest");
172 trace_kvm_s390_skey_related_inst(vcpu);
173 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
178 static int handle_skey(struct kvm_vcpu *vcpu)
180 int rc = __skey_check_enable(vcpu);
184 vcpu->stat.instruction_storage_key++;
186 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
187 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
189 kvm_s390_rewind_psw(vcpu, 4);
190 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
194 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
196 vcpu->stat.instruction_ipte_interlock++;
197 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
198 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
199 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
200 kvm_s390_rewind_psw(vcpu, 4);
201 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
205 static int handle_test_block(struct kvm_vcpu *vcpu)
210 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
211 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
213 kvm_s390_get_regs_rre(vcpu, NULL, ®2);
214 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
215 addr = kvm_s390_logical_to_effective(vcpu, addr);
216 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
217 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
218 addr = kvm_s390_real_to_abs(vcpu, addr);
220 if (kvm_is_error_gpa(vcpu->kvm, addr))
221 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
223 * We don't expect errors on modern systems, and do not care
224 * about storage keys (yet), so let's just clear the page.
226 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
228 kvm_s390_set_psw_cc(vcpu, 0);
229 vcpu->run->s.regs.gprs[0] = 0;
233 static int handle_tpi(struct kvm_vcpu *vcpu)
235 struct kvm_s390_interrupt_info *inti;
242 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
244 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
246 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
248 kvm_s390_set_psw_cc(vcpu, 0);
252 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
253 tpi_data[1] = inti->io.io_int_parm;
254 tpi_data[2] = inti->io.io_int_word;
257 * Store the two-word I/O interruption code into the
260 len = sizeof(tpi_data) - 4;
261 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
263 rc = kvm_s390_inject_prog_cond(vcpu, rc);
264 goto reinject_interrupt;
268 * Store the three-word I/O interruption code into
269 * the appropriate lowcore area.
271 len = sizeof(tpi_data);
272 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
273 /* failed writes to the low core are not recoverable */
275 goto reinject_interrupt;
279 /* irq was successfully handed to the guest */
281 kvm_s390_set_psw_cc(vcpu, 1);
285 * If we encounter a problem storing the interruption code, the
286 * instruction is suppressed from the guest's view: reinject the
289 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
293 /* don't set the cc, a pgm irq was injected or we drop to user space */
294 return rc ? -EFAULT : 0;
297 static int handle_tsch(struct kvm_vcpu *vcpu)
299 struct kvm_s390_interrupt_info *inti = NULL;
300 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
302 /* a valid schid has at least one bit set */
303 if (vcpu->run->s.regs.gprs[1])
304 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
305 vcpu->run->s.regs.gprs[1]);
308 * Prepare exit to userspace.
309 * We indicate whether we dequeued a pending I/O interrupt
310 * so that userspace can re-inject it if the instruction gets
311 * a program check. While this may re-order the pending I/O
312 * interrupts, this is no problem since the priority is kept
315 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
316 vcpu->run->s390_tsch.dequeued = !!inti;
318 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
319 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
320 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
321 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
323 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
328 static int handle_io_inst(struct kvm_vcpu *vcpu)
330 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
332 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
333 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
335 if (vcpu->kvm->arch.css_support) {
337 * Most I/O instructions will be handled by userspace.
338 * Exceptions are tpi and the interrupt portion of tsch.
340 if (vcpu->arch.sie_block->ipa == 0xb236)
341 return handle_tpi(vcpu);
342 if (vcpu->arch.sie_block->ipa == 0xb235)
343 return handle_tsch(vcpu);
344 /* Handle in userspace. */
348 * Set condition code 3 to stop the guest from issuing channel
351 kvm_s390_set_psw_cc(vcpu, 3);
356 static int handle_stfl(struct kvm_vcpu *vcpu)
361 vcpu->stat.instruction_stfl++;
363 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
364 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
367 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
368 * into a u32 memory representation. They will remain bits 0-31.
370 fac = *vcpu->kvm->arch.model.fac->list >> 32;
371 rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
375 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
376 trace_kvm_s390_handle_stfl(vcpu, fac);
380 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
381 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
382 #define PSW_ADDR_24 0x0000000000ffffffUL
383 #define PSW_ADDR_31 0x000000007fffffffUL
385 int is_valid_psw(psw_t *psw)
387 if (psw->mask & PSW_MASK_UNASSIGNED)
389 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
390 if (psw->addr & ~PSW_ADDR_31)
393 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
395 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
402 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
404 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
405 psw_compat_t new_psw;
410 if (gpsw->mask & PSW_MASK_PSTATE)
411 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
413 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
415 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
417 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
419 return kvm_s390_inject_prog_cond(vcpu, rc);
420 if (!(new_psw.mask & PSW32_MASK_BASE))
421 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
422 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
423 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
424 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
425 if (!is_valid_psw(gpsw))
426 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
430 static int handle_lpswe(struct kvm_vcpu *vcpu)
437 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
438 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
440 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
442 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
443 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
445 return kvm_s390_inject_prog_cond(vcpu, rc);
446 vcpu->arch.sie_block->gpsw = new_psw;
447 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
448 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
452 static int handle_stidp(struct kvm_vcpu *vcpu)
454 u64 stidp_data = vcpu->arch.stidp_data;
459 vcpu->stat.instruction_stidp++;
461 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
462 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
464 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
467 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
469 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
471 return kvm_s390_inject_prog_cond(vcpu, rc);
473 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
477 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
482 cpus = atomic_read(&vcpu->kvm->online_vcpus);
484 /* deal with other level 3 hypervisors */
485 if (stsi(mem, 3, 2, 2))
489 for (n = mem->count - 1; n > 0 ; n--)
490 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
492 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
493 mem->vm[0].cpus_total = cpus;
494 mem->vm[0].cpus_configured = cpus;
495 mem->vm[0].cpus_standby = 0;
496 mem->vm[0].cpus_reserved = 0;
497 mem->vm[0].caf = 1000;
498 memcpy(mem->vm[0].name, "KVMguest", 8);
499 ASCEBC(mem->vm[0].name, 8);
500 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
501 ASCEBC(mem->vm[0].cpi, 16);
504 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, ar_t ar,
505 u8 fc, u8 sel1, u16 sel2)
507 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
508 vcpu->run->s390_stsi.addr = addr;
509 vcpu->run->s390_stsi.ar = ar;
510 vcpu->run->s390_stsi.fc = fc;
511 vcpu->run->s390_stsi.sel1 = sel1;
512 vcpu->run->s390_stsi.sel2 = sel2;
515 static int handle_stsi(struct kvm_vcpu *vcpu)
517 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
518 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
519 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
520 unsigned long mem = 0;
525 vcpu->stat.instruction_stsi++;
526 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
528 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
529 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
532 kvm_s390_set_psw_cc(vcpu, 3);
536 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
537 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
538 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
541 vcpu->run->s.regs.gprs[0] = 3 << 28;
542 kvm_s390_set_psw_cc(vcpu, 0);
546 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
548 if (operand2 & 0xfff)
549 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
552 case 1: /* same handling for 1 and 2 */
554 mem = get_zeroed_page(GFP_KERNEL);
557 if (stsi((void *) mem, fc, sel1, sel2))
561 if (sel1 != 2 || sel2 != 2)
563 mem = get_zeroed_page(GFP_KERNEL);
566 handle_stsi_3_2_2(vcpu, (void *) mem);
570 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
572 rc = kvm_s390_inject_prog_cond(vcpu, rc);
575 if (vcpu->kvm->arch.user_stsi) {
576 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
579 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
581 kvm_s390_set_psw_cc(vcpu, 0);
582 vcpu->run->s.regs.gprs[0] = 0;
585 kvm_s390_set_psw_cc(vcpu, 3);
591 static const intercept_handler_t b2_handlers[256] = {
592 [0x02] = handle_stidp,
593 [0x04] = handle_set_clock,
594 [0x10] = handle_set_prefix,
595 [0x11] = handle_store_prefix,
596 [0x12] = handle_store_cpu_address,
597 [0x21] = handle_ipte_interlock,
598 [0x29] = handle_skey,
599 [0x2a] = handle_skey,
600 [0x2b] = handle_skey,
601 [0x2c] = handle_test_block,
602 [0x30] = handle_io_inst,
603 [0x31] = handle_io_inst,
604 [0x32] = handle_io_inst,
605 [0x33] = handle_io_inst,
606 [0x34] = handle_io_inst,
607 [0x35] = handle_io_inst,
608 [0x36] = handle_io_inst,
609 [0x37] = handle_io_inst,
610 [0x38] = handle_io_inst,
611 [0x39] = handle_io_inst,
612 [0x3a] = handle_io_inst,
613 [0x3b] = handle_io_inst,
614 [0x3c] = handle_io_inst,
615 [0x50] = handle_ipte_interlock,
616 [0x5f] = handle_io_inst,
617 [0x74] = handle_io_inst,
618 [0x76] = handle_io_inst,
619 [0x7d] = handle_stsi,
620 [0xb1] = handle_stfl,
621 [0xb2] = handle_lpswe,
624 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
626 intercept_handler_t handler;
629 * A lot of B2 instructions are priviledged. Here we check for
630 * the privileged ones, that we can handle in the kernel.
631 * Anything else goes to userspace.
633 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
635 return handler(vcpu);
640 static int handle_epsw(struct kvm_vcpu *vcpu)
644 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
646 /* This basically extracts the mask half of the psw. */
647 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
648 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
650 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
651 vcpu->run->s.regs.gprs[reg2] |=
652 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
657 #define PFMF_RESERVED 0xfffc0101UL
658 #define PFMF_SK 0x00020000UL
659 #define PFMF_CF 0x00010000UL
660 #define PFMF_UI 0x00008000UL
661 #define PFMF_FSC 0x00007000UL
662 #define PFMF_NQ 0x00000800UL
663 #define PFMF_MR 0x00000400UL
664 #define PFMF_MC 0x00000200UL
665 #define PFMF_KEY 0x000000feUL
667 static int handle_pfmf(struct kvm_vcpu *vcpu)
670 unsigned long start, end;
672 vcpu->stat.instruction_pfmf++;
674 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
676 if (!MACHINE_HAS_PFMF)
677 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
679 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
680 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
682 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
683 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
685 /* Only provide non-quiescing support if the host supports it */
686 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
687 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
689 /* No support for conditional-SSKE */
690 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
691 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
693 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
694 start = kvm_s390_logical_to_effective(vcpu, start);
696 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
698 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
701 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
704 /* only support 2G frame size if EDAT2 is available and we are
705 not in 24-bit addressing mode */
706 if (!test_kvm_facility(vcpu->kvm, 78) ||
707 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
708 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
709 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
712 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
715 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
716 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
717 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
720 while (start < end) {
721 unsigned long useraddr, abs_addr;
723 /* Translate guest address to host address */
724 if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
725 abs_addr = kvm_s390_real_to_abs(vcpu, start);
728 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
729 if (kvm_is_error_hva(useraddr))
730 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
732 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
733 if (clear_user((void __user *)useraddr, PAGE_SIZE))
734 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
737 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
738 int rc = __skey_check_enable(vcpu);
742 if (set_guest_storage_key(current->mm, useraddr,
743 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
744 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
745 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
750 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
751 vcpu->run->s.regs.gprs[reg2] = end;
755 static int handle_essa(struct kvm_vcpu *vcpu)
757 /* entries expected to be 1FF */
758 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
759 unsigned long *cbrlo, cbrle;
763 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
764 gmap = vcpu->arch.gmap;
765 vcpu->stat.instruction_essa++;
766 if (!vcpu->kvm->arch.use_cmma)
767 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
769 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
770 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
772 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
773 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
775 /* Rewind PSW to repeat the ESSA instruction */
776 kvm_s390_rewind_psw(vcpu, 4);
777 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
778 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
779 down_read(&gmap->mm->mmap_sem);
780 for (i = 0; i < entries; ++i) {
782 if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
785 /* try to free backing */
786 __gmap_zap(gmap, cbrle);
788 up_read(&gmap->mm->mmap_sem);
790 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
794 static const intercept_handler_t b9_handlers[256] = {
795 [0x8a] = handle_ipte_interlock,
796 [0x8d] = handle_epsw,
797 [0x8e] = handle_ipte_interlock,
798 [0x8f] = handle_ipte_interlock,
799 [0xab] = handle_essa,
800 [0xaf] = handle_pfmf,
803 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
805 intercept_handler_t handler;
807 /* This is handled just as for the B2 instructions. */
808 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
810 return handler(vcpu);
815 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
817 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
818 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
819 int reg, rc, nr_regs;
824 vcpu->stat.instruction_lctl++;
826 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
827 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
829 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
832 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
834 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
835 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
837 nr_regs = ((reg3 - reg1) & 0xf) + 1;
838 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
840 return kvm_s390_inject_prog_cond(vcpu, rc);
844 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
845 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
848 reg = (reg + 1) % 16;
850 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
854 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
856 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
857 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
858 int reg, rc, nr_regs;
863 vcpu->stat.instruction_stctl++;
865 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
866 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
868 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
871 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
873 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
874 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
879 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
882 reg = (reg + 1) % 16;
884 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
885 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
888 static int handle_lctlg(struct kvm_vcpu *vcpu)
890 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
891 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
892 int reg, rc, nr_regs;
897 vcpu->stat.instruction_lctlg++;
899 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
900 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
902 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
905 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
907 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
908 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
910 nr_regs = ((reg3 - reg1) & 0xf) + 1;
911 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
913 return kvm_s390_inject_prog_cond(vcpu, rc);
917 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
920 reg = (reg + 1) % 16;
922 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
926 static int handle_stctg(struct kvm_vcpu *vcpu)
928 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
929 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
930 int reg, rc, nr_regs;
935 vcpu->stat.instruction_stctg++;
937 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
938 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
940 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
943 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
945 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
946 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
951 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
954 reg = (reg + 1) % 16;
956 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
957 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
960 static const intercept_handler_t eb_handlers[256] = {
961 [0x2f] = handle_lctlg,
962 [0x25] = handle_stctg,
965 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
967 intercept_handler_t handler;
969 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
971 return handler(vcpu);
975 static int handle_tprot(struct kvm_vcpu *vcpu)
977 u64 address1, address2;
978 unsigned long hva, gpa;
983 vcpu->stat.instruction_tprot++;
985 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
986 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
988 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
990 /* we only handle the Linux memory detection case:
992 * everything else goes to userspace. */
995 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
997 ret = guest_translate_address(vcpu, address1, ar, &gpa, 1);
998 if (ret == PGM_PROTECTION) {
999 /* Write protected? Try again with read-only... */
1001 ret = guest_translate_address(vcpu, address1, ar, &gpa, 0);
1004 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1005 ret = kvm_s390_inject_program_int(vcpu, ret);
1006 } else if (ret > 0) {
1007 /* Translation not available */
1008 kvm_s390_set_psw_cc(vcpu, 3);
1014 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1015 if (kvm_is_error_hva(hva)) {
1016 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1019 cc = 1; /* Write not permitted ==> read-only */
1020 kvm_s390_set_psw_cc(vcpu, cc);
1021 /* Note: CC2 only occurs for storage keys (not supported yet) */
1024 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1029 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1031 /* For e5xx... instructions we only handle TPROT */
1032 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1033 return handle_tprot(vcpu);
1037 static int handle_sckpf(struct kvm_vcpu *vcpu)
1041 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1042 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1044 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1045 return kvm_s390_inject_program_int(vcpu,
1048 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1049 vcpu->arch.sie_block->todpr = value;
1054 static const intercept_handler_t x01_handlers[256] = {
1055 [0x07] = handle_sckpf,
1058 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1060 intercept_handler_t handler;
1062 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1064 return handler(vcpu);