1 // SPDX-License-Identifier: GPL-2.0
3 * handling diagnose instructions
5 * Copyright IBM Corp. 2008, 2020
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
11 #include <linux/kvm.h>
12 #include <linux/kvm_host.h>
13 #include <asm/pgalloc.h>
15 #include <asm/virtio-ccw.h>
18 #include "trace-s390.h"
21 static int diag_release_pages(struct kvm_vcpu
*vcpu
)
23 unsigned long start
, end
;
24 unsigned long prefix
= kvm_s390_get_prefix(vcpu
);
26 start
= vcpu
->run
->s
.regs
.gprs
[(vcpu
->arch
.sie_block
->ipa
& 0xf0) >> 4];
27 end
= vcpu
->run
->s
.regs
.gprs
[vcpu
->arch
.sie_block
->ipa
& 0xf] + PAGE_SIZE
;
28 vcpu
->stat
.diagnose_10
++;
30 if (start
& ~PAGE_MASK
|| end
& ~PAGE_MASK
|| start
>= end
31 || start
< 2 * PAGE_SIZE
)
32 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
34 VCPU_EVENT(vcpu
, 5, "diag release pages %lX %lX", start
, end
);
37 * We checked for start >= end above, so lets check for the
38 * fast path (no prefix swap page involved)
40 if (end
<= prefix
|| start
>= prefix
+ 2 * PAGE_SIZE
) {
41 gmap_discard(vcpu
->arch
.gmap
, start
, end
);
44 * This is slow path. gmap_discard will check for start
45 * so lets split this into before prefix, prefix, after
46 * prefix and let gmap_discard make some of these calls
49 gmap_discard(vcpu
->arch
.gmap
, start
, prefix
);
51 gmap_discard(vcpu
->arch
.gmap
, 0, PAGE_SIZE
);
52 if (end
> prefix
+ PAGE_SIZE
)
53 gmap_discard(vcpu
->arch
.gmap
, PAGE_SIZE
, 2 * PAGE_SIZE
);
54 gmap_discard(vcpu
->arch
.gmap
, prefix
+ 2 * PAGE_SIZE
, end
);
59 static int __diag_page_ref_service(struct kvm_vcpu
*vcpu
)
73 u16 rx
= (vcpu
->arch
.sie_block
->ipa
& 0xf0) >> 4;
74 u16 ry
= (vcpu
->arch
.sie_block
->ipa
& 0x0f);
76 VCPU_EVENT(vcpu
, 3, "diag page reference parameter block at 0x%llx",
77 vcpu
->run
->s
.regs
.gprs
[rx
]);
78 vcpu
->stat
.diagnose_258
++;
79 if (vcpu
->run
->s
.regs
.gprs
[rx
] & 7)
80 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
81 rc
= read_guest(vcpu
, vcpu
->run
->s
.regs
.gprs
[rx
], rx
, &parm
, sizeof(parm
));
83 return kvm_s390_inject_prog_cond(vcpu
, rc
);
84 if (parm
.parm_version
!= 2 || parm
.parm_len
< 5 || parm
.code
!= 0x258)
85 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
87 switch (parm
.subcode
) {
89 VCPU_EVENT(vcpu
, 3, "pageref token addr 0x%llx "
90 "select mask 0x%llx compare mask 0x%llx",
91 parm
.token_addr
, parm
.select_mask
, parm
.compare_mask
);
92 if (vcpu
->arch
.pfault_token
!= KVM_S390_PFAULT_TOKEN_INVALID
) {
94 * If the pagefault handshake is already activated,
95 * the token must not be changed. We have to return
96 * decimal 8 instead, as mandated in SC24-6084.
98 vcpu
->run
->s
.regs
.gprs
[ry
] = 8;
102 if ((parm
.compare_mask
& parm
.select_mask
) != parm
.compare_mask
||
103 parm
.token_addr
& 7 || parm
.zarch
!= 0x8000000000000000ULL
)
104 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
106 if (kvm_is_error_gpa(vcpu
->kvm
, parm
.token_addr
))
107 return kvm_s390_inject_program_int(vcpu
, PGM_ADDRESSING
);
109 vcpu
->arch
.pfault_token
= parm
.token_addr
;
110 vcpu
->arch
.pfault_select
= parm
.select_mask
;
111 vcpu
->arch
.pfault_compare
= parm
.compare_mask
;
112 vcpu
->run
->s
.regs
.gprs
[ry
] = 0;
117 * Specification allows to let already pending tokens survive
118 * the cancel, therefore to reduce code complexity, we assume
119 * all outstanding tokens are already pending.
121 VCPU_EVENT(vcpu
, 3, "pageref cancel addr 0x%llx", parm
.token_addr
);
122 if (parm
.token_addr
|| parm
.select_mask
||
123 parm
.compare_mask
|| parm
.zarch
)
124 return kvm_s390_inject_program_int(vcpu
, PGM_SPECIFICATION
);
126 vcpu
->run
->s
.regs
.gprs
[ry
] = 0;
128 * If the pfault handling was not established or is already
129 * canceled SC24-6084 requests to return decimal 4.
131 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
132 vcpu
->run
->s
.regs
.gprs
[ry
] = 4;
134 vcpu
->arch
.pfault_token
= KVM_S390_PFAULT_TOKEN_INVALID
;
146 static int __diag_time_slice_end(struct kvm_vcpu
*vcpu
)
148 VCPU_EVENT(vcpu
, 5, "%s", "diag time slice end");
149 vcpu
->stat
.diagnose_44
++;
150 kvm_vcpu_on_spin(vcpu
, true);
154 static int __diag_time_slice_end_directed(struct kvm_vcpu
*vcpu
)
156 struct kvm_vcpu
*tcpu
;
159 tid
= vcpu
->run
->s
.regs
.gprs
[(vcpu
->arch
.sie_block
->ipa
& 0xf0) >> 4];
160 vcpu
->stat
.diagnose_9c
++;
163 if (tid
== vcpu
->vcpu_id
)
166 /* yield to invalid */
167 tcpu
= kvm_get_vcpu_by_id(vcpu
->kvm
, tid
);
171 /* target already running */
172 if (READ_ONCE(tcpu
->cpu
) >= 0)
175 if (kvm_vcpu_yield_to(tcpu
) <= 0)
178 VCPU_EVENT(vcpu
, 5, "diag time slice end directed to %d: done", tid
);
181 VCPU_EVENT(vcpu
, 5, "diag time slice end directed to %d: ignored", tid
);
182 vcpu
->stat
.diagnose_9c_ignored
++;
186 static int __diag_ipl_functions(struct kvm_vcpu
*vcpu
)
188 unsigned int reg
= vcpu
->arch
.sie_block
->ipa
& 0xf;
189 unsigned long subcode
= vcpu
->run
->s
.regs
.gprs
[reg
] & 0xffff;
191 VCPU_EVENT(vcpu
, 3, "diag ipl functions, subcode %lx", subcode
);
192 vcpu
->stat
.diagnose_308
++;
195 vcpu
->run
->s390_reset_flags
= KVM_S390_RESET_CLEAR
;
198 vcpu
->run
->s390_reset_flags
= 0;
205 * no need to check the return value of vcpu_stop as it can only have
206 * an error for protvirt, but protvirt means user cpu state
208 if (!kvm_s390_user_cpu_state_ctrl(vcpu
->kvm
))
209 kvm_s390_vcpu_stop(vcpu
);
210 vcpu
->run
->s390_reset_flags
|= KVM_S390_RESET_SUBSYSTEM
;
211 vcpu
->run
->s390_reset_flags
|= KVM_S390_RESET_IPL
;
212 vcpu
->run
->s390_reset_flags
|= KVM_S390_RESET_CPU_INIT
;
213 vcpu
->run
->exit_reason
= KVM_EXIT_S390_RESET
;
214 VCPU_EVENT(vcpu
, 3, "requesting userspace resets %llx",
215 vcpu
->run
->s390_reset_flags
);
216 trace_kvm_s390_request_resets(vcpu
->run
->s390_reset_flags
);
220 static int __diag_virtio_hypercall(struct kvm_vcpu
*vcpu
)
224 vcpu
->stat
.diagnose_500
++;
225 /* No virtio-ccw notification? Get out quickly. */
226 if (!vcpu
->kvm
->arch
.css_support
||
227 (vcpu
->run
->s
.regs
.gprs
[1] != KVM_S390_VIRTIO_CCW_NOTIFY
))
230 VCPU_EVENT(vcpu
, 4, "diag 0x500 schid 0x%8.8x queue 0x%x cookie 0x%llx",
231 (u32
) vcpu
->run
->s
.regs
.gprs
[2],
232 (u32
) vcpu
->run
->s
.regs
.gprs
[3],
233 vcpu
->run
->s
.regs
.gprs
[4]);
236 * The layout is as follows:
237 * - gpr 2 contains the subchannel id (passed as addr)
238 * - gpr 3 contains the virtqueue index (passed as datamatch)
239 * - gpr 4 contains the index on the bus (optionally)
241 ret
= kvm_io_bus_write_cookie(vcpu
, KVM_VIRTIO_CCW_NOTIFY_BUS
,
242 vcpu
->run
->s
.regs
.gprs
[2] & 0xffffffff,
243 8, &vcpu
->run
->s
.regs
.gprs
[3],
244 vcpu
->run
->s
.regs
.gprs
[4]);
247 * Return cookie in gpr 2, but don't overwrite the register if the
248 * diagnose will be handled by userspace.
250 if (ret
!= -EOPNOTSUPP
)
251 vcpu
->run
->s
.regs
.gprs
[2] = ret
;
252 /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
253 return ret
< 0 ? ret
: 0;
256 int kvm_s390_handle_diag(struct kvm_vcpu
*vcpu
)
258 int code
= kvm_s390_get_base_disp_rs(vcpu
, NULL
) & 0xffff;
260 if (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
)
261 return kvm_s390_inject_program_int(vcpu
, PGM_PRIVILEGED_OP
);
263 trace_kvm_s390_handle_diag(vcpu
, code
);
266 return diag_release_pages(vcpu
);
268 return __diag_time_slice_end(vcpu
);
270 return __diag_time_slice_end_directed(vcpu
);
272 return __diag_page_ref_service(vcpu
);
274 return __diag_ipl_functions(vcpu
);
276 return __diag_virtio_hypercall(vcpu
);
278 vcpu
->stat
.diagnose_other
++;