dccp: do not assume DCCP code is non preemptible
[linux/fpc-iii.git] / arch / s390 / kvm / priv.c
blob0a1591d3d25d07a6c2e98e406f77bb391ebb7c1a
1 /*
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>
26 #include <asm/gmap.h>
27 #include <asm/io.h>
28 #include <asm/ptrace.h>
29 #include <asm/compat.h>
30 #include "gaccess.h"
31 #include "kvm-s390.h"
32 #include "trace.h"
34 /* Handle SCK (SET CLOCK) interception */
35 static int handle_set_clock(struct kvm_vcpu *vcpu)
37 int rc;
38 ar_t ar;
39 u64 op2, val;
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, &ar);
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, ar, &val, sizeof(val));
48 if (rc)
49 return kvm_s390_inject_prog_cond(vcpu, rc);
51 VCPU_EVENT(vcpu, 3, "SCK: setting guest TOD to 0x%llx", val);
52 kvm_s390_set_tod_clock(vcpu->kvm, val);
54 kvm_s390_set_psw_cc(vcpu, 0);
55 return 0;
58 static int handle_set_prefix(struct kvm_vcpu *vcpu)
60 u64 operand2;
61 u32 address;
62 int rc;
63 ar_t ar;
65 vcpu->stat.instruction_spx++;
67 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
68 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
70 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
72 /* must be word boundary */
73 if (operand2 & 3)
74 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
76 /* get the value */
77 rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
78 if (rc)
79 return kvm_s390_inject_prog_cond(vcpu, rc);
81 address &= 0x7fffe000u;
84 * Make sure the new value is valid memory. We only need to check the
85 * first page, since address is 8k aligned and memory pieces are always
86 * at least 1MB aligned and have at least a size of 1MB.
88 if (kvm_is_error_gpa(vcpu->kvm, address))
89 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
91 kvm_s390_set_prefix(vcpu, address);
92 trace_kvm_s390_handle_prefix(vcpu, 1, address);
93 return 0;
96 static int handle_store_prefix(struct kvm_vcpu *vcpu)
98 u64 operand2;
99 u32 address;
100 int rc;
101 ar_t ar;
103 vcpu->stat.instruction_stpx++;
105 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
106 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
108 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
110 /* must be word boundary */
111 if (operand2 & 3)
112 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
114 address = kvm_s390_get_prefix(vcpu);
116 /* get the value */
117 rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
118 if (rc)
119 return kvm_s390_inject_prog_cond(vcpu, rc);
121 VCPU_EVENT(vcpu, 3, "STPX: storing prefix 0x%x into 0x%llx", address, operand2);
122 trace_kvm_s390_handle_prefix(vcpu, 0, address);
123 return 0;
126 static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
128 u16 vcpu_id = vcpu->vcpu_id;
129 u64 ga;
130 int rc;
131 ar_t ar;
133 vcpu->stat.instruction_stap++;
135 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
136 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
138 ga = kvm_s390_get_base_disp_s(vcpu, &ar);
140 if (ga & 1)
141 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
143 rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
144 if (rc)
145 return kvm_s390_inject_prog_cond(vcpu, rc);
147 VCPU_EVENT(vcpu, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id, ga);
148 trace_kvm_s390_handle_stap(vcpu, ga);
149 return 0;
152 static int __skey_check_enable(struct kvm_vcpu *vcpu)
154 int rc = 0;
155 if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
156 return rc;
158 rc = s390_enable_skey();
159 VCPU_EVENT(vcpu, 3, "%s", "enabling storage keys for guest");
160 trace_kvm_s390_skey_related_inst(vcpu);
161 vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
162 return rc;
166 static int handle_skey(struct kvm_vcpu *vcpu)
168 int rc = __skey_check_enable(vcpu);
170 if (rc)
171 return rc;
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 kvm_s390_retry_instr(vcpu);
178 VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
179 return 0;
182 static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
184 vcpu->stat.instruction_ipte_interlock++;
185 if (psw_bits(vcpu->arch.sie_block->gpsw).p)
186 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
187 wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
188 kvm_s390_retry_instr(vcpu);
189 VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
190 return 0;
193 static int handle_test_block(struct kvm_vcpu *vcpu)
195 gpa_t addr;
196 int reg2;
198 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
199 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
201 kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
202 addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
203 addr = kvm_s390_logical_to_effective(vcpu, addr);
204 if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
205 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
206 addr = kvm_s390_real_to_abs(vcpu, addr);
208 if (kvm_is_error_gpa(vcpu->kvm, addr))
209 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
211 * We don't expect errors on modern systems, and do not care
212 * about storage keys (yet), so let's just clear the page.
214 if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
215 return -EFAULT;
216 kvm_s390_set_psw_cc(vcpu, 0);
217 vcpu->run->s.regs.gprs[0] = 0;
218 return 0;
221 static int handle_tpi(struct kvm_vcpu *vcpu)
223 struct kvm_s390_interrupt_info *inti;
224 unsigned long len;
225 u32 tpi_data[3];
226 int rc;
227 u64 addr;
228 ar_t ar;
230 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
231 if (addr & 3)
232 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
234 inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
235 if (!inti) {
236 kvm_s390_set_psw_cc(vcpu, 0);
237 return 0;
240 tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
241 tpi_data[1] = inti->io.io_int_parm;
242 tpi_data[2] = inti->io.io_int_word;
243 if (addr) {
245 * Store the two-word I/O interruption code into the
246 * provided area.
248 len = sizeof(tpi_data) - 4;
249 rc = write_guest(vcpu, addr, ar, &tpi_data, len);
250 if (rc) {
251 rc = kvm_s390_inject_prog_cond(vcpu, rc);
252 goto reinject_interrupt;
254 } else {
256 * Store the three-word I/O interruption code into
257 * the appropriate lowcore area.
259 len = sizeof(tpi_data);
260 if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
261 /* failed writes to the low core are not recoverable */
262 rc = -EFAULT;
263 goto reinject_interrupt;
267 /* irq was successfully handed to the guest */
268 kfree(inti);
269 kvm_s390_set_psw_cc(vcpu, 1);
270 return 0;
271 reinject_interrupt:
273 * If we encounter a problem storing the interruption code, the
274 * instruction is suppressed from the guest's view: reinject the
275 * interrupt.
277 if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
278 kfree(inti);
279 rc = -EFAULT;
281 /* don't set the cc, a pgm irq was injected or we drop to user space */
282 return rc ? -EFAULT : 0;
285 static int handle_tsch(struct kvm_vcpu *vcpu)
287 struct kvm_s390_interrupt_info *inti = NULL;
288 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
290 /* a valid schid has at least one bit set */
291 if (vcpu->run->s.regs.gprs[1])
292 inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
293 vcpu->run->s.regs.gprs[1]);
296 * Prepare exit to userspace.
297 * We indicate whether we dequeued a pending I/O interrupt
298 * so that userspace can re-inject it if the instruction gets
299 * a program check. While this may re-order the pending I/O
300 * interrupts, this is no problem since the priority is kept
301 * intact.
303 vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
304 vcpu->run->s390_tsch.dequeued = !!inti;
305 if (inti) {
306 vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
307 vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
308 vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
309 vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
311 vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
312 kfree(inti);
313 return -EREMOTE;
316 static int handle_io_inst(struct kvm_vcpu *vcpu)
318 VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
320 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
321 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
323 if (vcpu->kvm->arch.css_support) {
325 * Most I/O instructions will be handled by userspace.
326 * Exceptions are tpi and the interrupt portion of tsch.
328 if (vcpu->arch.sie_block->ipa == 0xb236)
329 return handle_tpi(vcpu);
330 if (vcpu->arch.sie_block->ipa == 0xb235)
331 return handle_tsch(vcpu);
332 /* Handle in userspace. */
333 return -EOPNOTSUPP;
334 } else {
336 * Set condition code 3 to stop the guest from issuing channel
337 * I/O instructions.
339 kvm_s390_set_psw_cc(vcpu, 3);
340 return 0;
344 static int handle_stfl(struct kvm_vcpu *vcpu)
346 int rc;
347 unsigned int fac;
349 vcpu->stat.instruction_stfl++;
351 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
352 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
355 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
356 * into a u32 memory representation. They will remain bits 0-31.
358 fac = *vcpu->kvm->arch.model.fac_list >> 32;
359 rc = write_guest_lc(vcpu, offsetof(struct lowcore, stfl_fac_list),
360 &fac, sizeof(fac));
361 if (rc)
362 return rc;
363 VCPU_EVENT(vcpu, 3, "STFL: store facility list 0x%x", fac);
364 trace_kvm_s390_handle_stfl(vcpu, fac);
365 return 0;
368 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
369 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
370 #define PSW_ADDR_24 0x0000000000ffffffUL
371 #define PSW_ADDR_31 0x000000007fffffffUL
373 int is_valid_psw(psw_t *psw)
375 if (psw->mask & PSW_MASK_UNASSIGNED)
376 return 0;
377 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
378 if (psw->addr & ~PSW_ADDR_31)
379 return 0;
381 if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
382 return 0;
383 if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
384 return 0;
385 if (psw->addr & 1)
386 return 0;
387 return 1;
390 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
392 psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
393 psw_compat_t new_psw;
394 u64 addr;
395 int rc;
396 ar_t ar;
398 if (gpsw->mask & PSW_MASK_PSTATE)
399 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
401 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
402 if (addr & 7)
403 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
405 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
406 if (rc)
407 return kvm_s390_inject_prog_cond(vcpu, rc);
408 if (!(new_psw.mask & PSW32_MASK_BASE))
409 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
410 gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
411 gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
412 gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
413 if (!is_valid_psw(gpsw))
414 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
415 return 0;
418 static int handle_lpswe(struct kvm_vcpu *vcpu)
420 psw_t new_psw;
421 u64 addr;
422 int rc;
423 ar_t ar;
425 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
426 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
428 addr = kvm_s390_get_base_disp_s(vcpu, &ar);
429 if (addr & 7)
430 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
431 rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
432 if (rc)
433 return kvm_s390_inject_prog_cond(vcpu, rc);
434 vcpu->arch.sie_block->gpsw = new_psw;
435 if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
436 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
437 return 0;
440 static int handle_stidp(struct kvm_vcpu *vcpu)
442 u64 stidp_data = vcpu->arch.stidp_data;
443 u64 operand2;
444 int rc;
445 ar_t ar;
447 vcpu->stat.instruction_stidp++;
449 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
450 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
452 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
454 if (operand2 & 7)
455 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
457 rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
458 if (rc)
459 return kvm_s390_inject_prog_cond(vcpu, rc);
461 VCPU_EVENT(vcpu, 3, "STIDP: store cpu id 0x%llx", stidp_data);
462 return 0;
465 static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
467 int cpus = 0;
468 int n;
470 cpus = atomic_read(&vcpu->kvm->online_vcpus);
472 /* deal with other level 3 hypervisors */
473 if (stsi(mem, 3, 2, 2))
474 mem->count = 0;
475 if (mem->count < 8)
476 mem->count++;
477 for (n = mem->count - 1; n > 0 ; n--)
478 memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
480 memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
481 mem->vm[0].cpus_total = cpus;
482 mem->vm[0].cpus_configured = cpus;
483 mem->vm[0].cpus_standby = 0;
484 mem->vm[0].cpus_reserved = 0;
485 mem->vm[0].caf = 1000;
486 memcpy(mem->vm[0].name, "KVMguest", 8);
487 ASCEBC(mem->vm[0].name, 8);
488 memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
489 ASCEBC(mem->vm[0].cpi, 16);
492 static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, ar_t ar,
493 u8 fc, u8 sel1, u16 sel2)
495 vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
496 vcpu->run->s390_stsi.addr = addr;
497 vcpu->run->s390_stsi.ar = ar;
498 vcpu->run->s390_stsi.fc = fc;
499 vcpu->run->s390_stsi.sel1 = sel1;
500 vcpu->run->s390_stsi.sel2 = sel2;
503 static int handle_stsi(struct kvm_vcpu *vcpu)
505 int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
506 int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
507 int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
508 unsigned long mem = 0;
509 u64 operand2;
510 int rc = 0;
511 ar_t ar;
513 vcpu->stat.instruction_stsi++;
514 VCPU_EVENT(vcpu, 3, "STSI: fc: %u sel1: %u sel2: %u", fc, sel1, sel2);
516 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
517 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
519 if (fc > 3) {
520 kvm_s390_set_psw_cc(vcpu, 3);
521 return 0;
524 if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
525 || vcpu->run->s.regs.gprs[1] & 0xffff0000)
526 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
528 if (fc == 0) {
529 vcpu->run->s.regs.gprs[0] = 3 << 28;
530 kvm_s390_set_psw_cc(vcpu, 0);
531 return 0;
534 operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
536 if (operand2 & 0xfff)
537 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
539 switch (fc) {
540 case 1: /* same handling for 1 and 2 */
541 case 2:
542 mem = get_zeroed_page(GFP_KERNEL);
543 if (!mem)
544 goto out_no_data;
545 if (stsi((void *) mem, fc, sel1, sel2))
546 goto out_no_data;
547 break;
548 case 3:
549 if (sel1 != 2 || sel2 != 2)
550 goto out_no_data;
551 mem = get_zeroed_page(GFP_KERNEL);
552 if (!mem)
553 goto out_no_data;
554 handle_stsi_3_2_2(vcpu, (void *) mem);
555 break;
558 rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
559 if (rc) {
560 rc = kvm_s390_inject_prog_cond(vcpu, rc);
561 goto out;
563 if (vcpu->kvm->arch.user_stsi) {
564 insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
565 rc = -EREMOTE;
567 trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
568 free_page(mem);
569 kvm_s390_set_psw_cc(vcpu, 0);
570 vcpu->run->s.regs.gprs[0] = 0;
571 return rc;
572 out_no_data:
573 kvm_s390_set_psw_cc(vcpu, 3);
574 out:
575 free_page(mem);
576 return rc;
579 static const intercept_handler_t b2_handlers[256] = {
580 [0x02] = handle_stidp,
581 [0x04] = handle_set_clock,
582 [0x10] = handle_set_prefix,
583 [0x11] = handle_store_prefix,
584 [0x12] = handle_store_cpu_address,
585 [0x21] = handle_ipte_interlock,
586 [0x29] = handle_skey,
587 [0x2a] = handle_skey,
588 [0x2b] = handle_skey,
589 [0x2c] = handle_test_block,
590 [0x30] = handle_io_inst,
591 [0x31] = handle_io_inst,
592 [0x32] = handle_io_inst,
593 [0x33] = handle_io_inst,
594 [0x34] = handle_io_inst,
595 [0x35] = handle_io_inst,
596 [0x36] = handle_io_inst,
597 [0x37] = handle_io_inst,
598 [0x38] = handle_io_inst,
599 [0x39] = handle_io_inst,
600 [0x3a] = handle_io_inst,
601 [0x3b] = handle_io_inst,
602 [0x3c] = handle_io_inst,
603 [0x50] = handle_ipte_interlock,
604 [0x5f] = handle_io_inst,
605 [0x74] = handle_io_inst,
606 [0x76] = handle_io_inst,
607 [0x7d] = handle_stsi,
608 [0xb1] = handle_stfl,
609 [0xb2] = handle_lpswe,
612 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
614 intercept_handler_t handler;
617 * A lot of B2 instructions are priviledged. Here we check for
618 * the privileged ones, that we can handle in the kernel.
619 * Anything else goes to userspace.
621 handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
622 if (handler)
623 return handler(vcpu);
625 return -EOPNOTSUPP;
628 static int handle_epsw(struct kvm_vcpu *vcpu)
630 int reg1, reg2;
632 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
634 /* This basically extracts the mask half of the psw. */
635 vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
636 vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
637 if (reg2) {
638 vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
639 vcpu->run->s.regs.gprs[reg2] |=
640 vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
642 return 0;
645 #define PFMF_RESERVED 0xfffc0101UL
646 #define PFMF_SK 0x00020000UL
647 #define PFMF_CF 0x00010000UL
648 #define PFMF_UI 0x00008000UL
649 #define PFMF_FSC 0x00007000UL
650 #define PFMF_NQ 0x00000800UL
651 #define PFMF_MR 0x00000400UL
652 #define PFMF_MC 0x00000200UL
653 #define PFMF_KEY 0x000000feUL
655 static int handle_pfmf(struct kvm_vcpu *vcpu)
657 int reg1, reg2;
658 unsigned long start, end;
660 vcpu->stat.instruction_pfmf++;
662 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
664 if (!test_kvm_facility(vcpu->kvm, 8))
665 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
667 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
668 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
670 if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
671 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
673 /* Only provide non-quiescing support if the host supports it */
674 if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
675 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
677 /* No support for conditional-SSKE */
678 if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
679 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
681 start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
682 start = kvm_s390_logical_to_effective(vcpu, start);
684 switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
685 case 0x00000000:
686 end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
687 break;
688 case 0x00001000:
689 end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
690 break;
691 case 0x00002000:
692 /* only support 2G frame size if EDAT2 is available and we are
693 not in 24-bit addressing mode */
694 if (!test_kvm_facility(vcpu->kvm, 78) ||
695 psw_bits(vcpu->arch.sie_block->gpsw).eaba == PSW_AMODE_24BIT)
696 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
697 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
698 break;
699 default:
700 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
703 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
704 if (kvm_s390_check_low_addr_prot_real(vcpu, start))
705 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
708 while (start < end) {
709 unsigned long useraddr, abs_addr;
711 /* Translate guest address to host address */
712 if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
713 abs_addr = kvm_s390_real_to_abs(vcpu, start);
714 else
715 abs_addr = start;
716 useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
717 if (kvm_is_error_hva(useraddr))
718 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
720 if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
721 if (clear_user((void __user *)useraddr, PAGE_SIZE))
722 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
725 if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
726 int rc = __skey_check_enable(vcpu);
728 if (rc)
729 return rc;
730 if (set_guest_storage_key(current->mm, useraddr,
731 vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
732 vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
733 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
736 start += PAGE_SIZE;
738 if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
739 vcpu->run->s.regs.gprs[reg2] = end;
740 return 0;
743 static int handle_essa(struct kvm_vcpu *vcpu)
745 /* entries expected to be 1FF */
746 int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
747 unsigned long *cbrlo, cbrle;
748 struct gmap *gmap;
749 int i;
751 VCPU_EVENT(vcpu, 4, "ESSA: release %d pages", entries);
752 gmap = vcpu->arch.gmap;
753 vcpu->stat.instruction_essa++;
754 if (!vcpu->kvm->arch.use_cmma)
755 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
757 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
758 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
760 if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
761 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
763 /* Retry the ESSA instruction */
764 kvm_s390_retry_instr(vcpu);
765 vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
766 cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
767 down_read(&gmap->mm->mmap_sem);
768 for (i = 0; i < entries; ++i) {
769 cbrle = cbrlo[i];
770 if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
771 /* invalid entry */
772 break;
773 /* try to free backing */
774 __gmap_zap(gmap, cbrle);
776 up_read(&gmap->mm->mmap_sem);
777 if (i < entries)
778 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
779 return 0;
782 static const intercept_handler_t b9_handlers[256] = {
783 [0x8a] = handle_ipte_interlock,
784 [0x8d] = handle_epsw,
785 [0x8e] = handle_ipte_interlock,
786 [0x8f] = handle_ipte_interlock,
787 [0xab] = handle_essa,
788 [0xaf] = handle_pfmf,
791 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
793 intercept_handler_t handler;
795 /* This is handled just as for the B2 instructions. */
796 handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
797 if (handler)
798 return handler(vcpu);
800 return -EOPNOTSUPP;
803 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
805 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
806 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
807 int reg, rc, nr_regs;
808 u32 ctl_array[16];
809 u64 ga;
810 ar_t ar;
812 vcpu->stat.instruction_lctl++;
814 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
815 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
817 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
819 if (ga & 3)
820 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
822 VCPU_EVENT(vcpu, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
823 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
825 nr_regs = ((reg3 - reg1) & 0xf) + 1;
826 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
827 if (rc)
828 return kvm_s390_inject_prog_cond(vcpu, rc);
829 reg = reg1;
830 nr_regs = 0;
831 do {
832 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
833 vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
834 if (reg == reg3)
835 break;
836 reg = (reg + 1) % 16;
837 } while (1);
838 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
839 return 0;
842 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
844 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
845 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
846 int reg, rc, nr_regs;
847 u32 ctl_array[16];
848 u64 ga;
849 ar_t ar;
851 vcpu->stat.instruction_stctl++;
853 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
854 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
856 ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
858 if (ga & 3)
859 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
861 VCPU_EVENT(vcpu, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
862 trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
864 reg = reg1;
865 nr_regs = 0;
866 do {
867 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
868 if (reg == reg3)
869 break;
870 reg = (reg + 1) % 16;
871 } while (1);
872 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
873 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
876 static int handle_lctlg(struct kvm_vcpu *vcpu)
878 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
879 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
880 int reg, rc, nr_regs;
881 u64 ctl_array[16];
882 u64 ga;
883 ar_t ar;
885 vcpu->stat.instruction_lctlg++;
887 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
888 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
890 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
892 if (ga & 7)
893 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
895 VCPU_EVENT(vcpu, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
896 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
898 nr_regs = ((reg3 - reg1) & 0xf) + 1;
899 rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
900 if (rc)
901 return kvm_s390_inject_prog_cond(vcpu, rc);
902 reg = reg1;
903 nr_regs = 0;
904 do {
905 vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
906 if (reg == reg3)
907 break;
908 reg = (reg + 1) % 16;
909 } while (1);
910 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
911 return 0;
914 static int handle_stctg(struct kvm_vcpu *vcpu)
916 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
917 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
918 int reg, rc, nr_regs;
919 u64 ctl_array[16];
920 u64 ga;
921 ar_t ar;
923 vcpu->stat.instruction_stctg++;
925 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
926 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
928 ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
930 if (ga & 7)
931 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
933 VCPU_EVENT(vcpu, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1, reg3, ga);
934 trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
936 reg = reg1;
937 nr_regs = 0;
938 do {
939 ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
940 if (reg == reg3)
941 break;
942 reg = (reg + 1) % 16;
943 } while (1);
944 rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
945 return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
948 static const intercept_handler_t eb_handlers[256] = {
949 [0x2f] = handle_lctlg,
950 [0x25] = handle_stctg,
953 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
955 intercept_handler_t handler;
957 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
958 if (handler)
959 return handler(vcpu);
960 return -EOPNOTSUPP;
963 static int handle_tprot(struct kvm_vcpu *vcpu)
965 u64 address1, address2;
966 unsigned long hva, gpa;
967 int ret = 0, cc = 0;
968 bool writable;
969 ar_t ar;
971 vcpu->stat.instruction_tprot++;
973 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
974 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
976 kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
978 /* we only handle the Linux memory detection case:
979 * access key == 0
980 * everything else goes to userspace. */
981 if (address2 & 0xf0)
982 return -EOPNOTSUPP;
983 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
984 ipte_lock(vcpu);
985 ret = guest_translate_address(vcpu, address1, ar, &gpa, GACC_STORE);
986 if (ret == PGM_PROTECTION) {
987 /* Write protected? Try again with read-only... */
988 cc = 1;
989 ret = guest_translate_address(vcpu, address1, ar, &gpa,
990 GACC_FETCH);
992 if (ret) {
993 if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
994 ret = kvm_s390_inject_program_int(vcpu, ret);
995 } else if (ret > 0) {
996 /* Translation not available */
997 kvm_s390_set_psw_cc(vcpu, 3);
998 ret = 0;
1000 goto out_unlock;
1003 hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
1004 if (kvm_is_error_hva(hva)) {
1005 ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1006 } else {
1007 if (!writable)
1008 cc = 1; /* Write not permitted ==> read-only */
1009 kvm_s390_set_psw_cc(vcpu, cc);
1010 /* Note: CC2 only occurs for storage keys (not supported yet) */
1012 out_unlock:
1013 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
1014 ipte_unlock(vcpu);
1015 return ret;
1018 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
1020 /* For e5xx... instructions we only handle TPROT */
1021 if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
1022 return handle_tprot(vcpu);
1023 return -EOPNOTSUPP;
1026 static int handle_sckpf(struct kvm_vcpu *vcpu)
1028 u32 value;
1030 if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
1031 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
1033 if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
1034 return kvm_s390_inject_program_int(vcpu,
1035 PGM_SPECIFICATION);
1037 value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
1038 vcpu->arch.sie_block->todpr = value;
1040 return 0;
1043 static const intercept_handler_t x01_handlers[256] = {
1044 [0x07] = handle_sckpf,
1047 int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
1049 intercept_handler_t handler;
1051 handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
1052 if (handler)
1053 return handler(vcpu);
1054 return -EOPNOTSUPP;