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