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
->kvm
->arch
.model
.cpuid
;
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 enabled for the guest */
674 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
&&
675 !test_kvm_facility(vcpu
->kvm
, 14))
676 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
678 /* No support for conditional-SSKE */
679 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & (PFMF_MR
| PFMF_MC
))
680 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
682 start
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
683 start
= kvm_s390_logical_to_effective(vcpu
, start
);
685 switch (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) {
687 end
= (start
+ (1UL << 12)) & ~((1UL << 12) - 1);
690 end
= (start
+ (1UL << 20)) & ~((1UL << 20) - 1);
693 /* only support 2G frame size if EDAT2 is available and we are
694 not in 24-bit addressing mode */
695 if (!test_kvm_facility(vcpu
->kvm
, 78) ||
696 psw_bits(vcpu
->arch
.sie_block
->gpsw
).eaba
== PSW_AMODE_24BIT
)
697 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
698 end
= (start
+ (1UL << 31)) & ~((1UL << 31) - 1);
701 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
704 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
705 if (kvm_s390_check_low_addr_prot_real(vcpu
, start
))
706 return kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
709 while (start
< end
) {
710 unsigned long useraddr
, abs_addr
;
712 /* Translate guest address to host address */
713 if ((vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) == 0)
714 abs_addr
= kvm_s390_real_to_abs(vcpu
, start
);
717 useraddr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(abs_addr
));
718 if (kvm_is_error_hva(useraddr
))
719 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
721 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
722 if (clear_user((void __user
*)useraddr
, PAGE_SIZE
))
723 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
726 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_SK
) {
727 int rc
= __skey_check_enable(vcpu
);
731 if (set_guest_storage_key(current
->mm
, useraddr
,
732 vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_KEY
,
733 vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
))
734 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
739 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
)
740 vcpu
->run
->s
.regs
.gprs
[reg2
] = end
;
744 static int handle_essa(struct kvm_vcpu
*vcpu
)
746 /* entries expected to be 1FF */
747 int entries
= (vcpu
->arch
.sie_block
->cbrlo
& ~PAGE_MASK
) >> 3;
748 unsigned long *cbrlo
;
752 VCPU_EVENT(vcpu
, 4, "ESSA: release %d pages", entries
);
753 gmap
= vcpu
->arch
.gmap
;
754 vcpu
->stat
.instruction_essa
++;
755 if (!vcpu
->kvm
->arch
.use_cmma
)
756 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
758 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
759 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
761 if (((vcpu
->arch
.sie_block
->ipb
& 0xf0000000) >> 28) > 6)
762 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
764 /* Retry the ESSA instruction */
765 kvm_s390_retry_instr(vcpu
);
766 vcpu
->arch
.sie_block
->cbrlo
&= PAGE_MASK
; /* reset nceo */
767 cbrlo
= phys_to_virt(vcpu
->arch
.sie_block
->cbrlo
);
768 down_read(&gmap
->mm
->mmap_sem
);
769 for (i
= 0; i
< entries
; ++i
)
770 __gmap_zap(gmap
, cbrlo
[i
]);
771 up_read(&gmap
->mm
->mmap_sem
);
775 static const intercept_handler_t b9_handlers
[256] = {
776 [0x8a] = handle_ipte_interlock
,
777 [0x8d] = handle_epsw
,
778 [0x8e] = handle_ipte_interlock
,
779 [0x8f] = handle_ipte_interlock
,
780 [0xab] = handle_essa
,
781 [0xaf] = handle_pfmf
,
784 int kvm_s390_handle_b9(struct kvm_vcpu
*vcpu
)
786 intercept_handler_t handler
;
788 /* This is handled just as for the B2 instructions. */
789 handler
= b9_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
791 return handler(vcpu
);
796 int kvm_s390_handle_lctl(struct kvm_vcpu
*vcpu
)
798 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
799 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
800 int reg
, rc
, nr_regs
;
805 vcpu
->stat
.instruction_lctl
++;
807 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
808 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
810 ga
= kvm_s390_get_base_disp_rs(vcpu
, &ar
);
813 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
815 VCPU_EVENT(vcpu
, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
816 trace_kvm_s390_handle_lctl(vcpu
, 0, reg1
, reg3
, ga
);
818 nr_regs
= ((reg3
- reg1
) & 0xf) + 1;
819 rc
= read_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u32
));
821 return kvm_s390_inject_prog_cond(vcpu
, rc
);
825 vcpu
->arch
.sie_block
->gcr
[reg
] &= 0xffffffff00000000ul
;
826 vcpu
->arch
.sie_block
->gcr
[reg
] |= ctl_array
[nr_regs
++];
829 reg
= (reg
+ 1) % 16;
831 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
835 int kvm_s390_handle_stctl(struct kvm_vcpu
*vcpu
)
837 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
838 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
839 int reg
, rc
, nr_regs
;
844 vcpu
->stat
.instruction_stctl
++;
846 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
847 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
849 ga
= kvm_s390_get_base_disp_rs(vcpu
, &ar
);
852 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
854 VCPU_EVENT(vcpu
, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
855 trace_kvm_s390_handle_stctl(vcpu
, 0, reg1
, reg3
, ga
);
860 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
863 reg
= (reg
+ 1) % 16;
865 rc
= write_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u32
));
866 return rc
? kvm_s390_inject_prog_cond(vcpu
, rc
) : 0;
869 static int handle_lctlg(struct kvm_vcpu
*vcpu
)
871 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
872 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
873 int reg
, rc
, nr_regs
;
878 vcpu
->stat
.instruction_lctlg
++;
880 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
881 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
883 ga
= kvm_s390_get_base_disp_rsy(vcpu
, &ar
);
886 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
888 VCPU_EVENT(vcpu
, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
889 trace_kvm_s390_handle_lctl(vcpu
, 1, reg1
, reg3
, ga
);
891 nr_regs
= ((reg3
- reg1
) & 0xf) + 1;
892 rc
= read_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u64
));
894 return kvm_s390_inject_prog_cond(vcpu
, rc
);
898 vcpu
->arch
.sie_block
->gcr
[reg
] = ctl_array
[nr_regs
++];
901 reg
= (reg
+ 1) % 16;
903 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
907 static int handle_stctg(struct kvm_vcpu
*vcpu
)
909 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
910 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
911 int reg
, rc
, nr_regs
;
916 vcpu
->stat
.instruction_stctg
++;
918 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
919 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
921 ga
= kvm_s390_get_base_disp_rsy(vcpu
, &ar
);
924 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
926 VCPU_EVENT(vcpu
, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
927 trace_kvm_s390_handle_stctl(vcpu
, 1, reg1
, reg3
, ga
);
932 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
935 reg
= (reg
+ 1) % 16;
937 rc
= write_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u64
));
938 return rc
? kvm_s390_inject_prog_cond(vcpu
, rc
) : 0;
941 static const intercept_handler_t eb_handlers
[256] = {
942 [0x2f] = handle_lctlg
,
943 [0x25] = handle_stctg
,
946 int kvm_s390_handle_eb(struct kvm_vcpu
*vcpu
)
948 intercept_handler_t handler
;
950 handler
= eb_handlers
[vcpu
->arch
.sie_block
->ipb
& 0xff];
952 return handler(vcpu
);
956 static int handle_tprot(struct kvm_vcpu
*vcpu
)
958 u64 address1
, address2
;
959 unsigned long hva
, gpa
;
964 vcpu
->stat
.instruction_tprot
++;
966 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
967 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
969 kvm_s390_get_base_disp_sse(vcpu
, &address1
, &address2
, &ar
, NULL
);
971 /* we only handle the Linux memory detection case:
973 * everything else goes to userspace. */
976 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
978 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
, GACC_STORE
);
979 if (ret
== PGM_PROTECTION
) {
980 /* Write protected? Try again with read-only... */
982 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
,
986 if (ret
== PGM_ADDRESSING
|| ret
== PGM_TRANSLATION_SPEC
) {
987 ret
= kvm_s390_inject_program_int(vcpu
, ret
);
988 } else if (ret
> 0) {
989 /* Translation not available */
990 kvm_s390_set_psw_cc(vcpu
, 3);
996 hva
= gfn_to_hva_prot(vcpu
->kvm
, gpa_to_gfn(gpa
), &writable
);
997 if (kvm_is_error_hva(hva
)) {
998 ret
= kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
1001 cc
= 1; /* Write not permitted ==> read-only */
1002 kvm_s390_set_psw_cc(vcpu
, cc
);
1003 /* Note: CC2 only occurs for storage keys (not supported yet) */
1006 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
1011 int kvm_s390_handle_e5(struct kvm_vcpu
*vcpu
)
1013 /* For e5xx... instructions we only handle TPROT */
1014 if ((vcpu
->arch
.sie_block
->ipa
& 0x00ff) == 0x01)
1015 return handle_tprot(vcpu
);
1019 static int handle_sckpf(struct kvm_vcpu
*vcpu
)
1023 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1024 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1026 if (vcpu
->run
->s
.regs
.gprs
[0] & 0x00000000ffff0000)
1027 return kvm_s390_inject_program_int(vcpu
,
1030 value
= vcpu
->run
->s
.regs
.gprs
[0] & 0x000000000000ffff;
1031 vcpu
->arch
.sie_block
->todpr
= value
;
1036 static const intercept_handler_t x01_handlers
[256] = {
1037 [0x07] = handle_sckpf
,
1040 int kvm_s390_handle_01(struct kvm_vcpu
*vcpu
)
1042 intercept_handler_t handler
;
1044 handler
= x01_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
1046 return handler(vcpu
);