]>
Commit | Line | Data |
---|---|---|
b0c632db | 1 | /* |
a53c8fab | 2 | * hosting zSeries kernel virtual machines |
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> | |
12 | * Heiko Carstens <heiko.carstens@de.ibm.com> | |
628eb9b8 | 13 | * Christian Ehrhardt <ehrhardt@de.ibm.com> |
15f36ebd | 14 | * Jason J. Herne <jjherne@us.ibm.com> |
b0c632db HC |
15 | */ |
16 | ||
17 | #include <linux/compiler.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/fs.h> | |
ca872302 | 20 | #include <linux/hrtimer.h> |
b0c632db HC |
21 | #include <linux/init.h> |
22 | #include <linux/kvm.h> | |
23 | #include <linux/kvm_host.h> | |
24 | #include <linux/module.h> | |
a374e892 | 25 | #include <linux/random.h> |
b0c632db | 26 | #include <linux/slab.h> |
ba5c1e9b | 27 | #include <linux/timer.h> |
41408c28 | 28 | #include <linux/vmalloc.h> |
cbb870c8 | 29 | #include <asm/asm-offsets.h> |
b0c632db HC |
30 | #include <asm/lowcore.h> |
31 | #include <asm/pgtable.h> | |
f5daba1d | 32 | #include <asm/nmi.h> |
a0616cde | 33 | #include <asm/switch_to.h> |
6d3da241 | 34 | #include <asm/isc.h> |
1526bf9c | 35 | #include <asm/sclp.h> |
8f2abe6a | 36 | #include "kvm-s390.h" |
b0c632db HC |
37 | #include "gaccess.h" |
38 | ||
ea2cdd27 DH |
39 | #define KMSG_COMPONENT "kvm-s390" |
40 | #undef pr_fmt | |
41 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
42 | ||
5786fffa CH |
43 | #define CREATE_TRACE_POINTS |
44 | #include "trace.h" | |
ade38c31 | 45 | #include "trace-s390.h" |
5786fffa | 46 | |
41408c28 | 47 | #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ |
816c7667 JF |
48 | #define LOCAL_IRQS 32 |
49 | #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ | |
50 | (KVM_MAX_VCPUS + LOCAL_IRQS)) | |
41408c28 | 51 | |
b0c632db HC |
52 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
53 | ||
54 | struct kvm_stats_debugfs_item debugfs_entries[] = { | |
55 | { "userspace_handled", VCPU_STAT(exit_userspace) }, | |
0eaeafa1 | 56 | { "exit_null", VCPU_STAT(exit_null) }, |
8f2abe6a CB |
57 | { "exit_validity", VCPU_STAT(exit_validity) }, |
58 | { "exit_stop_request", VCPU_STAT(exit_stop_request) }, | |
59 | { "exit_external_request", VCPU_STAT(exit_external_request) }, | |
60 | { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) }, | |
ba5c1e9b CO |
61 | { "exit_instruction", VCPU_STAT(exit_instruction) }, |
62 | { "exit_program_interruption", VCPU_STAT(exit_program_interruption) }, | |
63 | { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) }, | |
f7819512 | 64 | { "halt_successful_poll", VCPU_STAT(halt_successful_poll) }, |
ce2e4f0b | 65 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
f5e10b09 | 66 | { "instruction_lctlg", VCPU_STAT(instruction_lctlg) }, |
ba5c1e9b | 67 | { "instruction_lctl", VCPU_STAT(instruction_lctl) }, |
aba07508 DH |
68 | { "instruction_stctl", VCPU_STAT(instruction_stctl) }, |
69 | { "instruction_stctg", VCPU_STAT(instruction_stctg) }, | |
ba5c1e9b | 70 | { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) }, |
7697e71f | 71 | { "deliver_external_call", VCPU_STAT(deliver_external_call) }, |
ba5c1e9b CO |
72 | { "deliver_service_signal", VCPU_STAT(deliver_service_signal) }, |
73 | { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) }, | |
74 | { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) }, | |
75 | { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) }, | |
76 | { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) }, | |
77 | { "deliver_program_interruption", VCPU_STAT(deliver_program_int) }, | |
78 | { "exit_wait_state", VCPU_STAT(exit_wait_state) }, | |
69d0d3a3 | 79 | { "instruction_pfmf", VCPU_STAT(instruction_pfmf) }, |
453423dc CB |
80 | { "instruction_stidp", VCPU_STAT(instruction_stidp) }, |
81 | { "instruction_spx", VCPU_STAT(instruction_spx) }, | |
82 | { "instruction_stpx", VCPU_STAT(instruction_stpx) }, | |
83 | { "instruction_stap", VCPU_STAT(instruction_stap) }, | |
84 | { "instruction_storage_key", VCPU_STAT(instruction_storage_key) }, | |
8a242234 | 85 | { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) }, |
453423dc CB |
86 | { "instruction_stsch", VCPU_STAT(instruction_stsch) }, |
87 | { "instruction_chsc", VCPU_STAT(instruction_chsc) }, | |
b31288fa | 88 | { "instruction_essa", VCPU_STAT(instruction_essa) }, |
453423dc CB |
89 | { "instruction_stsi", VCPU_STAT(instruction_stsi) }, |
90 | { "instruction_stfl", VCPU_STAT(instruction_stfl) }, | |
bb25b9ba | 91 | { "instruction_tprot", VCPU_STAT(instruction_tprot) }, |
5288fbf0 | 92 | { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) }, |
bd59d3a4 | 93 | { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) }, |
7697e71f | 94 | { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) }, |
5288fbf0 | 95 | { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) }, |
42cb0c9f DH |
96 | { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) }, |
97 | { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) }, | |
5288fbf0 | 98 | { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) }, |
42cb0c9f DH |
99 | { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) }, |
100 | { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) }, | |
cd7b4b61 | 101 | { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) }, |
5288fbf0 CB |
102 | { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) }, |
103 | { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) }, | |
104 | { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) }, | |
42cb0c9f DH |
105 | { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) }, |
106 | { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) }, | |
107 | { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) }, | |
388186bc | 108 | { "diagnose_10", VCPU_STAT(diagnose_10) }, |
e28acfea | 109 | { "diagnose_44", VCPU_STAT(diagnose_44) }, |
41628d33 | 110 | { "diagnose_9c", VCPU_STAT(diagnose_9c) }, |
175a5c9e CB |
111 | { "diagnose_258", VCPU_STAT(diagnose_258) }, |
112 | { "diagnose_308", VCPU_STAT(diagnose_308) }, | |
113 | { "diagnose_500", VCPU_STAT(diagnose_500) }, | |
b0c632db HC |
114 | { NULL } |
115 | }; | |
116 | ||
9d8d5786 MM |
117 | /* upper facilities limit for kvm */ |
118 | unsigned long kvm_s390_fac_list_mask[] = { | |
a3ed8dae | 119 | 0xffe6fffbfcfdfc40UL, |
53df84f8 | 120 | 0x005e800000000000UL, |
9d8d5786 | 121 | }; |
b0c632db | 122 | |
9d8d5786 | 123 | unsigned long kvm_s390_fac_list_mask_size(void) |
78c4b59f | 124 | { |
9d8d5786 MM |
125 | BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64); |
126 | return ARRAY_SIZE(kvm_s390_fac_list_mask); | |
78c4b59f MM |
127 | } |
128 | ||
9d8d5786 | 129 | static struct gmap_notifier gmap_notifier; |
78f26131 | 130 | debug_info_t *kvm_s390_dbf; |
9d8d5786 | 131 | |
b0c632db | 132 | /* Section: not file related */ |
13a34e06 | 133 | int kvm_arch_hardware_enable(void) |
b0c632db HC |
134 | { |
135 | /* every s390 is virtualization enabled ;-) */ | |
10474ae8 | 136 | return 0; |
b0c632db HC |
137 | } |
138 | ||
2c70fe44 CB |
139 | static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address); |
140 | ||
b0c632db HC |
141 | int kvm_arch_hardware_setup(void) |
142 | { | |
2c70fe44 CB |
143 | gmap_notifier.notifier_call = kvm_gmap_notifier; |
144 | gmap_register_ipte_notifier(&gmap_notifier); | |
b0c632db HC |
145 | return 0; |
146 | } | |
147 | ||
148 | void kvm_arch_hardware_unsetup(void) | |
149 | { | |
2c70fe44 | 150 | gmap_unregister_ipte_notifier(&gmap_notifier); |
b0c632db HC |
151 | } |
152 | ||
b0c632db HC |
153 | int kvm_arch_init(void *opaque) |
154 | { | |
78f26131 CB |
155 | kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); |
156 | if (!kvm_s390_dbf) | |
157 | return -ENOMEM; | |
158 | ||
159 | if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) { | |
160 | debug_unregister(kvm_s390_dbf); | |
161 | return -ENOMEM; | |
162 | } | |
163 | ||
84877d93 CH |
164 | /* Register floating interrupt controller interface. */ |
165 | return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); | |
b0c632db HC |
166 | } |
167 | ||
78f26131 CB |
168 | void kvm_arch_exit(void) |
169 | { | |
170 | debug_unregister(kvm_s390_dbf); | |
171 | } | |
172 | ||
b0c632db HC |
173 | /* Section: device related */ |
174 | long kvm_arch_dev_ioctl(struct file *filp, | |
175 | unsigned int ioctl, unsigned long arg) | |
176 | { | |
177 | if (ioctl == KVM_S390_ENABLE_SIE) | |
178 | return s390_enable_sie(); | |
179 | return -EINVAL; | |
180 | } | |
181 | ||
784aa3d7 | 182 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
b0c632db | 183 | { |
d7b0b5eb CO |
184 | int r; |
185 | ||
2bd0ac4e | 186 | switch (ext) { |
d7b0b5eb | 187 | case KVM_CAP_S390_PSW: |
b6cf8788 | 188 | case KVM_CAP_S390_GMAP: |
52e16b18 | 189 | case KVM_CAP_SYNC_MMU: |
1efd0f59 CO |
190 | #ifdef CONFIG_KVM_S390_UCONTROL |
191 | case KVM_CAP_S390_UCONTROL: | |
192 | #endif | |
3c038e6b | 193 | case KVM_CAP_ASYNC_PF: |
60b413c9 | 194 | case KVM_CAP_SYNC_REGS: |
14eebd91 | 195 | case KVM_CAP_ONE_REG: |
d6712df9 | 196 | case KVM_CAP_ENABLE_CAP: |
fa6b7fe9 | 197 | case KVM_CAP_S390_CSS_SUPPORT: |
10ccaa1e | 198 | case KVM_CAP_IOEVENTFD: |
c05c4186 | 199 | case KVM_CAP_DEVICE_CTRL: |
d938dc55 | 200 | case KVM_CAP_ENABLE_CAP_VM: |
78599d90 | 201 | case KVM_CAP_S390_IRQCHIP: |
f2061656 | 202 | case KVM_CAP_VM_ATTRIBUTES: |
6352e4d2 | 203 | case KVM_CAP_MP_STATE: |
47b43c52 | 204 | case KVM_CAP_S390_INJECT_IRQ: |
2444b352 | 205 | case KVM_CAP_S390_USER_SIGP: |
e44fc8c9 | 206 | case KVM_CAP_S390_USER_STSI: |
30ee2a98 | 207 | case KVM_CAP_S390_SKEYS: |
816c7667 | 208 | case KVM_CAP_S390_IRQ_STATE: |
d7b0b5eb CO |
209 | r = 1; |
210 | break; | |
41408c28 TH |
211 | case KVM_CAP_S390_MEM_OP: |
212 | r = MEM_OP_MAX_SIZE; | |
213 | break; | |
e726b1bd CB |
214 | case KVM_CAP_NR_VCPUS: |
215 | case KVM_CAP_MAX_VCPUS: | |
216 | r = KVM_MAX_VCPUS; | |
217 | break; | |
e1e2e605 NW |
218 | case KVM_CAP_NR_MEMSLOTS: |
219 | r = KVM_USER_MEM_SLOTS; | |
220 | break; | |
1526bf9c | 221 | case KVM_CAP_S390_COW: |
abf09bed | 222 | r = MACHINE_HAS_ESOP; |
1526bf9c | 223 | break; |
68c55750 EF |
224 | case KVM_CAP_S390_VECTOR_REGISTERS: |
225 | r = MACHINE_HAS_VX; | |
226 | break; | |
2bd0ac4e | 227 | default: |
d7b0b5eb | 228 | r = 0; |
2bd0ac4e | 229 | } |
d7b0b5eb | 230 | return r; |
b0c632db HC |
231 | } |
232 | ||
15f36ebd JH |
233 | static void kvm_s390_sync_dirty_log(struct kvm *kvm, |
234 | struct kvm_memory_slot *memslot) | |
235 | { | |
236 | gfn_t cur_gfn, last_gfn; | |
237 | unsigned long address; | |
238 | struct gmap *gmap = kvm->arch.gmap; | |
239 | ||
240 | down_read(&gmap->mm->mmap_sem); | |
241 | /* Loop over all guest pages */ | |
242 | last_gfn = memslot->base_gfn + memslot->npages; | |
243 | for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) { | |
244 | address = gfn_to_hva_memslot(memslot, cur_gfn); | |
245 | ||
246 | if (gmap_test_and_clear_dirty(address, gmap)) | |
247 | mark_page_dirty(kvm, cur_gfn); | |
248 | } | |
249 | up_read(&gmap->mm->mmap_sem); | |
250 | } | |
251 | ||
b0c632db HC |
252 | /* Section: vm related */ |
253 | /* | |
254 | * Get (and clear) the dirty memory log for a memory slot. | |
255 | */ | |
256 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, | |
257 | struct kvm_dirty_log *log) | |
258 | { | |
15f36ebd JH |
259 | int r; |
260 | unsigned long n; | |
9f6b8029 | 261 | struct kvm_memslots *slots; |
15f36ebd JH |
262 | struct kvm_memory_slot *memslot; |
263 | int is_dirty = 0; | |
264 | ||
265 | mutex_lock(&kvm->slots_lock); | |
266 | ||
267 | r = -EINVAL; | |
268 | if (log->slot >= KVM_USER_MEM_SLOTS) | |
269 | goto out; | |
270 | ||
9f6b8029 PB |
271 | slots = kvm_memslots(kvm); |
272 | memslot = id_to_memslot(slots, log->slot); | |
15f36ebd JH |
273 | r = -ENOENT; |
274 | if (!memslot->dirty_bitmap) | |
275 | goto out; | |
276 | ||
277 | kvm_s390_sync_dirty_log(kvm, memslot); | |
278 | r = kvm_get_dirty_log(kvm, log, &is_dirty); | |
279 | if (r) | |
280 | goto out; | |
281 | ||
282 | /* Clear the dirty log */ | |
283 | if (is_dirty) { | |
284 | n = kvm_dirty_bitmap_bytes(memslot); | |
285 | memset(memslot->dirty_bitmap, 0, n); | |
286 | } | |
287 | r = 0; | |
288 | out: | |
289 | mutex_unlock(&kvm->slots_lock); | |
290 | return r; | |
b0c632db HC |
291 | } |
292 | ||
d938dc55 CH |
293 | static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) |
294 | { | |
295 | int r; | |
296 | ||
297 | if (cap->flags) | |
298 | return -EINVAL; | |
299 | ||
300 | switch (cap->cap) { | |
84223598 CH |
301 | case KVM_CAP_S390_IRQCHIP: |
302 | kvm->arch.use_irqchip = 1; | |
303 | r = 0; | |
304 | break; | |
2444b352 DH |
305 | case KVM_CAP_S390_USER_SIGP: |
306 | kvm->arch.user_sigp = 1; | |
307 | r = 0; | |
308 | break; | |
68c55750 | 309 | case KVM_CAP_S390_VECTOR_REGISTERS: |
18280d8b MM |
310 | if (MACHINE_HAS_VX) { |
311 | set_kvm_facility(kvm->arch.model.fac->mask, 129); | |
312 | set_kvm_facility(kvm->arch.model.fac->list, 129); | |
313 | r = 0; | |
314 | } else | |
315 | r = -EINVAL; | |
68c55750 | 316 | break; |
e44fc8c9 ET |
317 | case KVM_CAP_S390_USER_STSI: |
318 | kvm->arch.user_stsi = 1; | |
319 | r = 0; | |
320 | break; | |
d938dc55 CH |
321 | default: |
322 | r = -EINVAL; | |
323 | break; | |
324 | } | |
325 | return r; | |
326 | } | |
327 | ||
8c0a7ce6 DD |
328 | static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) |
329 | { | |
330 | int ret; | |
331 | ||
332 | switch (attr->attr) { | |
333 | case KVM_S390_VM_MEM_LIMIT_SIZE: | |
334 | ret = 0; | |
335 | if (put_user(kvm->arch.gmap->asce_end, (u64 __user *)attr->addr)) | |
336 | ret = -EFAULT; | |
337 | break; | |
338 | default: | |
339 | ret = -ENXIO; | |
340 | break; | |
341 | } | |
342 | return ret; | |
343 | } | |
344 | ||
345 | static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) | |
4f718eab DD |
346 | { |
347 | int ret; | |
348 | unsigned int idx; | |
349 | switch (attr->attr) { | |
350 | case KVM_S390_VM_MEM_ENABLE_CMMA: | |
e6db1d61 DD |
351 | /* enable CMMA only for z10 and later (EDAT_1) */ |
352 | ret = -EINVAL; | |
353 | if (!MACHINE_IS_LPAR || !MACHINE_HAS_EDAT1) | |
354 | break; | |
355 | ||
4f718eab DD |
356 | ret = -EBUSY; |
357 | mutex_lock(&kvm->lock); | |
358 | if (atomic_read(&kvm->online_vcpus) == 0) { | |
359 | kvm->arch.use_cmma = 1; | |
360 | ret = 0; | |
361 | } | |
362 | mutex_unlock(&kvm->lock); | |
363 | break; | |
364 | case KVM_S390_VM_MEM_CLR_CMMA: | |
c3489155 DD |
365 | ret = -EINVAL; |
366 | if (!kvm->arch.use_cmma) | |
367 | break; | |
368 | ||
4f718eab DD |
369 | mutex_lock(&kvm->lock); |
370 | idx = srcu_read_lock(&kvm->srcu); | |
a13cff31 | 371 | s390_reset_cmma(kvm->arch.gmap->mm); |
4f718eab DD |
372 | srcu_read_unlock(&kvm->srcu, idx); |
373 | mutex_unlock(&kvm->lock); | |
374 | ret = 0; | |
375 | break; | |
8c0a7ce6 DD |
376 | case KVM_S390_VM_MEM_LIMIT_SIZE: { |
377 | unsigned long new_limit; | |
378 | ||
379 | if (kvm_is_ucontrol(kvm)) | |
380 | return -EINVAL; | |
381 | ||
382 | if (get_user(new_limit, (u64 __user *)attr->addr)) | |
383 | return -EFAULT; | |
384 | ||
385 | if (new_limit > kvm->arch.gmap->asce_end) | |
386 | return -E2BIG; | |
387 | ||
388 | ret = -EBUSY; | |
389 | mutex_lock(&kvm->lock); | |
390 | if (atomic_read(&kvm->online_vcpus) == 0) { | |
391 | /* gmap_alloc will round the limit up */ | |
392 | struct gmap *new = gmap_alloc(current->mm, new_limit); | |
393 | ||
394 | if (!new) { | |
395 | ret = -ENOMEM; | |
396 | } else { | |
397 | gmap_free(kvm->arch.gmap); | |
398 | new->private = kvm; | |
399 | kvm->arch.gmap = new; | |
400 | ret = 0; | |
401 | } | |
402 | } | |
403 | mutex_unlock(&kvm->lock); | |
404 | break; | |
405 | } | |
4f718eab DD |
406 | default: |
407 | ret = -ENXIO; | |
408 | break; | |
409 | } | |
410 | return ret; | |
411 | } | |
412 | ||
a374e892 TK |
413 | static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); |
414 | ||
415 | static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) | |
416 | { | |
417 | struct kvm_vcpu *vcpu; | |
418 | int i; | |
419 | ||
9d8d5786 | 420 | if (!test_kvm_facility(kvm, 76)) |
a374e892 TK |
421 | return -EINVAL; |
422 | ||
423 | mutex_lock(&kvm->lock); | |
424 | switch (attr->attr) { | |
425 | case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: | |
426 | get_random_bytes( | |
427 | kvm->arch.crypto.crycb->aes_wrapping_key_mask, | |
428 | sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); | |
429 | kvm->arch.crypto.aes_kw = 1; | |
430 | break; | |
431 | case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: | |
432 | get_random_bytes( | |
433 | kvm->arch.crypto.crycb->dea_wrapping_key_mask, | |
434 | sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); | |
435 | kvm->arch.crypto.dea_kw = 1; | |
436 | break; | |
437 | case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: | |
438 | kvm->arch.crypto.aes_kw = 0; | |
439 | memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, | |
440 | sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); | |
441 | break; | |
442 | case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: | |
443 | kvm->arch.crypto.dea_kw = 0; | |
444 | memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, | |
445 | sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); | |
446 | break; | |
447 | default: | |
448 | mutex_unlock(&kvm->lock); | |
449 | return -ENXIO; | |
450 | } | |
451 | ||
452 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
453 | kvm_s390_vcpu_crypto_setup(vcpu); | |
454 | exit_sie(vcpu); | |
455 | } | |
456 | mutex_unlock(&kvm->lock); | |
457 | return 0; | |
458 | } | |
459 | ||
72f25020 JH |
460 | static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) |
461 | { | |
462 | u8 gtod_high; | |
463 | ||
464 | if (copy_from_user(>od_high, (void __user *)attr->addr, | |
465 | sizeof(gtod_high))) | |
466 | return -EFAULT; | |
467 | ||
468 | if (gtod_high != 0) | |
469 | return -EINVAL; | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
474 | static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) | |
475 | { | |
476 | struct kvm_vcpu *cur_vcpu; | |
477 | unsigned int vcpu_idx; | |
478 | u64 host_tod, gtod; | |
479 | int r; | |
480 | ||
481 | if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) | |
482 | return -EFAULT; | |
483 | ||
484 | r = store_tod_clock(&host_tod); | |
485 | if (r) | |
486 | return r; | |
487 | ||
488 | mutex_lock(&kvm->lock); | |
489 | kvm->arch.epoch = gtod - host_tod; | |
27406cd5 CB |
490 | kvm_s390_vcpu_block_all(kvm); |
491 | kvm_for_each_vcpu(vcpu_idx, cur_vcpu, kvm) | |
72f25020 | 492 | cur_vcpu->arch.sie_block->epoch = kvm->arch.epoch; |
27406cd5 | 493 | kvm_s390_vcpu_unblock_all(kvm); |
72f25020 JH |
494 | mutex_unlock(&kvm->lock); |
495 | return 0; | |
496 | } | |
497 | ||
498 | static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) | |
499 | { | |
500 | int ret; | |
501 | ||
502 | if (attr->flags) | |
503 | return -EINVAL; | |
504 | ||
505 | switch (attr->attr) { | |
506 | case KVM_S390_VM_TOD_HIGH: | |
507 | ret = kvm_s390_set_tod_high(kvm, attr); | |
508 | break; | |
509 | case KVM_S390_VM_TOD_LOW: | |
510 | ret = kvm_s390_set_tod_low(kvm, attr); | |
511 | break; | |
512 | default: | |
513 | ret = -ENXIO; | |
514 | break; | |
515 | } | |
516 | return ret; | |
517 | } | |
518 | ||
519 | static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) | |
520 | { | |
521 | u8 gtod_high = 0; | |
522 | ||
523 | if (copy_to_user((void __user *)attr->addr, >od_high, | |
524 | sizeof(gtod_high))) | |
525 | return -EFAULT; | |
526 | ||
527 | return 0; | |
528 | } | |
529 | ||
530 | static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) | |
531 | { | |
532 | u64 host_tod, gtod; | |
533 | int r; | |
534 | ||
535 | r = store_tod_clock(&host_tod); | |
536 | if (r) | |
537 | return r; | |
538 | ||
539 | gtod = host_tod + kvm->arch.epoch; | |
540 | if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) | |
541 | return -EFAULT; | |
542 | ||
543 | return 0; | |
544 | } | |
545 | ||
546 | static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) | |
547 | { | |
548 | int ret; | |
549 | ||
550 | if (attr->flags) | |
551 | return -EINVAL; | |
552 | ||
553 | switch (attr->attr) { | |
554 | case KVM_S390_VM_TOD_HIGH: | |
555 | ret = kvm_s390_get_tod_high(kvm, attr); | |
556 | break; | |
557 | case KVM_S390_VM_TOD_LOW: | |
558 | ret = kvm_s390_get_tod_low(kvm, attr); | |
559 | break; | |
560 | default: | |
561 | ret = -ENXIO; | |
562 | break; | |
563 | } | |
564 | return ret; | |
565 | } | |
566 | ||
658b6eda MM |
567 | static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) |
568 | { | |
569 | struct kvm_s390_vm_cpu_processor *proc; | |
570 | int ret = 0; | |
571 | ||
572 | mutex_lock(&kvm->lock); | |
573 | if (atomic_read(&kvm->online_vcpus)) { | |
574 | ret = -EBUSY; | |
575 | goto out; | |
576 | } | |
577 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); | |
578 | if (!proc) { | |
579 | ret = -ENOMEM; | |
580 | goto out; | |
581 | } | |
582 | if (!copy_from_user(proc, (void __user *)attr->addr, | |
583 | sizeof(*proc))) { | |
584 | memcpy(&kvm->arch.model.cpu_id, &proc->cpuid, | |
585 | sizeof(struct cpuid)); | |
586 | kvm->arch.model.ibc = proc->ibc; | |
981467c9 | 587 | memcpy(kvm->arch.model.fac->list, proc->fac_list, |
658b6eda MM |
588 | S390_ARCH_FAC_LIST_SIZE_BYTE); |
589 | } else | |
590 | ret = -EFAULT; | |
591 | kfree(proc); | |
592 | out: | |
593 | mutex_unlock(&kvm->lock); | |
594 | return ret; | |
595 | } | |
596 | ||
597 | static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) | |
598 | { | |
599 | int ret = -ENXIO; | |
600 | ||
601 | switch (attr->attr) { | |
602 | case KVM_S390_VM_CPU_PROCESSOR: | |
603 | ret = kvm_s390_set_processor(kvm, attr); | |
604 | break; | |
605 | } | |
606 | return ret; | |
607 | } | |
608 | ||
609 | static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) | |
610 | { | |
611 | struct kvm_s390_vm_cpu_processor *proc; | |
612 | int ret = 0; | |
613 | ||
614 | proc = kzalloc(sizeof(*proc), GFP_KERNEL); | |
615 | if (!proc) { | |
616 | ret = -ENOMEM; | |
617 | goto out; | |
618 | } | |
619 | memcpy(&proc->cpuid, &kvm->arch.model.cpu_id, sizeof(struct cpuid)); | |
620 | proc->ibc = kvm->arch.model.ibc; | |
981467c9 | 621 | memcpy(&proc->fac_list, kvm->arch.model.fac->list, S390_ARCH_FAC_LIST_SIZE_BYTE); |
658b6eda MM |
622 | if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) |
623 | ret = -EFAULT; | |
624 | kfree(proc); | |
625 | out: | |
626 | return ret; | |
627 | } | |
628 | ||
629 | static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) | |
630 | { | |
631 | struct kvm_s390_vm_cpu_machine *mach; | |
632 | int ret = 0; | |
633 | ||
634 | mach = kzalloc(sizeof(*mach), GFP_KERNEL); | |
635 | if (!mach) { | |
636 | ret = -ENOMEM; | |
637 | goto out; | |
638 | } | |
639 | get_cpu_id((struct cpuid *) &mach->cpuid); | |
37c5f6c8 | 640 | mach->ibc = sclp.ibc; |
981467c9 MM |
641 | memcpy(&mach->fac_mask, kvm->arch.model.fac->mask, |
642 | S390_ARCH_FAC_LIST_SIZE_BYTE); | |
658b6eda | 643 | memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list, |
94422ee8 | 644 | S390_ARCH_FAC_LIST_SIZE_BYTE); |
658b6eda MM |
645 | if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) |
646 | ret = -EFAULT; | |
647 | kfree(mach); | |
648 | out: | |
649 | return ret; | |
650 | } | |
651 | ||
652 | static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) | |
653 | { | |
654 | int ret = -ENXIO; | |
655 | ||
656 | switch (attr->attr) { | |
657 | case KVM_S390_VM_CPU_PROCESSOR: | |
658 | ret = kvm_s390_get_processor(kvm, attr); | |
659 | break; | |
660 | case KVM_S390_VM_CPU_MACHINE: | |
661 | ret = kvm_s390_get_machine(kvm, attr); | |
662 | break; | |
663 | } | |
664 | return ret; | |
665 | } | |
666 | ||
f2061656 DD |
667 | static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) |
668 | { | |
669 | int ret; | |
670 | ||
671 | switch (attr->group) { | |
4f718eab | 672 | case KVM_S390_VM_MEM_CTRL: |
8c0a7ce6 | 673 | ret = kvm_s390_set_mem_control(kvm, attr); |
4f718eab | 674 | break; |
72f25020 JH |
675 | case KVM_S390_VM_TOD: |
676 | ret = kvm_s390_set_tod(kvm, attr); | |
677 | break; | |
658b6eda MM |
678 | case KVM_S390_VM_CPU_MODEL: |
679 | ret = kvm_s390_set_cpu_model(kvm, attr); | |
680 | break; | |
a374e892 TK |
681 | case KVM_S390_VM_CRYPTO: |
682 | ret = kvm_s390_vm_set_crypto(kvm, attr); | |
683 | break; | |
f2061656 DD |
684 | default: |
685 | ret = -ENXIO; | |
686 | break; | |
687 | } | |
688 | ||
689 | return ret; | |
690 | } | |
691 | ||
692 | static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) | |
693 | { | |
8c0a7ce6 DD |
694 | int ret; |
695 | ||
696 | switch (attr->group) { | |
697 | case KVM_S390_VM_MEM_CTRL: | |
698 | ret = kvm_s390_get_mem_control(kvm, attr); | |
699 | break; | |
72f25020 JH |
700 | case KVM_S390_VM_TOD: |
701 | ret = kvm_s390_get_tod(kvm, attr); | |
702 | break; | |
658b6eda MM |
703 | case KVM_S390_VM_CPU_MODEL: |
704 | ret = kvm_s390_get_cpu_model(kvm, attr); | |
705 | break; | |
8c0a7ce6 DD |
706 | default: |
707 | ret = -ENXIO; | |
708 | break; | |
709 | } | |
710 | ||
711 | return ret; | |
f2061656 DD |
712 | } |
713 | ||
714 | static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) | |
715 | { | |
716 | int ret; | |
717 | ||
718 | switch (attr->group) { | |
4f718eab DD |
719 | case KVM_S390_VM_MEM_CTRL: |
720 | switch (attr->attr) { | |
721 | case KVM_S390_VM_MEM_ENABLE_CMMA: | |
722 | case KVM_S390_VM_MEM_CLR_CMMA: | |
8c0a7ce6 | 723 | case KVM_S390_VM_MEM_LIMIT_SIZE: |
4f718eab DD |
724 | ret = 0; |
725 | break; | |
726 | default: | |
727 | ret = -ENXIO; | |
728 | break; | |
729 | } | |
730 | break; | |
72f25020 JH |
731 | case KVM_S390_VM_TOD: |
732 | switch (attr->attr) { | |
733 | case KVM_S390_VM_TOD_LOW: | |
734 | case KVM_S390_VM_TOD_HIGH: | |
735 | ret = 0; | |
736 | break; | |
737 | default: | |
738 | ret = -ENXIO; | |
739 | break; | |
740 | } | |
741 | break; | |
658b6eda MM |
742 | case KVM_S390_VM_CPU_MODEL: |
743 | switch (attr->attr) { | |
744 | case KVM_S390_VM_CPU_PROCESSOR: | |
745 | case KVM_S390_VM_CPU_MACHINE: | |
746 | ret = 0; | |
747 | break; | |
748 | default: | |
749 | ret = -ENXIO; | |
750 | break; | |
751 | } | |
752 | break; | |
a374e892 TK |
753 | case KVM_S390_VM_CRYPTO: |
754 | switch (attr->attr) { | |
755 | case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: | |
756 | case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: | |
757 | case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: | |
758 | case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: | |
759 | ret = 0; | |
760 | break; | |
761 | default: | |
762 | ret = -ENXIO; | |
763 | break; | |
764 | } | |
765 | break; | |
f2061656 DD |
766 | default: |
767 | ret = -ENXIO; | |
768 | break; | |
769 | } | |
770 | ||
771 | return ret; | |
772 | } | |
773 | ||
30ee2a98 JH |
774 | static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) |
775 | { | |
776 | uint8_t *keys; | |
777 | uint64_t hva; | |
778 | unsigned long curkey; | |
779 | int i, r = 0; | |
780 | ||
781 | if (args->flags != 0) | |
782 | return -EINVAL; | |
783 | ||
784 | /* Is this guest using storage keys? */ | |
785 | if (!mm_use_skey(current->mm)) | |
786 | return KVM_S390_GET_SKEYS_NONE; | |
787 | ||
788 | /* Enforce sane limit on memory allocation */ | |
789 | if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) | |
790 | return -EINVAL; | |
791 | ||
792 | keys = kmalloc_array(args->count, sizeof(uint8_t), | |
793 | GFP_KERNEL | __GFP_NOWARN); | |
794 | if (!keys) | |
795 | keys = vmalloc(sizeof(uint8_t) * args->count); | |
796 | if (!keys) | |
797 | return -ENOMEM; | |
798 | ||
799 | for (i = 0; i < args->count; i++) { | |
800 | hva = gfn_to_hva(kvm, args->start_gfn + i); | |
801 | if (kvm_is_error_hva(hva)) { | |
802 | r = -EFAULT; | |
803 | goto out; | |
804 | } | |
805 | ||
806 | curkey = get_guest_storage_key(current->mm, hva); | |
807 | if (IS_ERR_VALUE(curkey)) { | |
808 | r = curkey; | |
809 | goto out; | |
810 | } | |
811 | keys[i] = curkey; | |
812 | } | |
813 | ||
814 | r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, | |
815 | sizeof(uint8_t) * args->count); | |
816 | if (r) | |
817 | r = -EFAULT; | |
818 | out: | |
819 | kvfree(keys); | |
820 | return r; | |
821 | } | |
822 | ||
823 | static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) | |
824 | { | |
825 | uint8_t *keys; | |
826 | uint64_t hva; | |
827 | int i, r = 0; | |
828 | ||
829 | if (args->flags != 0) | |
830 | return -EINVAL; | |
831 | ||
832 | /* Enforce sane limit on memory allocation */ | |
833 | if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) | |
834 | return -EINVAL; | |
835 | ||
836 | keys = kmalloc_array(args->count, sizeof(uint8_t), | |
837 | GFP_KERNEL | __GFP_NOWARN); | |
838 | if (!keys) | |
839 | keys = vmalloc(sizeof(uint8_t) * args->count); | |
840 | if (!keys) | |
841 | return -ENOMEM; | |
842 | ||
843 | r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, | |
844 | sizeof(uint8_t) * args->count); | |
845 | if (r) { | |
846 | r = -EFAULT; | |
847 | goto out; | |
848 | } | |
849 | ||
850 | /* Enable storage key handling for the guest */ | |
14d4a425 DD |
851 | r = s390_enable_skey(); |
852 | if (r) | |
853 | goto out; | |
30ee2a98 JH |
854 | |
855 | for (i = 0; i < args->count; i++) { | |
856 | hva = gfn_to_hva(kvm, args->start_gfn + i); | |
857 | if (kvm_is_error_hva(hva)) { | |
858 | r = -EFAULT; | |
859 | goto out; | |
860 | } | |
861 | ||
862 | /* Lowest order bit is reserved */ | |
863 | if (keys[i] & 0x01) { | |
864 | r = -EINVAL; | |
865 | goto out; | |
866 | } | |
867 | ||
868 | r = set_guest_storage_key(current->mm, hva, | |
869 | (unsigned long)keys[i], 0); | |
870 | if (r) | |
871 | goto out; | |
872 | } | |
873 | out: | |
874 | kvfree(keys); | |
875 | return r; | |
876 | } | |
877 | ||
b0c632db HC |
878 | long kvm_arch_vm_ioctl(struct file *filp, |
879 | unsigned int ioctl, unsigned long arg) | |
880 | { | |
881 | struct kvm *kvm = filp->private_data; | |
882 | void __user *argp = (void __user *)arg; | |
f2061656 | 883 | struct kvm_device_attr attr; |
b0c632db HC |
884 | int r; |
885 | ||
886 | switch (ioctl) { | |
ba5c1e9b CO |
887 | case KVM_S390_INTERRUPT: { |
888 | struct kvm_s390_interrupt s390int; | |
889 | ||
890 | r = -EFAULT; | |
891 | if (copy_from_user(&s390int, argp, sizeof(s390int))) | |
892 | break; | |
893 | r = kvm_s390_inject_vm(kvm, &s390int); | |
894 | break; | |
895 | } | |
d938dc55 CH |
896 | case KVM_ENABLE_CAP: { |
897 | struct kvm_enable_cap cap; | |
898 | r = -EFAULT; | |
899 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
900 | break; | |
901 | r = kvm_vm_ioctl_enable_cap(kvm, &cap); | |
902 | break; | |
903 | } | |
84223598 CH |
904 | case KVM_CREATE_IRQCHIP: { |
905 | struct kvm_irq_routing_entry routing; | |
906 | ||
907 | r = -EINVAL; | |
908 | if (kvm->arch.use_irqchip) { | |
909 | /* Set up dummy routing. */ | |
910 | memset(&routing, 0, sizeof(routing)); | |
911 | kvm_set_irq_routing(kvm, &routing, 0, 0); | |
912 | r = 0; | |
913 | } | |
914 | break; | |
915 | } | |
f2061656 DD |
916 | case KVM_SET_DEVICE_ATTR: { |
917 | r = -EFAULT; | |
918 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) | |
919 | break; | |
920 | r = kvm_s390_vm_set_attr(kvm, &attr); | |
921 | break; | |
922 | } | |
923 | case KVM_GET_DEVICE_ATTR: { | |
924 | r = -EFAULT; | |
925 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) | |
926 | break; | |
927 | r = kvm_s390_vm_get_attr(kvm, &attr); | |
928 | break; | |
929 | } | |
930 | case KVM_HAS_DEVICE_ATTR: { | |
931 | r = -EFAULT; | |
932 | if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) | |
933 | break; | |
934 | r = kvm_s390_vm_has_attr(kvm, &attr); | |
935 | break; | |
936 | } | |
30ee2a98 JH |
937 | case KVM_S390_GET_SKEYS: { |
938 | struct kvm_s390_skeys args; | |
939 | ||
940 | r = -EFAULT; | |
941 | if (copy_from_user(&args, argp, | |
942 | sizeof(struct kvm_s390_skeys))) | |
943 | break; | |
944 | r = kvm_s390_get_skeys(kvm, &args); | |
945 | break; | |
946 | } | |
947 | case KVM_S390_SET_SKEYS: { | |
948 | struct kvm_s390_skeys args; | |
949 | ||
950 | r = -EFAULT; | |
951 | if (copy_from_user(&args, argp, | |
952 | sizeof(struct kvm_s390_skeys))) | |
953 | break; | |
954 | r = kvm_s390_set_skeys(kvm, &args); | |
955 | break; | |
956 | } | |
b0c632db | 957 | default: |
367e1319 | 958 | r = -ENOTTY; |
b0c632db HC |
959 | } |
960 | ||
961 | return r; | |
962 | } | |
963 | ||
45c9b47c TK |
964 | static int kvm_s390_query_ap_config(u8 *config) |
965 | { | |
966 | u32 fcn_code = 0x04000000UL; | |
86044c8c | 967 | u32 cc = 0; |
45c9b47c | 968 | |
86044c8c | 969 | memset(config, 0, 128); |
45c9b47c TK |
970 | asm volatile( |
971 | "lgr 0,%1\n" | |
972 | "lgr 2,%2\n" | |
973 | ".long 0xb2af0000\n" /* PQAP(QCI) */ | |
86044c8c | 974 | "0: ipm %0\n" |
45c9b47c | 975 | "srl %0,28\n" |
86044c8c CB |
976 | "1:\n" |
977 | EX_TABLE(0b, 1b) | |
978 | : "+r" (cc) | |
45c9b47c TK |
979 | : "r" (fcn_code), "r" (config) |
980 | : "cc", "0", "2", "memory" | |
981 | ); | |
982 | ||
983 | return cc; | |
984 | } | |
985 | ||
986 | static int kvm_s390_apxa_installed(void) | |
987 | { | |
988 | u8 config[128]; | |
989 | int cc; | |
990 | ||
991 | if (test_facility(2) && test_facility(12)) { | |
992 | cc = kvm_s390_query_ap_config(config); | |
993 | ||
994 | if (cc) | |
995 | pr_err("PQAP(QCI) failed with cc=%d", cc); | |
996 | else | |
997 | return config[0] & 0x40; | |
998 | } | |
999 | ||
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | static void kvm_s390_set_crycb_format(struct kvm *kvm) | |
1004 | { | |
1005 | kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; | |
1006 | ||
1007 | if (kvm_s390_apxa_installed()) | |
1008 | kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; | |
1009 | else | |
1010 | kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; | |
1011 | } | |
1012 | ||
9d8d5786 MM |
1013 | static void kvm_s390_get_cpu_id(struct cpuid *cpu_id) |
1014 | { | |
1015 | get_cpu_id(cpu_id); | |
1016 | cpu_id->version = 0xff; | |
1017 | } | |
1018 | ||
5102ee87 TK |
1019 | static int kvm_s390_crypto_init(struct kvm *kvm) |
1020 | { | |
9d8d5786 | 1021 | if (!test_kvm_facility(kvm, 76)) |
5102ee87 TK |
1022 | return 0; |
1023 | ||
1024 | kvm->arch.crypto.crycb = kzalloc(sizeof(*kvm->arch.crypto.crycb), | |
1025 | GFP_KERNEL | GFP_DMA); | |
1026 | if (!kvm->arch.crypto.crycb) | |
1027 | return -ENOMEM; | |
1028 | ||
45c9b47c | 1029 | kvm_s390_set_crycb_format(kvm); |
5102ee87 | 1030 | |
ed6f76b4 TK |
1031 | /* Enable AES/DEA protected key functions by default */ |
1032 | kvm->arch.crypto.aes_kw = 1; | |
1033 | kvm->arch.crypto.dea_kw = 1; | |
1034 | get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, | |
1035 | sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); | |
1036 | get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, | |
1037 | sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); | |
a374e892 | 1038 | |
5102ee87 TK |
1039 | return 0; |
1040 | } | |
1041 | ||
e08b9637 | 1042 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
b0c632db | 1043 | { |
9d8d5786 | 1044 | int i, rc; |
b0c632db | 1045 | char debug_name[16]; |
f6c137ff | 1046 | static unsigned long sca_offset; |
b0c632db | 1047 | |
e08b9637 CO |
1048 | rc = -EINVAL; |
1049 | #ifdef CONFIG_KVM_S390_UCONTROL | |
1050 | if (type & ~KVM_VM_S390_UCONTROL) | |
1051 | goto out_err; | |
1052 | if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) | |
1053 | goto out_err; | |
1054 | #else | |
1055 | if (type) | |
1056 | goto out_err; | |
1057 | #endif | |
1058 | ||
b0c632db HC |
1059 | rc = s390_enable_sie(); |
1060 | if (rc) | |
d89f5eff | 1061 | goto out_err; |
b0c632db | 1062 | |
b290411a CO |
1063 | rc = -ENOMEM; |
1064 | ||
b0c632db HC |
1065 | kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL); |
1066 | if (!kvm->arch.sca) | |
d89f5eff | 1067 | goto out_err; |
f6c137ff CB |
1068 | spin_lock(&kvm_lock); |
1069 | sca_offset = (sca_offset + 16) & 0x7f0; | |
1070 | kvm->arch.sca = (struct sca_block *) ((char *) kvm->arch.sca + sca_offset); | |
1071 | spin_unlock(&kvm_lock); | |
b0c632db HC |
1072 | |
1073 | sprintf(debug_name, "kvm-%u", current->pid); | |
1074 | ||
1cb9cf72 | 1075 | kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); |
b0c632db | 1076 | if (!kvm->arch.dbf) |
40f5b735 | 1077 | goto out_err; |
b0c632db | 1078 | |
9d8d5786 MM |
1079 | /* |
1080 | * The architectural maximum amount of facilities is 16 kbit. To store | |
1081 | * this amount, 2 kbyte of memory is required. Thus we need a full | |
981467c9 MM |
1082 | * page to hold the guest facility list (arch.model.fac->list) and the |
1083 | * facility mask (arch.model.fac->mask). Its address size has to be | |
9d8d5786 MM |
1084 | * 31 bits and word aligned. |
1085 | */ | |
1086 | kvm->arch.model.fac = | |
981467c9 | 1087 | (struct kvm_s390_fac *) get_zeroed_page(GFP_KERNEL | GFP_DMA); |
9d8d5786 | 1088 | if (!kvm->arch.model.fac) |
40f5b735 | 1089 | goto out_err; |
9d8d5786 | 1090 | |
fb5bf93f | 1091 | /* Populate the facility mask initially. */ |
981467c9 | 1092 | memcpy(kvm->arch.model.fac->mask, S390_lowcore.stfle_fac_list, |
94422ee8 | 1093 | S390_ARCH_FAC_LIST_SIZE_BYTE); |
9d8d5786 MM |
1094 | for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) { |
1095 | if (i < kvm_s390_fac_list_mask_size()) | |
981467c9 | 1096 | kvm->arch.model.fac->mask[i] &= kvm_s390_fac_list_mask[i]; |
9d8d5786 | 1097 | else |
981467c9 | 1098 | kvm->arch.model.fac->mask[i] = 0UL; |
9d8d5786 MM |
1099 | } |
1100 | ||
981467c9 MM |
1101 | /* Populate the facility list initially. */ |
1102 | memcpy(kvm->arch.model.fac->list, kvm->arch.model.fac->mask, | |
1103 | S390_ARCH_FAC_LIST_SIZE_BYTE); | |
1104 | ||
9d8d5786 | 1105 | kvm_s390_get_cpu_id(&kvm->arch.model.cpu_id); |
37c5f6c8 | 1106 | kvm->arch.model.ibc = sclp.ibc & 0x0fff; |
9d8d5786 | 1107 | |
5102ee87 | 1108 | if (kvm_s390_crypto_init(kvm) < 0) |
40f5b735 | 1109 | goto out_err; |
5102ee87 | 1110 | |
ba5c1e9b | 1111 | spin_lock_init(&kvm->arch.float_int.lock); |
6d3da241 JF |
1112 | for (i = 0; i < FIRQ_LIST_COUNT; i++) |
1113 | INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); | |
8a242234 | 1114 | init_waitqueue_head(&kvm->arch.ipte_wq); |
a6b7e459 | 1115 | mutex_init(&kvm->arch.ipte_mutex); |
ba5c1e9b | 1116 | |
b0c632db | 1117 | debug_register_view(kvm->arch.dbf, &debug_sprintf_view); |
78f26131 | 1118 | VM_EVENT(kvm, 3, "vm created with type %lu", type); |
b0c632db | 1119 | |
e08b9637 CO |
1120 | if (type & KVM_VM_S390_UCONTROL) { |
1121 | kvm->arch.gmap = NULL; | |
1122 | } else { | |
0349985a | 1123 | kvm->arch.gmap = gmap_alloc(current->mm, (1UL << 44) - 1); |
e08b9637 | 1124 | if (!kvm->arch.gmap) |
40f5b735 | 1125 | goto out_err; |
2c70fe44 | 1126 | kvm->arch.gmap->private = kvm; |
24eb3a82 | 1127 | kvm->arch.gmap->pfault_enabled = 0; |
e08b9637 | 1128 | } |
fa6b7fe9 CH |
1129 | |
1130 | kvm->arch.css_support = 0; | |
84223598 | 1131 | kvm->arch.use_irqchip = 0; |
72f25020 | 1132 | kvm->arch.epoch = 0; |
fa6b7fe9 | 1133 | |
8ad35755 | 1134 | spin_lock_init(&kvm->arch.start_stop_lock); |
78f26131 | 1135 | KVM_EVENT(3, "vm 0x%p created by pid %u", kvm, current->pid); |
8ad35755 | 1136 | |
d89f5eff | 1137 | return 0; |
40f5b735 | 1138 | out_err: |
5102ee87 | 1139 | kfree(kvm->arch.crypto.crycb); |
9d8d5786 | 1140 | free_page((unsigned long)kvm->arch.model.fac); |
598841ca | 1141 | debug_unregister(kvm->arch.dbf); |
b0c632db | 1142 | free_page((unsigned long)(kvm->arch.sca)); |
78f26131 | 1143 | KVM_EVENT(3, "creation of vm failed: %d", rc); |
d89f5eff | 1144 | return rc; |
b0c632db HC |
1145 | } |
1146 | ||
d329c035 CB |
1147 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
1148 | { | |
1149 | VCPU_EVENT(vcpu, 3, "%s", "free cpu"); | |
ade38c31 | 1150 | trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); |
67335e63 | 1151 | kvm_s390_clear_local_irqs(vcpu); |
3c038e6b | 1152 | kvm_clear_async_pf_completion_queue(vcpu); |
58f9460b CO |
1153 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
1154 | clear_bit(63 - vcpu->vcpu_id, | |
1155 | (unsigned long *) &vcpu->kvm->arch.sca->mcn); | |
1156 | if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda == | |
1157 | (__u64) vcpu->arch.sie_block) | |
1158 | vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0; | |
1159 | } | |
abf4a71e | 1160 | smp_mb(); |
27e0393f CO |
1161 | |
1162 | if (kvm_is_ucontrol(vcpu->kvm)) | |
1163 | gmap_free(vcpu->arch.gmap); | |
1164 | ||
e6db1d61 | 1165 | if (vcpu->kvm->arch.use_cmma) |
b31605c1 | 1166 | kvm_s390_vcpu_unsetup_cmma(vcpu); |
d329c035 | 1167 | free_page((unsigned long)(vcpu->arch.sie_block)); |
b31288fa | 1168 | |
6692cef3 | 1169 | kvm_vcpu_uninit(vcpu); |
b110feaf | 1170 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
d329c035 CB |
1171 | } |
1172 | ||
1173 | static void kvm_free_vcpus(struct kvm *kvm) | |
1174 | { | |
1175 | unsigned int i; | |
988a2cae | 1176 | struct kvm_vcpu *vcpu; |
d329c035 | 1177 | |
988a2cae GN |
1178 | kvm_for_each_vcpu(i, vcpu, kvm) |
1179 | kvm_arch_vcpu_destroy(vcpu); | |
1180 | ||
1181 | mutex_lock(&kvm->lock); | |
1182 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) | |
1183 | kvm->vcpus[i] = NULL; | |
1184 | ||
1185 | atomic_set(&kvm->online_vcpus, 0); | |
1186 | mutex_unlock(&kvm->lock); | |
d329c035 CB |
1187 | } |
1188 | ||
b0c632db HC |
1189 | void kvm_arch_destroy_vm(struct kvm *kvm) |
1190 | { | |
d329c035 | 1191 | kvm_free_vcpus(kvm); |
9d8d5786 | 1192 | free_page((unsigned long)kvm->arch.model.fac); |
b0c632db | 1193 | free_page((unsigned long)(kvm->arch.sca)); |
d329c035 | 1194 | debug_unregister(kvm->arch.dbf); |
5102ee87 | 1195 | kfree(kvm->arch.crypto.crycb); |
27e0393f CO |
1196 | if (!kvm_is_ucontrol(kvm)) |
1197 | gmap_free(kvm->arch.gmap); | |
841b91c5 | 1198 | kvm_s390_destroy_adapters(kvm); |
67335e63 | 1199 | kvm_s390_clear_float_irqs(kvm); |
78f26131 | 1200 | KVM_EVENT(3, "vm 0x%p destroyed", kvm); |
b0c632db HC |
1201 | } |
1202 | ||
1203 | /* Section: vcpu related */ | |
dafd032a DD |
1204 | static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) |
1205 | { | |
1206 | vcpu->arch.gmap = gmap_alloc(current->mm, -1UL); | |
1207 | if (!vcpu->arch.gmap) | |
1208 | return -ENOMEM; | |
1209 | vcpu->arch.gmap->private = vcpu->kvm; | |
1210 | ||
1211 | return 0; | |
1212 | } | |
1213 | ||
b0c632db HC |
1214 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
1215 | { | |
3c038e6b DD |
1216 | vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; |
1217 | kvm_clear_async_pf_completion_queue(vcpu); | |
59674c1a CB |
1218 | vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | |
1219 | KVM_SYNC_GPRS | | |
9eed0735 | 1220 | KVM_SYNC_ACRS | |
b028ee3e DH |
1221 | KVM_SYNC_CRS | |
1222 | KVM_SYNC_ARCH0 | | |
1223 | KVM_SYNC_PFAULT; | |
68c55750 EF |
1224 | if (test_kvm_facility(vcpu->kvm, 129)) |
1225 | vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; | |
dafd032a DD |
1226 | |
1227 | if (kvm_is_ucontrol(vcpu->kvm)) | |
1228 | return __kvm_ucontrol_vcpu_init(vcpu); | |
1229 | ||
b0c632db HC |
1230 | return 0; |
1231 | } | |
1232 | ||
b0c632db HC |
1233 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
1234 | { | |
4725c860 | 1235 | save_fp_ctl(&vcpu->arch.host_fpregs.fpc); |
18280d8b | 1236 | if (test_kvm_facility(vcpu->kvm, 129)) |
68c55750 EF |
1237 | save_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs); |
1238 | else | |
1239 | save_fp_regs(vcpu->arch.host_fpregs.fprs); | |
b0c632db | 1240 | save_access_regs(vcpu->arch.host_acrs); |
18280d8b | 1241 | if (test_kvm_facility(vcpu->kvm, 129)) { |
68c55750 EF |
1242 | restore_fp_ctl(&vcpu->run->s.regs.fpc); |
1243 | restore_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs); | |
1244 | } else { | |
1245 | restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc); | |
1246 | restore_fp_regs(vcpu->arch.guest_fpregs.fprs); | |
1247 | } | |
59674c1a | 1248 | restore_access_regs(vcpu->run->s.regs.acrs); |
480e5926 | 1249 | gmap_enable(vcpu->arch.gmap); |
9e6dabef | 1250 | atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); |
b0c632db HC |
1251 | } |
1252 | ||
1253 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) | |
1254 | { | |
9e6dabef | 1255 | atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags); |
480e5926 | 1256 | gmap_disable(vcpu->arch.gmap); |
18280d8b | 1257 | if (test_kvm_facility(vcpu->kvm, 129)) { |
68c55750 EF |
1258 | save_fp_ctl(&vcpu->run->s.regs.fpc); |
1259 | save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs); | |
1260 | } else { | |
1261 | save_fp_ctl(&vcpu->arch.guest_fpregs.fpc); | |
1262 | save_fp_regs(vcpu->arch.guest_fpregs.fprs); | |
1263 | } | |
59674c1a | 1264 | save_access_regs(vcpu->run->s.regs.acrs); |
4725c860 | 1265 | restore_fp_ctl(&vcpu->arch.host_fpregs.fpc); |
18280d8b | 1266 | if (test_kvm_facility(vcpu->kvm, 129)) |
68c55750 EF |
1267 | restore_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs); |
1268 | else | |
1269 | restore_fp_regs(vcpu->arch.host_fpregs.fprs); | |
b0c632db HC |
1270 | restore_access_regs(vcpu->arch.host_acrs); |
1271 | } | |
1272 | ||
1273 | static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu) | |
1274 | { | |
1275 | /* this equals initial cpu reset in pop, but we don't switch to ESA */ | |
1276 | vcpu->arch.sie_block->gpsw.mask = 0UL; | |
1277 | vcpu->arch.sie_block->gpsw.addr = 0UL; | |
8d26cf7b | 1278 | kvm_s390_set_prefix(vcpu, 0); |
b0c632db HC |
1279 | vcpu->arch.sie_block->cputm = 0UL; |
1280 | vcpu->arch.sie_block->ckc = 0UL; | |
1281 | vcpu->arch.sie_block->todpr = 0; | |
1282 | memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64)); | |
1283 | vcpu->arch.sie_block->gcr[0] = 0xE0UL; | |
1284 | vcpu->arch.sie_block->gcr[14] = 0xC2000000UL; | |
1285 | vcpu->arch.guest_fpregs.fpc = 0; | |
1286 | asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc)); | |
1287 | vcpu->arch.sie_block->gbea = 1; | |
672550fb | 1288 | vcpu->arch.sie_block->pp = 0; |
3c038e6b DD |
1289 | vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; |
1290 | kvm_clear_async_pf_completion_queue(vcpu); | |
6352e4d2 DH |
1291 | if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) |
1292 | kvm_s390_vcpu_stop(vcpu); | |
2ed10cc1 | 1293 | kvm_s390_clear_local_irqs(vcpu); |
b0c632db HC |
1294 | } |
1295 | ||
31928aa5 | 1296 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
42897d86 | 1297 | { |
72f25020 JH |
1298 | mutex_lock(&vcpu->kvm->lock); |
1299 | vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; | |
1300 | mutex_unlock(&vcpu->kvm->lock); | |
dafd032a DD |
1301 | if (!kvm_is_ucontrol(vcpu->kvm)) |
1302 | vcpu->arch.gmap = vcpu->kvm->arch.gmap; | |
42897d86 MT |
1303 | } |
1304 | ||
5102ee87 TK |
1305 | static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) |
1306 | { | |
9d8d5786 | 1307 | if (!test_kvm_facility(vcpu->kvm, 76)) |
5102ee87 TK |
1308 | return; |
1309 | ||
a374e892 TK |
1310 | vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); |
1311 | ||
1312 | if (vcpu->kvm->arch.crypto.aes_kw) | |
1313 | vcpu->arch.sie_block->ecb3 |= ECB3_AES; | |
1314 | if (vcpu->kvm->arch.crypto.dea_kw) | |
1315 | vcpu->arch.sie_block->ecb3 |= ECB3_DEA; | |
1316 | ||
5102ee87 TK |
1317 | vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; |
1318 | } | |
1319 | ||
b31605c1 DD |
1320 | void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) |
1321 | { | |
1322 | free_page(vcpu->arch.sie_block->cbrlo); | |
1323 | vcpu->arch.sie_block->cbrlo = 0; | |
1324 | } | |
1325 | ||
1326 | int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) | |
1327 | { | |
1328 | vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL); | |
1329 | if (!vcpu->arch.sie_block->cbrlo) | |
1330 | return -ENOMEM; | |
1331 | ||
1332 | vcpu->arch.sie_block->ecb2 |= 0x80; | |
1333 | vcpu->arch.sie_block->ecb2 &= ~0x08; | |
1334 | return 0; | |
1335 | } | |
1336 | ||
91520f1a MM |
1337 | static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) |
1338 | { | |
1339 | struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; | |
1340 | ||
1341 | vcpu->arch.cpu_id = model->cpu_id; | |
1342 | vcpu->arch.sie_block->ibc = model->ibc; | |
1343 | vcpu->arch.sie_block->fac = (int) (long) model->fac->list; | |
1344 | } | |
1345 | ||
b0c632db HC |
1346 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
1347 | { | |
b31605c1 | 1348 | int rc = 0; |
b31288fa | 1349 | |
9e6dabef CH |
1350 | atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | |
1351 | CPUSTAT_SM | | |
a4a4f191 GH |
1352 | CPUSTAT_STOPPED); |
1353 | ||
53df84f8 GH |
1354 | if (test_kvm_facility(vcpu->kvm, 78)) |
1355 | atomic_set_mask(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags); | |
1356 | else if (test_kvm_facility(vcpu->kvm, 8)) | |
a4a4f191 GH |
1357 | atomic_set_mask(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags); |
1358 | ||
91520f1a MM |
1359 | kvm_s390_vcpu_setup_model(vcpu); |
1360 | ||
fc34531d | 1361 | vcpu->arch.sie_block->ecb = 6; |
9d8d5786 | 1362 | if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73)) |
7feb6bb8 MM |
1363 | vcpu->arch.sie_block->ecb |= 0x10; |
1364 | ||
69d0d3a3 | 1365 | vcpu->arch.sie_block->ecb2 = 8; |
ea5f4969 | 1366 | vcpu->arch.sie_block->eca = 0xC1002000U; |
37c5f6c8 | 1367 | if (sclp.has_siif) |
217a4406 | 1368 | vcpu->arch.sie_block->eca |= 1; |
37c5f6c8 | 1369 | if (sclp.has_sigpif) |
ea5f4969 | 1370 | vcpu->arch.sie_block->eca |= 0x10000000U; |
18280d8b | 1371 | if (test_kvm_facility(vcpu->kvm, 129)) { |
13211ea7 EF |
1372 | vcpu->arch.sie_block->eca |= 0x00020000; |
1373 | vcpu->arch.sie_block->ecd |= 0x20000000; | |
1374 | } | |
492d8642 | 1375 | vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; |
5a5e6536 | 1376 | |
e6db1d61 | 1377 | if (vcpu->kvm->arch.use_cmma) { |
b31605c1 DD |
1378 | rc = kvm_s390_vcpu_setup_cmma(vcpu); |
1379 | if (rc) | |
1380 | return rc; | |
b31288fa | 1381 | } |
0ac96caf | 1382 | hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
ca872302 | 1383 | vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; |
9d8d5786 | 1384 | |
5102ee87 TK |
1385 | kvm_s390_vcpu_crypto_setup(vcpu); |
1386 | ||
b31605c1 | 1387 | return rc; |
b0c632db HC |
1388 | } |
1389 | ||
1390 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, | |
1391 | unsigned int id) | |
1392 | { | |
4d47555a | 1393 | struct kvm_vcpu *vcpu; |
7feb6bb8 | 1394 | struct sie_page *sie_page; |
4d47555a CO |
1395 | int rc = -EINVAL; |
1396 | ||
1397 | if (id >= KVM_MAX_VCPUS) | |
1398 | goto out; | |
1399 | ||
1400 | rc = -ENOMEM; | |
b0c632db | 1401 | |
b110feaf | 1402 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
b0c632db | 1403 | if (!vcpu) |
4d47555a | 1404 | goto out; |
b0c632db | 1405 | |
7feb6bb8 MM |
1406 | sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); |
1407 | if (!sie_page) | |
b0c632db HC |
1408 | goto out_free_cpu; |
1409 | ||
7feb6bb8 MM |
1410 | vcpu->arch.sie_block = &sie_page->sie_block; |
1411 | vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb; | |
68c55750 | 1412 | vcpu->arch.host_vregs = &sie_page->vregs; |
7feb6bb8 | 1413 | |
b0c632db | 1414 | vcpu->arch.sie_block->icpua = id; |
58f9460b CO |
1415 | if (!kvm_is_ucontrol(kvm)) { |
1416 | if (!kvm->arch.sca) { | |
1417 | WARN_ON_ONCE(1); | |
1418 | goto out_free_cpu; | |
1419 | } | |
1420 | if (!kvm->arch.sca->cpu[id].sda) | |
1421 | kvm->arch.sca->cpu[id].sda = | |
1422 | (__u64) vcpu->arch.sie_block; | |
1423 | vcpu->arch.sie_block->scaoh = | |
1424 | (__u32)(((__u64)kvm->arch.sca) >> 32); | |
1425 | vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca; | |
1426 | set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn); | |
1427 | } | |
b0c632db | 1428 | |
ba5c1e9b | 1429 | spin_lock_init(&vcpu->arch.local_int.lock); |
ba5c1e9b | 1430 | vcpu->arch.local_int.float_int = &kvm->arch.float_int; |
d0321a24 | 1431 | vcpu->arch.local_int.wq = &vcpu->wq; |
5288fbf0 | 1432 | vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags; |
ba5c1e9b | 1433 | |
b0c632db HC |
1434 | rc = kvm_vcpu_init(vcpu, kvm, id); |
1435 | if (rc) | |
7b06bf2f | 1436 | goto out_free_sie_block; |
b0c632db HC |
1437 | VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu, |
1438 | vcpu->arch.sie_block); | |
ade38c31 | 1439 | trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block); |
b0c632db | 1440 | |
b0c632db | 1441 | return vcpu; |
7b06bf2f WY |
1442 | out_free_sie_block: |
1443 | free_page((unsigned long)(vcpu->arch.sie_block)); | |
b0c632db | 1444 | out_free_cpu: |
b110feaf | 1445 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
4d47555a | 1446 | out: |
b0c632db HC |
1447 | return ERR_PTR(rc); |
1448 | } | |
1449 | ||
b0c632db HC |
1450 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
1451 | { | |
9a022067 | 1452 | return kvm_s390_vcpu_has_irq(vcpu, 0); |
b0c632db HC |
1453 | } |
1454 | ||
27406cd5 | 1455 | void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) |
49b99e1e CB |
1456 | { |
1457 | atomic_set_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); | |
61a6df54 | 1458 | exit_sie(vcpu); |
49b99e1e CB |
1459 | } |
1460 | ||
27406cd5 | 1461 | void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) |
49b99e1e CB |
1462 | { |
1463 | atomic_clear_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); | |
1464 | } | |
1465 | ||
8e236546 CB |
1466 | static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) |
1467 | { | |
1468 | atomic_set_mask(PROG_REQUEST, &vcpu->arch.sie_block->prog20); | |
61a6df54 | 1469 | exit_sie(vcpu); |
8e236546 CB |
1470 | } |
1471 | ||
1472 | static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) | |
1473 | { | |
1474 | atomic_clear_mask(PROG_REQUEST, &vcpu->arch.sie_block->prog20); | |
1475 | } | |
1476 | ||
49b99e1e CB |
1477 | /* |
1478 | * Kick a guest cpu out of SIE and wait until SIE is not running. | |
1479 | * If the CPU is not running (e.g. waiting as idle) the function will | |
1480 | * return immediately. */ | |
1481 | void exit_sie(struct kvm_vcpu *vcpu) | |
1482 | { | |
1483 | atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags); | |
1484 | while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) | |
1485 | cpu_relax(); | |
1486 | } | |
1487 | ||
8e236546 CB |
1488 | /* Kick a guest cpu out of SIE to process a request synchronously */ |
1489 | void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) | |
49b99e1e | 1490 | { |
8e236546 CB |
1491 | kvm_make_request(req, vcpu); |
1492 | kvm_s390_vcpu_request(vcpu); | |
49b99e1e CB |
1493 | } |
1494 | ||
2c70fe44 CB |
1495 | static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address) |
1496 | { | |
1497 | int i; | |
1498 | struct kvm *kvm = gmap->private; | |
1499 | struct kvm_vcpu *vcpu; | |
1500 | ||
1501 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
1502 | /* match against both prefix pages */ | |
fda902cb | 1503 | if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) { |
2c70fe44 | 1504 | VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address); |
8e236546 | 1505 | kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu); |
2c70fe44 CB |
1506 | } |
1507 | } | |
1508 | } | |
1509 | ||
b6d33834 CD |
1510 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
1511 | { | |
1512 | /* kvm common code refers to this, but never calls it */ | |
1513 | BUG(); | |
1514 | return 0; | |
1515 | } | |
1516 | ||
14eebd91 CO |
1517 | static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, |
1518 | struct kvm_one_reg *reg) | |
1519 | { | |
1520 | int r = -EINVAL; | |
1521 | ||
1522 | switch (reg->id) { | |
29b7c71b CO |
1523 | case KVM_REG_S390_TODPR: |
1524 | r = put_user(vcpu->arch.sie_block->todpr, | |
1525 | (u32 __user *)reg->addr); | |
1526 | break; | |
1527 | case KVM_REG_S390_EPOCHDIFF: | |
1528 | r = put_user(vcpu->arch.sie_block->epoch, | |
1529 | (u64 __user *)reg->addr); | |
1530 | break; | |
46a6dd1c J |
1531 | case KVM_REG_S390_CPU_TIMER: |
1532 | r = put_user(vcpu->arch.sie_block->cputm, | |
1533 | (u64 __user *)reg->addr); | |
1534 | break; | |
1535 | case KVM_REG_S390_CLOCK_COMP: | |
1536 | r = put_user(vcpu->arch.sie_block->ckc, | |
1537 | (u64 __user *)reg->addr); | |
1538 | break; | |
536336c2 DD |
1539 | case KVM_REG_S390_PFTOKEN: |
1540 | r = put_user(vcpu->arch.pfault_token, | |
1541 | (u64 __user *)reg->addr); | |
1542 | break; | |
1543 | case KVM_REG_S390_PFCOMPARE: | |
1544 | r = put_user(vcpu->arch.pfault_compare, | |
1545 | (u64 __user *)reg->addr); | |
1546 | break; | |
1547 | case KVM_REG_S390_PFSELECT: | |
1548 | r = put_user(vcpu->arch.pfault_select, | |
1549 | (u64 __user *)reg->addr); | |
1550 | break; | |
672550fb CB |
1551 | case KVM_REG_S390_PP: |
1552 | r = put_user(vcpu->arch.sie_block->pp, | |
1553 | (u64 __user *)reg->addr); | |
1554 | break; | |
afa45ff5 CB |
1555 | case KVM_REG_S390_GBEA: |
1556 | r = put_user(vcpu->arch.sie_block->gbea, | |
1557 | (u64 __user *)reg->addr); | |
1558 | break; | |
14eebd91 CO |
1559 | default: |
1560 | break; | |
1561 | } | |
1562 | ||
1563 | return r; | |
1564 | } | |
1565 | ||
1566 | static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, | |
1567 | struct kvm_one_reg *reg) | |
1568 | { | |
1569 | int r = -EINVAL; | |
1570 | ||
1571 | switch (reg->id) { | |
29b7c71b CO |
1572 | case KVM_REG_S390_TODPR: |
1573 | r = get_user(vcpu->arch.sie_block->todpr, | |
1574 | (u32 __user *)reg->addr); | |
1575 | break; | |
1576 | case KVM_REG_S390_EPOCHDIFF: | |
1577 | r = get_user(vcpu->arch.sie_block->epoch, | |
1578 | (u64 __user *)reg->addr); | |
1579 | break; | |
46a6dd1c J |
1580 | case KVM_REG_S390_CPU_TIMER: |
1581 | r = get_user(vcpu->arch.sie_block->cputm, | |
1582 | (u64 __user *)reg->addr); | |
1583 | break; | |
1584 | case KVM_REG_S390_CLOCK_COMP: | |
1585 | r = get_user(vcpu->arch.sie_block->ckc, | |
1586 | (u64 __user *)reg->addr); | |
1587 | break; | |
536336c2 DD |
1588 | case KVM_REG_S390_PFTOKEN: |
1589 | r = get_user(vcpu->arch.pfault_token, | |
1590 | (u64 __user *)reg->addr); | |
9fbd8082 DH |
1591 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) |
1592 | kvm_clear_async_pf_completion_queue(vcpu); | |
536336c2 DD |
1593 | break; |
1594 | case KVM_REG_S390_PFCOMPARE: | |
1595 | r = get_user(vcpu->arch.pfault_compare, | |
1596 | (u64 __user *)reg->addr); | |
1597 | break; | |
1598 | case KVM_REG_S390_PFSELECT: | |
1599 | r = get_user(vcpu->arch.pfault_select, | |
1600 | (u64 __user *)reg->addr); | |
1601 | break; | |
672550fb CB |
1602 | case KVM_REG_S390_PP: |
1603 | r = get_user(vcpu->arch.sie_block->pp, | |
1604 | (u64 __user *)reg->addr); | |
1605 | break; | |
afa45ff5 CB |
1606 | case KVM_REG_S390_GBEA: |
1607 | r = get_user(vcpu->arch.sie_block->gbea, | |
1608 | (u64 __user *)reg->addr); | |
1609 | break; | |
14eebd91 CO |
1610 | default: |
1611 | break; | |
1612 | } | |
1613 | ||
1614 | return r; | |
1615 | } | |
b6d33834 | 1616 | |
b0c632db HC |
1617 | static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) |
1618 | { | |
b0c632db | 1619 | kvm_s390_vcpu_initial_reset(vcpu); |
b0c632db HC |
1620 | return 0; |
1621 | } | |
1622 | ||
1623 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
1624 | { | |
5a32c1af | 1625 | memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); |
b0c632db HC |
1626 | return 0; |
1627 | } | |
1628 | ||
1629 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
1630 | { | |
5a32c1af | 1631 | memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); |
b0c632db HC |
1632 | return 0; |
1633 | } | |
1634 | ||
1635 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
1636 | struct kvm_sregs *sregs) | |
1637 | { | |
59674c1a | 1638 | memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); |
b0c632db | 1639 | memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); |
59674c1a | 1640 | restore_access_regs(vcpu->run->s.regs.acrs); |
b0c632db HC |
1641 | return 0; |
1642 | } | |
1643 | ||
1644 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | |
1645 | struct kvm_sregs *sregs) | |
1646 | { | |
59674c1a | 1647 | memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); |
b0c632db | 1648 | memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); |
b0c632db HC |
1649 | return 0; |
1650 | } | |
1651 | ||
1652 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
1653 | { | |
4725c860 MS |
1654 | if (test_fp_ctl(fpu->fpc)) |
1655 | return -EINVAL; | |
b0c632db | 1656 | memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs)); |
4725c860 MS |
1657 | vcpu->arch.guest_fpregs.fpc = fpu->fpc; |
1658 | restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc); | |
1659 | restore_fp_regs(vcpu->arch.guest_fpregs.fprs); | |
b0c632db HC |
1660 | return 0; |
1661 | } | |
1662 | ||
1663 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
1664 | { | |
b0c632db HC |
1665 | memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs)); |
1666 | fpu->fpc = vcpu->arch.guest_fpregs.fpc; | |
b0c632db HC |
1667 | return 0; |
1668 | } | |
1669 | ||
1670 | static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) | |
1671 | { | |
1672 | int rc = 0; | |
1673 | ||
7a42fdc2 | 1674 | if (!is_vcpu_stopped(vcpu)) |
b0c632db | 1675 | rc = -EBUSY; |
d7b0b5eb CO |
1676 | else { |
1677 | vcpu->run->psw_mask = psw.mask; | |
1678 | vcpu->run->psw_addr = psw.addr; | |
1679 | } | |
b0c632db HC |
1680 | return rc; |
1681 | } | |
1682 | ||
1683 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | |
1684 | struct kvm_translation *tr) | |
1685 | { | |
1686 | return -EINVAL; /* not implemented yet */ | |
1687 | } | |
1688 | ||
27291e21 DH |
1689 | #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ |
1690 | KVM_GUESTDBG_USE_HW_BP | \ | |
1691 | KVM_GUESTDBG_ENABLE) | |
1692 | ||
d0bfb940 JK |
1693 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
1694 | struct kvm_guest_debug *dbg) | |
b0c632db | 1695 | { |
27291e21 DH |
1696 | int rc = 0; |
1697 | ||
1698 | vcpu->guest_debug = 0; | |
1699 | kvm_s390_clear_bp_data(vcpu); | |
1700 | ||
2de3bfc2 | 1701 | if (dbg->control & ~VALID_GUESTDBG_FLAGS) |
27291e21 DH |
1702 | return -EINVAL; |
1703 | ||
1704 | if (dbg->control & KVM_GUESTDBG_ENABLE) { | |
1705 | vcpu->guest_debug = dbg->control; | |
1706 | /* enforce guest PER */ | |
1707 | atomic_set_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); | |
1708 | ||
1709 | if (dbg->control & KVM_GUESTDBG_USE_HW_BP) | |
1710 | rc = kvm_s390_import_bp_data(vcpu, dbg); | |
1711 | } else { | |
1712 | atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); | |
1713 | vcpu->arch.guestdbg.last_bp = 0; | |
1714 | } | |
1715 | ||
1716 | if (rc) { | |
1717 | vcpu->guest_debug = 0; | |
1718 | kvm_s390_clear_bp_data(vcpu); | |
1719 | atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags); | |
1720 | } | |
1721 | ||
1722 | return rc; | |
b0c632db HC |
1723 | } |
1724 | ||
62d9f0db MT |
1725 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
1726 | struct kvm_mp_state *mp_state) | |
1727 | { | |
6352e4d2 DH |
1728 | /* CHECK_STOP and LOAD are not supported yet */ |
1729 | return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : | |
1730 | KVM_MP_STATE_OPERATING; | |
62d9f0db MT |
1731 | } |
1732 | ||
1733 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
1734 | struct kvm_mp_state *mp_state) | |
1735 | { | |
6352e4d2 DH |
1736 | int rc = 0; |
1737 | ||
1738 | /* user space knows about this interface - let it control the state */ | |
1739 | vcpu->kvm->arch.user_cpu_state_ctrl = 1; | |
1740 | ||
1741 | switch (mp_state->mp_state) { | |
1742 | case KVM_MP_STATE_STOPPED: | |
1743 | kvm_s390_vcpu_stop(vcpu); | |
1744 | break; | |
1745 | case KVM_MP_STATE_OPERATING: | |
1746 | kvm_s390_vcpu_start(vcpu); | |
1747 | break; | |
1748 | case KVM_MP_STATE_LOAD: | |
1749 | case KVM_MP_STATE_CHECK_STOP: | |
1750 | /* fall through - CHECK_STOP and LOAD are not supported yet */ | |
1751 | default: | |
1752 | rc = -ENXIO; | |
1753 | } | |
1754 | ||
1755 | return rc; | |
62d9f0db MT |
1756 | } |
1757 | ||
8ad35755 DH |
1758 | static bool ibs_enabled(struct kvm_vcpu *vcpu) |
1759 | { | |
1760 | return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS; | |
1761 | } | |
1762 | ||
2c70fe44 CB |
1763 | static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) |
1764 | { | |
785dbef4 CB |
1765 | if (!vcpu->requests) |
1766 | return 0; | |
8ad35755 | 1767 | retry: |
8e236546 | 1768 | kvm_s390_vcpu_request_handled(vcpu); |
2c70fe44 CB |
1769 | /* |
1770 | * We use MMU_RELOAD just to re-arm the ipte notifier for the | |
1771 | * guest prefix page. gmap_ipte_notify will wait on the ptl lock. | |
1772 | * This ensures that the ipte instruction for this request has | |
1773 | * already finished. We might race against a second unmapper that | |
1774 | * wants to set the blocking bit. Lets just retry the request loop. | |
1775 | */ | |
8ad35755 | 1776 | if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) { |
2c70fe44 CB |
1777 | int rc; |
1778 | rc = gmap_ipte_notify(vcpu->arch.gmap, | |
fda902cb | 1779 | kvm_s390_get_prefix(vcpu), |
2c70fe44 CB |
1780 | PAGE_SIZE * 2); |
1781 | if (rc) | |
1782 | return rc; | |
8ad35755 | 1783 | goto retry; |
2c70fe44 | 1784 | } |
8ad35755 | 1785 | |
d3d692c8 DH |
1786 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { |
1787 | vcpu->arch.sie_block->ihcpu = 0xffff; | |
1788 | goto retry; | |
1789 | } | |
1790 | ||
8ad35755 DH |
1791 | if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { |
1792 | if (!ibs_enabled(vcpu)) { | |
1793 | trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); | |
1794 | atomic_set_mask(CPUSTAT_IBS, | |
1795 | &vcpu->arch.sie_block->cpuflags); | |
1796 | } | |
1797 | goto retry; | |
2c70fe44 | 1798 | } |
8ad35755 DH |
1799 | |
1800 | if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { | |
1801 | if (ibs_enabled(vcpu)) { | |
1802 | trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); | |
1803 | atomic_clear_mask(CPUSTAT_IBS, | |
1804 | &vcpu->arch.sie_block->cpuflags); | |
1805 | } | |
1806 | goto retry; | |
1807 | } | |
1808 | ||
0759d068 DH |
1809 | /* nothing to do, just clear the request */ |
1810 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); | |
1811 | ||
2c70fe44 CB |
1812 | return 0; |
1813 | } | |
1814 | ||
fa576c58 TH |
1815 | /** |
1816 | * kvm_arch_fault_in_page - fault-in guest page if necessary | |
1817 | * @vcpu: The corresponding virtual cpu | |
1818 | * @gpa: Guest physical address | |
1819 | * @writable: Whether the page should be writable or not | |
1820 | * | |
1821 | * Make sure that a guest page has been faulted-in on the host. | |
1822 | * | |
1823 | * Return: Zero on success, negative error code otherwise. | |
1824 | */ | |
1825 | long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) | |
24eb3a82 | 1826 | { |
527e30b4 MS |
1827 | return gmap_fault(vcpu->arch.gmap, gpa, |
1828 | writable ? FAULT_FLAG_WRITE : 0); | |
24eb3a82 DD |
1829 | } |
1830 | ||
3c038e6b DD |
1831 | static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, |
1832 | unsigned long token) | |
1833 | { | |
1834 | struct kvm_s390_interrupt inti; | |
383d0b05 | 1835 | struct kvm_s390_irq irq; |
3c038e6b DD |
1836 | |
1837 | if (start_token) { | |
383d0b05 JF |
1838 | irq.u.ext.ext_params2 = token; |
1839 | irq.type = KVM_S390_INT_PFAULT_INIT; | |
1840 | WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); | |
3c038e6b DD |
1841 | } else { |
1842 | inti.type = KVM_S390_INT_PFAULT_DONE; | |
383d0b05 | 1843 | inti.parm64 = token; |
3c038e6b DD |
1844 | WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); |
1845 | } | |
1846 | } | |
1847 | ||
1848 | void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, | |
1849 | struct kvm_async_pf *work) | |
1850 | { | |
1851 | trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); | |
1852 | __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); | |
1853 | } | |
1854 | ||
1855 | void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, | |
1856 | struct kvm_async_pf *work) | |
1857 | { | |
1858 | trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); | |
1859 | __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); | |
1860 | } | |
1861 | ||
1862 | void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, | |
1863 | struct kvm_async_pf *work) | |
1864 | { | |
1865 | /* s390 will always inject the page directly */ | |
1866 | } | |
1867 | ||
1868 | bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu) | |
1869 | { | |
1870 | /* | |
1871 | * s390 will always inject the page directly, | |
1872 | * but we still want check_async_completion to cleanup | |
1873 | */ | |
1874 | return true; | |
1875 | } | |
1876 | ||
1877 | static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) | |
1878 | { | |
1879 | hva_t hva; | |
1880 | struct kvm_arch_async_pf arch; | |
1881 | int rc; | |
1882 | ||
1883 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) | |
1884 | return 0; | |
1885 | if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != | |
1886 | vcpu->arch.pfault_compare) | |
1887 | return 0; | |
1888 | if (psw_extint_disabled(vcpu)) | |
1889 | return 0; | |
9a022067 | 1890 | if (kvm_s390_vcpu_has_irq(vcpu, 0)) |
3c038e6b DD |
1891 | return 0; |
1892 | if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul)) | |
1893 | return 0; | |
1894 | if (!vcpu->arch.gmap->pfault_enabled) | |
1895 | return 0; | |
1896 | ||
81480cc1 HC |
1897 | hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); |
1898 | hva += current->thread.gmap_addr & ~PAGE_MASK; | |
1899 | if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) | |
3c038e6b DD |
1900 | return 0; |
1901 | ||
1902 | rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); | |
1903 | return rc; | |
1904 | } | |
1905 | ||
3fb4c40f | 1906 | static int vcpu_pre_run(struct kvm_vcpu *vcpu) |
b0c632db | 1907 | { |
3fb4c40f | 1908 | int rc, cpuflags; |
e168bf8d | 1909 | |
3c038e6b DD |
1910 | /* |
1911 | * On s390 notifications for arriving pages will be delivered directly | |
1912 | * to the guest but the house keeping for completed pfaults is | |
1913 | * handled outside the worker. | |
1914 | */ | |
1915 | kvm_check_async_pf_completion(vcpu); | |
1916 | ||
5a32c1af | 1917 | memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16); |
b0c632db HC |
1918 | |
1919 | if (need_resched()) | |
1920 | schedule(); | |
1921 | ||
d3a73acb | 1922 | if (test_cpu_flag(CIF_MCCK_PENDING)) |
71cde587 CB |
1923 | s390_handle_mcck(); |
1924 | ||
79395031 JF |
1925 | if (!kvm_is_ucontrol(vcpu->kvm)) { |
1926 | rc = kvm_s390_deliver_pending_interrupts(vcpu); | |
1927 | if (rc) | |
1928 | return rc; | |
1929 | } | |
0ff31867 | 1930 | |
2c70fe44 CB |
1931 | rc = kvm_s390_handle_requests(vcpu); |
1932 | if (rc) | |
1933 | return rc; | |
1934 | ||
27291e21 DH |
1935 | if (guestdbg_enabled(vcpu)) { |
1936 | kvm_s390_backup_guest_per_regs(vcpu); | |
1937 | kvm_s390_patch_guest_per_regs(vcpu); | |
1938 | } | |
1939 | ||
b0c632db | 1940 | vcpu->arch.sie_block->icptcode = 0; |
3fb4c40f TH |
1941 | cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); |
1942 | VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); | |
1943 | trace_kvm_s390_sie_enter(vcpu, cpuflags); | |
2b29a9fd | 1944 | |
3fb4c40f TH |
1945 | return 0; |
1946 | } | |
1947 | ||
492d8642 TH |
1948 | static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) |
1949 | { | |
1950 | psw_t *psw = &vcpu->arch.sie_block->gpsw; | |
1951 | u8 opcode; | |
1952 | int rc; | |
1953 | ||
1954 | VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); | |
1955 | trace_kvm_s390_sie_fault(vcpu); | |
1956 | ||
1957 | /* | |
1958 | * We want to inject an addressing exception, which is defined as a | |
1959 | * suppressing or terminating exception. However, since we came here | |
1960 | * by a DAT access exception, the PSW still points to the faulting | |
1961 | * instruction since DAT exceptions are nullifying. So we've got | |
1962 | * to look up the current opcode to get the length of the instruction | |
1963 | * to be able to forward the PSW. | |
1964 | */ | |
8ae04b8f | 1965 | rc = read_guest(vcpu, psw->addr, 0, &opcode, 1); |
492d8642 TH |
1966 | if (rc) |
1967 | return kvm_s390_inject_prog_cond(vcpu, rc); | |
1968 | psw->addr = __rewind_psw(*psw, -insn_length(opcode)); | |
1969 | ||
1970 | return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
1971 | } | |
1972 | ||
3fb4c40f TH |
1973 | static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) |
1974 | { | |
24eb3a82 | 1975 | int rc = -1; |
2b29a9fd DD |
1976 | |
1977 | VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", | |
1978 | vcpu->arch.sie_block->icptcode); | |
1979 | trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); | |
1980 | ||
27291e21 DH |
1981 | if (guestdbg_enabled(vcpu)) |
1982 | kvm_s390_restore_guest_per_regs(vcpu); | |
1983 | ||
3fb4c40f | 1984 | if (exit_reason >= 0) { |
7c470539 | 1985 | rc = 0; |
210b1607 TH |
1986 | } else if (kvm_is_ucontrol(vcpu->kvm)) { |
1987 | vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; | |
1988 | vcpu->run->s390_ucontrol.trans_exc_code = | |
1989 | current->thread.gmap_addr; | |
1990 | vcpu->run->s390_ucontrol.pgm_code = 0x10; | |
1991 | rc = -EREMOTE; | |
24eb3a82 DD |
1992 | |
1993 | } else if (current->thread.gmap_pfault) { | |
3c038e6b | 1994 | trace_kvm_s390_major_guest_pfault(vcpu); |
24eb3a82 | 1995 | current->thread.gmap_pfault = 0; |
fa576c58 | 1996 | if (kvm_arch_setup_async_pf(vcpu)) { |
24eb3a82 | 1997 | rc = 0; |
fa576c58 TH |
1998 | } else { |
1999 | gpa_t gpa = current->thread.gmap_addr; | |
2000 | rc = kvm_arch_fault_in_page(vcpu, gpa, 1); | |
2001 | } | |
24eb3a82 DD |
2002 | } |
2003 | ||
492d8642 TH |
2004 | if (rc == -1) |
2005 | rc = vcpu_post_run_fault_in_sie(vcpu); | |
b0c632db | 2006 | |
5a32c1af | 2007 | memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16); |
3fb4c40f | 2008 | |
a76ccff6 TH |
2009 | if (rc == 0) { |
2010 | if (kvm_is_ucontrol(vcpu->kvm)) | |
2955c83f CB |
2011 | /* Don't exit for host interrupts. */ |
2012 | rc = vcpu->arch.sie_block->icptcode ? -EOPNOTSUPP : 0; | |
a76ccff6 TH |
2013 | else |
2014 | rc = kvm_handle_sie_intercept(vcpu); | |
2015 | } | |
2016 | ||
3fb4c40f TH |
2017 | return rc; |
2018 | } | |
2019 | ||
2020 | static int __vcpu_run(struct kvm_vcpu *vcpu) | |
2021 | { | |
2022 | int rc, exit_reason; | |
2023 | ||
800c1065 TH |
2024 | /* |
2025 | * We try to hold kvm->srcu during most of vcpu_run (except when run- | |
2026 | * ning the guest), so that memslots (and other stuff) are protected | |
2027 | */ | |
2028 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); | |
2029 | ||
a76ccff6 TH |
2030 | do { |
2031 | rc = vcpu_pre_run(vcpu); | |
2032 | if (rc) | |
2033 | break; | |
3fb4c40f | 2034 | |
800c1065 | 2035 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
a76ccff6 TH |
2036 | /* |
2037 | * As PF_VCPU will be used in fault handler, between | |
2038 | * guest_enter and guest_exit should be no uaccess. | |
2039 | */ | |
0097d12e CB |
2040 | local_irq_disable(); |
2041 | __kvm_guest_enter(); | |
2042 | local_irq_enable(); | |
a76ccff6 TH |
2043 | exit_reason = sie64a(vcpu->arch.sie_block, |
2044 | vcpu->run->s.regs.gprs); | |
0097d12e CB |
2045 | local_irq_disable(); |
2046 | __kvm_guest_exit(); | |
2047 | local_irq_enable(); | |
800c1065 | 2048 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
a76ccff6 TH |
2049 | |
2050 | rc = vcpu_post_run(vcpu, exit_reason); | |
27291e21 | 2051 | } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); |
3fb4c40f | 2052 | |
800c1065 | 2053 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
e168bf8d | 2054 | return rc; |
b0c632db HC |
2055 | } |
2056 | ||
b028ee3e DH |
2057 | static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
2058 | { | |
2059 | vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; | |
2060 | vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; | |
2061 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) | |
2062 | kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); | |
2063 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { | |
2064 | memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); | |
d3d692c8 DH |
2065 | /* some control register changes require a tlb flush */ |
2066 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); | |
b028ee3e DH |
2067 | } |
2068 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { | |
2069 | vcpu->arch.sie_block->cputm = kvm_run->s.regs.cputm; | |
2070 | vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; | |
2071 | vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; | |
2072 | vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; | |
2073 | vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; | |
2074 | } | |
2075 | if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { | |
2076 | vcpu->arch.pfault_token = kvm_run->s.regs.pft; | |
2077 | vcpu->arch.pfault_select = kvm_run->s.regs.pfs; | |
2078 | vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; | |
9fbd8082 DH |
2079 | if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) |
2080 | kvm_clear_async_pf_completion_queue(vcpu); | |
b028ee3e DH |
2081 | } |
2082 | kvm_run->kvm_dirty_regs = 0; | |
2083 | } | |
2084 | ||
2085 | static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
2086 | { | |
2087 | kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; | |
2088 | kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; | |
2089 | kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); | |
2090 | memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); | |
2091 | kvm_run->s.regs.cputm = vcpu->arch.sie_block->cputm; | |
2092 | kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; | |
2093 | kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; | |
2094 | kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; | |
2095 | kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; | |
2096 | kvm_run->s.regs.pft = vcpu->arch.pfault_token; | |
2097 | kvm_run->s.regs.pfs = vcpu->arch.pfault_select; | |
2098 | kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; | |
2099 | } | |
2100 | ||
b0c632db HC |
2101 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
2102 | { | |
8f2abe6a | 2103 | int rc; |
b0c632db HC |
2104 | sigset_t sigsaved; |
2105 | ||
27291e21 DH |
2106 | if (guestdbg_exit_pending(vcpu)) { |
2107 | kvm_s390_prepare_debug_exit(vcpu); | |
2108 | return 0; | |
2109 | } | |
2110 | ||
b0c632db HC |
2111 | if (vcpu->sigset_active) |
2112 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
2113 | ||
6352e4d2 DH |
2114 | if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { |
2115 | kvm_s390_vcpu_start(vcpu); | |
2116 | } else if (is_vcpu_stopped(vcpu)) { | |
ea2cdd27 | 2117 | pr_err_ratelimited("can't run stopped vcpu %d\n", |
6352e4d2 DH |
2118 | vcpu->vcpu_id); |
2119 | return -EINVAL; | |
2120 | } | |
b0c632db | 2121 | |
b028ee3e | 2122 | sync_regs(vcpu, kvm_run); |
d7b0b5eb | 2123 | |
dab4079d | 2124 | might_fault(); |
a76ccff6 | 2125 | rc = __vcpu_run(vcpu); |
9ace903d | 2126 | |
b1d16c49 CE |
2127 | if (signal_pending(current) && !rc) { |
2128 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
8f2abe6a | 2129 | rc = -EINTR; |
b1d16c49 | 2130 | } |
8f2abe6a | 2131 | |
27291e21 DH |
2132 | if (guestdbg_exit_pending(vcpu) && !rc) { |
2133 | kvm_s390_prepare_debug_exit(vcpu); | |
2134 | rc = 0; | |
2135 | } | |
2136 | ||
b8e660b8 | 2137 | if (rc == -EOPNOTSUPP) { |
8f2abe6a CB |
2138 | /* intercept cannot be handled in-kernel, prepare kvm-run */ |
2139 | kvm_run->exit_reason = KVM_EXIT_S390_SIEIC; | |
2140 | kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; | |
8f2abe6a CB |
2141 | kvm_run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; |
2142 | kvm_run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; | |
2143 | rc = 0; | |
2144 | } | |
2145 | ||
2146 | if (rc == -EREMOTE) { | |
2147 | /* intercept was handled, but userspace support is needed | |
2148 | * kvm_run has been prepared by the handler */ | |
2149 | rc = 0; | |
2150 | } | |
b0c632db | 2151 | |
b028ee3e | 2152 | store_regs(vcpu, kvm_run); |
d7b0b5eb | 2153 | |
b0c632db HC |
2154 | if (vcpu->sigset_active) |
2155 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
2156 | ||
b0c632db | 2157 | vcpu->stat.exit_userspace++; |
7e8e6ab4 | 2158 | return rc; |
b0c632db HC |
2159 | } |
2160 | ||
b0c632db HC |
2161 | /* |
2162 | * store status at address | |
2163 | * we use have two special cases: | |
2164 | * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit | |
2165 | * KVM_S390_STORE_STATUS_PREFIXED: -> prefix | |
2166 | */ | |
d0bce605 | 2167 | int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) |
b0c632db | 2168 | { |
092670cd | 2169 | unsigned char archmode = 1; |
fda902cb | 2170 | unsigned int px; |
178bd789 | 2171 | u64 clkcomp; |
d0bce605 | 2172 | int rc; |
b0c632db | 2173 | |
d0bce605 HC |
2174 | if (gpa == KVM_S390_STORE_STATUS_NOADDR) { |
2175 | if (write_guest_abs(vcpu, 163, &archmode, 1)) | |
b0c632db | 2176 | return -EFAULT; |
d0bce605 HC |
2177 | gpa = SAVE_AREA_BASE; |
2178 | } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { | |
2179 | if (write_guest_real(vcpu, 163, &archmode, 1)) | |
b0c632db | 2180 | return -EFAULT; |
d0bce605 HC |
2181 | gpa = kvm_s390_real_to_abs(vcpu, SAVE_AREA_BASE); |
2182 | } | |
2183 | rc = write_guest_abs(vcpu, gpa + offsetof(struct save_area, fp_regs), | |
2184 | vcpu->arch.guest_fpregs.fprs, 128); | |
2185 | rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, gp_regs), | |
2186 | vcpu->run->s.regs.gprs, 128); | |
2187 | rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, psw), | |
2188 | &vcpu->arch.sie_block->gpsw, 16); | |
fda902cb | 2189 | px = kvm_s390_get_prefix(vcpu); |
d0bce605 | 2190 | rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, pref_reg), |
fda902cb | 2191 | &px, 4); |
d0bce605 HC |
2192 | rc |= write_guest_abs(vcpu, |
2193 | gpa + offsetof(struct save_area, fp_ctrl_reg), | |
2194 | &vcpu->arch.guest_fpregs.fpc, 4); | |
2195 | rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, tod_reg), | |
2196 | &vcpu->arch.sie_block->todpr, 4); | |
2197 | rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, timer), | |
2198 | &vcpu->arch.sie_block->cputm, 8); | |
178bd789 | 2199 | clkcomp = vcpu->arch.sie_block->ckc >> 8; |
d0bce605 HC |
2200 | rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, clk_cmp), |
2201 | &clkcomp, 8); | |
2202 | rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, acc_regs), | |
2203 | &vcpu->run->s.regs.acrs, 64); | |
2204 | rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, ctrl_regs), | |
2205 | &vcpu->arch.sie_block->gcr, 128); | |
2206 | return rc ? -EFAULT : 0; | |
b0c632db HC |
2207 | } |
2208 | ||
e879892c TH |
2209 | int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) |
2210 | { | |
2211 | /* | |
2212 | * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy | |
2213 | * copying in vcpu load/put. Lets update our copies before we save | |
2214 | * it into the save area | |
2215 | */ | |
2216 | save_fp_ctl(&vcpu->arch.guest_fpregs.fpc); | |
2217 | save_fp_regs(vcpu->arch.guest_fpregs.fprs); | |
2218 | save_access_regs(vcpu->run->s.regs.acrs); | |
2219 | ||
2220 | return kvm_s390_store_status_unloaded(vcpu, addr); | |
2221 | } | |
2222 | ||
bc17de7c EF |
2223 | /* |
2224 | * store additional status at address | |
2225 | */ | |
2226 | int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu, | |
2227 | unsigned long gpa) | |
2228 | { | |
2229 | /* Only bits 0-53 are used for address formation */ | |
2230 | if (!(gpa & ~0x3ff)) | |
2231 | return 0; | |
2232 | ||
2233 | return write_guest_abs(vcpu, gpa & ~0x3ff, | |
2234 | (void *)&vcpu->run->s.regs.vrs, 512); | |
2235 | } | |
2236 | ||
2237 | int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr) | |
2238 | { | |
2239 | if (!test_kvm_facility(vcpu->kvm, 129)) | |
2240 | return 0; | |
2241 | ||
2242 | /* | |
2243 | * The guest VXRS are in the host VXRs due to the lazy | |
2244 | * copying in vcpu load/put. Let's update our copies before we save | |
2245 | * it into the save area. | |
2246 | */ | |
2247 | save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs); | |
2248 | ||
2249 | return kvm_s390_store_adtl_status_unloaded(vcpu, addr); | |
2250 | } | |
2251 | ||
8ad35755 DH |
2252 | static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) |
2253 | { | |
2254 | kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); | |
8e236546 | 2255 | kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); |
8ad35755 DH |
2256 | } |
2257 | ||
2258 | static void __disable_ibs_on_all_vcpus(struct kvm *kvm) | |
2259 | { | |
2260 | unsigned int i; | |
2261 | struct kvm_vcpu *vcpu; | |
2262 | ||
2263 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
2264 | __disable_ibs_on_vcpu(vcpu); | |
2265 | } | |
2266 | } | |
2267 | ||
2268 | static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) | |
2269 | { | |
2270 | kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); | |
8e236546 | 2271 | kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); |
8ad35755 DH |
2272 | } |
2273 | ||
6852d7b6 DH |
2274 | void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) |
2275 | { | |
8ad35755 DH |
2276 | int i, online_vcpus, started_vcpus = 0; |
2277 | ||
2278 | if (!is_vcpu_stopped(vcpu)) | |
2279 | return; | |
2280 | ||
6852d7b6 | 2281 | trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); |
8ad35755 | 2282 | /* Only one cpu at a time may enter/leave the STOPPED state. */ |
433b9ee4 | 2283 | spin_lock(&vcpu->kvm->arch.start_stop_lock); |
8ad35755 DH |
2284 | online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); |
2285 | ||
2286 | for (i = 0; i < online_vcpus; i++) { | |
2287 | if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) | |
2288 | started_vcpus++; | |
2289 | } | |
2290 | ||
2291 | if (started_vcpus == 0) { | |
2292 | /* we're the only active VCPU -> speed it up */ | |
2293 | __enable_ibs_on_vcpu(vcpu); | |
2294 | } else if (started_vcpus == 1) { | |
2295 | /* | |
2296 | * As we are starting a second VCPU, we have to disable | |
2297 | * the IBS facility on all VCPUs to remove potentially | |
2298 | * oustanding ENABLE requests. | |
2299 | */ | |
2300 | __disable_ibs_on_all_vcpus(vcpu->kvm); | |
2301 | } | |
2302 | ||
6852d7b6 | 2303 | atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); |
8ad35755 DH |
2304 | /* |
2305 | * Another VCPU might have used IBS while we were offline. | |
2306 | * Let's play safe and flush the VCPU at startup. | |
2307 | */ | |
d3d692c8 | 2308 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
433b9ee4 | 2309 | spin_unlock(&vcpu->kvm->arch.start_stop_lock); |
8ad35755 | 2310 | return; |
6852d7b6 DH |
2311 | } |
2312 | ||
2313 | void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) | |
2314 | { | |
8ad35755 DH |
2315 | int i, online_vcpus, started_vcpus = 0; |
2316 | struct kvm_vcpu *started_vcpu = NULL; | |
2317 | ||
2318 | if (is_vcpu_stopped(vcpu)) | |
2319 | return; | |
2320 | ||
6852d7b6 | 2321 | trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); |
8ad35755 | 2322 | /* Only one cpu at a time may enter/leave the STOPPED state. */ |
433b9ee4 | 2323 | spin_lock(&vcpu->kvm->arch.start_stop_lock); |
8ad35755 DH |
2324 | online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); |
2325 | ||
32f5ff63 | 2326 | /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */ |
6cddd432 | 2327 | kvm_s390_clear_stop_irq(vcpu); |
32f5ff63 | 2328 | |
6cddd432 | 2329 | atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); |
8ad35755 DH |
2330 | __disable_ibs_on_vcpu(vcpu); |
2331 | ||
2332 | for (i = 0; i < online_vcpus; i++) { | |
2333 | if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) { | |
2334 | started_vcpus++; | |
2335 | started_vcpu = vcpu->kvm->vcpus[i]; | |
2336 | } | |
2337 | } | |
2338 | ||
2339 | if (started_vcpus == 1) { | |
2340 | /* | |
2341 | * As we only have one VCPU left, we want to enable the | |
2342 | * IBS facility for that VCPU to speed it up. | |
2343 | */ | |
2344 | __enable_ibs_on_vcpu(started_vcpu); | |
2345 | } | |
2346 | ||
433b9ee4 | 2347 | spin_unlock(&vcpu->kvm->arch.start_stop_lock); |
8ad35755 | 2348 | return; |
6852d7b6 DH |
2349 | } |
2350 | ||
d6712df9 CH |
2351 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
2352 | struct kvm_enable_cap *cap) | |
2353 | { | |
2354 | int r; | |
2355 | ||
2356 | if (cap->flags) | |
2357 | return -EINVAL; | |
2358 | ||
2359 | switch (cap->cap) { | |
fa6b7fe9 CH |
2360 | case KVM_CAP_S390_CSS_SUPPORT: |
2361 | if (!vcpu->kvm->arch.css_support) { | |
2362 | vcpu->kvm->arch.css_support = 1; | |
2363 | trace_kvm_s390_enable_css(vcpu->kvm); | |
2364 | } | |
2365 | r = 0; | |
2366 | break; | |
d6712df9 CH |
2367 | default: |
2368 | r = -EINVAL; | |
2369 | break; | |
2370 | } | |
2371 | return r; | |
2372 | } | |
2373 | ||
41408c28 TH |
2374 | static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu, |
2375 | struct kvm_s390_mem_op *mop) | |
2376 | { | |
2377 | void __user *uaddr = (void __user *)mop->buf; | |
2378 | void *tmpbuf = NULL; | |
2379 | int r, srcu_idx; | |
2380 | const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION | |
2381 | | KVM_S390_MEMOP_F_CHECK_ONLY; | |
2382 | ||
2383 | if (mop->flags & ~supported_flags) | |
2384 | return -EINVAL; | |
2385 | ||
2386 | if (mop->size > MEM_OP_MAX_SIZE) | |
2387 | return -E2BIG; | |
2388 | ||
2389 | if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { | |
2390 | tmpbuf = vmalloc(mop->size); | |
2391 | if (!tmpbuf) | |
2392 | return -ENOMEM; | |
2393 | } | |
2394 | ||
2395 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); | |
2396 | ||
2397 | switch (mop->op) { | |
2398 | case KVM_S390_MEMOP_LOGICAL_READ: | |
2399 | if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { | |
2400 | r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, false); | |
2401 | break; | |
2402 | } | |
2403 | r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); | |
2404 | if (r == 0) { | |
2405 | if (copy_to_user(uaddr, tmpbuf, mop->size)) | |
2406 | r = -EFAULT; | |
2407 | } | |
2408 | break; | |
2409 | case KVM_S390_MEMOP_LOGICAL_WRITE: | |
2410 | if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { | |
2411 | r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, true); | |
2412 | break; | |
2413 | } | |
2414 | if (copy_from_user(tmpbuf, uaddr, mop->size)) { | |
2415 | r = -EFAULT; | |
2416 | break; | |
2417 | } | |
2418 | r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size); | |
2419 | break; | |
2420 | default: | |
2421 | r = -EINVAL; | |
2422 | } | |
2423 | ||
2424 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); | |
2425 | ||
2426 | if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) | |
2427 | kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); | |
2428 | ||
2429 | vfree(tmpbuf); | |
2430 | return r; | |
2431 | } | |
2432 | ||
b0c632db HC |
2433 | long kvm_arch_vcpu_ioctl(struct file *filp, |
2434 | unsigned int ioctl, unsigned long arg) | |
2435 | { | |
2436 | struct kvm_vcpu *vcpu = filp->private_data; | |
2437 | void __user *argp = (void __user *)arg; | |
800c1065 | 2438 | int idx; |
bc923cc9 | 2439 | long r; |
b0c632db | 2440 | |
93736624 | 2441 | switch (ioctl) { |
47b43c52 JF |
2442 | case KVM_S390_IRQ: { |
2443 | struct kvm_s390_irq s390irq; | |
2444 | ||
2445 | r = -EFAULT; | |
2446 | if (copy_from_user(&s390irq, argp, sizeof(s390irq))) | |
2447 | break; | |
2448 | r = kvm_s390_inject_vcpu(vcpu, &s390irq); | |
2449 | break; | |
2450 | } | |
93736624 | 2451 | case KVM_S390_INTERRUPT: { |
ba5c1e9b | 2452 | struct kvm_s390_interrupt s390int; |
383d0b05 | 2453 | struct kvm_s390_irq s390irq; |
ba5c1e9b | 2454 | |
93736624 | 2455 | r = -EFAULT; |
ba5c1e9b | 2456 | if (copy_from_user(&s390int, argp, sizeof(s390int))) |
93736624 | 2457 | break; |
383d0b05 JF |
2458 | if (s390int_to_s390irq(&s390int, &s390irq)) |
2459 | return -EINVAL; | |
2460 | r = kvm_s390_inject_vcpu(vcpu, &s390irq); | |
93736624 | 2461 | break; |
ba5c1e9b | 2462 | } |
b0c632db | 2463 | case KVM_S390_STORE_STATUS: |
800c1065 | 2464 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
bc923cc9 | 2465 | r = kvm_s390_vcpu_store_status(vcpu, arg); |
800c1065 | 2466 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
bc923cc9 | 2467 | break; |
b0c632db HC |
2468 | case KVM_S390_SET_INITIAL_PSW: { |
2469 | psw_t psw; | |
2470 | ||
bc923cc9 | 2471 | r = -EFAULT; |
b0c632db | 2472 | if (copy_from_user(&psw, argp, sizeof(psw))) |
bc923cc9 AK |
2473 | break; |
2474 | r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); | |
2475 | break; | |
b0c632db HC |
2476 | } |
2477 | case KVM_S390_INITIAL_RESET: | |
bc923cc9 AK |
2478 | r = kvm_arch_vcpu_ioctl_initial_reset(vcpu); |
2479 | break; | |
14eebd91 CO |
2480 | case KVM_SET_ONE_REG: |
2481 | case KVM_GET_ONE_REG: { | |
2482 | struct kvm_one_reg reg; | |
2483 | r = -EFAULT; | |
2484 | if (copy_from_user(®, argp, sizeof(reg))) | |
2485 | break; | |
2486 | if (ioctl == KVM_SET_ONE_REG) | |
2487 | r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); | |
2488 | else | |
2489 | r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); | |
2490 | break; | |
2491 | } | |
27e0393f CO |
2492 | #ifdef CONFIG_KVM_S390_UCONTROL |
2493 | case KVM_S390_UCAS_MAP: { | |
2494 | struct kvm_s390_ucas_mapping ucasmap; | |
2495 | ||
2496 | if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { | |
2497 | r = -EFAULT; | |
2498 | break; | |
2499 | } | |
2500 | ||
2501 | if (!kvm_is_ucontrol(vcpu->kvm)) { | |
2502 | r = -EINVAL; | |
2503 | break; | |
2504 | } | |
2505 | ||
2506 | r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, | |
2507 | ucasmap.vcpu_addr, ucasmap.length); | |
2508 | break; | |
2509 | } | |
2510 | case KVM_S390_UCAS_UNMAP: { | |
2511 | struct kvm_s390_ucas_mapping ucasmap; | |
2512 | ||
2513 | if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { | |
2514 | r = -EFAULT; | |
2515 | break; | |
2516 | } | |
2517 | ||
2518 | if (!kvm_is_ucontrol(vcpu->kvm)) { | |
2519 | r = -EINVAL; | |
2520 | break; | |
2521 | } | |
2522 | ||
2523 | r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, | |
2524 | ucasmap.length); | |
2525 | break; | |
2526 | } | |
2527 | #endif | |
ccc7910f | 2528 | case KVM_S390_VCPU_FAULT: { |
527e30b4 | 2529 | r = gmap_fault(vcpu->arch.gmap, arg, 0); |
ccc7910f CO |
2530 | break; |
2531 | } | |
d6712df9 CH |
2532 | case KVM_ENABLE_CAP: |
2533 | { | |
2534 | struct kvm_enable_cap cap; | |
2535 | r = -EFAULT; | |
2536 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
2537 | break; | |
2538 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); | |
2539 | break; | |
2540 | } | |
41408c28 TH |
2541 | case KVM_S390_MEM_OP: { |
2542 | struct kvm_s390_mem_op mem_op; | |
2543 | ||
2544 | if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) | |
2545 | r = kvm_s390_guest_mem_op(vcpu, &mem_op); | |
2546 | else | |
2547 | r = -EFAULT; | |
2548 | break; | |
2549 | } | |
816c7667 JF |
2550 | case KVM_S390_SET_IRQ_STATE: { |
2551 | struct kvm_s390_irq_state irq_state; | |
2552 | ||
2553 | r = -EFAULT; | |
2554 | if (copy_from_user(&irq_state, argp, sizeof(irq_state))) | |
2555 | break; | |
2556 | if (irq_state.len > VCPU_IRQS_MAX_BUF || | |
2557 | irq_state.len == 0 || | |
2558 | irq_state.len % sizeof(struct kvm_s390_irq) > 0) { | |
2559 | r = -EINVAL; | |
2560 | break; | |
2561 | } | |
2562 | r = kvm_s390_set_irq_state(vcpu, | |
2563 | (void __user *) irq_state.buf, | |
2564 | irq_state.len); | |
2565 | break; | |
2566 | } | |
2567 | case KVM_S390_GET_IRQ_STATE: { | |
2568 | struct kvm_s390_irq_state irq_state; | |
2569 | ||
2570 | r = -EFAULT; | |
2571 | if (copy_from_user(&irq_state, argp, sizeof(irq_state))) | |
2572 | break; | |
2573 | if (irq_state.len == 0) { | |
2574 | r = -EINVAL; | |
2575 | break; | |
2576 | } | |
2577 | r = kvm_s390_get_irq_state(vcpu, | |
2578 | (__u8 __user *) irq_state.buf, | |
2579 | irq_state.len); | |
2580 | break; | |
2581 | } | |
b0c632db | 2582 | default: |
3e6afcf1 | 2583 | r = -ENOTTY; |
b0c632db | 2584 | } |
bc923cc9 | 2585 | return r; |
b0c632db HC |
2586 | } |
2587 | ||
5b1c1493 CO |
2588 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
2589 | { | |
2590 | #ifdef CONFIG_KVM_S390_UCONTROL | |
2591 | if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) | |
2592 | && (kvm_is_ucontrol(vcpu->kvm))) { | |
2593 | vmf->page = virt_to_page(vcpu->arch.sie_block); | |
2594 | get_page(vmf->page); | |
2595 | return 0; | |
2596 | } | |
2597 | #endif | |
2598 | return VM_FAULT_SIGBUS; | |
2599 | } | |
2600 | ||
5587027c AK |
2601 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
2602 | unsigned long npages) | |
db3fe4eb TY |
2603 | { |
2604 | return 0; | |
2605 | } | |
2606 | ||
b0c632db | 2607 | /* Section: memory related */ |
f7784b8e MT |
2608 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
2609 | struct kvm_memory_slot *memslot, | |
09170a49 | 2610 | const struct kvm_userspace_memory_region *mem, |
7b6195a9 | 2611 | enum kvm_mr_change change) |
b0c632db | 2612 | { |
dd2887e7 NW |
2613 | /* A few sanity checks. We can have memory slots which have to be |
2614 | located/ended at a segment boundary (1MB). The memory in userland is | |
2615 | ok to be fragmented into various different vmas. It is okay to mmap() | |
2616 | and munmap() stuff in this slot after doing this call at any time */ | |
b0c632db | 2617 | |
598841ca | 2618 | if (mem->userspace_addr & 0xffffful) |
b0c632db HC |
2619 | return -EINVAL; |
2620 | ||
598841ca | 2621 | if (mem->memory_size & 0xffffful) |
b0c632db HC |
2622 | return -EINVAL; |
2623 | ||
f7784b8e MT |
2624 | return 0; |
2625 | } | |
2626 | ||
2627 | void kvm_arch_commit_memory_region(struct kvm *kvm, | |
09170a49 | 2628 | const struct kvm_userspace_memory_region *mem, |
8482644a | 2629 | const struct kvm_memory_slot *old, |
f36f3f28 | 2630 | const struct kvm_memory_slot *new, |
8482644a | 2631 | enum kvm_mr_change change) |
f7784b8e | 2632 | { |
f7850c92 | 2633 | int rc; |
f7784b8e | 2634 | |
2cef4deb CB |
2635 | /* If the basics of the memslot do not change, we do not want |
2636 | * to update the gmap. Every update causes several unnecessary | |
2637 | * segment translation exceptions. This is usually handled just | |
2638 | * fine by the normal fault handler + gmap, but it will also | |
2639 | * cause faults on the prefix page of running guest CPUs. | |
2640 | */ | |
2641 | if (old->userspace_addr == mem->userspace_addr && | |
2642 | old->base_gfn * PAGE_SIZE == mem->guest_phys_addr && | |
2643 | old->npages * PAGE_SIZE == mem->memory_size) | |
2644 | return; | |
598841ca CO |
2645 | |
2646 | rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr, | |
2647 | mem->guest_phys_addr, mem->memory_size); | |
2648 | if (rc) | |
ea2cdd27 | 2649 | pr_warn("failed to commit memory region\n"); |
598841ca | 2650 | return; |
b0c632db HC |
2651 | } |
2652 | ||
b0c632db HC |
2653 | static int __init kvm_s390_init(void) |
2654 | { | |
9d8d5786 | 2655 | return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
b0c632db HC |
2656 | } |
2657 | ||
2658 | static void __exit kvm_s390_exit(void) | |
2659 | { | |
2660 | kvm_exit(); | |
2661 | } | |
2662 | ||
2663 | module_init(kvm_s390_init); | |
2664 | module_exit(kvm_s390_exit); | |
566af940 CH |
2665 | |
2666 | /* | |
2667 | * Enable autoloading of the kvm module. | |
2668 | * Note that we add the module alias here instead of virt/kvm/kvm_main.c | |
2669 | * since x86 takes a different approach. | |
2670 | */ | |
2671 | #include <linux/miscdevice.h> | |
2672 | MODULE_ALIAS_MISCDEV(KVM_MINOR); | |
2673 | MODULE_ALIAS("devname:kvm"); |