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>
35 /* Handle SCK (SET CLOCK) interception */
36 static int handle_set_clock(struct kvm_vcpu
*vcpu
)
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 VCPU_EVENT(vcpu
, 3, "SCK: setting guest TOD to 0x%llx", val
);
53 kvm_s390_set_tod_clock(vcpu
->kvm
, val
);
55 kvm_s390_set_psw_cc(vcpu
, 0);
59 static int handle_set_prefix(struct kvm_vcpu
*vcpu
)
66 vcpu
->stat
.instruction_spx
++;
68 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
69 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
71 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
73 /* must be word boundary */
75 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
78 rc
= read_guest(vcpu
, operand2
, ar
, &address
, sizeof(address
));
80 return kvm_s390_inject_prog_cond(vcpu
, rc
);
82 address
&= 0x7fffe000u
;
85 * Make sure the new value is valid memory. We only need to check the
86 * first page, since address is 8k aligned and memory pieces are always
87 * at least 1MB aligned and have at least a size of 1MB.
89 if (kvm_is_error_gpa(vcpu
->kvm
, address
))
90 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
92 kvm_s390_set_prefix(vcpu
, address
);
93 trace_kvm_s390_handle_prefix(vcpu
, 1, address
);
97 static int handle_store_prefix(struct kvm_vcpu
*vcpu
)
104 vcpu
->stat
.instruction_stpx
++;
106 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
107 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
109 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
111 /* must be word boundary */
113 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
115 address
= kvm_s390_get_prefix(vcpu
);
118 rc
= write_guest(vcpu
, operand2
, ar
, &address
, sizeof(address
));
120 return kvm_s390_inject_prog_cond(vcpu
, rc
);
122 VCPU_EVENT(vcpu
, 3, "STPX: storing prefix 0x%x into 0x%llx", address
, operand2
);
123 trace_kvm_s390_handle_prefix(vcpu
, 0, address
);
127 static int handle_store_cpu_address(struct kvm_vcpu
*vcpu
)
129 u16 vcpu_id
= vcpu
->vcpu_id
;
134 vcpu
->stat
.instruction_stap
++;
136 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
137 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
139 ga
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
142 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
144 rc
= write_guest(vcpu
, ga
, ar
, &vcpu_id
, sizeof(vcpu_id
));
146 return kvm_s390_inject_prog_cond(vcpu
, rc
);
148 VCPU_EVENT(vcpu
, 3, "STAP: storing cpu address (%u) to 0x%llx", vcpu_id
, ga
);
149 trace_kvm_s390_handle_stap(vcpu
, ga
);
153 static int __skey_check_enable(struct kvm_vcpu
*vcpu
)
157 trace_kvm_s390_skey_related_inst(vcpu
);
158 if (!(vcpu
->arch
.sie_block
->ictl
& (ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
)))
161 rc
= s390_enable_skey();
162 VCPU_EVENT(vcpu
, 3, "enabling storage keys for guest: %d", rc
);
164 vcpu
->arch
.sie_block
->ictl
&= ~(ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
);
168 static int try_handle_skey(struct kvm_vcpu
*vcpu
)
172 vcpu
->stat
.instruction_storage_key
++;
173 rc
= __skey_check_enable(vcpu
);
177 /* with storage-key facility, SIE interprets it for us */
178 kvm_s390_retry_instr(vcpu
);
179 VCPU_EVENT(vcpu
, 4, "%s", "retrying storage key operation");
182 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
183 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
187 static int handle_iske(struct kvm_vcpu
*vcpu
)
194 rc
= try_handle_skey(vcpu
);
196 return rc
!= -EAGAIN
? rc
: 0;
198 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
200 addr
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
201 addr
= kvm_s390_logical_to_effective(vcpu
, addr
);
202 addr
= kvm_s390_real_to_abs(vcpu
, addr
);
203 addr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(addr
));
204 if (kvm_is_error_hva(addr
))
205 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
207 down_read(¤t
->mm
->mmap_sem
);
208 rc
= get_guest_storage_key(current
->mm
, addr
, &key
);
209 up_read(¤t
->mm
->mmap_sem
);
211 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
212 vcpu
->run
->s
.regs
.gprs
[reg1
] &= ~0xff;
213 vcpu
->run
->s
.regs
.gprs
[reg1
] |= key
;
217 static int handle_rrbe(struct kvm_vcpu
*vcpu
)
223 rc
= try_handle_skey(vcpu
);
225 return rc
!= -EAGAIN
? rc
: 0;
227 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
229 addr
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
230 addr
= kvm_s390_logical_to_effective(vcpu
, addr
);
231 addr
= kvm_s390_real_to_abs(vcpu
, addr
);
232 addr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(addr
));
233 if (kvm_is_error_hva(addr
))
234 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
236 down_read(¤t
->mm
->mmap_sem
);
237 rc
= reset_guest_reference_bit(current
->mm
, addr
);
238 up_read(¤t
->mm
->mmap_sem
);
240 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
242 kvm_s390_set_psw_cc(vcpu
, rc
);
250 static int handle_sske(struct kvm_vcpu
*vcpu
)
252 unsigned char m3
= vcpu
->arch
.sie_block
->ipb
>> 28;
253 unsigned long start
, end
;
254 unsigned char key
, oldkey
;
258 rc
= try_handle_skey(vcpu
);
260 return rc
!= -EAGAIN
? rc
: 0;
262 if (!test_kvm_facility(vcpu
->kvm
, 8))
264 if (!test_kvm_facility(vcpu
->kvm
, 10))
265 m3
&= ~(SSKE_MC
| SSKE_MR
);
266 if (!test_kvm_facility(vcpu
->kvm
, 14))
269 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
271 key
= vcpu
->run
->s
.regs
.gprs
[reg1
] & 0xfe;
272 start
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
273 start
= kvm_s390_logical_to_effective(vcpu
, start
);
275 /* start already designates an absolute address */
276 end
= (start
+ (1UL << 20)) & ~((1UL << 20) - 1);
278 start
= kvm_s390_real_to_abs(vcpu
, start
);
279 end
= start
+ PAGE_SIZE
;
282 while (start
!= end
) {
283 unsigned long addr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(start
));
285 if (kvm_is_error_hva(addr
))
286 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
288 down_read(¤t
->mm
->mmap_sem
);
289 rc
= cond_set_guest_storage_key(current
->mm
, addr
, key
, &oldkey
,
290 m3
& SSKE_NQ
, m3
& SSKE_MR
,
292 up_read(¤t
->mm
->mmap_sem
);
294 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
298 if (m3
& (SSKE_MC
| SSKE_MR
)) {
300 /* skey in reg1 is unpredictable */
301 kvm_s390_set_psw_cc(vcpu
, 3);
303 kvm_s390_set_psw_cc(vcpu
, rc
);
304 vcpu
->run
->s
.regs
.gprs
[reg1
] &= ~0xff00UL
;
305 vcpu
->run
->s
.regs
.gprs
[reg1
] |= (u64
) oldkey
<< 8;
309 if (psw_bits(vcpu
->arch
.sie_block
->gpsw
).eaba
== PSW_AMODE_64BIT
)
310 vcpu
->run
->s
.regs
.gprs
[reg2
] &= ~PAGE_MASK
;
312 vcpu
->run
->s
.regs
.gprs
[reg2
] &= ~0xfffff000UL
;
313 end
= kvm_s390_logical_to_effective(vcpu
, end
);
314 vcpu
->run
->s
.regs
.gprs
[reg2
] |= end
;
319 static int handle_ipte_interlock(struct kvm_vcpu
*vcpu
)
321 vcpu
->stat
.instruction_ipte_interlock
++;
322 if (psw_bits(vcpu
->arch
.sie_block
->gpsw
).p
)
323 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
324 wait_event(vcpu
->kvm
->arch
.ipte_wq
, !ipte_lock_held(vcpu
));
325 kvm_s390_retry_instr(vcpu
);
326 VCPU_EVENT(vcpu
, 4, "%s", "retrying ipte interlock operation");
330 static int handle_test_block(struct kvm_vcpu
*vcpu
)
335 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
336 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
338 kvm_s390_get_regs_rre(vcpu
, NULL
, ®2
);
339 addr
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
340 addr
= kvm_s390_logical_to_effective(vcpu
, addr
);
341 if (kvm_s390_check_low_addr_prot_real(vcpu
, addr
))
342 return kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
343 addr
= kvm_s390_real_to_abs(vcpu
, addr
);
345 if (kvm_is_error_gpa(vcpu
->kvm
, addr
))
346 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
348 * We don't expect errors on modern systems, and do not care
349 * about storage keys (yet), so let's just clear the page.
351 if (kvm_clear_guest(vcpu
->kvm
, addr
, PAGE_SIZE
))
353 kvm_s390_set_psw_cc(vcpu
, 0);
354 vcpu
->run
->s
.regs
.gprs
[0] = 0;
358 static int handle_tpi(struct kvm_vcpu
*vcpu
)
360 struct kvm_s390_interrupt_info
*inti
;
367 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
369 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
371 inti
= kvm_s390_get_io_int(vcpu
->kvm
, vcpu
->arch
.sie_block
->gcr
[6], 0);
373 kvm_s390_set_psw_cc(vcpu
, 0);
377 tpi_data
[0] = inti
->io
.subchannel_id
<< 16 | inti
->io
.subchannel_nr
;
378 tpi_data
[1] = inti
->io
.io_int_parm
;
379 tpi_data
[2] = inti
->io
.io_int_word
;
382 * Store the two-word I/O interruption code into the
385 len
= sizeof(tpi_data
) - 4;
386 rc
= write_guest(vcpu
, addr
, ar
, &tpi_data
, len
);
388 rc
= kvm_s390_inject_prog_cond(vcpu
, rc
);
389 goto reinject_interrupt
;
393 * Store the three-word I/O interruption code into
394 * the appropriate lowcore area.
396 len
= sizeof(tpi_data
);
397 if (write_guest_lc(vcpu
, __LC_SUBCHANNEL_ID
, &tpi_data
, len
)) {
398 /* failed writes to the low core are not recoverable */
400 goto reinject_interrupt
;
404 /* irq was successfully handed to the guest */
406 kvm_s390_set_psw_cc(vcpu
, 1);
410 * If we encounter a problem storing the interruption code, the
411 * instruction is suppressed from the guest's view: reinject the
414 if (kvm_s390_reinject_io_int(vcpu
->kvm
, inti
)) {
418 /* don't set the cc, a pgm irq was injected or we drop to user space */
419 return rc
? -EFAULT
: 0;
422 static int handle_tsch(struct kvm_vcpu
*vcpu
)
424 struct kvm_s390_interrupt_info
*inti
= NULL
;
425 const u64 isc_mask
= 0xffUL
<< 24; /* all iscs set */
427 /* a valid schid has at least one bit set */
428 if (vcpu
->run
->s
.regs
.gprs
[1])
429 inti
= kvm_s390_get_io_int(vcpu
->kvm
, isc_mask
,
430 vcpu
->run
->s
.regs
.gprs
[1]);
433 * Prepare exit to userspace.
434 * We indicate whether we dequeued a pending I/O interrupt
435 * so that userspace can re-inject it if the instruction gets
436 * a program check. While this may re-order the pending I/O
437 * interrupts, this is no problem since the priority is kept
440 vcpu
->run
->exit_reason
= KVM_EXIT_S390_TSCH
;
441 vcpu
->run
->s390_tsch
.dequeued
= !!inti
;
443 vcpu
->run
->s390_tsch
.subchannel_id
= inti
->io
.subchannel_id
;
444 vcpu
->run
->s390_tsch
.subchannel_nr
= inti
->io
.subchannel_nr
;
445 vcpu
->run
->s390_tsch
.io_int_parm
= inti
->io
.io_int_parm
;
446 vcpu
->run
->s390_tsch
.io_int_word
= inti
->io
.io_int_word
;
448 vcpu
->run
->s390_tsch
.ipb
= vcpu
->arch
.sie_block
->ipb
;
453 static int handle_io_inst(struct kvm_vcpu
*vcpu
)
455 VCPU_EVENT(vcpu
, 4, "%s", "I/O instruction");
457 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
458 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
460 if (vcpu
->kvm
->arch
.css_support
) {
462 * Most I/O instructions will be handled by userspace.
463 * Exceptions are tpi and the interrupt portion of tsch.
465 if (vcpu
->arch
.sie_block
->ipa
== 0xb236)
466 return handle_tpi(vcpu
);
467 if (vcpu
->arch
.sie_block
->ipa
== 0xb235)
468 return handle_tsch(vcpu
);
469 /* Handle in userspace. */
473 * Set condition code 3 to stop the guest from issuing channel
476 kvm_s390_set_psw_cc(vcpu
, 3);
481 static int handle_stfl(struct kvm_vcpu
*vcpu
)
486 vcpu
->stat
.instruction_stfl
++;
488 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
489 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
492 * We need to shift the lower 32 facility bits (bit 0-31) from a u64
493 * into a u32 memory representation. They will remain bits 0-31.
495 fac
= *vcpu
->kvm
->arch
.model
.fac_list
>> 32;
496 rc
= write_guest_lc(vcpu
, offsetof(struct lowcore
, stfl_fac_list
),
500 VCPU_EVENT(vcpu
, 3, "STFL: store facility list 0x%x", fac
);
501 trace_kvm_s390_handle_stfl(vcpu
, fac
);
505 #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
506 #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
507 #define PSW_ADDR_24 0x0000000000ffffffUL
508 #define PSW_ADDR_31 0x000000007fffffffUL
510 int is_valid_psw(psw_t
*psw
)
512 if (psw
->mask
& PSW_MASK_UNASSIGNED
)
514 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_BA
) {
515 if (psw
->addr
& ~PSW_ADDR_31
)
518 if (!(psw
->mask
& PSW_MASK_ADDR_MODE
) && (psw
->addr
& ~PSW_ADDR_24
))
520 if ((psw
->mask
& PSW_MASK_ADDR_MODE
) == PSW_MASK_EA
)
527 int kvm_s390_handle_lpsw(struct kvm_vcpu
*vcpu
)
529 psw_t
*gpsw
= &vcpu
->arch
.sie_block
->gpsw
;
530 psw_compat_t new_psw
;
535 if (gpsw
->mask
& PSW_MASK_PSTATE
)
536 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
538 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
540 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
542 rc
= read_guest(vcpu
, addr
, ar
, &new_psw
, sizeof(new_psw
));
544 return kvm_s390_inject_prog_cond(vcpu
, rc
);
545 if (!(new_psw
.mask
& PSW32_MASK_BASE
))
546 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
547 gpsw
->mask
= (new_psw
.mask
& ~PSW32_MASK_BASE
) << 32;
548 gpsw
->mask
|= new_psw
.addr
& PSW32_ADDR_AMODE
;
549 gpsw
->addr
= new_psw
.addr
& ~PSW32_ADDR_AMODE
;
550 if (!is_valid_psw(gpsw
))
551 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
555 static int handle_lpswe(struct kvm_vcpu
*vcpu
)
562 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
563 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
565 addr
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
567 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
568 rc
= read_guest(vcpu
, addr
, ar
, &new_psw
, sizeof(new_psw
));
570 return kvm_s390_inject_prog_cond(vcpu
, rc
);
571 vcpu
->arch
.sie_block
->gpsw
= new_psw
;
572 if (!is_valid_psw(&vcpu
->arch
.sie_block
->gpsw
))
573 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
577 static int handle_stidp(struct kvm_vcpu
*vcpu
)
579 u64 stidp_data
= vcpu
->kvm
->arch
.model
.cpuid
;
584 vcpu
->stat
.instruction_stidp
++;
586 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
587 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
589 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
592 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
594 rc
= write_guest(vcpu
, operand2
, ar
, &stidp_data
, sizeof(stidp_data
));
596 return kvm_s390_inject_prog_cond(vcpu
, rc
);
598 VCPU_EVENT(vcpu
, 3, "STIDP: store cpu id 0x%llx", stidp_data
);
602 static void handle_stsi_3_2_2(struct kvm_vcpu
*vcpu
, struct sysinfo_3_2_2
*mem
)
607 cpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
609 /* deal with other level 3 hypervisors */
610 if (stsi(mem
, 3, 2, 2))
614 for (n
= mem
->count
- 1; n
> 0 ; n
--)
615 memcpy(&mem
->vm
[n
], &mem
->vm
[n
- 1], sizeof(mem
->vm
[0]));
617 memset(&mem
->vm
[0], 0, sizeof(mem
->vm
[0]));
618 mem
->vm
[0].cpus_total
= cpus
;
619 mem
->vm
[0].cpus_configured
= cpus
;
620 mem
->vm
[0].cpus_standby
= 0;
621 mem
->vm
[0].cpus_reserved
= 0;
622 mem
->vm
[0].caf
= 1000;
623 memcpy(mem
->vm
[0].name
, "KVMguest", 8);
624 ASCEBC(mem
->vm
[0].name
, 8);
625 memcpy(mem
->vm
[0].cpi
, "KVM/Linux ", 16);
626 ASCEBC(mem
->vm
[0].cpi
, 16);
629 static void insert_stsi_usr_data(struct kvm_vcpu
*vcpu
, u64 addr
, ar_t ar
,
630 u8 fc
, u8 sel1
, u16 sel2
)
632 vcpu
->run
->exit_reason
= KVM_EXIT_S390_STSI
;
633 vcpu
->run
->s390_stsi
.addr
= addr
;
634 vcpu
->run
->s390_stsi
.ar
= ar
;
635 vcpu
->run
->s390_stsi
.fc
= fc
;
636 vcpu
->run
->s390_stsi
.sel1
= sel1
;
637 vcpu
->run
->s390_stsi
.sel2
= sel2
;
640 static int handle_stsi(struct kvm_vcpu
*vcpu
)
642 int fc
= (vcpu
->run
->s
.regs
.gprs
[0] & 0xf0000000) >> 28;
643 int sel1
= vcpu
->run
->s
.regs
.gprs
[0] & 0xff;
644 int sel2
= vcpu
->run
->s
.regs
.gprs
[1] & 0xffff;
645 unsigned long mem
= 0;
650 vcpu
->stat
.instruction_stsi
++;
651 VCPU_EVENT(vcpu
, 3, "STSI: fc: %u sel1: %u sel2: %u", fc
, sel1
, sel2
);
653 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
654 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
657 kvm_s390_set_psw_cc(vcpu
, 3);
661 if (vcpu
->run
->s
.regs
.gprs
[0] & 0x0fffff00
662 || vcpu
->run
->s
.regs
.gprs
[1] & 0xffff0000)
663 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
666 vcpu
->run
->s
.regs
.gprs
[0] = 3 << 28;
667 kvm_s390_set_psw_cc(vcpu
, 0);
671 operand2
= kvm_s390_get_base_disp_s(vcpu
, &ar
);
673 if (operand2
& 0xfff)
674 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
677 case 1: /* same handling for 1 and 2 */
679 mem
= get_zeroed_page(GFP_KERNEL
);
682 if (stsi((void *) mem
, fc
, sel1
, sel2
))
686 if (sel1
!= 2 || sel2
!= 2)
688 mem
= get_zeroed_page(GFP_KERNEL
);
691 handle_stsi_3_2_2(vcpu
, (void *) mem
);
695 rc
= write_guest(vcpu
, operand2
, ar
, (void *)mem
, PAGE_SIZE
);
697 rc
= kvm_s390_inject_prog_cond(vcpu
, rc
);
700 if (vcpu
->kvm
->arch
.user_stsi
) {
701 insert_stsi_usr_data(vcpu
, operand2
, ar
, fc
, sel1
, sel2
);
704 trace_kvm_s390_handle_stsi(vcpu
, fc
, sel1
, sel2
, operand2
);
706 kvm_s390_set_psw_cc(vcpu
, 0);
707 vcpu
->run
->s
.regs
.gprs
[0] = 0;
710 kvm_s390_set_psw_cc(vcpu
, 3);
716 static const intercept_handler_t b2_handlers
[256] = {
717 [0x02] = handle_stidp
,
718 [0x04] = handle_set_clock
,
719 [0x10] = handle_set_prefix
,
720 [0x11] = handle_store_prefix
,
721 [0x12] = handle_store_cpu_address
,
722 [0x14] = kvm_s390_handle_vsie
,
723 [0x21] = handle_ipte_interlock
,
724 [0x29] = handle_iske
,
725 [0x2a] = handle_rrbe
,
726 [0x2b] = handle_sske
,
727 [0x2c] = handle_test_block
,
728 [0x30] = handle_io_inst
,
729 [0x31] = handle_io_inst
,
730 [0x32] = handle_io_inst
,
731 [0x33] = handle_io_inst
,
732 [0x34] = handle_io_inst
,
733 [0x35] = handle_io_inst
,
734 [0x36] = handle_io_inst
,
735 [0x37] = handle_io_inst
,
736 [0x38] = handle_io_inst
,
737 [0x39] = handle_io_inst
,
738 [0x3a] = handle_io_inst
,
739 [0x3b] = handle_io_inst
,
740 [0x3c] = handle_io_inst
,
741 [0x50] = handle_ipte_interlock
,
742 [0x5f] = handle_io_inst
,
743 [0x74] = handle_io_inst
,
744 [0x76] = handle_io_inst
,
745 [0x7d] = handle_stsi
,
746 [0xb1] = handle_stfl
,
747 [0xb2] = handle_lpswe
,
750 int kvm_s390_handle_b2(struct kvm_vcpu
*vcpu
)
752 intercept_handler_t handler
;
755 * A lot of B2 instructions are priviledged. Here we check for
756 * the privileged ones, that we can handle in the kernel.
757 * Anything else goes to userspace.
759 handler
= b2_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
761 return handler(vcpu
);
766 static int handle_epsw(struct kvm_vcpu
*vcpu
)
770 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
772 /* This basically extracts the mask half of the psw. */
773 vcpu
->run
->s
.regs
.gprs
[reg1
] &= 0xffffffff00000000UL
;
774 vcpu
->run
->s
.regs
.gprs
[reg1
] |= vcpu
->arch
.sie_block
->gpsw
.mask
>> 32;
776 vcpu
->run
->s
.regs
.gprs
[reg2
] &= 0xffffffff00000000UL
;
777 vcpu
->run
->s
.regs
.gprs
[reg2
] |=
778 vcpu
->arch
.sie_block
->gpsw
.mask
& 0x00000000ffffffffUL
;
783 #define PFMF_RESERVED 0xfffc0101UL
784 #define PFMF_SK 0x00020000UL
785 #define PFMF_CF 0x00010000UL
786 #define PFMF_UI 0x00008000UL
787 #define PFMF_FSC 0x00007000UL
788 #define PFMF_NQ 0x00000800UL
789 #define PFMF_MR 0x00000400UL
790 #define PFMF_MC 0x00000200UL
791 #define PFMF_KEY 0x000000feUL
793 static int handle_pfmf(struct kvm_vcpu
*vcpu
)
795 bool mr
= false, mc
= false, nq
;
797 unsigned long start
, end
;
800 vcpu
->stat
.instruction_pfmf
++;
802 kvm_s390_get_regs_rre(vcpu
, ®1
, ®2
);
804 if (!test_kvm_facility(vcpu
->kvm
, 8))
805 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
807 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
808 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
810 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_RESERVED
)
811 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
813 /* Only provide non-quiescing support if enabled for the guest */
814 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
&&
815 !test_kvm_facility(vcpu
->kvm
, 14))
816 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
818 /* Only provide conditional-SSKE support if enabled for the guest */
819 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_SK
&&
820 test_kvm_facility(vcpu
->kvm
, 10)) {
821 mr
= vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_MR
;
822 mc
= vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_MC
;
825 nq
= vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_NQ
;
826 key
= vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_KEY
;
827 start
= vcpu
->run
->s
.regs
.gprs
[reg2
] & PAGE_MASK
;
828 start
= kvm_s390_logical_to_effective(vcpu
, start
);
830 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
831 if (kvm_s390_check_low_addr_prot_real(vcpu
, start
))
832 return kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
835 switch (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) {
837 /* only 4k frames specify a real address */
838 start
= kvm_s390_real_to_abs(vcpu
, start
);
839 end
= (start
+ (1UL << 12)) & ~((1UL << 12) - 1);
842 end
= (start
+ (1UL << 20)) & ~((1UL << 20) - 1);
845 /* only support 2G frame size if EDAT2 is available and we are
846 not in 24-bit addressing mode */
847 if (!test_kvm_facility(vcpu
->kvm
, 78) ||
848 psw_bits(vcpu
->arch
.sie_block
->gpsw
).eaba
== PSW_AMODE_24BIT
)
849 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
850 end
= (start
+ (1UL << 31)) & ~((1UL << 31) - 1);
853 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
856 while (start
!= end
) {
857 unsigned long useraddr
;
859 /* Translate guest address to host address */
860 useraddr
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(start
));
861 if (kvm_is_error_hva(useraddr
))
862 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
864 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_CF
) {
865 if (clear_user((void __user
*)useraddr
, PAGE_SIZE
))
866 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
869 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_SK
) {
870 int rc
= __skey_check_enable(vcpu
);
874 down_read(¤t
->mm
->mmap_sem
);
875 rc
= cond_set_guest_storage_key(current
->mm
, useraddr
,
876 key
, NULL
, nq
, mr
, mc
);
877 up_read(¤t
->mm
->mmap_sem
);
879 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
884 if (vcpu
->run
->s
.regs
.gprs
[reg1
] & PFMF_FSC
) {
885 if (psw_bits(vcpu
->arch
.sie_block
->gpsw
).eaba
== PSW_AMODE_64BIT
) {
886 vcpu
->run
->s
.regs
.gprs
[reg2
] = end
;
888 vcpu
->run
->s
.regs
.gprs
[reg2
] &= ~0xffffffffUL
;
889 end
= kvm_s390_logical_to_effective(vcpu
, end
);
890 vcpu
->run
->s
.regs
.gprs
[reg2
] |= end
;
896 static int handle_essa(struct kvm_vcpu
*vcpu
)
898 /* entries expected to be 1FF */
899 int entries
= (vcpu
->arch
.sie_block
->cbrlo
& ~PAGE_MASK
) >> 3;
900 unsigned long *cbrlo
;
904 VCPU_EVENT(vcpu
, 4, "ESSA: release %d pages", entries
);
905 gmap
= vcpu
->arch
.gmap
;
906 vcpu
->stat
.instruction_essa
++;
907 if (!vcpu
->kvm
->arch
.use_cmma
)
908 return kvm_s390_inject_program_int(vcpu
, PGM_OPERATION
);
910 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
911 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
913 if (((vcpu
->arch
.sie_block
->ipb
& 0xf0000000) >> 28) > 6)
914 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
916 /* Retry the ESSA instruction */
917 kvm_s390_retry_instr(vcpu
);
918 vcpu
->arch
.sie_block
->cbrlo
&= PAGE_MASK
; /* reset nceo */
919 cbrlo
= phys_to_virt(vcpu
->arch
.sie_block
->cbrlo
);
920 down_read(&gmap
->mm
->mmap_sem
);
921 for (i
= 0; i
< entries
; ++i
)
922 __gmap_zap(gmap
, cbrlo
[i
]);
923 up_read(&gmap
->mm
->mmap_sem
);
927 static const intercept_handler_t b9_handlers
[256] = {
928 [0x8a] = handle_ipte_interlock
,
929 [0x8d] = handle_epsw
,
930 [0x8e] = handle_ipte_interlock
,
931 [0x8f] = handle_ipte_interlock
,
932 [0xab] = handle_essa
,
933 [0xaf] = handle_pfmf
,
936 int kvm_s390_handle_b9(struct kvm_vcpu
*vcpu
)
938 intercept_handler_t handler
;
940 /* This is handled just as for the B2 instructions. */
941 handler
= b9_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
943 return handler(vcpu
);
948 int kvm_s390_handle_lctl(struct kvm_vcpu
*vcpu
)
950 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
951 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
952 int reg
, rc
, nr_regs
;
957 vcpu
->stat
.instruction_lctl
++;
959 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
960 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
962 ga
= kvm_s390_get_base_disp_rs(vcpu
, &ar
);
965 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
967 VCPU_EVENT(vcpu
, 4, "LCTL: r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
968 trace_kvm_s390_handle_lctl(vcpu
, 0, reg1
, reg3
, ga
);
970 nr_regs
= ((reg3
- reg1
) & 0xf) + 1;
971 rc
= read_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u32
));
973 return kvm_s390_inject_prog_cond(vcpu
, rc
);
977 vcpu
->arch
.sie_block
->gcr
[reg
] &= 0xffffffff00000000ul
;
978 vcpu
->arch
.sie_block
->gcr
[reg
] |= ctl_array
[nr_regs
++];
981 reg
= (reg
+ 1) % 16;
983 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
987 int kvm_s390_handle_stctl(struct kvm_vcpu
*vcpu
)
989 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
990 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
991 int reg
, rc
, nr_regs
;
996 vcpu
->stat
.instruction_stctl
++;
998 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
999 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1001 ga
= kvm_s390_get_base_disp_rs(vcpu
, &ar
);
1004 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
1006 VCPU_EVENT(vcpu
, 4, "STCTL r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
1007 trace_kvm_s390_handle_stctl(vcpu
, 0, reg1
, reg3
, ga
);
1012 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
1015 reg
= (reg
+ 1) % 16;
1017 rc
= write_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u32
));
1018 return rc
? kvm_s390_inject_prog_cond(vcpu
, rc
) : 0;
1021 static int handle_lctlg(struct kvm_vcpu
*vcpu
)
1023 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
1024 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
1025 int reg
, rc
, nr_regs
;
1030 vcpu
->stat
.instruction_lctlg
++;
1032 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1033 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1035 ga
= kvm_s390_get_base_disp_rsy(vcpu
, &ar
);
1038 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
1040 VCPU_EVENT(vcpu
, 4, "LCTLG: r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
1041 trace_kvm_s390_handle_lctl(vcpu
, 1, reg1
, reg3
, ga
);
1043 nr_regs
= ((reg3
- reg1
) & 0xf) + 1;
1044 rc
= read_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u64
));
1046 return kvm_s390_inject_prog_cond(vcpu
, rc
);
1050 vcpu
->arch
.sie_block
->gcr
[reg
] = ctl_array
[nr_regs
++];
1053 reg
= (reg
+ 1) % 16;
1055 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
1059 static int handle_stctg(struct kvm_vcpu
*vcpu
)
1061 int reg1
= (vcpu
->arch
.sie_block
->ipa
& 0x00f0) >> 4;
1062 int reg3
= vcpu
->arch
.sie_block
->ipa
& 0x000f;
1063 int reg
, rc
, nr_regs
;
1068 vcpu
->stat
.instruction_stctg
++;
1070 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1071 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1073 ga
= kvm_s390_get_base_disp_rsy(vcpu
, &ar
);
1076 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
1078 VCPU_EVENT(vcpu
, 4, "STCTG r1:%d, r3:%d, addr: 0x%llx", reg1
, reg3
, ga
);
1079 trace_kvm_s390_handle_stctl(vcpu
, 1, reg1
, reg3
, ga
);
1084 ctl_array
[nr_regs
++] = vcpu
->arch
.sie_block
->gcr
[reg
];
1087 reg
= (reg
+ 1) % 16;
1089 rc
= write_guest(vcpu
, ga
, ar
, ctl_array
, nr_regs
* sizeof(u64
));
1090 return rc
? kvm_s390_inject_prog_cond(vcpu
, rc
) : 0;
1093 static const intercept_handler_t eb_handlers
[256] = {
1094 [0x2f] = handle_lctlg
,
1095 [0x25] = handle_stctg
,
1098 int kvm_s390_handle_eb(struct kvm_vcpu
*vcpu
)
1100 intercept_handler_t handler
;
1102 handler
= eb_handlers
[vcpu
->arch
.sie_block
->ipb
& 0xff];
1104 return handler(vcpu
);
1108 static int handle_tprot(struct kvm_vcpu
*vcpu
)
1110 u64 address1
, address2
;
1111 unsigned long hva
, gpa
;
1112 int ret
= 0, cc
= 0;
1116 vcpu
->stat
.instruction_tprot
++;
1118 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1119 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1121 kvm_s390_get_base_disp_sse(vcpu
, &address1
, &address2
, &ar
, NULL
);
1123 /* we only handle the Linux memory detection case:
1125 * everything else goes to userspace. */
1126 if (address2
& 0xf0)
1128 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
1130 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
, GACC_STORE
);
1131 if (ret
== PGM_PROTECTION
) {
1132 /* Write protected? Try again with read-only... */
1134 ret
= guest_translate_address(vcpu
, address1
, ar
, &gpa
,
1138 if (ret
== PGM_ADDRESSING
|| ret
== PGM_TRANSLATION_SPEC
) {
1139 ret
= kvm_s390_inject_program_int(vcpu
, ret
);
1140 } else if (ret
> 0) {
1141 /* Translation not available */
1142 kvm_s390_set_psw_cc(vcpu
, 3);
1148 hva
= gfn_to_hva_prot(vcpu
->kvm
, gpa_to_gfn(gpa
), &writable
);
1149 if (kvm_is_error_hva(hva
)) {
1150 ret
= kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
1153 cc
= 1; /* Write not permitted ==> read-only */
1154 kvm_s390_set_psw_cc(vcpu
, cc
);
1155 /* Note: CC2 only occurs for storage keys (not supported yet) */
1158 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_DAT
)
1163 int kvm_s390_handle_e5(struct kvm_vcpu
*vcpu
)
1165 /* For e5xx... instructions we only handle TPROT */
1166 if ((vcpu
->arch
.sie_block
->ipa
& 0x00ff) == 0x01)
1167 return handle_tprot(vcpu
);
1171 static int handle_sckpf(struct kvm_vcpu
*vcpu
)
1175 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
1176 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
1178 if (vcpu
->run
->s
.regs
.gprs
[0] & 0x00000000ffff0000)
1179 return kvm_s390_inject_program_int(vcpu
,
1182 value
= vcpu
->run
->s
.regs
.gprs
[0] & 0x000000000000ffff;
1183 vcpu
->arch
.sie_block
->todpr
= value
;
1188 static int handle_ptff(struct kvm_vcpu
*vcpu
)
1190 /* we don't emulate any control instructions yet */
1191 kvm_s390_set_psw_cc(vcpu
, 3);
1195 static const intercept_handler_t x01_handlers
[256] = {
1196 [0x04] = handle_ptff
,
1197 [0x07] = handle_sckpf
,
1200 int kvm_s390_handle_01(struct kvm_vcpu
*vcpu
)
1202 intercept_handler_t handler
;
1204 handler
= x01_handlers
[vcpu
->arch
.sie_block
->ipa
& 0x00ff];
1206 return handler(vcpu
);