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>
28 #include <asm/ptrace.h>
29 #include <asm/compat.h>
35 static int handle_ri(struct kvm_vcpu *vcpu)
37 if (test_kvm_facility(vcpu->kvm, 64)) {
38 vcpu->arch.sie_block->ecb3 |= 0x01;
39 kvm_s390_retry_instr(vcpu);
42 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
45 int kvm_s390_handle_aa(struct kvm_vcpu *vcpu)
47 if ((vcpu->arch.sie_block->ipa & 0xf) <= 4)
48 return handle_ri(vcpu);
53 /* Handle SCK (SET CLOCK) interception */
54 static int handle_set_clock(struct kvm_vcpu *vcpu)
60 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
61 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
63 op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
64 if (op2 & 7) /* Operand must be on a doubleword boundary */
65 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
66 rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
68 return kvm_s390_inject_prog_cond(vcpu, rc);
70 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
71 kvm_s390_set_tod_clock(vcpu->kvm, val);
73 kvm_s390_set_psw_cc(vcpu, 0);
77 static int handle_set_prefix(struct kvm_vcpu *vcpu)
84 vcpu->stat.instruction_spx++;
86 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
87 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
89 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
91 /* must be word boundary */
93 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
96 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
98 return kvm_s390_inject_prog_cond(vcpu, rc);
100 address &= 0x7fffe000u;
103 * Make sure the new value is valid memory. We only need to check the
104 * first page, since address is 8k aligned and memory pieces are always
105 * at least 1MB aligned and have at least a size of 1MB.
107 if (kvm_is_error_gpa(vcpu->kvm, address))
108 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
110 kvm_s390_set_prefix(vcpu, address);
111 trace_kvm_s390_handle_prefix(vcpu, 1, address);
115 static int handle_store_prefix(struct kvm_vcpu *vcpu)
122 vcpu->stat.instruction_stpx++;
124 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
125 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
127 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
129 /* must be word boundary */
131 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
133 address = kvm_s390_get_prefix(vcpu);
136 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
138 return kvm_s390_inject_prog_cond(vcpu, rc);
140 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
141 trace_kvm_s390_handle_prefix(vcpu, 0, address);
145 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
147 u16 vcpu_id = vcpu->vcpu_id;
152 vcpu->stat.instruction_stap++;
154 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
155 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
157 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
160 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
162 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
164 return kvm_s390_inject_prog_cond(vcpu, rc);
166 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
167 trace_kvm_s390_handle_stap(vcpu, ga);
171 static int __skey_check_enable(struct kvm_vcpu *vcpu)
175 trace_kvm_s390_skey_related_inst(vcpu);
176 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
179 rc = s390_enable_skey();
180 VCPU_EVENT(vcpu, 3, "enabling storage keys for guest: %d", rc);
182 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
186 static int try_handle_skey(struct kvm_vcpu *vcpu)
190 vcpu->stat.instruction_storage_key++;
191 rc = __skey_check_enable(vcpu);
195 /* with storage-key facility, SIE interprets it for us */
196 kvm_s390_retry_instr(vcpu);
197 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
203 static int handle_iske(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 rc = try_handle_skey(vcpu);
215 return rc != -EAGAIN ? rc : 0;
217 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
219 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
220 addr = kvm_s390_logical_to_effective(vcpu, addr);
221 addr = kvm_s390_real_to_abs(vcpu, addr);
222 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
223 if (kvm_is_error_hva(addr))
224 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
226 down_read(¤t->mm->mmap_sem);
227 rc = get_guest_storage_key(current->mm, addr, &key);
228 up_read(¤t->mm->mmap_sem);
230 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
231 vcpu->run->s.regs.gprs[reg1] &= ~0xff;
232 vcpu->run->s.regs.gprs[reg1] |= key;
236 static int handle_rrbe(struct kvm_vcpu *vcpu)
242 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
243 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
245 rc = try_handle_skey(vcpu);
247 return rc != -EAGAIN ? rc : 0;
249 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
251 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
252 addr = kvm_s390_logical_to_effective(vcpu, addr);
253 addr = kvm_s390_real_to_abs(vcpu, addr);
254 addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(addr));
255 if (kvm_is_error_hva(addr))
256 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
258 down_read(¤t->mm->mmap_sem);
259 rc = reset_guest_reference_bit(current->mm, addr);
260 up_read(¤t->mm->mmap_sem);
262 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
264 kvm_s390_set_psw_cc(vcpu, rc);
272 static int handle_sske(struct kvm_vcpu *vcpu)
274 unsigned char m3 = vcpu->arch.sie_block->ipb >> 28;
275 unsigned long start, end;
276 unsigned char key, oldkey;
280 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
281 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
283 rc = try_handle_skey(vcpu);
285 return rc != -EAGAIN ? rc : 0;
287 if (!test_kvm_facility(vcpu->kvm, 8))
289 if (!test_kvm_facility(vcpu->kvm, 10))
290 m3 &= ~(SSKE_MC | SSKE_MR);
291 if (!test_kvm_facility(vcpu->kvm, 14))
294 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
296 key = vcpu->run->s.regs.gprs[reg1] & 0xfe;
297 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
298 start = kvm_s390_logical_to_effective(vcpu, start);
300 /* start already designates an absolute address */
301 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
303 start = kvm_s390_real_to_abs(vcpu, start);
304 end = start + PAGE_SIZE;
307 while (start != end) {
308 unsigned long addr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
310 if (kvm_is_error_hva(addr))
311 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
313 down_read(¤t->mm->mmap_sem);
314 rc = cond_set_guest_storage_key(current->mm, addr, key, &oldkey,
315 m3 & SSKE_NQ, m3 & SSKE_MR,
317 up_read(¤t->mm->mmap_sem);
319 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
323 if (m3 & (SSKE_MC | SSKE_MR)) {
325 /* skey in reg1 is unpredictable */
326 kvm_s390_set_psw_cc(vcpu, 3);
328 kvm_s390_set_psw_cc(vcpu, rc);
329 vcpu->run->s.regs.gprs[reg1] &= ~0xff00UL;
330 vcpu->run->s.regs.gprs[reg1] |= (u64) oldkey << 8;
334 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT)
335 vcpu->run->s.regs.gprs[reg2] &= ~PAGE_MASK;
337 vcpu->run->s.regs.gprs[reg2] &= ~0xfffff000UL;
338 end = kvm_s390_logical_to_effective(vcpu, end);
339 vcpu->run->s.regs.gprs[reg2] |= end;
344 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
346 vcpu->stat.instruction_ipte_interlock++;
347 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
348 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
349 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
350 kvm_s390_retry_instr(vcpu);
351 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
355 static int handle_test_block(struct kvm_vcpu *vcpu)
360 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
361 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
363 kvm_s390_get_regs_rre(vcpu, NULL, ®2);
364 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
365 addr = kvm_s390_logical_to_effective(vcpu, addr);
366 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
367 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
368 addr = kvm_s390_real_to_abs(vcpu, addr);
370 if (kvm_is_error_gpa(vcpu->kvm, addr))
371 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
373 * We don't expect errors on modern systems, and do not care
374 * about storage keys (yet), so let's just clear the page.
376 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
378 kvm_s390_set_psw_cc(vcpu, 0);
379 vcpu->run->s.regs.gprs[0] = 0;
383 static int handle_tpi(struct kvm_vcpu *vcpu)
385 struct kvm_s390_interrupt_info *inti;
392 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
394 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
396 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
398 kvm_s390_set_psw_cc(vcpu, 0);
402 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
403 tpi_data[1] = inti->io.io_int_parm;
404 tpi_data[2] = inti->io.io_int_word;
407 * Store the two-word I/O interruption code into the
410 len = sizeof(tpi_data) - 4;
411 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
413 rc = kvm_s390_inject_prog_cond(vcpu, rc);
414 goto reinject_interrupt;
418 * Store the three-word I/O interruption code into
419 * the appropriate lowcore area.
421 len = sizeof(tpi_data);
422 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
423 /* failed writes to the low core are not recoverable */
425 goto reinject_interrupt;
429 /* irq was successfully handed to the guest */
431 kvm_s390_set_psw_cc(vcpu, 1);
435 * If we encounter a problem storing the interruption code, the
436 * instruction is suppressed from the guest's view: reinject the
439 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
443 /* don't set the cc, a pgm irq was injected or we drop to user space */
444 return rc ? -EFAULT : 0;
447 static int handle_tsch(struct kvm_vcpu *vcpu)
449 struct kvm_s390_interrupt_info *inti = NULL;
450 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
452 /* a valid schid has at least one bit set */
453 if (vcpu->run->s.regs.gprs[1])
454 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
455 vcpu->run->s.regs.gprs[1]);
458 * Prepare exit to userspace.
459 * We indicate whether we dequeued a pending I/O interrupt
460 * so that userspace can re-inject it if the instruction gets
461 * a program check. While this may re-order the pending I/O
462 * interrupts, this is no problem since the priority is kept
465 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
466 vcpu->run->s390_tsch.dequeued = !!inti;
468 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
469 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
470 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
471 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
473 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
478 static int handle_io_inst(struct kvm_vcpu *vcpu)
480 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
482 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
483 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
485 if (vcpu->kvm->arch.css_support) {
487 * Most I/O instructions will be handled by userspace.
488 * Exceptions are tpi and the interrupt portion of tsch.
490 if (vcpu->arch.sie_block->ipa == 0xb236)
491 return handle_tpi(vcpu);
492 if (vcpu->arch.sie_block->ipa == 0xb235)
493 return handle_tsch(vcpu);
494 /* Handle in userspace. */
498 * Set condition code 3 to stop the guest from issuing channel
501 kvm_s390_set_psw_cc(vcpu, 3);
506 static int handle_stfl(struct kvm_vcpu *vcpu)
511 vcpu->stat.instruction_stfl++;
513 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
514 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
517 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
518 * into a u32 memory representation. They will remain bits 0-31.
520 fac = *vcpu->kvm->arch.model.fac_list >> 32;
521 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
525 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
526 trace_kvm_s390_handle_stfl(vcpu, fac);
530 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
531 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
532 #define PSW_ADDR_24 0x0000000000ffffffUL
533 #define PSW_ADDR_31 0x000000007fffffffUL
535 int is_valid_psw(psw_t *psw)
537 if (psw->mask & PSW_MASK_UNASSIGNED)
539 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
540 if (psw->addr & ~PSW_ADDR_31)
543 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
545 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
552 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
554 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
555 psw_compat_t new_psw;
560 if (gpsw->mask & PSW_MASK_PSTATE)
561 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
563 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
565 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
567 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
569 return kvm_s390_inject_prog_cond(vcpu, rc);
570 if (!(new_psw.mask & PSW32_MASK_BASE))
571 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
572 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
573 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
574 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
575 if (!is_valid_psw(gpsw))
576 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
580 static int handle_lpswe(struct kvm_vcpu *vcpu)
587 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
588 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
590 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
592 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
593 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
595 return kvm_s390_inject_prog_cond(vcpu, rc);
596 vcpu->arch.sie_block->gpsw = new_psw;
597 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
598 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
602 static int handle_stidp(struct kvm_vcpu *vcpu)
604 u64 stidp_data = vcpu->kvm->arch.model.cpuid;
609 vcpu->stat.instruction_stidp++;
611 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
612 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
614 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
617 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
619 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
621 return kvm_s390_inject_prog_cond(vcpu, rc);
623 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
627 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
632 cpus = atomic_read(&vcpu->kvm->online_vcpus);
634 /* deal with other level 3 hypervisors */
635 if (stsi(mem, 3, 2, 2))
639 for (n = mem->count - 1; n > 0 ; n--)
640 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
642 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
643 mem->vm[0].cpus_total = cpus;
644 mem->vm[0].cpus_configured = cpus;
645 mem->vm[0].cpus_standby = 0;
646 mem->vm[0].cpus_reserved = 0;
647 mem->vm[0].caf = 1000;
648 memcpy(mem->vm[0].name, "KVMguest", 8);
649 ASCEBC(mem->vm[0].name, 8);
650 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
651 ASCEBC(mem->vm[0].cpi, 16);
654 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, ar_t ar,
655 u8 fc, u8 sel1, u16 sel2)
657 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
658 vcpu->run->s390_stsi.addr = addr;
659 vcpu->run->s390_stsi.ar = ar;
660 vcpu->run->s390_stsi.fc = fc;
661 vcpu->run->s390_stsi.sel1 = sel1;
662 vcpu->run->s390_stsi.sel2 = sel2;
665 static int handle_stsi(struct kvm_vcpu *vcpu)
667 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
668 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
669 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
670 unsigned long mem = 0;
675 vcpu->stat.instruction_stsi++;
676 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
678 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
679 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
682 kvm_s390_set_psw_cc(vcpu, 3);
686 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
687 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
688 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
691 vcpu->run->s.regs.gprs[0] = 3 << 28;
692 kvm_s390_set_psw_cc(vcpu, 0);
696 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
698 if (operand2 & 0xfff)
699 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
702 case 1: /* same handling for 1 and 2 */
704 mem = get_zeroed_page(GFP_KERNEL);
707 if (stsi((void *) mem, fc, sel1, sel2))
711 if (sel1 != 2 || sel2 != 2)
713 mem = get_zeroed_page(GFP_KERNEL);
716 handle_stsi_3_2_2(vcpu, (void *) mem);
720 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
722 rc = kvm_s390_inject_prog_cond(vcpu, rc);
725 if (vcpu->kvm->arch.user_stsi) {
726 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
729 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
731 kvm_s390_set_psw_cc(vcpu, 0);
732 vcpu->run->s.regs.gprs[0] = 0;
735 kvm_s390_set_psw_cc(vcpu, 3);
741 static const intercept_handler_t b2_handlers[256] = {
742 [0x02] = handle_stidp,
743 [0x04] = handle_set_clock,
744 [0x10] = handle_set_prefix,
745 [0x11] = handle_store_prefix,
746 [0x12] = handle_store_cpu_address,
747 [0x14] = kvm_s390_handle_vsie,
748 [0x21] = handle_ipte_interlock,
749 [0x29] = handle_iske,
750 [0x2a] = handle_rrbe,
751 [0x2b] = handle_sske,
752 [0x2c] = handle_test_block,
753 [0x30] = handle_io_inst,
754 [0x31] = handle_io_inst,
755 [0x32] = handle_io_inst,
756 [0x33] = handle_io_inst,
757 [0x34] = handle_io_inst,
758 [0x35] = handle_io_inst,
759 [0x36] = handle_io_inst,
760 [0x37] = handle_io_inst,
761 [0x38] = handle_io_inst,
762 [0x39] = handle_io_inst,
763 [0x3a] = handle_io_inst,
764 [0x3b] = handle_io_inst,
765 [0x3c] = handle_io_inst,
766 [0x50] = handle_ipte_interlock,
767 [0x5f] = handle_io_inst,
768 [0x74] = handle_io_inst,
769 [0x76] = handle_io_inst,
770 [0x7d] = handle_stsi,
771 [0xb1] = handle_stfl,
772 [0xb2] = handle_lpswe,
775 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
777 intercept_handler_t handler;
780 * A lot of B2 instructions are priviledged. Here we check for
781 * the privileged ones, that we can handle in the kernel.
782 * Anything else goes to userspace.
784 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
786 return handler(vcpu);
791 static int handle_epsw(struct kvm_vcpu *vcpu)
795 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
797 /* This basically extracts the mask half of the psw. */
798 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
799 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
801 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
802 vcpu->run->s.regs.gprs[reg2] |=
803 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
808 #define PFMF_RESERVED 0xfffc0101UL
809 #define PFMF_SK 0x00020000UL
810 #define PFMF_CF 0x00010000UL
811 #define PFMF_UI 0x00008000UL
812 #define PFMF_FSC 0x00007000UL
813 #define PFMF_NQ 0x00000800UL
814 #define PFMF_MR 0x00000400UL
815 #define PFMF_MC 0x00000200UL
816 #define PFMF_KEY 0x000000feUL
818 static int handle_pfmf(struct kvm_vcpu *vcpu)
820 bool mr = false, mc = false, nq;
822 unsigned long start, end;
825 vcpu->stat.instruction_pfmf++;
827 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
829 if (!test_kvm_facility(vcpu->kvm, 8))
830 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
832 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
833 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
835 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
836 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
838 /* Only provide non-quiescing support if enabled for the guest */
839 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
840 !test_kvm_facility(vcpu->kvm, 14))
841 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
843 /* Only provide conditional-SSKE support if enabled for the guest */
844 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK &&
845 test_kvm_facility(vcpu->kvm, 10)) {
846 mr = vcpu->run->s.regs.gprs[reg1] & PFMF_MR;
847 mc = vcpu->run->s.regs.gprs[reg1] & PFMF_MC;
850 nq = vcpu->run->s.regs.gprs[reg1] & PFMF_NQ;
851 key = vcpu->run->s.regs.gprs[reg1] & PFMF_KEY;
852 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
853 start = kvm_s390_logical_to_effective(vcpu, start);
855 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
856 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
857 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
860 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
862 /* only 4k frames specify a real address */
863 start = kvm_s390_real_to_abs(vcpu, start);
864 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
867 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
870 /* only support 2G frame size if EDAT2 is available and we are
871 not in 24-bit addressing mode */
872 if (!test_kvm_facility(vcpu->kvm, 78) ||
873 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
874 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
875 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
878 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
881 while (start != end) {
882 unsigned long useraddr;
884 /* Translate guest address to host address */
885 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(start));
886 if (kvm_is_error_hva(useraddr))
887 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
889 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
890 if (clear_user((void __user *)useraddr, PAGE_SIZE))
891 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
894 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
895 int rc = __skey_check_enable(vcpu);
899 down_read(¤t->mm->mmap_sem);
900 rc = cond_set_guest_storage_key(current->mm, useraddr,
901 key, NULL, nq, mr, mc);
902 up_read(¤t->mm->mmap_sem);
904 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
909 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
910 if (psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_64BIT) {
911 vcpu->run->s.regs.gprs[reg2] = end;
913 vcpu->run->s.regs.gprs[reg2] &= ~0xffffffffUL;
914 end = kvm_s390_logical_to_effective(vcpu, end);
915 vcpu->run->s.regs.gprs[reg2] |= end;
921 static int handle_essa(struct kvm_vcpu *vcpu)
923 /* entries expected to be 1FF */
924 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
925 unsigned long *cbrlo;
929 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
930 gmap = vcpu->arch.gmap;
931 vcpu->stat.instruction_essa++;
932 if (!vcpu->kvm->arch.use_cmma)
933 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
935 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
936 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
938 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
939 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
941 /* Retry the ESSA instruction */
942 kvm_s390_retry_instr(vcpu);
943 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
944 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
945 down_read(&gmap->mm->mmap_sem);
946 for (i = 0; i < entries; ++i)
947 __gmap_zap(gmap, cbrlo[i]);
948 up_read(&gmap->mm->mmap_sem);
952 static const intercept_handler_t b9_handlers[256] = {
953 [0x8a] = handle_ipte_interlock,
954 [0x8d] = handle_epsw,
955 [0x8e] = handle_ipte_interlock,
956 [0x8f] = handle_ipte_interlock,
957 [0xab] = handle_essa,
958 [0xaf] = handle_pfmf,
961 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
963 intercept_handler_t handler;
965 /* This is handled just as for the B2 instructions. */
966 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
968 return handler(vcpu);
973 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
975 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
976 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
977 int reg, rc, nr_regs;
982 vcpu->stat.instruction_lctl++;
984 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
985 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
987 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
990 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
992 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
993 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
995 nr_regs = ((reg3 - reg1) & 0xf) + 1;
996 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
998 return kvm_s390_inject_prog_cond(vcpu, rc);
1002 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
1003 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
1006 reg = (reg + 1) % 16;
1008 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1012 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
1014 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1015 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1016 int reg, rc, nr_regs;
1021 vcpu->stat.instruction_stctl++;
1023 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1024 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1026 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
1029 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1031 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1032 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
1037 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1040 reg = (reg + 1) % 16;
1042 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
1043 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1046 static int handle_lctlg(struct kvm_vcpu *vcpu)
1048 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1049 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1050 int reg, rc, nr_regs;
1055 vcpu->stat.instruction_lctlg++;
1057 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1058 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1060 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1063 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1065 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1066 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
1068 nr_regs = ((reg3 - reg1) & 0xf) + 1;
1069 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1071 return kvm_s390_inject_prog_cond(vcpu, rc);
1075 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
1078 reg = (reg + 1) % 16;
1080 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
1084 static int handle_stctg(struct kvm_vcpu *vcpu)
1086 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
1087 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
1088 int reg, rc, nr_regs;
1093 vcpu->stat.instruction_stctg++;
1095 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1096 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1098 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
1101 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
1103 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
1104 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
1109 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
1112 reg = (reg + 1) % 16;
1114 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
1115 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
1118 static const intercept_handler_t eb_handlers[256] = {
1119 [0x2f] = handle_lctlg,
1120 [0x25] = handle_stctg,
1126 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
1128 intercept_handler_t handler;
1130 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
1132 return handler(vcpu);
1136 static int handle_tprot(struct kvm_vcpu *vcpu)
1138 u64 address1, address2;
1139 unsigned long hva, gpa;
1140 int ret = 0, cc = 0;
1144 vcpu->stat.instruction_tprot++;
1146 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1147 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1149 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
1151 /* we only handle the Linux memory detection case:
1153 * everything else goes to userspace. */
1154 if (address2 & 0xf0)
1156 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1158 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
1159 if (ret == PGM_PROTECTION) {
1160 /* Write protected? Try again with read-only... */
1162 ret = guest_translate_address(vcpu, address1, ar, &gpa,
1166 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
1167 ret = kvm_s390_inject_program_int(vcpu, ret);
1168 } else if (ret > 0) {
1169 /* Translation not available */
1170 kvm_s390_set_psw_cc(vcpu, 3);
1176 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1177 if (kvm_is_error_hva(hva)) {
1178 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1181 cc = 1; /* Write not permitted ==> read-only */
1182 kvm_s390_set_psw_cc(vcpu, cc);
1183 /* Note: CC2 only occurs for storage keys (not supported yet) */
1186 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1191 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1193 /* For e5xx... instructions we only handle TPROT */
1194 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1195 return handle_tprot(vcpu);
1199 static int handle_sckpf(struct kvm_vcpu *vcpu)
1203 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1204 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1206 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1207 return kvm_s390_inject_program_int(vcpu,
1210 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1211 vcpu->arch.sie_block->todpr = value;
1216 static int handle_ptff(struct kvm_vcpu *vcpu)
1218 /* we don't emulate any control instructions yet */
1219 kvm_s390_set_psw_cc(vcpu, 3);
1223 static const intercept_handler_t x01_handlers[256] = {
1224 [0x04] = handle_ptff,
1225 [0x07] = handle_sckpf,
1228 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1230 intercept_handler_t handler;
1232 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1234 return handler(vcpu);