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
;
42 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
43 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
45 op2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
46 if (op2
& 7) /* Operand must be on a doubleword boundary */
47 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
48 rc
= read_guest(vcpu
, op2
, ar
, &val
, sizeof(val
));
50 return kvm_s390_inject_prog_cond(vcpu
, rc
);
52 if (store_tod_clock(&hostclk
)) {
53 kvm_s390_set_psw_cc(vcpu
, 3);
56 val
= (val
- hostclk
) & ~0x3fUL
;
58 mutex_lock(&vcpu
->kvm
->lock
);
59 kvm_for_each_vcpu(i
, cpup
, vcpu
->kvm
)
60 cpup
->arch
.sie_block
->epoch
= val
;
61 mutex_unlock(&vcpu
->kvm
->lock
);
63 kvm_s390_set_psw_cc(vcpu
, 0);
67 static int handle_set_prefix(struct kvm_vcpu
*vcpu
)
74 vcpu
->stat
.instruction_spx
++;
76 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
77 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
79 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
81 /* must be word boundary */
83 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
86 rc
= read_guest(vcpu
, operand2
, ar
, &address
, sizeof(address
));
88 return kvm_s390_inject_prog_cond(vcpu
, rc
);
90 address
&= 0x7fffe000u
;
93 * Make sure the new value is valid memory. We only need to check the
94 * first page, since address is 8k aligned and memory pieces are always
95 * at least 1MB aligned and have at least a size of 1MB.
97 if (kvm_is_error_gpa(vcpu
->kvm
, address
))
98 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
100 kvm_s390_set_prefix(vcpu
, address
);
102 VCPU_EVENT(vcpu
, 5, "setting prefix to %x", address
);
103 trace_kvm_s390_handle_prefix(vcpu
, 1, address
);
107 static int handle_store_prefix(struct kvm_vcpu
*vcpu
)
114 vcpu
->stat
.instruction_stpx
++;
116 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
117 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
119 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
121 /* must be word boundary */
123 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
125 address
= kvm_s390_get_prefix(vcpu
);
128 rc
= write_guest(vcpu
, operand2
, ar
, &address
, sizeof(address
));
130 return kvm_s390_inject_prog_cond(vcpu
, rc
);
132 VCPU_EVENT(vcpu
, 5, "storing prefix to %x", address
);
133 trace_kvm_s390_handle_prefix(vcpu
, 0, address
);
137 static int handle_store_cpu_address(struct kvm_vcpu
*vcpu
)
139 u16 vcpu_id
= vcpu
->vcpu_id
;
144 vcpu
->stat
.instruction_stap
++;
146 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
147 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
149 ga
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
152 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
154 rc
= write_guest(vcpu
, ga
, ar
, &vcpu_id
, sizeof(vcpu_id
));
156 return kvm_s390_inject_prog_cond(vcpu
, rc
);
158 VCPU_EVENT(vcpu
, 5, "storing cpu address to %llx", ga
);
159 trace_kvm_s390_handle_stap(vcpu
, ga
);
163 static int __skey_check_enable(struct kvm_vcpu
*vcpu
)
166 if (!(vcpu
->arch
.sie_block
->ictl
& (ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
)))
169 rc
= s390_enable_skey();
170 trace_kvm_s390_skey_related_inst(vcpu
);
171 vcpu
->arch
.sie_block
->ictl
&= ~(ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
);
176 static int handle_skey(struct kvm_vcpu
*vcpu
)
178 int rc
= __skey_check_enable(vcpu
);
182 vcpu
->stat
.instruction_storage_key
++;
184 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
185 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
187 kvm_s390_rewind_psw(vcpu
, 4);
188 VCPU_EVENT(vcpu
, 4, "%s", "retrying storage key operation");
192 static int handle_ipte_interlock(struct kvm_vcpu
*vcpu
)
194 vcpu
->stat
.instruction_ipte_interlock
++;
195 if (psw_bits(vcpu
->arch
.sie_block
->gpsw
).p
)
196 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
197 wait_event(vcpu
->kvm
->arch
.ipte_wq
, !ipte_lock_held(vcpu
));
198 kvm_s390_rewind_psw(vcpu
, 4);
199 VCPU_EVENT(vcpu
, 4, "%s", "retrying ipte interlock operation");
203 static int handle_test_block(struct kvm_vcpu
*vcpu
)
208 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
209 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
211 kvm_s390_get_regs_rre(vcpu
, NULL
, ®2
);
212 addr
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
213 addr
= kvm_s390_logical_to_effective(vcpu
, addr
);
214 if (kvm_s390_check_low_addr_prot_real(vcpu
, addr
))
215 return kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
216 addr
= kvm_s390_real_to_abs(vcpu
, addr
);
218 if (kvm_is_error_gpa(vcpu
->kvm
, addr
))
219 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
221 * We don't expect errors on modern systems, and do not care
222 * about storage keys (yet), so let's just clear the page.
224 if (kvm_clear_guest(vcpu
->kvm
, addr
, PAGE_SIZE
))
226 kvm_s390_set_psw_cc(vcpu
, 0);
227 vcpu
->run
->s
.regs
.gprs
[0] = 0;
231 static int handle_tpi(struct kvm_vcpu
*vcpu
)
233 struct kvm_s390_interrupt_info
*inti
;
240 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
242 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
244 inti
= kvm_s390_get_io_int(vcpu
->kvm
, vcpu
->arch
.sie_block
->gcr
[6], 0);
246 kvm_s390_set_psw_cc(vcpu
, 0);
250 tpi_data
[0] = inti
->io
.subchannel_id
<< 16 | inti
->io
.subchannel_nr
;
251 tpi_data
[1] = inti
->io
.io_int_parm
;
252 tpi_data
[2] = inti
->io
.io_int_word
;
255 * Store the two-word I/O interruption code into the
258 len
= sizeof(tpi_data
) - 4;
259 rc
= write_guest(vcpu
, addr
, ar
, &tpi_data
, len
);
261 rc
= kvm_s390_inject_prog_cond(vcpu
, rc
);
262 goto reinject_interrupt
;
266 * Store the three-word I/O interruption code into
267 * the appropriate lowcore area.
269 len
= sizeof(tpi_data
);
270 if (write_guest_lc(vcpu
, __LC_SUBCHANNEL_ID
, &tpi_data
, len
)) {
271 /* failed writes to the low core are not recoverable */
273 goto reinject_interrupt
;
277 /* irq was successfully handed to the guest */
279 kvm_s390_set_psw_cc(vcpu
, 1);
283 * If we encounter a problem storing the interruption code, the
284 * instruction is suppressed from the guest's view: reinject the
287 if (kvm_s390_reinject_io_int(vcpu
->kvm
, inti
)) {
291 /* don't set the cc, a pgm irq was injected or we drop to user space */
292 return rc
? -EFAULT
: 0;
295 static int handle_tsch(struct kvm_vcpu
*vcpu
)
297 struct kvm_s390_interrupt_info
*inti
= NULL
;
298 const u64 isc_mask
= 0xffUL
<< 24; /* all iscs set */
300 /* a valid schid has at least one bit set */
301 if (vcpu
->run
->s
.regs
.gprs
[1])
302 inti
= kvm_s390_get_io_int(vcpu
->kvm
, isc_mask
,
303 vcpu
->run
->s
.regs
.gprs
[1]);
306 * Prepare exit to userspace.
307 * We indicate whether we dequeued a pending I/O interrupt
308 * so that userspace can re-inject it if the instruction gets
309 * a program check. While this may re-order the pending I/O
310 * interrupts, this is no problem since the priority is kept
313 vcpu
->run
->exit_reason
= KVM_EXIT_S390_TSCH
;
314 vcpu
->run
->s390_tsch
.dequeued
= !!inti
;
316 vcpu
->run
->s390_tsch
.subchannel_id
= inti
->io
.subchannel_id
;
317 vcpu
->run
->s390_tsch
.subchannel_nr
= inti
->io
.subchannel_nr
;
318 vcpu
->run
->s390_tsch
.io_int_parm
= inti
->io
.io_int_parm
;
319 vcpu
->run
->s390_tsch
.io_int_word
= inti
->io
.io_int_word
;
321 vcpu
->run
->s390_tsch
.ipb
= vcpu
->arch
.sie_block
->ipb
;
326 static int handle_io_inst(struct kvm_vcpu
*vcpu
)
328 VCPU_EVENT(vcpu
, 4, "%s", "I/O instruction");
330 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
331 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
333 if (vcpu
->kvm
->arch
.css_support
) {
335 * Most I/O instructions will be handled by userspace.
336 * Exceptions are tpi and the interrupt portion of tsch.
338 if (vcpu
->arch
.sie_block
->ipa
== 0xb236)
339 return handle_tpi(vcpu
);
340 if (vcpu
->arch
.sie_block
->ipa
== 0xb235)
341 return handle_tsch(vcpu
);
342 /* Handle in userspace. */
346 * Set condition code 3 to stop the guest from issuing channel
349 kvm_s390_set_psw_cc(vcpu
, 3);
354 static int handle_stfl(struct kvm_vcpu
*vcpu
)
359 vcpu
->stat
.instruction_stfl
++;
361 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
362 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
365 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
366 * into a u32 memory representation. They will remain bits 0-31.
368 fac
= *vcpu
->kvm
->arch
.model
.fac
->list
>> 32;
369 rc
= write_guest_lc(vcpu
, offsetof(struct _lowcore
, stfl_fac_list
),
373 VCPU_EVENT(vcpu
, 5, "store facility list value %x", fac
);
374 trace_kvm_s390_handle_stfl(vcpu
, fac
);
378 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
379 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
380 #define PSW_ADDR_24 0x0000000000ffffffUL
381 #define PSW_ADDR_31 0x000000007fffffffUL
383 int is_valid_psw(psw_t
*psw
)
385 if (psw
->mask
& PSW_MASK_UNASSIGNED
)
387 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_BA
) {
388 if (psw
->addr
& ~PSW_ADDR_31
)
391 if (!(psw
->mask
& PSW_MASK_ADDR_MODE
) && (psw
->addr
& ~PSW_ADDR_24
))
393 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_EA
)
400 int kvm_s390_handle_lpsw(struct kvm_vcpu
*vcpu
)
402 psw_t
*gpsw
= &vcpu
->arch
.sie_block
->gpsw
;
403 psw_compat_t new_psw
;
408 if (gpsw
->mask
& PSW_MASK_PSTATE
)
409 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
411 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
413 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
415 rc
= read_guest(vcpu
, addr
, ar
, &new_psw
, sizeof(new_psw
));
417 return kvm_s390_inject_prog_cond(vcpu
, rc
);
418 if (!(new_psw
.mask
& PSW32_MASK_BASE
))
419 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
420 gpsw
->mask
= (new_psw
.mask
& ~PSW32_MASK_BASE
) << 32;
421 gpsw
->mask
|= new_psw
.addr
& PSW32_ADDR_AMODE
;
422 gpsw
->addr
= new_psw
.addr
& ~PSW32_ADDR_AMODE
;
423 if (!is_valid_psw(gpsw
))
424 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
428 static int handle_lpswe(struct kvm_vcpu
*vcpu
)
435 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
436 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
438 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
440 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
441 rc
= read_guest(vcpu
, addr
, ar
, &new_psw
, sizeof(new_psw
));
443 return kvm_s390_inject_prog_cond(vcpu
, rc
);
444 vcpu
->arch
.sie_block
->gpsw
= new_psw
;
445 if (!is_valid_psw(&vcpu
->arch
.sie_block
->gpsw
))
446 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
450 static int handle_stidp(struct kvm_vcpu
*vcpu
)
452 u64 stidp_data
= vcpu
->arch
.stidp_data
;
457 vcpu
->stat
.instruction_stidp
++;
459 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
460 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
462 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
465 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
467 rc
= write_guest(vcpu
, operand2
, ar
, &stidp_data
, sizeof(stidp_data
));
469 return kvm_s390_inject_prog_cond(vcpu
, rc
);
471 VCPU_EVENT(vcpu
, 5, "%s", "store cpu id");
475 static void handle_stsi_3_2_2(struct kvm_vcpu
*vcpu
, struct sysinfo_3_2_2
*mem
)
480 cpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
482 /* deal with other level 3 hypervisors */
483 if (stsi(mem
, 3, 2, 2))
487 for (n
= mem
->count
- 1; n
> 0 ; n
--)
488 memcpy(&mem
->vm
[n
], &mem
->vm
[n
- 1], sizeof(mem
->vm
[0]));
490 memset(&mem
->vm
[0], 0, sizeof(mem
->vm
[0]));
491 mem
->vm
[0].cpus_total
= cpus
;
492 mem
->vm
[0].cpus_configured
= cpus
;
493 mem
->vm
[0].cpus_standby
= 0;
494 mem
->vm
[0].cpus_reserved
= 0;
495 mem
->vm
[0].caf
= 1000;
496 memcpy(mem
->vm
[0].name
, "KVMguest", 8);
497 ASCEBC(mem
->vm
[0].name
, 8);
498 memcpy(mem
->vm
[0].cpi
, "KVM/Linux ", 16);
499 ASCEBC(mem
->vm
[0].cpi
, 16);
502 static void insert_stsi_usr_data(struct kvm_vcpu
*vcpu
, u64 addr
, ar_t ar
,
503 u8 fc
, u8 sel1
, u16 sel2
)
505 vcpu
->run
->exit_reason
= KVM_EXIT_S390_STSI
;
506 vcpu
->run
->s390_stsi
.addr
= addr
;
507 vcpu
->run
->s390_stsi
.ar
= ar
;
508 vcpu
->run
->s390_stsi
.fc
= fc
;
509 vcpu
->run
->s390_stsi
.sel1
= sel1
;
510 vcpu
->run
->s390_stsi
.sel2
= sel2
;
513 static int handle_stsi(struct kvm_vcpu
*vcpu
)
515 int fc
= (vcpu
->run
->s
.regs
.gprs
[0] & 0xf0000000) >> 28;
516 int sel1
= vcpu
->run
->s
.regs
.gprs
[0] & 0xff;
517 int sel2
= vcpu
->run
->s
.regs
.gprs
[1] & 0xffff;
518 unsigned long mem
= 0;
523 vcpu
->stat
.instruction_stsi
++;
524 VCPU_EVENT(vcpu
, 4, "stsi: fc: %x sel1: %x sel2: %x", fc
, sel1
, sel2
);
526 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
527 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
530 kvm_s390_set_psw_cc(vcpu
, 3);
534 if (vcpu
->run
->s
.regs
.gprs
[0] & 0x0fffff00
535 || vcpu
->run
->s
.regs
.gprs
[1] & 0xffff0000)
536 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
539 vcpu
->run
->s
.regs
.gprs
[0] = 3 << 28;
540 kvm_s390_set_psw_cc(vcpu
, 0);
544 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
546 if (operand2
& 0xfff)
547 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
550 case 1: /* same handling for 1 and 2 */
552 mem
= get_zeroed_page(GFP_KERNEL
);
555 if (stsi((void *) mem
, fc
, sel1
, sel2
))
559 if (sel1
!= 2 || sel2
!= 2)
561 mem
= get_zeroed_page(GFP_KERNEL
);
564 handle_stsi_3_2_2(vcpu
, (void *) mem
);
568 rc
= write_guest(vcpu
, operand2
, ar
, (void *)mem
, PAGE_SIZE
);
570 rc
= kvm_s390_inject_prog_cond(vcpu
, rc
);
573 if (vcpu
->kvm
->arch
.user_stsi
) {
574 insert_stsi_usr_data(vcpu
, operand2
, ar
, fc
, sel1
, sel2
);
577 trace_kvm_s390_handle_stsi(vcpu
, fc
, sel1
, sel2
, operand2
);
579 kvm_s390_set_psw_cc(vcpu
, 0);
580 vcpu
->run
->s
.regs
.gprs
[0] = 0;
583 kvm_s390_set_psw_cc(vcpu
, 3);
589 static const intercept_handler_t b2_handlers
[256] = {
590 [0x02] = handle_stidp
,
591 [0x04] = handle_set_clock
,
592 [0x10] = handle_set_prefix
,
593 [0x11] = handle_store_prefix
,
594 [0x12] = handle_store_cpu_address
,
595 [0x21] = handle_ipte_interlock
,
596 [0x29] = handle_skey
,
597 [0x2a] = handle_skey
,
598 [0x2b] = handle_skey
,
599 [0x2c] = handle_test_block
,
600 [0x30] = handle_io_inst
,
601 [0x31] = handle_io_inst
,
602 [0x32] = handle_io_inst
,
603 [0x33] = handle_io_inst
,
604 [0x34] = handle_io_inst
,
605 [0x35] = handle_io_inst
,
606 [0x36] = handle_io_inst
,
607 [0x37] = handle_io_inst
,
608 [0x38] = handle_io_inst
,
609 [0x39] = handle_io_inst
,
610 [0x3a] = handle_io_inst
,
611 [0x3b] = handle_io_inst
,
612 [0x3c] = handle_io_inst
,
613 [0x50] = handle_ipte_interlock
,
614 [0x5f] = handle_io_inst
,
615 [0x74] = handle_io_inst
,
616 [0x76] = handle_io_inst
,
617 [0x7d] = handle_stsi
,
618 [0xb1] = handle_stfl
,
619 [0xb2] = handle_lpswe
,
622 int kvm_s390_handle_b2(struct kvm_vcpu
*vcpu
)
624 intercept_handler_t handler
;
627 * A lot of B2 instructions are priviledged. Here we check for
628 * the privileged ones, that we can handle in the kernel.
629 * Anything else goes to userspace.
631 handler
= b2_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
633 return handler(vcpu
);
638 static int handle_epsw(struct kvm_vcpu
*vcpu
)
642 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
644 /* This basically extracts the mask half of the psw. */
645 vcpu
->run
->s
.regs
.gprs
[reg1
] &= 0xffffffff00000000UL
;
646 vcpu
->run
->s
.regs
.gprs
[reg1
] |= vcpu
->arch
.sie_block
->gpsw
.mask
>> 32;
648 vcpu
->run
->s
.regs
.gprs
[reg2
] &= 0xffffffff00000000UL
;
649 vcpu
->run
->s
.regs
.gprs
[reg2
] |=
650 vcpu
->arch
.sie_block
->gpsw
.mask
& 0x00000000ffffffffUL
;
655 #define PFMF_RESERVED 0xfffc0101UL
656 #define PFMF_SK 0x00020000UL
657 #define PFMF_CF 0x00010000UL
658 #define PFMF_UI 0x00008000UL
659 #define PFMF_FSC 0x00007000UL
660 #define PFMF_NQ 0x00000800UL
661 #define PFMF_MR 0x00000400UL
662 #define PFMF_MC 0x00000200UL
663 #define PFMF_KEY 0x000000feUL
665 static int handle_pfmf(struct kvm_vcpu
*vcpu
)
668 unsigned long start
, end
;
670 vcpu
->stat
.instruction_pfmf
++;
672 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
674 if (!MACHINE_HAS_PFMF
)
675 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
677 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
678 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
680 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_RESERVED
)
681 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
683 /* Only provide non-quiescing support if the host supports it */
684 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
&& !test_facility(14))
685 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
687 /* No support for conditional-SSKE */
688 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & (PFMF_MR
| PFMF_MC
))
689 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
691 start
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
692 start
= kvm_s390_logical_to_effective(vcpu
, start
);
694 switch (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) {
696 end
= (start
+ (1UL << 12)) & ~((1UL << 12) - 1);
699 end
= (start
+ (1UL << 20)) & ~((1UL << 20) - 1);
702 /* only support 2G frame size if EDAT2 is available and we are
703 not in 24-bit addressing mode */
704 if (!test_kvm_facility(vcpu
->kvm
, 78) ||
705 psw_bits(vcpu
->arch
.sie_block
->gpsw
).eaba
== PSW_AMODE_24BIT
)
706 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
707 end
= (start
+ (1UL << 31)) & ~((1UL << 31) - 1);
710 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
713 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
714 if (kvm_s390_check_low_addr_prot_real(vcpu
, start
))
715 return kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
718 while (start
< end
) {
719 unsigned long useraddr
, abs_addr
;
721 /* Translate guest address to host address */
722 if ((vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) == 0)
723 abs_addr
= kvm_s390_real_to_abs(vcpu
, start
);
726 useraddr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(abs_addr
));
727 if (kvm_is_error_hva(useraddr
))
728 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
730 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
731 if (clear_user((void __user
*)useraddr
, PAGE_SIZE
))
732 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
735 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_SK
) {
736 int rc
= __skey_check_enable(vcpu
);
740 if (set_guest_storage_key(current
->mm
, useraddr
,
741 vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_KEY
,
742 vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
))
743 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
748 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
)
749 vcpu
->run
->s
.regs
.gprs
[reg2
] = end
;
753 static int handle_essa(struct kvm_vcpu
*vcpu
)
755 /* entries expected to be 1FF */
756 int entries
= (vcpu
->arch
.sie_block
->cbrlo
& ~PAGE_MASK
) >> 3;
757 unsigned long *cbrlo
, cbrle
;
761 VCPU_EVENT(vcpu
, 5, "cmma release %d pages", entries
);
762 gmap
= vcpu
->arch
.gmap
;
763 vcpu
->stat
.instruction_essa
++;
764 if (!kvm_s390_cmma_enabled(vcpu
->kvm
))
765 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
767 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
768 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
770 if (((vcpu
->arch
.sie_block
->ipb
& 0xf0000000) >> 28) > 6)
771 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
773 /* Rewind PSW to repeat the ESSA instruction */
774 kvm_s390_rewind_psw(vcpu
, 4);
775 vcpu
->arch
.sie_block
->cbrlo
&= PAGE_MASK
; /* reset nceo */
776 cbrlo
= phys_to_virt(vcpu
->arch
.sie_block
->cbrlo
);
777 down_read(&gmap
->mm
->mmap_sem
);
778 for (i
= 0; i
< entries
; ++i
) {
780 if (unlikely(cbrle
& ~PAGE_MASK
|| cbrle
< 2 * PAGE_SIZE
))
783 /* try to free backing */
784 __gmap_zap(gmap
, cbrle
);
786 up_read(&gmap
->mm
->mmap_sem
);
788 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
792 static const intercept_handler_t b9_handlers
[256] = {
793 [0x8a] = handle_ipte_interlock
,
794 [0x8d] = handle_epsw
,
795 [0x8e] = handle_ipte_interlock
,
796 [0x8f] = handle_ipte_interlock
,
797 [0xab] = handle_essa
,
798 [0xaf] = handle_pfmf
,
801 int kvm_s390_handle_b9(struct kvm_vcpu
*vcpu
)
803 intercept_handler_t handler
;
805 /* This is handled just as for the B2 instructions. */
806 handler
= b9_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
808 return handler(vcpu
);
813 int kvm_s390_handle_lctl(struct kvm_vcpu
*vcpu
)
815 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
816 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
817 int reg
, rc
, nr_regs
;
822 vcpu
->stat
.instruction_lctl
++;
824 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
825 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
827 ga
= kvm_s390_get_base_disp_rs(vcpu
, &ar
);
830 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
832 VCPU_EVENT(vcpu
, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1
, reg3
, ga
);
833 trace_kvm_s390_handle_lctl(vcpu
, 0, reg1
, reg3
, ga
);
835 nr_regs
= ((reg3
- reg1
) & 0xf) + 1;
836 rc
= read_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u32
));
838 return kvm_s390_inject_prog_cond(vcpu
, rc
);
842 vcpu
->arch
.sie_block
->gcr
[reg
] &= 0xffffffff00000000ul
;
843 vcpu
->arch
.sie_block
->gcr
[reg
] |= ctl_array
[nr_regs
++];
846 reg
= (reg
+ 1) % 16;
848 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
852 int kvm_s390_handle_stctl(struct kvm_vcpu
*vcpu
)
854 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
855 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
856 int reg
, rc
, nr_regs
;
861 vcpu
->stat
.instruction_stctl
++;
863 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
864 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
866 ga
= kvm_s390_get_base_disp_rs(vcpu
, &ar
);
869 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
871 VCPU_EVENT(vcpu
, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1
, reg3
, ga
);
872 trace_kvm_s390_handle_stctl(vcpu
, 0, reg1
, reg3
, ga
);
877 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
880 reg
= (reg
+ 1) % 16;
882 rc
= write_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u32
));
883 return rc
? kvm_s390_inject_prog_cond(vcpu
, rc
) : 0;
886 static int handle_lctlg(struct kvm_vcpu
*vcpu
)
888 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
889 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
890 int reg
, rc
, nr_regs
;
895 vcpu
->stat
.instruction_lctlg
++;
897 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
898 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
900 ga
= kvm_s390_get_base_disp_rsy(vcpu
, &ar
);
903 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
905 VCPU_EVENT(vcpu
, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1
, reg3
, ga
);
906 trace_kvm_s390_handle_lctl(vcpu
, 1, reg1
, reg3
, ga
);
908 nr_regs
= ((reg3
- reg1
) & 0xf) + 1;
909 rc
= read_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u64
));
911 return kvm_s390_inject_prog_cond(vcpu
, rc
);
915 vcpu
->arch
.sie_block
->gcr
[reg
] = ctl_array
[nr_regs
++];
918 reg
= (reg
+ 1) % 16;
920 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
924 static int handle_stctg(struct kvm_vcpu
*vcpu
)
926 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
927 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
928 int reg
, rc
, nr_regs
;
933 vcpu
->stat
.instruction_stctg
++;
935 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
936 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
938 ga
= kvm_s390_get_base_disp_rsy(vcpu
, &ar
);
941 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
943 VCPU_EVENT(vcpu
, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1
, reg3
, ga
);
944 trace_kvm_s390_handle_stctl(vcpu
, 1, reg1
, reg3
, ga
);
949 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
952 reg
= (reg
+ 1) % 16;
954 rc
= write_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u64
));
955 return rc
? kvm_s390_inject_prog_cond(vcpu
, rc
) : 0;
958 static const intercept_handler_t eb_handlers
[256] = {
959 [0x2f] = handle_lctlg
,
960 [0x25] = handle_stctg
,
963 int kvm_s390_handle_eb(struct kvm_vcpu
*vcpu
)
965 intercept_handler_t handler
;
967 handler
= eb_handlers
[vcpu
->arch
.sie_block
->ipb
& 0xff];
969 return handler(vcpu
);
973 static int handle_tprot(struct kvm_vcpu
*vcpu
)
975 u64 address1
, address2
;
976 unsigned long hva
, gpa
;
981 vcpu
->stat
.instruction_tprot
++;
983 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
984 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
986 kvm_s390_get_base_disp_sse(vcpu
, &address1
, &address2
, &ar
, NULL
);
988 /* we only handle the Linux memory detection case:
990 * everything else goes to userspace. */
993 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
995 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
, 1);
996 if (ret
== PGM_PROTECTION
) {
997 /* Write protected? Try again with read-only... */
999 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
, 0);
1002 if (ret
== PGM_ADDRESSING
|| ret
== PGM_TRANSLATION_SPEC
) {
1003 ret
= kvm_s390_inject_program_int(vcpu
, ret
);
1004 } else if (ret
> 0) {
1005 /* Translation not available */
1006 kvm_s390_set_psw_cc(vcpu
, 3);
1012 hva
= gfn_to_hva_prot(vcpu
->kvm
, gpa_to_gfn(gpa
), &writable
);
1013 if (kvm_is_error_hva(hva
)) {
1014 ret
= kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
1017 cc
= 1; /* Write not permitted ==> read-only */
1018 kvm_s390_set_psw_cc(vcpu
, cc
);
1019 /* Note: CC2 only occurs for storage keys (not supported yet) */
1022 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
1027 int kvm_s390_handle_e5(struct kvm_vcpu
*vcpu
)
1029 /* For e5xx... instructions we only handle TPROT */
1030 if ((vcpu
->arch
.sie_block
->ipa
& 0x00ff) == 0x01)
1031 return handle_tprot(vcpu
);
1035 static int handle_sckpf(struct kvm_vcpu
*vcpu
)
1039 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1040 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1042 if (vcpu
->run
->s
.regs
.gprs
[0] & 0x00000000ffff0000)
1043 return kvm_s390_inject_program_int(vcpu
,
1046 value
= vcpu
->run
->s
.regs
.gprs
[0] & 0x000000000000ffff;
1047 vcpu
->arch
.sie_block
->todpr
= value
;
1052 static const intercept_handler_t x01_handlers
[256] = {
1053 [0x07] = handle_sckpf
,
1056 int kvm_s390_handle_01(struct kvm_vcpu
*vcpu
)
1058 intercept_handler_t handler
;
1060 handler
= x01_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
1062 return handler(vcpu
);