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>
28 #include <asm/ptrace.h>
29 #include <asm/compat.h>
34 /* Handle SCK (SET CLOCK) interception */
35 static int handle_set_clock(struct kvm_vcpu
*vcpu
)
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
));
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);
58 static int handle_set_prefix(struct kvm_vcpu
*vcpu
)
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 */
74 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
77 rc
= read_guest(vcpu
, operand2
, ar
, &address
, sizeof(address
));
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
);
96 static int handle_store_prefix(struct kvm_vcpu
*vcpu
)
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 */
112 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
114 address
= kvm_s390_get_prefix(vcpu
);
117 rc
= write_guest(vcpu
, operand2
, ar
, &address
, sizeof(address
));
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
);
126 static int handle_store_cpu_address(struct kvm_vcpu
*vcpu
)
128 u16 vcpu_id
= vcpu
->vcpu_id
;
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
);
141 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
143 rc
= write_guest(vcpu
, ga
, ar
, &vcpu_id
, sizeof(vcpu_id
));
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
);
152 static int __skey_check_enable(struct kvm_vcpu
*vcpu
)
155 if (!(vcpu
->arch
.sie_block
->ictl
& (ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
)))
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
);
166 static int handle_skey(struct kvm_vcpu
*vcpu
)
168 int rc
= __skey_check_enable(vcpu
);
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");
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");
193 static int handle_test_block(struct kvm_vcpu
*vcpu
)
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
, ®2
);
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
))
216 kvm_s390_set_psw_cc(vcpu
, 0);
217 vcpu
->run
->s
.regs
.gprs
[0] = 0;
221 static int handle_tpi(struct kvm_vcpu
*vcpu
)
223 struct kvm_s390_interrupt_info
*inti
;
230 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
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);
236 kvm_s390_set_psw_cc(vcpu
, 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
;
245 * Store the two-word I/O interruption code into the
248 len
= sizeof(tpi_data
) - 4;
249 rc
= write_guest(vcpu
, addr
, ar
, &tpi_data
, len
);
251 rc
= kvm_s390_inject_prog_cond(vcpu
, rc
);
252 goto reinject_interrupt
;
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 */
263 goto reinject_interrupt
;
267 /* irq was successfully handed to the guest */
269 kvm_s390_set_psw_cc(vcpu
, 1);
273 * If we encounter a problem storing the interruption code, the
274 * instruction is suppressed from the guest's view: reinject the
277 if (kvm_s390_reinject_io_int(vcpu
->kvm
, inti
)) {
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
303 vcpu
->run
->exit_reason
= KVM_EXIT_S390_TSCH
;
304 vcpu
->run
->s390_tsch
.dequeued
= !!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
;
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. */
336 * Set condition code 3 to stop the guest from issuing channel
339 kvm_s390_set_psw_cc(vcpu
, 3);
344 static int handle_stfl(struct kvm_vcpu
*vcpu
)
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
),
363 VCPU_EVENT(vcpu
, 3, "STFL: store facility list 0x%x", fac
);
364 trace_kvm_s390_handle_stfl(vcpu
, fac
);
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
)
377 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_BA
) {
378 if (psw
->addr
& ~PSW_ADDR_31
)
381 if (!(psw
->mask
& PSW_MASK_ADDR_MODE
) && (psw
->addr
& ~PSW_ADDR_24
))
383 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_EA
)
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
;
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
);
403 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
405 rc
= read_guest(vcpu
, addr
, ar
, &new_psw
, sizeof(new_psw
));
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
);
418 static int handle_lpswe(struct kvm_vcpu
*vcpu
)
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
);
430 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
431 rc
= read_guest(vcpu
, addr
, ar
, &new_psw
, sizeof(new_psw
));
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
);
440 static int handle_stidp(struct kvm_vcpu
*vcpu
)
442 u64 stidp_data
= vcpu
->arch
.stidp_data
;
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
);
455 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
457 rc
= write_guest(vcpu
, operand2
, ar
, &stidp_data
, sizeof(stidp_data
));
459 return kvm_s390_inject_prog_cond(vcpu
, rc
);
461 VCPU_EVENT(vcpu
, 3, "STIDP: store cpu id 0x%llx", stidp_data
);
465 static void handle_stsi_3_2_2(struct kvm_vcpu
*vcpu
, struct sysinfo_3_2_2
*mem
)
470 cpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
472 /* deal with other level 3 hypervisors */
473 if (stsi(mem
, 3, 2, 2))
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;
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
);
520 kvm_s390_set_psw_cc(vcpu
, 3);
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
);
529 vcpu
->run
->s
.regs
.gprs
[0] = 3 << 28;
530 kvm_s390_set_psw_cc(vcpu
, 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
);
540 case 1: /* same handling for 1 and 2 */
542 mem
= get_zeroed_page(GFP_KERNEL
);
545 if (stsi((void *) mem
, fc
, sel1
, sel2
))
549 if (sel1
!= 2 || sel2
!= 2)
551 mem
= get_zeroed_page(GFP_KERNEL
);
554 handle_stsi_3_2_2(vcpu
, (void *) mem
);
558 rc
= write_guest(vcpu
, operand2
, ar
, (void *)mem
, PAGE_SIZE
);
560 rc
= kvm_s390_inject_prog_cond(vcpu
, rc
);
563 if (vcpu
->kvm
->arch
.user_stsi
) {
564 insert_stsi_usr_data(vcpu
, operand2
, ar
, fc
, sel1
, sel2
);
567 trace_kvm_s390_handle_stsi(vcpu
, fc
, sel1
, sel2
, operand2
);
569 kvm_s390_set_psw_cc(vcpu
, 0);
570 vcpu
->run
->s
.regs
.gprs
[0] = 0;
573 kvm_s390_set_psw_cc(vcpu
, 3);
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];
623 return handler(vcpu
);
628 static int handle_epsw(struct kvm_vcpu
*vcpu
)
632 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
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;
638 vcpu
->run
->s
.regs
.gprs
[reg2
] &= 0xffffffff00000000UL
;
639 vcpu
->run
->s
.regs
.gprs
[reg2
] |=
640 vcpu
->arch
.sie_block
->gpsw
.mask
& 0x00000000ffffffffUL
;
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
)
658 unsigned long start
, end
;
660 vcpu
->stat
.instruction_pfmf
++;
662 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
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
) {
686 end
= (start
+ (1UL << 12)) & ~((1UL << 12) - 1);
689 end
= (start
+ (1UL << 20)) & ~((1UL << 20) - 1);
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);
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
);
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
);
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
);
738 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
)
739 vcpu
->run
->s
.regs
.gprs
[reg2
] = end
;
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
;
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
) {
770 if (unlikely(cbrle
& ~PAGE_MASK
|| cbrle
< 2 * PAGE_SIZE
))
773 /* try to free backing */
774 __gmap_zap(gmap
, cbrle
);
776 up_read(&gmap
->mm
->mmap_sem
);
778 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
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];
798 return handler(vcpu
);
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
;
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
);
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
));
828 return kvm_s390_inject_prog_cond(vcpu
, rc
);
832 vcpu
->arch
.sie_block
->gcr
[reg
] &= 0xffffffff00000000ul
;
833 vcpu
->arch
.sie_block
->gcr
[reg
] |= ctl_array
[nr_regs
++];
836 reg
= (reg
+ 1) % 16;
838 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
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
;
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
);
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
);
867 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
870 reg
= (reg
+ 1) % 16;
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
;
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
);
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
));
901 return kvm_s390_inject_prog_cond(vcpu
, rc
);
905 vcpu
->arch
.sie_block
->gcr
[reg
] = ctl_array
[nr_regs
++];
908 reg
= (reg
+ 1) % 16;
910 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
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
;
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
);
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
);
939 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
942 reg
= (reg
+ 1) % 16;
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];
959 return handler(vcpu
);
963 static int handle_tprot(struct kvm_vcpu
*vcpu
)
965 u64 address1
, address2
;
966 unsigned long hva
, gpa
;
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:
980 * everything else goes to userspace. */
983 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
985 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
, GACC_STORE
);
986 if (ret
== PGM_PROTECTION
) {
987 /* Write protected? Try again with read-only... */
989 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
,
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);
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
);
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) */
1013 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
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
);
1026 static int handle_sckpf(struct kvm_vcpu
*vcpu
)
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
,
1037 value
= vcpu
->run
->s
.regs
.gprs
[0] & 0x000000000000ffff;
1038 vcpu
->arch
.sie_block
->todpr
= value
;
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];
1053 return handler(vcpu
);