blob: 610e90afadf29dd5d28735b45931b23d662350e1 [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)
Dominik Dingel40f5b732015-03-12 13:55:53 +0100900 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100901
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)
Dominik Dingel40f5b732015-03-12 13:55:53 +0100912 goto out_err;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100913
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)
Dominik Dingel40f5b732015-03-12 13:55:53 +0100932 goto out_err;
Tony Krowiak5102ee82014-06-27 14:46:01 -0400933
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)
Dominik Dingel40f5b732015-03-12 13:55:53 +0100947 goto out_err;
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;
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100960out_err:
Dominik Dingel40f5b732015-03-12 13:55:53 +0100961 kfree(kvm->arch.crypto.crycb);
962 free_page((unsigned long)kvm->arch.model.fac);
963 debug_unregister(kvm->arch.dbf);
964 free_page((unsigned long)(kvm->arch.sca));
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100965 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100966}
967
Christian Borntraegerd329c032008-11-26 14:50:27 +0100968void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
969{
970 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
Cornelia Huckade38c32012-07-23 17:20:30 +0200971 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
Christian Borntraeger67335e62014-03-25 17:09:08 +0100972 kvm_s390_clear_local_irqs(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200973 kvm_clear_async_pf_completion_queue(vcpu);
Carsten Otte58f94602012-01-04 10:25:27 +0100974 if (!kvm_is_ucontrol(vcpu->kvm)) {
975 clear_bit(63 - vcpu->vcpu_id,
976 (unsigned long *) &vcpu->kvm->arch.sca->mcn);
977 if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
978 (__u64) vcpu->arch.sie_block)
979 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
980 }
Carsten Otteabf4a712009-05-12 17:21:51 +0200981 smp_mb();
Carsten Otte27e03932012-01-04 10:25:21 +0100982
983 if (kvm_is_ucontrol(vcpu->kvm))
984 gmap_free(vcpu->arch.gmap);
985
Dominik Dingelb31605c2014-03-25 13:47:11 +0100986 if (kvm_s390_cmma_enabled(vcpu->kvm))
987 kvm_s390_vcpu_unsetup_cmma(vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +0100988 free_page((unsigned long)(vcpu->arch.sie_block));
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200989
Christian Borntraeger6692cef2008-11-26 14:51:08 +0100990 kvm_vcpu_uninit(vcpu);
Michael Muellerb110fea2013-06-12 13:54:54 +0200991 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +0100992}
993
994static void kvm_free_vcpus(struct kvm *kvm)
995{
996 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300997 struct kvm_vcpu *vcpu;
Christian Borntraegerd329c032008-11-26 14:50:27 +0100998
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300999 kvm_for_each_vcpu(i, vcpu, kvm)
1000 kvm_arch_vcpu_destroy(vcpu);
1001
1002 mutex_lock(&kvm->lock);
1003 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1004 kvm->vcpus[i] = NULL;
1005
1006 atomic_set(&kvm->online_vcpus, 0);
1007 mutex_unlock(&kvm->lock);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001008}
1009
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001010void kvm_arch_destroy_vm(struct kvm *kvm)
1011{
Christian Borntraegerd329c032008-11-26 14:50:27 +01001012 kvm_free_vcpus(kvm);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001013 free_page((unsigned long)kvm->arch.model.fac);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001014 free_page((unsigned long)(kvm->arch.sca));
Christian Borntraegerd329c032008-11-26 14:50:27 +01001015 debug_unregister(kvm->arch.dbf);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001016 kfree(kvm->arch.crypto.crycb);
Carsten Otte27e03932012-01-04 10:25:21 +01001017 if (!kvm_is_ucontrol(kvm))
1018 gmap_free(kvm->arch.gmap);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001019 kvm_s390_destroy_adapters(kvm);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001020 kvm_s390_clear_float_irqs(kvm);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001021}
1022
1023/* Section: vcpu related */
Dominik Dingeldafd0322014-12-02 16:53:21 +01001024static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1025{
1026 vcpu->arch.gmap = gmap_alloc(current->mm, -1UL);
1027 if (!vcpu->arch.gmap)
1028 return -ENOMEM;
1029 vcpu->arch.gmap->private = vcpu->kvm;
1030
1031 return 0;
1032}
1033
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001034int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1035{
Dominik Dingel3c038e62013-10-07 17:11:48 +02001036 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1037 kvm_clear_async_pf_completion_queue(vcpu);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001038 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
1039 KVM_SYNC_GPRS |
Christian Borntraeger9eed07352012-02-06 10:59:07 +01001040 KVM_SYNC_ACRS |
David Hildenbrandb028ee32014-07-17 10:47:43 +02001041 KVM_SYNC_CRS |
1042 KVM_SYNC_ARCH0 |
1043 KVM_SYNC_PFAULT;
Eric Farman68c55752014-06-09 10:57:26 -04001044 if (test_kvm_facility(vcpu->kvm, 129))
1045 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
Dominik Dingeldafd0322014-12-02 16:53:21 +01001046
1047 if (kvm_is_ucontrol(vcpu->kvm))
1048 return __kvm_ucontrol_vcpu_init(vcpu);
1049
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001050 return 0;
1051}
1052
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001053void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1054{
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001055 save_fp_ctl(&vcpu->arch.host_fpregs.fpc);
Eric Farman68c55752014-06-09 10:57:26 -04001056 if (vcpu->kvm->arch.use_vectors)
1057 save_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs);
1058 else
1059 save_fp_regs(vcpu->arch.host_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001060 save_access_regs(vcpu->arch.host_acrs);
Eric Farman68c55752014-06-09 10:57:26 -04001061 if (vcpu->kvm->arch.use_vectors) {
1062 restore_fp_ctl(&vcpu->run->s.regs.fpc);
1063 restore_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
1064 } else {
1065 restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1066 restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
1067 }
Christian Borntraeger59674c12012-01-11 11:20:33 +01001068 restore_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001069 gmap_enable(vcpu->arch.gmap);
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001070 atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001071}
1072
1073void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1074{
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001075 atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001076 gmap_disable(vcpu->arch.gmap);
Eric Farman68c55752014-06-09 10:57:26 -04001077 if (vcpu->kvm->arch.use_vectors) {
1078 save_fp_ctl(&vcpu->run->s.regs.fpc);
1079 save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
1080 } else {
1081 save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1082 save_fp_regs(vcpu->arch.guest_fpregs.fprs);
1083 }
Christian Borntraeger59674c12012-01-11 11:20:33 +01001084 save_access_regs(vcpu->run->s.regs.acrs);
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001085 restore_fp_ctl(&vcpu->arch.host_fpregs.fpc);
Eric Farman68c55752014-06-09 10:57:26 -04001086 if (vcpu->kvm->arch.use_vectors)
1087 restore_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs);
1088 else
1089 restore_fp_regs(vcpu->arch.host_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001090 restore_access_regs(vcpu->arch.host_acrs);
1091}
1092
1093static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1094{
1095 /* this equals initial cpu reset in pop, but we don't switch to ESA */
1096 vcpu->arch.sie_block->gpsw.mask = 0UL;
1097 vcpu->arch.sie_block->gpsw.addr = 0UL;
Christian Borntraeger8d26cf72012-01-11 11:19:32 +01001098 kvm_s390_set_prefix(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001099 vcpu->arch.sie_block->cputm = 0UL;
1100 vcpu->arch.sie_block->ckc = 0UL;
1101 vcpu->arch.sie_block->todpr = 0;
1102 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1103 vcpu->arch.sie_block->gcr[0] = 0xE0UL;
1104 vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
1105 vcpu->arch.guest_fpregs.fpc = 0;
1106 asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
1107 vcpu->arch.sie_block->gbea = 1;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001108 vcpu->arch.sie_block->pp = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001109 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1110 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001111 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
1112 kvm_s390_vcpu_stop(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001113 kvm_s390_clear_local_irqs(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001114}
1115
Dominik Dingel31928aa2014-12-04 15:47:07 +01001116void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001117{
Jason J. Herne72f25022014-11-25 09:46:02 -05001118 mutex_lock(&vcpu->kvm->lock);
1119 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
1120 mutex_unlock(&vcpu->kvm->lock);
Dominik Dingeldafd0322014-12-02 16:53:21 +01001121 if (!kvm_is_ucontrol(vcpu->kvm))
1122 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001123}
1124
Tony Krowiak5102ee82014-06-27 14:46:01 -04001125static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
1126{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001127 if (!test_kvm_facility(vcpu->kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -04001128 return;
1129
Tony Krowiaka374e892014-09-03 10:13:53 +02001130 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1131
1132 if (vcpu->kvm->arch.crypto.aes_kw)
1133 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1134 if (vcpu->kvm->arch.crypto.dea_kw)
1135 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1136
Tony Krowiak5102ee82014-06-27 14:46:01 -04001137 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
1138}
1139
Dominik Dingelb31605c2014-03-25 13:47:11 +01001140void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1141{
1142 free_page(vcpu->arch.sie_block->cbrlo);
1143 vcpu->arch.sie_block->cbrlo = 0;
1144}
1145
1146int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1147{
1148 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1149 if (!vcpu->arch.sie_block->cbrlo)
1150 return -ENOMEM;
1151
1152 vcpu->arch.sie_block->ecb2 |= 0x80;
1153 vcpu->arch.sie_block->ecb2 &= ~0x08;
1154 return 0;
1155}
1156
Michael Mueller91520f12015-02-27 14:32:11 +01001157static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
1158{
1159 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
1160
1161 vcpu->arch.cpu_id = model->cpu_id;
1162 vcpu->arch.sie_block->ibc = model->ibc;
1163 vcpu->arch.sie_block->fac = (int) (long) model->fac->list;
1164}
1165
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001166int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1167{
Dominik Dingelb31605c2014-03-25 13:47:11 +01001168 int rc = 0;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001169
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001170 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
1171 CPUSTAT_SM |
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02001172 CPUSTAT_STOPPED |
1173 CPUSTAT_GED);
Michael Mueller91520f12015-02-27 14:32:11 +01001174 kvm_s390_vcpu_setup_model(vcpu);
1175
Christian Borntraegerfc345312010-06-17 23:16:20 +02001176 vcpu->arch.sie_block->ecb = 6;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001177 if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73))
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001178 vcpu->arch.sie_block->ecb |= 0x10;
1179
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02001180 vcpu->arch.sie_block->ecb2 = 8;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001181 vcpu->arch.sie_block->eca = 0xC1002000U;
Heiko Carstens217a4402013-12-30 12:54:14 +01001182 if (sclp_has_siif())
1183 vcpu->arch.sie_block->eca |= 1;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001184 if (sclp_has_sigpif())
1185 vcpu->arch.sie_block->eca |= 0x10000000U;
Eric Farman13211ea2014-04-30 13:39:46 -04001186 if (vcpu->kvm->arch.use_vectors) {
1187 vcpu->arch.sie_block->eca |= 0x00020000;
1188 vcpu->arch.sie_block->ecd |= 0x20000000;
1189 }
Thomas Huth492d8642015-02-10 16:11:01 +01001190 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
Matthew Rosato5a5e6532013-01-29 11:48:20 -05001191
Dominik Dingelb31605c2014-03-25 13:47:11 +01001192 if (kvm_s390_cmma_enabled(vcpu->kvm)) {
1193 rc = kvm_s390_vcpu_setup_cmma(vcpu);
1194 if (rc)
1195 return rc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001196 }
David Hildenbrand0ac96ca2014-12-12 15:17:31 +01001197 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Christian Borntraegerca872302009-05-12 17:21:49 +02001198 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001199
Tony Krowiak5102ee82014-06-27 14:46:01 -04001200 kvm_s390_vcpu_crypto_setup(vcpu);
1201
Dominik Dingelb31605c2014-03-25 13:47:11 +01001202 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001203}
1204
1205struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1206 unsigned int id)
1207{
Carsten Otte4d475552011-10-18 12:27:12 +02001208 struct kvm_vcpu *vcpu;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001209 struct sie_page *sie_page;
Carsten Otte4d475552011-10-18 12:27:12 +02001210 int rc = -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001211
Carsten Otte4d475552011-10-18 12:27:12 +02001212 if (id >= KVM_MAX_VCPUS)
1213 goto out;
1214
1215 rc = -ENOMEM;
1216
Michael Muellerb110fea2013-06-12 13:54:54 +02001217 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001218 if (!vcpu)
Carsten Otte4d475552011-10-18 12:27:12 +02001219 goto out;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001220
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001221 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
1222 if (!sie_page)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001223 goto out_free_cpu;
1224
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001225 vcpu->arch.sie_block = &sie_page->sie_block;
1226 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
Eric Farman68c55752014-06-09 10:57:26 -04001227 vcpu->arch.host_vregs = &sie_page->vregs;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001228
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001229 vcpu->arch.sie_block->icpua = id;
Carsten Otte58f94602012-01-04 10:25:27 +01001230 if (!kvm_is_ucontrol(kvm)) {
1231 if (!kvm->arch.sca) {
1232 WARN_ON_ONCE(1);
1233 goto out_free_cpu;
1234 }
1235 if (!kvm->arch.sca->cpu[id].sda)
1236 kvm->arch.sca->cpu[id].sda =
1237 (__u64) vcpu->arch.sie_block;
1238 vcpu->arch.sie_block->scaoh =
1239 (__u32)(((__u64)kvm->arch.sca) >> 32);
1240 vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
1241 set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
1242 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001243
Carsten Otteba5c1e92008-03-25 18:47:26 +01001244 spin_lock_init(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001245 vcpu->arch.local_int.float_int = &kvm->arch.float_int;
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001246 vcpu->arch.local_int.wq = &vcpu->wq;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01001247 vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001248
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001249 rc = kvm_vcpu_init(vcpu, kvm, id);
1250 if (rc)
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001251 goto out_free_sie_block;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001252 VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
1253 vcpu->arch.sie_block);
Cornelia Huckade38c32012-07-23 17:20:30 +02001254 trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001255
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001256 return vcpu;
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001257out_free_sie_block:
1258 free_page((unsigned long)(vcpu->arch.sie_block));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001259out_free_cpu:
Michael Muellerb110fea2013-06-12 13:54:54 +02001260 kmem_cache_free(kvm_vcpu_cache, vcpu);
Carsten Otte4d475552011-10-18 12:27:12 +02001261out:
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001262 return ERR_PTR(rc);
1263}
1264
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001265int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1266{
David Hildenbrand9a022062014-08-05 17:40:47 +02001267 return kvm_s390_vcpu_has_irq(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001268}
1269
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001270void s390_vcpu_block(struct kvm_vcpu *vcpu)
1271{
1272 atomic_set_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
1273}
1274
1275void s390_vcpu_unblock(struct kvm_vcpu *vcpu)
1276{
1277 atomic_clear_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
1278}
1279
1280/*
1281 * Kick a guest cpu out of SIE and wait until SIE is not running.
1282 * If the CPU is not running (e.g. waiting as idle) the function will
1283 * return immediately. */
1284void exit_sie(struct kvm_vcpu *vcpu)
1285{
1286 atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
1287 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
1288 cpu_relax();
1289}
1290
1291/* Kick a guest cpu out of SIE and prevent SIE-reentry */
1292void exit_sie_sync(struct kvm_vcpu *vcpu)
1293{
1294 s390_vcpu_block(vcpu);
1295 exit_sie(vcpu);
1296}
1297
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001298static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
1299{
1300 int i;
1301 struct kvm *kvm = gmap->private;
1302 struct kvm_vcpu *vcpu;
1303
1304 kvm_for_each_vcpu(i, vcpu, kvm) {
1305 /* match against both prefix pages */
Michael Muellerfda902c2014-05-13 16:58:30 +02001306 if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001307 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
1308 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
1309 exit_sie_sync(vcpu);
1310 }
1311 }
1312}
1313
Christoffer Dallb6d33832012-03-08 16:44:24 -05001314int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1315{
1316 /* kvm common code refers to this, but never calls it */
1317 BUG();
1318 return 0;
1319}
1320
Carsten Otte14eebd92012-05-15 14:15:26 +02001321static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
1322 struct kvm_one_reg *reg)
1323{
1324 int r = -EINVAL;
1325
1326 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001327 case KVM_REG_S390_TODPR:
1328 r = put_user(vcpu->arch.sie_block->todpr,
1329 (u32 __user *)reg->addr);
1330 break;
1331 case KVM_REG_S390_EPOCHDIFF:
1332 r = put_user(vcpu->arch.sie_block->epoch,
1333 (u64 __user *)reg->addr);
1334 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001335 case KVM_REG_S390_CPU_TIMER:
1336 r = put_user(vcpu->arch.sie_block->cputm,
1337 (u64 __user *)reg->addr);
1338 break;
1339 case KVM_REG_S390_CLOCK_COMP:
1340 r = put_user(vcpu->arch.sie_block->ckc,
1341 (u64 __user *)reg->addr);
1342 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001343 case KVM_REG_S390_PFTOKEN:
1344 r = put_user(vcpu->arch.pfault_token,
1345 (u64 __user *)reg->addr);
1346 break;
1347 case KVM_REG_S390_PFCOMPARE:
1348 r = put_user(vcpu->arch.pfault_compare,
1349 (u64 __user *)reg->addr);
1350 break;
1351 case KVM_REG_S390_PFSELECT:
1352 r = put_user(vcpu->arch.pfault_select,
1353 (u64 __user *)reg->addr);
1354 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001355 case KVM_REG_S390_PP:
1356 r = put_user(vcpu->arch.sie_block->pp,
1357 (u64 __user *)reg->addr);
1358 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001359 case KVM_REG_S390_GBEA:
1360 r = put_user(vcpu->arch.sie_block->gbea,
1361 (u64 __user *)reg->addr);
1362 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001363 default:
1364 break;
1365 }
1366
1367 return r;
1368}
1369
1370static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
1371 struct kvm_one_reg *reg)
1372{
1373 int r = -EINVAL;
1374
1375 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001376 case KVM_REG_S390_TODPR:
1377 r = get_user(vcpu->arch.sie_block->todpr,
1378 (u32 __user *)reg->addr);
1379 break;
1380 case KVM_REG_S390_EPOCHDIFF:
1381 r = get_user(vcpu->arch.sie_block->epoch,
1382 (u64 __user *)reg->addr);
1383 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001384 case KVM_REG_S390_CPU_TIMER:
1385 r = get_user(vcpu->arch.sie_block->cputm,
1386 (u64 __user *)reg->addr);
1387 break;
1388 case KVM_REG_S390_CLOCK_COMP:
1389 r = get_user(vcpu->arch.sie_block->ckc,
1390 (u64 __user *)reg->addr);
1391 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001392 case KVM_REG_S390_PFTOKEN:
1393 r = get_user(vcpu->arch.pfault_token,
1394 (u64 __user *)reg->addr);
David Hildenbrand9fbd8082014-10-09 15:01:38 +02001395 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1396 kvm_clear_async_pf_completion_queue(vcpu);
Dominik Dingel536336c2013-09-30 10:55:33 +02001397 break;
1398 case KVM_REG_S390_PFCOMPARE:
1399 r = get_user(vcpu->arch.pfault_compare,
1400 (u64 __user *)reg->addr);
1401 break;
1402 case KVM_REG_S390_PFSELECT:
1403 r = get_user(vcpu->arch.pfault_select,
1404 (u64 __user *)reg->addr);
1405 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001406 case KVM_REG_S390_PP:
1407 r = get_user(vcpu->arch.sie_block->pp,
1408 (u64 __user *)reg->addr);
1409 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001410 case KVM_REG_S390_GBEA:
1411 r = get_user(vcpu->arch.sie_block->gbea,
1412 (u64 __user *)reg->addr);
1413 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001414 default:
1415 break;
1416 }
1417
1418 return r;
1419}
Christoffer Dallb6d33832012-03-08 16:44:24 -05001420
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001421static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
1422{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001423 kvm_s390_vcpu_initial_reset(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001424 return 0;
1425}
1426
1427int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1428{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001429 memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001430 return 0;
1431}
1432
1433int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1434{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001435 memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001436 return 0;
1437}
1438
1439int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1440 struct kvm_sregs *sregs)
1441{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001442 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001443 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
Christian Borntraeger59674c12012-01-11 11:20:33 +01001444 restore_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001445 return 0;
1446}
1447
1448int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1449 struct kvm_sregs *sregs)
1450{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001451 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001452 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001453 return 0;
1454}
1455
1456int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1457{
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001458 if (test_fp_ctl(fpu->fpc))
1459 return -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001460 memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001461 vcpu->arch.guest_fpregs.fpc = fpu->fpc;
1462 restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1463 restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001464 return 0;
1465}
1466
1467int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1468{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001469 memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
1470 fpu->fpc = vcpu->arch.guest_fpregs.fpc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001471 return 0;
1472}
1473
1474static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
1475{
1476 int rc = 0;
1477
David Hildenbrand7a42fdc2014-05-05 16:26:19 +02001478 if (!is_vcpu_stopped(vcpu))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001479 rc = -EBUSY;
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001480 else {
1481 vcpu->run->psw_mask = psw.mask;
1482 vcpu->run->psw_addr = psw.addr;
1483 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001484 return rc;
1485}
1486
1487int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1488 struct kvm_translation *tr)
1489{
1490 return -EINVAL; /* not implemented yet */
1491}
1492
David Hildenbrand27291e22014-01-23 12:26:52 +01001493#define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
1494 KVM_GUESTDBG_USE_HW_BP | \
1495 KVM_GUESTDBG_ENABLE)
1496
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001497int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1498 struct kvm_guest_debug *dbg)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001499{
David Hildenbrand27291e22014-01-23 12:26:52 +01001500 int rc = 0;
1501
1502 vcpu->guest_debug = 0;
1503 kvm_s390_clear_bp_data(vcpu);
1504
David Hildenbrand2de3bfc2014-05-20 17:25:20 +02001505 if (dbg->control & ~VALID_GUESTDBG_FLAGS)
David Hildenbrand27291e22014-01-23 12:26:52 +01001506 return -EINVAL;
1507
1508 if (dbg->control & KVM_GUESTDBG_ENABLE) {
1509 vcpu->guest_debug = dbg->control;
1510 /* enforce guest PER */
1511 atomic_set_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1512
1513 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
1514 rc = kvm_s390_import_bp_data(vcpu, dbg);
1515 } else {
1516 atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1517 vcpu->arch.guestdbg.last_bp = 0;
1518 }
1519
1520 if (rc) {
1521 vcpu->guest_debug = 0;
1522 kvm_s390_clear_bp_data(vcpu);
1523 atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1524 }
1525
1526 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001527}
1528
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001529int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1530 struct kvm_mp_state *mp_state)
1531{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001532 /* CHECK_STOP and LOAD are not supported yet */
1533 return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
1534 KVM_MP_STATE_OPERATING;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001535}
1536
1537int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1538 struct kvm_mp_state *mp_state)
1539{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001540 int rc = 0;
1541
1542 /* user space knows about this interface - let it control the state */
1543 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
1544
1545 switch (mp_state->mp_state) {
1546 case KVM_MP_STATE_STOPPED:
1547 kvm_s390_vcpu_stop(vcpu);
1548 break;
1549 case KVM_MP_STATE_OPERATING:
1550 kvm_s390_vcpu_start(vcpu);
1551 break;
1552 case KVM_MP_STATE_LOAD:
1553 case KVM_MP_STATE_CHECK_STOP:
1554 /* fall through - CHECK_STOP and LOAD are not supported yet */
1555 default:
1556 rc = -ENXIO;
1557 }
1558
1559 return rc;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001560}
1561
Dominik Dingelb31605c2014-03-25 13:47:11 +01001562bool kvm_s390_cmma_enabled(struct kvm *kvm)
1563{
1564 if (!MACHINE_IS_LPAR)
1565 return false;
1566 /* only enable for z10 and later */
1567 if (!MACHINE_HAS_EDAT1)
1568 return false;
1569 if (!kvm->arch.use_cmma)
1570 return false;
1571 return true;
1572}
1573
David Hildenbrand8ad35752014-03-14 11:00:21 +01001574static bool ibs_enabled(struct kvm_vcpu *vcpu)
1575{
1576 return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
1577}
1578
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001579static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
1580{
David Hildenbrand8ad35752014-03-14 11:00:21 +01001581retry:
1582 s390_vcpu_unblock(vcpu);
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001583 /*
1584 * We use MMU_RELOAD just to re-arm the ipte notifier for the
1585 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
1586 * This ensures that the ipte instruction for this request has
1587 * already finished. We might race against a second unmapper that
1588 * wants to set the blocking bit. Lets just retry the request loop.
1589 */
David Hildenbrand8ad35752014-03-14 11:00:21 +01001590 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001591 int rc;
1592 rc = gmap_ipte_notify(vcpu->arch.gmap,
Michael Muellerfda902c2014-05-13 16:58:30 +02001593 kvm_s390_get_prefix(vcpu),
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001594 PAGE_SIZE * 2);
1595 if (rc)
1596 return rc;
David Hildenbrand8ad35752014-03-14 11:00:21 +01001597 goto retry;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001598 }
David Hildenbrand8ad35752014-03-14 11:00:21 +01001599
David Hildenbrandd3d692c2014-07-29 08:53:36 +02001600 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
1601 vcpu->arch.sie_block->ihcpu = 0xffff;
1602 goto retry;
1603 }
1604
David Hildenbrand8ad35752014-03-14 11:00:21 +01001605 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
1606 if (!ibs_enabled(vcpu)) {
1607 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
1608 atomic_set_mask(CPUSTAT_IBS,
1609 &vcpu->arch.sie_block->cpuflags);
1610 }
1611 goto retry;
1612 }
1613
1614 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
1615 if (ibs_enabled(vcpu)) {
1616 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
1617 atomic_clear_mask(CPUSTAT_IBS,
1618 &vcpu->arch.sie_block->cpuflags);
1619 }
1620 goto retry;
1621 }
1622
David Hildenbrand0759d062014-05-13 16:54:32 +02001623 /* nothing to do, just clear the request */
1624 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
1625
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001626 return 0;
1627}
1628
Thomas Huthfa576c52014-05-06 17:20:16 +02001629/**
1630 * kvm_arch_fault_in_page - fault-in guest page if necessary
1631 * @vcpu: The corresponding virtual cpu
1632 * @gpa: Guest physical address
1633 * @writable: Whether the page should be writable or not
1634 *
1635 * Make sure that a guest page has been faulted-in on the host.
1636 *
1637 * Return: Zero on success, negative error code otherwise.
1638 */
1639long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001640{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001641 return gmap_fault(vcpu->arch.gmap, gpa,
1642 writable ? FAULT_FLAG_WRITE : 0);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001643}
1644
Dominik Dingel3c038e62013-10-07 17:11:48 +02001645static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
1646 unsigned long token)
1647{
1648 struct kvm_s390_interrupt inti;
Jens Freimann383d0b02014-07-29 15:11:49 +02001649 struct kvm_s390_irq irq;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001650
1651 if (start_token) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001652 irq.u.ext.ext_params2 = token;
1653 irq.type = KVM_S390_INT_PFAULT_INIT;
1654 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
Dominik Dingel3c038e62013-10-07 17:11:48 +02001655 } else {
1656 inti.type = KVM_S390_INT_PFAULT_DONE;
Jens Freimann383d0b02014-07-29 15:11:49 +02001657 inti.parm64 = token;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001658 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
1659 }
1660}
1661
1662void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
1663 struct kvm_async_pf *work)
1664{
1665 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
1666 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
1667}
1668
1669void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
1670 struct kvm_async_pf *work)
1671{
1672 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
1673 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
1674}
1675
1676void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
1677 struct kvm_async_pf *work)
1678{
1679 /* s390 will always inject the page directly */
1680}
1681
1682bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
1683{
1684 /*
1685 * s390 will always inject the page directly,
1686 * but we still want check_async_completion to cleanup
1687 */
1688 return true;
1689}
1690
1691static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
1692{
1693 hva_t hva;
1694 struct kvm_arch_async_pf arch;
1695 int rc;
1696
1697 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1698 return 0;
1699 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
1700 vcpu->arch.pfault_compare)
1701 return 0;
1702 if (psw_extint_disabled(vcpu))
1703 return 0;
David Hildenbrand9a022062014-08-05 17:40:47 +02001704 if (kvm_s390_vcpu_has_irq(vcpu, 0))
Dominik Dingel3c038e62013-10-07 17:11:48 +02001705 return 0;
1706 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
1707 return 0;
1708 if (!vcpu->arch.gmap->pfault_enabled)
1709 return 0;
1710
Heiko Carstens81480cc2014-01-01 16:36:07 +01001711 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
1712 hva += current->thread.gmap_addr & ~PAGE_MASK;
1713 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
Dominik Dingel3c038e62013-10-07 17:11:48 +02001714 return 0;
1715
1716 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
1717 return rc;
1718}
1719
Thomas Huth3fb4c402013-09-12 10:33:43 +02001720static int vcpu_pre_run(struct kvm_vcpu *vcpu)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001721{
Thomas Huth3fb4c402013-09-12 10:33:43 +02001722 int rc, cpuflags;
Carsten Ottee168bf82012-01-04 10:25:22 +01001723
Dominik Dingel3c038e62013-10-07 17:11:48 +02001724 /*
1725 * On s390 notifications for arriving pages will be delivered directly
1726 * to the guest but the house keeping for completed pfaults is
1727 * handled outside the worker.
1728 */
1729 kvm_check_async_pf_completion(vcpu);
1730
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001731 memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001732
1733 if (need_resched())
1734 schedule();
1735
Martin Schwidefskyd3a73ac2014-04-15 12:55:07 +02001736 if (test_cpu_flag(CIF_MCCK_PENDING))
Christian Borntraeger71cde582008-05-21 13:37:34 +02001737 s390_handle_mcck();
1738
Jens Freimann79395032014-04-17 10:10:30 +02001739 if (!kvm_is_ucontrol(vcpu->kvm)) {
1740 rc = kvm_s390_deliver_pending_interrupts(vcpu);
1741 if (rc)
1742 return rc;
1743 }
Carsten Otte0ff31862008-05-21 13:37:37 +02001744
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001745 rc = kvm_s390_handle_requests(vcpu);
1746 if (rc)
1747 return rc;
1748
David Hildenbrand27291e22014-01-23 12:26:52 +01001749 if (guestdbg_enabled(vcpu)) {
1750 kvm_s390_backup_guest_per_regs(vcpu);
1751 kvm_s390_patch_guest_per_regs(vcpu);
1752 }
1753
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001754 vcpu->arch.sie_block->icptcode = 0;
Thomas Huth3fb4c402013-09-12 10:33:43 +02001755 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
1756 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
1757 trace_kvm_s390_sie_enter(vcpu, cpuflags);
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02001758
Thomas Huth3fb4c402013-09-12 10:33:43 +02001759 return 0;
1760}
1761
Thomas Huth492d8642015-02-10 16:11:01 +01001762static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
1763{
1764 psw_t *psw = &vcpu->arch.sie_block->gpsw;
1765 u8 opcode;
1766 int rc;
1767
1768 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
1769 trace_kvm_s390_sie_fault(vcpu);
1770
1771 /*
1772 * We want to inject an addressing exception, which is defined as a
1773 * suppressing or terminating exception. However, since we came here
1774 * by a DAT access exception, the PSW still points to the faulting
1775 * instruction since DAT exceptions are nullifying. So we've got
1776 * to look up the current opcode to get the length of the instruction
1777 * to be able to forward the PSW.
1778 */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001779 rc = read_guest(vcpu, psw->addr, 0, &opcode, 1);
Thomas Huth492d8642015-02-10 16:11:01 +01001780 if (rc)
1781 return kvm_s390_inject_prog_cond(vcpu, rc);
1782 psw->addr = __rewind_psw(*psw, -insn_length(opcode));
1783
1784 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1785}
1786
Thomas Huth3fb4c402013-09-12 10:33:43 +02001787static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
1788{
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001789 int rc = -1;
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02001790
1791 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
1792 vcpu->arch.sie_block->icptcode);
1793 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
1794
David Hildenbrand27291e22014-01-23 12:26:52 +01001795 if (guestdbg_enabled(vcpu))
1796 kvm_s390_restore_guest_per_regs(vcpu);
1797
Thomas Huth3fb4c402013-09-12 10:33:43 +02001798 if (exit_reason >= 0) {
Martin Schwidefsky7c470532013-05-17 14:41:37 +02001799 rc = 0;
Thomas Huth210b16072013-09-19 16:26:18 +02001800 } else if (kvm_is_ucontrol(vcpu->kvm)) {
1801 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
1802 vcpu->run->s390_ucontrol.trans_exc_code =
1803 current->thread.gmap_addr;
1804 vcpu->run->s390_ucontrol.pgm_code = 0x10;
1805 rc = -EREMOTE;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001806
1807 } else if (current->thread.gmap_pfault) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001808 trace_kvm_s390_major_guest_pfault(vcpu);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001809 current->thread.gmap_pfault = 0;
Thomas Huthfa576c52014-05-06 17:20:16 +02001810 if (kvm_arch_setup_async_pf(vcpu)) {
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001811 rc = 0;
Thomas Huthfa576c52014-05-06 17:20:16 +02001812 } else {
1813 gpa_t gpa = current->thread.gmap_addr;
1814 rc = kvm_arch_fault_in_page(vcpu, gpa, 1);
1815 }
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001816 }
1817
Thomas Huth492d8642015-02-10 16:11:01 +01001818 if (rc == -1)
1819 rc = vcpu_post_run_fault_in_sie(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001820
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001821 memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
Thomas Huth3fb4c402013-09-12 10:33:43 +02001822
Thomas Hutha76ccff2013-09-12 10:33:44 +02001823 if (rc == 0) {
1824 if (kvm_is_ucontrol(vcpu->kvm))
Christian Borntraeger2955c832014-03-06 16:01:38 +01001825 /* Don't exit for host interrupts. */
1826 rc = vcpu->arch.sie_block->icptcode ? -EOPNOTSUPP : 0;
Thomas Hutha76ccff2013-09-12 10:33:44 +02001827 else
1828 rc = kvm_handle_sie_intercept(vcpu);
1829 }
1830
Thomas Huth3fb4c402013-09-12 10:33:43 +02001831 return rc;
1832}
1833
1834static int __vcpu_run(struct kvm_vcpu *vcpu)
1835{
1836 int rc, exit_reason;
1837
Thomas Huth800c1062013-09-12 10:33:45 +02001838 /*
1839 * We try to hold kvm->srcu during most of vcpu_run (except when run-
1840 * ning the guest), so that memslots (and other stuff) are protected
1841 */
1842 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1843
Thomas Hutha76ccff2013-09-12 10:33:44 +02001844 do {
1845 rc = vcpu_pre_run(vcpu);
1846 if (rc)
1847 break;
Thomas Huth3fb4c402013-09-12 10:33:43 +02001848
Thomas Huth800c1062013-09-12 10:33:45 +02001849 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Thomas Hutha76ccff2013-09-12 10:33:44 +02001850 /*
1851 * As PF_VCPU will be used in fault handler, between
1852 * guest_enter and guest_exit should be no uaccess.
1853 */
1854 preempt_disable();
1855 kvm_guest_enter();
1856 preempt_enable();
1857 exit_reason = sie64a(vcpu->arch.sie_block,
1858 vcpu->run->s.regs.gprs);
1859 kvm_guest_exit();
Thomas Huth800c1062013-09-12 10:33:45 +02001860 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02001861
Thomas Hutha76ccff2013-09-12 10:33:44 +02001862 rc = vcpu_post_run(vcpu, exit_reason);
David Hildenbrand27291e22014-01-23 12:26:52 +01001863 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
Thomas Huth3fb4c402013-09-12 10:33:43 +02001864
Thomas Huth800c1062013-09-12 10:33:45 +02001865 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Carsten Ottee168bf82012-01-04 10:25:22 +01001866 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001867}
1868
David Hildenbrandb028ee32014-07-17 10:47:43 +02001869static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1870{
1871 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
1872 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
1873 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
1874 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
1875 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
1876 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
David Hildenbrandd3d692c2014-07-29 08:53:36 +02001877 /* some control register changes require a tlb flush */
1878 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02001879 }
1880 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
1881 vcpu->arch.sie_block->cputm = kvm_run->s.regs.cputm;
1882 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
1883 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
1884 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
1885 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
1886 }
1887 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
1888 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
1889 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
1890 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
David Hildenbrand9fbd8082014-10-09 15:01:38 +02001891 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1892 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02001893 }
1894 kvm_run->kvm_dirty_regs = 0;
1895}
1896
1897static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1898{
1899 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
1900 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
1901 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
1902 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
1903 kvm_run->s.regs.cputm = vcpu->arch.sie_block->cputm;
1904 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
1905 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
1906 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
1907 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
1908 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
1909 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
1910 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
1911}
1912
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001913int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1914{
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001915 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001916 sigset_t sigsaved;
1917
David Hildenbrand27291e22014-01-23 12:26:52 +01001918 if (guestdbg_exit_pending(vcpu)) {
1919 kvm_s390_prepare_debug_exit(vcpu);
1920 return 0;
1921 }
1922
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001923 if (vcpu->sigset_active)
1924 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
1925
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001926 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
1927 kvm_s390_vcpu_start(vcpu);
1928 } else if (is_vcpu_stopped(vcpu)) {
1929 pr_err_ratelimited("kvm-s390: can't run stopped vcpu %d\n",
1930 vcpu->vcpu_id);
1931 return -EINVAL;
1932 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001933
David Hildenbrandb028ee32014-07-17 10:47:43 +02001934 sync_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001935
Heiko Carstensdab4079d2009-06-12 10:26:32 +02001936 might_fault();
Thomas Hutha76ccff2013-09-12 10:33:44 +02001937 rc = __vcpu_run(vcpu);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +02001938
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02001939 if (signal_pending(current) && !rc) {
1940 kvm_run->exit_reason = KVM_EXIT_INTR;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001941 rc = -EINTR;
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02001942 }
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001943
David Hildenbrand27291e22014-01-23 12:26:52 +01001944 if (guestdbg_exit_pending(vcpu) && !rc) {
1945 kvm_s390_prepare_debug_exit(vcpu);
1946 rc = 0;
1947 }
1948
Heiko Carstensb8e660b2010-02-26 22:37:41 +01001949 if (rc == -EOPNOTSUPP) {
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001950 /* intercept cannot be handled in-kernel, prepare kvm-run */
1951 kvm_run->exit_reason = KVM_EXIT_S390_SIEIC;
1952 kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001953 kvm_run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
1954 kvm_run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
1955 rc = 0;
1956 }
1957
1958 if (rc == -EREMOTE) {
1959 /* intercept was handled, but userspace support is needed
1960 * kvm_run has been prepared by the handler */
1961 rc = 0;
1962 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001963
David Hildenbrandb028ee32014-07-17 10:47:43 +02001964 store_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001965
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001966 if (vcpu->sigset_active)
1967 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1968
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001969 vcpu->stat.exit_userspace++;
Heiko Carstens7e8e6ab2008-04-04 15:12:35 +02001970 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001971}
1972
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001973/*
1974 * store status at address
1975 * we use have two special cases:
1976 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
1977 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
1978 */
Heiko Carstensd0bce602014-01-01 16:45:58 +01001979int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001980{
Carsten Otte092670c2011-07-24 10:48:22 +02001981 unsigned char archmode = 1;
Michael Muellerfda902c2014-05-13 16:58:30 +02001982 unsigned int px;
Thomas Huth178bd782013-11-13 20:28:18 +01001983 u64 clkcomp;
Heiko Carstensd0bce602014-01-01 16:45:58 +01001984 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001985
Heiko Carstensd0bce602014-01-01 16:45:58 +01001986 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
1987 if (write_guest_abs(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001988 return -EFAULT;
Heiko Carstensd0bce602014-01-01 16:45:58 +01001989 gpa = SAVE_AREA_BASE;
1990 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
1991 if (write_guest_real(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001992 return -EFAULT;
Heiko Carstensd0bce602014-01-01 16:45:58 +01001993 gpa = kvm_s390_real_to_abs(vcpu, SAVE_AREA_BASE);
1994 }
1995 rc = write_guest_abs(vcpu, gpa + offsetof(struct save_area, fp_regs),
1996 vcpu->arch.guest_fpregs.fprs, 128);
1997 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, gp_regs),
1998 vcpu->run->s.regs.gprs, 128);
1999 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, psw),
2000 &vcpu->arch.sie_block->gpsw, 16);
Michael Muellerfda902c2014-05-13 16:58:30 +02002001 px = kvm_s390_get_prefix(vcpu);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002002 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, pref_reg),
Michael Muellerfda902c2014-05-13 16:58:30 +02002003 &px, 4);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002004 rc |= write_guest_abs(vcpu,
2005 gpa + offsetof(struct save_area, fp_ctrl_reg),
2006 &vcpu->arch.guest_fpregs.fpc, 4);
2007 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, tod_reg),
2008 &vcpu->arch.sie_block->todpr, 4);
2009 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, timer),
2010 &vcpu->arch.sie_block->cputm, 8);
Thomas Huth178bd782013-11-13 20:28:18 +01002011 clkcomp = vcpu->arch.sie_block->ckc >> 8;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002012 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, clk_cmp),
2013 &clkcomp, 8);
2014 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, acc_regs),
2015 &vcpu->run->s.regs.acrs, 64);
2016 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, ctrl_regs),
2017 &vcpu->arch.sie_block->gcr, 128);
2018 return rc ? -EFAULT : 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002019}
2020
Thomas Huthe8798922013-11-06 15:46:33 +01002021int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2022{
2023 /*
2024 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
2025 * copying in vcpu load/put. Lets update our copies before we save
2026 * it into the save area
2027 */
2028 save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
2029 save_fp_regs(vcpu->arch.guest_fpregs.fprs);
2030 save_access_regs(vcpu->run->s.regs.acrs);
2031
2032 return kvm_s390_store_status_unloaded(vcpu, addr);
2033}
2034
Eric Farmanbc17de72014-04-14 16:01:09 -04002035/*
2036 * store additional status at address
2037 */
2038int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
2039 unsigned long gpa)
2040{
2041 /* Only bits 0-53 are used for address formation */
2042 if (!(gpa & ~0x3ff))
2043 return 0;
2044
2045 return write_guest_abs(vcpu, gpa & ~0x3ff,
2046 (void *)&vcpu->run->s.regs.vrs, 512);
2047}
2048
2049int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr)
2050{
2051 if (!test_kvm_facility(vcpu->kvm, 129))
2052 return 0;
2053
2054 /*
2055 * The guest VXRS are in the host VXRs due to the lazy
2056 * copying in vcpu load/put. Let's update our copies before we save
2057 * it into the save area.
2058 */
2059 save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
2060
2061 return kvm_s390_store_adtl_status_unloaded(vcpu, addr);
2062}
2063
David Hildenbrand8ad35752014-03-14 11:00:21 +01002064static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2065{
2066 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
2067 kvm_make_request(KVM_REQ_DISABLE_IBS, vcpu);
2068 exit_sie_sync(vcpu);
2069}
2070
2071static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
2072{
2073 unsigned int i;
2074 struct kvm_vcpu *vcpu;
2075
2076 kvm_for_each_vcpu(i, vcpu, kvm) {
2077 __disable_ibs_on_vcpu(vcpu);
2078 }
2079}
2080
2081static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2082{
2083 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
2084 kvm_make_request(KVM_REQ_ENABLE_IBS, vcpu);
2085 exit_sie_sync(vcpu);
2086}
2087
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002088void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
2089{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002090 int i, online_vcpus, started_vcpus = 0;
2091
2092 if (!is_vcpu_stopped(vcpu))
2093 return;
2094
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002095 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002096 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002097 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002098 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2099
2100 for (i = 0; i < online_vcpus; i++) {
2101 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
2102 started_vcpus++;
2103 }
2104
2105 if (started_vcpus == 0) {
2106 /* we're the only active VCPU -> speed it up */
2107 __enable_ibs_on_vcpu(vcpu);
2108 } else if (started_vcpus == 1) {
2109 /*
2110 * As we are starting a second VCPU, we have to disable
2111 * the IBS facility on all VCPUs to remove potentially
2112 * oustanding ENABLE requests.
2113 */
2114 __disable_ibs_on_all_vcpus(vcpu->kvm);
2115 }
2116
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002117 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002118 /*
2119 * Another VCPU might have used IBS while we were offline.
2120 * Let's play safe and flush the VCPU at startup.
2121 */
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002122 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002123 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002124 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002125}
2126
2127void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
2128{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002129 int i, online_vcpus, started_vcpus = 0;
2130 struct kvm_vcpu *started_vcpu = NULL;
2131
2132 if (is_vcpu_stopped(vcpu))
2133 return;
2134
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002135 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002136 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002137 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002138 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2139
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002140 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
David Hildenbrand6cddd432014-10-15 16:48:53 +02002141 kvm_s390_clear_stop_irq(vcpu);
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002142
David Hildenbrand6cddd432014-10-15 16:48:53 +02002143 atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002144 __disable_ibs_on_vcpu(vcpu);
2145
2146 for (i = 0; i < online_vcpus; i++) {
2147 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
2148 started_vcpus++;
2149 started_vcpu = vcpu->kvm->vcpus[i];
2150 }
2151 }
2152
2153 if (started_vcpus == 1) {
2154 /*
2155 * As we only have one VCPU left, we want to enable the
2156 * IBS facility for that VCPU to speed it up.
2157 */
2158 __enable_ibs_on_vcpu(started_vcpu);
2159 }
2160
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002161 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002162 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002163}
2164
Cornelia Huckd6712df2012-12-20 15:32:11 +01002165static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2166 struct kvm_enable_cap *cap)
2167{
2168 int r;
2169
2170 if (cap->flags)
2171 return -EINVAL;
2172
2173 switch (cap->cap) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002174 case KVM_CAP_S390_CSS_SUPPORT:
2175 if (!vcpu->kvm->arch.css_support) {
2176 vcpu->kvm->arch.css_support = 1;
2177 trace_kvm_s390_enable_css(vcpu->kvm);
2178 }
2179 r = 0;
2180 break;
Cornelia Huckd6712df2012-12-20 15:32:11 +01002181 default:
2182 r = -EINVAL;
2183 break;
2184 }
2185 return r;
2186}
2187
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002188long kvm_arch_vcpu_ioctl(struct file *filp,
2189 unsigned int ioctl, unsigned long arg)
2190{
2191 struct kvm_vcpu *vcpu = filp->private_data;
2192 void __user *argp = (void __user *)arg;
Thomas Huth800c1062013-09-12 10:33:45 +02002193 int idx;
Avi Kivitybc923cc2010-05-13 12:21:46 +03002194 long r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002195
Avi Kivity937366242010-05-13 12:35:17 +03002196 switch (ioctl) {
2197 case KVM_S390_INTERRUPT: {
Carsten Otteba5c1e92008-03-25 18:47:26 +01002198 struct kvm_s390_interrupt s390int;
Jens Freimann383d0b02014-07-29 15:11:49 +02002199 struct kvm_s390_irq s390irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002200
Avi Kivity937366242010-05-13 12:35:17 +03002201 r = -EFAULT;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002202 if (copy_from_user(&s390int, argp, sizeof(s390int)))
Avi Kivity937366242010-05-13 12:35:17 +03002203 break;
Jens Freimann383d0b02014-07-29 15:11:49 +02002204 if (s390int_to_s390irq(&s390int, &s390irq))
2205 return -EINVAL;
2206 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
Avi Kivity937366242010-05-13 12:35:17 +03002207 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002208 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002209 case KVM_S390_STORE_STATUS:
Thomas Huth800c1062013-09-12 10:33:45 +02002210 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002211 r = kvm_s390_vcpu_store_status(vcpu, arg);
Thomas Huth800c1062013-09-12 10:33:45 +02002212 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002213 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002214 case KVM_S390_SET_INITIAL_PSW: {
2215 psw_t psw;
2216
Avi Kivitybc923cc2010-05-13 12:21:46 +03002217 r = -EFAULT;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002218 if (copy_from_user(&psw, argp, sizeof(psw)))
Avi Kivitybc923cc2010-05-13 12:21:46 +03002219 break;
2220 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
2221 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002222 }
2223 case KVM_S390_INITIAL_RESET:
Avi Kivitybc923cc2010-05-13 12:21:46 +03002224 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
2225 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02002226 case KVM_SET_ONE_REG:
2227 case KVM_GET_ONE_REG: {
2228 struct kvm_one_reg reg;
2229 r = -EFAULT;
2230 if (copy_from_user(&reg, argp, sizeof(reg)))
2231 break;
2232 if (ioctl == KVM_SET_ONE_REG)
2233 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
2234 else
2235 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
2236 break;
2237 }
Carsten Otte27e03932012-01-04 10:25:21 +01002238#ifdef CONFIG_KVM_S390_UCONTROL
2239 case KVM_S390_UCAS_MAP: {
2240 struct kvm_s390_ucas_mapping ucasmap;
2241
2242 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2243 r = -EFAULT;
2244 break;
2245 }
2246
2247 if (!kvm_is_ucontrol(vcpu->kvm)) {
2248 r = -EINVAL;
2249 break;
2250 }
2251
2252 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
2253 ucasmap.vcpu_addr, ucasmap.length);
2254 break;
2255 }
2256 case KVM_S390_UCAS_UNMAP: {
2257 struct kvm_s390_ucas_mapping ucasmap;
2258
2259 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2260 r = -EFAULT;
2261 break;
2262 }
2263
2264 if (!kvm_is_ucontrol(vcpu->kvm)) {
2265 r = -EINVAL;
2266 break;
2267 }
2268
2269 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
2270 ucasmap.length);
2271 break;
2272 }
2273#endif
Carsten Otteccc79102012-01-04 10:25:26 +01002274 case KVM_S390_VCPU_FAULT: {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002275 r = gmap_fault(vcpu->arch.gmap, arg, 0);
Carsten Otteccc79102012-01-04 10:25:26 +01002276 break;
2277 }
Cornelia Huckd6712df2012-12-20 15:32:11 +01002278 case KVM_ENABLE_CAP:
2279 {
2280 struct kvm_enable_cap cap;
2281 r = -EFAULT;
2282 if (copy_from_user(&cap, argp, sizeof(cap)))
2283 break;
2284 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2285 break;
2286 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002287 default:
Carsten Otte3e6afcf2012-01-04 10:25:30 +01002288 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002289 }
Avi Kivitybc923cc2010-05-13 12:21:46 +03002290 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002291}
2292
Carsten Otte5b1c1492012-01-04 10:25:23 +01002293int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2294{
2295#ifdef CONFIG_KVM_S390_UCONTROL
2296 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
2297 && (kvm_is_ucontrol(vcpu->kvm))) {
2298 vmf->page = virt_to_page(vcpu->arch.sie_block);
2299 get_page(vmf->page);
2300 return 0;
2301 }
2302#endif
2303 return VM_FAULT_SIGBUS;
2304}
2305
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05302306int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2307 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09002308{
2309 return 0;
2310}
2311
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002312/* Section: memory related */
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002313int kvm_arch_prepare_memory_region(struct kvm *kvm,
2314 struct kvm_memory_slot *memslot,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09002315 struct kvm_userspace_memory_region *mem,
2316 enum kvm_mr_change change)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002317{
Nick Wangdd2887e2013-03-25 17:22:57 +01002318 /* A few sanity checks. We can have memory slots which have to be
2319 located/ended at a segment boundary (1MB). The memory in userland is
2320 ok to be fragmented into various different vmas. It is okay to mmap()
2321 and munmap() stuff in this slot after doing this call at any time */
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002322
Carsten Otte598841c2011-07-24 10:48:21 +02002323 if (mem->userspace_addr & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002324 return -EINVAL;
2325
Carsten Otte598841c2011-07-24 10:48:21 +02002326 if (mem->memory_size & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002327 return -EINVAL;
2328
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002329 return 0;
2330}
2331
2332void kvm_arch_commit_memory_region(struct kvm *kvm,
2333 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002334 const struct kvm_memory_slot *old,
2335 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002336{
Carsten Ottef7850c92011-07-24 10:48:23 +02002337 int rc;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002338
Christian Borntraeger2cef4de2013-03-25 17:22:48 +01002339 /* If the basics of the memslot do not change, we do not want
2340 * to update the gmap. Every update causes several unnecessary
2341 * segment translation exceptions. This is usually handled just
2342 * fine by the normal fault handler + gmap, but it will also
2343 * cause faults on the prefix page of running guest CPUs.
2344 */
2345 if (old->userspace_addr == mem->userspace_addr &&
2346 old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
2347 old->npages * PAGE_SIZE == mem->memory_size)
2348 return;
Carsten Otte598841c2011-07-24 10:48:21 +02002349
2350 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
2351 mem->guest_phys_addr, mem->memory_size);
2352 if (rc)
Carsten Ottef7850c92011-07-24 10:48:23 +02002353 printk(KERN_WARNING "kvm-s390: failed to commit memory region\n");
Carsten Otte598841c2011-07-24 10:48:21 +02002354 return;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002355}
2356
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002357static int __init kvm_s390_init(void)
2358{
Michael Mueller9d8d5782015-02-02 15:42:51 +01002359 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002360}
2361
2362static void __exit kvm_s390_exit(void)
2363{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002364 kvm_exit();
2365}
2366
2367module_init(kvm_s390_init);
2368module_exit(kvm_s390_exit);
Cornelia Huck566af942013-05-27 18:42:33 +02002369
2370/*
2371 * Enable autoloading of the kvm module.
2372 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
2373 * since x86 takes a different approach.
2374 */
2375#include <linux/miscdevice.h>
2376MODULE_ALIAS_MISCDEV(KVM_MINOR);
2377MODULE_ALIAS("devname:kvm");