2 * handling privileged instructions
4 * Copyright IBM Corp. 2008
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 <asm/asm-offsets.h>
18 #include <asm/current.h>
19 #include <asm/debug.h>
20 #include <asm/ebcdic.h>
21 #include <asm/sysinfo.h>
22 #include <asm/ptrace.h>
23 #include <asm/compat.h>
28 static int handle_set_prefix(struct kvm_vcpu *vcpu)
34 vcpu->stat.instruction_spx++;
36 operand2 = kvm_s390_get_base_disp_s(vcpu);
38 /* must be word boundary */
40 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
45 if (get_guest(vcpu, address, (u32 __user *) operand2)) {
46 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
50 address = address & 0x7fffe000u;
52 /* make sure that the new value is valid memory */
53 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
54 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1))) {
55 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
59 kvm_s390_set_prefix(vcpu, address);
61 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
62 trace_kvm_s390_handle_prefix(vcpu, 1, address);
67 static int handle_store_prefix(struct kvm_vcpu *vcpu)
72 vcpu->stat.instruction_stpx++;
74 operand2 = kvm_s390_get_base_disp_s(vcpu);
76 /* must be word boundary */
78 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
82 address = vcpu->arch.sie_block->prefix;
83 address = address & 0x7fffe000u;
86 if (put_guest(vcpu, address, (u32 __user *)operand2)) {
87 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
91 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
92 trace_kvm_s390_handle_prefix(vcpu, 0, address);
97 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
102 vcpu->stat.instruction_stap++;
104 useraddr = kvm_s390_get_base_disp_s(vcpu);
107 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
111 rc = put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr);
113 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
117 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
118 trace_kvm_s390_handle_stap(vcpu, useraddr);
123 static int handle_skey(struct kvm_vcpu *vcpu)
125 vcpu->stat.instruction_storage_key++;
126 vcpu->arch.sie_block->gpsw.addr -= 4;
127 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
131 static int handle_tpi(struct kvm_vcpu *vcpu)
133 struct kvm_s390_interrupt_info *inti;
137 addr = kvm_s390_get_base_disp_s(vcpu);
139 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
143 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
149 * Store the two-word I/O interruption code into the
152 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) addr);
153 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) (addr + 2));
154 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) (addr + 4));
157 * Store the three-word I/O interruption code into
158 * the appropriate lowcore area.
160 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
161 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
162 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
163 put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
167 /* Set condition code and we're done. */
168 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
169 vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44;
174 static int handle_tsch(struct kvm_vcpu *vcpu)
176 struct kvm_s390_interrupt_info *inti;
178 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
179 vcpu->run->s.regs.gprs[1]);
182 * Prepare exit to userspace.
183 * We indicate whether we dequeued a pending I/O interrupt
184 * so that userspace can re-inject it if the instruction gets
185 * a program check. While this may re-order the pending I/O
186 * interrupts, this is no problem since the priority is kept
189 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
190 vcpu->run->s390_tsch.dequeued = !!inti;
192 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
193 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
194 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
195 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
197 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
202 static int handle_io_inst(struct kvm_vcpu *vcpu)
204 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
206 if (vcpu->kvm->arch.css_support) {
208 * Most I/O instructions will be handled by userspace.
209 * Exceptions are tpi and the interrupt portion of tsch.
211 if (vcpu->arch.sie_block->ipa == 0xb236)
212 return handle_tpi(vcpu);
213 if (vcpu->arch.sie_block->ipa == 0xb235)
214 return handle_tsch(vcpu);
215 /* Handle in userspace. */
219 * Set condition code 3 to stop the guest from issueing channel
222 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
223 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
228 static int handle_stfl(struct kvm_vcpu *vcpu)
230 unsigned int facility_list;
233 vcpu->stat.instruction_stfl++;
234 /* only pass the facility bits, which we can handle */
235 facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
237 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
238 &facility_list, sizeof(facility_list));
240 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
242 VCPU_EVENT(vcpu, 5, "store facility list value %x",
244 trace_kvm_s390_handle_stfl(vcpu, facility_list);
249 static void handle_new_psw(struct kvm_vcpu *vcpu)
251 /* Check whether the new psw is enabled for machine checks. */
252 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
253 kvm_s390_deliver_pending_machine_checks(vcpu);
256 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
257 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
258 #define PSW_ADDR_24 0x0000000000ffffffUL
259 #define PSW_ADDR_31 0x000000007fffffffUL
261 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
264 psw_compat_t new_psw;
266 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
267 return kvm_s390_inject_program_int(vcpu,
268 PGM_PRIVILEGED_OPERATION);
270 addr = kvm_s390_get_base_disp_s(vcpu);
273 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
275 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
276 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
278 if (!(new_psw.mask & PSW32_MASK_BASE))
279 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
281 vcpu->arch.sie_block->gpsw.mask =
282 (new_psw.mask & ~PSW32_MASK_BASE) << 32;
283 vcpu->arch.sie_block->gpsw.mask |= new_psw.addr & PSW32_ADDR_AMODE;
284 vcpu->arch.sie_block->gpsw.addr = new_psw.addr & ~PSW32_ADDR_AMODE;
286 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_UNASSIGNED) ||
287 (!(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) &&
288 (vcpu->arch.sie_block->gpsw.addr & ~PSW_ADDR_24)) ||
289 ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) ==
291 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
293 handle_new_psw(vcpu);
297 static int handle_lpswe(struct kvm_vcpu *vcpu)
302 addr = kvm_s390_get_base_disp_s(vcpu);
305 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
307 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
308 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
310 vcpu->arch.sie_block->gpsw.mask = new_psw.mask;
311 vcpu->arch.sie_block->gpsw.addr = new_psw.addr;
313 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_UNASSIGNED) ||
314 (((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) ==
316 (vcpu->arch.sie_block->gpsw.addr & ~PSW_ADDR_31)) ||
317 (!(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) &&
318 (vcpu->arch.sie_block->gpsw.addr & ~PSW_ADDR_24)) ||
319 ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_ADDR_MODE) ==
321 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
323 handle_new_psw(vcpu);
327 static int handle_stidp(struct kvm_vcpu *vcpu)
332 vcpu->stat.instruction_stidp++;
334 operand2 = kvm_s390_get_base_disp_s(vcpu);
337 kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
341 rc = put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2);
343 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
347 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
352 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
354 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
358 spin_lock(&fi->lock);
359 for (n = 0; n < KVM_MAX_VCPUS; n++)
360 if (fi->local_int[n])
362 spin_unlock(&fi->lock);
364 /* deal with other level 3 hypervisors */
365 if (stsi(mem, 3, 2, 2))
369 for (n = mem->count - 1; n > 0 ; n--)
370 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
372 mem->vm[0].cpus_total = cpus;
373 mem->vm[0].cpus_configured = cpus;
374 mem->vm[0].cpus_standby = 0;
375 mem->vm[0].cpus_reserved = 0;
376 mem->vm[0].caf = 1000;
377 memcpy(mem->vm[0].name, "KVMguest", 8);
378 ASCEBC(mem->vm[0].name, 8);
379 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
380 ASCEBC(mem->vm[0].cpi, 16);
383 static int handle_stsi(struct kvm_vcpu *vcpu)
385 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
386 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
387 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
391 vcpu->stat.instruction_stsi++;
392 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
394 operand2 = kvm_s390_get_base_disp_s(vcpu);
396 if (operand2 & 0xfff && fc > 0)
397 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
401 vcpu->run->s.regs.gprs[0] = 3 << 28;
402 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
404 case 1: /* same handling for 1 and 2 */
406 mem = get_zeroed_page(GFP_KERNEL);
409 if (stsi((void *) mem, fc, sel1, sel2))
413 if (sel1 != 2 || sel2 != 2)
415 mem = get_zeroed_page(GFP_KERNEL);
418 handle_stsi_3_2_2(vcpu, (void *) mem);
424 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
425 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
428 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
430 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
431 vcpu->run->s.regs.gprs[0] = 0;
436 /* condition code 3 */
437 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
441 static const intercept_handler_t b2_handlers[256] = {
442 [0x02] = handle_stidp,
443 [0x10] = handle_set_prefix,
444 [0x11] = handle_store_prefix,
445 [0x12] = handle_store_cpu_address,
446 [0x29] = handle_skey,
447 [0x2a] = handle_skey,
448 [0x2b] = handle_skey,
449 [0x30] = handle_io_inst,
450 [0x31] = handle_io_inst,
451 [0x32] = handle_io_inst,
452 [0x33] = handle_io_inst,
453 [0x34] = handle_io_inst,
454 [0x35] = handle_io_inst,
455 [0x36] = handle_io_inst,
456 [0x37] = handle_io_inst,
457 [0x38] = handle_io_inst,
458 [0x39] = handle_io_inst,
459 [0x3a] = handle_io_inst,
460 [0x3b] = handle_io_inst,
461 [0x3c] = handle_io_inst,
462 [0x5f] = handle_io_inst,
463 [0x74] = handle_io_inst,
464 [0x76] = handle_io_inst,
465 [0x7d] = handle_stsi,
466 [0xb1] = handle_stfl,
467 [0xb2] = handle_lpswe,
470 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
472 intercept_handler_t handler;
475 * a lot of B2 instructions are priviledged. We first check for
476 * the privileged ones, that we can handle in the kernel. If the
477 * kernel can handle this instruction, we check for the problem
478 * state bit and (a) handle the instruction or (b) send a code 2
480 * Anything else goes to userspace.*/
481 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
483 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
484 return kvm_s390_inject_program_int(vcpu,
485 PGM_PRIVILEGED_OPERATION);
487 return handler(vcpu);
492 static int handle_epsw(struct kvm_vcpu *vcpu)
496 reg1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 24;
497 reg2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
499 /* This basically extracts the mask half of the psw. */
500 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000;
501 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
503 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000;
504 vcpu->run->s.regs.gprs[reg2] |=
505 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffff;
510 static const intercept_handler_t b9_handlers[256] = {
511 [0x8d] = handle_epsw,
512 [0x9c] = handle_io_inst,
515 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
517 intercept_handler_t handler;
519 /* This is handled just as for the B2 instructions. */
520 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
522 if ((handler != handle_epsw) &&
523 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE))
524 return kvm_s390_inject_program_int(vcpu,
525 PGM_PRIVILEGED_OPERATION);
527 return handler(vcpu);
532 static const intercept_handler_t eb_handlers[256] = {
533 [0x8a] = handle_io_inst,
536 int kvm_s390_handle_priv_eb(struct kvm_vcpu *vcpu)
538 intercept_handler_t handler;
540 /* All eb instructions that end up here are privileged. */
541 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
542 return kvm_s390_inject_program_int(vcpu,
543 PGM_PRIVILEGED_OPERATION);
544 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
546 return handler(vcpu);
550 static int handle_tprot(struct kvm_vcpu *vcpu)
552 u64 address1, address2;
553 struct vm_area_struct *vma;
554 unsigned long user_address;
556 vcpu->stat.instruction_tprot++;
558 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
560 /* we only handle the Linux memory detection case:
563 * everything else goes to userspace. */
566 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
569 down_read(¤t->mm->mmap_sem);
570 user_address = __gmap_translate(address1, vcpu->arch.gmap);
571 if (IS_ERR_VALUE(user_address))
573 vma = find_vma(current->mm, user_address);
576 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
577 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
578 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
579 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
580 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
582 up_read(¤t->mm->mmap_sem);
586 up_read(¤t->mm->mmap_sem);
587 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
590 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
592 /* For e5xx... instructions we only handle TPROT */
593 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
594 return handle_tprot(vcpu);
598 static int handle_sckpf(struct kvm_vcpu *vcpu)
602 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
603 return kvm_s390_inject_program_int(vcpu,
604 PGM_PRIVILEGED_OPERATION);
606 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
607 return kvm_s390_inject_program_int(vcpu,
610 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
611 vcpu->arch.sie_block->todpr = value;
616 static const intercept_handler_t x01_handlers[256] = {
617 [0x07] = handle_sckpf,
620 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
622 intercept_handler_t handler;
624 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
626 return handler(vcpu);