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