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
;
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
);
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
, &val
, sizeof(val
));
49 return kvm_s390_inject_prog_cond(vcpu
, rc
);
51 if (store_tod_clock(&hostclk
)) {
52 kvm_s390_set_psw_cc(vcpu
, 3);
55 val
= (val
- hostclk
) & ~0x3fUL
;
57 mutex_lock(&vcpu
->kvm
->lock
);
58 kvm_for_each_vcpu(i
, cpup
, vcpu
->kvm
)
59 cpup
->arch
.sie_block
->epoch
= val
;
60 mutex_unlock(&vcpu
->kvm
->lock
);
62 kvm_s390_set_psw_cc(vcpu
, 0);
66 static int handle_set_prefix(struct kvm_vcpu
*vcpu
)
72 vcpu
->stat
.instruction_spx
++;
74 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
75 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
77 operand2
= kvm_s390_get_base_disp_s(vcpu
);
79 /* must be word boundary */
81 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
84 rc
= read_guest(vcpu
, operand2
, &address
, sizeof(address
));
86 return kvm_s390_inject_prog_cond(vcpu
, rc
);
88 address
&= 0x7fffe000u
;
91 * Make sure the new value is valid memory. We only need to check the
92 * first page, since address is 8k aligned and memory pieces are always
93 * at least 1MB aligned and have at least a size of 1MB.
95 if (kvm_is_error_gpa(vcpu
->kvm
, address
))
96 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
98 kvm_s390_set_prefix(vcpu
, address
);
100 VCPU_EVENT(vcpu
, 5, "setting prefix to %x", address
);
101 trace_kvm_s390_handle_prefix(vcpu
, 1, address
);
105 static int handle_store_prefix(struct kvm_vcpu
*vcpu
)
111 vcpu
->stat
.instruction_stpx
++;
113 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
114 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
116 operand2
= kvm_s390_get_base_disp_s(vcpu
);
118 /* must be word boundary */
120 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
122 address
= kvm_s390_get_prefix(vcpu
);
125 rc
= write_guest(vcpu
, operand2
, &address
, sizeof(address
));
127 return kvm_s390_inject_prog_cond(vcpu
, rc
);
129 VCPU_EVENT(vcpu
, 5, "storing prefix to %x", address
);
130 trace_kvm_s390_handle_prefix(vcpu
, 0, address
);
134 static int handle_store_cpu_address(struct kvm_vcpu
*vcpu
)
136 u16 vcpu_id
= vcpu
->vcpu_id
;
140 vcpu
->stat
.instruction_stap
++;
142 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
143 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
145 ga
= kvm_s390_get_base_disp_s(vcpu
);
148 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
150 rc
= write_guest(vcpu
, ga
, &vcpu_id
, sizeof(vcpu_id
));
152 return kvm_s390_inject_prog_cond(vcpu
, rc
);
154 VCPU_EVENT(vcpu
, 5, "storing cpu address to %llx", ga
);
155 trace_kvm_s390_handle_stap(vcpu
, ga
);
159 static void __skey_check_enable(struct kvm_vcpu
*vcpu
)
161 if (!(vcpu
->arch
.sie_block
->ictl
& (ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
)))
165 trace_kvm_s390_skey_related_inst(vcpu
);
166 vcpu
->arch
.sie_block
->ictl
&= ~(ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
);
170 static int handle_skey(struct kvm_vcpu
*vcpu
)
172 __skey_check_enable(vcpu
);
174 vcpu
->stat
.instruction_storage_key
++;
176 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
177 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
179 vcpu
->arch
.sie_block
->gpsw
.addr
=
180 __rewind_psw(vcpu
->arch
.sie_block
->gpsw
, 4);
181 VCPU_EVENT(vcpu
, 4, "%s", "retrying storage key operation");
185 static int handle_ipte_interlock(struct kvm_vcpu
*vcpu
)
187 psw_t
*psw
= &vcpu
->arch
.sie_block
->gpsw
;
189 vcpu
->stat
.instruction_ipte_interlock
++;
190 if (psw_bits(*psw
).p
)
191 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
192 wait_event(vcpu
->kvm
->arch
.ipte_wq
, !ipte_lock_held(vcpu
));
193 psw
->addr
= __rewind_psw(*psw
, 4);
194 VCPU_EVENT(vcpu
, 4, "%s", "retrying ipte interlock operation");
198 static int handle_test_block(struct kvm_vcpu
*vcpu
)
203 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
204 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
206 kvm_s390_get_regs_rre(vcpu
, NULL
, ®2
);
207 addr
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
208 addr
= kvm_s390_logical_to_effective(vcpu
, addr
);
209 if (kvm_s390_check_low_addr_protection(vcpu
, addr
))
210 return kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
211 addr
= kvm_s390_real_to_abs(vcpu
, addr
);
213 if (kvm_is_error_gpa(vcpu
->kvm
, addr
))
214 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
216 * We don't expect errors on modern systems, and do not care
217 * about storage keys (yet), so let's just clear the page.
219 if (kvm_clear_guest(vcpu
->kvm
, addr
, PAGE_SIZE
))
221 kvm_s390_set_psw_cc(vcpu
, 0);
222 vcpu
->run
->s
.regs
.gprs
[0] = 0;
226 static int handle_tpi(struct kvm_vcpu
*vcpu
)
228 struct kvm_s390_interrupt_info
*inti
;
235 addr
= kvm_s390_get_base_disp_s(vcpu
);
237 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
239 inti
= kvm_s390_get_io_int(vcpu
->kvm
, vcpu
->arch
.sie_block
->gcr
[6], 0);
243 tpi_data
[0] = inti
->io
.subchannel_id
<< 16 | inti
->io
.subchannel_nr
;
244 tpi_data
[1] = inti
->io
.io_int_parm
;
245 tpi_data
[2] = inti
->io
.io_int_word
;
248 * Store the two-word I/O interruption code into the
251 len
= sizeof(tpi_data
) - 4;
252 rc
= write_guest(vcpu
, addr
, &tpi_data
, len
);
254 return kvm_s390_inject_prog_cond(vcpu
, rc
);
257 * Store the three-word I/O interruption code into
258 * the appropriate lowcore area.
260 len
= sizeof(tpi_data
);
261 if (write_guest_lc(vcpu
, __LC_SUBCHANNEL_ID
, &tpi_data
, len
))
265 * If we encounter a problem storing the interruption code, the
266 * instruction is suppressed from the guest's view: reinject the
272 kvm_s390_reinject_io_int(vcpu
->kvm
, inti
);
274 /* Set condition code and we're done. */
276 kvm_s390_set_psw_cc(vcpu
, cc
);
277 return rc
? -EFAULT
: 0;
280 static int handle_tsch(struct kvm_vcpu
*vcpu
)
282 struct kvm_s390_interrupt_info
*inti
;
284 inti
= kvm_s390_get_io_int(vcpu
->kvm
, 0,
285 vcpu
->run
->s
.regs
.gprs
[1]);
288 * Prepare exit to userspace.
289 * We indicate whether we dequeued a pending I/O interrupt
290 * so that userspace can re-inject it if the instruction gets
291 * a program check. While this may re-order the pending I/O
292 * interrupts, this is no problem since the priority is kept
295 vcpu
->run
->exit_reason
= KVM_EXIT_S390_TSCH
;
296 vcpu
->run
->s390_tsch
.dequeued
= !!inti
;
298 vcpu
->run
->s390_tsch
.subchannel_id
= inti
->io
.subchannel_id
;
299 vcpu
->run
->s390_tsch
.subchannel_nr
= inti
->io
.subchannel_nr
;
300 vcpu
->run
->s390_tsch
.io_int_parm
= inti
->io
.io_int_parm
;
301 vcpu
->run
->s390_tsch
.io_int_word
= inti
->io
.io_int_word
;
303 vcpu
->run
->s390_tsch
.ipb
= vcpu
->arch
.sie_block
->ipb
;
308 static int handle_io_inst(struct kvm_vcpu
*vcpu
)
310 VCPU_EVENT(vcpu
, 4, "%s", "I/O instruction");
312 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
313 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
315 if (vcpu
->kvm
->arch
.css_support
) {
317 * Most I/O instructions will be handled by userspace.
318 * Exceptions are tpi and the interrupt portion of tsch.
320 if (vcpu
->arch
.sie_block
->ipa
== 0xb236)
321 return handle_tpi(vcpu
);
322 if (vcpu
->arch
.sie_block
->ipa
== 0xb235)
323 return handle_tsch(vcpu
);
324 /* Handle in userspace. */
328 * Set condition code 3 to stop the guest from issuing channel
331 kvm_s390_set_psw_cc(vcpu
, 3);
336 static int handle_stfl(struct kvm_vcpu
*vcpu
)
340 vcpu
->stat
.instruction_stfl
++;
342 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
343 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
345 rc
= write_guest_lc(vcpu
, offsetof(struct _lowcore
, stfl_fac_list
),
349 VCPU_EVENT(vcpu
, 5, "store facility list value %x",
350 *(unsigned int *) vfacilities
);
351 trace_kvm_s390_handle_stfl(vcpu
, *(unsigned int *) vfacilities
);
355 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
356 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
357 #define PSW_ADDR_24 0x0000000000ffffffUL
358 #define PSW_ADDR_31 0x000000007fffffffUL
360 int is_valid_psw(psw_t
*psw
)
362 if (psw
->mask
& PSW_MASK_UNASSIGNED
)
364 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_BA
) {
365 if (psw
->addr
& ~PSW_ADDR_31
)
368 if (!(psw
->mask
& PSW_MASK_ADDR_MODE
) && (psw
->addr
& ~PSW_ADDR_24
))
370 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_EA
)
377 int kvm_s390_handle_lpsw(struct kvm_vcpu
*vcpu
)
379 psw_t
*gpsw
= &vcpu
->arch
.sie_block
->gpsw
;
380 psw_compat_t new_psw
;
384 if (gpsw
->mask
& PSW_MASK_PSTATE
)
385 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
387 addr
= kvm_s390_get_base_disp_s(vcpu
);
389 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
391 rc
= read_guest(vcpu
, addr
, &new_psw
, sizeof(new_psw
));
393 return kvm_s390_inject_prog_cond(vcpu
, rc
);
394 if (!(new_psw
.mask
& PSW32_MASK_BASE
))
395 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
396 gpsw
->mask
= (new_psw
.mask
& ~PSW32_MASK_BASE
) << 32;
397 gpsw
->mask
|= new_psw
.addr
& PSW32_ADDR_AMODE
;
398 gpsw
->addr
= new_psw
.addr
& ~PSW32_ADDR_AMODE
;
399 if (!is_valid_psw(gpsw
))
400 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
404 static int handle_lpswe(struct kvm_vcpu
*vcpu
)
410 if (vcpu
->arch
.sie_block
->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
);
415 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
416 rc
= read_guest(vcpu
, addr
, &new_psw
, sizeof(new_psw
));
418 return kvm_s390_inject_prog_cond(vcpu
, rc
);
419 vcpu
->arch
.sie_block
->gpsw
= new_psw
;
420 if (!is_valid_psw(&vcpu
->arch
.sie_block
->gpsw
))
421 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
425 static int handle_stidp(struct kvm_vcpu
*vcpu
)
427 u64 stidp_data
= vcpu
->arch
.stidp_data
;
431 vcpu
->stat
.instruction_stidp
++;
433 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
434 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
436 operand2
= kvm_s390_get_base_disp_s(vcpu
);
439 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
441 rc
= write_guest(vcpu
, operand2
, &stidp_data
, sizeof(stidp_data
));
443 return kvm_s390_inject_prog_cond(vcpu
, rc
);
445 VCPU_EVENT(vcpu
, 5, "%s", "store cpu id");
449 static void handle_stsi_3_2_2(struct kvm_vcpu
*vcpu
, struct sysinfo_3_2_2
*mem
)
454 cpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
456 /* deal with other level 3 hypervisors */
457 if (stsi(mem
, 3, 2, 2))
461 for (n
= mem
->count
- 1; n
> 0 ; n
--)
462 memcpy(&mem
->vm
[n
], &mem
->vm
[n
- 1], sizeof(mem
->vm
[0]));
464 mem
->vm
[0].cpus_total
= cpus
;
465 mem
->vm
[0].cpus_configured
= cpus
;
466 mem
->vm
[0].cpus_standby
= 0;
467 mem
->vm
[0].cpus_reserved
= 0;
468 mem
->vm
[0].caf
= 1000;
469 memcpy(mem
->vm
[0].name
, "KVMguest", 8);
470 ASCEBC(mem
->vm
[0].name
, 8);
471 memcpy(mem
->vm
[0].cpi
, "KVM/Linux ", 16);
472 ASCEBC(mem
->vm
[0].cpi
, 16);
475 static int handle_stsi(struct kvm_vcpu
*vcpu
)
477 int fc
= (vcpu
->run
->s
.regs
.gprs
[0] & 0xf0000000) >> 28;
478 int sel1
= vcpu
->run
->s
.regs
.gprs
[0] & 0xff;
479 int sel2
= vcpu
->run
->s
.regs
.gprs
[1] & 0xffff;
480 unsigned long mem
= 0;
484 vcpu
->stat
.instruction_stsi
++;
485 VCPU_EVENT(vcpu
, 4, "stsi: fc: %x sel1: %x sel2: %x", fc
, sel1
, sel2
);
487 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
488 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
491 kvm_s390_set_psw_cc(vcpu
, 3);
495 if (vcpu
->run
->s
.regs
.gprs
[0] & 0x0fffff00
496 || vcpu
->run
->s
.regs
.gprs
[1] & 0xffff0000)
497 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
500 vcpu
->run
->s
.regs
.gprs
[0] = 3 << 28;
501 kvm_s390_set_psw_cc(vcpu
, 0);
505 operand2
= kvm_s390_get_base_disp_s(vcpu
);
507 if (operand2
& 0xfff)
508 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
511 case 1: /* same handling for 1 and 2 */
513 mem
= get_zeroed_page(GFP_KERNEL
);
516 if (stsi((void *) mem
, fc
, sel1
, sel2
))
520 if (sel1
!= 2 || sel2
!= 2)
522 mem
= get_zeroed_page(GFP_KERNEL
);
525 handle_stsi_3_2_2(vcpu
, (void *) mem
);
529 rc
= write_guest(vcpu
, operand2
, (void *)mem
, PAGE_SIZE
);
531 rc
= kvm_s390_inject_prog_cond(vcpu
, rc
);
534 trace_kvm_s390_handle_stsi(vcpu
, fc
, sel1
, sel2
, operand2
);
536 kvm_s390_set_psw_cc(vcpu
, 0);
537 vcpu
->run
->s
.regs
.gprs
[0] = 0;
540 kvm_s390_set_psw_cc(vcpu
, 3);
546 static const intercept_handler_t b2_handlers
[256] = {
547 [0x02] = handle_stidp
,
548 [0x04] = handle_set_clock
,
549 [0x10] = handle_set_prefix
,
550 [0x11] = handle_store_prefix
,
551 [0x12] = handle_store_cpu_address
,
552 [0x21] = handle_ipte_interlock
,
553 [0x29] = handle_skey
,
554 [0x2a] = handle_skey
,
555 [0x2b] = handle_skey
,
556 [0x2c] = handle_test_block
,
557 [0x30] = handle_io_inst
,
558 [0x31] = handle_io_inst
,
559 [0x32] = handle_io_inst
,
560 [0x33] = handle_io_inst
,
561 [0x34] = handle_io_inst
,
562 [0x35] = handle_io_inst
,
563 [0x36] = handle_io_inst
,
564 [0x37] = handle_io_inst
,
565 [0x38] = handle_io_inst
,
566 [0x39] = handle_io_inst
,
567 [0x3a] = handle_io_inst
,
568 [0x3b] = handle_io_inst
,
569 [0x3c] = handle_io_inst
,
570 [0x50] = handle_ipte_interlock
,
571 [0x5f] = handle_io_inst
,
572 [0x74] = handle_io_inst
,
573 [0x76] = handle_io_inst
,
574 [0x7d] = handle_stsi
,
575 [0xb1] = handle_stfl
,
576 [0xb2] = handle_lpswe
,
579 int kvm_s390_handle_b2(struct kvm_vcpu
*vcpu
)
581 intercept_handler_t handler
;
584 * A lot of B2 instructions are priviledged. Here we check for
585 * the privileged ones, that we can handle in the kernel.
586 * Anything else goes to userspace.
588 handler
= b2_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
590 return handler(vcpu
);
595 static int handle_epsw(struct kvm_vcpu
*vcpu
)
599 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
601 /* This basically extracts the mask half of the psw. */
602 vcpu
->run
->s
.regs
.gprs
[reg1
] &= 0xffffffff00000000UL
;
603 vcpu
->run
->s
.regs
.gprs
[reg1
] |= vcpu
->arch
.sie_block
->gpsw
.mask
>> 32;
605 vcpu
->run
->s
.regs
.gprs
[reg2
] &= 0xffffffff00000000UL
;
606 vcpu
->run
->s
.regs
.gprs
[reg2
] |=
607 vcpu
->arch
.sie_block
->gpsw
.mask
& 0x00000000ffffffffUL
;
612 #define PFMF_RESERVED 0xfffc0101UL
613 #define PFMF_SK 0x00020000UL
614 #define PFMF_CF 0x00010000UL
615 #define PFMF_UI 0x00008000UL
616 #define PFMF_FSC 0x00007000UL
617 #define PFMF_NQ 0x00000800UL
618 #define PFMF_MR 0x00000400UL
619 #define PFMF_MC 0x00000200UL
620 #define PFMF_KEY 0x000000feUL
622 static int handle_pfmf(struct kvm_vcpu
*vcpu
)
625 unsigned long start
, end
;
627 vcpu
->stat
.instruction_pfmf
++;
629 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
631 if (!MACHINE_HAS_PFMF
)
632 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
634 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
635 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
637 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_RESERVED
)
638 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
640 /* Only provide non-quiescing support if the host supports it */
641 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
&& !test_facility(14))
642 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
644 /* No support for conditional-SSKE */
645 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & (PFMF_MR
| PFMF_MC
))
646 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
648 start
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
649 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
650 if (kvm_s390_check_low_addr_protection(vcpu
, start
))
651 return kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
654 switch (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) {
656 end
= (start
+ (1UL << 12)) & ~((1UL << 12) - 1);
659 end
= (start
+ (1UL << 20)) & ~((1UL << 20) - 1);
661 /* We dont support EDAT2
663 end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
666 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
668 while (start
< end
) {
669 unsigned long useraddr
, abs_addr
;
671 /* Translate guest address to host address */
672 if ((vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) == 0)
673 abs_addr
= kvm_s390_real_to_abs(vcpu
, start
);
676 useraddr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(abs_addr
));
677 if (kvm_is_error_hva(useraddr
))
678 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
680 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
681 if (clear_user((void __user
*)useraddr
, PAGE_SIZE
))
682 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
685 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_SK
) {
686 __skey_check_enable(vcpu
);
687 if (set_guest_storage_key(current
->mm
, useraddr
,
688 vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_KEY
,
689 vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
))
690 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
695 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
)
696 vcpu
->run
->s
.regs
.gprs
[reg2
] = end
;
700 static int handle_essa(struct kvm_vcpu
*vcpu
)
702 /* entries expected to be 1FF */
703 int entries
= (vcpu
->arch
.sie_block
->cbrlo
& ~PAGE_MASK
) >> 3;
704 unsigned long *cbrlo
, cbrle
;
708 VCPU_EVENT(vcpu
, 5, "cmma release %d pages", entries
);
709 gmap
= vcpu
->arch
.gmap
;
710 vcpu
->stat
.instruction_essa
++;
711 if (!kvm_s390_cmma_enabled(vcpu
->kvm
))
712 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
714 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
715 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
717 if (((vcpu
->arch
.sie_block
->ipb
& 0xf0000000) >> 28) > 6)
718 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
720 /* Rewind PSW to repeat the ESSA instruction */
721 vcpu
->arch
.sie_block
->gpsw
.addr
=
722 __rewind_psw(vcpu
->arch
.sie_block
->gpsw
, 4);
723 vcpu
->arch
.sie_block
->cbrlo
&= PAGE_MASK
; /* reset nceo */
724 cbrlo
= phys_to_virt(vcpu
->arch
.sie_block
->cbrlo
);
725 down_read(&gmap
->mm
->mmap_sem
);
726 for (i
= 0; i
< entries
; ++i
) {
728 if (unlikely(cbrle
& ~PAGE_MASK
|| cbrle
< 2 * PAGE_SIZE
))
731 /* try to free backing */
732 __gmap_zap(gmap
, cbrle
);
734 up_read(&gmap
->mm
->mmap_sem
);
736 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
740 static const intercept_handler_t b9_handlers
[256] = {
741 [0x8a] = handle_ipte_interlock
,
742 [0x8d] = handle_epsw
,
743 [0x8e] = handle_ipte_interlock
,
744 [0x8f] = handle_ipte_interlock
,
745 [0xab] = handle_essa
,
746 [0xaf] = handle_pfmf
,
749 int kvm_s390_handle_b9(struct kvm_vcpu
*vcpu
)
751 intercept_handler_t handler
;
753 /* This is handled just as for the B2 instructions. */
754 handler
= b9_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
756 return handler(vcpu
);
761 int kvm_s390_handle_lctl(struct kvm_vcpu
*vcpu
)
763 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
764 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
769 vcpu
->stat
.instruction_lctl
++;
771 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
772 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
774 ga
= kvm_s390_get_base_disp_rs(vcpu
);
777 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
779 VCPU_EVENT(vcpu
, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1
, reg3
, ga
);
780 trace_kvm_s390_handle_lctl(vcpu
, 0, reg1
, reg3
, ga
);
784 rc
= read_guest(vcpu
, ga
, &val
, sizeof(val
));
786 return kvm_s390_inject_prog_cond(vcpu
, rc
);
787 vcpu
->arch
.sie_block
->gcr
[reg
] &= 0xffffffff00000000ul
;
788 vcpu
->arch
.sie_block
->gcr
[reg
] |= val
;
792 reg
= (reg
+ 1) % 16;
798 int kvm_s390_handle_stctl(struct kvm_vcpu
*vcpu
)
800 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
801 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
806 vcpu
->stat
.instruction_stctl
++;
808 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
809 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
811 ga
= kvm_s390_get_base_disp_rs(vcpu
);
814 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
816 VCPU_EVENT(vcpu
, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1
, reg3
, ga
);
817 trace_kvm_s390_handle_stctl(vcpu
, 0, reg1
, reg3
, ga
);
821 val
= vcpu
->arch
.sie_block
->gcr
[reg
] & 0x00000000fffffffful
;
822 rc
= write_guest(vcpu
, ga
, &val
, sizeof(val
));
824 return kvm_s390_inject_prog_cond(vcpu
, rc
);
828 reg
= (reg
+ 1) % 16;
834 static int handle_lctlg(struct kvm_vcpu
*vcpu
)
836 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
837 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
841 vcpu
->stat
.instruction_lctlg
++;
843 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
844 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
846 ga
= kvm_s390_get_base_disp_rsy(vcpu
);
849 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
853 VCPU_EVENT(vcpu
, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1
, reg3
, ga
);
854 trace_kvm_s390_handle_lctl(vcpu
, 1, reg1
, reg3
, ga
);
857 rc
= read_guest(vcpu
, ga
, &val
, sizeof(val
));
859 return kvm_s390_inject_prog_cond(vcpu
, rc
);
860 vcpu
->arch
.sie_block
->gcr
[reg
] = val
;
864 reg
= (reg
+ 1) % 16;
870 static int handle_stctg(struct kvm_vcpu
*vcpu
)
872 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
873 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
877 vcpu
->stat
.instruction_stctg
++;
879 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
880 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
882 ga
= kvm_s390_get_base_disp_rsy(vcpu
);
885 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
889 VCPU_EVENT(vcpu
, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1
, reg3
, ga
);
890 trace_kvm_s390_handle_stctl(vcpu
, 1, reg1
, reg3
, ga
);
893 val
= vcpu
->arch
.sie_block
->gcr
[reg
];
894 rc
= write_guest(vcpu
, ga
, &val
, sizeof(val
));
896 return kvm_s390_inject_prog_cond(vcpu
, rc
);
900 reg
= (reg
+ 1) % 16;
906 static const intercept_handler_t eb_handlers
[256] = {
907 [0x2f] = handle_lctlg
,
908 [0x25] = handle_stctg
,
911 int kvm_s390_handle_eb(struct kvm_vcpu
*vcpu
)
913 intercept_handler_t handler
;
915 handler
= eb_handlers
[vcpu
->arch
.sie_block
->ipb
& 0xff];
917 return handler(vcpu
);
921 static int handle_tprot(struct kvm_vcpu
*vcpu
)
923 u64 address1
, address2
;
924 unsigned long hva
, gpa
;
928 vcpu
->stat
.instruction_tprot
++;
930 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
931 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
933 kvm_s390_get_base_disp_sse(vcpu
, &address1
, &address2
);
935 /* we only handle the Linux memory detection case:
937 * everything else goes to userspace. */
940 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
942 ret
= guest_translate_address(vcpu
, address1
, &gpa
, 1);
943 if (ret
== PGM_PROTECTION
) {
944 /* Write protected? Try again with read-only... */
946 ret
= guest_translate_address(vcpu
, address1
, &gpa
, 0);
949 if (ret
== PGM_ADDRESSING
|| ret
== PGM_TRANSLATION_SPEC
) {
950 ret
= kvm_s390_inject_program_int(vcpu
, ret
);
951 } else if (ret
> 0) {
952 /* Translation not available */
953 kvm_s390_set_psw_cc(vcpu
, 3);
959 hva
= gfn_to_hva_prot(vcpu
->kvm
, gpa_to_gfn(gpa
), &writable
);
960 if (kvm_is_error_hva(hva
)) {
961 ret
= kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
964 cc
= 1; /* Write not permitted ==> read-only */
965 kvm_s390_set_psw_cc(vcpu
, cc
);
966 /* Note: CC2 only occurs for storage keys (not supported yet) */
969 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
974 int kvm_s390_handle_e5(struct kvm_vcpu
*vcpu
)
976 /* For e5xx... instructions we only handle TPROT */
977 if ((vcpu
->arch
.sie_block
->ipa
& 0x00ff) == 0x01)
978 return handle_tprot(vcpu
);
982 static int handle_sckpf(struct kvm_vcpu
*vcpu
)
986 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
987 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
989 if (vcpu
->run
->s
.regs
.gprs
[0] & 0x00000000ffff0000)
990 return kvm_s390_inject_program_int(vcpu
,
993 value
= vcpu
->run
->s
.regs
.gprs
[0] & 0x000000000000ffff;
994 vcpu
->arch
.sie_block
->todpr
= value
;
999 static const intercept_handler_t x01_handlers
[256] = {
1000 [0x07] = handle_sckpf
,
1003 int kvm_s390_handle_01(struct kvm_vcpu
*vcpu
)
1005 intercept_handler_t handler
;
1007 handler
= x01_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
1009 return handler(vcpu
);