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 <linux/mm_types.h>
20 #include <asm/asm-offsets.h>
21 #include <asm/facility.h>
22 #include <asm/current.h>
23 #include <asm/debug.h>
24 #include <asm/ebcdic.h>
25 #include <asm/sysinfo.h>
26 #include <asm/pgtable.h>
27 #include <asm/pgalloc.h>
30 #include <asm/ptrace.h>
31 #include <asm/compat.h>
37 static int handle_ri(struct kvm_vcpu
*vcpu
)
39 if (test_kvm_facility(vcpu
->kvm
, 64)) {
40 VCPU_EVENT(vcpu
, 3, "%s", "ENABLE: RI (lazy)");
41 vcpu
->arch
.sie_block
->ecb3
|= ECB3_RI
;
42 kvm_s390_retry_instr(vcpu
);
45 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
48 int kvm_s390_handle_aa(struct kvm_vcpu
*vcpu
)
50 if ((vcpu
->arch
.sie_block
->ipa
& 0xf) <= 4)
51 return handle_ri(vcpu
);
56 static int handle_gs(struct kvm_vcpu
*vcpu
)
58 if (test_kvm_facility(vcpu
->kvm
, 133)) {
59 VCPU_EVENT(vcpu
, 3, "%s", "ENABLE: GS (lazy)");
62 current
->thread
.gs_cb
= (struct gs_cb
*)&vcpu
->run
->s
.regs
.gscb
;
63 restore_gs_cb(current
->thread
.gs_cb
);
65 vcpu
->arch
.sie_block
->ecb
|= ECB_GS
;
66 vcpu
->arch
.sie_block
->ecd
|= ECD_HOSTREGMGMT
;
67 vcpu
->arch
.gs_enabled
= 1;
68 kvm_s390_retry_instr(vcpu
);
71 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
74 int kvm_s390_handle_e3(struct kvm_vcpu
*vcpu
)
76 int code
= vcpu
->arch
.sie_block
->ipb
& 0xff;
78 if (code
== 0x49 || code
== 0x4d)
79 return handle_gs(vcpu
);
83 /* Handle SCK (SET CLOCK) interception */
84 static int handle_set_clock(struct kvm_vcpu
*vcpu
)
90 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
91 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
93 op2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
94 if (op2
& 7) /* Operand must be on a doubleword boundary */
95 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
96 rc
= read_guest(vcpu
, op2
, ar
, &val
, sizeof(val
));
98 return kvm_s390_inject_prog_cond(vcpu
, rc
);
100 VCPU_EVENT(vcpu
, 3, "SCK: setting guest TOD to 0x%llx", val
);
101 kvm_s390_set_tod_clock(vcpu
->kvm
, val
);
103 kvm_s390_set_psw_cc(vcpu
, 0);
107 static int handle_set_prefix(struct kvm_vcpu
*vcpu
)
114 vcpu
->stat
.instruction_spx
++;
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
);
126 rc
= read_guest(vcpu
, operand2
, ar
, &address
, sizeof(address
));
128 return kvm_s390_inject_prog_cond(vcpu
, rc
);
130 address
&= 0x7fffe000u
;
133 * Make sure the new value is valid memory. We only need to check the
134 * first page, since address is 8k aligned and memory pieces are always
135 * at least 1MB aligned and have at least a size of 1MB.
137 if (kvm_is_error_gpa(vcpu
->kvm
, address
))
138 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
140 kvm_s390_set_prefix(vcpu
, address
);
141 trace_kvm_s390_handle_prefix(vcpu
, 1, address
);
145 static int handle_store_prefix(struct kvm_vcpu
*vcpu
)
152 vcpu
->stat
.instruction_stpx
++;
154 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
155 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
157 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
159 /* must be word boundary */
161 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
163 address
= kvm_s390_get_prefix(vcpu
);
166 rc
= write_guest(vcpu
, operand2
, ar
, &address
, sizeof(address
));
168 return kvm_s390_inject_prog_cond(vcpu
, rc
);
170 VCPU_EVENT(vcpu
, 3, "STPX: storing prefix 0x%x into 0x%llx", address
, operand2
);
171 trace_kvm_s390_handle_prefix(vcpu
, 0, address
);
175 static int handle_store_cpu_address(struct kvm_vcpu
*vcpu
)
177 u16 vcpu_id
= vcpu
->vcpu_id
;
182 vcpu
->stat
.instruction_stap
++;
184 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
185 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
187 ga
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
190 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
192 rc
= write_guest(vcpu
, ga
, ar
, &vcpu_id
, sizeof(vcpu_id
));
194 return kvm_s390_inject_prog_cond(vcpu
, rc
);
196 VCPU_EVENT(vcpu
, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id
, ga
);
197 trace_kvm_s390_handle_stap(vcpu
, ga
);
201 int kvm_s390_skey_check_enable(struct kvm_vcpu
*vcpu
)
204 struct kvm_s390_sie_block
*sie_block
= vcpu
->arch
.sie_block
;
206 trace_kvm_s390_skey_related_inst(vcpu
);
207 if (!(sie_block
->ictl
& (ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
)) &&
208 !(atomic_read(&sie_block
->cpuflags
) & CPUSTAT_KSS
))
211 rc
= s390_enable_skey();
212 VCPU_EVENT(vcpu
, 3, "enabling storage keys for guest: %d", rc
);
214 if (atomic_read(&sie_block
->cpuflags
) & CPUSTAT_KSS
)
215 atomic_andnot(CPUSTAT_KSS
, &sie_block
->cpuflags
);
217 sie_block
->ictl
&= ~(ICTL_ISKE
| ICTL_SSKE
|
223 static int try_handle_skey(struct kvm_vcpu
*vcpu
)
227 vcpu
->stat
.instruction_storage_key
++;
228 rc
= kvm_s390_skey_check_enable(vcpu
);
232 /* with storage-key facility, SIE interprets it for us */
233 kvm_s390_retry_instr(vcpu
);
234 VCPU_EVENT(vcpu
, 4, "%s", "retrying storage key operation");
237 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
238 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
242 static int handle_iske(struct kvm_vcpu
*vcpu
)
249 rc
= try_handle_skey(vcpu
);
251 return rc
!= -EAGAIN
? rc
: 0;
253 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
255 addr
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
256 addr
= kvm_s390_logical_to_effective(vcpu
, addr
);
257 addr
= kvm_s390_real_to_abs(vcpu
, addr
);
258 addr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(addr
));
259 if (kvm_is_error_hva(addr
))
260 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
262 down_read(¤t
->mm
->mmap_sem
);
263 rc
= get_guest_storage_key(current
->mm
, addr
, &key
);
264 up_read(¤t
->mm
->mmap_sem
);
266 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
267 vcpu
->run
->s
.regs
.gprs
[reg1
] &= ~0xff;
268 vcpu
->run
->s
.regs
.gprs
[reg1
] |= key
;
272 static int handle_rrbe(struct kvm_vcpu
*vcpu
)
278 rc
= try_handle_skey(vcpu
);
280 return rc
!= -EAGAIN
? rc
: 0;
282 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
284 addr
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
285 addr
= kvm_s390_logical_to_effective(vcpu
, addr
);
286 addr
= kvm_s390_real_to_abs(vcpu
, addr
);
287 addr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(addr
));
288 if (kvm_is_error_hva(addr
))
289 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
291 down_read(¤t
->mm
->mmap_sem
);
292 rc
= reset_guest_reference_bit(current
->mm
, addr
);
293 up_read(¤t
->mm
->mmap_sem
);
295 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
297 kvm_s390_set_psw_cc(vcpu
, rc
);
305 static int handle_sske(struct kvm_vcpu
*vcpu
)
307 unsigned char m3
= vcpu
->arch
.sie_block
->ipb
>> 28;
308 unsigned long start
, end
;
309 unsigned char key
, oldkey
;
313 rc
= try_handle_skey(vcpu
);
315 return rc
!= -EAGAIN
? rc
: 0;
317 if (!test_kvm_facility(vcpu
->kvm
, 8))
319 if (!test_kvm_facility(vcpu
->kvm
, 10))
320 m3
&= ~(SSKE_MC
| SSKE_MR
);
321 if (!test_kvm_facility(vcpu
->kvm
, 14))
324 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
326 key
= vcpu
->run
->s
.regs
.gprs
[reg1
] & 0xfe;
327 start
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
328 start
= kvm_s390_logical_to_effective(vcpu
, start
);
330 /* start already designates an absolute address */
331 end
= (start
+ (1UL << 20)) & ~((1UL << 20) - 1);
333 start
= kvm_s390_real_to_abs(vcpu
, start
);
334 end
= start
+ PAGE_SIZE
;
337 while (start
!= end
) {
338 unsigned long addr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(start
));
340 if (kvm_is_error_hva(addr
))
341 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
343 down_read(¤t
->mm
->mmap_sem
);
344 rc
= cond_set_guest_storage_key(current
->mm
, addr
, key
, &oldkey
,
345 m3
& SSKE_NQ
, m3
& SSKE_MR
,
347 up_read(¤t
->mm
->mmap_sem
);
349 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
353 if (m3
& (SSKE_MC
| SSKE_MR
)) {
355 /* skey in reg1 is unpredictable */
356 kvm_s390_set_psw_cc(vcpu
, 3);
358 kvm_s390_set_psw_cc(vcpu
, rc
);
359 vcpu
->run
->s
.regs
.gprs
[reg1
] &= ~0xff00UL
;
360 vcpu
->run
->s
.regs
.gprs
[reg1
] |= (u64
) oldkey
<< 8;
364 if (psw_bits(vcpu
->arch
.sie_block
->gpsw
).eaba
== PSW_AMODE_64BIT
)
365 vcpu
->run
->s
.regs
.gprs
[reg2
] &= ~PAGE_MASK
;
367 vcpu
->run
->s
.regs
.gprs
[reg2
] &= ~0xfffff000UL
;
368 end
= kvm_s390_logical_to_effective(vcpu
, end
);
369 vcpu
->run
->s
.regs
.gprs
[reg2
] |= end
;
374 static int handle_ipte_interlock(struct kvm_vcpu
*vcpu
)
376 vcpu
->stat
.instruction_ipte_interlock
++;
377 if (psw_bits(vcpu
->arch
.sie_block
->gpsw
).p
)
378 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
379 wait_event(vcpu
->kvm
->arch
.ipte_wq
, !ipte_lock_held(vcpu
));
380 kvm_s390_retry_instr(vcpu
);
381 VCPU_EVENT(vcpu
, 4, "%s", "retrying ipte interlock operation");
385 static int handle_test_block(struct kvm_vcpu
*vcpu
)
390 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
391 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
393 kvm_s390_get_regs_rre(vcpu
, NULL
, ®2
);
394 addr
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
395 addr
= kvm_s390_logical_to_effective(vcpu
, addr
);
396 if (kvm_s390_check_low_addr_prot_real(vcpu
, addr
))
397 return kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
398 addr
= kvm_s390_real_to_abs(vcpu
, addr
);
400 if (kvm_is_error_gpa(vcpu
->kvm
, addr
))
401 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
403 * We don't expect errors on modern systems, and do not care
404 * about storage keys (yet), so let's just clear the page.
406 if (kvm_clear_guest(vcpu
->kvm
, addr
, PAGE_SIZE
))
408 kvm_s390_set_psw_cc(vcpu
, 0);
409 vcpu
->run
->s
.regs
.gprs
[0] = 0;
413 static int handle_tpi(struct kvm_vcpu
*vcpu
)
415 struct kvm_s390_interrupt_info
*inti
;
422 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
424 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
426 inti
= kvm_s390_get_io_int(vcpu
->kvm
, vcpu
->arch
.sie_block
->gcr
[6], 0);
428 kvm_s390_set_psw_cc(vcpu
, 0);
432 tpi_data
[0] = inti
->io
.subchannel_id
<< 16 | inti
->io
.subchannel_nr
;
433 tpi_data
[1] = inti
->io
.io_int_parm
;
434 tpi_data
[2] = inti
->io
.io_int_word
;
437 * Store the two-word I/O interruption code into the
440 len
= sizeof(tpi_data
) - 4;
441 rc
= write_guest(vcpu
, addr
, ar
, &tpi_data
, len
);
443 rc
= kvm_s390_inject_prog_cond(vcpu
, rc
);
444 goto reinject_interrupt
;
448 * Store the three-word I/O interruption code into
449 * the appropriate lowcore area.
451 len
= sizeof(tpi_data
);
452 if (write_guest_lc(vcpu
, __LC_SUBCHANNEL_ID
, &tpi_data
, len
)) {
453 /* failed writes to the low core are not recoverable */
455 goto reinject_interrupt
;
459 /* irq was successfully handed to the guest */
461 kvm_s390_set_psw_cc(vcpu
, 1);
465 * If we encounter a problem storing the interruption code, the
466 * instruction is suppressed from the guest's view: reinject the
469 if (kvm_s390_reinject_io_int(vcpu
->kvm
, inti
)) {
473 /* don't set the cc, a pgm irq was injected or we drop to user space */
474 return rc
? -EFAULT
: 0;
477 static int handle_tsch(struct kvm_vcpu
*vcpu
)
479 struct kvm_s390_interrupt_info
*inti
= NULL
;
480 const u64 isc_mask
= 0xffUL
<< 24; /* all iscs set */
482 /* a valid schid has at least one bit set */
483 if (vcpu
->run
->s
.regs
.gprs
[1])
484 inti
= kvm_s390_get_io_int(vcpu
->kvm
, isc_mask
,
485 vcpu
->run
->s
.regs
.gprs
[1]);
488 * Prepare exit to userspace.
489 * We indicate whether we dequeued a pending I/O interrupt
490 * so that userspace can re-inject it if the instruction gets
491 * a program check. While this may re-order the pending I/O
492 * interrupts, this is no problem since the priority is kept
495 vcpu
->run
->exit_reason
= KVM_EXIT_S390_TSCH
;
496 vcpu
->run
->s390_tsch
.dequeued
= !!inti
;
498 vcpu
->run
->s390_tsch
.subchannel_id
= inti
->io
.subchannel_id
;
499 vcpu
->run
->s390_tsch
.subchannel_nr
= inti
->io
.subchannel_nr
;
500 vcpu
->run
->s390_tsch
.io_int_parm
= inti
->io
.io_int_parm
;
501 vcpu
->run
->s390_tsch
.io_int_word
= inti
->io
.io_int_word
;
503 vcpu
->run
->s390_tsch
.ipb
= vcpu
->arch
.sie_block
->ipb
;
508 static int handle_io_inst(struct kvm_vcpu
*vcpu
)
510 VCPU_EVENT(vcpu
, 4, "%s", "I/O instruction");
512 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
513 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
515 if (vcpu
->kvm
->arch
.css_support
) {
517 * Most I/O instructions will be handled by userspace.
518 * Exceptions are tpi and the interrupt portion of tsch.
520 if (vcpu
->arch
.sie_block
->ipa
== 0xb236)
521 return handle_tpi(vcpu
);
522 if (vcpu
->arch
.sie_block
->ipa
== 0xb235)
523 return handle_tsch(vcpu
);
524 /* Handle in userspace. */
528 * Set condition code 3 to stop the guest from issuing channel
531 kvm_s390_set_psw_cc(vcpu
, 3);
536 static int handle_stfl(struct kvm_vcpu
*vcpu
)
541 vcpu
->stat
.instruction_stfl
++;
543 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
544 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
547 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
548 * into a u32 memory representation. They will remain bits 0-31.
550 fac
= *vcpu
->kvm
->arch
.model
.fac_list
>> 32;
551 rc
= write_guest_lc(vcpu
, offsetof(struct lowcore
, stfl_fac_list
),
555 VCPU_EVENT(vcpu
, 3, "STFL: store facility list 0x%x", fac
);
556 trace_kvm_s390_handle_stfl(vcpu
, fac
);
560 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
561 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
562 #define PSW_ADDR_24 0x0000000000ffffffUL
563 #define PSW_ADDR_31 0x000000007fffffffUL
565 int is_valid_psw(psw_t
*psw
)
567 if (psw
->mask
& PSW_MASK_UNASSIGNED
)
569 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_BA
) {
570 if (psw
->addr
& ~PSW_ADDR_31
)
573 if (!(psw
->mask
& PSW_MASK_ADDR_MODE
) && (psw
->addr
& ~PSW_ADDR_24
))
575 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_EA
)
582 int kvm_s390_handle_lpsw(struct kvm_vcpu
*vcpu
)
584 psw_t
*gpsw
= &vcpu
->arch
.sie_block
->gpsw
;
585 psw_compat_t new_psw
;
590 if (gpsw
->mask
& PSW_MASK_PSTATE
)
591 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
593 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
595 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
597 rc
= read_guest(vcpu
, addr
, ar
, &new_psw
, sizeof(new_psw
));
599 return kvm_s390_inject_prog_cond(vcpu
, rc
);
600 if (!(new_psw
.mask
& PSW32_MASK_BASE
))
601 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
602 gpsw
->mask
= (new_psw
.mask
& ~PSW32_MASK_BASE
) << 32;
603 gpsw
->mask
|= new_psw
.addr
& PSW32_ADDR_AMODE
;
604 gpsw
->addr
= new_psw
.addr
& ~PSW32_ADDR_AMODE
;
605 if (!is_valid_psw(gpsw
))
606 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
610 static int handle_lpswe(struct kvm_vcpu
*vcpu
)
617 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
618 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
620 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
622 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
623 rc
= read_guest(vcpu
, addr
, ar
, &new_psw
, sizeof(new_psw
));
625 return kvm_s390_inject_prog_cond(vcpu
, rc
);
626 vcpu
->arch
.sie_block
->gpsw
= new_psw
;
627 if (!is_valid_psw(&vcpu
->arch
.sie_block
->gpsw
))
628 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
632 static int handle_stidp(struct kvm_vcpu
*vcpu
)
634 u64 stidp_data
= vcpu
->kvm
->arch
.model
.cpuid
;
639 vcpu
->stat
.instruction_stidp
++;
641 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
642 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
644 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
647 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
649 rc
= write_guest(vcpu
, operand2
, ar
, &stidp_data
, sizeof(stidp_data
));
651 return kvm_s390_inject_prog_cond(vcpu
, rc
);
653 VCPU_EVENT(vcpu
, 3, "STIDP: store cpu id 0x%llx", stidp_data
);
657 static void handle_stsi_3_2_2(struct kvm_vcpu
*vcpu
, struct sysinfo_3_2_2
*mem
)
662 cpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
664 /* deal with other level 3 hypervisors */
665 if (stsi(mem
, 3, 2, 2))
669 for (n
= mem
->count
- 1; n
> 0 ; n
--)
670 memcpy(&mem
->vm
[n
], &mem
->vm
[n
- 1], sizeof(mem
->vm
[0]));
672 memset(&mem
->vm
[0], 0, sizeof(mem
->vm
[0]));
673 mem
->vm
[0].cpus_total
= cpus
;
674 mem
->vm
[0].cpus_configured
= cpus
;
675 mem
->vm
[0].cpus_standby
= 0;
676 mem
->vm
[0].cpus_reserved
= 0;
677 mem
->vm
[0].caf
= 1000;
678 memcpy(mem
->vm
[0].name
, "KVMguest", 8);
679 ASCEBC(mem
->vm
[0].name
, 8);
680 memcpy(mem
->vm
[0].cpi
, "KVM/Linux ", 16);
681 ASCEBC(mem
->vm
[0].cpi
, 16);
684 static void insert_stsi_usr_data(struct kvm_vcpu
*vcpu
, u64 addr
, u8 ar
,
685 u8 fc
, u8 sel1
, u16 sel2
)
687 vcpu
->run
->exit_reason
= KVM_EXIT_S390_STSI
;
688 vcpu
->run
->s390_stsi
.addr
= addr
;
689 vcpu
->run
->s390_stsi
.ar
= ar
;
690 vcpu
->run
->s390_stsi
.fc
= fc
;
691 vcpu
->run
->s390_stsi
.sel1
= sel1
;
692 vcpu
->run
->s390_stsi
.sel2
= sel2
;
695 static int handle_stsi(struct kvm_vcpu
*vcpu
)
697 int fc
= (vcpu
->run
->s
.regs
.gprs
[0] & 0xf0000000) >> 28;
698 int sel1
= vcpu
->run
->s
.regs
.gprs
[0] & 0xff;
699 int sel2
= vcpu
->run
->s
.regs
.gprs
[1] & 0xffff;
700 unsigned long mem
= 0;
705 vcpu
->stat
.instruction_stsi
++;
706 VCPU_EVENT(vcpu
, 3, "STSI: fc: %u sel1: %u sel2: %u", fc
, sel1
, sel2
);
708 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
709 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
712 kvm_s390_set_psw_cc(vcpu
, 3);
716 if (vcpu
->run
->s
.regs
.gprs
[0] & 0x0fffff00
717 || vcpu
->run
->s
.regs
.gprs
[1] & 0xffff0000)
718 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
721 vcpu
->run
->s
.regs
.gprs
[0] = 3 << 28;
722 kvm_s390_set_psw_cc(vcpu
, 0);
726 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
728 if (operand2
& 0xfff)
729 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
732 case 1: /* same handling for 1 and 2 */
734 mem
= get_zeroed_page(GFP_KERNEL
);
737 if (stsi((void *) mem
, fc
, sel1
, sel2
))
741 if (sel1
!= 2 || sel2
!= 2)
743 mem
= get_zeroed_page(GFP_KERNEL
);
746 handle_stsi_3_2_2(vcpu
, (void *) mem
);
750 rc
= write_guest(vcpu
, operand2
, ar
, (void *)mem
, PAGE_SIZE
);
752 rc
= kvm_s390_inject_prog_cond(vcpu
, rc
);
755 if (vcpu
->kvm
->arch
.user_stsi
) {
756 insert_stsi_usr_data(vcpu
, operand2
, ar
, fc
, sel1
, sel2
);
759 trace_kvm_s390_handle_stsi(vcpu
, fc
, sel1
, sel2
, operand2
);
761 kvm_s390_set_psw_cc(vcpu
, 0);
762 vcpu
->run
->s
.regs
.gprs
[0] = 0;
765 kvm_s390_set_psw_cc(vcpu
, 3);
771 static const intercept_handler_t b2_handlers
[256] = {
772 [0x02] = handle_stidp
,
773 [0x04] = handle_set_clock
,
774 [0x10] = handle_set_prefix
,
775 [0x11] = handle_store_prefix
,
776 [0x12] = handle_store_cpu_address
,
777 [0x14] = kvm_s390_handle_vsie
,
778 [0x21] = handle_ipte_interlock
,
779 [0x29] = handle_iske
,
780 [0x2a] = handle_rrbe
,
781 [0x2b] = handle_sske
,
782 [0x2c] = handle_test_block
,
783 [0x30] = handle_io_inst
,
784 [0x31] = handle_io_inst
,
785 [0x32] = handle_io_inst
,
786 [0x33] = handle_io_inst
,
787 [0x34] = handle_io_inst
,
788 [0x35] = handle_io_inst
,
789 [0x36] = handle_io_inst
,
790 [0x37] = handle_io_inst
,
791 [0x38] = handle_io_inst
,
792 [0x39] = handle_io_inst
,
793 [0x3a] = handle_io_inst
,
794 [0x3b] = handle_io_inst
,
795 [0x3c] = handle_io_inst
,
796 [0x50] = handle_ipte_interlock
,
797 [0x56] = handle_sthyi
,
798 [0x5f] = handle_io_inst
,
799 [0x74] = handle_io_inst
,
800 [0x76] = handle_io_inst
,
801 [0x7d] = handle_stsi
,
802 [0xb1] = handle_stfl
,
803 [0xb2] = handle_lpswe
,
806 int kvm_s390_handle_b2(struct kvm_vcpu
*vcpu
)
808 intercept_handler_t handler
;
811 * A lot of B2 instructions are priviledged. Here we check for
812 * the privileged ones, that we can handle in the kernel.
813 * Anything else goes to userspace.
815 handler
= b2_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
817 return handler(vcpu
);
822 static int handle_epsw(struct kvm_vcpu
*vcpu
)
826 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
828 /* This basically extracts the mask half of the psw. */
829 vcpu
->run
->s
.regs
.gprs
[reg1
] &= 0xffffffff00000000UL
;
830 vcpu
->run
->s
.regs
.gprs
[reg1
] |= vcpu
->arch
.sie_block
->gpsw
.mask
>> 32;
832 vcpu
->run
->s
.regs
.gprs
[reg2
] &= 0xffffffff00000000UL
;
833 vcpu
->run
->s
.regs
.gprs
[reg2
] |=
834 vcpu
->arch
.sie_block
->gpsw
.mask
& 0x00000000ffffffffUL
;
839 #define PFMF_RESERVED 0xfffc0101UL
840 #define PFMF_SK 0x00020000UL
841 #define PFMF_CF 0x00010000UL
842 #define PFMF_UI 0x00008000UL
843 #define PFMF_FSC 0x00007000UL
844 #define PFMF_NQ 0x00000800UL
845 #define PFMF_MR 0x00000400UL
846 #define PFMF_MC 0x00000200UL
847 #define PFMF_KEY 0x000000feUL
849 static int handle_pfmf(struct kvm_vcpu
*vcpu
)
851 bool mr
= false, mc
= false, nq
;
853 unsigned long start
, end
;
856 vcpu
->stat
.instruction_pfmf
++;
858 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
860 if (!test_kvm_facility(vcpu
->kvm
, 8))
861 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
863 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
864 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
866 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_RESERVED
)
867 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
869 /* Only provide non-quiescing support if enabled for the guest */
870 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
&&
871 !test_kvm_facility(vcpu
->kvm
, 14))
872 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
874 /* Only provide conditional-SSKE support if enabled for the guest */
875 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_SK
&&
876 test_kvm_facility(vcpu
->kvm
, 10)) {
877 mr
= vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_MR
;
878 mc
= vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_MC
;
881 nq
= vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
;
882 key
= vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_KEY
;
883 start
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
884 start
= kvm_s390_logical_to_effective(vcpu
, start
);
886 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
887 if (kvm_s390_check_low_addr_prot_real(vcpu
, start
))
888 return kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
891 switch (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) {
893 /* only 4k frames specify a real address */
894 start
= kvm_s390_real_to_abs(vcpu
, start
);
895 end
= (start
+ (1UL << 12)) & ~((1UL << 12) - 1);
898 end
= (start
+ (1UL << 20)) & ~((1UL << 20) - 1);
901 /* only support 2G frame size if EDAT2 is available and we are
902 not in 24-bit addressing mode */
903 if (!test_kvm_facility(vcpu
->kvm
, 78) ||
904 psw_bits(vcpu
->arch
.sie_block
->gpsw
).eaba
== PSW_AMODE_24BIT
)
905 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
906 end
= (start
+ (1UL << 31)) & ~((1UL << 31) - 1);
909 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
912 while (start
!= end
) {
913 unsigned long useraddr
;
915 /* Translate guest address to host address */
916 useraddr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(start
));
917 if (kvm_is_error_hva(useraddr
))
918 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
920 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
921 if (clear_user((void __user
*)useraddr
, PAGE_SIZE
))
922 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
925 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_SK
) {
926 int rc
= kvm_s390_skey_check_enable(vcpu
);
930 down_read(¤t
->mm
->mmap_sem
);
931 rc
= cond_set_guest_storage_key(current
->mm
, useraddr
,
932 key
, NULL
, nq
, mr
, mc
);
933 up_read(¤t
->mm
->mmap_sem
);
935 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
940 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) {
941 if (psw_bits(vcpu
->arch
.sie_block
->gpsw
).eaba
== PSW_AMODE_64BIT
) {
942 vcpu
->run
->s
.regs
.gprs
[reg2
] = end
;
944 vcpu
->run
->s
.regs
.gprs
[reg2
] &= ~0xffffffffUL
;
945 end
= kvm_s390_logical_to_effective(vcpu
, end
);
946 vcpu
->run
->s
.regs
.gprs
[reg2
] |= end
;
952 static int handle_essa(struct kvm_vcpu
*vcpu
)
954 /* entries expected to be 1FF */
955 int entries
= (vcpu
->arch
.sie_block
->cbrlo
& ~PAGE_MASK
) >> 3;
956 unsigned long *cbrlo
;
960 VCPU_EVENT(vcpu
, 4, "ESSA: release %d pages", entries
);
961 gmap
= vcpu
->arch
.gmap
;
962 vcpu
->stat
.instruction_essa
++;
963 if (!vcpu
->kvm
->arch
.use_cmma
)
964 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
966 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
967 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
969 if (((vcpu
->arch
.sie_block
->ipb
& 0xf0000000) >> 28) > 6)
970 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
972 /* Retry the ESSA instruction */
973 kvm_s390_retry_instr(vcpu
);
974 vcpu
->arch
.sie_block
->cbrlo
&= PAGE_MASK
; /* reset nceo */
975 cbrlo
= phys_to_virt(vcpu
->arch
.sie_block
->cbrlo
);
976 down_read(&gmap
->mm
->mmap_sem
);
977 for (i
= 0; i
< entries
; ++i
)
978 __gmap_zap(gmap
, cbrlo
[i
]);
979 up_read(&gmap
->mm
->mmap_sem
);
983 static const intercept_handler_t b9_handlers
[256] = {
984 [0x8a] = handle_ipte_interlock
,
985 [0x8d] = handle_epsw
,
986 [0x8e] = handle_ipte_interlock
,
987 [0x8f] = handle_ipte_interlock
,
988 [0xab] = handle_essa
,
989 [0xaf] = handle_pfmf
,
992 int kvm_s390_handle_b9(struct kvm_vcpu
*vcpu
)
994 intercept_handler_t handler
;
996 /* This is handled just as for the B2 instructions. */
997 handler
= b9_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
999 return handler(vcpu
);
1004 int kvm_s390_handle_lctl(struct kvm_vcpu
*vcpu
)
1006 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
1007 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
1008 int reg
, rc
, nr_regs
;
1013 vcpu
->stat
.instruction_lctl
++;
1015 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1016 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1018 ga
= kvm_s390_get_base_disp_rs(vcpu
, &ar
);
1021 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
1023 VCPU_EVENT(vcpu
, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
1024 trace_kvm_s390_handle_lctl(vcpu
, 0, reg1
, reg3
, ga
);
1026 nr_regs
= ((reg3
- reg1
) & 0xf) + 1;
1027 rc
= read_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u32
));
1029 return kvm_s390_inject_prog_cond(vcpu
, rc
);
1033 vcpu
->arch
.sie_block
->gcr
[reg
] &= 0xffffffff00000000ul
;
1034 vcpu
->arch
.sie_block
->gcr
[reg
] |= ctl_array
[nr_regs
++];
1037 reg
= (reg
+ 1) % 16;
1039 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
1043 int kvm_s390_handle_stctl(struct kvm_vcpu
*vcpu
)
1045 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
1046 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
1047 int reg
, rc
, nr_regs
;
1052 vcpu
->stat
.instruction_stctl
++;
1054 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1055 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1057 ga
= kvm_s390_get_base_disp_rs(vcpu
, &ar
);
1060 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
1062 VCPU_EVENT(vcpu
, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
1063 trace_kvm_s390_handle_stctl(vcpu
, 0, reg1
, reg3
, ga
);
1068 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
1071 reg
= (reg
+ 1) % 16;
1073 rc
= write_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u32
));
1074 return rc
? kvm_s390_inject_prog_cond(vcpu
, rc
) : 0;
1077 static int handle_lctlg(struct kvm_vcpu
*vcpu
)
1079 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
1080 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
1081 int reg
, rc
, nr_regs
;
1086 vcpu
->stat
.instruction_lctlg
++;
1088 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1089 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1091 ga
= kvm_s390_get_base_disp_rsy(vcpu
, &ar
);
1094 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
1096 VCPU_EVENT(vcpu
, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
1097 trace_kvm_s390_handle_lctl(vcpu
, 1, reg1
, reg3
, ga
);
1099 nr_regs
= ((reg3
- reg1
) & 0xf) + 1;
1100 rc
= read_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u64
));
1102 return kvm_s390_inject_prog_cond(vcpu
, rc
);
1106 vcpu
->arch
.sie_block
->gcr
[reg
] = ctl_array
[nr_regs
++];
1109 reg
= (reg
+ 1) % 16;
1111 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
1115 static int handle_stctg(struct kvm_vcpu
*vcpu
)
1117 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
1118 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
1119 int reg
, rc
, nr_regs
;
1124 vcpu
->stat
.instruction_stctg
++;
1126 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1127 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1129 ga
= kvm_s390_get_base_disp_rsy(vcpu
, &ar
);
1132 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
1134 VCPU_EVENT(vcpu
, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
1135 trace_kvm_s390_handle_stctl(vcpu
, 1, reg1
, reg3
, ga
);
1140 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
1143 reg
= (reg
+ 1) % 16;
1145 rc
= write_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u64
));
1146 return rc
? kvm_s390_inject_prog_cond(vcpu
, rc
) : 0;
1149 static const intercept_handler_t eb_handlers
[256] = {
1150 [0x2f] = handle_lctlg
,
1151 [0x25] = handle_stctg
,
1157 int kvm_s390_handle_eb(struct kvm_vcpu
*vcpu
)
1159 intercept_handler_t handler
;
1161 handler
= eb_handlers
[vcpu
->arch
.sie_block
->ipb
& 0xff];
1163 return handler(vcpu
);
1167 static int handle_tprot(struct kvm_vcpu
*vcpu
)
1169 u64 address1
, address2
;
1170 unsigned long hva
, gpa
;
1171 int ret
= 0, cc
= 0;
1175 vcpu
->stat
.instruction_tprot
++;
1177 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1178 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1180 kvm_s390_get_base_disp_sse(vcpu
, &address1
, &address2
, &ar
, NULL
);
1182 /* we only handle the Linux memory detection case:
1184 * everything else goes to userspace. */
1185 if (address2
& 0xf0)
1187 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
1189 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
, GACC_STORE
);
1190 if (ret
== PGM_PROTECTION
) {
1191 /* Write protected? Try again with read-only... */
1193 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
,
1197 if (ret
== PGM_ADDRESSING
|| ret
== PGM_TRANSLATION_SPEC
) {
1198 ret
= kvm_s390_inject_program_int(vcpu
, ret
);
1199 } else if (ret
> 0) {
1200 /* Translation not available */
1201 kvm_s390_set_psw_cc(vcpu
, 3);
1207 hva
= gfn_to_hva_prot(vcpu
->kvm
, gpa_to_gfn(gpa
), &writable
);
1208 if (kvm_is_error_hva(hva
)) {
1209 ret
= kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
1212 cc
= 1; /* Write not permitted ==> read-only */
1213 kvm_s390_set_psw_cc(vcpu
, cc
);
1214 /* Note: CC2 only occurs for storage keys (not supported yet) */
1217 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
1222 int kvm_s390_handle_e5(struct kvm_vcpu
*vcpu
)
1224 /* For e5xx... instructions we only handle TPROT */
1225 if ((vcpu
->arch
.sie_block
->ipa
& 0x00ff) == 0x01)
1226 return handle_tprot(vcpu
);
1230 static int handle_sckpf(struct kvm_vcpu
*vcpu
)
1234 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1235 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1237 if (vcpu
->run
->s
.regs
.gprs
[0] & 0x00000000ffff0000)
1238 return kvm_s390_inject_program_int(vcpu
,
1241 value
= vcpu
->run
->s
.regs
.gprs
[0] & 0x000000000000ffff;
1242 vcpu
->arch
.sie_block
->todpr
= value
;
1247 static int handle_ptff(struct kvm_vcpu
*vcpu
)
1249 /* we don't emulate any control instructions yet */
1250 kvm_s390_set_psw_cc(vcpu
, 3);
1254 static const intercept_handler_t x01_handlers
[256] = {
1255 [0x04] = handle_ptff
,
1256 [0x07] = handle_sckpf
,
1259 int kvm_s390_handle_01(struct kvm_vcpu
*vcpu
)
1261 intercept_handler_t handler
;
1263 handler
= x01_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
1265 return handler(vcpu
);