blob: 8bc25d405edff23a166a90dff614968012438a83 [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>
Thomas Huth41408c282015-02-06 15:01:21 +010028#include <linux/vmalloc.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010029#include <asm/asm-offsets.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010030#include <asm/lowcore.h>
31#include <asm/pgtable.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010032#include <asm/nmi.h>
David Howellsa0616cd2012-03-28 18:30:02 +010033#include <asm/switch_to.h>
Jens Freimann6d3da242013-07-03 15:18:35 +020034#include <asm/isc.h>
Christian Borntraeger1526bf92012-05-15 14:15:25 +020035#include <asm/sclp.h>
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010036#include "kvm-s390.h"
Heiko Carstensb0c632d2008-03-25 18:47:20 +010037#include "gaccess.h"
38
Cornelia Huck5786fff2012-07-23 17:20:29 +020039#define CREATE_TRACE_POINTS
40#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020041#include "trace-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020042
Thomas Huth41408c282015-02-06 15:01:21 +010043#define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
44
Heiko Carstensb0c632d2008-03-25 18:47:20 +010045#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
46
47struct kvm_stats_debugfs_item debugfs_entries[] = {
48 { "userspace_handled", VCPU_STAT(exit_userspace) },
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +020049 { "exit_null", VCPU_STAT(exit_null) },
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010050 { "exit_validity", VCPU_STAT(exit_validity) },
51 { "exit_stop_request", VCPU_STAT(exit_stop_request) },
52 { "exit_external_request", VCPU_STAT(exit_external_request) },
53 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010054 { "exit_instruction", VCPU_STAT(exit_instruction) },
55 { "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
56 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
Paolo Bonzinif7819512015-02-04 18:20:58 +010057 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
David Hildenbrandce2e4f02014-07-11 10:00:43 +020058 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020059 { "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010060 { "instruction_lctl", VCPU_STAT(instruction_lctl) },
David Hildenbrandaba07502014-01-23 10:47:13 +010061 { "instruction_stctl", VCPU_STAT(instruction_stctl) },
62 { "instruction_stctg", VCPU_STAT(instruction_stctg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010063 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020064 { "deliver_external_call", VCPU_STAT(deliver_external_call) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010065 { "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
66 { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
67 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
68 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
69 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
70 { "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
71 { "exit_wait_state", VCPU_STAT(exit_wait_state) },
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020072 { "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010073 { "instruction_stidp", VCPU_STAT(instruction_stidp) },
74 { "instruction_spx", VCPU_STAT(instruction_spx) },
75 { "instruction_stpx", VCPU_STAT(instruction_stpx) },
76 { "instruction_stap", VCPU_STAT(instruction_stap) },
77 { "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
Heiko Carstens8a2422342014-01-10 14:33:28 +010078 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010079 { "instruction_stsch", VCPU_STAT(instruction_stsch) },
80 { "instruction_chsc", VCPU_STAT(instruction_chsc) },
Konstantin Weitzb31288f2013-04-17 17:36:29 +020081 { "instruction_essa", VCPU_STAT(instruction_essa) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010082 { "instruction_stsi", VCPU_STAT(instruction_stsi) },
83 { "instruction_stfl", VCPU_STAT(instruction_stfl) },
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +020084 { "instruction_tprot", VCPU_STAT(instruction_tprot) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010085 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
Cornelia Huckbd59d3a2011-11-17 11:00:42 +010086 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020087 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010088 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +020089 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
90 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010091 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +020092 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
93 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
Eric Farmancd7b4b62015-02-12 09:06:34 -050094 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010095 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
96 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
97 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +020098 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
99 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
100 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
Christian Borntraeger388186b2011-10-30 15:17:03 +0100101 { "diagnose_10", VCPU_STAT(diagnose_10) },
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100102 { "diagnose_44", VCPU_STAT(diagnose_44) },
Konstantin Weitz41628d32012-04-25 15:30:38 +0200103 { "diagnose_9c", VCPU_STAT(diagnose_9c) },
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100104 { NULL }
105};
106
Michael Mueller9d8d5782015-02-02 15:42:51 +0100107/* upper facilities limit for kvm */
108unsigned long kvm_s390_fac_list_mask[] = {
Christian Borntraegera3ed8da2015-03-18 13:54:31 +0100109 0xffe6fffbfcfdfc40UL,
110 0x205c800000000000UL,
Michael Mueller9d8d5782015-02-02 15:42:51 +0100111};
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100112
Michael Mueller9d8d5782015-02-02 15:42:51 +0100113unsigned long kvm_s390_fac_list_mask_size(void)
Michael Mueller78c4b59f2013-07-26 15:04:04 +0200114{
Michael Mueller9d8d5782015-02-02 15:42:51 +0100115 BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
116 return ARRAY_SIZE(kvm_s390_fac_list_mask);
Michael Mueller78c4b59f2013-07-26 15:04:04 +0200117}
118
Michael Mueller9d8d5782015-02-02 15:42:51 +0100119static struct gmap_notifier gmap_notifier;
120
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100121/* Section: not file related */
Radim Krčmář13a34e02014-08-28 15:13:03 +0200122int kvm_arch_hardware_enable(void)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100123{
124 /* every s390 is virtualization enabled ;-) */
Alexander Graf10474ae2009-09-15 11:37:46 +0200125 return 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100126}
127
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200128static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address);
129
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100130int kvm_arch_hardware_setup(void)
131{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200132 gmap_notifier.notifier_call = kvm_gmap_notifier;
133 gmap_register_ipte_notifier(&gmap_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100134 return 0;
135}
136
137void kvm_arch_hardware_unsetup(void)
138{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200139 gmap_unregister_ipte_notifier(&gmap_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100140}
141
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100142int kvm_arch_init(void *opaque)
143{
Cornelia Huck84877d92014-09-02 10:27:35 +0100144 /* Register floating interrupt controller interface. */
145 return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100146}
147
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100148/* Section: device related */
149long kvm_arch_dev_ioctl(struct file *filp,
150 unsigned int ioctl, unsigned long arg)
151{
152 if (ioctl == KVM_S390_ENABLE_SIE)
153 return s390_enable_sie();
154 return -EINVAL;
155}
156
Alexander Graf784aa3d2014-07-14 18:27:35 +0200157int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100158{
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100159 int r;
160
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200161 switch (ext) {
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100162 case KVM_CAP_S390_PSW:
Christian Borntraegerb6cf8782011-09-20 17:07:29 +0200163 case KVM_CAP_S390_GMAP:
Christian Borntraeger52e16b12011-11-17 11:00:44 +0100164 case KVM_CAP_SYNC_MMU:
Carsten Otte1efd0f52012-01-04 10:25:29 +0100165#ifdef CONFIG_KVM_S390_UCONTROL
166 case KVM_CAP_S390_UCONTROL:
167#endif
Dominik Dingel3c038e62013-10-07 17:11:48 +0200168 case KVM_CAP_ASYNC_PF:
Christian Borntraeger60b413c2012-01-11 11:20:31 +0100169 case KVM_CAP_SYNC_REGS:
Carsten Otte14eebd92012-05-15 14:15:26 +0200170 case KVM_CAP_ONE_REG:
Cornelia Huckd6712df2012-12-20 15:32:11 +0100171 case KVM_CAP_ENABLE_CAP:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100172 case KVM_CAP_S390_CSS_SUPPORT:
Cornelia Huckebc32262014-05-09 15:00:46 +0200173 case KVM_CAP_IRQFD:
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100174 case KVM_CAP_IOEVENTFD:
Jens Freimannc05c4182013-10-07 16:13:45 +0200175 case KVM_CAP_DEVICE_CTRL:
Cornelia Huckd938dc52013-10-23 18:26:34 +0200176 case KVM_CAP_ENABLE_CAP_VM:
Cornelia Huck78599d92014-07-15 09:54:39 +0200177 case KVM_CAP_S390_IRQCHIP:
Dominik Dingelf2061652014-04-09 13:13:00 +0200178 case KVM_CAP_VM_ATTRIBUTES:
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200179 case KVM_CAP_MP_STATE:
Jens Freimann47b43c52014-11-11 20:57:06 +0100180 case KVM_CAP_S390_INJECT_IRQ:
David Hildenbrand2444b352014-10-09 14:10:13 +0200181 case KVM_CAP_S390_USER_SIGP:
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100182 case KVM_CAP_S390_USER_STSI:
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400183 case KVM_CAP_S390_SKEYS:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100184 r = 1;
185 break;
Thomas Huth41408c282015-02-06 15:01:21 +0100186 case KVM_CAP_S390_MEM_OP:
187 r = MEM_OP_MAX_SIZE;
188 break;
Christian Borntraegere726b1b2012-05-02 10:50:38 +0200189 case KVM_CAP_NR_VCPUS:
190 case KVM_CAP_MAX_VCPUS:
191 r = KVM_MAX_VCPUS;
192 break;
Nick Wange1e2e602013-03-25 17:22:58 +0100193 case KVM_CAP_NR_MEMSLOTS:
194 r = KVM_USER_MEM_SLOTS;
195 break;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200196 case KVM_CAP_S390_COW:
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100197 r = MACHINE_HAS_ESOP;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200198 break;
Eric Farman68c55752014-06-09 10:57:26 -0400199 case KVM_CAP_S390_VECTOR_REGISTERS:
200 r = MACHINE_HAS_VX;
201 break;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200202 default:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100203 r = 0;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200204 }
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100205 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100206}
207
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400208static void kvm_s390_sync_dirty_log(struct kvm *kvm,
209 struct kvm_memory_slot *memslot)
210{
211 gfn_t cur_gfn, last_gfn;
212 unsigned long address;
213 struct gmap *gmap = kvm->arch.gmap;
214
215 down_read(&gmap->mm->mmap_sem);
216 /* Loop over all guest pages */
217 last_gfn = memslot->base_gfn + memslot->npages;
218 for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
219 address = gfn_to_hva_memslot(memslot, cur_gfn);
220
221 if (gmap_test_and_clear_dirty(address, gmap))
222 mark_page_dirty(kvm, cur_gfn);
223 }
224 up_read(&gmap->mm->mmap_sem);
225}
226
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100227/* Section: vm related */
228/*
229 * Get (and clear) the dirty memory log for a memory slot.
230 */
231int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
232 struct kvm_dirty_log *log)
233{
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400234 int r;
235 unsigned long n;
236 struct kvm_memory_slot *memslot;
237 int is_dirty = 0;
238
239 mutex_lock(&kvm->slots_lock);
240
241 r = -EINVAL;
242 if (log->slot >= KVM_USER_MEM_SLOTS)
243 goto out;
244
245 memslot = id_to_memslot(kvm->memslots, log->slot);
246 r = -ENOENT;
247 if (!memslot->dirty_bitmap)
248 goto out;
249
250 kvm_s390_sync_dirty_log(kvm, memslot);
251 r = kvm_get_dirty_log(kvm, log, &is_dirty);
252 if (r)
253 goto out;
254
255 /* Clear the dirty log */
256 if (is_dirty) {
257 n = kvm_dirty_bitmap_bytes(memslot);
258 memset(memslot->dirty_bitmap, 0, n);
259 }
260 r = 0;
261out:
262 mutex_unlock(&kvm->slots_lock);
263 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100264}
265
Cornelia Huckd938dc52013-10-23 18:26:34 +0200266static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
267{
268 int r;
269
270 if (cap->flags)
271 return -EINVAL;
272
273 switch (cap->cap) {
Cornelia Huck84223592013-07-15 13:36:01 +0200274 case KVM_CAP_S390_IRQCHIP:
275 kvm->arch.use_irqchip = 1;
276 r = 0;
277 break;
David Hildenbrand2444b352014-10-09 14:10:13 +0200278 case KVM_CAP_S390_USER_SIGP:
279 kvm->arch.user_sigp = 1;
280 r = 0;
281 break;
Eric Farman68c55752014-06-09 10:57:26 -0400282 case KVM_CAP_S390_VECTOR_REGISTERS:
Michael Mueller18280d82015-03-16 16:05:41 +0100283 if (MACHINE_HAS_VX) {
284 set_kvm_facility(kvm->arch.model.fac->mask, 129);
285 set_kvm_facility(kvm->arch.model.fac->list, 129);
286 r = 0;
287 } else
288 r = -EINVAL;
Eric Farman68c55752014-06-09 10:57:26 -0400289 break;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100290 case KVM_CAP_S390_USER_STSI:
291 kvm->arch.user_stsi = 1;
292 r = 0;
293 break;
Cornelia Huckd938dc52013-10-23 18:26:34 +0200294 default:
295 r = -EINVAL;
296 break;
297 }
298 return r;
299}
300
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100301static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
302{
303 int ret;
304
305 switch (attr->attr) {
306 case KVM_S390_VM_MEM_LIMIT_SIZE:
307 ret = 0;
308 if (put_user(kvm->arch.gmap->asce_end, (u64 __user *)attr->addr))
309 ret = -EFAULT;
310 break;
311 default:
312 ret = -ENXIO;
313 break;
314 }
315 return ret;
316}
317
318static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200319{
320 int ret;
321 unsigned int idx;
322 switch (attr->attr) {
323 case KVM_S390_VM_MEM_ENABLE_CMMA:
324 ret = -EBUSY;
325 mutex_lock(&kvm->lock);
326 if (atomic_read(&kvm->online_vcpus) == 0) {
327 kvm->arch.use_cmma = 1;
328 ret = 0;
329 }
330 mutex_unlock(&kvm->lock);
331 break;
332 case KVM_S390_VM_MEM_CLR_CMMA:
333 mutex_lock(&kvm->lock);
334 idx = srcu_read_lock(&kvm->srcu);
Dominik Dingela13cff32014-10-23 12:07:14 +0200335 s390_reset_cmma(kvm->arch.gmap->mm);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200336 srcu_read_unlock(&kvm->srcu, idx);
337 mutex_unlock(&kvm->lock);
338 ret = 0;
339 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100340 case KVM_S390_VM_MEM_LIMIT_SIZE: {
341 unsigned long new_limit;
342
343 if (kvm_is_ucontrol(kvm))
344 return -EINVAL;
345
346 if (get_user(new_limit, (u64 __user *)attr->addr))
347 return -EFAULT;
348
349 if (new_limit > kvm->arch.gmap->asce_end)
350 return -E2BIG;
351
352 ret = -EBUSY;
353 mutex_lock(&kvm->lock);
354 if (atomic_read(&kvm->online_vcpus) == 0) {
355 /* gmap_alloc will round the limit up */
356 struct gmap *new = gmap_alloc(current->mm, new_limit);
357
358 if (!new) {
359 ret = -ENOMEM;
360 } else {
361 gmap_free(kvm->arch.gmap);
362 new->private = kvm;
363 kvm->arch.gmap = new;
364 ret = 0;
365 }
366 }
367 mutex_unlock(&kvm->lock);
368 break;
369 }
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200370 default:
371 ret = -ENXIO;
372 break;
373 }
374 return ret;
375}
376
Tony Krowiaka374e892014-09-03 10:13:53 +0200377static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
378
379static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
380{
381 struct kvm_vcpu *vcpu;
382 int i;
383
Michael Mueller9d8d5782015-02-02 15:42:51 +0100384 if (!test_kvm_facility(kvm, 76))
Tony Krowiaka374e892014-09-03 10:13:53 +0200385 return -EINVAL;
386
387 mutex_lock(&kvm->lock);
388 switch (attr->attr) {
389 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
390 get_random_bytes(
391 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
392 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
393 kvm->arch.crypto.aes_kw = 1;
394 break;
395 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
396 get_random_bytes(
397 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
398 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
399 kvm->arch.crypto.dea_kw = 1;
400 break;
401 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
402 kvm->arch.crypto.aes_kw = 0;
403 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
404 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
405 break;
406 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
407 kvm->arch.crypto.dea_kw = 0;
408 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
409 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
410 break;
411 default:
412 mutex_unlock(&kvm->lock);
413 return -ENXIO;
414 }
415
416 kvm_for_each_vcpu(i, vcpu, kvm) {
417 kvm_s390_vcpu_crypto_setup(vcpu);
418 exit_sie(vcpu);
419 }
420 mutex_unlock(&kvm->lock);
421 return 0;
422}
423
Jason J. Herne72f25022014-11-25 09:46:02 -0500424static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
425{
426 u8 gtod_high;
427
428 if (copy_from_user(&gtod_high, (void __user *)attr->addr,
429 sizeof(gtod_high)))
430 return -EFAULT;
431
432 if (gtod_high != 0)
433 return -EINVAL;
434
435 return 0;
436}
437
438static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
439{
440 struct kvm_vcpu *cur_vcpu;
441 unsigned int vcpu_idx;
442 u64 host_tod, gtod;
443 int r;
444
445 if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
446 return -EFAULT;
447
448 r = store_tod_clock(&host_tod);
449 if (r)
450 return r;
451
452 mutex_lock(&kvm->lock);
453 kvm->arch.epoch = gtod - host_tod;
454 kvm_for_each_vcpu(vcpu_idx, cur_vcpu, kvm) {
455 cur_vcpu->arch.sie_block->epoch = kvm->arch.epoch;
456 exit_sie(cur_vcpu);
457 }
458 mutex_unlock(&kvm->lock);
459 return 0;
460}
461
462static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
463{
464 int ret;
465
466 if (attr->flags)
467 return -EINVAL;
468
469 switch (attr->attr) {
470 case KVM_S390_VM_TOD_HIGH:
471 ret = kvm_s390_set_tod_high(kvm, attr);
472 break;
473 case KVM_S390_VM_TOD_LOW:
474 ret = kvm_s390_set_tod_low(kvm, attr);
475 break;
476 default:
477 ret = -ENXIO;
478 break;
479 }
480 return ret;
481}
482
483static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
484{
485 u8 gtod_high = 0;
486
487 if (copy_to_user((void __user *)attr->addr, &gtod_high,
488 sizeof(gtod_high)))
489 return -EFAULT;
490
491 return 0;
492}
493
494static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
495{
496 u64 host_tod, gtod;
497 int r;
498
499 r = store_tod_clock(&host_tod);
500 if (r)
501 return r;
502
503 gtod = host_tod + kvm->arch.epoch;
504 if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
505 return -EFAULT;
506
507 return 0;
508}
509
510static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
511{
512 int ret;
513
514 if (attr->flags)
515 return -EINVAL;
516
517 switch (attr->attr) {
518 case KVM_S390_VM_TOD_HIGH:
519 ret = kvm_s390_get_tod_high(kvm, attr);
520 break;
521 case KVM_S390_VM_TOD_LOW:
522 ret = kvm_s390_get_tod_low(kvm, attr);
523 break;
524 default:
525 ret = -ENXIO;
526 break;
527 }
528 return ret;
529}
530
Michael Mueller658b6ed2015-02-02 15:49:35 +0100531static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
532{
533 struct kvm_s390_vm_cpu_processor *proc;
534 int ret = 0;
535
536 mutex_lock(&kvm->lock);
537 if (atomic_read(&kvm->online_vcpus)) {
538 ret = -EBUSY;
539 goto out;
540 }
541 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
542 if (!proc) {
543 ret = -ENOMEM;
544 goto out;
545 }
546 if (!copy_from_user(proc, (void __user *)attr->addr,
547 sizeof(*proc))) {
548 memcpy(&kvm->arch.model.cpu_id, &proc->cpuid,
549 sizeof(struct cpuid));
550 kvm->arch.model.ibc = proc->ibc;
Michael Mueller981467c2015-02-24 13:51:04 +0100551 memcpy(kvm->arch.model.fac->list, proc->fac_list,
Michael Mueller658b6ed2015-02-02 15:49:35 +0100552 S390_ARCH_FAC_LIST_SIZE_BYTE);
553 } else
554 ret = -EFAULT;
555 kfree(proc);
556out:
557 mutex_unlock(&kvm->lock);
558 return ret;
559}
560
561static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
562{
563 int ret = -ENXIO;
564
565 switch (attr->attr) {
566 case KVM_S390_VM_CPU_PROCESSOR:
567 ret = kvm_s390_set_processor(kvm, attr);
568 break;
569 }
570 return ret;
571}
572
573static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
574{
575 struct kvm_s390_vm_cpu_processor *proc;
576 int ret = 0;
577
578 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
579 if (!proc) {
580 ret = -ENOMEM;
581 goto out;
582 }
583 memcpy(&proc->cpuid, &kvm->arch.model.cpu_id, sizeof(struct cpuid));
584 proc->ibc = kvm->arch.model.ibc;
Michael Mueller981467c2015-02-24 13:51:04 +0100585 memcpy(&proc->fac_list, kvm->arch.model.fac->list, S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100586 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
587 ret = -EFAULT;
588 kfree(proc);
589out:
590 return ret;
591}
592
593static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
594{
595 struct kvm_s390_vm_cpu_machine *mach;
596 int ret = 0;
597
598 mach = kzalloc(sizeof(*mach), GFP_KERNEL);
599 if (!mach) {
600 ret = -ENOMEM;
601 goto out;
602 }
603 get_cpu_id((struct cpuid *) &mach->cpuid);
604 mach->ibc = sclp_get_ibc();
Michael Mueller981467c2015-02-24 13:51:04 +0100605 memcpy(&mach->fac_mask, kvm->arch.model.fac->mask,
606 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100607 memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +0100608 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100609 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
610 ret = -EFAULT;
611 kfree(mach);
612out:
613 return ret;
614}
615
616static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
617{
618 int ret = -ENXIO;
619
620 switch (attr->attr) {
621 case KVM_S390_VM_CPU_PROCESSOR:
622 ret = kvm_s390_get_processor(kvm, attr);
623 break;
624 case KVM_S390_VM_CPU_MACHINE:
625 ret = kvm_s390_get_machine(kvm, attr);
626 break;
627 }
628 return ret;
629}
630
Dominik Dingelf2061652014-04-09 13:13:00 +0200631static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
632{
633 int ret;
634
635 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200636 case KVM_S390_VM_MEM_CTRL:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100637 ret = kvm_s390_set_mem_control(kvm, attr);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200638 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500639 case KVM_S390_VM_TOD:
640 ret = kvm_s390_set_tod(kvm, attr);
641 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100642 case KVM_S390_VM_CPU_MODEL:
643 ret = kvm_s390_set_cpu_model(kvm, attr);
644 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200645 case KVM_S390_VM_CRYPTO:
646 ret = kvm_s390_vm_set_crypto(kvm, attr);
647 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200648 default:
649 ret = -ENXIO;
650 break;
651 }
652
653 return ret;
654}
655
656static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
657{
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100658 int ret;
659
660 switch (attr->group) {
661 case KVM_S390_VM_MEM_CTRL:
662 ret = kvm_s390_get_mem_control(kvm, attr);
663 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500664 case KVM_S390_VM_TOD:
665 ret = kvm_s390_get_tod(kvm, attr);
666 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100667 case KVM_S390_VM_CPU_MODEL:
668 ret = kvm_s390_get_cpu_model(kvm, attr);
669 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100670 default:
671 ret = -ENXIO;
672 break;
673 }
674
675 return ret;
Dominik Dingelf2061652014-04-09 13:13:00 +0200676}
677
678static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
679{
680 int ret;
681
682 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200683 case KVM_S390_VM_MEM_CTRL:
684 switch (attr->attr) {
685 case KVM_S390_VM_MEM_ENABLE_CMMA:
686 case KVM_S390_VM_MEM_CLR_CMMA:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100687 case KVM_S390_VM_MEM_LIMIT_SIZE:
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200688 ret = 0;
689 break;
690 default:
691 ret = -ENXIO;
692 break;
693 }
694 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500695 case KVM_S390_VM_TOD:
696 switch (attr->attr) {
697 case KVM_S390_VM_TOD_LOW:
698 case KVM_S390_VM_TOD_HIGH:
699 ret = 0;
700 break;
701 default:
702 ret = -ENXIO;
703 break;
704 }
705 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100706 case KVM_S390_VM_CPU_MODEL:
707 switch (attr->attr) {
708 case KVM_S390_VM_CPU_PROCESSOR:
709 case KVM_S390_VM_CPU_MACHINE:
710 ret = 0;
711 break;
712 default:
713 ret = -ENXIO;
714 break;
715 }
716 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200717 case KVM_S390_VM_CRYPTO:
718 switch (attr->attr) {
719 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
720 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
721 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
722 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
723 ret = 0;
724 break;
725 default:
726 ret = -ENXIO;
727 break;
728 }
729 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200730 default:
731 ret = -ENXIO;
732 break;
733 }
734
735 return ret;
736}
737
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400738static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
739{
740 uint8_t *keys;
741 uint64_t hva;
742 unsigned long curkey;
743 int i, r = 0;
744
745 if (args->flags != 0)
746 return -EINVAL;
747
748 /* Is this guest using storage keys? */
749 if (!mm_use_skey(current->mm))
750 return KVM_S390_GET_SKEYS_NONE;
751
752 /* Enforce sane limit on memory allocation */
753 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
754 return -EINVAL;
755
756 keys = kmalloc_array(args->count, sizeof(uint8_t),
757 GFP_KERNEL | __GFP_NOWARN);
758 if (!keys)
759 keys = vmalloc(sizeof(uint8_t) * args->count);
760 if (!keys)
761 return -ENOMEM;
762
763 for (i = 0; i < args->count; i++) {
764 hva = gfn_to_hva(kvm, args->start_gfn + i);
765 if (kvm_is_error_hva(hva)) {
766 r = -EFAULT;
767 goto out;
768 }
769
770 curkey = get_guest_storage_key(current->mm, hva);
771 if (IS_ERR_VALUE(curkey)) {
772 r = curkey;
773 goto out;
774 }
775 keys[i] = curkey;
776 }
777
778 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
779 sizeof(uint8_t) * args->count);
780 if (r)
781 r = -EFAULT;
782out:
783 kvfree(keys);
784 return r;
785}
786
787static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
788{
789 uint8_t *keys;
790 uint64_t hva;
791 int i, r = 0;
792
793 if (args->flags != 0)
794 return -EINVAL;
795
796 /* Enforce sane limit on memory allocation */
797 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
798 return -EINVAL;
799
800 keys = kmalloc_array(args->count, sizeof(uint8_t),
801 GFP_KERNEL | __GFP_NOWARN);
802 if (!keys)
803 keys = vmalloc(sizeof(uint8_t) * args->count);
804 if (!keys)
805 return -ENOMEM;
806
807 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
808 sizeof(uint8_t) * args->count);
809 if (r) {
810 r = -EFAULT;
811 goto out;
812 }
813
814 /* Enable storage key handling for the guest */
815 s390_enable_skey();
816
817 for (i = 0; i < args->count; i++) {
818 hva = gfn_to_hva(kvm, args->start_gfn + i);
819 if (kvm_is_error_hva(hva)) {
820 r = -EFAULT;
821 goto out;
822 }
823
824 /* Lowest order bit is reserved */
825 if (keys[i] & 0x01) {
826 r = -EINVAL;
827 goto out;
828 }
829
830 r = set_guest_storage_key(current->mm, hva,
831 (unsigned long)keys[i], 0);
832 if (r)
833 goto out;
834 }
835out:
836 kvfree(keys);
837 return r;
838}
839
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100840long kvm_arch_vm_ioctl(struct file *filp,
841 unsigned int ioctl, unsigned long arg)
842{
843 struct kvm *kvm = filp->private_data;
844 void __user *argp = (void __user *)arg;
Dominik Dingelf2061652014-04-09 13:13:00 +0200845 struct kvm_device_attr attr;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100846 int r;
847
848 switch (ioctl) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100849 case KVM_S390_INTERRUPT: {
850 struct kvm_s390_interrupt s390int;
851
852 r = -EFAULT;
853 if (copy_from_user(&s390int, argp, sizeof(s390int)))
854 break;
855 r = kvm_s390_inject_vm(kvm, &s390int);
856 break;
857 }
Cornelia Huckd938dc52013-10-23 18:26:34 +0200858 case KVM_ENABLE_CAP: {
859 struct kvm_enable_cap cap;
860 r = -EFAULT;
861 if (copy_from_user(&cap, argp, sizeof(cap)))
862 break;
863 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
864 break;
865 }
Cornelia Huck84223592013-07-15 13:36:01 +0200866 case KVM_CREATE_IRQCHIP: {
867 struct kvm_irq_routing_entry routing;
868
869 r = -EINVAL;
870 if (kvm->arch.use_irqchip) {
871 /* Set up dummy routing. */
872 memset(&routing, 0, sizeof(routing));
873 kvm_set_irq_routing(kvm, &routing, 0, 0);
874 r = 0;
875 }
876 break;
877 }
Dominik Dingelf2061652014-04-09 13:13:00 +0200878 case KVM_SET_DEVICE_ATTR: {
879 r = -EFAULT;
880 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
881 break;
882 r = kvm_s390_vm_set_attr(kvm, &attr);
883 break;
884 }
885 case KVM_GET_DEVICE_ATTR: {
886 r = -EFAULT;
887 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
888 break;
889 r = kvm_s390_vm_get_attr(kvm, &attr);
890 break;
891 }
892 case KVM_HAS_DEVICE_ATTR: {
893 r = -EFAULT;
894 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
895 break;
896 r = kvm_s390_vm_has_attr(kvm, &attr);
897 break;
898 }
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400899 case KVM_S390_GET_SKEYS: {
900 struct kvm_s390_skeys args;
901
902 r = -EFAULT;
903 if (copy_from_user(&args, argp,
904 sizeof(struct kvm_s390_skeys)))
905 break;
906 r = kvm_s390_get_skeys(kvm, &args);
907 break;
908 }
909 case KVM_S390_SET_SKEYS: {
910 struct kvm_s390_skeys args;
911
912 r = -EFAULT;
913 if (copy_from_user(&args, argp,
914 sizeof(struct kvm_s390_skeys)))
915 break;
916 r = kvm_s390_set_skeys(kvm, &args);
917 break;
918 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100919 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300920 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100921 }
922
923 return r;
924}
925
Tony Krowiak45c9b472015-01-13 11:33:26 -0500926static int kvm_s390_query_ap_config(u8 *config)
927{
928 u32 fcn_code = 0x04000000UL;
Christian Borntraeger86044c82015-02-26 13:53:47 +0100929 u32 cc = 0;
Tony Krowiak45c9b472015-01-13 11:33:26 -0500930
Christian Borntraeger86044c82015-02-26 13:53:47 +0100931 memset(config, 0, 128);
Tony Krowiak45c9b472015-01-13 11:33:26 -0500932 asm volatile(
933 "lgr 0,%1\n"
934 "lgr 2,%2\n"
935 ".long 0xb2af0000\n" /* PQAP(QCI) */
Christian Borntraeger86044c82015-02-26 13:53:47 +0100936 "0: ipm %0\n"
Tony Krowiak45c9b472015-01-13 11:33:26 -0500937 "srl %0,28\n"
Christian Borntraeger86044c82015-02-26 13:53:47 +0100938 "1:\n"
939 EX_TABLE(0b, 1b)
940 : "+r" (cc)
Tony Krowiak45c9b472015-01-13 11:33:26 -0500941 : "r" (fcn_code), "r" (config)
942 : "cc", "0", "2", "memory"
943 );
944
945 return cc;
946}
947
948static int kvm_s390_apxa_installed(void)
949{
950 u8 config[128];
951 int cc;
952
953 if (test_facility(2) && test_facility(12)) {
954 cc = kvm_s390_query_ap_config(config);
955
956 if (cc)
957 pr_err("PQAP(QCI) failed with cc=%d", cc);
958 else
959 return config[0] & 0x40;
960 }
961
962 return 0;
963}
964
965static void kvm_s390_set_crycb_format(struct kvm *kvm)
966{
967 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
968
969 if (kvm_s390_apxa_installed())
970 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
971 else
972 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
973}
974
Michael Mueller9d8d5782015-02-02 15:42:51 +0100975static void kvm_s390_get_cpu_id(struct cpuid *cpu_id)
976{
977 get_cpu_id(cpu_id);
978 cpu_id->version = 0xff;
979}
980
Tony Krowiak5102ee82014-06-27 14:46:01 -0400981static int kvm_s390_crypto_init(struct kvm *kvm)
982{
Michael Mueller9d8d5782015-02-02 15:42:51 +0100983 if (!test_kvm_facility(kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -0400984 return 0;
985
986 kvm->arch.crypto.crycb = kzalloc(sizeof(*kvm->arch.crypto.crycb),
987 GFP_KERNEL | GFP_DMA);
988 if (!kvm->arch.crypto.crycb)
989 return -ENOMEM;
990
Tony Krowiak45c9b472015-01-13 11:33:26 -0500991 kvm_s390_set_crycb_format(kvm);
Tony Krowiak5102ee82014-06-27 14:46:01 -0400992
Tony Krowiaked6f76b2015-02-24 14:06:57 -0500993 /* Enable AES/DEA protected key functions by default */
994 kvm->arch.crypto.aes_kw = 1;
995 kvm->arch.crypto.dea_kw = 1;
996 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
997 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
998 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
999 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
Tony Krowiaka374e892014-09-03 10:13:53 +02001000
Tony Krowiak5102ee82014-06-27 14:46:01 -04001001 return 0;
1002}
1003
Carsten Ottee08b9632012-01-04 10:25:20 +01001004int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001005{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001006 int i, rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001007 char debug_name[16];
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001008 static unsigned long sca_offset;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001009
Carsten Ottee08b9632012-01-04 10:25:20 +01001010 rc = -EINVAL;
1011#ifdef CONFIG_KVM_S390_UCONTROL
1012 if (type & ~KVM_VM_S390_UCONTROL)
1013 goto out_err;
1014 if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1015 goto out_err;
1016#else
1017 if (type)
1018 goto out_err;
1019#endif
1020
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001021 rc = s390_enable_sie();
1022 if (rc)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001023 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001024
Carsten Otteb2904112011-10-18 12:27:13 +02001025 rc = -ENOMEM;
1026
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001027 kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
1028 if (!kvm->arch.sca)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001029 goto out_err;
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001030 spin_lock(&kvm_lock);
1031 sca_offset = (sca_offset + 16) & 0x7f0;
1032 kvm->arch.sca = (struct sca_block *) ((char *) kvm->arch.sca + sca_offset);
1033 spin_unlock(&kvm_lock);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001034
1035 sprintf(debug_name, "kvm-%u", current->pid);
1036
1037 kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
1038 if (!kvm->arch.dbf)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001039 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001040
Michael Mueller9d8d5782015-02-02 15:42:51 +01001041 /*
1042 * The architectural maximum amount of facilities is 16 kbit. To store
1043 * this amount, 2 kbyte of memory is required. Thus we need a full
Michael Mueller981467c2015-02-24 13:51:04 +01001044 * page to hold the guest facility list (arch.model.fac->list) and the
1045 * facility mask (arch.model.fac->mask). Its address size has to be
Michael Mueller9d8d5782015-02-02 15:42:51 +01001046 * 31 bits and word aligned.
1047 */
1048 kvm->arch.model.fac =
Michael Mueller981467c2015-02-24 13:51:04 +01001049 (struct kvm_s390_fac *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001050 if (!kvm->arch.model.fac)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001051 goto out_err;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001052
Michael Muellerfb5bf932015-02-27 14:25:10 +01001053 /* Populate the facility mask initially. */
Michael Mueller981467c2015-02-24 13:51:04 +01001054 memcpy(kvm->arch.model.fac->mask, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +01001055 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001056 for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
1057 if (i < kvm_s390_fac_list_mask_size())
Michael Mueller981467c2015-02-24 13:51:04 +01001058 kvm->arch.model.fac->mask[i] &= kvm_s390_fac_list_mask[i];
Michael Mueller9d8d5782015-02-02 15:42:51 +01001059 else
Michael Mueller981467c2015-02-24 13:51:04 +01001060 kvm->arch.model.fac->mask[i] = 0UL;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001061 }
1062
Michael Mueller981467c2015-02-24 13:51:04 +01001063 /* Populate the facility list initially. */
1064 memcpy(kvm->arch.model.fac->list, kvm->arch.model.fac->mask,
1065 S390_ARCH_FAC_LIST_SIZE_BYTE);
1066
Michael Mueller9d8d5782015-02-02 15:42:51 +01001067 kvm_s390_get_cpu_id(&kvm->arch.model.cpu_id);
Michael Mueller658b6ed2015-02-02 15:49:35 +01001068 kvm->arch.model.ibc = sclp_get_ibc() & 0x0fff;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001069
Tony Krowiak5102ee82014-06-27 14:46:01 -04001070 if (kvm_s390_crypto_init(kvm) < 0)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001071 goto out_err;
Tony Krowiak5102ee82014-06-27 14:46:01 -04001072
Carsten Otteba5c1e92008-03-25 18:47:26 +01001073 spin_lock_init(&kvm->arch.float_int.lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001074 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1075 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
Heiko Carstens8a2422342014-01-10 14:33:28 +01001076 init_waitqueue_head(&kvm->arch.ipte_wq);
Thomas Hutha6b7e452014-10-01 14:48:42 +02001077 mutex_init(&kvm->arch.ipte_mutex);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001078
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001079 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
1080 VM_EVENT(kvm, 3, "%s", "vm created");
1081
Carsten Ottee08b9632012-01-04 10:25:20 +01001082 if (type & KVM_VM_S390_UCONTROL) {
1083 kvm->arch.gmap = NULL;
1084 } else {
Christian Borntraeger03499852014-08-25 12:38:57 +02001085 kvm->arch.gmap = gmap_alloc(current->mm, (1UL << 44) - 1);
Carsten Ottee08b9632012-01-04 10:25:20 +01001086 if (!kvm->arch.gmap)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001087 goto out_err;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001088 kvm->arch.gmap->private = kvm;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001089 kvm->arch.gmap->pfault_enabled = 0;
Carsten Ottee08b9632012-01-04 10:25:20 +01001090 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001091
1092 kvm->arch.css_support = 0;
Cornelia Huck84223592013-07-15 13:36:01 +02001093 kvm->arch.use_irqchip = 0;
Jason J. Herne72f25022014-11-25 09:46:02 -05001094 kvm->arch.epoch = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001095
David Hildenbrand8ad35752014-03-14 11:00:21 +01001096 spin_lock_init(&kvm->arch.start_stop_lock);
1097
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001098 return 0;
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001099out_err:
Dominik Dingel40f5b732015-03-12 13:55:53 +01001100 kfree(kvm->arch.crypto.crycb);
1101 free_page((unsigned long)kvm->arch.model.fac);
1102 debug_unregister(kvm->arch.dbf);
1103 free_page((unsigned long)(kvm->arch.sca));
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001104 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001105}
1106
Christian Borntraegerd329c032008-11-26 14:50:27 +01001107void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1108{
1109 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
Cornelia Huckade38c32012-07-23 17:20:30 +02001110 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001111 kvm_s390_clear_local_irqs(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001112 kvm_clear_async_pf_completion_queue(vcpu);
Carsten Otte58f94602012-01-04 10:25:27 +01001113 if (!kvm_is_ucontrol(vcpu->kvm)) {
1114 clear_bit(63 - vcpu->vcpu_id,
1115 (unsigned long *) &vcpu->kvm->arch.sca->mcn);
1116 if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
1117 (__u64) vcpu->arch.sie_block)
1118 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
1119 }
Carsten Otteabf4a712009-05-12 17:21:51 +02001120 smp_mb();
Carsten Otte27e03932012-01-04 10:25:21 +01001121
1122 if (kvm_is_ucontrol(vcpu->kvm))
1123 gmap_free(vcpu->arch.gmap);
1124
Dominik Dingelb31605c2014-03-25 13:47:11 +01001125 if (kvm_s390_cmma_enabled(vcpu->kvm))
1126 kvm_s390_vcpu_unsetup_cmma(vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001127 free_page((unsigned long)(vcpu->arch.sie_block));
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001128
Christian Borntraeger6692cef2008-11-26 14:51:08 +01001129 kvm_vcpu_uninit(vcpu);
Michael Muellerb110fea2013-06-12 13:54:54 +02001130 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001131}
1132
1133static void kvm_free_vcpus(struct kvm *kvm)
1134{
1135 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001136 struct kvm_vcpu *vcpu;
Christian Borntraegerd329c032008-11-26 14:50:27 +01001137
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001138 kvm_for_each_vcpu(i, vcpu, kvm)
1139 kvm_arch_vcpu_destroy(vcpu);
1140
1141 mutex_lock(&kvm->lock);
1142 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1143 kvm->vcpus[i] = NULL;
1144
1145 atomic_set(&kvm->online_vcpus, 0);
1146 mutex_unlock(&kvm->lock);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001147}
1148
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001149void kvm_arch_destroy_vm(struct kvm *kvm)
1150{
Christian Borntraegerd329c032008-11-26 14:50:27 +01001151 kvm_free_vcpus(kvm);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001152 free_page((unsigned long)kvm->arch.model.fac);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001153 free_page((unsigned long)(kvm->arch.sca));
Christian Borntraegerd329c032008-11-26 14:50:27 +01001154 debug_unregister(kvm->arch.dbf);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001155 kfree(kvm->arch.crypto.crycb);
Carsten Otte27e03932012-01-04 10:25:21 +01001156 if (!kvm_is_ucontrol(kvm))
1157 gmap_free(kvm->arch.gmap);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001158 kvm_s390_destroy_adapters(kvm);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001159 kvm_s390_clear_float_irqs(kvm);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001160}
1161
1162/* Section: vcpu related */
Dominik Dingeldafd0322014-12-02 16:53:21 +01001163static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1164{
1165 vcpu->arch.gmap = gmap_alloc(current->mm, -1UL);
1166 if (!vcpu->arch.gmap)
1167 return -ENOMEM;
1168 vcpu->arch.gmap->private = vcpu->kvm;
1169
1170 return 0;
1171}
1172
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001173int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1174{
Dominik Dingel3c038e62013-10-07 17:11:48 +02001175 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1176 kvm_clear_async_pf_completion_queue(vcpu);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001177 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
1178 KVM_SYNC_GPRS |
Christian Borntraeger9eed07352012-02-06 10:59:07 +01001179 KVM_SYNC_ACRS |
David Hildenbrandb028ee32014-07-17 10:47:43 +02001180 KVM_SYNC_CRS |
1181 KVM_SYNC_ARCH0 |
1182 KVM_SYNC_PFAULT;
Eric Farman68c55752014-06-09 10:57:26 -04001183 if (test_kvm_facility(vcpu->kvm, 129))
1184 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
Dominik Dingeldafd0322014-12-02 16:53:21 +01001185
1186 if (kvm_is_ucontrol(vcpu->kvm))
1187 return __kvm_ucontrol_vcpu_init(vcpu);
1188
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001189 return 0;
1190}
1191
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001192void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1193{
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001194 save_fp_ctl(&vcpu->arch.host_fpregs.fpc);
Michael Mueller18280d82015-03-16 16:05:41 +01001195 if (test_kvm_facility(vcpu->kvm, 129))
Eric Farman68c55752014-06-09 10:57:26 -04001196 save_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs);
1197 else
1198 save_fp_regs(vcpu->arch.host_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001199 save_access_regs(vcpu->arch.host_acrs);
Michael Mueller18280d82015-03-16 16:05:41 +01001200 if (test_kvm_facility(vcpu->kvm, 129)) {
Eric Farman68c55752014-06-09 10:57:26 -04001201 restore_fp_ctl(&vcpu->run->s.regs.fpc);
1202 restore_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
1203 } else {
1204 restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1205 restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
1206 }
Christian Borntraeger59674c12012-01-11 11:20:33 +01001207 restore_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001208 gmap_enable(vcpu->arch.gmap);
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001209 atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001210}
1211
1212void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1213{
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001214 atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001215 gmap_disable(vcpu->arch.gmap);
Michael Mueller18280d82015-03-16 16:05:41 +01001216 if (test_kvm_facility(vcpu->kvm, 129)) {
Eric Farman68c55752014-06-09 10:57:26 -04001217 save_fp_ctl(&vcpu->run->s.regs.fpc);
1218 save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
1219 } else {
1220 save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1221 save_fp_regs(vcpu->arch.guest_fpregs.fprs);
1222 }
Christian Borntraeger59674c12012-01-11 11:20:33 +01001223 save_access_regs(vcpu->run->s.regs.acrs);
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001224 restore_fp_ctl(&vcpu->arch.host_fpregs.fpc);
Michael Mueller18280d82015-03-16 16:05:41 +01001225 if (test_kvm_facility(vcpu->kvm, 129))
Eric Farman68c55752014-06-09 10:57:26 -04001226 restore_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs);
1227 else
1228 restore_fp_regs(vcpu->arch.host_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001229 restore_access_regs(vcpu->arch.host_acrs);
1230}
1231
1232static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1233{
1234 /* this equals initial cpu reset in pop, but we don't switch to ESA */
1235 vcpu->arch.sie_block->gpsw.mask = 0UL;
1236 vcpu->arch.sie_block->gpsw.addr = 0UL;
Christian Borntraeger8d26cf72012-01-11 11:19:32 +01001237 kvm_s390_set_prefix(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001238 vcpu->arch.sie_block->cputm = 0UL;
1239 vcpu->arch.sie_block->ckc = 0UL;
1240 vcpu->arch.sie_block->todpr = 0;
1241 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1242 vcpu->arch.sie_block->gcr[0] = 0xE0UL;
1243 vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
1244 vcpu->arch.guest_fpregs.fpc = 0;
1245 asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
1246 vcpu->arch.sie_block->gbea = 1;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001247 vcpu->arch.sie_block->pp = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001248 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1249 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001250 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
1251 kvm_s390_vcpu_stop(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001252 kvm_s390_clear_local_irqs(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001253}
1254
Dominik Dingel31928aa2014-12-04 15:47:07 +01001255void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001256{
Jason J. Herne72f25022014-11-25 09:46:02 -05001257 mutex_lock(&vcpu->kvm->lock);
1258 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
1259 mutex_unlock(&vcpu->kvm->lock);
Dominik Dingeldafd0322014-12-02 16:53:21 +01001260 if (!kvm_is_ucontrol(vcpu->kvm))
1261 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001262}
1263
Tony Krowiak5102ee82014-06-27 14:46:01 -04001264static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
1265{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001266 if (!test_kvm_facility(vcpu->kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -04001267 return;
1268
Tony Krowiaka374e892014-09-03 10:13:53 +02001269 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1270
1271 if (vcpu->kvm->arch.crypto.aes_kw)
1272 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1273 if (vcpu->kvm->arch.crypto.dea_kw)
1274 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1275
Tony Krowiak5102ee82014-06-27 14:46:01 -04001276 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
1277}
1278
Dominik Dingelb31605c2014-03-25 13:47:11 +01001279void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1280{
1281 free_page(vcpu->arch.sie_block->cbrlo);
1282 vcpu->arch.sie_block->cbrlo = 0;
1283}
1284
1285int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1286{
1287 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1288 if (!vcpu->arch.sie_block->cbrlo)
1289 return -ENOMEM;
1290
1291 vcpu->arch.sie_block->ecb2 |= 0x80;
1292 vcpu->arch.sie_block->ecb2 &= ~0x08;
1293 return 0;
1294}
1295
Michael Mueller91520f12015-02-27 14:32:11 +01001296static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
1297{
1298 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
1299
1300 vcpu->arch.cpu_id = model->cpu_id;
1301 vcpu->arch.sie_block->ibc = model->ibc;
1302 vcpu->arch.sie_block->fac = (int) (long) model->fac->list;
1303}
1304
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001305int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1306{
Dominik Dingelb31605c2014-03-25 13:47:11 +01001307 int rc = 0;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001308
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001309 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
1310 CPUSTAT_SM |
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02001311 CPUSTAT_STOPPED |
1312 CPUSTAT_GED);
Michael Mueller91520f12015-02-27 14:32:11 +01001313 kvm_s390_vcpu_setup_model(vcpu);
1314
Christian Borntraegerfc345312010-06-17 23:16:20 +02001315 vcpu->arch.sie_block->ecb = 6;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001316 if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73))
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001317 vcpu->arch.sie_block->ecb |= 0x10;
1318
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02001319 vcpu->arch.sie_block->ecb2 = 8;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001320 vcpu->arch.sie_block->eca = 0xC1002000U;
Heiko Carstens217a4402013-12-30 12:54:14 +01001321 if (sclp_has_siif())
1322 vcpu->arch.sie_block->eca |= 1;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001323 if (sclp_has_sigpif())
1324 vcpu->arch.sie_block->eca |= 0x10000000U;
Michael Mueller18280d82015-03-16 16:05:41 +01001325 if (test_kvm_facility(vcpu->kvm, 129)) {
Eric Farman13211ea2014-04-30 13:39:46 -04001326 vcpu->arch.sie_block->eca |= 0x00020000;
1327 vcpu->arch.sie_block->ecd |= 0x20000000;
1328 }
Thomas Huth492d8642015-02-10 16:11:01 +01001329 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
Matthew Rosato5a5e6532013-01-29 11:48:20 -05001330
Dominik Dingelb31605c2014-03-25 13:47:11 +01001331 if (kvm_s390_cmma_enabled(vcpu->kvm)) {
1332 rc = kvm_s390_vcpu_setup_cmma(vcpu);
1333 if (rc)
1334 return rc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001335 }
David Hildenbrand0ac96ca2014-12-12 15:17:31 +01001336 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Christian Borntraegerca872302009-05-12 17:21:49 +02001337 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001338
Tony Krowiak5102ee82014-06-27 14:46:01 -04001339 kvm_s390_vcpu_crypto_setup(vcpu);
1340
Dominik Dingelb31605c2014-03-25 13:47:11 +01001341 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001342}
1343
1344struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1345 unsigned int id)
1346{
Carsten Otte4d475552011-10-18 12:27:12 +02001347 struct kvm_vcpu *vcpu;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001348 struct sie_page *sie_page;
Carsten Otte4d475552011-10-18 12:27:12 +02001349 int rc = -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001350
Carsten Otte4d475552011-10-18 12:27:12 +02001351 if (id >= KVM_MAX_VCPUS)
1352 goto out;
1353
1354 rc = -ENOMEM;
1355
Michael Muellerb110fea2013-06-12 13:54:54 +02001356 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001357 if (!vcpu)
Carsten Otte4d475552011-10-18 12:27:12 +02001358 goto out;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001359
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001360 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
1361 if (!sie_page)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001362 goto out_free_cpu;
1363
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001364 vcpu->arch.sie_block = &sie_page->sie_block;
1365 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
Eric Farman68c55752014-06-09 10:57:26 -04001366 vcpu->arch.host_vregs = &sie_page->vregs;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001367
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001368 vcpu->arch.sie_block->icpua = id;
Carsten Otte58f94602012-01-04 10:25:27 +01001369 if (!kvm_is_ucontrol(kvm)) {
1370 if (!kvm->arch.sca) {
1371 WARN_ON_ONCE(1);
1372 goto out_free_cpu;
1373 }
1374 if (!kvm->arch.sca->cpu[id].sda)
1375 kvm->arch.sca->cpu[id].sda =
1376 (__u64) vcpu->arch.sie_block;
1377 vcpu->arch.sie_block->scaoh =
1378 (__u32)(((__u64)kvm->arch.sca) >> 32);
1379 vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
1380 set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
1381 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001382
Carsten Otteba5c1e92008-03-25 18:47:26 +01001383 spin_lock_init(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001384 vcpu->arch.local_int.float_int = &kvm->arch.float_int;
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001385 vcpu->arch.local_int.wq = &vcpu->wq;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01001386 vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001387
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001388 rc = kvm_vcpu_init(vcpu, kvm, id);
1389 if (rc)
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001390 goto out_free_sie_block;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001391 VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
1392 vcpu->arch.sie_block);
Cornelia Huckade38c32012-07-23 17:20:30 +02001393 trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001394
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001395 return vcpu;
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001396out_free_sie_block:
1397 free_page((unsigned long)(vcpu->arch.sie_block));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001398out_free_cpu:
Michael Muellerb110fea2013-06-12 13:54:54 +02001399 kmem_cache_free(kvm_vcpu_cache, vcpu);
Carsten Otte4d475552011-10-18 12:27:12 +02001400out:
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001401 return ERR_PTR(rc);
1402}
1403
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001404int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1405{
David Hildenbrand9a022062014-08-05 17:40:47 +02001406 return kvm_s390_vcpu_has_irq(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001407}
1408
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001409void s390_vcpu_block(struct kvm_vcpu *vcpu)
1410{
1411 atomic_set_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
1412}
1413
1414void s390_vcpu_unblock(struct kvm_vcpu *vcpu)
1415{
1416 atomic_clear_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
1417}
1418
1419/*
1420 * Kick a guest cpu out of SIE and wait until SIE is not running.
1421 * If the CPU is not running (e.g. waiting as idle) the function will
1422 * return immediately. */
1423void exit_sie(struct kvm_vcpu *vcpu)
1424{
1425 atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
1426 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
1427 cpu_relax();
1428}
1429
1430/* Kick a guest cpu out of SIE and prevent SIE-reentry */
1431void exit_sie_sync(struct kvm_vcpu *vcpu)
1432{
1433 s390_vcpu_block(vcpu);
1434 exit_sie(vcpu);
1435}
1436
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001437static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
1438{
1439 int i;
1440 struct kvm *kvm = gmap->private;
1441 struct kvm_vcpu *vcpu;
1442
1443 kvm_for_each_vcpu(i, vcpu, kvm) {
1444 /* match against both prefix pages */
Michael Muellerfda902c2014-05-13 16:58:30 +02001445 if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001446 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
1447 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
1448 exit_sie_sync(vcpu);
1449 }
1450 }
1451}
1452
Christoffer Dallb6d33832012-03-08 16:44:24 -05001453int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1454{
1455 /* kvm common code refers to this, but never calls it */
1456 BUG();
1457 return 0;
1458}
1459
Carsten Otte14eebd92012-05-15 14:15:26 +02001460static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
1461 struct kvm_one_reg *reg)
1462{
1463 int r = -EINVAL;
1464
1465 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001466 case KVM_REG_S390_TODPR:
1467 r = put_user(vcpu->arch.sie_block->todpr,
1468 (u32 __user *)reg->addr);
1469 break;
1470 case KVM_REG_S390_EPOCHDIFF:
1471 r = put_user(vcpu->arch.sie_block->epoch,
1472 (u64 __user *)reg->addr);
1473 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001474 case KVM_REG_S390_CPU_TIMER:
1475 r = put_user(vcpu->arch.sie_block->cputm,
1476 (u64 __user *)reg->addr);
1477 break;
1478 case KVM_REG_S390_CLOCK_COMP:
1479 r = put_user(vcpu->arch.sie_block->ckc,
1480 (u64 __user *)reg->addr);
1481 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001482 case KVM_REG_S390_PFTOKEN:
1483 r = put_user(vcpu->arch.pfault_token,
1484 (u64 __user *)reg->addr);
1485 break;
1486 case KVM_REG_S390_PFCOMPARE:
1487 r = put_user(vcpu->arch.pfault_compare,
1488 (u64 __user *)reg->addr);
1489 break;
1490 case KVM_REG_S390_PFSELECT:
1491 r = put_user(vcpu->arch.pfault_select,
1492 (u64 __user *)reg->addr);
1493 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001494 case KVM_REG_S390_PP:
1495 r = put_user(vcpu->arch.sie_block->pp,
1496 (u64 __user *)reg->addr);
1497 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001498 case KVM_REG_S390_GBEA:
1499 r = put_user(vcpu->arch.sie_block->gbea,
1500 (u64 __user *)reg->addr);
1501 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001502 default:
1503 break;
1504 }
1505
1506 return r;
1507}
1508
1509static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
1510 struct kvm_one_reg *reg)
1511{
1512 int r = -EINVAL;
1513
1514 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001515 case KVM_REG_S390_TODPR:
1516 r = get_user(vcpu->arch.sie_block->todpr,
1517 (u32 __user *)reg->addr);
1518 break;
1519 case KVM_REG_S390_EPOCHDIFF:
1520 r = get_user(vcpu->arch.sie_block->epoch,
1521 (u64 __user *)reg->addr);
1522 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001523 case KVM_REG_S390_CPU_TIMER:
1524 r = get_user(vcpu->arch.sie_block->cputm,
1525 (u64 __user *)reg->addr);
1526 break;
1527 case KVM_REG_S390_CLOCK_COMP:
1528 r = get_user(vcpu->arch.sie_block->ckc,
1529 (u64 __user *)reg->addr);
1530 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001531 case KVM_REG_S390_PFTOKEN:
1532 r = get_user(vcpu->arch.pfault_token,
1533 (u64 __user *)reg->addr);
David Hildenbrand9fbd8082014-10-09 15:01:38 +02001534 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1535 kvm_clear_async_pf_completion_queue(vcpu);
Dominik Dingel536336c2013-09-30 10:55:33 +02001536 break;
1537 case KVM_REG_S390_PFCOMPARE:
1538 r = get_user(vcpu->arch.pfault_compare,
1539 (u64 __user *)reg->addr);
1540 break;
1541 case KVM_REG_S390_PFSELECT:
1542 r = get_user(vcpu->arch.pfault_select,
1543 (u64 __user *)reg->addr);
1544 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001545 case KVM_REG_S390_PP:
1546 r = get_user(vcpu->arch.sie_block->pp,
1547 (u64 __user *)reg->addr);
1548 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001549 case KVM_REG_S390_GBEA:
1550 r = get_user(vcpu->arch.sie_block->gbea,
1551 (u64 __user *)reg->addr);
1552 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001553 default:
1554 break;
1555 }
1556
1557 return r;
1558}
Christoffer Dallb6d33832012-03-08 16:44:24 -05001559
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001560static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
1561{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001562 kvm_s390_vcpu_initial_reset(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001563 return 0;
1564}
1565
1566int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1567{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001568 memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001569 return 0;
1570}
1571
1572int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1573{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001574 memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001575 return 0;
1576}
1577
1578int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1579 struct kvm_sregs *sregs)
1580{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001581 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001582 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
Christian Borntraeger59674c12012-01-11 11:20:33 +01001583 restore_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001584 return 0;
1585}
1586
1587int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1588 struct kvm_sregs *sregs)
1589{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001590 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001591 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001592 return 0;
1593}
1594
1595int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1596{
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001597 if (test_fp_ctl(fpu->fpc))
1598 return -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001599 memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001600 vcpu->arch.guest_fpregs.fpc = fpu->fpc;
1601 restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1602 restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001603 return 0;
1604}
1605
1606int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1607{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001608 memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
1609 fpu->fpc = vcpu->arch.guest_fpregs.fpc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001610 return 0;
1611}
1612
1613static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
1614{
1615 int rc = 0;
1616
David Hildenbrand7a42fdc2014-05-05 16:26:19 +02001617 if (!is_vcpu_stopped(vcpu))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001618 rc = -EBUSY;
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001619 else {
1620 vcpu->run->psw_mask = psw.mask;
1621 vcpu->run->psw_addr = psw.addr;
1622 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001623 return rc;
1624}
1625
1626int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1627 struct kvm_translation *tr)
1628{
1629 return -EINVAL; /* not implemented yet */
1630}
1631
David Hildenbrand27291e22014-01-23 12:26:52 +01001632#define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
1633 KVM_GUESTDBG_USE_HW_BP | \
1634 KVM_GUESTDBG_ENABLE)
1635
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001636int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1637 struct kvm_guest_debug *dbg)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001638{
David Hildenbrand27291e22014-01-23 12:26:52 +01001639 int rc = 0;
1640
1641 vcpu->guest_debug = 0;
1642 kvm_s390_clear_bp_data(vcpu);
1643
David Hildenbrand2de3bfc2014-05-20 17:25:20 +02001644 if (dbg->control & ~VALID_GUESTDBG_FLAGS)
David Hildenbrand27291e22014-01-23 12:26:52 +01001645 return -EINVAL;
1646
1647 if (dbg->control & KVM_GUESTDBG_ENABLE) {
1648 vcpu->guest_debug = dbg->control;
1649 /* enforce guest PER */
1650 atomic_set_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1651
1652 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
1653 rc = kvm_s390_import_bp_data(vcpu, dbg);
1654 } else {
1655 atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1656 vcpu->arch.guestdbg.last_bp = 0;
1657 }
1658
1659 if (rc) {
1660 vcpu->guest_debug = 0;
1661 kvm_s390_clear_bp_data(vcpu);
1662 atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1663 }
1664
1665 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001666}
1667
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001668int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1669 struct kvm_mp_state *mp_state)
1670{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001671 /* CHECK_STOP and LOAD are not supported yet */
1672 return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
1673 KVM_MP_STATE_OPERATING;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001674}
1675
1676int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1677 struct kvm_mp_state *mp_state)
1678{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001679 int rc = 0;
1680
1681 /* user space knows about this interface - let it control the state */
1682 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
1683
1684 switch (mp_state->mp_state) {
1685 case KVM_MP_STATE_STOPPED:
1686 kvm_s390_vcpu_stop(vcpu);
1687 break;
1688 case KVM_MP_STATE_OPERATING:
1689 kvm_s390_vcpu_start(vcpu);
1690 break;
1691 case KVM_MP_STATE_LOAD:
1692 case KVM_MP_STATE_CHECK_STOP:
1693 /* fall through - CHECK_STOP and LOAD are not supported yet */
1694 default:
1695 rc = -ENXIO;
1696 }
1697
1698 return rc;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001699}
1700
Dominik Dingelb31605c2014-03-25 13:47:11 +01001701bool kvm_s390_cmma_enabled(struct kvm *kvm)
1702{
1703 if (!MACHINE_IS_LPAR)
1704 return false;
1705 /* only enable for z10 and later */
1706 if (!MACHINE_HAS_EDAT1)
1707 return false;
1708 if (!kvm->arch.use_cmma)
1709 return false;
1710 return true;
1711}
1712
David Hildenbrand8ad35752014-03-14 11:00:21 +01001713static bool ibs_enabled(struct kvm_vcpu *vcpu)
1714{
1715 return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
1716}
1717
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001718static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
1719{
David Hildenbrand8ad35752014-03-14 11:00:21 +01001720retry:
1721 s390_vcpu_unblock(vcpu);
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001722 /*
1723 * We use MMU_RELOAD just to re-arm the ipte notifier for the
1724 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
1725 * This ensures that the ipte instruction for this request has
1726 * already finished. We might race against a second unmapper that
1727 * wants to set the blocking bit. Lets just retry the request loop.
1728 */
David Hildenbrand8ad35752014-03-14 11:00:21 +01001729 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001730 int rc;
1731 rc = gmap_ipte_notify(vcpu->arch.gmap,
Michael Muellerfda902c2014-05-13 16:58:30 +02001732 kvm_s390_get_prefix(vcpu),
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001733 PAGE_SIZE * 2);
1734 if (rc)
1735 return rc;
David Hildenbrand8ad35752014-03-14 11:00:21 +01001736 goto retry;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001737 }
David Hildenbrand8ad35752014-03-14 11:00:21 +01001738
David Hildenbrandd3d692c2014-07-29 08:53:36 +02001739 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
1740 vcpu->arch.sie_block->ihcpu = 0xffff;
1741 goto retry;
1742 }
1743
David Hildenbrand8ad35752014-03-14 11:00:21 +01001744 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
1745 if (!ibs_enabled(vcpu)) {
1746 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
1747 atomic_set_mask(CPUSTAT_IBS,
1748 &vcpu->arch.sie_block->cpuflags);
1749 }
1750 goto retry;
1751 }
1752
1753 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
1754 if (ibs_enabled(vcpu)) {
1755 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
1756 atomic_clear_mask(CPUSTAT_IBS,
1757 &vcpu->arch.sie_block->cpuflags);
1758 }
1759 goto retry;
1760 }
1761
David Hildenbrand0759d062014-05-13 16:54:32 +02001762 /* nothing to do, just clear the request */
1763 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
1764
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001765 return 0;
1766}
1767
Thomas Huthfa576c52014-05-06 17:20:16 +02001768/**
1769 * kvm_arch_fault_in_page - fault-in guest page if necessary
1770 * @vcpu: The corresponding virtual cpu
1771 * @gpa: Guest physical address
1772 * @writable: Whether the page should be writable or not
1773 *
1774 * Make sure that a guest page has been faulted-in on the host.
1775 *
1776 * Return: Zero on success, negative error code otherwise.
1777 */
1778long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001779{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001780 return gmap_fault(vcpu->arch.gmap, gpa,
1781 writable ? FAULT_FLAG_WRITE : 0);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001782}
1783
Dominik Dingel3c038e62013-10-07 17:11:48 +02001784static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
1785 unsigned long token)
1786{
1787 struct kvm_s390_interrupt inti;
Jens Freimann383d0b02014-07-29 15:11:49 +02001788 struct kvm_s390_irq irq;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001789
1790 if (start_token) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001791 irq.u.ext.ext_params2 = token;
1792 irq.type = KVM_S390_INT_PFAULT_INIT;
1793 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
Dominik Dingel3c038e62013-10-07 17:11:48 +02001794 } else {
1795 inti.type = KVM_S390_INT_PFAULT_DONE;
Jens Freimann383d0b02014-07-29 15:11:49 +02001796 inti.parm64 = token;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001797 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
1798 }
1799}
1800
1801void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
1802 struct kvm_async_pf *work)
1803{
1804 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
1805 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
1806}
1807
1808void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
1809 struct kvm_async_pf *work)
1810{
1811 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
1812 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
1813}
1814
1815void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
1816 struct kvm_async_pf *work)
1817{
1818 /* s390 will always inject the page directly */
1819}
1820
1821bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
1822{
1823 /*
1824 * s390 will always inject the page directly,
1825 * but we still want check_async_completion to cleanup
1826 */
1827 return true;
1828}
1829
1830static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
1831{
1832 hva_t hva;
1833 struct kvm_arch_async_pf arch;
1834 int rc;
1835
1836 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1837 return 0;
1838 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
1839 vcpu->arch.pfault_compare)
1840 return 0;
1841 if (psw_extint_disabled(vcpu))
1842 return 0;
David Hildenbrand9a022062014-08-05 17:40:47 +02001843 if (kvm_s390_vcpu_has_irq(vcpu, 0))
Dominik Dingel3c038e62013-10-07 17:11:48 +02001844 return 0;
1845 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
1846 return 0;
1847 if (!vcpu->arch.gmap->pfault_enabled)
1848 return 0;
1849
Heiko Carstens81480cc2014-01-01 16:36:07 +01001850 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
1851 hva += current->thread.gmap_addr & ~PAGE_MASK;
1852 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
Dominik Dingel3c038e62013-10-07 17:11:48 +02001853 return 0;
1854
1855 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
1856 return rc;
1857}
1858
Thomas Huth3fb4c402013-09-12 10:33:43 +02001859static int vcpu_pre_run(struct kvm_vcpu *vcpu)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001860{
Thomas Huth3fb4c402013-09-12 10:33:43 +02001861 int rc, cpuflags;
Carsten Ottee168bf82012-01-04 10:25:22 +01001862
Dominik Dingel3c038e62013-10-07 17:11:48 +02001863 /*
1864 * On s390 notifications for arriving pages will be delivered directly
1865 * to the guest but the house keeping for completed pfaults is
1866 * handled outside the worker.
1867 */
1868 kvm_check_async_pf_completion(vcpu);
1869
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001870 memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001871
1872 if (need_resched())
1873 schedule();
1874
Martin Schwidefskyd3a73ac2014-04-15 12:55:07 +02001875 if (test_cpu_flag(CIF_MCCK_PENDING))
Christian Borntraeger71cde582008-05-21 13:37:34 +02001876 s390_handle_mcck();
1877
Jens Freimann79395032014-04-17 10:10:30 +02001878 if (!kvm_is_ucontrol(vcpu->kvm)) {
1879 rc = kvm_s390_deliver_pending_interrupts(vcpu);
1880 if (rc)
1881 return rc;
1882 }
Carsten Otte0ff31862008-05-21 13:37:37 +02001883
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001884 rc = kvm_s390_handle_requests(vcpu);
1885 if (rc)
1886 return rc;
1887
David Hildenbrand27291e22014-01-23 12:26:52 +01001888 if (guestdbg_enabled(vcpu)) {
1889 kvm_s390_backup_guest_per_regs(vcpu);
1890 kvm_s390_patch_guest_per_regs(vcpu);
1891 }
1892
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001893 vcpu->arch.sie_block->icptcode = 0;
Thomas Huth3fb4c402013-09-12 10:33:43 +02001894 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
1895 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
1896 trace_kvm_s390_sie_enter(vcpu, cpuflags);
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02001897
Thomas Huth3fb4c402013-09-12 10:33:43 +02001898 return 0;
1899}
1900
Thomas Huth492d8642015-02-10 16:11:01 +01001901static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
1902{
1903 psw_t *psw = &vcpu->arch.sie_block->gpsw;
1904 u8 opcode;
1905 int rc;
1906
1907 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
1908 trace_kvm_s390_sie_fault(vcpu);
1909
1910 /*
1911 * We want to inject an addressing exception, which is defined as a
1912 * suppressing or terminating exception. However, since we came here
1913 * by a DAT access exception, the PSW still points to the faulting
1914 * instruction since DAT exceptions are nullifying. So we've got
1915 * to look up the current opcode to get the length of the instruction
1916 * to be able to forward the PSW.
1917 */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001918 rc = read_guest(vcpu, psw->addr, 0, &opcode, 1);
Thomas Huth492d8642015-02-10 16:11:01 +01001919 if (rc)
1920 return kvm_s390_inject_prog_cond(vcpu, rc);
1921 psw->addr = __rewind_psw(*psw, -insn_length(opcode));
1922
1923 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1924}
1925
Thomas Huth3fb4c402013-09-12 10:33:43 +02001926static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
1927{
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001928 int rc = -1;
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02001929
1930 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
1931 vcpu->arch.sie_block->icptcode);
1932 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
1933
David Hildenbrand27291e22014-01-23 12:26:52 +01001934 if (guestdbg_enabled(vcpu))
1935 kvm_s390_restore_guest_per_regs(vcpu);
1936
Thomas Huth3fb4c402013-09-12 10:33:43 +02001937 if (exit_reason >= 0) {
Martin Schwidefsky7c470532013-05-17 14:41:37 +02001938 rc = 0;
Thomas Huth210b16072013-09-19 16:26:18 +02001939 } else if (kvm_is_ucontrol(vcpu->kvm)) {
1940 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
1941 vcpu->run->s390_ucontrol.trans_exc_code =
1942 current->thread.gmap_addr;
1943 vcpu->run->s390_ucontrol.pgm_code = 0x10;
1944 rc = -EREMOTE;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001945
1946 } else if (current->thread.gmap_pfault) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001947 trace_kvm_s390_major_guest_pfault(vcpu);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001948 current->thread.gmap_pfault = 0;
Thomas Huthfa576c52014-05-06 17:20:16 +02001949 if (kvm_arch_setup_async_pf(vcpu)) {
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001950 rc = 0;
Thomas Huthfa576c52014-05-06 17:20:16 +02001951 } else {
1952 gpa_t gpa = current->thread.gmap_addr;
1953 rc = kvm_arch_fault_in_page(vcpu, gpa, 1);
1954 }
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001955 }
1956
Thomas Huth492d8642015-02-10 16:11:01 +01001957 if (rc == -1)
1958 rc = vcpu_post_run_fault_in_sie(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001959
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001960 memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
Thomas Huth3fb4c402013-09-12 10:33:43 +02001961
Thomas Hutha76ccff2013-09-12 10:33:44 +02001962 if (rc == 0) {
1963 if (kvm_is_ucontrol(vcpu->kvm))
Christian Borntraeger2955c832014-03-06 16:01:38 +01001964 /* Don't exit for host interrupts. */
1965 rc = vcpu->arch.sie_block->icptcode ? -EOPNOTSUPP : 0;
Thomas Hutha76ccff2013-09-12 10:33:44 +02001966 else
1967 rc = kvm_handle_sie_intercept(vcpu);
1968 }
1969
Thomas Huth3fb4c402013-09-12 10:33:43 +02001970 return rc;
1971}
1972
1973static int __vcpu_run(struct kvm_vcpu *vcpu)
1974{
1975 int rc, exit_reason;
1976
Thomas Huth800c1062013-09-12 10:33:45 +02001977 /*
1978 * We try to hold kvm->srcu during most of vcpu_run (except when run-
1979 * ning the guest), so that memslots (and other stuff) are protected
1980 */
1981 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1982
Thomas Hutha76ccff2013-09-12 10:33:44 +02001983 do {
1984 rc = vcpu_pre_run(vcpu);
1985 if (rc)
1986 break;
Thomas Huth3fb4c402013-09-12 10:33:43 +02001987
Thomas Huth800c1062013-09-12 10:33:45 +02001988 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Thomas Hutha76ccff2013-09-12 10:33:44 +02001989 /*
1990 * As PF_VCPU will be used in fault handler, between
1991 * guest_enter and guest_exit should be no uaccess.
1992 */
1993 preempt_disable();
1994 kvm_guest_enter();
1995 preempt_enable();
1996 exit_reason = sie64a(vcpu->arch.sie_block,
1997 vcpu->run->s.regs.gprs);
1998 kvm_guest_exit();
Thomas Huth800c1062013-09-12 10:33:45 +02001999 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002000
Thomas Hutha76ccff2013-09-12 10:33:44 +02002001 rc = vcpu_post_run(vcpu, exit_reason);
David Hildenbrand27291e22014-01-23 12:26:52 +01002002 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002003
Thomas Huth800c1062013-09-12 10:33:45 +02002004 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Carsten Ottee168bf82012-01-04 10:25:22 +01002005 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002006}
2007
David Hildenbrandb028ee32014-07-17 10:47:43 +02002008static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2009{
2010 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2011 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2012 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2013 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2014 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2015 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002016 /* some control register changes require a tlb flush */
2017 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002018 }
2019 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
2020 vcpu->arch.sie_block->cputm = kvm_run->s.regs.cputm;
2021 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2022 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2023 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2024 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2025 }
2026 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2027 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2028 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2029 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
David Hildenbrand9fbd8082014-10-09 15:01:38 +02002030 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
2031 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002032 }
2033 kvm_run->kvm_dirty_regs = 0;
2034}
2035
2036static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2037{
2038 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
2039 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
2040 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
2041 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
2042 kvm_run->s.regs.cputm = vcpu->arch.sie_block->cputm;
2043 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
2044 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
2045 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
2046 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
2047 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
2048 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
2049 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
2050}
2051
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002052int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2053{
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002054 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002055 sigset_t sigsaved;
2056
David Hildenbrand27291e22014-01-23 12:26:52 +01002057 if (guestdbg_exit_pending(vcpu)) {
2058 kvm_s390_prepare_debug_exit(vcpu);
2059 return 0;
2060 }
2061
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002062 if (vcpu->sigset_active)
2063 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2064
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002065 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
2066 kvm_s390_vcpu_start(vcpu);
2067 } else if (is_vcpu_stopped(vcpu)) {
2068 pr_err_ratelimited("kvm-s390: can't run stopped vcpu %d\n",
2069 vcpu->vcpu_id);
2070 return -EINVAL;
2071 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002072
David Hildenbrandb028ee32014-07-17 10:47:43 +02002073 sync_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002074
Heiko Carstensdab4079d2009-06-12 10:26:32 +02002075 might_fault();
Thomas Hutha76ccff2013-09-12 10:33:44 +02002076 rc = __vcpu_run(vcpu);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +02002077
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002078 if (signal_pending(current) && !rc) {
2079 kvm_run->exit_reason = KVM_EXIT_INTR;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002080 rc = -EINTR;
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002081 }
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002082
David Hildenbrand27291e22014-01-23 12:26:52 +01002083 if (guestdbg_exit_pending(vcpu) && !rc) {
2084 kvm_s390_prepare_debug_exit(vcpu);
2085 rc = 0;
2086 }
2087
Heiko Carstensb8e660b2010-02-26 22:37:41 +01002088 if (rc == -EOPNOTSUPP) {
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002089 /* intercept cannot be handled in-kernel, prepare kvm-run */
2090 kvm_run->exit_reason = KVM_EXIT_S390_SIEIC;
2091 kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002092 kvm_run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
2093 kvm_run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
2094 rc = 0;
2095 }
2096
2097 if (rc == -EREMOTE) {
2098 /* intercept was handled, but userspace support is needed
2099 * kvm_run has been prepared by the handler */
2100 rc = 0;
2101 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002102
David Hildenbrandb028ee32014-07-17 10:47:43 +02002103 store_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002104
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002105 if (vcpu->sigset_active)
2106 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2107
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002108 vcpu->stat.exit_userspace++;
Heiko Carstens7e8e6ab2008-04-04 15:12:35 +02002109 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002110}
2111
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002112/*
2113 * store status at address
2114 * we use have two special cases:
2115 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2116 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2117 */
Heiko Carstensd0bce602014-01-01 16:45:58 +01002118int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002119{
Carsten Otte092670c2011-07-24 10:48:22 +02002120 unsigned char archmode = 1;
Michael Muellerfda902c2014-05-13 16:58:30 +02002121 unsigned int px;
Thomas Huth178bd782013-11-13 20:28:18 +01002122 u64 clkcomp;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002123 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002124
Heiko Carstensd0bce602014-01-01 16:45:58 +01002125 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
2126 if (write_guest_abs(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002127 return -EFAULT;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002128 gpa = SAVE_AREA_BASE;
2129 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
2130 if (write_guest_real(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002131 return -EFAULT;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002132 gpa = kvm_s390_real_to_abs(vcpu, SAVE_AREA_BASE);
2133 }
2134 rc = write_guest_abs(vcpu, gpa + offsetof(struct save_area, fp_regs),
2135 vcpu->arch.guest_fpregs.fprs, 128);
2136 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, gp_regs),
2137 vcpu->run->s.regs.gprs, 128);
2138 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, psw),
2139 &vcpu->arch.sie_block->gpsw, 16);
Michael Muellerfda902c2014-05-13 16:58:30 +02002140 px = kvm_s390_get_prefix(vcpu);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002141 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, pref_reg),
Michael Muellerfda902c2014-05-13 16:58:30 +02002142 &px, 4);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002143 rc |= write_guest_abs(vcpu,
2144 gpa + offsetof(struct save_area, fp_ctrl_reg),
2145 &vcpu->arch.guest_fpregs.fpc, 4);
2146 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, tod_reg),
2147 &vcpu->arch.sie_block->todpr, 4);
2148 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, timer),
2149 &vcpu->arch.sie_block->cputm, 8);
Thomas Huth178bd782013-11-13 20:28:18 +01002150 clkcomp = vcpu->arch.sie_block->ckc >> 8;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002151 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, clk_cmp),
2152 &clkcomp, 8);
2153 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, acc_regs),
2154 &vcpu->run->s.regs.acrs, 64);
2155 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, ctrl_regs),
2156 &vcpu->arch.sie_block->gcr, 128);
2157 return rc ? -EFAULT : 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002158}
2159
Thomas Huthe8798922013-11-06 15:46:33 +01002160int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2161{
2162 /*
2163 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
2164 * copying in vcpu load/put. Lets update our copies before we save
2165 * it into the save area
2166 */
2167 save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
2168 save_fp_regs(vcpu->arch.guest_fpregs.fprs);
2169 save_access_regs(vcpu->run->s.regs.acrs);
2170
2171 return kvm_s390_store_status_unloaded(vcpu, addr);
2172}
2173
Eric Farmanbc17de72014-04-14 16:01:09 -04002174/*
2175 * store additional status at address
2176 */
2177int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
2178 unsigned long gpa)
2179{
2180 /* Only bits 0-53 are used for address formation */
2181 if (!(gpa & ~0x3ff))
2182 return 0;
2183
2184 return write_guest_abs(vcpu, gpa & ~0x3ff,
2185 (void *)&vcpu->run->s.regs.vrs, 512);
2186}
2187
2188int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr)
2189{
2190 if (!test_kvm_facility(vcpu->kvm, 129))
2191 return 0;
2192
2193 /*
2194 * The guest VXRS are in the host VXRs due to the lazy
2195 * copying in vcpu load/put. Let's update our copies before we save
2196 * it into the save area.
2197 */
2198 save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
2199
2200 return kvm_s390_store_adtl_status_unloaded(vcpu, addr);
2201}
2202
David Hildenbrand8ad35752014-03-14 11:00:21 +01002203static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2204{
2205 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
2206 kvm_make_request(KVM_REQ_DISABLE_IBS, vcpu);
2207 exit_sie_sync(vcpu);
2208}
2209
2210static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
2211{
2212 unsigned int i;
2213 struct kvm_vcpu *vcpu;
2214
2215 kvm_for_each_vcpu(i, vcpu, kvm) {
2216 __disable_ibs_on_vcpu(vcpu);
2217 }
2218}
2219
2220static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2221{
2222 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
2223 kvm_make_request(KVM_REQ_ENABLE_IBS, vcpu);
2224 exit_sie_sync(vcpu);
2225}
2226
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002227void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
2228{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002229 int i, online_vcpus, started_vcpus = 0;
2230
2231 if (!is_vcpu_stopped(vcpu))
2232 return;
2233
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002234 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002235 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002236 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002237 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2238
2239 for (i = 0; i < online_vcpus; i++) {
2240 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
2241 started_vcpus++;
2242 }
2243
2244 if (started_vcpus == 0) {
2245 /* we're the only active VCPU -> speed it up */
2246 __enable_ibs_on_vcpu(vcpu);
2247 } else if (started_vcpus == 1) {
2248 /*
2249 * As we are starting a second VCPU, we have to disable
2250 * the IBS facility on all VCPUs to remove potentially
2251 * oustanding ENABLE requests.
2252 */
2253 __disable_ibs_on_all_vcpus(vcpu->kvm);
2254 }
2255
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002256 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002257 /*
2258 * Another VCPU might have used IBS while we were offline.
2259 * Let's play safe and flush the VCPU at startup.
2260 */
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002261 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002262 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002263 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002264}
2265
2266void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
2267{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002268 int i, online_vcpus, started_vcpus = 0;
2269 struct kvm_vcpu *started_vcpu = NULL;
2270
2271 if (is_vcpu_stopped(vcpu))
2272 return;
2273
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002274 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002275 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002276 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002277 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2278
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002279 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
David Hildenbrand6cddd432014-10-15 16:48:53 +02002280 kvm_s390_clear_stop_irq(vcpu);
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002281
David Hildenbrand6cddd432014-10-15 16:48:53 +02002282 atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002283 __disable_ibs_on_vcpu(vcpu);
2284
2285 for (i = 0; i < online_vcpus; i++) {
2286 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
2287 started_vcpus++;
2288 started_vcpu = vcpu->kvm->vcpus[i];
2289 }
2290 }
2291
2292 if (started_vcpus == 1) {
2293 /*
2294 * As we only have one VCPU left, we want to enable the
2295 * IBS facility for that VCPU to speed it up.
2296 */
2297 __enable_ibs_on_vcpu(started_vcpu);
2298 }
2299
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002300 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002301 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002302}
2303
Cornelia Huckd6712df2012-12-20 15:32:11 +01002304static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2305 struct kvm_enable_cap *cap)
2306{
2307 int r;
2308
2309 if (cap->flags)
2310 return -EINVAL;
2311
2312 switch (cap->cap) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002313 case KVM_CAP_S390_CSS_SUPPORT:
2314 if (!vcpu->kvm->arch.css_support) {
2315 vcpu->kvm->arch.css_support = 1;
2316 trace_kvm_s390_enable_css(vcpu->kvm);
2317 }
2318 r = 0;
2319 break;
Cornelia Huckd6712df2012-12-20 15:32:11 +01002320 default:
2321 r = -EINVAL;
2322 break;
2323 }
2324 return r;
2325}
2326
Thomas Huth41408c282015-02-06 15:01:21 +01002327static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
2328 struct kvm_s390_mem_op *mop)
2329{
2330 void __user *uaddr = (void __user *)mop->buf;
2331 void *tmpbuf = NULL;
2332 int r, srcu_idx;
2333 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
2334 | KVM_S390_MEMOP_F_CHECK_ONLY;
2335
2336 if (mop->flags & ~supported_flags)
2337 return -EINVAL;
2338
2339 if (mop->size > MEM_OP_MAX_SIZE)
2340 return -E2BIG;
2341
2342 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
2343 tmpbuf = vmalloc(mop->size);
2344 if (!tmpbuf)
2345 return -ENOMEM;
2346 }
2347
2348 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2349
2350 switch (mop->op) {
2351 case KVM_S390_MEMOP_LOGICAL_READ:
2352 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
2353 r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, false);
2354 break;
2355 }
2356 r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2357 if (r == 0) {
2358 if (copy_to_user(uaddr, tmpbuf, mop->size))
2359 r = -EFAULT;
2360 }
2361 break;
2362 case KVM_S390_MEMOP_LOGICAL_WRITE:
2363 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
2364 r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, true);
2365 break;
2366 }
2367 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
2368 r = -EFAULT;
2369 break;
2370 }
2371 r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2372 break;
2373 default:
2374 r = -EINVAL;
2375 }
2376
2377 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2378
2379 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
2380 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
2381
2382 vfree(tmpbuf);
2383 return r;
2384}
2385
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002386long kvm_arch_vcpu_ioctl(struct file *filp,
2387 unsigned int ioctl, unsigned long arg)
2388{
2389 struct kvm_vcpu *vcpu = filp->private_data;
2390 void __user *argp = (void __user *)arg;
Thomas Huth800c1062013-09-12 10:33:45 +02002391 int idx;
Avi Kivitybc923cc2010-05-13 12:21:46 +03002392 long r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002393
Avi Kivity937366242010-05-13 12:35:17 +03002394 switch (ioctl) {
Jens Freimann47b43c52014-11-11 20:57:06 +01002395 case KVM_S390_IRQ: {
2396 struct kvm_s390_irq s390irq;
2397
2398 r = -EFAULT;
2399 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
2400 break;
2401 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
2402 break;
2403 }
Avi Kivity937366242010-05-13 12:35:17 +03002404 case KVM_S390_INTERRUPT: {
Carsten Otteba5c1e92008-03-25 18:47:26 +01002405 struct kvm_s390_interrupt s390int;
Jens Freimann383d0b02014-07-29 15:11:49 +02002406 struct kvm_s390_irq s390irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002407
Avi Kivity937366242010-05-13 12:35:17 +03002408 r = -EFAULT;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002409 if (copy_from_user(&s390int, argp, sizeof(s390int)))
Avi Kivity937366242010-05-13 12:35:17 +03002410 break;
Jens Freimann383d0b02014-07-29 15:11:49 +02002411 if (s390int_to_s390irq(&s390int, &s390irq))
2412 return -EINVAL;
2413 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
Avi Kivity937366242010-05-13 12:35:17 +03002414 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002415 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002416 case KVM_S390_STORE_STATUS:
Thomas Huth800c1062013-09-12 10:33:45 +02002417 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002418 r = kvm_s390_vcpu_store_status(vcpu, arg);
Thomas Huth800c1062013-09-12 10:33:45 +02002419 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002420 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002421 case KVM_S390_SET_INITIAL_PSW: {
2422 psw_t psw;
2423
Avi Kivitybc923cc2010-05-13 12:21:46 +03002424 r = -EFAULT;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002425 if (copy_from_user(&psw, argp, sizeof(psw)))
Avi Kivitybc923cc2010-05-13 12:21:46 +03002426 break;
2427 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
2428 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002429 }
2430 case KVM_S390_INITIAL_RESET:
Avi Kivitybc923cc2010-05-13 12:21:46 +03002431 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
2432 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02002433 case KVM_SET_ONE_REG:
2434 case KVM_GET_ONE_REG: {
2435 struct kvm_one_reg reg;
2436 r = -EFAULT;
2437 if (copy_from_user(&reg, argp, sizeof(reg)))
2438 break;
2439 if (ioctl == KVM_SET_ONE_REG)
2440 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
2441 else
2442 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
2443 break;
2444 }
Carsten Otte27e03932012-01-04 10:25:21 +01002445#ifdef CONFIG_KVM_S390_UCONTROL
2446 case KVM_S390_UCAS_MAP: {
2447 struct kvm_s390_ucas_mapping ucasmap;
2448
2449 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2450 r = -EFAULT;
2451 break;
2452 }
2453
2454 if (!kvm_is_ucontrol(vcpu->kvm)) {
2455 r = -EINVAL;
2456 break;
2457 }
2458
2459 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
2460 ucasmap.vcpu_addr, ucasmap.length);
2461 break;
2462 }
2463 case KVM_S390_UCAS_UNMAP: {
2464 struct kvm_s390_ucas_mapping ucasmap;
2465
2466 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2467 r = -EFAULT;
2468 break;
2469 }
2470
2471 if (!kvm_is_ucontrol(vcpu->kvm)) {
2472 r = -EINVAL;
2473 break;
2474 }
2475
2476 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
2477 ucasmap.length);
2478 break;
2479 }
2480#endif
Carsten Otteccc79102012-01-04 10:25:26 +01002481 case KVM_S390_VCPU_FAULT: {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002482 r = gmap_fault(vcpu->arch.gmap, arg, 0);
Carsten Otteccc79102012-01-04 10:25:26 +01002483 break;
2484 }
Cornelia Huckd6712df2012-12-20 15:32:11 +01002485 case KVM_ENABLE_CAP:
2486 {
2487 struct kvm_enable_cap cap;
2488 r = -EFAULT;
2489 if (copy_from_user(&cap, argp, sizeof(cap)))
2490 break;
2491 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2492 break;
2493 }
Thomas Huth41408c282015-02-06 15:01:21 +01002494 case KVM_S390_MEM_OP: {
2495 struct kvm_s390_mem_op mem_op;
2496
2497 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
2498 r = kvm_s390_guest_mem_op(vcpu, &mem_op);
2499 else
2500 r = -EFAULT;
2501 break;
2502 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002503 default:
Carsten Otte3e6afcf2012-01-04 10:25:30 +01002504 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002505 }
Avi Kivitybc923cc2010-05-13 12:21:46 +03002506 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002507}
2508
Carsten Otte5b1c1492012-01-04 10:25:23 +01002509int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2510{
2511#ifdef CONFIG_KVM_S390_UCONTROL
2512 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
2513 && (kvm_is_ucontrol(vcpu->kvm))) {
2514 vmf->page = virt_to_page(vcpu->arch.sie_block);
2515 get_page(vmf->page);
2516 return 0;
2517 }
2518#endif
2519 return VM_FAULT_SIGBUS;
2520}
2521
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05302522int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2523 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09002524{
2525 return 0;
2526}
2527
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002528/* Section: memory related */
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002529int kvm_arch_prepare_memory_region(struct kvm *kvm,
2530 struct kvm_memory_slot *memslot,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09002531 struct kvm_userspace_memory_region *mem,
2532 enum kvm_mr_change change)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002533{
Nick Wangdd2887e2013-03-25 17:22:57 +01002534 /* A few sanity checks. We can have memory slots which have to be
2535 located/ended at a segment boundary (1MB). The memory in userland is
2536 ok to be fragmented into various different vmas. It is okay to mmap()
2537 and munmap() stuff in this slot after doing this call at any time */
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002538
Carsten Otte598841c2011-07-24 10:48:21 +02002539 if (mem->userspace_addr & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002540 return -EINVAL;
2541
Carsten Otte598841c2011-07-24 10:48:21 +02002542 if (mem->memory_size & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002543 return -EINVAL;
2544
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002545 return 0;
2546}
2547
2548void kvm_arch_commit_memory_region(struct kvm *kvm,
2549 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002550 const struct kvm_memory_slot *old,
2551 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002552{
Carsten Ottef7850c92011-07-24 10:48:23 +02002553 int rc;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002554
Christian Borntraeger2cef4de2013-03-25 17:22:48 +01002555 /* If the basics of the memslot do not change, we do not want
2556 * to update the gmap. Every update causes several unnecessary
2557 * segment translation exceptions. This is usually handled just
2558 * fine by the normal fault handler + gmap, but it will also
2559 * cause faults on the prefix page of running guest CPUs.
2560 */
2561 if (old->userspace_addr == mem->userspace_addr &&
2562 old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
2563 old->npages * PAGE_SIZE == mem->memory_size)
2564 return;
Carsten Otte598841c2011-07-24 10:48:21 +02002565
2566 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
2567 mem->guest_phys_addr, mem->memory_size);
2568 if (rc)
Carsten Ottef7850c92011-07-24 10:48:23 +02002569 printk(KERN_WARNING "kvm-s390: failed to commit memory region\n");
Carsten Otte598841c2011-07-24 10:48:21 +02002570 return;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002571}
2572
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002573static int __init kvm_s390_init(void)
2574{
Michael Mueller9d8d5782015-02-02 15:42:51 +01002575 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002576}
2577
2578static void __exit kvm_s390_exit(void)
2579{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002580 kvm_exit();
2581}
2582
2583module_init(kvm_s390_init);
2584module_exit(kvm_s390_exit);
Cornelia Huck566af942013-05-27 18:42:33 +02002585
2586/*
2587 * Enable autoloading of the kvm module.
2588 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
2589 * since x86 takes a different approach.
2590 */
2591#include <linux/miscdevice.h>
2592MODULE_ALIAS_MISCDEV(KVM_MINOR);
2593MODULE_ALIAS("devname:kvm");