KVM: s390: switch to get_tod_clock() and fix STP sync races
[linux-2.6-block.git] / arch / s390 / kvm / kvm-s390.h
CommitLineData
b0c632db 1/*
a53c8fab 2 * definition for kvm on s390
b0c632db 3 *
a53c8fab 4 * Copyright IBM Corp. 2008, 2009
b0c632db
HC
5 *
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.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
628eb9b8 12 * Christian Ehrhardt <ehrhardt@de.ibm.com>
b0c632db
HC
13 */
14
15#ifndef ARCH_S390_KVM_S390_H
16#define ARCH_S390_KVM_S390_H
8f2abe6a 17
ca872302 18#include <linux/hrtimer.h>
ba5c1e9b 19#include <linux/kvm.h>
8f2abe6a 20#include <linux/kvm_host.h>
9d8d5786 21#include <asm/facility.h>
8f2abe6a
CB
22
23typedef int (*intercept_handler_t)(struct kvm_vcpu *vcpu);
24
7feb6bb8
MM
25/* Transactional Memory Execution related macros */
26#define IS_TE_ENABLED(vcpu) ((vcpu->arch.sie_block->ecb & 0x10))
7feb6bb8
MM
27#define TDB_FORMAT1 1
28#define IS_ITDB_VALID(vcpu) ((*(char *)vcpu->arch.sie_block->itdba == TDB_FORMAT1))
29
78f26131
CB
30extern debug_info_t *kvm_s390_dbf;
31#define KVM_EVENT(d_loglevel, d_string, d_args...)\
32do { \
33 debug_sprintf_event(kvm_s390_dbf, d_loglevel, d_string "\n", \
34 d_args); \
35} while (0)
36
b0c632db
HC
37#define VM_EVENT(d_kvm, d_loglevel, d_string, d_args...)\
38do { \
39 debug_sprintf_event(d_kvm->arch.dbf, d_loglevel, d_string "\n", \
40 d_args); \
41} while (0)
42
43#define VCPU_EVENT(d_vcpu, d_loglevel, d_string, d_args...)\
44do { \
45 debug_sprintf_event(d_vcpu->kvm->arch.dbf, d_loglevel, \
46 "%02d[%016lx-%016lx]: " d_string "\n", d_vcpu->vcpu_id, \
47 d_vcpu->arch.sie_block->gpsw.mask, d_vcpu->arch.sie_block->gpsw.addr,\
48 d_args); \
49} while (0)
ba5c1e9b 50
7a42fdc2 51static inline int is_vcpu_stopped(struct kvm_vcpu *vcpu)
ba5c1e9b 52{
7a42fdc2 53 return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOPPED;
ba5c1e9b
CO
54}
55
e08b9637
CO
56static inline int kvm_is_ucontrol(struct kvm *kvm)
57{
58#ifdef CONFIG_KVM_S390_UCONTROL
59 if (kvm->arch.gmap)
60 return 0;
61 return 1;
62#else
63 return 0;
64#endif
65}
8d26cf7b 66
fda902cb
MM
67#define GUEST_PREFIX_SHIFT 13
68static inline u32 kvm_s390_get_prefix(struct kvm_vcpu *vcpu)
69{
70 return vcpu->arch.sie_block->prefix << GUEST_PREFIX_SHIFT;
71}
72
8d26cf7b
CB
73static inline void kvm_s390_set_prefix(struct kvm_vcpu *vcpu, u32 prefix)
74{
71db35d2
CB
75 VCPU_EVENT(vcpu, 3, "set prefix of cpu %03u to 0x%x", vcpu->vcpu_id,
76 prefix);
fda902cb 77 vcpu->arch.sie_block->prefix = prefix >> GUEST_PREFIX_SHIFT;
d3d692c8 78 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2c70fe44 79 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
8d26cf7b
CB
80}
81
8ae04b8f
AY
82typedef u8 __bitwise ar_t;
83
84static inline u64 kvm_s390_get_base_disp_s(struct kvm_vcpu *vcpu, ar_t *ar)
b1c571a5 85{
0c29b229
CB
86 u32 base2 = vcpu->arch.sie_block->ipb >> 28;
87 u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
b1c571a5 88
8ae04b8f
AY
89 if (ar)
90 *ar = base2;
91
b1c571a5
CH
92 return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
93}
94
95static inline void kvm_s390_get_base_disp_sse(struct kvm_vcpu *vcpu,
8ae04b8f
AY
96 u64 *address1, u64 *address2,
97 ar_t *ar_b1, ar_t *ar_b2)
b1c571a5 98{
0c29b229
CB
99 u32 base1 = (vcpu->arch.sie_block->ipb & 0xf0000000) >> 28;
100 u32 disp1 = (vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16;
101 u32 base2 = (vcpu->arch.sie_block->ipb & 0xf000) >> 12;
102 u32 disp2 = vcpu->arch.sie_block->ipb & 0x0fff;
b1c571a5
CH
103
104 *address1 = (base1 ? vcpu->run->s.regs.gprs[base1] : 0) + disp1;
105 *address2 = (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
8ae04b8f
AY
106
107 if (ar_b1)
108 *ar_b1 = base1;
109 if (ar_b2)
110 *ar_b2 = base2;
b1c571a5
CH
111}
112
69d0d3a3
CB
113static inline void kvm_s390_get_regs_rre(struct kvm_vcpu *vcpu, int *r1, int *r2)
114{
ff7158b2
TH
115 if (r1)
116 *r1 = (vcpu->arch.sie_block->ipb & 0x00f00000) >> 20;
117 if (r2)
118 *r2 = (vcpu->arch.sie_block->ipb & 0x000f0000) >> 16;
69d0d3a3
CB
119}
120
8ae04b8f 121static inline u64 kvm_s390_get_base_disp_rsy(struct kvm_vcpu *vcpu, ar_t *ar)
b1c571a5 122{
0c29b229
CB
123 u32 base2 = vcpu->arch.sie_block->ipb >> 28;
124 u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
b1c571a5 125 ((vcpu->arch.sie_block->ipb & 0xff00) << 4);
0c29b229
CB
126 /* The displacement is a 20bit _SIGNED_ value */
127 if (disp2 & 0x80000)
128 disp2+=0xfff00000;
b1c571a5 129
8ae04b8f
AY
130 if (ar)
131 *ar = base2;
132
0c29b229 133 return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + (long)(int)disp2;
b1c571a5
CH
134}
135
8ae04b8f 136static inline u64 kvm_s390_get_base_disp_rs(struct kvm_vcpu *vcpu, ar_t *ar)
b1c571a5 137{
0c29b229
CB
138 u32 base2 = vcpu->arch.sie_block->ipb >> 28;
139 u32 disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
b1c571a5 140
8ae04b8f
AY
141 if (ar)
142 *ar = base2;
143
b1c571a5
CH
144 return (base2 ? vcpu->run->s.regs.gprs[base2] : 0) + disp2;
145}
146
ea828ebf
TH
147/* Set the condition code in the guest program status word */
148static inline void kvm_s390_set_psw_cc(struct kvm_vcpu *vcpu, unsigned long cc)
149{
150 vcpu->arch.sie_block->gpsw.mask &= ~(3UL << 44);
151 vcpu->arch.sie_block->gpsw.mask |= cc << 44;
152}
153
1e8d2424 154/* test availability of facility in a kvm instance */
9d8d5786
MM
155static inline int test_kvm_facility(struct kvm *kvm, unsigned long nr)
156{
981467c9
MM
157 return __test_facility(nr, kvm->arch.model.fac->mask) &&
158 __test_facility(nr, kvm->arch.model.fac->list);
9d8d5786
MM
159}
160
18280d8b
MM
161static inline int set_kvm_facility(u64 *fac_list, unsigned long nr)
162{
163 unsigned char *ptr;
164
165 if (nr >= MAX_FACILITY_BIT)
166 return -EINVAL;
167 ptr = (unsigned char *) fac_list + (nr >> 3);
168 *ptr |= (0x80UL >> (nr & 7));
169 return 0;
170}
171
6352e4d2
DH
172/* are cpu states controlled by user space */
173static inline int kvm_s390_user_cpu_state_ctrl(struct kvm *kvm)
174{
175 return kvm->arch.user_cpu_state_ctrl != 0;
176}
177
66933b78 178/* implemented in interrupt.c */
ba5c1e9b 179int kvm_s390_handle_wait(struct kvm_vcpu *vcpu);
0e9c85a5 180void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu);
ca872302 181enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer);
614aeab4 182int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu);
2ed10cc1 183void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu);
67335e63 184void kvm_s390_clear_float_irqs(struct kvm *kvm);
db4a29cb
HC
185int __must_check kvm_s390_inject_vm(struct kvm *kvm,
186 struct kvm_s390_interrupt *s390int);
187int __must_check kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
383d0b05 188 struct kvm_s390_irq *irq);
66933b78
DH
189static inline int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
190 struct kvm_s390_pgm_info *pgm_info)
191{
192 struct kvm_s390_irq irq = {
193 .type = KVM_S390_PROGRAM_INT,
194 .u.pgm = *pgm_info,
195 };
196
197 return kvm_s390_inject_vcpu(vcpu, &irq);
198}
199static inline int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
200{
201 struct kvm_s390_irq irq = {
202 .type = KVM_S390_PROGRAM_INT,
203 .u.pgm.code = code,
204 };
205
206 return kvm_s390_inject_vcpu(vcpu, &irq);
207}
fa6b7fe9 208struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
6d3da241 209 u64 isc_mask, u32 schid);
15462e37
DH
210int kvm_s390_reinject_io_int(struct kvm *kvm,
211 struct kvm_s390_interrupt_info *inti);
841b91c5 212int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked);
453423dc 213
04b41acd
TH
214/* implemented in intercept.c */
215void kvm_s390_rewind_psw(struct kvm_vcpu *vcpu, int ilc);
216int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu);
217
453423dc 218/* implemented in priv.c */
a3fb577e 219int is_valid_psw(psw_t *psw);
70455a36 220int kvm_s390_handle_b2(struct kvm_vcpu *vcpu);
bb25b9ba 221int kvm_s390_handle_e5(struct kvm_vcpu *vcpu);
8c3f61e2 222int kvm_s390_handle_01(struct kvm_vcpu *vcpu);
48a3e950
CH
223int kvm_s390_handle_b9(struct kvm_vcpu *vcpu);
224int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu);
aba07508 225int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu);
953ed88d
TH
226int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu);
227int kvm_s390_handle_eb(struct kvm_vcpu *vcpu);
5288fbf0
CB
228
229/* implemented in sigp.c */
230int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu);
4953919f 231int kvm_s390_handle_sigp_pei(struct kvm_vcpu *vcpu);
5288fbf0
CB
232
233/* implemented in kvm-s390.c */
fa576c58 234long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable);
e879892c 235int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long addr);
bc17de7c
EF
236int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
237 unsigned long addr);
e879892c 238int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr);
bc17de7c 239int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr);
6852d7b6
DH
240void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu);
241void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu);
27406cd5
CB
242void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu);
243void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu);
49b99e1e 244void exit_sie(struct kvm_vcpu *vcpu);
8e236546 245void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu);
b31605c1
DD
246int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu);
247void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu);
9d8d5786
MM
248unsigned long kvm_s390_fac_list_mask_size(void);
249extern unsigned long kvm_s390_fac_list_mask[];
b31605c1 250
e28acfea
CB
251/* implemented in diag.c */
252int kvm_s390_handle_diag(struct kvm_vcpu *vcpu);
253
27406cd5
CB
254static inline void kvm_s390_vcpu_block_all(struct kvm *kvm)
255{
256 int i;
257 struct kvm_vcpu *vcpu;
258
259 WARN_ON(!mutex_is_locked(&kvm->lock));
260 kvm_for_each_vcpu(i, vcpu, kvm)
261 kvm_s390_vcpu_block(vcpu);
262}
263
264static inline void kvm_s390_vcpu_unblock_all(struct kvm *kvm)
265{
266 int i;
267 struct kvm_vcpu *vcpu;
268
269 kvm_for_each_vcpu(i, vcpu, kvm)
270 kvm_s390_vcpu_unblock(vcpu);
271}
272
1b0462e5
HC
273/**
274 * kvm_s390_inject_prog_cond - conditionally inject a program check
275 * @vcpu: virtual cpu
276 * @rc: original return/error code
277 *
278 * This function is supposed to be used after regular guest access functions
279 * failed, to conditionally inject a program check to a vcpu. The typical
280 * pattern would look like
281 *
282 * rc = write_guest(vcpu, addr, data, len);
283 * if (rc)
284 * return kvm_s390_inject_prog_cond(vcpu, rc);
285 *
286 * A negative return code from guest access functions implies an internal error
287 * like e.g. out of memory. In these cases no program check should be injected
288 * to the guest.
289 * A positive value implies that an exception happened while accessing a guest's
290 * memory. In this case all data belonging to the corresponding program check
291 * has been stored in vcpu->arch.pgm and can be injected with
292 * kvm_s390_inject_prog_irq().
293 *
294 * Returns: - the original @rc value if @rc was negative (internal error)
295 * - zero if @rc was already zero
296 * - zero or error code from injecting if @rc was positive
297 * (program check injected to @vcpu)
298 */
299static inline int kvm_s390_inject_prog_cond(struct kvm_vcpu *vcpu, int rc)
300{
301 if (rc <= 0)
302 return rc;
303 return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
304}
305
383d0b05
JF
306int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
307 struct kvm_s390_irq *s390irq);
308
3c038e6b 309/* implemented in interrupt.c */
9a022067 310int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop);
3c038e6b 311int psw_extint_disabled(struct kvm_vcpu *vcpu);
841b91c5 312void kvm_s390_destroy_adapters(struct kvm *kvm);
ea5f4969 313int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu);
84877d93 314extern struct kvm_device_ops kvm_flic_ops;
6cddd432
DH
315int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu);
316void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu);
816c7667
JF
317int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu,
318 void __user *buf, int len);
319int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu,
320 __u8 __user *buf, int len);
3c038e6b 321
27291e21
DH
322/* implemented in guestdbg.c */
323void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu);
324void kvm_s390_restore_guest_per_regs(struct kvm_vcpu *vcpu);
325void kvm_s390_patch_guest_per_regs(struct kvm_vcpu *vcpu);
326int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
327 struct kvm_guest_debug *dbg);
328void kvm_s390_clear_bp_data(struct kvm_vcpu *vcpu);
329void kvm_s390_prepare_debug_exit(struct kvm_vcpu *vcpu);
330void kvm_s390_handle_per_event(struct kvm_vcpu *vcpu);
331
b0c632db 332#endif