blob: 02e03c862a60c33787e7393434e383471e63cb3c [file] [log] [blame]
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * hosting zSeries kernel virtual machines
Heiko Carstensb0c632d2008-03-25 18:47:20 +01003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2008, 2009
Heiko Carstensb0c632d2008-03-25 18:47:20 +01005 *
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>
Christian Ehrhardt628eb9b2009-05-25 13:40:51 +020013 * Christian Ehrhardt <ehrhardt@de.ibm.com>
Jason J. Herne15f36eb2012-08-02 10:10:17 -040014 * Jason J. Herne <jjherne@us.ibm.com>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010015 */
16
17#include <linux/compiler.h>
18#include <linux/err.h>
19#include <linux/fs.h>
Christian Borntraegerca872302009-05-12 17:21:49 +020020#include <linux/hrtimer.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010021#include <linux/init.h>
22#include <linux/kvm.h>
23#include <linux/kvm_host.h>
24#include <linux/module.h>
Tony Krowiaka374e892014-09-03 10:13:53 +020025#include <linux/random.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010026#include <linux/slab.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010027#include <linux/timer.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010028#include <asm/asm-offsets.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010029#include <asm/lowcore.h>
30#include <asm/pgtable.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010031#include <asm/nmi.h>
David Howellsa0616cd2012-03-28 18:30:02 +010032#include <asm/switch_to.h>
Christian Borntraeger1526bf92012-05-15 14:15:25 +020033#include <asm/sclp.h>
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010034#include "kvm-s390.h"
Heiko Carstensb0c632d2008-03-25 18:47:20 +010035#include "gaccess.h"
36
Cornelia Huck5786fff2012-07-23 17:20:29 +020037#define CREATE_TRACE_POINTS
38#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020039#include "trace-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020040
Heiko Carstensb0c632d2008-03-25 18:47:20 +010041#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
42
43struct kvm_stats_debugfs_item debugfs_entries[] = {
44 { "userspace_handled", VCPU_STAT(exit_userspace) },
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +020045 { "exit_null", VCPU_STAT(exit_null) },
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010046 { "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) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010050 { "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) },
Paolo Bonzinif7819512015-02-04 18:20:58 +010053 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
David Hildenbrandce2e4f02014-07-11 10:00:43 +020054 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020055 { "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010056 { "instruction_lctl", VCPU_STAT(instruction_lctl) },
David Hildenbrandaba07502014-01-23 10:47:13 +010057 { "instruction_stctl", VCPU_STAT(instruction_stctl) },
58 { "instruction_stctg", VCPU_STAT(instruction_stctg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010059 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020060 { "deliver_external_call", VCPU_STAT(deliver_external_call) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010061 { "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) },
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020068 { "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010069 { "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) },
Heiko Carstens8a2422342014-01-10 14:33:28 +010074 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010075 { "instruction_stsch", VCPU_STAT(instruction_stsch) },
76 { "instruction_chsc", VCPU_STAT(instruction_chsc) },
Konstantin Weitzb31288f2013-04-17 17:36:29 +020077 { "instruction_essa", VCPU_STAT(instruction_essa) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010078 { "instruction_stsi", VCPU_STAT(instruction_stsi) },
79 { "instruction_stfl", VCPU_STAT(instruction_stfl) },
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +020080 { "instruction_tprot", VCPU_STAT(instruction_tprot) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010081 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
Cornelia Huckbd59d3a2011-11-17 11:00:42 +010082 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020083 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010084 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +020085 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
86 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010087 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +020088 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
89 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
Eric Farmancd7b4b62015-02-12 09:06:34 -050090 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010091 { "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) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +020094 { "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) },
Christian Borntraeger388186b2011-10-30 15:17:03 +010097 { "diagnose_10", VCPU_STAT(diagnose_10) },
Christian Borntraegere28acfe2008-03-25 18:47:34 +010098 { "diagnose_44", VCPU_STAT(diagnose_44) },
Konstantin Weitz41628d32012-04-25 15:30:38 +020099 { "diagnose_9c", VCPU_STAT(diagnose_9c) },
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100100 { NULL }
101};
102
Michael Mueller9d8d5782015-02-02 15:42:51 +0100103/* upper facilities limit for kvm */
104unsigned long kvm_s390_fac_list_mask[] = {
105 0xff82fffbf4fc2000UL,
106 0x005c000000000000UL,
Eric Farman13211ea2014-04-30 13:39:46 -0400107 0x4000000000000000UL,
Michael Mueller9d8d5782015-02-02 15:42:51 +0100108};
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100109
Michael Mueller9d8d5782015-02-02 15:42:51 +0100110unsigned long kvm_s390_fac_list_mask_size(void)
Michael Mueller78c4b59f2013-07-26 15:04:04 +0200111{
Michael Mueller9d8d5782015-02-02 15:42:51 +0100112 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);
Michael Mueller78c4b59f2013-07-26 15:04:04 +0200114}
115
Michael Mueller9d8d5782015-02-02 15:42:51 +0100116static struct gmap_notifier gmap_notifier;
117
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100118/* Section: not file related */
Radim Krčmář13a34e02014-08-28 15:13:03 +0200119int kvm_arch_hardware_enable(void)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100120{
121 /* every s390 is virtualization enabled ;-) */
Alexander Graf10474ae2009-09-15 11:37:46 +0200122 return 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100123}
124
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200125static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address);
126
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100127int kvm_arch_hardware_setup(void)
128{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200129 gmap_notifier.notifier_call = kvm_gmap_notifier;
130 gmap_register_ipte_notifier(&gmap_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100131 return 0;
132}
133
134void kvm_arch_hardware_unsetup(void)
135{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200136 gmap_unregister_ipte_notifier(&gmap_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100137}
138
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100139int kvm_arch_init(void *opaque)
140{
Cornelia Huck84877d92014-09-02 10:27:35 +0100141 /* Register floating interrupt controller interface. */
142 return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100143}
144
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100145/* Section: device related */
146long 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
Alexander Graf784aa3d2014-07-14 18:27:35 +0200154int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100155{
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100156 int r;
157
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200158 switch (ext) {
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100159 case KVM_CAP_S390_PSW:
Christian Borntraegerb6cf8782011-09-20 17:07:29 +0200160 case KVM_CAP_S390_GMAP:
Christian Borntraeger52e16b12011-11-17 11:00:44 +0100161 case KVM_CAP_SYNC_MMU:
Carsten Otte1efd0f52012-01-04 10:25:29 +0100162#ifdef CONFIG_KVM_S390_UCONTROL
163 case KVM_CAP_S390_UCONTROL:
164#endif
Dominik Dingel3c038e62013-10-07 17:11:48 +0200165 case KVM_CAP_ASYNC_PF:
Christian Borntraeger60b413c2012-01-11 11:20:31 +0100166 case KVM_CAP_SYNC_REGS:
Carsten Otte14eebd92012-05-15 14:15:26 +0200167 case KVM_CAP_ONE_REG:
Cornelia Huckd6712df2012-12-20 15:32:11 +0100168 case KVM_CAP_ENABLE_CAP:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100169 case KVM_CAP_S390_CSS_SUPPORT:
Cornelia Huckebc32262014-05-09 15:00:46 +0200170 case KVM_CAP_IRQFD:
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100171 case KVM_CAP_IOEVENTFD:
Jens Freimannc05c4182013-10-07 16:13:45 +0200172 case KVM_CAP_DEVICE_CTRL:
Cornelia Huckd938dc52013-10-23 18:26:34 +0200173 case KVM_CAP_ENABLE_CAP_VM:
Cornelia Huck78599d92014-07-15 09:54:39 +0200174 case KVM_CAP_S390_IRQCHIP:
Dominik Dingelf2061652014-04-09 13:13:00 +0200175 case KVM_CAP_VM_ATTRIBUTES:
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200176 case KVM_CAP_MP_STATE:
David Hildenbrand2444b352014-10-09 14:10:13 +0200177 case KVM_CAP_S390_USER_SIGP:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100178 r = 1;
179 break;
Christian Borntraegere726b1b2012-05-02 10:50:38 +0200180 case KVM_CAP_NR_VCPUS:
181 case KVM_CAP_MAX_VCPUS:
182 r = KVM_MAX_VCPUS;
183 break;
Nick Wange1e2e602013-03-25 17:22:58 +0100184 case KVM_CAP_NR_MEMSLOTS:
185 r = KVM_USER_MEM_SLOTS;
186 break;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200187 case KVM_CAP_S390_COW:
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100188 r = MACHINE_HAS_ESOP;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200189 break;
Eric Farman68c55752014-06-09 10:57:26 -0400190 case KVM_CAP_S390_VECTOR_REGISTERS:
191 r = MACHINE_HAS_VX;
192 break;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200193 default:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100194 r = 0;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200195 }
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100196 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100197}
198
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400199static 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
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100218/* Section: vm related */
219/*
220 * Get (and clear) the dirty memory log for a memory slot.
221 */
222int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
223 struct kvm_dirty_log *log)
224{
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400225 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;
252out:
253 mutex_unlock(&kvm->slots_lock);
254 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100255}
256
Cornelia Huckd938dc52013-10-23 18:26:34 +0200257static 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) {
Cornelia Huck84223592013-07-15 13:36:01 +0200265 case KVM_CAP_S390_IRQCHIP:
266 kvm->arch.use_irqchip = 1;
267 r = 0;
268 break;
David Hildenbrand2444b352014-10-09 14:10:13 +0200269 case KVM_CAP_S390_USER_SIGP:
270 kvm->arch.user_sigp = 1;
271 r = 0;
272 break;
Eric Farman68c55752014-06-09 10:57:26 -0400273 case KVM_CAP_S390_VECTOR_REGISTERS:
274 kvm->arch.use_vectors = MACHINE_HAS_VX;
275 r = MACHINE_HAS_VX ? 0 : -EINVAL;
276 break;
Cornelia Huckd938dc52013-10-23 18:26:34 +0200277 default:
278 r = -EINVAL;
279 break;
280 }
281 return r;
282}
283
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100284static 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
301static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200302{
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);
Dominik Dingela13cff32014-10-23 12:07:14 +0200318 s390_reset_cmma(kvm->arch.gmap->mm);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200319 srcu_read_unlock(&kvm->srcu, idx);
320 mutex_unlock(&kvm->lock);
321 ret = 0;
322 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100323 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 }
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200353 default:
354 ret = -ENXIO;
355 break;
356 }
357 return ret;
358}
359
Tony Krowiaka374e892014-09-03 10:13:53 +0200360static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
361
362static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
363{
364 struct kvm_vcpu *vcpu;
365 int i;
366
Michael Mueller9d8d5782015-02-02 15:42:51 +0100367 if (!test_kvm_facility(kvm, 76))
Tony Krowiaka374e892014-09-03 10:13:53 +0200368 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
Jason J. Herne72f25022014-11-25 09:46:02 -0500407static 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(&gtod_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
421static 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(&gtod, (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
445static 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
466static 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, &gtod_high,
471 sizeof(gtod_high)))
472 return -EFAULT;
473
474 return 0;
475}
476
477static 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, &gtod, sizeof(gtod)))
488 return -EFAULT;
489
490 return 0;
491}
492
493static 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
Michael Mueller658b6ed2015-02-02 15:49:35 +0100514static 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;
Michael Mueller981467c2015-02-24 13:51:04 +0100534 memcpy(kvm->arch.model.fac->list, proc->fac_list,
Michael Mueller658b6ed2015-02-02 15:49:35 +0100535 S390_ARCH_FAC_LIST_SIZE_BYTE);
536 } else
537 ret = -EFAULT;
538 kfree(proc);
539out:
540 mutex_unlock(&kvm->lock);
541 return ret;
542}
543
544static 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
556static 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;
Michael Mueller981467c2015-02-24 13:51:04 +0100568 memcpy(&proc->fac_list, kvm->arch.model.fac->list, S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100569 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
570 ret = -EFAULT;
571 kfree(proc);
572out:
573 return ret;
574}
575
576static 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();
Michael Mueller981467c2015-02-24 13:51:04 +0100588 memcpy(&mach->fac_mask, kvm->arch.model.fac->mask,
589 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100590 memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +0100591 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100592 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
593 ret = -EFAULT;
594 kfree(mach);
595out:
596 return ret;
597}
598
599static 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
Dominik Dingelf2061652014-04-09 13:13:00 +0200614static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
615{
616 int ret;
617
618 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200619 case KVM_S390_VM_MEM_CTRL:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100620 ret = kvm_s390_set_mem_control(kvm, attr);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200621 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500622 case KVM_S390_VM_TOD:
623 ret = kvm_s390_set_tod(kvm, attr);
624 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100625 case KVM_S390_VM_CPU_MODEL:
626 ret = kvm_s390_set_cpu_model(kvm, attr);
627 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200628 case KVM_S390_VM_CRYPTO:
629 ret = kvm_s390_vm_set_crypto(kvm, attr);
630 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200631 default:
632 ret = -ENXIO;
633 break;
634 }
635
636 return ret;
637}
638
639static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
640{
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100641 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;
Jason J. Herne72f25022014-11-25 09:46:02 -0500647 case KVM_S390_VM_TOD:
648 ret = kvm_s390_get_tod(kvm, attr);
649 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100650 case KVM_S390_VM_CPU_MODEL:
651 ret = kvm_s390_get_cpu_model(kvm, attr);
652 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100653 default:
654 ret = -ENXIO;
655 break;
656 }
657
658 return ret;
Dominik Dingelf2061652014-04-09 13:13:00 +0200659}
660
661static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
662{
663 int ret;
664
665 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200666 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:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100670 case KVM_S390_VM_MEM_LIMIT_SIZE:
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200671 ret = 0;
672 break;
673 default:
674 ret = -ENXIO;
675 break;
676 }
677 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500678 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;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100689 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;
Tony Krowiaka374e892014-09-03 10:13:53 +0200700 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;
Dominik Dingelf2061652014-04-09 13:13:00 +0200713 default:
714 ret = -ENXIO;
715 break;
716 }
717
718 return ret;
719}
720
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100721long 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;
Dominik Dingelf2061652014-04-09 13:13:00 +0200726 struct kvm_device_attr attr;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100727 int r;
728
729 switch (ioctl) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100730 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 }
Cornelia Huckd938dc52013-10-23 18:26:34 +0200739 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 }
Cornelia Huck84223592013-07-15 13:36:01 +0200747 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 }
Dominik Dingelf2061652014-04-09 13:13:00 +0200759 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 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100780 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300781 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100782 }
783
784 return r;
785}
786
Tony Krowiak45c9b472015-01-13 11:33:26 -0500787static int kvm_s390_query_ap_config(u8 *config)
788{
789 u32 fcn_code = 0x04000000UL;
Christian Borntraeger86044c82015-02-26 13:53:47 +0100790 u32 cc = 0;
Tony Krowiak45c9b472015-01-13 11:33:26 -0500791
Christian Borntraeger86044c82015-02-26 13:53:47 +0100792 memset(config, 0, 128);
Tony Krowiak45c9b472015-01-13 11:33:26 -0500793 asm volatile(
794 "lgr 0,%1\n"
795 "lgr 2,%2\n"
796 ".long 0xb2af0000\n" /* PQAP(QCI) */
Christian Borntraeger86044c82015-02-26 13:53:47 +0100797 "0: ipm %0\n"
Tony Krowiak45c9b472015-01-13 11:33:26 -0500798 "srl %0,28\n"
Christian Borntraeger86044c82015-02-26 13:53:47 +0100799 "1:\n"
800 EX_TABLE(0b, 1b)
801 : "+r" (cc)
Tony Krowiak45c9b472015-01-13 11:33:26 -0500802 : "r" (fcn_code), "r" (config)
803 : "cc", "0", "2", "memory"
804 );
805
806 return cc;
807}
808
809static 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
826static 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
Michael Mueller9d8d5782015-02-02 15:42:51 +0100836static void kvm_s390_get_cpu_id(struct cpuid *cpu_id)
837{
838 get_cpu_id(cpu_id);
839 cpu_id->version = 0xff;
840}
841
Tony Krowiak5102ee82014-06-27 14:46:01 -0400842static int kvm_s390_crypto_init(struct kvm *kvm)
843{
Michael Mueller9d8d5782015-02-02 15:42:51 +0100844 if (!test_kvm_facility(kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -0400845 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
Tony Krowiak45c9b472015-01-13 11:33:26 -0500852 kvm_s390_set_crycb_format(kvm);
Tony Krowiak5102ee82014-06-27 14:46:01 -0400853
Tony Krowiaked6f76b2015-02-24 14:06:57 -0500854 /* 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));
Tony Krowiaka374e892014-09-03 10:13:53 +0200861
Tony Krowiak5102ee82014-06-27 14:46:01 -0400862 return 0;
863}
864
Carsten Ottee08b9632012-01-04 10:25:20 +0100865int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100866{
Michael Mueller9d8d5782015-02-02 15:42:51 +0100867 int i, rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100868 char debug_name[16];
Christian Borntraegerf6c137f2014-03-19 11:18:29 +0100869 static unsigned long sca_offset;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100870
Carsten Ottee08b9632012-01-04 10:25:20 +0100871 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
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100882 rc = s390_enable_sie();
883 if (rc)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100884 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100885
Carsten Otteb2904112011-10-18 12:27:13 +0200886 rc = -ENOMEM;
887
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100888 kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
889 if (!kvm->arch.sca)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100890 goto out_err;
Christian Borntraegerf6c137f2014-03-19 11:18:29 +0100891 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);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100895
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)
900 goto out_nodbf;
901
Michael Mueller9d8d5782015-02-02 15:42:51 +0100902 /*
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
Michael Mueller981467c2015-02-24 13:51:04 +0100905 * 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
Michael Mueller9d8d5782015-02-02 15:42:51 +0100907 * 31 bits and word aligned.
908 */
909 kvm->arch.model.fac =
Michael Mueller981467c2015-02-24 13:51:04 +0100910 (struct kvm_s390_fac *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Michael Mueller9d8d5782015-02-02 15:42:51 +0100911 if (!kvm->arch.model.fac)
912 goto out_nofac;
913
Michael Muellerfb5bf932015-02-27 14:25:10 +0100914 /* Populate the facility mask initially. */
Michael Mueller981467c2015-02-24 13:51:04 +0100915 memcpy(kvm->arch.model.fac->mask, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +0100916 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller9d8d5782015-02-02 15:42:51 +0100917 for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
918 if (i < kvm_s390_fac_list_mask_size())
Michael Mueller981467c2015-02-24 13:51:04 +0100919 kvm->arch.model.fac->mask[i] &= kvm_s390_fac_list_mask[i];
Michael Mueller9d8d5782015-02-02 15:42:51 +0100920 else
Michael Mueller981467c2015-02-24 13:51:04 +0100921 kvm->arch.model.fac->mask[i] = 0UL;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100922 }
923
Michael Mueller981467c2015-02-24 13:51:04 +0100924 /* 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
Michael Mueller9d8d5782015-02-02 15:42:51 +0100928 kvm_s390_get_cpu_id(&kvm->arch.model.cpu_id);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100929 kvm->arch.model.ibc = sclp_get_ibc() & 0x0fff;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100930
Tony Krowiak5102ee82014-06-27 14:46:01 -0400931 if (kvm_s390_crypto_init(kvm) < 0)
932 goto out_crypto;
933
Carsten Otteba5c1e92008-03-25 18:47:26 +0100934 spin_lock_init(&kvm->arch.float_int.lock);
935 INIT_LIST_HEAD(&kvm->arch.float_int.list);
Heiko Carstens8a2422342014-01-10 14:33:28 +0100936 init_waitqueue_head(&kvm->arch.ipte_wq);
Thomas Hutha6b7e452014-10-01 14:48:42 +0200937 mutex_init(&kvm->arch.ipte_mutex);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100938
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100939 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
940 VM_EVENT(kvm, 3, "%s", "vm created");
941
Carsten Ottee08b9632012-01-04 10:25:20 +0100942 if (type & KVM_VM_S390_UCONTROL) {
943 kvm->arch.gmap = NULL;
944 } else {
Christian Borntraeger03499852014-08-25 12:38:57 +0200945 kvm->arch.gmap = gmap_alloc(current->mm, (1UL << 44) - 1);
Carsten Ottee08b9632012-01-04 10:25:20 +0100946 if (!kvm->arch.gmap)
947 goto out_nogmap;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200948 kvm->arch.gmap->private = kvm;
Dominik Dingel24eb3a82013-06-17 16:25:18 +0200949 kvm->arch.gmap->pfault_enabled = 0;
Carsten Ottee08b9632012-01-04 10:25:20 +0100950 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100951
952 kvm->arch.css_support = 0;
Cornelia Huck84223592013-07-15 13:36:01 +0200953 kvm->arch.use_irqchip = 0;
Eric Farman68c55752014-06-09 10:57:26 -0400954 kvm->arch.use_vectors = 0;
Jason J. Herne72f25022014-11-25 09:46:02 -0500955 kvm->arch.epoch = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100956
David Hildenbrand8ad35752014-03-14 11:00:21 +0100957 spin_lock_init(&kvm->arch.start_stop_lock);
958
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100959 return 0;
Carsten Otte598841c2011-07-24 10:48:21 +0200960out_nogmap:
Tony Krowiak5102ee82014-06-27 14:46:01 -0400961 kfree(kvm->arch.crypto.crycb);
962out_crypto:
Michael Mueller9d8d5782015-02-02 15:42:51 +0100963 free_page((unsigned long)kvm->arch.model.fac);
964out_nofac:
Carsten Otte598841c2011-07-24 10:48:21 +0200965 debug_unregister(kvm->arch.dbf);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100966out_nodbf:
967 free_page((unsigned long)(kvm->arch.sca));
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100968out_err:
969 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100970}
971
Christian Borntraegerd329c032008-11-26 14:50:27 +0100972void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
973{
974 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
Cornelia Huckade38c32012-07-23 17:20:30 +0200975 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
Christian Borntraeger67335e62014-03-25 17:09:08 +0100976 kvm_s390_clear_local_irqs(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200977 kvm_clear_async_pf_completion_queue(vcpu);
Carsten Otte58f94602012-01-04 10:25:27 +0100978 if (!kvm_is_ucontrol(vcpu->kvm)) {
979 clear_bit(63 - vcpu->vcpu_id,
980 (unsigned long *) &vcpu->kvm->arch.sca->mcn);
981 if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
982 (__u64) vcpu->arch.sie_block)
983 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
984 }
Carsten Otteabf4a712009-05-12 17:21:51 +0200985 smp_mb();
Carsten Otte27e03932012-01-04 10:25:21 +0100986
987 if (kvm_is_ucontrol(vcpu->kvm))
988 gmap_free(vcpu->arch.gmap);
989
Dominik Dingelb31605c2014-03-25 13:47:11 +0100990 if (kvm_s390_cmma_enabled(vcpu->kvm))
991 kvm_s390_vcpu_unsetup_cmma(vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +0100992 free_page((unsigned long)(vcpu->arch.sie_block));
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200993
Christian Borntraeger6692cef2008-11-26 14:51:08 +0100994 kvm_vcpu_uninit(vcpu);
Michael Muellerb110fea2013-06-12 13:54:54 +0200995 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +0100996}
997
998static void kvm_free_vcpus(struct kvm *kvm)
999{
1000 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001001 struct kvm_vcpu *vcpu;
Christian Borntraegerd329c032008-11-26 14:50:27 +01001002
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001003 kvm_for_each_vcpu(i, vcpu, kvm)
1004 kvm_arch_vcpu_destroy(vcpu);
1005
1006 mutex_lock(&kvm->lock);
1007 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1008 kvm->vcpus[i] = NULL;
1009
1010 atomic_set(&kvm->online_vcpus, 0);
1011 mutex_unlock(&kvm->lock);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001012}
1013
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001014void kvm_arch_destroy_vm(struct kvm *kvm)
1015{
Christian Borntraegerd329c032008-11-26 14:50:27 +01001016 kvm_free_vcpus(kvm);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001017 free_page((unsigned long)kvm->arch.model.fac);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001018 free_page((unsigned long)(kvm->arch.sca));
Christian Borntraegerd329c032008-11-26 14:50:27 +01001019 debug_unregister(kvm->arch.dbf);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001020 kfree(kvm->arch.crypto.crycb);
Carsten Otte27e03932012-01-04 10:25:21 +01001021 if (!kvm_is_ucontrol(kvm))
1022 gmap_free(kvm->arch.gmap);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001023 kvm_s390_destroy_adapters(kvm);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001024 kvm_s390_clear_float_irqs(kvm);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001025}
1026
1027/* Section: vcpu related */
Dominik Dingeldafd0322014-12-02 16:53:21 +01001028static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1029{
1030 vcpu->arch.gmap = gmap_alloc(current->mm, -1UL);
1031 if (!vcpu->arch.gmap)
1032 return -ENOMEM;
1033 vcpu->arch.gmap->private = vcpu->kvm;
1034
1035 return 0;
1036}
1037
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001038int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1039{
Dominik Dingel3c038e62013-10-07 17:11:48 +02001040 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1041 kvm_clear_async_pf_completion_queue(vcpu);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001042 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
1043 KVM_SYNC_GPRS |
Christian Borntraeger9eed07352012-02-06 10:59:07 +01001044 KVM_SYNC_ACRS |
David Hildenbrandb028ee32014-07-17 10:47:43 +02001045 KVM_SYNC_CRS |
1046 KVM_SYNC_ARCH0 |
1047 KVM_SYNC_PFAULT;
Eric Farman68c55752014-06-09 10:57:26 -04001048 if (test_kvm_facility(vcpu->kvm, 129))
1049 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
Dominik Dingeldafd0322014-12-02 16:53:21 +01001050
1051 if (kvm_is_ucontrol(vcpu->kvm))
1052 return __kvm_ucontrol_vcpu_init(vcpu);
1053
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001054 return 0;
1055}
1056
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001057void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1058{
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001059 save_fp_ctl(&vcpu->arch.host_fpregs.fpc);
Eric Farman68c55752014-06-09 10:57:26 -04001060 if (vcpu->kvm->arch.use_vectors)
1061 save_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs);
1062 else
1063 save_fp_regs(vcpu->arch.host_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001064 save_access_regs(vcpu->arch.host_acrs);
Eric Farman68c55752014-06-09 10:57:26 -04001065 if (vcpu->kvm->arch.use_vectors) {
1066 restore_fp_ctl(&vcpu->run->s.regs.fpc);
1067 restore_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
1068 } else {
1069 restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1070 restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
1071 }
Christian Borntraeger59674c12012-01-11 11:20:33 +01001072 restore_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001073 gmap_enable(vcpu->arch.gmap);
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001074 atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001075}
1076
1077void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1078{
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001079 atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001080 gmap_disable(vcpu->arch.gmap);
Eric Farman68c55752014-06-09 10:57:26 -04001081 if (vcpu->kvm->arch.use_vectors) {
1082 save_fp_ctl(&vcpu->run->s.regs.fpc);
1083 save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
1084 } else {
1085 save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1086 save_fp_regs(vcpu->arch.guest_fpregs.fprs);
1087 }
Christian Borntraeger59674c12012-01-11 11:20:33 +01001088 save_access_regs(vcpu->run->s.regs.acrs);
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001089 restore_fp_ctl(&vcpu->arch.host_fpregs.fpc);
Eric Farman68c55752014-06-09 10:57:26 -04001090 if (vcpu->kvm->arch.use_vectors)
1091 restore_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs);
1092 else
1093 restore_fp_regs(vcpu->arch.host_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001094 restore_access_regs(vcpu->arch.host_acrs);
1095}
1096
1097static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1098{
1099 /* this equals initial cpu reset in pop, but we don't switch to ESA */
1100 vcpu->arch.sie_block->gpsw.mask = 0UL;
1101 vcpu->arch.sie_block->gpsw.addr = 0UL;
Christian Borntraeger8d26cf72012-01-11 11:19:32 +01001102 kvm_s390_set_prefix(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001103 vcpu->arch.sie_block->cputm = 0UL;
1104 vcpu->arch.sie_block->ckc = 0UL;
1105 vcpu->arch.sie_block->todpr = 0;
1106 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1107 vcpu->arch.sie_block->gcr[0] = 0xE0UL;
1108 vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
1109 vcpu->arch.guest_fpregs.fpc = 0;
1110 asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
1111 vcpu->arch.sie_block->gbea = 1;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001112 vcpu->arch.sie_block->pp = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001113 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1114 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001115 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
1116 kvm_s390_vcpu_stop(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001117 kvm_s390_clear_local_irqs(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001118}
1119
Dominik Dingel31928aa2014-12-04 15:47:07 +01001120void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001121{
Jason J. Herne72f25022014-11-25 09:46:02 -05001122 mutex_lock(&vcpu->kvm->lock);
1123 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
1124 mutex_unlock(&vcpu->kvm->lock);
Dominik Dingeldafd0322014-12-02 16:53:21 +01001125 if (!kvm_is_ucontrol(vcpu->kvm))
1126 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001127}
1128
Tony Krowiak5102ee82014-06-27 14:46:01 -04001129static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
1130{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001131 if (!test_kvm_facility(vcpu->kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -04001132 return;
1133
Tony Krowiaka374e892014-09-03 10:13:53 +02001134 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1135
1136 if (vcpu->kvm->arch.crypto.aes_kw)
1137 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1138 if (vcpu->kvm->arch.crypto.dea_kw)
1139 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1140
Tony Krowiak5102ee82014-06-27 14:46:01 -04001141 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
1142}
1143
Dominik Dingelb31605c2014-03-25 13:47:11 +01001144void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1145{
1146 free_page(vcpu->arch.sie_block->cbrlo);
1147 vcpu->arch.sie_block->cbrlo = 0;
1148}
1149
1150int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1151{
1152 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1153 if (!vcpu->arch.sie_block->cbrlo)
1154 return -ENOMEM;
1155
1156 vcpu->arch.sie_block->ecb2 |= 0x80;
1157 vcpu->arch.sie_block->ecb2 &= ~0x08;
1158 return 0;
1159}
1160
Michael Mueller91520f12015-02-27 14:32:11 +01001161static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
1162{
1163 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
1164
1165 vcpu->arch.cpu_id = model->cpu_id;
1166 vcpu->arch.sie_block->ibc = model->ibc;
1167 vcpu->arch.sie_block->fac = (int) (long) model->fac->list;
1168}
1169
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001170int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1171{
Dominik Dingelb31605c2014-03-25 13:47:11 +01001172 int rc = 0;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001173
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001174 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
1175 CPUSTAT_SM |
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02001176 CPUSTAT_STOPPED |
1177 CPUSTAT_GED);
Michael Mueller91520f12015-02-27 14:32:11 +01001178 kvm_s390_vcpu_setup_model(vcpu);
1179
Christian Borntraegerfc345312010-06-17 23:16:20 +02001180 vcpu->arch.sie_block->ecb = 6;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001181 if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73))
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001182 vcpu->arch.sie_block->ecb |= 0x10;
1183
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02001184 vcpu->arch.sie_block->ecb2 = 8;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001185 vcpu->arch.sie_block->eca = 0xC1002000U;
Heiko Carstens217a4402013-12-30 12:54:14 +01001186 if (sclp_has_siif())
1187 vcpu->arch.sie_block->eca |= 1;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001188 if (sclp_has_sigpif())
1189 vcpu->arch.sie_block->eca |= 0x10000000U;
Eric Farman13211ea2014-04-30 13:39:46 -04001190 if (vcpu->kvm->arch.use_vectors) {
1191 vcpu->arch.sie_block->eca |= 0x00020000;
1192 vcpu->arch.sie_block->ecd |= 0x20000000;
1193 }
Thomas Huth492d8642015-02-10 16:11:01 +01001194 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
Matthew Rosato5a5e6532013-01-29 11:48:20 -05001195
Dominik Dingelb31605c2014-03-25 13:47:11 +01001196 if (kvm_s390_cmma_enabled(vcpu->kvm)) {
1197 rc = kvm_s390_vcpu_setup_cmma(vcpu);
1198 if (rc)
1199 return rc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001200 }
David Hildenbrand0ac96ca2014-12-12 15:17:31 +01001201 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Christian Borntraegerca872302009-05-12 17:21:49 +02001202 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001203
Tony Krowiak5102ee82014-06-27 14:46:01 -04001204 kvm_s390_vcpu_crypto_setup(vcpu);
1205
Dominik Dingelb31605c2014-03-25 13:47:11 +01001206 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001207}
1208
1209struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1210 unsigned int id)
1211{
Carsten Otte4d475552011-10-18 12:27:12 +02001212 struct kvm_vcpu *vcpu;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001213 struct sie_page *sie_page;
Carsten Otte4d475552011-10-18 12:27:12 +02001214 int rc = -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001215
Carsten Otte4d475552011-10-18 12:27:12 +02001216 if (id >= KVM_MAX_VCPUS)
1217 goto out;
1218
1219 rc = -ENOMEM;
1220
Michael Muellerb110fea2013-06-12 13:54:54 +02001221 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001222 if (!vcpu)
Carsten Otte4d475552011-10-18 12:27:12 +02001223 goto out;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001224
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001225 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
1226 if (!sie_page)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001227 goto out_free_cpu;
1228
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001229 vcpu->arch.sie_block = &sie_page->sie_block;
1230 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
Eric Farman68c55752014-06-09 10:57:26 -04001231 vcpu->arch.host_vregs = &sie_page->vregs;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001232
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001233 vcpu->arch.sie_block->icpua = id;
Carsten Otte58f94602012-01-04 10:25:27 +01001234 if (!kvm_is_ucontrol(kvm)) {
1235 if (!kvm->arch.sca) {
1236 WARN_ON_ONCE(1);
1237 goto out_free_cpu;
1238 }
1239 if (!kvm->arch.sca->cpu[id].sda)
1240 kvm->arch.sca->cpu[id].sda =
1241 (__u64) vcpu->arch.sie_block;
1242 vcpu->arch.sie_block->scaoh =
1243 (__u32)(((__u64)kvm->arch.sca) >> 32);
1244 vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
1245 set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
1246 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001247
Carsten Otteba5c1e92008-03-25 18:47:26 +01001248 spin_lock_init(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001249 vcpu->arch.local_int.float_int = &kvm->arch.float_int;
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001250 vcpu->arch.local_int.wq = &vcpu->wq;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01001251 vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001252
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001253 rc = kvm_vcpu_init(vcpu, kvm, id);
1254 if (rc)
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001255 goto out_free_sie_block;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001256 VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
1257 vcpu->arch.sie_block);
Cornelia Huckade38c32012-07-23 17:20:30 +02001258 trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001259
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001260 return vcpu;
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001261out_free_sie_block:
1262 free_page((unsigned long)(vcpu->arch.sie_block));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001263out_free_cpu:
Michael Muellerb110fea2013-06-12 13:54:54 +02001264 kmem_cache_free(kvm_vcpu_cache, vcpu);
Carsten Otte4d475552011-10-18 12:27:12 +02001265out:
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001266 return ERR_PTR(rc);
1267}
1268
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001269int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1270{
David Hildenbrand9a022062014-08-05 17:40:47 +02001271 return kvm_s390_vcpu_has_irq(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001272}
1273
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001274void s390_vcpu_block(struct kvm_vcpu *vcpu)
1275{
1276 atomic_set_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
1277}
1278
1279void s390_vcpu_unblock(struct kvm_vcpu *vcpu)
1280{
1281 atomic_clear_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
1282}
1283
1284/*
1285 * Kick a guest cpu out of SIE and wait until SIE is not running.
1286 * If the CPU is not running (e.g. waiting as idle) the function will
1287 * return immediately. */
1288void exit_sie(struct kvm_vcpu *vcpu)
1289{
1290 atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
1291 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
1292 cpu_relax();
1293}
1294
1295/* Kick a guest cpu out of SIE and prevent SIE-reentry */
1296void exit_sie_sync(struct kvm_vcpu *vcpu)
1297{
1298 s390_vcpu_block(vcpu);
1299 exit_sie(vcpu);
1300}
1301
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001302static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
1303{
1304 int i;
1305 struct kvm *kvm = gmap->private;
1306 struct kvm_vcpu *vcpu;
1307
1308 kvm_for_each_vcpu(i, vcpu, kvm) {
1309 /* match against both prefix pages */
Michael Muellerfda902c2014-05-13 16:58:30 +02001310 if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001311 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
1312 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
1313 exit_sie_sync(vcpu);
1314 }
1315 }
1316}
1317
Christoffer Dallb6d33832012-03-08 16:44:24 -05001318int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1319{
1320 /* kvm common code refers to this, but never calls it */
1321 BUG();
1322 return 0;
1323}
1324
Carsten Otte14eebd92012-05-15 14:15:26 +02001325static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
1326 struct kvm_one_reg *reg)
1327{
1328 int r = -EINVAL;
1329
1330 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001331 case KVM_REG_S390_TODPR:
1332 r = put_user(vcpu->arch.sie_block->todpr,
1333 (u32 __user *)reg->addr);
1334 break;
1335 case KVM_REG_S390_EPOCHDIFF:
1336 r = put_user(vcpu->arch.sie_block->epoch,
1337 (u64 __user *)reg->addr);
1338 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001339 case KVM_REG_S390_CPU_TIMER:
1340 r = put_user(vcpu->arch.sie_block->cputm,
1341 (u64 __user *)reg->addr);
1342 break;
1343 case KVM_REG_S390_CLOCK_COMP:
1344 r = put_user(vcpu->arch.sie_block->ckc,
1345 (u64 __user *)reg->addr);
1346 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001347 case KVM_REG_S390_PFTOKEN:
1348 r = put_user(vcpu->arch.pfault_token,
1349 (u64 __user *)reg->addr);
1350 break;
1351 case KVM_REG_S390_PFCOMPARE:
1352 r = put_user(vcpu->arch.pfault_compare,
1353 (u64 __user *)reg->addr);
1354 break;
1355 case KVM_REG_S390_PFSELECT:
1356 r = put_user(vcpu->arch.pfault_select,
1357 (u64 __user *)reg->addr);
1358 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001359 case KVM_REG_S390_PP:
1360 r = put_user(vcpu->arch.sie_block->pp,
1361 (u64 __user *)reg->addr);
1362 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001363 case KVM_REG_S390_GBEA:
1364 r = put_user(vcpu->arch.sie_block->gbea,
1365 (u64 __user *)reg->addr);
1366 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001367 default:
1368 break;
1369 }
1370
1371 return r;
1372}
1373
1374static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
1375 struct kvm_one_reg *reg)
1376{
1377 int r = -EINVAL;
1378
1379 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001380 case KVM_REG_S390_TODPR:
1381 r = get_user(vcpu->arch.sie_block->todpr,
1382 (u32 __user *)reg->addr);
1383 break;
1384 case KVM_REG_S390_EPOCHDIFF:
1385 r = get_user(vcpu->arch.sie_block->epoch,
1386 (u64 __user *)reg->addr);
1387 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001388 case KVM_REG_S390_CPU_TIMER:
1389 r = get_user(vcpu->arch.sie_block->cputm,
1390 (u64 __user *)reg->addr);
1391 break;
1392 case KVM_REG_S390_CLOCK_COMP:
1393 r = get_user(vcpu->arch.sie_block->ckc,
1394 (u64 __user *)reg->addr);
1395 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001396 case KVM_REG_S390_PFTOKEN:
1397 r = get_user(vcpu->arch.pfault_token,
1398 (u64 __user *)reg->addr);
David Hildenbrand9fbd8082014-10-09 15:01:38 +02001399 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1400 kvm_clear_async_pf_completion_queue(vcpu);
Dominik Dingel536336c2013-09-30 10:55:33 +02001401 break;
1402 case KVM_REG_S390_PFCOMPARE:
1403 r = get_user(vcpu->arch.pfault_compare,
1404 (u64 __user *)reg->addr);
1405 break;
1406 case KVM_REG_S390_PFSELECT:
1407 r = get_user(vcpu->arch.pfault_select,
1408 (u64 __user *)reg->addr);
1409 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001410 case KVM_REG_S390_PP:
1411 r = get_user(vcpu->arch.sie_block->pp,
1412 (u64 __user *)reg->addr);
1413 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001414 case KVM_REG_S390_GBEA:
1415 r = get_user(vcpu->arch.sie_block->gbea,
1416 (u64 __user *)reg->addr);
1417 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001418 default:
1419 break;
1420 }
1421
1422 return r;
1423}
Christoffer Dallb6d33832012-03-08 16:44:24 -05001424
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001425static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
1426{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001427 kvm_s390_vcpu_initial_reset(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001428 return 0;
1429}
1430
1431int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1432{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001433 memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001434 return 0;
1435}
1436
1437int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1438{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001439 memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001440 return 0;
1441}
1442
1443int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1444 struct kvm_sregs *sregs)
1445{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001446 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001447 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
Christian Borntraeger59674c12012-01-11 11:20:33 +01001448 restore_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001449 return 0;
1450}
1451
1452int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1453 struct kvm_sregs *sregs)
1454{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001455 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001456 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001457 return 0;
1458}
1459
1460int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1461{
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001462 if (test_fp_ctl(fpu->fpc))
1463 return -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001464 memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001465 vcpu->arch.guest_fpregs.fpc = fpu->fpc;
1466 restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1467 restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001468 return 0;
1469}
1470
1471int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1472{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001473 memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
1474 fpu->fpc = vcpu->arch.guest_fpregs.fpc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001475 return 0;
1476}
1477
1478static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
1479{
1480 int rc = 0;
1481
David Hildenbrand7a42fdc2014-05-05 16:26:19 +02001482 if (!is_vcpu_stopped(vcpu))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001483 rc = -EBUSY;
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001484 else {
1485 vcpu->run->psw_mask = psw.mask;
1486 vcpu->run->psw_addr = psw.addr;
1487 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001488 return rc;
1489}
1490
1491int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1492 struct kvm_translation *tr)
1493{
1494 return -EINVAL; /* not implemented yet */
1495}
1496
David Hildenbrand27291e22014-01-23 12:26:52 +01001497#define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
1498 KVM_GUESTDBG_USE_HW_BP | \
1499 KVM_GUESTDBG_ENABLE)
1500
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001501int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1502 struct kvm_guest_debug *dbg)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001503{
David Hildenbrand27291e22014-01-23 12:26:52 +01001504 int rc = 0;
1505
1506 vcpu->guest_debug = 0;
1507 kvm_s390_clear_bp_data(vcpu);
1508
David Hildenbrand2de3bfc2014-05-20 17:25:20 +02001509 if (dbg->control & ~VALID_GUESTDBG_FLAGS)
David Hildenbrand27291e22014-01-23 12:26:52 +01001510 return -EINVAL;
1511
1512 if (dbg->control & KVM_GUESTDBG_ENABLE) {
1513 vcpu->guest_debug = dbg->control;
1514 /* enforce guest PER */
1515 atomic_set_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1516
1517 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
1518 rc = kvm_s390_import_bp_data(vcpu, dbg);
1519 } else {
1520 atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1521 vcpu->arch.guestdbg.last_bp = 0;
1522 }
1523
1524 if (rc) {
1525 vcpu->guest_debug = 0;
1526 kvm_s390_clear_bp_data(vcpu);
1527 atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1528 }
1529
1530 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001531}
1532
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001533int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1534 struct kvm_mp_state *mp_state)
1535{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001536 /* CHECK_STOP and LOAD are not supported yet */
1537 return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
1538 KVM_MP_STATE_OPERATING;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001539}
1540
1541int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1542 struct kvm_mp_state *mp_state)
1543{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001544 int rc = 0;
1545
1546 /* user space knows about this interface - let it control the state */
1547 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
1548
1549 switch (mp_state->mp_state) {
1550 case KVM_MP_STATE_STOPPED:
1551 kvm_s390_vcpu_stop(vcpu);
1552 break;
1553 case KVM_MP_STATE_OPERATING:
1554 kvm_s390_vcpu_start(vcpu);
1555 break;
1556 case KVM_MP_STATE_LOAD:
1557 case KVM_MP_STATE_CHECK_STOP:
1558 /* fall through - CHECK_STOP and LOAD are not supported yet */
1559 default:
1560 rc = -ENXIO;
1561 }
1562
1563 return rc;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001564}
1565
Dominik Dingelb31605c2014-03-25 13:47:11 +01001566bool kvm_s390_cmma_enabled(struct kvm *kvm)
1567{
1568 if (!MACHINE_IS_LPAR)
1569 return false;
1570 /* only enable for z10 and later */
1571 if (!MACHINE_HAS_EDAT1)
1572 return false;
1573 if (!kvm->arch.use_cmma)
1574 return false;
1575 return true;
1576}
1577
David Hildenbrand8ad35752014-03-14 11:00:21 +01001578static bool ibs_enabled(struct kvm_vcpu *vcpu)
1579{
1580 return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
1581}
1582
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001583static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
1584{
David Hildenbrand8ad35752014-03-14 11:00:21 +01001585retry:
1586 s390_vcpu_unblock(vcpu);
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001587 /*
1588 * We use MMU_RELOAD just to re-arm the ipte notifier for the
1589 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
1590 * This ensures that the ipte instruction for this request has
1591 * already finished. We might race against a second unmapper that
1592 * wants to set the blocking bit. Lets just retry the request loop.
1593 */
David Hildenbrand8ad35752014-03-14 11:00:21 +01001594 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001595 int rc;
1596 rc = gmap_ipte_notify(vcpu->arch.gmap,
Michael Muellerfda902c2014-05-13 16:58:30 +02001597 kvm_s390_get_prefix(vcpu),
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001598 PAGE_SIZE * 2);
1599 if (rc)
1600 return rc;
David Hildenbrand8ad35752014-03-14 11:00:21 +01001601 goto retry;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001602 }
David Hildenbrand8ad35752014-03-14 11:00:21 +01001603
David Hildenbrandd3d692c2014-07-29 08:53:36 +02001604 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
1605 vcpu->arch.sie_block->ihcpu = 0xffff;
1606 goto retry;
1607 }
1608
David Hildenbrand8ad35752014-03-14 11:00:21 +01001609 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
1610 if (!ibs_enabled(vcpu)) {
1611 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
1612 atomic_set_mask(CPUSTAT_IBS,
1613 &vcpu->arch.sie_block->cpuflags);
1614 }
1615 goto retry;
1616 }
1617
1618 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
1619 if (ibs_enabled(vcpu)) {
1620 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
1621 atomic_clear_mask(CPUSTAT_IBS,
1622 &vcpu->arch.sie_block->cpuflags);
1623 }
1624 goto retry;
1625 }
1626
David Hildenbrand0759d062014-05-13 16:54:32 +02001627 /* nothing to do, just clear the request */
1628 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
1629
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001630 return 0;
1631}
1632
Thomas Huthfa576c52014-05-06 17:20:16 +02001633/**
1634 * kvm_arch_fault_in_page - fault-in guest page if necessary
1635 * @vcpu: The corresponding virtual cpu
1636 * @gpa: Guest physical address
1637 * @writable: Whether the page should be writable or not
1638 *
1639 * Make sure that a guest page has been faulted-in on the host.
1640 *
1641 * Return: Zero on success, negative error code otherwise.
1642 */
1643long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001644{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001645 return gmap_fault(vcpu->arch.gmap, gpa,
1646 writable ? FAULT_FLAG_WRITE : 0);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001647}
1648
Dominik Dingel3c038e62013-10-07 17:11:48 +02001649static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
1650 unsigned long token)
1651{
1652 struct kvm_s390_interrupt inti;
Jens Freimann383d0b02014-07-29 15:11:49 +02001653 struct kvm_s390_irq irq;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001654
1655 if (start_token) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001656 irq.u.ext.ext_params2 = token;
1657 irq.type = KVM_S390_INT_PFAULT_INIT;
1658 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
Dominik Dingel3c038e62013-10-07 17:11:48 +02001659 } else {
1660 inti.type = KVM_S390_INT_PFAULT_DONE;
Jens Freimann383d0b02014-07-29 15:11:49 +02001661 inti.parm64 = token;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001662 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
1663 }
1664}
1665
1666void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
1667 struct kvm_async_pf *work)
1668{
1669 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
1670 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
1671}
1672
1673void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
1674 struct kvm_async_pf *work)
1675{
1676 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
1677 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
1678}
1679
1680void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
1681 struct kvm_async_pf *work)
1682{
1683 /* s390 will always inject the page directly */
1684}
1685
1686bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
1687{
1688 /*
1689 * s390 will always inject the page directly,
1690 * but we still want check_async_completion to cleanup
1691 */
1692 return true;
1693}
1694
1695static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
1696{
1697 hva_t hva;
1698 struct kvm_arch_async_pf arch;
1699 int rc;
1700
1701 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1702 return 0;
1703 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
1704 vcpu->arch.pfault_compare)
1705 return 0;
1706 if (psw_extint_disabled(vcpu))
1707 return 0;
David Hildenbrand9a022062014-08-05 17:40:47 +02001708 if (kvm_s390_vcpu_has_irq(vcpu, 0))
Dominik Dingel3c038e62013-10-07 17:11:48 +02001709 return 0;
1710 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
1711 return 0;
1712 if (!vcpu->arch.gmap->pfault_enabled)
1713 return 0;
1714
Heiko Carstens81480cc2014-01-01 16:36:07 +01001715 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
1716 hva += current->thread.gmap_addr & ~PAGE_MASK;
1717 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
Dominik Dingel3c038e62013-10-07 17:11:48 +02001718 return 0;
1719
1720 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
1721 return rc;
1722}
1723
Thomas Huth3fb4c402013-09-12 10:33:43 +02001724static int vcpu_pre_run(struct kvm_vcpu *vcpu)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001725{
Thomas Huth3fb4c402013-09-12 10:33:43 +02001726 int rc, cpuflags;
Carsten Ottee168bf82012-01-04 10:25:22 +01001727
Dominik Dingel3c038e62013-10-07 17:11:48 +02001728 /*
1729 * On s390 notifications for arriving pages will be delivered directly
1730 * to the guest but the house keeping for completed pfaults is
1731 * handled outside the worker.
1732 */
1733 kvm_check_async_pf_completion(vcpu);
1734
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001735 memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001736
1737 if (need_resched())
1738 schedule();
1739
Martin Schwidefskyd3a73ac2014-04-15 12:55:07 +02001740 if (test_cpu_flag(CIF_MCCK_PENDING))
Christian Borntraeger71cde582008-05-21 13:37:34 +02001741 s390_handle_mcck();
1742
Jens Freimann79395032014-04-17 10:10:30 +02001743 if (!kvm_is_ucontrol(vcpu->kvm)) {
1744 rc = kvm_s390_deliver_pending_interrupts(vcpu);
1745 if (rc)
1746 return rc;
1747 }
Carsten Otte0ff31862008-05-21 13:37:37 +02001748
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001749 rc = kvm_s390_handle_requests(vcpu);
1750 if (rc)
1751 return rc;
1752
David Hildenbrand27291e22014-01-23 12:26:52 +01001753 if (guestdbg_enabled(vcpu)) {
1754 kvm_s390_backup_guest_per_regs(vcpu);
1755 kvm_s390_patch_guest_per_regs(vcpu);
1756 }
1757
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001758 vcpu->arch.sie_block->icptcode = 0;
Thomas Huth3fb4c402013-09-12 10:33:43 +02001759 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
1760 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
1761 trace_kvm_s390_sie_enter(vcpu, cpuflags);
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02001762
Thomas Huth3fb4c402013-09-12 10:33:43 +02001763 return 0;
1764}
1765
Thomas Huth492d8642015-02-10 16:11:01 +01001766static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
1767{
1768 psw_t *psw = &vcpu->arch.sie_block->gpsw;
1769 u8 opcode;
1770 int rc;
1771
1772 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
1773 trace_kvm_s390_sie_fault(vcpu);
1774
1775 /*
1776 * We want to inject an addressing exception, which is defined as a
1777 * suppressing or terminating exception. However, since we came here
1778 * by a DAT access exception, the PSW still points to the faulting
1779 * instruction since DAT exceptions are nullifying. So we've got
1780 * to look up the current opcode to get the length of the instruction
1781 * to be able to forward the PSW.
1782 */
1783 rc = read_guest(vcpu, psw->addr, &opcode, 1);
1784 if (rc)
1785 return kvm_s390_inject_prog_cond(vcpu, rc);
1786 psw->addr = __rewind_psw(*psw, -insn_length(opcode));
1787
1788 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1789}
1790
Thomas Huth3fb4c402013-09-12 10:33:43 +02001791static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
1792{
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001793 int rc = -1;
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02001794
1795 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
1796 vcpu->arch.sie_block->icptcode);
1797 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
1798
David Hildenbrand27291e22014-01-23 12:26:52 +01001799 if (guestdbg_enabled(vcpu))
1800 kvm_s390_restore_guest_per_regs(vcpu);
1801
Thomas Huth3fb4c402013-09-12 10:33:43 +02001802 if (exit_reason >= 0) {
Martin Schwidefsky7c470532013-05-17 14:41:37 +02001803 rc = 0;
Thomas Huth210b16072013-09-19 16:26:18 +02001804 } else if (kvm_is_ucontrol(vcpu->kvm)) {
1805 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
1806 vcpu->run->s390_ucontrol.trans_exc_code =
1807 current->thread.gmap_addr;
1808 vcpu->run->s390_ucontrol.pgm_code = 0x10;
1809 rc = -EREMOTE;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001810
1811 } else if (current->thread.gmap_pfault) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001812 trace_kvm_s390_major_guest_pfault(vcpu);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001813 current->thread.gmap_pfault = 0;
Thomas Huthfa576c52014-05-06 17:20:16 +02001814 if (kvm_arch_setup_async_pf(vcpu)) {
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001815 rc = 0;
Thomas Huthfa576c52014-05-06 17:20:16 +02001816 } else {
1817 gpa_t gpa = current->thread.gmap_addr;
1818 rc = kvm_arch_fault_in_page(vcpu, gpa, 1);
1819 }
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001820 }
1821
Thomas Huth492d8642015-02-10 16:11:01 +01001822 if (rc == -1)
1823 rc = vcpu_post_run_fault_in_sie(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001824
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001825 memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
Thomas Huth3fb4c402013-09-12 10:33:43 +02001826
Thomas Hutha76ccff2013-09-12 10:33:44 +02001827 if (rc == 0) {
1828 if (kvm_is_ucontrol(vcpu->kvm))
Christian Borntraeger2955c832014-03-06 16:01:38 +01001829 /* Don't exit for host interrupts. */
1830 rc = vcpu->arch.sie_block->icptcode ? -EOPNOTSUPP : 0;
Thomas Hutha76ccff2013-09-12 10:33:44 +02001831 else
1832 rc = kvm_handle_sie_intercept(vcpu);
1833 }
1834
Thomas Huth3fb4c402013-09-12 10:33:43 +02001835 return rc;
1836}
1837
1838static int __vcpu_run(struct kvm_vcpu *vcpu)
1839{
1840 int rc, exit_reason;
1841
Thomas Huth800c1062013-09-12 10:33:45 +02001842 /*
1843 * We try to hold kvm->srcu during most of vcpu_run (except when run-
1844 * ning the guest), so that memslots (and other stuff) are protected
1845 */
1846 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1847
Thomas Hutha76ccff2013-09-12 10:33:44 +02001848 do {
1849 rc = vcpu_pre_run(vcpu);
1850 if (rc)
1851 break;
Thomas Huth3fb4c402013-09-12 10:33:43 +02001852
Thomas Huth800c1062013-09-12 10:33:45 +02001853 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Thomas Hutha76ccff2013-09-12 10:33:44 +02001854 /*
1855 * As PF_VCPU will be used in fault handler, between
1856 * guest_enter and guest_exit should be no uaccess.
1857 */
1858 preempt_disable();
1859 kvm_guest_enter();
1860 preempt_enable();
1861 exit_reason = sie64a(vcpu->arch.sie_block,
1862 vcpu->run->s.regs.gprs);
1863 kvm_guest_exit();
Thomas Huth800c1062013-09-12 10:33:45 +02001864 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02001865
Thomas Hutha76ccff2013-09-12 10:33:44 +02001866 rc = vcpu_post_run(vcpu, exit_reason);
David Hildenbrand27291e22014-01-23 12:26:52 +01001867 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
Thomas Huth3fb4c402013-09-12 10:33:43 +02001868
Thomas Huth800c1062013-09-12 10:33:45 +02001869 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Carsten Ottee168bf82012-01-04 10:25:22 +01001870 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001871}
1872
David Hildenbrandb028ee32014-07-17 10:47:43 +02001873static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1874{
1875 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
1876 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
1877 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
1878 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
1879 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
1880 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
David Hildenbrandd3d692c2014-07-29 08:53:36 +02001881 /* some control register changes require a tlb flush */
1882 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02001883 }
1884 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
1885 vcpu->arch.sie_block->cputm = kvm_run->s.regs.cputm;
1886 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
1887 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
1888 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
1889 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
1890 }
1891 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
1892 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
1893 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
1894 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
David Hildenbrand9fbd8082014-10-09 15:01:38 +02001895 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1896 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02001897 }
1898 kvm_run->kvm_dirty_regs = 0;
1899}
1900
1901static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1902{
1903 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
1904 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
1905 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
1906 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
1907 kvm_run->s.regs.cputm = vcpu->arch.sie_block->cputm;
1908 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
1909 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
1910 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
1911 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
1912 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
1913 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
1914 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
1915}
1916
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001917int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1918{
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001919 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001920 sigset_t sigsaved;
1921
David Hildenbrand27291e22014-01-23 12:26:52 +01001922 if (guestdbg_exit_pending(vcpu)) {
1923 kvm_s390_prepare_debug_exit(vcpu);
1924 return 0;
1925 }
1926
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001927 if (vcpu->sigset_active)
1928 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1929
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001930 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
1931 kvm_s390_vcpu_start(vcpu);
1932 } else if (is_vcpu_stopped(vcpu)) {
1933 pr_err_ratelimited("kvm-s390: can't run stopped vcpu %d\n",
1934 vcpu->vcpu_id);
1935 return -EINVAL;
1936 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001937
David Hildenbrandb028ee32014-07-17 10:47:43 +02001938 sync_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001939
Heiko Carstensdab4079d2009-06-12 10:26:32 +02001940 might_fault();
Thomas Hutha76ccff2013-09-12 10:33:44 +02001941 rc = __vcpu_run(vcpu);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +02001942
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02001943 if (signal_pending(current) && !rc) {
1944 kvm_run->exit_reason = KVM_EXIT_INTR;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001945 rc = -EINTR;
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02001946 }
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001947
David Hildenbrand27291e22014-01-23 12:26:52 +01001948 if (guestdbg_exit_pending(vcpu) && !rc) {
1949 kvm_s390_prepare_debug_exit(vcpu);
1950 rc = 0;
1951 }
1952
Heiko Carstensb8e660b2010-02-26 22:37:41 +01001953 if (rc == -EOPNOTSUPP) {
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001954 /* intercept cannot be handled in-kernel, prepare kvm-run */
1955 kvm_run->exit_reason = KVM_EXIT_S390_SIEIC;
1956 kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001957 kvm_run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
1958 kvm_run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
1959 rc = 0;
1960 }
1961
1962 if (rc == -EREMOTE) {
1963 /* intercept was handled, but userspace support is needed
1964 * kvm_run has been prepared by the handler */
1965 rc = 0;
1966 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001967
David Hildenbrandb028ee32014-07-17 10:47:43 +02001968 store_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001969
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001970 if (vcpu->sigset_active)
1971 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1972
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001973 vcpu->stat.exit_userspace++;
Heiko Carstens7e8e6ab2008-04-04 15:12:35 +02001974 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001975}
1976
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001977/*
1978 * store status at address
1979 * we use have two special cases:
1980 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
1981 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
1982 */
Heiko Carstensd0bce602014-01-01 16:45:58 +01001983int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001984{
Carsten Otte092670c2011-07-24 10:48:22 +02001985 unsigned char archmode = 1;
Michael Muellerfda902c2014-05-13 16:58:30 +02001986 unsigned int px;
Thomas Huth178bd782013-11-13 20:28:18 +01001987 u64 clkcomp;
Heiko Carstensd0bce602014-01-01 16:45:58 +01001988 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001989
Heiko Carstensd0bce602014-01-01 16:45:58 +01001990 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
1991 if (write_guest_abs(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001992 return -EFAULT;
Heiko Carstensd0bce602014-01-01 16:45:58 +01001993 gpa = SAVE_AREA_BASE;
1994 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
1995 if (write_guest_real(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001996 return -EFAULT;
Heiko Carstensd0bce602014-01-01 16:45:58 +01001997 gpa = kvm_s390_real_to_abs(vcpu, SAVE_AREA_BASE);
1998 }
1999 rc = write_guest_abs(vcpu, gpa + offsetof(struct save_area, fp_regs),
2000 vcpu->arch.guest_fpregs.fprs, 128);
2001 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, gp_regs),
2002 vcpu->run->s.regs.gprs, 128);
2003 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, psw),
2004 &vcpu->arch.sie_block->gpsw, 16);
Michael Muellerfda902c2014-05-13 16:58:30 +02002005 px = kvm_s390_get_prefix(vcpu);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002006 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, pref_reg),
Michael Muellerfda902c2014-05-13 16:58:30 +02002007 &px, 4);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002008 rc |= write_guest_abs(vcpu,
2009 gpa + offsetof(struct save_area, fp_ctrl_reg),
2010 &vcpu->arch.guest_fpregs.fpc, 4);
2011 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, tod_reg),
2012 &vcpu->arch.sie_block->todpr, 4);
2013 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, timer),
2014 &vcpu->arch.sie_block->cputm, 8);
Thomas Huth178bd782013-11-13 20:28:18 +01002015 clkcomp = vcpu->arch.sie_block->ckc >> 8;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002016 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, clk_cmp),
2017 &clkcomp, 8);
2018 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, acc_regs),
2019 &vcpu->run->s.regs.acrs, 64);
2020 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, ctrl_regs),
2021 &vcpu->arch.sie_block->gcr, 128);
2022 return rc ? -EFAULT : 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002023}
2024
Thomas Huthe8798922013-11-06 15:46:33 +01002025int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2026{
2027 /*
2028 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
2029 * copying in vcpu load/put. Lets update our copies before we save
2030 * it into the save area
2031 */
2032 save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
2033 save_fp_regs(vcpu->arch.guest_fpregs.fprs);
2034 save_access_regs(vcpu->run->s.regs.acrs);
2035
2036 return kvm_s390_store_status_unloaded(vcpu, addr);
2037}
2038
Eric Farmanbc17de72014-04-14 16:01:09 -04002039/*
2040 * store additional status at address
2041 */
2042int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
2043 unsigned long gpa)
2044{
2045 /* Only bits 0-53 are used for address formation */
2046 if (!(gpa & ~0x3ff))
2047 return 0;
2048
2049 return write_guest_abs(vcpu, gpa & ~0x3ff,
2050 (void *)&vcpu->run->s.regs.vrs, 512);
2051}
2052
2053int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr)
2054{
2055 if (!test_kvm_facility(vcpu->kvm, 129))
2056 return 0;
2057
2058 /*
2059 * The guest VXRS are in the host VXRs due to the lazy
2060 * copying in vcpu load/put. Let's update our copies before we save
2061 * it into the save area.
2062 */
2063 save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
2064
2065 return kvm_s390_store_adtl_status_unloaded(vcpu, addr);
2066}
2067
David Hildenbrand8ad35752014-03-14 11:00:21 +01002068static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2069{
2070 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
2071 kvm_make_request(KVM_REQ_DISABLE_IBS, vcpu);
2072 exit_sie_sync(vcpu);
2073}
2074
2075static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
2076{
2077 unsigned int i;
2078 struct kvm_vcpu *vcpu;
2079
2080 kvm_for_each_vcpu(i, vcpu, kvm) {
2081 __disable_ibs_on_vcpu(vcpu);
2082 }
2083}
2084
2085static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2086{
2087 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
2088 kvm_make_request(KVM_REQ_ENABLE_IBS, vcpu);
2089 exit_sie_sync(vcpu);
2090}
2091
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002092void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
2093{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002094 int i, online_vcpus, started_vcpus = 0;
2095
2096 if (!is_vcpu_stopped(vcpu))
2097 return;
2098
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002099 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002100 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002101 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002102 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2103
2104 for (i = 0; i < online_vcpus; i++) {
2105 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
2106 started_vcpus++;
2107 }
2108
2109 if (started_vcpus == 0) {
2110 /* we're the only active VCPU -> speed it up */
2111 __enable_ibs_on_vcpu(vcpu);
2112 } else if (started_vcpus == 1) {
2113 /*
2114 * As we are starting a second VCPU, we have to disable
2115 * the IBS facility on all VCPUs to remove potentially
2116 * oustanding ENABLE requests.
2117 */
2118 __disable_ibs_on_all_vcpus(vcpu->kvm);
2119 }
2120
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002121 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002122 /*
2123 * Another VCPU might have used IBS while we were offline.
2124 * Let's play safe and flush the VCPU at startup.
2125 */
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002126 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002127 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002128 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002129}
2130
2131void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
2132{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002133 int i, online_vcpus, started_vcpus = 0;
2134 struct kvm_vcpu *started_vcpu = NULL;
2135
2136 if (is_vcpu_stopped(vcpu))
2137 return;
2138
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002139 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002140 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002141 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002142 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2143
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002144 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
David Hildenbrand6cddd432014-10-15 16:48:53 +02002145 kvm_s390_clear_stop_irq(vcpu);
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002146
David Hildenbrand6cddd432014-10-15 16:48:53 +02002147 atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002148 __disable_ibs_on_vcpu(vcpu);
2149
2150 for (i = 0; i < online_vcpus; i++) {
2151 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
2152 started_vcpus++;
2153 started_vcpu = vcpu->kvm->vcpus[i];
2154 }
2155 }
2156
2157 if (started_vcpus == 1) {
2158 /*
2159 * As we only have one VCPU left, we want to enable the
2160 * IBS facility for that VCPU to speed it up.
2161 */
2162 __enable_ibs_on_vcpu(started_vcpu);
2163 }
2164
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002165 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002166 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002167}
2168
Cornelia Huckd6712df2012-12-20 15:32:11 +01002169static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2170 struct kvm_enable_cap *cap)
2171{
2172 int r;
2173
2174 if (cap->flags)
2175 return -EINVAL;
2176
2177 switch (cap->cap) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002178 case KVM_CAP_S390_CSS_SUPPORT:
2179 if (!vcpu->kvm->arch.css_support) {
2180 vcpu->kvm->arch.css_support = 1;
2181 trace_kvm_s390_enable_css(vcpu->kvm);
2182 }
2183 r = 0;
2184 break;
Cornelia Huckd6712df2012-12-20 15:32:11 +01002185 default:
2186 r = -EINVAL;
2187 break;
2188 }
2189 return r;
2190}
2191
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002192long kvm_arch_vcpu_ioctl(struct file *filp,
2193 unsigned int ioctl, unsigned long arg)
2194{
2195 struct kvm_vcpu *vcpu = filp->private_data;
2196 void __user *argp = (void __user *)arg;
Thomas Huth800c1062013-09-12 10:33:45 +02002197 int idx;
Avi Kivitybc923cc2010-05-13 12:21:46 +03002198 long r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002199
Avi Kivity937366242010-05-13 12:35:17 +03002200 switch (ioctl) {
2201 case KVM_S390_INTERRUPT: {
Carsten Otteba5c1e92008-03-25 18:47:26 +01002202 struct kvm_s390_interrupt s390int;
Jens Freimann383d0b02014-07-29 15:11:49 +02002203 struct kvm_s390_irq s390irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002204
Avi Kivity937366242010-05-13 12:35:17 +03002205 r = -EFAULT;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002206 if (copy_from_user(&s390int, argp, sizeof(s390int)))
Avi Kivity937366242010-05-13 12:35:17 +03002207 break;
Jens Freimann383d0b02014-07-29 15:11:49 +02002208 if (s390int_to_s390irq(&s390int, &s390irq))
2209 return -EINVAL;
2210 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
Avi Kivity937366242010-05-13 12:35:17 +03002211 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002212 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002213 case KVM_S390_STORE_STATUS:
Thomas Huth800c1062013-09-12 10:33:45 +02002214 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002215 r = kvm_s390_vcpu_store_status(vcpu, arg);
Thomas Huth800c1062013-09-12 10:33:45 +02002216 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002217 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002218 case KVM_S390_SET_INITIAL_PSW: {
2219 psw_t psw;
2220
Avi Kivitybc923cc2010-05-13 12:21:46 +03002221 r = -EFAULT;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002222 if (copy_from_user(&psw, argp, sizeof(psw)))
Avi Kivitybc923cc2010-05-13 12:21:46 +03002223 break;
2224 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
2225 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002226 }
2227 case KVM_S390_INITIAL_RESET:
Avi Kivitybc923cc2010-05-13 12:21:46 +03002228 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
2229 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02002230 case KVM_SET_ONE_REG:
2231 case KVM_GET_ONE_REG: {
2232 struct kvm_one_reg reg;
2233 r = -EFAULT;
2234 if (copy_from_user(&reg, argp, sizeof(reg)))
2235 break;
2236 if (ioctl == KVM_SET_ONE_REG)
2237 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
2238 else
2239 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
2240 break;
2241 }
Carsten Otte27e03932012-01-04 10:25:21 +01002242#ifdef CONFIG_KVM_S390_UCONTROL
2243 case KVM_S390_UCAS_MAP: {
2244 struct kvm_s390_ucas_mapping ucasmap;
2245
2246 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2247 r = -EFAULT;
2248 break;
2249 }
2250
2251 if (!kvm_is_ucontrol(vcpu->kvm)) {
2252 r = -EINVAL;
2253 break;
2254 }
2255
2256 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
2257 ucasmap.vcpu_addr, ucasmap.length);
2258 break;
2259 }
2260 case KVM_S390_UCAS_UNMAP: {
2261 struct kvm_s390_ucas_mapping ucasmap;
2262
2263 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2264 r = -EFAULT;
2265 break;
2266 }
2267
2268 if (!kvm_is_ucontrol(vcpu->kvm)) {
2269 r = -EINVAL;
2270 break;
2271 }
2272
2273 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
2274 ucasmap.length);
2275 break;
2276 }
2277#endif
Carsten Otteccc79102012-01-04 10:25:26 +01002278 case KVM_S390_VCPU_FAULT: {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002279 r = gmap_fault(vcpu->arch.gmap, arg, 0);
Carsten Otteccc79102012-01-04 10:25:26 +01002280 break;
2281 }
Cornelia Huckd6712df2012-12-20 15:32:11 +01002282 case KVM_ENABLE_CAP:
2283 {
2284 struct kvm_enable_cap cap;
2285 r = -EFAULT;
2286 if (copy_from_user(&cap, argp, sizeof(cap)))
2287 break;
2288 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2289 break;
2290 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002291 default:
Carsten Otte3e6afcf2012-01-04 10:25:30 +01002292 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002293 }
Avi Kivitybc923cc2010-05-13 12:21:46 +03002294 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002295}
2296
Carsten Otte5b1c1492012-01-04 10:25:23 +01002297int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2298{
2299#ifdef CONFIG_KVM_S390_UCONTROL
2300 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
2301 && (kvm_is_ucontrol(vcpu->kvm))) {
2302 vmf->page = virt_to_page(vcpu->arch.sie_block);
2303 get_page(vmf->page);
2304 return 0;
2305 }
2306#endif
2307 return VM_FAULT_SIGBUS;
2308}
2309
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05302310int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2311 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09002312{
2313 return 0;
2314}
2315
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002316/* Section: memory related */
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002317int kvm_arch_prepare_memory_region(struct kvm *kvm,
2318 struct kvm_memory_slot *memslot,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09002319 struct kvm_userspace_memory_region *mem,
2320 enum kvm_mr_change change)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002321{
Nick Wangdd2887e2013-03-25 17:22:57 +01002322 /* A few sanity checks. We can have memory slots which have to be
2323 located/ended at a segment boundary (1MB). The memory in userland is
2324 ok to be fragmented into various different vmas. It is okay to mmap()
2325 and munmap() stuff in this slot after doing this call at any time */
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002326
Carsten Otte598841c2011-07-24 10:48:21 +02002327 if (mem->userspace_addr & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002328 return -EINVAL;
2329
Carsten Otte598841c2011-07-24 10:48:21 +02002330 if (mem->memory_size & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002331 return -EINVAL;
2332
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002333 return 0;
2334}
2335
2336void kvm_arch_commit_memory_region(struct kvm *kvm,
2337 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002338 const struct kvm_memory_slot *old,
2339 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002340{
Carsten Ottef7850c92011-07-24 10:48:23 +02002341 int rc;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002342
Christian Borntraeger2cef4de2013-03-25 17:22:48 +01002343 /* If the basics of the memslot do not change, we do not want
2344 * to update the gmap. Every update causes several unnecessary
2345 * segment translation exceptions. This is usually handled just
2346 * fine by the normal fault handler + gmap, but it will also
2347 * cause faults on the prefix page of running guest CPUs.
2348 */
2349 if (old->userspace_addr == mem->userspace_addr &&
2350 old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
2351 old->npages * PAGE_SIZE == mem->memory_size)
2352 return;
Carsten Otte598841c2011-07-24 10:48:21 +02002353
2354 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
2355 mem->guest_phys_addr, mem->memory_size);
2356 if (rc)
Carsten Ottef7850c92011-07-24 10:48:23 +02002357 printk(KERN_WARNING "kvm-s390: failed to commit memory region\n");
Carsten Otte598841c2011-07-24 10:48:21 +02002358 return;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002359}
2360
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002361static int __init kvm_s390_init(void)
2362{
Michael Mueller9d8d5782015-02-02 15:42:51 +01002363 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002364}
2365
2366static void __exit kvm_s390_exit(void)
2367{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002368 kvm_exit();
2369}
2370
2371module_init(kvm_s390_init);
2372module_exit(kvm_s390_exit);
Cornelia Huck566af942013-05-27 18:42:33 +02002373
2374/*
2375 * Enable autoloading of the kvm module.
2376 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
2377 * since x86 takes a different approach.
2378 */
2379#include <linux/miscdevice.h>
2380MODULE_ALIAS_MISCDEV(KVM_MINOR);
2381MODULE_ALIAS("devname:kvm");