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 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
43 if (get_guest(vcpu, address, (u32 __user *) operand2))
44 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
46 address = address & 0x7fffe000u;
48 /* make sure that the new value is valid memory */
49 if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
50 (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)))
51 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
53 kvm_s390_set_prefix(vcpu, address);
55 VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
56 trace_kvm_s390_handle_prefix(vcpu, 1, address);
60 static int handle_store_prefix(struct kvm_vcpu *vcpu)
65 vcpu->stat.instruction_stpx++;
67 operand2 = kvm_s390_get_base_disp_s(vcpu);
69 /* must be word boundary */
71 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
73 address = vcpu->arch.sie_block->prefix;
74 address = address & 0x7fffe000u;
77 if (put_guest(vcpu, address, (u32 __user *)operand2))
78 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
80 VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
81 trace_kvm_s390_handle_prefix(vcpu, 0, address);
85 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
89 vcpu->stat.instruction_stap++;
91 useraddr = kvm_s390_get_base_disp_s(vcpu);
94 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
96 if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr))
97 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
99 VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
100 trace_kvm_s390_handle_stap(vcpu, useraddr);
104 static int handle_skey(struct kvm_vcpu *vcpu)
106 vcpu->stat.instruction_storage_key++;
107 vcpu->arch.sie_block->gpsw.addr -= 4;
108 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
112 static int handle_tpi(struct kvm_vcpu *vcpu)
114 struct kvm_s390_interrupt_info *inti;
118 addr = kvm_s390_get_base_disp_s(vcpu);
120 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
122 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
128 * Store the two-word I/O interruption code into the
131 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) addr);
132 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) (addr + 2));
133 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) (addr + 4));
136 * Store the three-word I/O interruption code into
137 * the appropriate lowcore area.
139 put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
140 put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
141 put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
142 put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
146 /* Set condition code and we're done. */
147 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
148 vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44;
152 static int handle_tsch(struct kvm_vcpu *vcpu)
154 struct kvm_s390_interrupt_info *inti;
156 inti = kvm_s390_get_io_int(vcpu->kvm, 0,
157 vcpu->run->s.regs.gprs[1]);
160 * Prepare exit to userspace.
161 * We indicate whether we dequeued a pending I/O interrupt
162 * so that userspace can re-inject it if the instruction gets
163 * a program check. While this may re-order the pending I/O
164 * interrupts, this is no problem since the priority is kept
167 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
168 vcpu->run->s390_tsch.dequeued = !!inti;
170 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
171 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
172 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
173 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
175 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
180 static int handle_io_inst(struct kvm_vcpu *vcpu)
182 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
184 if (vcpu->kvm->arch.css_support) {
186 * Most I/O instructions will be handled by userspace.
187 * Exceptions are tpi and the interrupt portion of tsch.
189 if (vcpu->arch.sie_block->ipa == 0xb236)
190 return handle_tpi(vcpu);
191 if (vcpu->arch.sie_block->ipa == 0xb235)
192 return handle_tsch(vcpu);
193 /* Handle in userspace. */
197 * Set condition code 3 to stop the guest from issueing channel
200 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
201 vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
206 static int handle_stfl(struct kvm_vcpu *vcpu)
208 unsigned int facility_list;
211 vcpu->stat.instruction_stfl++;
212 /* only pass the facility bits, which we can handle */
213 facility_list = S390_lowcore.stfl_fac_list & 0xff00fff3;
215 rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
216 &facility_list, sizeof(facility_list));
218 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
219 VCPU_EVENT(vcpu, 5, "store facility list value %x", facility_list);
220 trace_kvm_s390_handle_stfl(vcpu, facility_list);
224 static void handle_new_psw(struct kvm_vcpu *vcpu)
226 /* Check whether the new psw is enabled for machine checks. */
227 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
228 kvm_s390_deliver_pending_machine_checks(vcpu);
231 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
232 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
233 #define PSW_ADDR_24 0x0000000000ffffffUL
234 #define PSW_ADDR_31 0x000000007fffffffUL
236 static int is_valid_psw(psw_t *psw) {
237 if (psw->mask & PSW_MASK_UNASSIGNED)
239 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
240 if (psw->addr & ~PSW_ADDR_31)
243 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
245 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
250 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
252 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
253 psw_compat_t new_psw;
256 if (gpsw->mask & PSW_MASK_PSTATE)
257 return kvm_s390_inject_program_int(vcpu,
258 PGM_PRIVILEGED_OPERATION);
259 addr = kvm_s390_get_base_disp_s(vcpu);
261 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
262 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
263 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
264 if (!(new_psw.mask & PSW32_MASK_BASE))
265 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
266 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
267 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
268 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
269 if (!is_valid_psw(gpsw))
270 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
271 handle_new_psw(vcpu);
275 static int handle_lpswe(struct kvm_vcpu *vcpu)
280 addr = kvm_s390_get_base_disp_s(vcpu);
282 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
283 if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
284 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
285 vcpu->arch.sie_block->gpsw = new_psw;
286 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
287 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
288 handle_new_psw(vcpu);
292 static int handle_stidp(struct kvm_vcpu *vcpu)
296 vcpu->stat.instruction_stidp++;
298 operand2 = kvm_s390_get_base_disp_s(vcpu);
301 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
303 if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
304 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
306 VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
310 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
312 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
316 spin_lock(&fi->lock);
317 for (n = 0; n < KVM_MAX_VCPUS; n++)
318 if (fi->local_int[n])
320 spin_unlock(&fi->lock);
322 /* deal with other level 3 hypervisors */
323 if (stsi(mem, 3, 2, 2))
327 for (n = mem->count - 1; n > 0 ; n--)
328 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
330 mem->vm[0].cpus_total = cpus;
331 mem->vm[0].cpus_configured = cpus;
332 mem->vm[0].cpus_standby = 0;
333 mem->vm[0].cpus_reserved = 0;
334 mem->vm[0].caf = 1000;
335 memcpy(mem->vm[0].name, "KVMguest", 8);
336 ASCEBC(mem->vm[0].name, 8);
337 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
338 ASCEBC(mem->vm[0].cpi, 16);
341 static int handle_stsi(struct kvm_vcpu *vcpu)
343 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
344 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
345 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
350 vcpu->stat.instruction_stsi++;
351 VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
353 operand2 = kvm_s390_get_base_disp_s(vcpu);
355 if (operand2 & 0xfff && fc > 0)
356 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
360 vcpu->run->s.regs.gprs[0] = 3 << 28;
361 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
363 case 1: /* same handling for 1 and 2 */
365 mem = get_zeroed_page(GFP_KERNEL);
368 if (stsi((void *) mem, fc, sel1, sel2))
372 if (sel1 != 2 || sel2 != 2)
374 mem = get_zeroed_page(GFP_KERNEL);
377 handle_stsi_3_2_2(vcpu, (void *) mem);
383 if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
384 rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
387 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
389 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
390 vcpu->run->s.regs.gprs[0] = 0;
395 /* condition code 3 */
396 vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
400 static const intercept_handler_t b2_handlers[256] = {
401 [0x02] = handle_stidp,
402 [0x10] = handle_set_prefix,
403 [0x11] = handle_store_prefix,
404 [0x12] = handle_store_cpu_address,
405 [0x29] = handle_skey,
406 [0x2a] = handle_skey,
407 [0x2b] = handle_skey,
408 [0x30] = handle_io_inst,
409 [0x31] = handle_io_inst,
410 [0x32] = handle_io_inst,
411 [0x33] = handle_io_inst,
412 [0x34] = handle_io_inst,
413 [0x35] = handle_io_inst,
414 [0x36] = handle_io_inst,
415 [0x37] = handle_io_inst,
416 [0x38] = handle_io_inst,
417 [0x39] = handle_io_inst,
418 [0x3a] = handle_io_inst,
419 [0x3b] = handle_io_inst,
420 [0x3c] = handle_io_inst,
421 [0x5f] = handle_io_inst,
422 [0x74] = handle_io_inst,
423 [0x76] = handle_io_inst,
424 [0x7d] = handle_stsi,
425 [0xb1] = handle_stfl,
426 [0xb2] = handle_lpswe,
429 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
431 intercept_handler_t handler;
434 * a lot of B2 instructions are priviledged. We first check for
435 * the privileged ones, that we can handle in the kernel. If the
436 * kernel can handle this instruction, we check for the problem
437 * state bit and (a) handle the instruction or (b) send a code 2
439 * Anything else goes to userspace.*/
440 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
442 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
443 return kvm_s390_inject_program_int(vcpu,
444 PGM_PRIVILEGED_OPERATION);
446 return handler(vcpu);
451 static int handle_epsw(struct kvm_vcpu *vcpu)
455 reg1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 24;
456 reg2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
458 /* This basically extracts the mask half of the psw. */
459 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000;
460 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
462 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000;
463 vcpu->run->s.regs.gprs[reg2] |=
464 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffff;
469 static const intercept_handler_t b9_handlers[256] = {
470 [0x8d] = handle_epsw,
471 [0x9c] = handle_io_inst,
474 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
476 intercept_handler_t handler;
478 /* This is handled just as for the B2 instructions. */
479 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
481 if ((handler != handle_epsw) &&
482 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE))
483 return kvm_s390_inject_program_int(vcpu,
484 PGM_PRIVILEGED_OPERATION);
486 return handler(vcpu);
491 static const intercept_handler_t eb_handlers[256] = {
492 [0x8a] = handle_io_inst,
495 int kvm_s390_handle_priv_eb(struct kvm_vcpu *vcpu)
497 intercept_handler_t handler;
499 /* All eb instructions that end up here are privileged. */
500 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
501 return kvm_s390_inject_program_int(vcpu,
502 PGM_PRIVILEGED_OPERATION);
503 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
505 return handler(vcpu);
509 static int handle_tprot(struct kvm_vcpu *vcpu)
511 u64 address1, address2;
512 struct vm_area_struct *vma;
513 unsigned long user_address;
515 vcpu->stat.instruction_tprot++;
517 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
519 /* we only handle the Linux memory detection case:
522 * everything else goes to userspace. */
525 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
528 down_read(¤t->mm->mmap_sem);
529 user_address = __gmap_translate(address1, vcpu->arch.gmap);
530 if (IS_ERR_VALUE(user_address))
532 vma = find_vma(current->mm, user_address);
535 vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
536 if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
537 vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
538 if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
539 vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
541 up_read(¤t->mm->mmap_sem);
545 up_read(¤t->mm->mmap_sem);
546 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
549 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
551 /* For e5xx... instructions we only handle TPROT */
552 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
553 return handle_tprot(vcpu);
557 static int handle_sckpf(struct kvm_vcpu *vcpu)
561 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
562 return kvm_s390_inject_program_int(vcpu,
563 PGM_PRIVILEGED_OPERATION);
565 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
566 return kvm_s390_inject_program_int(vcpu,
569 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
570 vcpu->arch.sie_block->todpr = value;
575 static const intercept_handler_t x01_handlers[256] = {
576 [0x07] = handle_sckpf,
579 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
581 intercept_handler_t handler;
583 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
585 return handler(vcpu);