1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * definition for kvm on s390
4 *
5 * Copyright IBM Corp. 2008, 2020
6 *
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
9 * Christian Ehrhardt <ehrhardt@de.ibm.com>
10 */
11
12 #ifndef ARCH_S390_KVM_S390_H
13 #define ARCH_S390_KVM_S390_H
14
15 #include <linux/hrtimer.h>
16 #include <linux/kvm.h>
17 #include <linux/kvm_host.h>
18 #include <linux/lockdep.h>
19 #include <asm/facility.h>
20 #include <asm/processor.h>
21 #include <asm/sclp.h>
22 #include "dat.h"
23 #include "gmap.h"
24
25 #define KVM_S390_UCONTROL_MEMSLOT (KVM_USER_MEM_SLOTS + 0)
26
27 union kvm_s390_quad {
28 __uint128_t sixteen;
29 unsigned long eight;
30 unsigned int four;
31 unsigned short two;
32 unsigned char one;
33 };
34
kvm_s390_fpu_store(struct kvm_run * run)35 static inline void kvm_s390_fpu_store(struct kvm_run *run)
36 {
37 fpu_stfpc(&run->s.regs.fpc);
38 if (cpu_has_vx())
39 save_vx_regs((__vector128 *)&run->s.regs.vrs);
40 else
41 save_fp_regs((freg_t *)&run->s.regs.fprs);
42 }
43
kvm_s390_fpu_load(struct kvm_run * run)44 static inline void kvm_s390_fpu_load(struct kvm_run *run)
45 {
46 fpu_lfpc_safe(&run->s.regs.fpc);
47 if (cpu_has_vx())
48 load_vx_regs((__vector128 *)&run->s.regs.vrs);
49 else
50 load_fp_regs((freg_t *)&run->s.regs.fprs);
51 }
52
53 /* Transactional Memory Execution related macros */
54 #define IS_TE_ENABLED(vcpu) ((vcpu->arch.sie_block->ecb & ECB_TE))
55 #define TDB_FORMAT1 1
56 #define IS_ITDB_VALID(vcpu) \
57 ((*(char *)phys_to_virt((vcpu)->arch.sie_block->itdba) == TDB_FORMAT1))
58
59 extern debug_info_t *kvm_s390_dbf;
60 extern debug_info_t *kvm_s390_dbf_uv;
61
62 #define KVM_UV_EVENT(d_kvm, d_loglevel, d_string, d_args...)\
63 do { \
64 debug_sprintf_event((d_kvm)->arch.dbf, d_loglevel, d_string "\n", \
65 d_args); \
66 debug_sprintf_event(kvm_s390_dbf_uv, d_loglevel, \
67 "%d: " d_string "\n", (d_kvm)->userspace_pid, \
68 d_args); \
69 } while (0)
70
71 #define KVM_EVENT(d_loglevel, d_string, d_args...)\
72 do { \
73 debug_sprintf_event(kvm_s390_dbf, d_loglevel, d_string "\n", \
74 d_args); \
75 } while (0)
76
77 #define VM_EVENT(d_kvm, d_loglevel, d_string, d_args...)\
78 do { \
79 debug_sprintf_event(d_kvm->arch.dbf, d_loglevel, d_string "\n", \
80 d_args); \
81 } while (0)
82
83 #define VCPU_EVENT(d_vcpu, d_loglevel, d_string, d_args...)\
84 do { \
85 debug_sprintf_event(d_vcpu->kvm->arch.dbf, d_loglevel, \
86 "%02d[%016lx-%016lx]: " d_string "\n", d_vcpu->vcpu_id, \
87 d_vcpu->arch.sie_block->gpsw.mask, d_vcpu->arch.sie_block->gpsw.addr,\
88 d_args); \
89 } while (0)
90
kvm_s390_set_cpuflags(struct kvm_vcpu * vcpu,u32 flags)91 static inline void kvm_s390_set_cpuflags(struct kvm_vcpu *vcpu, u32 flags)
92 {
93 atomic_or(flags, &vcpu->arch.sie_block->cpuflags);
94 }
95
kvm_s390_clear_cpuflags(struct kvm_vcpu * vcpu,u32 flags)96 static inline void kvm_s390_clear_cpuflags(struct kvm_vcpu *vcpu, u32 flags)
97 {
98 atomic_andnot(flags, &vcpu->arch.sie_block->cpuflags);
99 }
100
kvm_s390_test_cpuflags(struct kvm_vcpu * vcpu,u32 flags)101 static inline bool kvm_s390_test_cpuflags(struct kvm_vcpu *vcpu, u32 flags)
102 {
103 return (atomic_read(&vcpu->arch.sie_block->cpuflags) & flags) == flags;
104 }
105
is_vcpu_stopped(struct kvm_vcpu * vcpu)106 static inline int is_vcpu_stopped(struct kvm_vcpu *vcpu)
107 {
108 return kvm_s390_test_cpuflags(vcpu, CPUSTAT_STOPPED);
109 }
110
is_vcpu_idle(struct kvm_vcpu * vcpu)111 static inline int is_vcpu_idle(struct kvm_vcpu *vcpu)
112 {
113 return test_bit(vcpu->vcpu_idx, vcpu->kvm->arch.idle_mask);
114 }
115
kvm_is_ucontrol(struct kvm * kvm)116 static inline int kvm_is_ucontrol(struct kvm *kvm)
117 {
118 #ifdef CONFIG_KVM_S390_UCONTROL
119 return test_bit(GMAP_FLAG_IS_UCONTROL, &kvm->arch.gmap->flags);
120 #else
121 return 0;
122 #endif
123 }
124
125 #define GUEST_PREFIX_SHIFT 12
126 #define GUEST_PREFIX_MASK_ZARCH 0x7fffe
127 #define GUEST_PREFIX_MASK_ESA 0x7ffff
kvm_s390_get_prefix(struct kvm_vcpu * vcpu)128 static inline u32 kvm_s390_get_prefix(struct kvm_vcpu *vcpu)
129 {
130 return vcpu->arch.sie_block->prefix << GUEST_PREFIX_SHIFT;
131 }
132
kvm_s390_set_prefix(struct kvm_vcpu * vcpu,u32 prefix)133 static inline void kvm_s390_set_prefix(struct kvm_vcpu *vcpu, u32 prefix)
134 {
135 VCPU_EVENT(vcpu, 3, "set prefix of cpu %03u to 0x%x", vcpu->vcpu_id,
136 prefix);
137 vcpu->arch.sie_block->prefix = prefix >> GUEST_PREFIX_SHIFT;
138 vcpu->arch.sie_block->prefix &= GUEST_PREFIX_MASK_ZARCH;
139 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
140 kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
141 }
142
kvm_s390_get_base_disp_s(struct kvm_vcpu * vcpu,u8 * ar)143 static inline u64 kvm_s390_get_base_disp_s(struct kvm_vcpu *vcpu, u8 *ar)
144 {
145 u32 base2 = vcpu->arch.sie_block->ipb >> 28;
146 u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
147
148 if (ar)
149 *ar = base2;
150
151 return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
152 }
153
kvm_s390_get_base_disp_siy(struct kvm_vcpu * vcpu,u8 * ar)154 static inline u64 kvm_s390_get_base_disp_siy(struct kvm_vcpu *vcpu, u8 *ar)
155 {
156 u32 base1 = vcpu->arch.sie_block->ipb >> 28;
157 s64 disp1;
158
159 /* The displacement is a 20bit _SIGNED_ value */
160 disp1 = sign_extend64(((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
161 ((vcpu->arch.sie_block->ipb & 0xff00) << 4), 19);
162
163 if (ar)
164 *ar = base1;
165
166 return (base1 ? vcpu->run->s.regs.gprs[base1] : 0) + disp1;
167 }
168
kvm_s390_get_base_disp_sse(struct kvm_vcpu * vcpu,u64 * address1,u64 * address2,u8 * ar_b1,u8 * ar_b2)169 static inline void kvm_s390_get_base_disp_sse(struct kvm_vcpu *vcpu,
170 u64 *address1, u64 *address2,
171 u8 *ar_b1, u8 *ar_b2)
172 {
173 u32 base1 = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
174 u32 disp1 = (vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16;
175 u32 base2 = (vcpu->arch.sie_block->ipb & 0xf000) >> 12;
176 u32 disp2 = vcpu->arch.sie_block->ipb & 0x0fff;
177
178 *address1 = (base1 ? vcpu->run->s.regs.gprs[base1] : 0) + disp1;
179 *address2 = (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
180
181 if (ar_b1)
182 *ar_b1 = base1;
183 if (ar_b2)
184 *ar_b2 = base2;
185 }
186
kvm_s390_get_regs_rre(struct kvm_vcpu * vcpu,int * r1,int * r2)187 static inline void kvm_s390_get_regs_rre(struct kvm_vcpu *vcpu, int *r1, int *r2)
188 {
189 if (r1)
190 *r1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 20;
191 if (r2)
192 *r2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
193 }
194
kvm_s390_get_base_disp_rsy(struct kvm_vcpu * vcpu,u8 * ar)195 static inline u64 kvm_s390_get_base_disp_rsy(struct kvm_vcpu *vcpu, u8 *ar)
196 {
197 u32 base2 = vcpu->arch.sie_block->ipb >> 28;
198 u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
199 ((vcpu->arch.sie_block->ipb & 0xff00) << 4);
200 /* The displacement is a 20bit _SIGNED_ value */
201 if (disp2 & 0x80000)
202 disp2+=0xfff00000;
203
204 if (ar)
205 *ar = base2;
206
207 return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + (long)(int)disp2;
208 }
209
kvm_s390_get_base_disp_rs(struct kvm_vcpu * vcpu,u8 * ar)210 static inline u64 kvm_s390_get_base_disp_rs(struct kvm_vcpu *vcpu, u8 *ar)
211 {
212 u32 base2 = vcpu->arch.sie_block->ipb >> 28;
213 u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
214
215 if (ar)
216 *ar = base2;
217
218 return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
219 }
220
221 /* Set the condition code in the guest program status word */
kvm_s390_set_psw_cc(struct kvm_vcpu * vcpu,unsigned long cc)222 static inline void kvm_s390_set_psw_cc(struct kvm_vcpu *vcpu, unsigned long cc)
223 {
224 vcpu->arch.sie_block->gpsw.mask &= ~(3UL << 44);
225 vcpu->arch.sie_block->gpsw.mask |= cc << 44;
226 }
227
228 /* test availability of facility in a kvm instance */
test_kvm_facility(struct kvm * kvm,unsigned long nr)229 static inline int test_kvm_facility(struct kvm *kvm, unsigned long nr)
230 {
231 return __test_facility(nr, kvm->arch.model.fac_mask) &&
232 __test_facility(nr, kvm->arch.model.fac_list);
233 }
234
set_kvm_facility(u64 * fac_list,unsigned long nr)235 static inline int set_kvm_facility(u64 *fac_list, unsigned long nr)
236 {
237 unsigned char *ptr;
238
239 if (nr >= MAX_FACILITY_BIT)
240 return -EINVAL;
241 ptr = (unsigned char *) fac_list + (nr >> 3);
242 *ptr |= (0x80UL >> (nr & 7));
243 return 0;
244 }
245
test_kvm_cpu_feat(struct kvm * kvm,unsigned long nr)246 static inline int test_kvm_cpu_feat(struct kvm *kvm, unsigned long nr)
247 {
248 WARN_ON_ONCE(nr >= KVM_S390_VM_CPU_FEAT_NR_BITS);
249 return test_bit_inv(nr, kvm->arch.cpu_feat);
250 }
251
252 /* are cpu states controlled by user space */
kvm_s390_user_cpu_state_ctrl(struct kvm * kvm)253 static inline int kvm_s390_user_cpu_state_ctrl(struct kvm *kvm)
254 {
255 return kvm->arch.user_cpu_state_ctrl != 0;
256 }
257
kvm_s390_set_user_cpu_state_ctrl(struct kvm * kvm)258 static inline void kvm_s390_set_user_cpu_state_ctrl(struct kvm *kvm)
259 {
260 if (kvm->arch.user_cpu_state_ctrl)
261 return;
262
263 VM_EVENT(kvm, 3, "%s", "ENABLE: Userspace CPU state control");
264 kvm->arch.user_cpu_state_ctrl = 1;
265 }
266
267 /* get the end gfn of the last (highest gfn) memslot */
kvm_s390_get_gfn_end(struct kvm_memslots * slots)268 static inline unsigned long kvm_s390_get_gfn_end(struct kvm_memslots *slots)
269 {
270 struct rb_node *node;
271 struct kvm_memory_slot *ms;
272
273 if (WARN_ON(kvm_memslots_empty(slots)))
274 return 0;
275
276 node = rb_last(&slots->gfn_tree);
277 ms = container_of(node, struct kvm_memory_slot, gfn_node[slots->node_idx]);
278 return ms->base_gfn + ms->npages;
279 }
280
kvm_s390_get_gisa_desc(struct kvm * kvm)281 static inline u32 kvm_s390_get_gisa_desc(struct kvm *kvm)
282 {
283 u32 gd;
284
285 if (!kvm->arch.gisa_int.origin)
286 return 0;
287
288 gd = virt_to_phys(kvm->arch.gisa_int.origin);
289
290 if (gd && sclp.has_gisaf)
291 gd |= GISA_FORMAT1;
292 return gd;
293 }
294
gpa_to_hva(struct kvm * kvm,gpa_t gpa)295 static inline hva_t gpa_to_hva(struct kvm *kvm, gpa_t gpa)
296 {
297 hva_t hva = gfn_to_hva(kvm, gpa_to_gfn(gpa));
298
299 if (!kvm_is_error_hva(hva))
300 hva |= offset_in_page(gpa);
301 return hva;
302 }
303
304 /* implemented in pv.c */
305 int kvm_s390_pv_destroy_cpu(struct kvm_vcpu *vcpu, u16 *rc, u16 *rrc);
306 int kvm_s390_pv_create_cpu(struct kvm_vcpu *vcpu, u16 *rc, u16 *rrc);
307 int kvm_s390_pv_set_aside(struct kvm *kvm, u16 *rc, u16 *rrc);
308 int kvm_s390_pv_deinit_aside_vm(struct kvm *kvm, u16 *rc, u16 *rrc);
309 int kvm_s390_pv_deinit_cleanup_all(struct kvm *kvm, u16 *rc, u16 *rrc);
310 int kvm_s390_pv_deinit_vm(struct kvm *kvm, u16 *rc, u16 *rrc);
311 int kvm_s390_pv_init_vm(struct kvm *kvm, u16 *rc, u16 *rrc);
312 int kvm_s390_pv_set_sec_parms(struct kvm *kvm, void *hdr, u64 length, u16 *rc,
313 u16 *rrc);
314 int kvm_s390_pv_unpack(struct kvm *kvm, unsigned long addr, unsigned long size,
315 unsigned long tweak, u16 *rc, u16 *rrc);
316 int kvm_s390_pv_set_cpu_state(struct kvm_vcpu *vcpu, u8 state);
317 int kvm_s390_pv_dump_cpu(struct kvm_vcpu *vcpu, void *buff, u16 *rc, u16 *rrc);
318 int kvm_s390_pv_dump_stor_state(struct kvm *kvm, void __user *buff_user,
319 u64 *gaddr, u64 buff_user_len, u16 *rc, u16 *rrc);
320 int kvm_s390_pv_dump_complete(struct kvm *kvm, void __user *buff_user,
321 u16 *rc, u16 *rrc);
322 int kvm_s390_pv_destroy_page(struct kvm *kvm, unsigned long gaddr);
323 int kvm_s390_pv_convert_to_secure(struct kvm *kvm, unsigned long gaddr);
324 int kvm_s390_pv_make_secure(struct kvm *kvm, unsigned long gaddr, void *uvcb);
325
kvm_s390_pv_get_handle(struct kvm * kvm)326 static inline u64 kvm_s390_pv_get_handle(struct kvm *kvm)
327 {
328 return kvm->arch.pv.handle;
329 }
330
kvm_s390_pv_cpu_get_handle(struct kvm_vcpu * vcpu)331 static inline u64 kvm_s390_pv_cpu_get_handle(struct kvm_vcpu *vcpu)
332 {
333 return vcpu->arch.pv.handle;
334 }
335
336 /**
337 * __kvm_s390_pv_destroy_page() - Destroy a guest page.
338 * @page: the page to destroy
339 *
340 * An attempt will be made to destroy the given guest page. If the attempt
341 * fails, an attempt is made to export the page. If both attempts fail, an
342 * appropriate error is returned.
343 *
344 * Context: must be called holding the mm lock for gmap->mm
345 */
__kvm_s390_pv_destroy_page(struct page * page)346 static inline int __kvm_s390_pv_destroy_page(struct page *page)
347 {
348 struct folio *folio = page_folio(page);
349 int rc;
350
351 /* Large folios cannot be secure. Small folio implies FW_LEVEL_PTE. */
352 if (folio_test_large(folio))
353 return -EFAULT;
354
355 rc = uv_destroy_folio(folio);
356 /*
357 * Fault handlers can race; it is possible that two CPUs will fault
358 * on the same secure page. One CPU can destroy the page, reboot,
359 * re-enter secure mode and import it, while the second CPU was
360 * stuck at the beginning of the handler. At some point the second
361 * CPU will be able to progress, and it will not be able to destroy
362 * the page. In that case we do not want to terminate the process,
363 * we instead try to export the page.
364 */
365 if (rc)
366 rc = uv_convert_from_secure_folio(folio);
367
368 return rc;
369 }
370
371 /* implemented in interrupt.c */
372 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu);
373 void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu);
374 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer);
375 int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu);
376 void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu);
377 void kvm_s390_clear_float_irqs(struct kvm *kvm);
378 int __must_check kvm_s390_inject_vm(struct kvm *kvm,
379 struct kvm_s390_interrupt *s390int,
380 struct kvm_s390_interrupt_info *inti);
381 int __must_check kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
382 struct kvm_s390_irq *irq);
kvm_s390_inject_prog_irq(struct kvm_vcpu * vcpu,struct kvm_s390_pgm_info * pgm_info)383 static inline int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
384 struct kvm_s390_pgm_info *pgm_info)
385 {
386 struct kvm_s390_irq irq = {
387 .type = KVM_S390_PROGRAM_INT,
388 .u.pgm = *pgm_info,
389 };
390
391 return kvm_s390_inject_vcpu(vcpu, &irq);
392 }
kvm_s390_inject_program_int(struct kvm_vcpu * vcpu,u16 code)393 static inline int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
394 {
395 struct kvm_s390_irq irq = {
396 .type = KVM_S390_PROGRAM_INT,
397 .u.pgm.code = code,
398 };
399
400 return kvm_s390_inject_vcpu(vcpu, &irq);
401 }
402 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
403 u64 isc_mask, u32 schid);
404 int kvm_s390_reinject_io_int(struct kvm *kvm,
405 struct kvm_s390_interrupt_info *inti);
406 int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked);
407
408 /* implemented in intercept.c */
409 u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu);
410 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu);
kvm_s390_rewind_psw(struct kvm_vcpu * vcpu,int ilen)411 static inline void kvm_s390_rewind_psw(struct kvm_vcpu *vcpu, int ilen)
412 {
413 struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
414
415 sie_block->gpsw.addr = __rewind_psw(sie_block->gpsw, ilen);
416 }
kvm_s390_forward_psw(struct kvm_vcpu * vcpu,int ilen)417 static inline void kvm_s390_forward_psw(struct kvm_vcpu *vcpu, int ilen)
418 {
419 kvm_s390_rewind_psw(vcpu, -ilen);
420 }
kvm_s390_retry_instr(struct kvm_vcpu * vcpu)421 static inline void kvm_s390_retry_instr(struct kvm_vcpu *vcpu)
422 {
423 /* don't inject PER events if we re-execute the instruction */
424 vcpu->arch.sie_block->icptstatus &= ~0x02;
425 kvm_s390_rewind_psw(vcpu, kvm_s390_get_ilen(vcpu));
426 }
427
428 int handle_sthyi(struct kvm_vcpu *vcpu);
429
430 /* implemented in priv.c */
431 int is_valid_psw(psw_t *psw);
432 int kvm_s390_handle_aa(struct kvm_vcpu *vcpu);
433 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu);
434 int kvm_s390_handle_e3(struct kvm_vcpu *vcpu);
435 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu);
436 int kvm_s390_handle_01(struct kvm_vcpu *vcpu);
437 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu);
438 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu);
439 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu);
440 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu);
441 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu);
442 int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu);
443
444 /* implemented in vsie.c */
445 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu);
446 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu);
447 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, gpa_t start, gpa_t end);
448 void kvm_s390_vsie_init(struct kvm *kvm);
449 void kvm_s390_vsie_destroy(struct kvm *kvm);
450
451 /* implemented in sigp.c */
452 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);
453 int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
454
455 /* implemented in s390.c */
456 int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod);
457 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr);
458 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);
459 int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu);
460 int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu);
461 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu);
462 void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu);
463 bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu);
464 void exit_sie(struct kvm_vcpu *vcpu);
465 void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu);
466 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu);
467 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu);
468 void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm);
469 __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu);
470 int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc);
471 int __kvm_s390_mprotect_many(struct gmap *gmap, gpa_t gpa, u8 npages, unsigned int prot,
472 unsigned long bits);
473
474 bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu);
475 void kvm_s390_update_cmma_dirty(struct kvm *kvm, const struct kvm_memory_slot *old);
476 int kvm_s390_vm_stop_migration(struct kvm *kvm);
477
478
479 /* implemented in diag.c */
480 int kvm_s390_handle_diag(struct kvm_vcpu *vcpu);
481
kvm_s390_vcpu_block_all(struct kvm * kvm)482 static inline void kvm_s390_vcpu_block_all(struct kvm *kvm)
483 {
484 unsigned long i;
485 struct kvm_vcpu *vcpu;
486
487 WARN_ON(!mutex_is_locked(&kvm->lock));
488 kvm_for_each_vcpu(i, vcpu, kvm)
489 kvm_s390_vcpu_block(vcpu);
490 }
491
kvm_s390_vcpu_unblock_all(struct kvm * kvm)492 static inline void kvm_s390_vcpu_unblock_all(struct kvm *kvm)
493 {
494 unsigned long i;
495 struct kvm_vcpu *vcpu;
496
497 kvm_for_each_vcpu(i, vcpu, kvm)
498 kvm_s390_vcpu_unblock(vcpu);
499 }
500
kvm_s390_get_tod_clock_fast(struct kvm * kvm)501 static inline u64 kvm_s390_get_tod_clock_fast(struct kvm *kvm)
502 {
503 u64 rc;
504
505 preempt_disable();
506 rc = get_tod_clock_fast() + kvm->arch.epoch;
507 preempt_enable();
508 return rc;
509 }
510
511 /**
512 * kvm_s390_inject_prog_cond - conditionally inject a program check
513 * @vcpu: virtual cpu
514 * @rc: original return/error code
515 *
516 * This function is supposed to be used after regular guest access functions
517 * failed, to conditionally inject a program check to a vcpu. The typical
518 * pattern would look like
519 *
520 * rc = write_guest(vcpu, addr, data, len);
521 * if (rc)
522 * return kvm_s390_inject_prog_cond(vcpu, rc);
523 *
524 * A negative return code from guest access functions implies an internal error
525 * like e.g. out of memory. In these cases no program check should be injected
526 * to the guest.
527 * A positive value implies that an exception happened while accessing a guest's
528 * memory. In this case all data belonging to the corresponding program check
529 * has been stored in vcpu->arch.pgm and can be injected with
530 * kvm_s390_inject_prog_irq().
531 *
532 * Returns: - the original @rc value if @rc was negative (internal error)
533 * - zero if @rc was already zero
534 * - zero or error code from injecting if @rc was positive
535 * (program check injected to @vcpu)
536 */
kvm_s390_inject_prog_cond(struct kvm_vcpu * vcpu,int rc)537 static inline int kvm_s390_inject_prog_cond(struct kvm_vcpu *vcpu, int rc)
538 {
539 if (rc <= 0)
540 return rc;
541 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
542 }
543
544 int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
545 struct kvm_s390_irq *s390irq);
546
547 /* implemented in interrupt.c */
548 int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop);
549 int psw_extint_disabled(struct kvm_vcpu *vcpu);
550 void kvm_s390_destroy_adapters(struct kvm *kvm);
551 int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu);
552 extern struct kvm_device_ops kvm_flic_ops;
553 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu);
554 int kvm_s390_is_restart_irq_pending(struct kvm_vcpu *vcpu);
555 void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu);
556 int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu,
557 void __user *buf, int len);
558 int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu,
559 __u8 __user *buf, int len);
560 void kvm_s390_gisa_init(struct kvm *kvm);
561 void kvm_s390_gisa_clear(struct kvm *kvm);
562 void kvm_s390_gisa_destroy(struct kvm *kvm);
563 void kvm_s390_gisa_disable(struct kvm *kvm);
564 void kvm_s390_gisa_enable(struct kvm *kvm);
565 int __init kvm_s390_gib_init(u8 nisc);
566 void kvm_s390_gib_destroy(void);
567 void kvm_s390_unmap_all_adapters(struct kvm *kvm);
568
569
570 /* implemented in guestdbg.c */
571 void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu);
572 void kvm_s390_restore_guest_per_regs(struct kvm_vcpu *vcpu);
573 void kvm_s390_patch_guest_per_regs(struct kvm_vcpu *vcpu);
574 int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
575 struct kvm_guest_debug *dbg);
576 void kvm_s390_clear_bp_data(struct kvm_vcpu *vcpu);
577 void kvm_s390_prepare_debug_exit(struct kvm_vcpu *vcpu);
578 int kvm_s390_handle_per_ifetch_icpt(struct kvm_vcpu *vcpu);
579 int kvm_s390_handle_per_event(struct kvm_vcpu *vcpu);
580
kvm_s390_use_sca_entries(void)581 static inline int kvm_s390_use_sca_entries(void)
582 {
583 /*
584 * Without SIGP interpretation, only SRS interpretation (if available)
585 * might use the entries. By not setting the entries and keeping them
586 * invalid, hardware will not access them but intercept.
587 */
588 return sclp.has_sigpif && sclp.has_esca;
589 }
590 void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
591 struct mcck_volatile_info *mcck_info);
592
kvm_s390_cur_gmap_fault_is_write(void)593 static inline bool kvm_s390_cur_gmap_fault_is_write(void)
594 {
595 if (current->thread.gmap_int_code == PGM_PROTECTION)
596 return true;
597 return test_facility(75) && (current->thread.gmap_teid.fsi == TEID_FSI_STORE);
598 }
599
kvm_s390_is_migration_mode(struct kvm * kvm)600 static __always_inline int kvm_s390_is_migration_mode(struct kvm *kvm)
601 {
602 return kvm->arch.migration_mode;
603 }
604
605 /**
606 * kvm_s390_vcpu_crypto_reset_all
607 *
608 * Reset the crypto attributes for each vcpu. This can be done while the vcpus
609 * are running as each vcpu will be removed from SIE before resetting the crypt
610 * attributes and restored to SIE afterward.
611 *
612 * Note: The kvm->lock must be held while calling this function
613 *
614 * @kvm: the KVM guest
615 */
616 void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm);
617
618 /**
619 * kvm_s390_vcpu_pci_enable_interp
620 *
621 * Set the associated PCI attributes for each vcpu to allow for zPCI Load/Store
622 * interpretation as well as adapter interruption forwarding.
623 *
624 * @kvm: the KVM guest
625 */
626 void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm);
627
628 /**
629 * diag9c_forwarding_hz
630 *
631 * Set the maximum number of diag9c forwarding per second
632 */
633 extern unsigned int diag9c_forwarding_hz;
634
635 #endif
636