xref: /linux/arch/s390/kvm/s390/s390.h (revision c03717417232c690534315a428e019a006502031)
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 
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 
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 
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 
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 
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 
106 static inline int is_vcpu_stopped(struct kvm_vcpu *vcpu)
107 {
108 	return kvm_s390_test_cpuflags(vcpu, CPUSTAT_STOPPED);
109 }
110 
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 
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
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 
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 
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 
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 
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 
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 
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 
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 */
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 */
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 
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 
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 */
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 
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 */
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 
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 
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 
326 static inline u64 kvm_s390_pv_get_handle(struct kvm *kvm)
327 {
328 	return kvm->arch.pv.handle;
329 }
330 
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  */
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 void kvm_s390_pv_sclp_kick(struct kvm_vcpu *vcpu);
379 int __must_check kvm_s390_inject_vm(struct kvm *kvm,
380 				    struct kvm_s390_interrupt *s390int,
381 				    struct kvm_s390_interrupt_info *inti);
382 int __must_check kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
383 				      struct kvm_s390_irq *irq);
384 static inline int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
385 					   struct kvm_s390_pgm_info *pgm_info)
386 {
387 	struct kvm_s390_irq irq = {
388 		.type = KVM_S390_PROGRAM_INT,
389 		.u.pgm = *pgm_info,
390 	};
391 
392 	return kvm_s390_inject_vcpu(vcpu, &irq);
393 }
394 static inline int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
395 {
396 	struct kvm_s390_irq irq = {
397 		.type = KVM_S390_PROGRAM_INT,
398 		.u.pgm.code = code,
399 	};
400 
401 	return kvm_s390_inject_vcpu(vcpu, &irq);
402 }
403 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
404 						    u64 isc_mask, u32 schid);
405 int kvm_s390_reinject_io_int(struct kvm *kvm,
406 			     struct kvm_s390_interrupt_info *inti);
407 int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked);
408 
409 /* implemented in intercept.c */
410 u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu);
411 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu);
412 static inline void kvm_s390_rewind_psw(struct kvm_vcpu *vcpu, int ilen)
413 {
414 	struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
415 
416 	sie_block->gpsw.addr = __rewind_psw(sie_block->gpsw, ilen);
417 }
418 static inline void kvm_s390_forward_psw(struct kvm_vcpu *vcpu, int ilen)
419 {
420 	kvm_s390_rewind_psw(vcpu, -ilen);
421 }
422 static inline void kvm_s390_retry_instr(struct kvm_vcpu *vcpu)
423 {
424 	/* don't inject PER events if we re-execute the instruction */
425 	vcpu->arch.sie_block->icptstatus &= ~0x02;
426 	kvm_s390_rewind_psw(vcpu, kvm_s390_get_ilen(vcpu));
427 }
428 
429 int handle_sthyi(struct kvm_vcpu *vcpu);
430 
431 /* implemented in priv.c */
432 int is_valid_psw(psw_t *psw);
433 int kvm_s390_handle_aa(struct kvm_vcpu *vcpu);
434 int kvm_s390_handle_b2(struct kvm_vcpu *vcpu);
435 int kvm_s390_handle_e3(struct kvm_vcpu *vcpu);
436 int kvm_s390_handle_e5(struct kvm_vcpu *vcpu);
437 int kvm_s390_handle_01(struct kvm_vcpu *vcpu);
438 int kvm_s390_handle_b9(struct kvm_vcpu *vcpu);
439 int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu);
440 int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu);
441 int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu);
442 int kvm_s390_handle_eb(struct kvm_vcpu *vcpu);
443 int kvm_s390_skey_check_enable(struct kvm_vcpu *vcpu);
444 
445 /* implemented in vsie.c */
446 int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu);
447 void kvm_s390_vsie_kick(struct kvm_vcpu *vcpu);
448 void kvm_s390_vsie_gmap_notifier(struct gmap *gmap, gpa_t start, gpa_t end);
449 void kvm_s390_vsie_init(struct kvm *kvm);
450 void kvm_s390_vsie_destroy(struct kvm *kvm);
451 
452 /* implemented in sigp.c */
453 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);
454 int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
455 
456 /* implemented in s390.c */
457 int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod);
458 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr);
459 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);
460 int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu);
461 int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu);
462 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu);
463 void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu);
464 bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu);
465 void exit_sie(struct kvm_vcpu *vcpu);
466 void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu);
467 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu);
468 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu);
469 void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm);
470 __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu);
471 int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc);
472 int __kvm_s390_mprotect_many(struct gmap *gmap, gpa_t gpa, u8 npages, unsigned int prot,
473 			     unsigned long bits);
474 
475 bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu);
476 void kvm_s390_update_cmma_dirty(struct kvm *kvm, const struct kvm_memory_slot *old);
477 int kvm_s390_vm_stop_migration(struct kvm *kvm);
478 
479 
480 /* implemented in diag.c */
481 int kvm_s390_handle_diag(struct kvm_vcpu *vcpu);
482 
483 static inline void kvm_s390_vcpu_block_all(struct kvm *kvm)
484 {
485 	unsigned long i;
486 	struct kvm_vcpu *vcpu;
487 
488 	WARN_ON(!mutex_is_locked(&kvm->lock));
489 	kvm_for_each_vcpu(i, vcpu, kvm)
490 		kvm_s390_vcpu_block(vcpu);
491 }
492 
493 static inline void kvm_s390_vcpu_unblock_all(struct kvm *kvm)
494 {
495 	unsigned long i;
496 	struct kvm_vcpu *vcpu;
497 
498 	kvm_for_each_vcpu(i, vcpu, kvm)
499 		kvm_s390_vcpu_unblock(vcpu);
500 }
501 
502 static inline u64 kvm_s390_get_tod_clock_fast(struct kvm *kvm)
503 {
504 	u64 rc;
505 
506 	preempt_disable();
507 	rc = get_tod_clock_fast() + kvm->arch.epoch;
508 	preempt_enable();
509 	return rc;
510 }
511 
512 /**
513  * kvm_s390_inject_prog_cond - conditionally inject a program check
514  * @vcpu: virtual cpu
515  * @rc: original return/error code
516  *
517  * This function is supposed to be used after regular guest access functions
518  * failed, to conditionally inject a program check to a vcpu. The typical
519  * pattern would look like
520  *
521  * rc = write_guest(vcpu, addr, data, len);
522  * if (rc)
523  *	return kvm_s390_inject_prog_cond(vcpu, rc);
524  *
525  * A negative return code from guest access functions implies an internal error
526  * like e.g. out of memory. In these cases no program check should be injected
527  * to the guest.
528  * A positive value implies that an exception happened while accessing a guest's
529  * memory. In this case all data belonging to the corresponding program check
530  * has been stored in vcpu->arch.pgm and can be injected with
531  * kvm_s390_inject_prog_irq().
532  *
533  * Returns: - the original @rc value if @rc was negative (internal error)
534  *	    - zero if @rc was already zero
535  *	    - zero or error code from injecting if @rc was positive
536  *	      (program check injected to @vcpu)
537  */
538 static inline int kvm_s390_inject_prog_cond(struct kvm_vcpu *vcpu, int rc)
539 {
540 	if (rc <= 0)
541 		return rc;
542 	return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
543 }
544 
545 int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
546 			struct kvm_s390_irq *s390irq);
547 
548 /* implemented in interrupt.c */
549 int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop);
550 int psw_extint_disabled(struct kvm_vcpu *vcpu);
551 void kvm_s390_destroy_adapters(struct kvm *kvm);
552 int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu);
553 extern struct kvm_device_ops kvm_flic_ops;
554 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu);
555 int kvm_s390_is_restart_irq_pending(struct kvm_vcpu *vcpu);
556 void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu);
557 int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu,
558 			   void __user *buf, int len);
559 int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu,
560 			   __u8 __user *buf, int len);
561 void kvm_s390_gisa_init(struct kvm *kvm);
562 void kvm_s390_gisa_clear(struct kvm *kvm);
563 void kvm_s390_gisa_destroy(struct kvm *kvm);
564 void kvm_s390_gisa_disable(struct kvm *kvm);
565 void kvm_s390_gisa_enable(struct kvm *kvm);
566 int __init kvm_s390_gib_init(u8 nisc);
567 void kvm_s390_gib_destroy(void);
568 void kvm_s390_unmap_all_adapters(struct kvm *kvm);
569 
570 
571 /* implemented in guestdbg.c */
572 void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu);
573 void kvm_s390_restore_guest_per_regs(struct kvm_vcpu *vcpu);
574 void kvm_s390_patch_guest_per_regs(struct kvm_vcpu *vcpu);
575 int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
576 			    struct kvm_guest_debug *dbg);
577 void kvm_s390_clear_bp_data(struct kvm_vcpu *vcpu);
578 void kvm_s390_prepare_debug_exit(struct kvm_vcpu *vcpu);
579 int kvm_s390_handle_per_ifetch_icpt(struct kvm_vcpu *vcpu);
580 int kvm_s390_handle_per_event(struct kvm_vcpu *vcpu);
581 
582 static inline int kvm_s390_use_sca_entries(void)
583 {
584 	/*
585 	 * Without SIGP interpretation, only SRS interpretation (if available)
586 	 * might use the entries. By not setting the entries and keeping them
587 	 * invalid, hardware will not access them but intercept.
588 	 */
589 	return sclp.has_sigpif && sclp.has_esca;
590 }
591 void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
592 				     struct mcck_volatile_info *mcck_info);
593 
594 static inline bool kvm_s390_cur_gmap_fault_is_write(void)
595 {
596 	if (current->thread.gmap_int_code == PGM_PROTECTION)
597 		return true;
598 	return test_facility(75) && (current->thread.gmap_teid.fsi == TEID_FSI_STORE);
599 }
600 
601 static __always_inline int kvm_s390_is_migration_mode(struct kvm *kvm)
602 {
603 	return kvm->arch.migration_mode;
604 }
605 
606 /**
607  * kvm_s390_vcpu_crypto_reset_all
608  *
609  * Reset the crypto attributes for each vcpu. This can be done while the vcpus
610  * are running as each vcpu will be removed from SIE before resetting the crypt
611  * attributes and restored to SIE afterward.
612  *
613  * Note: The kvm->lock must be held while calling this function
614  *
615  * @kvm: the KVM guest
616  */
617 void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm);
618 
619 /**
620  * kvm_s390_vcpu_pci_enable_interp
621  *
622  * Set the associated PCI attributes for each vcpu to allow for zPCI Load/Store
623  * interpretation as well as adapter interruption forwarding.
624  *
625  * @kvm: the KVM guest
626  */
627 void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm);
628 
629 /**
630  * diag9c_forwarding_hz
631  *
632  * Set the maximum number of diag9c forwarding per second
633  */
634 extern unsigned int diag9c_forwarding_hz;
635 
636 #endif
637