blob: 641639b2fbd01a2e09ca6f7d00568d0c66accbff [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 Huth41408c22015-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
David Hildenbrandea2cdd22015-05-20 13:24:02 +020039#define KMSG_COMPONENT "kvm-s390"
40#undef pr_fmt
41#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
42
Cornelia Huck5786fff2012-07-23 17:20:29 +020043#define CREATE_TRACE_POINTS
44#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020045#include "trace-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020046
Thomas Huth41408c22015-02-06 15:01:21 +010047#define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
Jens Freimann816c7662014-11-24 17:13:46 +010048#define LOCAL_IRQS 32
49#define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
50 (KVM_MAX_VCPUS + LOCAL_IRQS))
Thomas Huth41408c22015-02-06 15:01:21 +010051
Heiko Carstensb0c632d2008-03-25 18:47:20 +010052#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
53
54struct kvm_stats_debugfs_item debugfs_entries[] = {
55 { "userspace_handled", VCPU_STAT(exit_userspace) },
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +020056 { "exit_null", VCPU_STAT(exit_null) },
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010057 { "exit_validity", VCPU_STAT(exit_validity) },
58 { "exit_stop_request", VCPU_STAT(exit_stop_request) },
59 { "exit_external_request", VCPU_STAT(exit_external_request) },
60 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010061 { "exit_instruction", VCPU_STAT(exit_instruction) },
62 { "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
63 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
Paolo Bonzinif7819512015-02-04 18:20:58 +010064 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
David Hildenbrandce2e4f02014-07-11 10:00:43 +020065 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020066 { "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010067 { "instruction_lctl", VCPU_STAT(instruction_lctl) },
David Hildenbrandaba07502014-01-23 10:47:13 +010068 { "instruction_stctl", VCPU_STAT(instruction_stctl) },
69 { "instruction_stctg", VCPU_STAT(instruction_stctg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010070 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020071 { "deliver_external_call", VCPU_STAT(deliver_external_call) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010072 { "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
73 { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
74 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
75 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
76 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
77 { "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
78 { "exit_wait_state", VCPU_STAT(exit_wait_state) },
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020079 { "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010080 { "instruction_stidp", VCPU_STAT(instruction_stidp) },
81 { "instruction_spx", VCPU_STAT(instruction_spx) },
82 { "instruction_stpx", VCPU_STAT(instruction_stpx) },
83 { "instruction_stap", VCPU_STAT(instruction_stap) },
84 { "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
Heiko Carstens8a2422342014-01-10 14:33:28 +010085 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010086 { "instruction_stsch", VCPU_STAT(instruction_stsch) },
87 { "instruction_chsc", VCPU_STAT(instruction_chsc) },
Konstantin Weitzb31288f2013-04-17 17:36:29 +020088 { "instruction_essa", VCPU_STAT(instruction_essa) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010089 { "instruction_stsi", VCPU_STAT(instruction_stsi) },
90 { "instruction_stfl", VCPU_STAT(instruction_stfl) },
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +020091 { "instruction_tprot", VCPU_STAT(instruction_tprot) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010092 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
Cornelia Huckbd59d3a2011-11-17 11:00:42 +010093 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020094 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010095 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +020096 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
97 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010098 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +020099 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
100 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
Eric Farmancd7b4b62015-02-12 09:06:34 -0500101 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100102 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
103 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
104 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200105 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
106 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
107 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
Christian Borntraeger388186b2011-10-30 15:17:03 +0100108 { "diagnose_10", VCPU_STAT(diagnose_10) },
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100109 { "diagnose_44", VCPU_STAT(diagnose_44) },
Konstantin Weitz41628d32012-04-25 15:30:38 +0200110 { "diagnose_9c", VCPU_STAT(diagnose_9c) },
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100111 { NULL }
112};
113
Michael Mueller9d8d5782015-02-02 15:42:51 +0100114/* upper facilities limit for kvm */
115unsigned long kvm_s390_fac_list_mask[] = {
Christian Borntraegera3ed8da2015-03-18 13:54:31 +0100116 0xffe6fffbfcfdfc40UL,
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100117 0x005e800000000000UL,
Michael Mueller9d8d5782015-02-02 15:42:51 +0100118};
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100119
Michael Mueller9d8d5782015-02-02 15:42:51 +0100120unsigned long kvm_s390_fac_list_mask_size(void)
Michael Mueller78c4b592013-07-26 15:04:04 +0200121{
Michael Mueller9d8d5782015-02-02 15:42:51 +0100122 BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
123 return ARRAY_SIZE(kvm_s390_fac_list_mask);
Michael Mueller78c4b592013-07-26 15:04:04 +0200124}
125
Michael Mueller9d8d5782015-02-02 15:42:51 +0100126static struct gmap_notifier gmap_notifier;
127
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100128/* Section: not file related */
Radim Krčmář13a34e02014-08-28 15:13:03 +0200129int kvm_arch_hardware_enable(void)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100130{
131 /* every s390 is virtualization enabled ;-) */
Alexander Graf10474ae2009-09-15 11:37:46 +0200132 return 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100133}
134
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200135static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address);
136
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100137int kvm_arch_hardware_setup(void)
138{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200139 gmap_notifier.notifier_call = kvm_gmap_notifier;
140 gmap_register_ipte_notifier(&gmap_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100141 return 0;
142}
143
144void kvm_arch_hardware_unsetup(void)
145{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200146 gmap_unregister_ipte_notifier(&gmap_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100147}
148
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100149int kvm_arch_init(void *opaque)
150{
Cornelia Huck84877d92014-09-02 10:27:35 +0100151 /* Register floating interrupt controller interface. */
152 return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100153}
154
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100155/* Section: device related */
156long kvm_arch_dev_ioctl(struct file *filp,
157 unsigned int ioctl, unsigned long arg)
158{
159 if (ioctl == KVM_S390_ENABLE_SIE)
160 return s390_enable_sie();
161 return -EINVAL;
162}
163
Alexander Graf784aa3d2014-07-14 18:27:35 +0200164int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100165{
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100166 int r;
167
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200168 switch (ext) {
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100169 case KVM_CAP_S390_PSW:
Christian Borntraegerb6cf8782011-09-20 17:07:29 +0200170 case KVM_CAP_S390_GMAP:
Christian Borntraeger52e16b12011-11-17 11:00:44 +0100171 case KVM_CAP_SYNC_MMU:
Carsten Otte1efd0f52012-01-04 10:25:29 +0100172#ifdef CONFIG_KVM_S390_UCONTROL
173 case KVM_CAP_S390_UCONTROL:
174#endif
Dominik Dingel3c038e62013-10-07 17:11:48 +0200175 case KVM_CAP_ASYNC_PF:
Christian Borntraeger60b413c2012-01-11 11:20:31 +0100176 case KVM_CAP_SYNC_REGS:
Carsten Otte14eebd92012-05-15 14:15:26 +0200177 case KVM_CAP_ONE_REG:
Cornelia Huckd6712df2012-12-20 15:32:11 +0100178 case KVM_CAP_ENABLE_CAP:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100179 case KVM_CAP_S390_CSS_SUPPORT:
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100180 case KVM_CAP_IOEVENTFD:
Jens Freimannc05c4182013-10-07 16:13:45 +0200181 case KVM_CAP_DEVICE_CTRL:
Cornelia Huckd938dc52013-10-23 18:26:34 +0200182 case KVM_CAP_ENABLE_CAP_VM:
Cornelia Huck78599d92014-07-15 09:54:39 +0200183 case KVM_CAP_S390_IRQCHIP:
Dominik Dingelf2061652014-04-09 13:13:00 +0200184 case KVM_CAP_VM_ATTRIBUTES:
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200185 case KVM_CAP_MP_STATE:
Jens Freimann47b43c52014-11-11 20:57:06 +0100186 case KVM_CAP_S390_INJECT_IRQ:
David Hildenbrand2444b352014-10-09 14:10:13 +0200187 case KVM_CAP_S390_USER_SIGP:
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100188 case KVM_CAP_S390_USER_STSI:
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400189 case KVM_CAP_S390_SKEYS:
Jens Freimann816c7662014-11-24 17:13:46 +0100190 case KVM_CAP_S390_IRQ_STATE:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100191 r = 1;
192 break;
Thomas Huth41408c22015-02-06 15:01:21 +0100193 case KVM_CAP_S390_MEM_OP:
194 r = MEM_OP_MAX_SIZE;
195 break;
Christian Borntraegere726b1b2012-05-02 10:50:38 +0200196 case KVM_CAP_NR_VCPUS:
197 case KVM_CAP_MAX_VCPUS:
198 r = KVM_MAX_VCPUS;
199 break;
Nick Wange1e2e602013-03-25 17:22:58 +0100200 case KVM_CAP_NR_MEMSLOTS:
201 r = KVM_USER_MEM_SLOTS;
202 break;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200203 case KVM_CAP_S390_COW:
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100204 r = MACHINE_HAS_ESOP;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200205 break;
Eric Farman68c55752014-06-09 10:57:26 -0400206 case KVM_CAP_S390_VECTOR_REGISTERS:
207 r = MACHINE_HAS_VX;
208 break;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200209 default:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100210 r = 0;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200211 }
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100212 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100213}
214
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400215static void kvm_s390_sync_dirty_log(struct kvm *kvm,
216 struct kvm_memory_slot *memslot)
217{
218 gfn_t cur_gfn, last_gfn;
219 unsigned long address;
220 struct gmap *gmap = kvm->arch.gmap;
221
222 down_read(&gmap->mm->mmap_sem);
223 /* Loop over all guest pages */
224 last_gfn = memslot->base_gfn + memslot->npages;
225 for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
226 address = gfn_to_hva_memslot(memslot, cur_gfn);
227
228 if (gmap_test_and_clear_dirty(address, gmap))
229 mark_page_dirty(kvm, cur_gfn);
230 }
231 up_read(&gmap->mm->mmap_sem);
232}
233
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100234/* Section: vm related */
235/*
236 * Get (and clear) the dirty memory log for a memory slot.
237 */
238int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
239 struct kvm_dirty_log *log)
240{
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400241 int r;
242 unsigned long n;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200243 struct kvm_memslots *slots;
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400244 struct kvm_memory_slot *memslot;
245 int is_dirty = 0;
246
247 mutex_lock(&kvm->slots_lock);
248
249 r = -EINVAL;
250 if (log->slot >= KVM_USER_MEM_SLOTS)
251 goto out;
252
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200253 slots = kvm_memslots(kvm);
254 memslot = id_to_memslot(slots, log->slot);
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400255 r = -ENOENT;
256 if (!memslot->dirty_bitmap)
257 goto out;
258
259 kvm_s390_sync_dirty_log(kvm, memslot);
260 r = kvm_get_dirty_log(kvm, log, &is_dirty);
261 if (r)
262 goto out;
263
264 /* Clear the dirty log */
265 if (is_dirty) {
266 n = kvm_dirty_bitmap_bytes(memslot);
267 memset(memslot->dirty_bitmap, 0, n);
268 }
269 r = 0;
270out:
271 mutex_unlock(&kvm->slots_lock);
272 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100273}
274
Cornelia Huckd938dc52013-10-23 18:26:34 +0200275static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
276{
277 int r;
278
279 if (cap->flags)
280 return -EINVAL;
281
282 switch (cap->cap) {
Cornelia Huck84223592013-07-15 13:36:01 +0200283 case KVM_CAP_S390_IRQCHIP:
284 kvm->arch.use_irqchip = 1;
285 r = 0;
286 break;
David Hildenbrand2444b352014-10-09 14:10:13 +0200287 case KVM_CAP_S390_USER_SIGP:
288 kvm->arch.user_sigp = 1;
289 r = 0;
290 break;
Eric Farman68c55752014-06-09 10:57:26 -0400291 case KVM_CAP_S390_VECTOR_REGISTERS:
Michael Mueller18280d82015-03-16 16:05:41 +0100292 if (MACHINE_HAS_VX) {
293 set_kvm_facility(kvm->arch.model.fac->mask, 129);
294 set_kvm_facility(kvm->arch.model.fac->list, 129);
295 r = 0;
296 } else
297 r = -EINVAL;
Eric Farman68c55752014-06-09 10:57:26 -0400298 break;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100299 case KVM_CAP_S390_USER_STSI:
300 kvm->arch.user_stsi = 1;
301 r = 0;
302 break;
Cornelia Huckd938dc52013-10-23 18:26:34 +0200303 default:
304 r = -EINVAL;
305 break;
306 }
307 return r;
308}
309
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100310static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
311{
312 int ret;
313
314 switch (attr->attr) {
315 case KVM_S390_VM_MEM_LIMIT_SIZE:
316 ret = 0;
317 if (put_user(kvm->arch.gmap->asce_end, (u64 __user *)attr->addr))
318 ret = -EFAULT;
319 break;
320 default:
321 ret = -ENXIO;
322 break;
323 }
324 return ret;
325}
326
327static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200328{
329 int ret;
330 unsigned int idx;
331 switch (attr->attr) {
332 case KVM_S390_VM_MEM_ENABLE_CMMA:
Dominik Dingele6db1d62015-05-07 15:41:57 +0200333 /* enable CMMA only for z10 and later (EDAT_1) */
334 ret = -EINVAL;
335 if (!MACHINE_IS_LPAR || !MACHINE_HAS_EDAT1)
336 break;
337
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200338 ret = -EBUSY;
339 mutex_lock(&kvm->lock);
340 if (atomic_read(&kvm->online_vcpus) == 0) {
341 kvm->arch.use_cmma = 1;
342 ret = 0;
343 }
344 mutex_unlock(&kvm->lock);
345 break;
346 case KVM_S390_VM_MEM_CLR_CMMA:
347 mutex_lock(&kvm->lock);
348 idx = srcu_read_lock(&kvm->srcu);
Dominik Dingela13cff32014-10-23 12:07:14 +0200349 s390_reset_cmma(kvm->arch.gmap->mm);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200350 srcu_read_unlock(&kvm->srcu, idx);
351 mutex_unlock(&kvm->lock);
352 ret = 0;
353 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100354 case KVM_S390_VM_MEM_LIMIT_SIZE: {
355 unsigned long new_limit;
356
357 if (kvm_is_ucontrol(kvm))
358 return -EINVAL;
359
360 if (get_user(new_limit, (u64 __user *)attr->addr))
361 return -EFAULT;
362
363 if (new_limit > kvm->arch.gmap->asce_end)
364 return -E2BIG;
365
366 ret = -EBUSY;
367 mutex_lock(&kvm->lock);
368 if (atomic_read(&kvm->online_vcpus) == 0) {
369 /* gmap_alloc will round the limit up */
370 struct gmap *new = gmap_alloc(current->mm, new_limit);
371
372 if (!new) {
373 ret = -ENOMEM;
374 } else {
375 gmap_free(kvm->arch.gmap);
376 new->private = kvm;
377 kvm->arch.gmap = new;
378 ret = 0;
379 }
380 }
381 mutex_unlock(&kvm->lock);
382 break;
383 }
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200384 default:
385 ret = -ENXIO;
386 break;
387 }
388 return ret;
389}
390
Tony Krowiaka374e892014-09-03 10:13:53 +0200391static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
392
393static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
394{
395 struct kvm_vcpu *vcpu;
396 int i;
397
Michael Mueller9d8d5782015-02-02 15:42:51 +0100398 if (!test_kvm_facility(kvm, 76))
Tony Krowiaka374e892014-09-03 10:13:53 +0200399 return -EINVAL;
400
401 mutex_lock(&kvm->lock);
402 switch (attr->attr) {
403 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
404 get_random_bytes(
405 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
406 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
407 kvm->arch.crypto.aes_kw = 1;
408 break;
409 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
410 get_random_bytes(
411 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
412 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
413 kvm->arch.crypto.dea_kw = 1;
414 break;
415 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
416 kvm->arch.crypto.aes_kw = 0;
417 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
418 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
419 break;
420 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
421 kvm->arch.crypto.dea_kw = 0;
422 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
423 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
424 break;
425 default:
426 mutex_unlock(&kvm->lock);
427 return -ENXIO;
428 }
429
430 kvm_for_each_vcpu(i, vcpu, kvm) {
431 kvm_s390_vcpu_crypto_setup(vcpu);
432 exit_sie(vcpu);
433 }
434 mutex_unlock(&kvm->lock);
435 return 0;
436}
437
Jason J. Herne72f25022014-11-25 09:46:02 -0500438static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
439{
440 u8 gtod_high;
441
442 if (copy_from_user(&gtod_high, (void __user *)attr->addr,
443 sizeof(gtod_high)))
444 return -EFAULT;
445
446 if (gtod_high != 0)
447 return -EINVAL;
448
449 return 0;
450}
451
452static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
453{
454 struct kvm_vcpu *cur_vcpu;
455 unsigned int vcpu_idx;
456 u64 host_tod, gtod;
457 int r;
458
459 if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
460 return -EFAULT;
461
462 r = store_tod_clock(&host_tod);
463 if (r)
464 return r;
465
466 mutex_lock(&kvm->lock);
467 kvm->arch.epoch = gtod - host_tod;
Christian Borntraeger27406cd2015-04-14 12:17:34 +0200468 kvm_s390_vcpu_block_all(kvm);
469 kvm_for_each_vcpu(vcpu_idx, cur_vcpu, kvm)
Jason J. Herne72f25022014-11-25 09:46:02 -0500470 cur_vcpu->arch.sie_block->epoch = kvm->arch.epoch;
Christian Borntraeger27406cd2015-04-14 12:17:34 +0200471 kvm_s390_vcpu_unblock_all(kvm);
Jason J. Herne72f25022014-11-25 09:46:02 -0500472 mutex_unlock(&kvm->lock);
473 return 0;
474}
475
476static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
477{
478 int ret;
479
480 if (attr->flags)
481 return -EINVAL;
482
483 switch (attr->attr) {
484 case KVM_S390_VM_TOD_HIGH:
485 ret = kvm_s390_set_tod_high(kvm, attr);
486 break;
487 case KVM_S390_VM_TOD_LOW:
488 ret = kvm_s390_set_tod_low(kvm, attr);
489 break;
490 default:
491 ret = -ENXIO;
492 break;
493 }
494 return ret;
495}
496
497static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
498{
499 u8 gtod_high = 0;
500
501 if (copy_to_user((void __user *)attr->addr, &gtod_high,
502 sizeof(gtod_high)))
503 return -EFAULT;
504
505 return 0;
506}
507
508static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
509{
510 u64 host_tod, gtod;
511 int r;
512
513 r = store_tod_clock(&host_tod);
514 if (r)
515 return r;
516
517 gtod = host_tod + kvm->arch.epoch;
518 if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
519 return -EFAULT;
520
521 return 0;
522}
523
524static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
525{
526 int ret;
527
528 if (attr->flags)
529 return -EINVAL;
530
531 switch (attr->attr) {
532 case KVM_S390_VM_TOD_HIGH:
533 ret = kvm_s390_get_tod_high(kvm, attr);
534 break;
535 case KVM_S390_VM_TOD_LOW:
536 ret = kvm_s390_get_tod_low(kvm, attr);
537 break;
538 default:
539 ret = -ENXIO;
540 break;
541 }
542 return ret;
543}
544
Michael Mueller658b6ed2015-02-02 15:49:35 +0100545static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
546{
547 struct kvm_s390_vm_cpu_processor *proc;
548 int ret = 0;
549
550 mutex_lock(&kvm->lock);
551 if (atomic_read(&kvm->online_vcpus)) {
552 ret = -EBUSY;
553 goto out;
554 }
555 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
556 if (!proc) {
557 ret = -ENOMEM;
558 goto out;
559 }
560 if (!copy_from_user(proc, (void __user *)attr->addr,
561 sizeof(*proc))) {
562 memcpy(&kvm->arch.model.cpu_id, &proc->cpuid,
563 sizeof(struct cpuid));
564 kvm->arch.model.ibc = proc->ibc;
Michael Mueller981467c2015-02-24 13:51:04 +0100565 memcpy(kvm->arch.model.fac->list, proc->fac_list,
Michael Mueller658b6ed2015-02-02 15:49:35 +0100566 S390_ARCH_FAC_LIST_SIZE_BYTE);
567 } else
568 ret = -EFAULT;
569 kfree(proc);
570out:
571 mutex_unlock(&kvm->lock);
572 return ret;
573}
574
575static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
576{
577 int ret = -ENXIO;
578
579 switch (attr->attr) {
580 case KVM_S390_VM_CPU_PROCESSOR:
581 ret = kvm_s390_set_processor(kvm, attr);
582 break;
583 }
584 return ret;
585}
586
587static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
588{
589 struct kvm_s390_vm_cpu_processor *proc;
590 int ret = 0;
591
592 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
593 if (!proc) {
594 ret = -ENOMEM;
595 goto out;
596 }
597 memcpy(&proc->cpuid, &kvm->arch.model.cpu_id, sizeof(struct cpuid));
598 proc->ibc = kvm->arch.model.ibc;
Michael Mueller981467c2015-02-24 13:51:04 +0100599 memcpy(&proc->fac_list, kvm->arch.model.fac->list, S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100600 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
601 ret = -EFAULT;
602 kfree(proc);
603out:
604 return ret;
605}
606
607static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
608{
609 struct kvm_s390_vm_cpu_machine *mach;
610 int ret = 0;
611
612 mach = kzalloc(sizeof(*mach), GFP_KERNEL);
613 if (!mach) {
614 ret = -ENOMEM;
615 goto out;
616 }
617 get_cpu_id((struct cpuid *) &mach->cpuid);
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200618 mach->ibc = sclp.ibc;
Michael Mueller981467c2015-02-24 13:51:04 +0100619 memcpy(&mach->fac_mask, kvm->arch.model.fac->mask,
620 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100621 memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +0100622 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100623 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
624 ret = -EFAULT;
625 kfree(mach);
626out:
627 return ret;
628}
629
630static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
631{
632 int ret = -ENXIO;
633
634 switch (attr->attr) {
635 case KVM_S390_VM_CPU_PROCESSOR:
636 ret = kvm_s390_get_processor(kvm, attr);
637 break;
638 case KVM_S390_VM_CPU_MACHINE:
639 ret = kvm_s390_get_machine(kvm, attr);
640 break;
641 }
642 return ret;
643}
644
Dominik Dingelf2061652014-04-09 13:13:00 +0200645static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
646{
647 int ret;
648
649 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200650 case KVM_S390_VM_MEM_CTRL:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100651 ret = kvm_s390_set_mem_control(kvm, attr);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200652 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500653 case KVM_S390_VM_TOD:
654 ret = kvm_s390_set_tod(kvm, attr);
655 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100656 case KVM_S390_VM_CPU_MODEL:
657 ret = kvm_s390_set_cpu_model(kvm, attr);
658 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200659 case KVM_S390_VM_CRYPTO:
660 ret = kvm_s390_vm_set_crypto(kvm, attr);
661 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200662 default:
663 ret = -ENXIO;
664 break;
665 }
666
667 return ret;
668}
669
670static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
671{
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100672 int ret;
673
674 switch (attr->group) {
675 case KVM_S390_VM_MEM_CTRL:
676 ret = kvm_s390_get_mem_control(kvm, attr);
677 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500678 case KVM_S390_VM_TOD:
679 ret = kvm_s390_get_tod(kvm, attr);
680 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100681 case KVM_S390_VM_CPU_MODEL:
682 ret = kvm_s390_get_cpu_model(kvm, attr);
683 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100684 default:
685 ret = -ENXIO;
686 break;
687 }
688
689 return ret;
Dominik Dingelf2061652014-04-09 13:13:00 +0200690}
691
692static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
693{
694 int ret;
695
696 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200697 case KVM_S390_VM_MEM_CTRL:
698 switch (attr->attr) {
699 case KVM_S390_VM_MEM_ENABLE_CMMA:
700 case KVM_S390_VM_MEM_CLR_CMMA:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100701 case KVM_S390_VM_MEM_LIMIT_SIZE:
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200702 ret = 0;
703 break;
704 default:
705 ret = -ENXIO;
706 break;
707 }
708 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500709 case KVM_S390_VM_TOD:
710 switch (attr->attr) {
711 case KVM_S390_VM_TOD_LOW:
712 case KVM_S390_VM_TOD_HIGH:
713 ret = 0;
714 break;
715 default:
716 ret = -ENXIO;
717 break;
718 }
719 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100720 case KVM_S390_VM_CPU_MODEL:
721 switch (attr->attr) {
722 case KVM_S390_VM_CPU_PROCESSOR:
723 case KVM_S390_VM_CPU_MACHINE:
724 ret = 0;
725 break;
726 default:
727 ret = -ENXIO;
728 break;
729 }
730 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200731 case KVM_S390_VM_CRYPTO:
732 switch (attr->attr) {
733 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
734 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
735 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
736 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
737 ret = 0;
738 break;
739 default:
740 ret = -ENXIO;
741 break;
742 }
743 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200744 default:
745 ret = -ENXIO;
746 break;
747 }
748
749 return ret;
750}
751
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400752static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
753{
754 uint8_t *keys;
755 uint64_t hva;
756 unsigned long curkey;
757 int i, r = 0;
758
759 if (args->flags != 0)
760 return -EINVAL;
761
762 /* Is this guest using storage keys? */
763 if (!mm_use_skey(current->mm))
764 return KVM_S390_GET_SKEYS_NONE;
765
766 /* Enforce sane limit on memory allocation */
767 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
768 return -EINVAL;
769
770 keys = kmalloc_array(args->count, sizeof(uint8_t),
771 GFP_KERNEL | __GFP_NOWARN);
772 if (!keys)
773 keys = vmalloc(sizeof(uint8_t) * args->count);
774 if (!keys)
775 return -ENOMEM;
776
777 for (i = 0; i < args->count; i++) {
778 hva = gfn_to_hva(kvm, args->start_gfn + i);
779 if (kvm_is_error_hva(hva)) {
780 r = -EFAULT;
781 goto out;
782 }
783
784 curkey = get_guest_storage_key(current->mm, hva);
785 if (IS_ERR_VALUE(curkey)) {
786 r = curkey;
787 goto out;
788 }
789 keys[i] = curkey;
790 }
791
792 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
793 sizeof(uint8_t) * args->count);
794 if (r)
795 r = -EFAULT;
796out:
797 kvfree(keys);
798 return r;
799}
800
801static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
802{
803 uint8_t *keys;
804 uint64_t hva;
805 int i, r = 0;
806
807 if (args->flags != 0)
808 return -EINVAL;
809
810 /* Enforce sane limit on memory allocation */
811 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
812 return -EINVAL;
813
814 keys = kmalloc_array(args->count, sizeof(uint8_t),
815 GFP_KERNEL | __GFP_NOWARN);
816 if (!keys)
817 keys = vmalloc(sizeof(uint8_t) * args->count);
818 if (!keys)
819 return -ENOMEM;
820
821 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
822 sizeof(uint8_t) * args->count);
823 if (r) {
824 r = -EFAULT;
825 goto out;
826 }
827
828 /* Enable storage key handling for the guest */
Dominik Dingel14d4a422015-05-07 15:16:13 +0200829 r = s390_enable_skey();
830 if (r)
831 goto out;
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400832
833 for (i = 0; i < args->count; i++) {
834 hva = gfn_to_hva(kvm, args->start_gfn + i);
835 if (kvm_is_error_hva(hva)) {
836 r = -EFAULT;
837 goto out;
838 }
839
840 /* Lowest order bit is reserved */
841 if (keys[i] & 0x01) {
842 r = -EINVAL;
843 goto out;
844 }
845
846 r = set_guest_storage_key(current->mm, hva,
847 (unsigned long)keys[i], 0);
848 if (r)
849 goto out;
850 }
851out:
852 kvfree(keys);
853 return r;
854}
855
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100856long kvm_arch_vm_ioctl(struct file *filp,
857 unsigned int ioctl, unsigned long arg)
858{
859 struct kvm *kvm = filp->private_data;
860 void __user *argp = (void __user *)arg;
Dominik Dingelf2061652014-04-09 13:13:00 +0200861 struct kvm_device_attr attr;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100862 int r;
863
864 switch (ioctl) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100865 case KVM_S390_INTERRUPT: {
866 struct kvm_s390_interrupt s390int;
867
868 r = -EFAULT;
869 if (copy_from_user(&s390int, argp, sizeof(s390int)))
870 break;
871 r = kvm_s390_inject_vm(kvm, &s390int);
872 break;
873 }
Cornelia Huckd938dc52013-10-23 18:26:34 +0200874 case KVM_ENABLE_CAP: {
875 struct kvm_enable_cap cap;
876 r = -EFAULT;
877 if (copy_from_user(&cap, argp, sizeof(cap)))
878 break;
879 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
880 break;
881 }
Cornelia Huck84223592013-07-15 13:36:01 +0200882 case KVM_CREATE_IRQCHIP: {
883 struct kvm_irq_routing_entry routing;
884
885 r = -EINVAL;
886 if (kvm->arch.use_irqchip) {
887 /* Set up dummy routing. */
888 memset(&routing, 0, sizeof(routing));
889 kvm_set_irq_routing(kvm, &routing, 0, 0);
890 r = 0;
891 }
892 break;
893 }
Dominik Dingelf2061652014-04-09 13:13:00 +0200894 case KVM_SET_DEVICE_ATTR: {
895 r = -EFAULT;
896 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
897 break;
898 r = kvm_s390_vm_set_attr(kvm, &attr);
899 break;
900 }
901 case KVM_GET_DEVICE_ATTR: {
902 r = -EFAULT;
903 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
904 break;
905 r = kvm_s390_vm_get_attr(kvm, &attr);
906 break;
907 }
908 case KVM_HAS_DEVICE_ATTR: {
909 r = -EFAULT;
910 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
911 break;
912 r = kvm_s390_vm_has_attr(kvm, &attr);
913 break;
914 }
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400915 case KVM_S390_GET_SKEYS: {
916 struct kvm_s390_skeys args;
917
918 r = -EFAULT;
919 if (copy_from_user(&args, argp,
920 sizeof(struct kvm_s390_skeys)))
921 break;
922 r = kvm_s390_get_skeys(kvm, &args);
923 break;
924 }
925 case KVM_S390_SET_SKEYS: {
926 struct kvm_s390_skeys args;
927
928 r = -EFAULT;
929 if (copy_from_user(&args, argp,
930 sizeof(struct kvm_s390_skeys)))
931 break;
932 r = kvm_s390_set_skeys(kvm, &args);
933 break;
934 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100935 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300936 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100937 }
938
939 return r;
940}
941
Tony Krowiak45c9b472015-01-13 11:33:26 -0500942static int kvm_s390_query_ap_config(u8 *config)
943{
944 u32 fcn_code = 0x04000000UL;
Christian Borntraeger86044c82015-02-26 13:53:47 +0100945 u32 cc = 0;
Tony Krowiak45c9b472015-01-13 11:33:26 -0500946
Christian Borntraeger86044c82015-02-26 13:53:47 +0100947 memset(config, 0, 128);
Tony Krowiak45c9b472015-01-13 11:33:26 -0500948 asm volatile(
949 "lgr 0,%1\n"
950 "lgr 2,%2\n"
951 ".long 0xb2af0000\n" /* PQAP(QCI) */
Christian Borntraeger86044c82015-02-26 13:53:47 +0100952 "0: ipm %0\n"
Tony Krowiak45c9b472015-01-13 11:33:26 -0500953 "srl %0,28\n"
Christian Borntraeger86044c82015-02-26 13:53:47 +0100954 "1:\n"
955 EX_TABLE(0b, 1b)
956 : "+r" (cc)
Tony Krowiak45c9b472015-01-13 11:33:26 -0500957 : "r" (fcn_code), "r" (config)
958 : "cc", "0", "2", "memory"
959 );
960
961 return cc;
962}
963
964static int kvm_s390_apxa_installed(void)
965{
966 u8 config[128];
967 int cc;
968
969 if (test_facility(2) && test_facility(12)) {
970 cc = kvm_s390_query_ap_config(config);
971
972 if (cc)
973 pr_err("PQAP(QCI) failed with cc=%d", cc);
974 else
975 return config[0] & 0x40;
976 }
977
978 return 0;
979}
980
981static void kvm_s390_set_crycb_format(struct kvm *kvm)
982{
983 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
984
985 if (kvm_s390_apxa_installed())
986 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
987 else
988 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
989}
990
Michael Mueller9d8d5782015-02-02 15:42:51 +0100991static void kvm_s390_get_cpu_id(struct cpuid *cpu_id)
992{
993 get_cpu_id(cpu_id);
994 cpu_id->version = 0xff;
995}
996
Tony Krowiak5102ee82014-06-27 14:46:01 -0400997static int kvm_s390_crypto_init(struct kvm *kvm)
998{
Michael Mueller9d8d5782015-02-02 15:42:51 +0100999 if (!test_kvm_facility(kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -04001000 return 0;
1001
1002 kvm->arch.crypto.crycb = kzalloc(sizeof(*kvm->arch.crypto.crycb),
1003 GFP_KERNEL | GFP_DMA);
1004 if (!kvm->arch.crypto.crycb)
1005 return -ENOMEM;
1006
Tony Krowiak45c9b472015-01-13 11:33:26 -05001007 kvm_s390_set_crycb_format(kvm);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001008
Tony Krowiaked6f76b2015-02-24 14:06:57 -05001009 /* Enable AES/DEA protected key functions by default */
1010 kvm->arch.crypto.aes_kw = 1;
1011 kvm->arch.crypto.dea_kw = 1;
1012 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1013 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1014 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1015 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
Tony Krowiaka374e892014-09-03 10:13:53 +02001016
Tony Krowiak5102ee82014-06-27 14:46:01 -04001017 return 0;
1018}
1019
Carsten Ottee08b9632012-01-04 10:25:20 +01001020int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001021{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001022 int i, rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001023 char debug_name[16];
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001024 static unsigned long sca_offset;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001025
Carsten Ottee08b9632012-01-04 10:25:20 +01001026 rc = -EINVAL;
1027#ifdef CONFIG_KVM_S390_UCONTROL
1028 if (type & ~KVM_VM_S390_UCONTROL)
1029 goto out_err;
1030 if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1031 goto out_err;
1032#else
1033 if (type)
1034 goto out_err;
1035#endif
1036
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001037 rc = s390_enable_sie();
1038 if (rc)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001039 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001040
Carsten Otteb2904112011-10-18 12:27:13 +02001041 rc = -ENOMEM;
1042
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001043 kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
1044 if (!kvm->arch.sca)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001045 goto out_err;
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001046 spin_lock(&kvm_lock);
1047 sca_offset = (sca_offset + 16) & 0x7f0;
1048 kvm->arch.sca = (struct sca_block *) ((char *) kvm->arch.sca + sca_offset);
1049 spin_unlock(&kvm_lock);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001050
1051 sprintf(debug_name, "kvm-%u", current->pid);
1052
1053 kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
1054 if (!kvm->arch.dbf)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001055 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001056
Michael Mueller9d8d5782015-02-02 15:42:51 +01001057 /*
1058 * The architectural maximum amount of facilities is 16 kbit. To store
1059 * this amount, 2 kbyte of memory is required. Thus we need a full
Michael Mueller981467c2015-02-24 13:51:04 +01001060 * page to hold the guest facility list (arch.model.fac->list) and the
1061 * facility mask (arch.model.fac->mask). Its address size has to be
Michael Mueller9d8d5782015-02-02 15:42:51 +01001062 * 31 bits and word aligned.
1063 */
1064 kvm->arch.model.fac =
Michael Mueller981467c2015-02-24 13:51:04 +01001065 (struct kvm_s390_fac *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001066 if (!kvm->arch.model.fac)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001067 goto out_err;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001068
Michael Muellerfb5bf932015-02-27 14:25:10 +01001069 /* Populate the facility mask initially. */
Michael Mueller981467c2015-02-24 13:51:04 +01001070 memcpy(kvm->arch.model.fac->mask, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +01001071 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001072 for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
1073 if (i < kvm_s390_fac_list_mask_size())
Michael Mueller981467c2015-02-24 13:51:04 +01001074 kvm->arch.model.fac->mask[i] &= kvm_s390_fac_list_mask[i];
Michael Mueller9d8d5782015-02-02 15:42:51 +01001075 else
Michael Mueller981467c2015-02-24 13:51:04 +01001076 kvm->arch.model.fac->mask[i] = 0UL;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001077 }
1078
Michael Mueller981467c2015-02-24 13:51:04 +01001079 /* Populate the facility list initially. */
1080 memcpy(kvm->arch.model.fac->list, kvm->arch.model.fac->mask,
1081 S390_ARCH_FAC_LIST_SIZE_BYTE);
1082
Michael Mueller9d8d5782015-02-02 15:42:51 +01001083 kvm_s390_get_cpu_id(&kvm->arch.model.cpu_id);
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001084 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001085
Tony Krowiak5102ee82014-06-27 14:46:01 -04001086 if (kvm_s390_crypto_init(kvm) < 0)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001087 goto out_err;
Tony Krowiak5102ee82014-06-27 14:46:01 -04001088
Carsten Otteba5c1e92008-03-25 18:47:26 +01001089 spin_lock_init(&kvm->arch.float_int.lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001090 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1091 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
Heiko Carstens8a2422342014-01-10 14:33:28 +01001092 init_waitqueue_head(&kvm->arch.ipte_wq);
Thomas Hutha6b7e452014-10-01 14:48:42 +02001093 mutex_init(&kvm->arch.ipte_mutex);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001094
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001095 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
1096 VM_EVENT(kvm, 3, "%s", "vm created");
1097
Carsten Ottee08b9632012-01-04 10:25:20 +01001098 if (type & KVM_VM_S390_UCONTROL) {
1099 kvm->arch.gmap = NULL;
1100 } else {
Christian Borntraeger03499852014-08-25 12:38:57 +02001101 kvm->arch.gmap = gmap_alloc(current->mm, (1UL << 44) - 1);
Carsten Ottee08b9632012-01-04 10:25:20 +01001102 if (!kvm->arch.gmap)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001103 goto out_err;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001104 kvm->arch.gmap->private = kvm;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001105 kvm->arch.gmap->pfault_enabled = 0;
Carsten Ottee08b9632012-01-04 10:25:20 +01001106 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001107
1108 kvm->arch.css_support = 0;
Cornelia Huck84223592013-07-15 13:36:01 +02001109 kvm->arch.use_irqchip = 0;
Jason J. Herne72f25022014-11-25 09:46:02 -05001110 kvm->arch.epoch = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001111
David Hildenbrand8ad35752014-03-14 11:00:21 +01001112 spin_lock_init(&kvm->arch.start_stop_lock);
1113
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001114 return 0;
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001115out_err:
Dominik Dingel40f5b732015-03-12 13:55:53 +01001116 kfree(kvm->arch.crypto.crycb);
1117 free_page((unsigned long)kvm->arch.model.fac);
1118 debug_unregister(kvm->arch.dbf);
1119 free_page((unsigned long)(kvm->arch.sca));
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001120 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001121}
1122
Christian Borntraegerd329c032008-11-26 14:50:27 +01001123void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1124{
1125 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
Cornelia Huckade38c32012-07-23 17:20:30 +02001126 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001127 kvm_s390_clear_local_irqs(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001128 kvm_clear_async_pf_completion_queue(vcpu);
Carsten Otte58f94602012-01-04 10:25:27 +01001129 if (!kvm_is_ucontrol(vcpu->kvm)) {
1130 clear_bit(63 - vcpu->vcpu_id,
1131 (unsigned long *) &vcpu->kvm->arch.sca->mcn);
1132 if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
1133 (__u64) vcpu->arch.sie_block)
1134 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
1135 }
Carsten Otteabf4a712009-05-12 17:21:51 +02001136 smp_mb();
Carsten Otte27e03932012-01-04 10:25:21 +01001137
1138 if (kvm_is_ucontrol(vcpu->kvm))
1139 gmap_free(vcpu->arch.gmap);
1140
Dominik Dingele6db1d62015-05-07 15:41:57 +02001141 if (vcpu->kvm->arch.use_cmma)
Dominik Dingelb31605c2014-03-25 13:47:11 +01001142 kvm_s390_vcpu_unsetup_cmma(vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001143 free_page((unsigned long)(vcpu->arch.sie_block));
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001144
Christian Borntraeger6692cef2008-11-26 14:51:08 +01001145 kvm_vcpu_uninit(vcpu);
Michael Muellerb110fea2013-06-12 13:54:54 +02001146 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001147}
1148
1149static void kvm_free_vcpus(struct kvm *kvm)
1150{
1151 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001152 struct kvm_vcpu *vcpu;
Christian Borntraegerd329c032008-11-26 14:50:27 +01001153
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001154 kvm_for_each_vcpu(i, vcpu, kvm)
1155 kvm_arch_vcpu_destroy(vcpu);
1156
1157 mutex_lock(&kvm->lock);
1158 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1159 kvm->vcpus[i] = NULL;
1160
1161 atomic_set(&kvm->online_vcpus, 0);
1162 mutex_unlock(&kvm->lock);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001163}
1164
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001165void kvm_arch_destroy_vm(struct kvm *kvm)
1166{
Christian Borntraegerd329c032008-11-26 14:50:27 +01001167 kvm_free_vcpus(kvm);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001168 free_page((unsigned long)kvm->arch.model.fac);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001169 free_page((unsigned long)(kvm->arch.sca));
Christian Borntraegerd329c032008-11-26 14:50:27 +01001170 debug_unregister(kvm->arch.dbf);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001171 kfree(kvm->arch.crypto.crycb);
Carsten Otte27e03932012-01-04 10:25:21 +01001172 if (!kvm_is_ucontrol(kvm))
1173 gmap_free(kvm->arch.gmap);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001174 kvm_s390_destroy_adapters(kvm);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001175 kvm_s390_clear_float_irqs(kvm);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001176}
1177
1178/* Section: vcpu related */
Dominik Dingeldafd0322014-12-02 16:53:21 +01001179static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1180{
1181 vcpu->arch.gmap = gmap_alloc(current->mm, -1UL);
1182 if (!vcpu->arch.gmap)
1183 return -ENOMEM;
1184 vcpu->arch.gmap->private = vcpu->kvm;
1185
1186 return 0;
1187}
1188
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001189int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1190{
Dominik Dingel3c038e62013-10-07 17:11:48 +02001191 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1192 kvm_clear_async_pf_completion_queue(vcpu);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001193 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
1194 KVM_SYNC_GPRS |
Christian Borntraeger9eed07352012-02-06 10:59:07 +01001195 KVM_SYNC_ACRS |
David Hildenbrandb028ee32014-07-17 10:47:43 +02001196 KVM_SYNC_CRS |
1197 KVM_SYNC_ARCH0 |
1198 KVM_SYNC_PFAULT;
Eric Farman68c55752014-06-09 10:57:26 -04001199 if (test_kvm_facility(vcpu->kvm, 129))
1200 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
Dominik Dingeldafd0322014-12-02 16:53:21 +01001201
1202 if (kvm_is_ucontrol(vcpu->kvm))
1203 return __kvm_ucontrol_vcpu_init(vcpu);
1204
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001205 return 0;
1206}
1207
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001208void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1209{
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001210 save_fp_ctl(&vcpu->arch.host_fpregs.fpc);
Michael Mueller18280d82015-03-16 16:05:41 +01001211 if (test_kvm_facility(vcpu->kvm, 129))
Eric Farman68c55752014-06-09 10:57:26 -04001212 save_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs);
1213 else
1214 save_fp_regs(vcpu->arch.host_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001215 save_access_regs(vcpu->arch.host_acrs);
Michael Mueller18280d82015-03-16 16:05:41 +01001216 if (test_kvm_facility(vcpu->kvm, 129)) {
Eric Farman68c55752014-06-09 10:57:26 -04001217 restore_fp_ctl(&vcpu->run->s.regs.fpc);
1218 restore_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
1219 } else {
1220 restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1221 restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
1222 }
Christian Borntraeger59674c12012-01-11 11:20:33 +01001223 restore_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001224 gmap_enable(vcpu->arch.gmap);
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001225 atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001226}
1227
1228void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1229{
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001230 atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001231 gmap_disable(vcpu->arch.gmap);
Michael Mueller18280d82015-03-16 16:05:41 +01001232 if (test_kvm_facility(vcpu->kvm, 129)) {
Eric Farman68c55752014-06-09 10:57:26 -04001233 save_fp_ctl(&vcpu->run->s.regs.fpc);
1234 save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
1235 } else {
1236 save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1237 save_fp_regs(vcpu->arch.guest_fpregs.fprs);
1238 }
Christian Borntraeger59674c12012-01-11 11:20:33 +01001239 save_access_regs(vcpu->run->s.regs.acrs);
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001240 restore_fp_ctl(&vcpu->arch.host_fpregs.fpc);
Michael Mueller18280d82015-03-16 16:05:41 +01001241 if (test_kvm_facility(vcpu->kvm, 129))
Eric Farman68c55752014-06-09 10:57:26 -04001242 restore_vx_regs((__vector128 *)&vcpu->arch.host_vregs->vrs);
1243 else
1244 restore_fp_regs(vcpu->arch.host_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001245 restore_access_regs(vcpu->arch.host_acrs);
1246}
1247
1248static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1249{
1250 /* this equals initial cpu reset in pop, but we don't switch to ESA */
1251 vcpu->arch.sie_block->gpsw.mask = 0UL;
1252 vcpu->arch.sie_block->gpsw.addr = 0UL;
Christian Borntraeger8d26cf72012-01-11 11:19:32 +01001253 kvm_s390_set_prefix(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001254 vcpu->arch.sie_block->cputm = 0UL;
1255 vcpu->arch.sie_block->ckc = 0UL;
1256 vcpu->arch.sie_block->todpr = 0;
1257 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1258 vcpu->arch.sie_block->gcr[0] = 0xE0UL;
1259 vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
1260 vcpu->arch.guest_fpregs.fpc = 0;
1261 asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
1262 vcpu->arch.sie_block->gbea = 1;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001263 vcpu->arch.sie_block->pp = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001264 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1265 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001266 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
1267 kvm_s390_vcpu_stop(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001268 kvm_s390_clear_local_irqs(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001269}
1270
Dominik Dingel31928aa2014-12-04 15:47:07 +01001271void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001272{
Jason J. Herne72f25022014-11-25 09:46:02 -05001273 mutex_lock(&vcpu->kvm->lock);
1274 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
1275 mutex_unlock(&vcpu->kvm->lock);
Dominik Dingeldafd0322014-12-02 16:53:21 +01001276 if (!kvm_is_ucontrol(vcpu->kvm))
1277 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001278}
1279
Tony Krowiak5102ee82014-06-27 14:46:01 -04001280static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
1281{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001282 if (!test_kvm_facility(vcpu->kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -04001283 return;
1284
Tony Krowiaka374e892014-09-03 10:13:53 +02001285 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1286
1287 if (vcpu->kvm->arch.crypto.aes_kw)
1288 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1289 if (vcpu->kvm->arch.crypto.dea_kw)
1290 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1291
Tony Krowiak5102ee82014-06-27 14:46:01 -04001292 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
1293}
1294
Dominik Dingelb31605c2014-03-25 13:47:11 +01001295void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1296{
1297 free_page(vcpu->arch.sie_block->cbrlo);
1298 vcpu->arch.sie_block->cbrlo = 0;
1299}
1300
1301int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1302{
1303 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1304 if (!vcpu->arch.sie_block->cbrlo)
1305 return -ENOMEM;
1306
1307 vcpu->arch.sie_block->ecb2 |= 0x80;
1308 vcpu->arch.sie_block->ecb2 &= ~0x08;
1309 return 0;
1310}
1311
Michael Mueller91520f12015-02-27 14:32:11 +01001312static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
1313{
1314 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
1315
1316 vcpu->arch.cpu_id = model->cpu_id;
1317 vcpu->arch.sie_block->ibc = model->ibc;
1318 vcpu->arch.sie_block->fac = (int) (long) model->fac->list;
1319}
1320
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001321int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1322{
Dominik Dingelb31605c2014-03-25 13:47:11 +01001323 int rc = 0;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001324
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001325 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
1326 CPUSTAT_SM |
Guenther Hutzla4a4f192015-03-31 14:39:49 +02001327 CPUSTAT_STOPPED);
1328
Guenther Hutzl53df84f2015-02-18 11:13:03 +01001329 if (test_kvm_facility(vcpu->kvm, 78))
1330 atomic_set_mask(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
1331 else if (test_kvm_facility(vcpu->kvm, 8))
Guenther Hutzla4a4f192015-03-31 14:39:49 +02001332 atomic_set_mask(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
1333
Michael Mueller91520f12015-02-27 14:32:11 +01001334 kvm_s390_vcpu_setup_model(vcpu);
1335
Christian Borntraegerfc345312010-06-17 23:16:20 +02001336 vcpu->arch.sie_block->ecb = 6;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001337 if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73))
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001338 vcpu->arch.sie_block->ecb |= 0x10;
1339
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02001340 vcpu->arch.sie_block->ecb2 = 8;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001341 vcpu->arch.sie_block->eca = 0xC1002000U;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001342 if (sclp.has_siif)
Heiko Carstens217a4402013-12-30 12:54:14 +01001343 vcpu->arch.sie_block->eca |= 1;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001344 if (sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001345 vcpu->arch.sie_block->eca |= 0x10000000U;
Michael Mueller18280d82015-03-16 16:05:41 +01001346 if (test_kvm_facility(vcpu->kvm, 129)) {
Eric Farman13211ea2014-04-30 13:39:46 -04001347 vcpu->arch.sie_block->eca |= 0x00020000;
1348 vcpu->arch.sie_block->ecd |= 0x20000000;
1349 }
Thomas Huth492d8642015-02-10 16:11:01 +01001350 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
Matthew Rosato5a5e6532013-01-29 11:48:20 -05001351
Dominik Dingele6db1d62015-05-07 15:41:57 +02001352 if (vcpu->kvm->arch.use_cmma) {
Dominik Dingelb31605c2014-03-25 13:47:11 +01001353 rc = kvm_s390_vcpu_setup_cmma(vcpu);
1354 if (rc)
1355 return rc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001356 }
David Hildenbrand0ac96ca2014-12-12 15:17:31 +01001357 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Christian Borntraegerca872302009-05-12 17:21:49 +02001358 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001359
Tony Krowiak5102ee82014-06-27 14:46:01 -04001360 kvm_s390_vcpu_crypto_setup(vcpu);
1361
Dominik Dingelb31605c2014-03-25 13:47:11 +01001362 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001363}
1364
1365struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1366 unsigned int id)
1367{
Carsten Otte4d475552011-10-18 12:27:12 +02001368 struct kvm_vcpu *vcpu;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001369 struct sie_page *sie_page;
Carsten Otte4d475552011-10-18 12:27:12 +02001370 int rc = -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001371
Carsten Otte4d475552011-10-18 12:27:12 +02001372 if (id >= KVM_MAX_VCPUS)
1373 goto out;
1374
1375 rc = -ENOMEM;
1376
Michael Muellerb110fea2013-06-12 13:54:54 +02001377 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001378 if (!vcpu)
Carsten Otte4d475552011-10-18 12:27:12 +02001379 goto out;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001380
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001381 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
1382 if (!sie_page)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001383 goto out_free_cpu;
1384
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001385 vcpu->arch.sie_block = &sie_page->sie_block;
1386 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
Eric Farman68c55752014-06-09 10:57:26 -04001387 vcpu->arch.host_vregs = &sie_page->vregs;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001388
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001389 vcpu->arch.sie_block->icpua = id;
Carsten Otte58f94602012-01-04 10:25:27 +01001390 if (!kvm_is_ucontrol(kvm)) {
1391 if (!kvm->arch.sca) {
1392 WARN_ON_ONCE(1);
1393 goto out_free_cpu;
1394 }
1395 if (!kvm->arch.sca->cpu[id].sda)
1396 kvm->arch.sca->cpu[id].sda =
1397 (__u64) vcpu->arch.sie_block;
1398 vcpu->arch.sie_block->scaoh =
1399 (__u32)(((__u64)kvm->arch.sca) >> 32);
1400 vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
1401 set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
1402 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001403
Carsten Otteba5c1e92008-03-25 18:47:26 +01001404 spin_lock_init(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001405 vcpu->arch.local_int.float_int = &kvm->arch.float_int;
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001406 vcpu->arch.local_int.wq = &vcpu->wq;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01001407 vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001408
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001409 rc = kvm_vcpu_init(vcpu, kvm, id);
1410 if (rc)
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001411 goto out_free_sie_block;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001412 VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
1413 vcpu->arch.sie_block);
Cornelia Huckade38c32012-07-23 17:20:30 +02001414 trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001415
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001416 return vcpu;
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001417out_free_sie_block:
1418 free_page((unsigned long)(vcpu->arch.sie_block));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001419out_free_cpu:
Michael Muellerb110fea2013-06-12 13:54:54 +02001420 kmem_cache_free(kvm_vcpu_cache, vcpu);
Carsten Otte4d475552011-10-18 12:27:12 +02001421out:
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001422 return ERR_PTR(rc);
1423}
1424
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001425int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1426{
David Hildenbrand9a022062014-08-05 17:40:47 +02001427 return kvm_s390_vcpu_has_irq(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001428}
1429
Christian Borntraeger27406cd2015-04-14 12:17:34 +02001430void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001431{
1432 atomic_set_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
David Hildenbrand61a6df52015-05-12 08:41:40 +02001433 exit_sie(vcpu);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001434}
1435
Christian Borntraeger27406cd2015-04-14 12:17:34 +02001436void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001437{
1438 atomic_clear_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
1439}
1440
Christian Borntraeger8e236542015-04-09 13:49:04 +02001441static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
1442{
1443 atomic_set_mask(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
David Hildenbrand61a6df52015-05-12 08:41:40 +02001444 exit_sie(vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001445}
1446
1447static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
1448{
1449 atomic_clear_mask(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
1450}
1451
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001452/*
1453 * Kick a guest cpu out of SIE and wait until SIE is not running.
1454 * If the CPU is not running (e.g. waiting as idle) the function will
1455 * return immediately. */
1456void exit_sie(struct kvm_vcpu *vcpu)
1457{
1458 atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
1459 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
1460 cpu_relax();
1461}
1462
Christian Borntraeger8e236542015-04-09 13:49:04 +02001463/* Kick a guest cpu out of SIE to process a request synchronously */
1464void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001465{
Christian Borntraeger8e236542015-04-09 13:49:04 +02001466 kvm_make_request(req, vcpu);
1467 kvm_s390_vcpu_request(vcpu);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001468}
1469
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001470static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
1471{
1472 int i;
1473 struct kvm *kvm = gmap->private;
1474 struct kvm_vcpu *vcpu;
1475
1476 kvm_for_each_vcpu(i, vcpu, kvm) {
1477 /* match against both prefix pages */
Michael Muellerfda902c2014-05-13 16:58:30 +02001478 if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001479 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001480 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001481 }
1482 }
1483}
1484
Christoffer Dallb6d33832012-03-08 16:44:24 -05001485int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1486{
1487 /* kvm common code refers to this, but never calls it */
1488 BUG();
1489 return 0;
1490}
1491
Carsten Otte14eebd92012-05-15 14:15:26 +02001492static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
1493 struct kvm_one_reg *reg)
1494{
1495 int r = -EINVAL;
1496
1497 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001498 case KVM_REG_S390_TODPR:
1499 r = put_user(vcpu->arch.sie_block->todpr,
1500 (u32 __user *)reg->addr);
1501 break;
1502 case KVM_REG_S390_EPOCHDIFF:
1503 r = put_user(vcpu->arch.sie_block->epoch,
1504 (u64 __user *)reg->addr);
1505 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001506 case KVM_REG_S390_CPU_TIMER:
1507 r = put_user(vcpu->arch.sie_block->cputm,
1508 (u64 __user *)reg->addr);
1509 break;
1510 case KVM_REG_S390_CLOCK_COMP:
1511 r = put_user(vcpu->arch.sie_block->ckc,
1512 (u64 __user *)reg->addr);
1513 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001514 case KVM_REG_S390_PFTOKEN:
1515 r = put_user(vcpu->arch.pfault_token,
1516 (u64 __user *)reg->addr);
1517 break;
1518 case KVM_REG_S390_PFCOMPARE:
1519 r = put_user(vcpu->arch.pfault_compare,
1520 (u64 __user *)reg->addr);
1521 break;
1522 case KVM_REG_S390_PFSELECT:
1523 r = put_user(vcpu->arch.pfault_select,
1524 (u64 __user *)reg->addr);
1525 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001526 case KVM_REG_S390_PP:
1527 r = put_user(vcpu->arch.sie_block->pp,
1528 (u64 __user *)reg->addr);
1529 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001530 case KVM_REG_S390_GBEA:
1531 r = put_user(vcpu->arch.sie_block->gbea,
1532 (u64 __user *)reg->addr);
1533 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001534 default:
1535 break;
1536 }
1537
1538 return r;
1539}
1540
1541static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
1542 struct kvm_one_reg *reg)
1543{
1544 int r = -EINVAL;
1545
1546 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001547 case KVM_REG_S390_TODPR:
1548 r = get_user(vcpu->arch.sie_block->todpr,
1549 (u32 __user *)reg->addr);
1550 break;
1551 case KVM_REG_S390_EPOCHDIFF:
1552 r = get_user(vcpu->arch.sie_block->epoch,
1553 (u64 __user *)reg->addr);
1554 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001555 case KVM_REG_S390_CPU_TIMER:
1556 r = get_user(vcpu->arch.sie_block->cputm,
1557 (u64 __user *)reg->addr);
1558 break;
1559 case KVM_REG_S390_CLOCK_COMP:
1560 r = get_user(vcpu->arch.sie_block->ckc,
1561 (u64 __user *)reg->addr);
1562 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001563 case KVM_REG_S390_PFTOKEN:
1564 r = get_user(vcpu->arch.pfault_token,
1565 (u64 __user *)reg->addr);
David Hildenbrand9fbd8082014-10-09 15:01:38 +02001566 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1567 kvm_clear_async_pf_completion_queue(vcpu);
Dominik Dingel536336c2013-09-30 10:55:33 +02001568 break;
1569 case KVM_REG_S390_PFCOMPARE:
1570 r = get_user(vcpu->arch.pfault_compare,
1571 (u64 __user *)reg->addr);
1572 break;
1573 case KVM_REG_S390_PFSELECT:
1574 r = get_user(vcpu->arch.pfault_select,
1575 (u64 __user *)reg->addr);
1576 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001577 case KVM_REG_S390_PP:
1578 r = get_user(vcpu->arch.sie_block->pp,
1579 (u64 __user *)reg->addr);
1580 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001581 case KVM_REG_S390_GBEA:
1582 r = get_user(vcpu->arch.sie_block->gbea,
1583 (u64 __user *)reg->addr);
1584 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001585 default:
1586 break;
1587 }
1588
1589 return r;
1590}
Christoffer Dallb6d33832012-03-08 16:44:24 -05001591
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001592static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
1593{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001594 kvm_s390_vcpu_initial_reset(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001595 return 0;
1596}
1597
1598int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1599{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001600 memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001601 return 0;
1602}
1603
1604int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1605{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001606 memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001607 return 0;
1608}
1609
1610int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1611 struct kvm_sregs *sregs)
1612{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001613 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001614 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
Christian Borntraeger59674c12012-01-11 11:20:33 +01001615 restore_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001616 return 0;
1617}
1618
1619int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1620 struct kvm_sregs *sregs)
1621{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001622 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001623 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001624 return 0;
1625}
1626
1627int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1628{
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001629 if (test_fp_ctl(fpu->fpc))
1630 return -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001631 memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001632 vcpu->arch.guest_fpregs.fpc = fpu->fpc;
1633 restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
1634 restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001635 return 0;
1636}
1637
1638int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1639{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001640 memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
1641 fpu->fpc = vcpu->arch.guest_fpregs.fpc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001642 return 0;
1643}
1644
1645static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
1646{
1647 int rc = 0;
1648
David Hildenbrand7a42fdc2014-05-05 16:26:19 +02001649 if (!is_vcpu_stopped(vcpu))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001650 rc = -EBUSY;
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001651 else {
1652 vcpu->run->psw_mask = psw.mask;
1653 vcpu->run->psw_addr = psw.addr;
1654 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001655 return rc;
1656}
1657
1658int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1659 struct kvm_translation *tr)
1660{
1661 return -EINVAL; /* not implemented yet */
1662}
1663
David Hildenbrand27291e22014-01-23 12:26:52 +01001664#define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
1665 KVM_GUESTDBG_USE_HW_BP | \
1666 KVM_GUESTDBG_ENABLE)
1667
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001668int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1669 struct kvm_guest_debug *dbg)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001670{
David Hildenbrand27291e22014-01-23 12:26:52 +01001671 int rc = 0;
1672
1673 vcpu->guest_debug = 0;
1674 kvm_s390_clear_bp_data(vcpu);
1675
David Hildenbrand2de3bfc2014-05-20 17:25:20 +02001676 if (dbg->control & ~VALID_GUESTDBG_FLAGS)
David Hildenbrand27291e22014-01-23 12:26:52 +01001677 return -EINVAL;
1678
1679 if (dbg->control & KVM_GUESTDBG_ENABLE) {
1680 vcpu->guest_debug = dbg->control;
1681 /* enforce guest PER */
1682 atomic_set_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1683
1684 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
1685 rc = kvm_s390_import_bp_data(vcpu, dbg);
1686 } else {
1687 atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1688 vcpu->arch.guestdbg.last_bp = 0;
1689 }
1690
1691 if (rc) {
1692 vcpu->guest_debug = 0;
1693 kvm_s390_clear_bp_data(vcpu);
1694 atomic_clear_mask(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
1695 }
1696
1697 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001698}
1699
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001700int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1701 struct kvm_mp_state *mp_state)
1702{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001703 /* CHECK_STOP and LOAD are not supported yet */
1704 return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
1705 KVM_MP_STATE_OPERATING;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001706}
1707
1708int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1709 struct kvm_mp_state *mp_state)
1710{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001711 int rc = 0;
1712
1713 /* user space knows about this interface - let it control the state */
1714 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
1715
1716 switch (mp_state->mp_state) {
1717 case KVM_MP_STATE_STOPPED:
1718 kvm_s390_vcpu_stop(vcpu);
1719 break;
1720 case KVM_MP_STATE_OPERATING:
1721 kvm_s390_vcpu_start(vcpu);
1722 break;
1723 case KVM_MP_STATE_LOAD:
1724 case KVM_MP_STATE_CHECK_STOP:
1725 /* fall through - CHECK_STOP and LOAD are not supported yet */
1726 default:
1727 rc = -ENXIO;
1728 }
1729
1730 return rc;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001731}
1732
David Hildenbrand8ad35752014-03-14 11:00:21 +01001733static bool ibs_enabled(struct kvm_vcpu *vcpu)
1734{
1735 return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
1736}
1737
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001738static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
1739{
Christian Borntraeger785dbef2015-04-16 16:58:22 +02001740 if (!vcpu->requests)
1741 return 0;
David Hildenbrand8ad35752014-03-14 11:00:21 +01001742retry:
Christian Borntraeger8e236542015-04-09 13:49:04 +02001743 kvm_s390_vcpu_request_handled(vcpu);
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001744 /*
1745 * We use MMU_RELOAD just to re-arm the ipte notifier for the
1746 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
1747 * This ensures that the ipte instruction for this request has
1748 * already finished. We might race against a second unmapper that
1749 * wants to set the blocking bit. Lets just retry the request loop.
1750 */
David Hildenbrand8ad35752014-03-14 11:00:21 +01001751 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001752 int rc;
1753 rc = gmap_ipte_notify(vcpu->arch.gmap,
Michael Muellerfda902c2014-05-13 16:58:30 +02001754 kvm_s390_get_prefix(vcpu),
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001755 PAGE_SIZE * 2);
1756 if (rc)
1757 return rc;
David Hildenbrand8ad35752014-03-14 11:00:21 +01001758 goto retry;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001759 }
David Hildenbrand8ad35752014-03-14 11:00:21 +01001760
David Hildenbrandd3d692c2014-07-29 08:53:36 +02001761 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
1762 vcpu->arch.sie_block->ihcpu = 0xffff;
1763 goto retry;
1764 }
1765
David Hildenbrand8ad35752014-03-14 11:00:21 +01001766 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
1767 if (!ibs_enabled(vcpu)) {
1768 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
1769 atomic_set_mask(CPUSTAT_IBS,
1770 &vcpu->arch.sie_block->cpuflags);
1771 }
1772 goto retry;
1773 }
1774
1775 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
1776 if (ibs_enabled(vcpu)) {
1777 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
1778 atomic_clear_mask(CPUSTAT_IBS,
1779 &vcpu->arch.sie_block->cpuflags);
1780 }
1781 goto retry;
1782 }
1783
David Hildenbrand0759d062014-05-13 16:54:32 +02001784 /* nothing to do, just clear the request */
1785 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
1786
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001787 return 0;
1788}
1789
Thomas Huthfa576c52014-05-06 17:20:16 +02001790/**
1791 * kvm_arch_fault_in_page - fault-in guest page if necessary
1792 * @vcpu: The corresponding virtual cpu
1793 * @gpa: Guest physical address
1794 * @writable: Whether the page should be writable or not
1795 *
1796 * Make sure that a guest page has been faulted-in on the host.
1797 *
1798 * Return: Zero on success, negative error code otherwise.
1799 */
1800long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001801{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02001802 return gmap_fault(vcpu->arch.gmap, gpa,
1803 writable ? FAULT_FLAG_WRITE : 0);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001804}
1805
Dominik Dingel3c038e62013-10-07 17:11:48 +02001806static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
1807 unsigned long token)
1808{
1809 struct kvm_s390_interrupt inti;
Jens Freimann383d0b02014-07-29 15:11:49 +02001810 struct kvm_s390_irq irq;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001811
1812 if (start_token) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001813 irq.u.ext.ext_params2 = token;
1814 irq.type = KVM_S390_INT_PFAULT_INIT;
1815 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
Dominik Dingel3c038e62013-10-07 17:11:48 +02001816 } else {
1817 inti.type = KVM_S390_INT_PFAULT_DONE;
Jens Freimann383d0b02014-07-29 15:11:49 +02001818 inti.parm64 = token;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001819 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
1820 }
1821}
1822
1823void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
1824 struct kvm_async_pf *work)
1825{
1826 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
1827 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
1828}
1829
1830void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
1831 struct kvm_async_pf *work)
1832{
1833 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
1834 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
1835}
1836
1837void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
1838 struct kvm_async_pf *work)
1839{
1840 /* s390 will always inject the page directly */
1841}
1842
1843bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
1844{
1845 /*
1846 * s390 will always inject the page directly,
1847 * but we still want check_async_completion to cleanup
1848 */
1849 return true;
1850}
1851
1852static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
1853{
1854 hva_t hva;
1855 struct kvm_arch_async_pf arch;
1856 int rc;
1857
1858 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1859 return 0;
1860 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
1861 vcpu->arch.pfault_compare)
1862 return 0;
1863 if (psw_extint_disabled(vcpu))
1864 return 0;
David Hildenbrand9a022062014-08-05 17:40:47 +02001865 if (kvm_s390_vcpu_has_irq(vcpu, 0))
Dominik Dingel3c038e62013-10-07 17:11:48 +02001866 return 0;
1867 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
1868 return 0;
1869 if (!vcpu->arch.gmap->pfault_enabled)
1870 return 0;
1871
Heiko Carstens81480cc2014-01-01 16:36:07 +01001872 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
1873 hva += current->thread.gmap_addr & ~PAGE_MASK;
1874 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
Dominik Dingel3c038e62013-10-07 17:11:48 +02001875 return 0;
1876
1877 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
1878 return rc;
1879}
1880
Thomas Huth3fb4c402013-09-12 10:33:43 +02001881static int vcpu_pre_run(struct kvm_vcpu *vcpu)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001882{
Thomas Huth3fb4c402013-09-12 10:33:43 +02001883 int rc, cpuflags;
Carsten Ottee168bf82012-01-04 10:25:22 +01001884
Dominik Dingel3c038e62013-10-07 17:11:48 +02001885 /*
1886 * On s390 notifications for arriving pages will be delivered directly
1887 * to the guest but the house keeping for completed pfaults is
1888 * handled outside the worker.
1889 */
1890 kvm_check_async_pf_completion(vcpu);
1891
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001892 memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001893
1894 if (need_resched())
1895 schedule();
1896
Martin Schwidefskyd3a73ac2014-04-15 12:55:07 +02001897 if (test_cpu_flag(CIF_MCCK_PENDING))
Christian Borntraeger71cde582008-05-21 13:37:34 +02001898 s390_handle_mcck();
1899
Jens Freimann79395032014-04-17 10:10:30 +02001900 if (!kvm_is_ucontrol(vcpu->kvm)) {
1901 rc = kvm_s390_deliver_pending_interrupts(vcpu);
1902 if (rc)
1903 return rc;
1904 }
Carsten Otte0ff31862008-05-21 13:37:37 +02001905
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001906 rc = kvm_s390_handle_requests(vcpu);
1907 if (rc)
1908 return rc;
1909
David Hildenbrand27291e22014-01-23 12:26:52 +01001910 if (guestdbg_enabled(vcpu)) {
1911 kvm_s390_backup_guest_per_regs(vcpu);
1912 kvm_s390_patch_guest_per_regs(vcpu);
1913 }
1914
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001915 vcpu->arch.sie_block->icptcode = 0;
Thomas Huth3fb4c402013-09-12 10:33:43 +02001916 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
1917 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
1918 trace_kvm_s390_sie_enter(vcpu, cpuflags);
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02001919
Thomas Huth3fb4c402013-09-12 10:33:43 +02001920 return 0;
1921}
1922
Thomas Huth492d8642015-02-10 16:11:01 +01001923static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
1924{
1925 psw_t *psw = &vcpu->arch.sie_block->gpsw;
1926 u8 opcode;
1927 int rc;
1928
1929 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
1930 trace_kvm_s390_sie_fault(vcpu);
1931
1932 /*
1933 * We want to inject an addressing exception, which is defined as a
1934 * suppressing or terminating exception. However, since we came here
1935 * by a DAT access exception, the PSW still points to the faulting
1936 * instruction since DAT exceptions are nullifying. So we've got
1937 * to look up the current opcode to get the length of the instruction
1938 * to be able to forward the PSW.
1939 */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03001940 rc = read_guest(vcpu, psw->addr, 0, &opcode, 1);
Thomas Huth492d8642015-02-10 16:11:01 +01001941 if (rc)
1942 return kvm_s390_inject_prog_cond(vcpu, rc);
1943 psw->addr = __rewind_psw(*psw, -insn_length(opcode));
1944
1945 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
1946}
1947
Thomas Huth3fb4c402013-09-12 10:33:43 +02001948static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
1949{
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001950 int rc = -1;
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02001951
1952 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
1953 vcpu->arch.sie_block->icptcode);
1954 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
1955
David Hildenbrand27291e22014-01-23 12:26:52 +01001956 if (guestdbg_enabled(vcpu))
1957 kvm_s390_restore_guest_per_regs(vcpu);
1958
Thomas Huth3fb4c402013-09-12 10:33:43 +02001959 if (exit_reason >= 0) {
Martin Schwidefsky7c470532013-05-17 14:41:37 +02001960 rc = 0;
Thomas Huth210b16072013-09-19 16:26:18 +02001961 } else if (kvm_is_ucontrol(vcpu->kvm)) {
1962 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
1963 vcpu->run->s390_ucontrol.trans_exc_code =
1964 current->thread.gmap_addr;
1965 vcpu->run->s390_ucontrol.pgm_code = 0x10;
1966 rc = -EREMOTE;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001967
1968 } else if (current->thread.gmap_pfault) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001969 trace_kvm_s390_major_guest_pfault(vcpu);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001970 current->thread.gmap_pfault = 0;
Thomas Huthfa576c52014-05-06 17:20:16 +02001971 if (kvm_arch_setup_async_pf(vcpu)) {
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001972 rc = 0;
Thomas Huthfa576c52014-05-06 17:20:16 +02001973 } else {
1974 gpa_t gpa = current->thread.gmap_addr;
1975 rc = kvm_arch_fault_in_page(vcpu, gpa, 1);
1976 }
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001977 }
1978
Thomas Huth492d8642015-02-10 16:11:01 +01001979 if (rc == -1)
1980 rc = vcpu_post_run_fault_in_sie(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001981
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001982 memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
Thomas Huth3fb4c402013-09-12 10:33:43 +02001983
Thomas Hutha76ccff2013-09-12 10:33:44 +02001984 if (rc == 0) {
1985 if (kvm_is_ucontrol(vcpu->kvm))
Christian Borntraeger2955c832014-03-06 16:01:38 +01001986 /* Don't exit for host interrupts. */
1987 rc = vcpu->arch.sie_block->icptcode ? -EOPNOTSUPP : 0;
Thomas Hutha76ccff2013-09-12 10:33:44 +02001988 else
1989 rc = kvm_handle_sie_intercept(vcpu);
1990 }
1991
Thomas Huth3fb4c402013-09-12 10:33:43 +02001992 return rc;
1993}
1994
1995static int __vcpu_run(struct kvm_vcpu *vcpu)
1996{
1997 int rc, exit_reason;
1998
Thomas Huth800c1062013-09-12 10:33:45 +02001999 /*
2000 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2001 * ning the guest), so that memslots (and other stuff) are protected
2002 */
2003 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2004
Thomas Hutha76ccff2013-09-12 10:33:44 +02002005 do {
2006 rc = vcpu_pre_run(vcpu);
2007 if (rc)
2008 break;
Thomas Huth3fb4c402013-09-12 10:33:43 +02002009
Thomas Huth800c1062013-09-12 10:33:45 +02002010 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Thomas Hutha76ccff2013-09-12 10:33:44 +02002011 /*
2012 * As PF_VCPU will be used in fault handler, between
2013 * guest_enter and guest_exit should be no uaccess.
2014 */
Christian Borntraeger0097d122015-04-30 13:43:30 +02002015 local_irq_disable();
2016 __kvm_guest_enter();
2017 local_irq_enable();
Thomas Hutha76ccff2013-09-12 10:33:44 +02002018 exit_reason = sie64a(vcpu->arch.sie_block,
2019 vcpu->run->s.regs.gprs);
Christian Borntraeger0097d122015-04-30 13:43:30 +02002020 local_irq_disable();
2021 __kvm_guest_exit();
2022 local_irq_enable();
Thomas Huth800c1062013-09-12 10:33:45 +02002023 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002024
Thomas Hutha76ccff2013-09-12 10:33:44 +02002025 rc = vcpu_post_run(vcpu, exit_reason);
David Hildenbrand27291e22014-01-23 12:26:52 +01002026 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002027
Thomas Huth800c1062013-09-12 10:33:45 +02002028 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Carsten Ottee168bf82012-01-04 10:25:22 +01002029 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002030}
2031
David Hildenbrandb028ee32014-07-17 10:47:43 +02002032static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2033{
2034 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2035 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2036 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2037 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2038 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2039 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002040 /* some control register changes require a tlb flush */
2041 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002042 }
2043 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
2044 vcpu->arch.sie_block->cputm = kvm_run->s.regs.cputm;
2045 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2046 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2047 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2048 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2049 }
2050 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2051 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2052 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2053 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
David Hildenbrand9fbd8082014-10-09 15:01:38 +02002054 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
2055 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002056 }
2057 kvm_run->kvm_dirty_regs = 0;
2058}
2059
2060static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2061{
2062 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
2063 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
2064 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
2065 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
2066 kvm_run->s.regs.cputm = vcpu->arch.sie_block->cputm;
2067 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
2068 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
2069 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
2070 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
2071 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
2072 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
2073 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
2074}
2075
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002076int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2077{
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002078 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002079 sigset_t sigsaved;
2080
David Hildenbrand27291e22014-01-23 12:26:52 +01002081 if (guestdbg_exit_pending(vcpu)) {
2082 kvm_s390_prepare_debug_exit(vcpu);
2083 return 0;
2084 }
2085
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002086 if (vcpu->sigset_active)
2087 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2088
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002089 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
2090 kvm_s390_vcpu_start(vcpu);
2091 } else if (is_vcpu_stopped(vcpu)) {
David Hildenbrandea2cdd22015-05-20 13:24:02 +02002092 pr_err_ratelimited("can't run stopped vcpu %d\n",
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002093 vcpu->vcpu_id);
2094 return -EINVAL;
2095 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002096
David Hildenbrandb028ee32014-07-17 10:47:43 +02002097 sync_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002098
Heiko Carstensdab4079d2009-06-12 10:26:32 +02002099 might_fault();
Thomas Hutha76ccff2013-09-12 10:33:44 +02002100 rc = __vcpu_run(vcpu);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +02002101
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002102 if (signal_pending(current) && !rc) {
2103 kvm_run->exit_reason = KVM_EXIT_INTR;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002104 rc = -EINTR;
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002105 }
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002106
David Hildenbrand27291e22014-01-23 12:26:52 +01002107 if (guestdbg_exit_pending(vcpu) && !rc) {
2108 kvm_s390_prepare_debug_exit(vcpu);
2109 rc = 0;
2110 }
2111
Heiko Carstensb8e660b2010-02-26 22:37:41 +01002112 if (rc == -EOPNOTSUPP) {
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002113 /* intercept cannot be handled in-kernel, prepare kvm-run */
2114 kvm_run->exit_reason = KVM_EXIT_S390_SIEIC;
2115 kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002116 kvm_run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
2117 kvm_run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
2118 rc = 0;
2119 }
2120
2121 if (rc == -EREMOTE) {
2122 /* intercept was handled, but userspace support is needed
2123 * kvm_run has been prepared by the handler */
2124 rc = 0;
2125 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002126
David Hildenbrandb028ee32014-07-17 10:47:43 +02002127 store_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002128
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002129 if (vcpu->sigset_active)
2130 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2131
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002132 vcpu->stat.exit_userspace++;
Heiko Carstens7e8e6ab2008-04-04 15:12:35 +02002133 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002134}
2135
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002136/*
2137 * store status at address
2138 * we use have two special cases:
2139 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2140 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2141 */
Heiko Carstensd0bce602014-01-01 16:45:58 +01002142int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002143{
Carsten Otte092670c2011-07-24 10:48:22 +02002144 unsigned char archmode = 1;
Michael Muellerfda902c2014-05-13 16:58:30 +02002145 unsigned int px;
Thomas Huth178bd782013-11-13 20:28:18 +01002146 u64 clkcomp;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002147 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002148
Heiko Carstensd0bce602014-01-01 16:45:58 +01002149 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
2150 if (write_guest_abs(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002151 return -EFAULT;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002152 gpa = SAVE_AREA_BASE;
2153 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
2154 if (write_guest_real(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002155 return -EFAULT;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002156 gpa = kvm_s390_real_to_abs(vcpu, SAVE_AREA_BASE);
2157 }
2158 rc = write_guest_abs(vcpu, gpa + offsetof(struct save_area, fp_regs),
2159 vcpu->arch.guest_fpregs.fprs, 128);
2160 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, gp_regs),
2161 vcpu->run->s.regs.gprs, 128);
2162 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, psw),
2163 &vcpu->arch.sie_block->gpsw, 16);
Michael Muellerfda902c2014-05-13 16:58:30 +02002164 px = kvm_s390_get_prefix(vcpu);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002165 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, pref_reg),
Michael Muellerfda902c2014-05-13 16:58:30 +02002166 &px, 4);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002167 rc |= write_guest_abs(vcpu,
2168 gpa + offsetof(struct save_area, fp_ctrl_reg),
2169 &vcpu->arch.guest_fpregs.fpc, 4);
2170 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, tod_reg),
2171 &vcpu->arch.sie_block->todpr, 4);
2172 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, timer),
2173 &vcpu->arch.sie_block->cputm, 8);
Thomas Huth178bd782013-11-13 20:28:18 +01002174 clkcomp = vcpu->arch.sie_block->ckc >> 8;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002175 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, clk_cmp),
2176 &clkcomp, 8);
2177 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, acc_regs),
2178 &vcpu->run->s.regs.acrs, 64);
2179 rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, ctrl_regs),
2180 &vcpu->arch.sie_block->gcr, 128);
2181 return rc ? -EFAULT : 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002182}
2183
Thomas Huthe8798922013-11-06 15:46:33 +01002184int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2185{
2186 /*
2187 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
2188 * copying in vcpu load/put. Lets update our copies before we save
2189 * it into the save area
2190 */
2191 save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
2192 save_fp_regs(vcpu->arch.guest_fpregs.fprs);
2193 save_access_regs(vcpu->run->s.regs.acrs);
2194
2195 return kvm_s390_store_status_unloaded(vcpu, addr);
2196}
2197
Eric Farmanbc17de72014-04-14 16:01:09 -04002198/*
2199 * store additional status at address
2200 */
2201int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
2202 unsigned long gpa)
2203{
2204 /* Only bits 0-53 are used for address formation */
2205 if (!(gpa & ~0x3ff))
2206 return 0;
2207
2208 return write_guest_abs(vcpu, gpa & ~0x3ff,
2209 (void *)&vcpu->run->s.regs.vrs, 512);
2210}
2211
2212int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr)
2213{
2214 if (!test_kvm_facility(vcpu->kvm, 129))
2215 return 0;
2216
2217 /*
2218 * The guest VXRS are in the host VXRs due to the lazy
2219 * copying in vcpu load/put. Let's update our copies before we save
2220 * it into the save area.
2221 */
2222 save_vx_regs((__vector128 *)&vcpu->run->s.regs.vrs);
2223
2224 return kvm_s390_store_adtl_status_unloaded(vcpu, addr);
2225}
2226
David Hildenbrand8ad35752014-03-14 11:00:21 +01002227static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2228{
2229 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02002230 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002231}
2232
2233static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
2234{
2235 unsigned int i;
2236 struct kvm_vcpu *vcpu;
2237
2238 kvm_for_each_vcpu(i, vcpu, kvm) {
2239 __disable_ibs_on_vcpu(vcpu);
2240 }
2241}
2242
2243static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2244{
2245 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02002246 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002247}
2248
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002249void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
2250{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002251 int i, online_vcpus, started_vcpus = 0;
2252
2253 if (!is_vcpu_stopped(vcpu))
2254 return;
2255
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002256 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002257 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002258 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002259 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2260
2261 for (i = 0; i < online_vcpus; i++) {
2262 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
2263 started_vcpus++;
2264 }
2265
2266 if (started_vcpus == 0) {
2267 /* we're the only active VCPU -> speed it up */
2268 __enable_ibs_on_vcpu(vcpu);
2269 } else if (started_vcpus == 1) {
2270 /*
2271 * As we are starting a second VCPU, we have to disable
2272 * the IBS facility on all VCPUs to remove potentially
2273 * oustanding ENABLE requests.
2274 */
2275 __disable_ibs_on_all_vcpus(vcpu->kvm);
2276 }
2277
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002278 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002279 /*
2280 * Another VCPU might have used IBS while we were offline.
2281 * Let's play safe and flush the VCPU at startup.
2282 */
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002283 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002284 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002285 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002286}
2287
2288void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
2289{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002290 int i, online_vcpus, started_vcpus = 0;
2291 struct kvm_vcpu *started_vcpu = NULL;
2292
2293 if (is_vcpu_stopped(vcpu))
2294 return;
2295
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002296 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002297 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002298 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002299 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2300
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002301 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
David Hildenbrand6cddd432014-10-15 16:48:53 +02002302 kvm_s390_clear_stop_irq(vcpu);
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002303
David Hildenbrand6cddd432014-10-15 16:48:53 +02002304 atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002305 __disable_ibs_on_vcpu(vcpu);
2306
2307 for (i = 0; i < online_vcpus; i++) {
2308 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
2309 started_vcpus++;
2310 started_vcpu = vcpu->kvm->vcpus[i];
2311 }
2312 }
2313
2314 if (started_vcpus == 1) {
2315 /*
2316 * As we only have one VCPU left, we want to enable the
2317 * IBS facility for that VCPU to speed it up.
2318 */
2319 __enable_ibs_on_vcpu(started_vcpu);
2320 }
2321
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002322 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002323 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002324}
2325
Cornelia Huckd6712df2012-12-20 15:32:11 +01002326static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2327 struct kvm_enable_cap *cap)
2328{
2329 int r;
2330
2331 if (cap->flags)
2332 return -EINVAL;
2333
2334 switch (cap->cap) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002335 case KVM_CAP_S390_CSS_SUPPORT:
2336 if (!vcpu->kvm->arch.css_support) {
2337 vcpu->kvm->arch.css_support = 1;
2338 trace_kvm_s390_enable_css(vcpu->kvm);
2339 }
2340 r = 0;
2341 break;
Cornelia Huckd6712df2012-12-20 15:32:11 +01002342 default:
2343 r = -EINVAL;
2344 break;
2345 }
2346 return r;
2347}
2348
Thomas Huth41408c22015-02-06 15:01:21 +01002349static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
2350 struct kvm_s390_mem_op *mop)
2351{
2352 void __user *uaddr = (void __user *)mop->buf;
2353 void *tmpbuf = NULL;
2354 int r, srcu_idx;
2355 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
2356 | KVM_S390_MEMOP_F_CHECK_ONLY;
2357
2358 if (mop->flags & ~supported_flags)
2359 return -EINVAL;
2360
2361 if (mop->size > MEM_OP_MAX_SIZE)
2362 return -E2BIG;
2363
2364 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
2365 tmpbuf = vmalloc(mop->size);
2366 if (!tmpbuf)
2367 return -ENOMEM;
2368 }
2369
2370 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2371
2372 switch (mop->op) {
2373 case KVM_S390_MEMOP_LOGICAL_READ:
2374 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
2375 r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, false);
2376 break;
2377 }
2378 r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2379 if (r == 0) {
2380 if (copy_to_user(uaddr, tmpbuf, mop->size))
2381 r = -EFAULT;
2382 }
2383 break;
2384 case KVM_S390_MEMOP_LOGICAL_WRITE:
2385 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
2386 r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, true);
2387 break;
2388 }
2389 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
2390 r = -EFAULT;
2391 break;
2392 }
2393 r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2394 break;
2395 default:
2396 r = -EINVAL;
2397 }
2398
2399 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2400
2401 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
2402 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
2403
2404 vfree(tmpbuf);
2405 return r;
2406}
2407
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002408long kvm_arch_vcpu_ioctl(struct file *filp,
2409 unsigned int ioctl, unsigned long arg)
2410{
2411 struct kvm_vcpu *vcpu = filp->private_data;
2412 void __user *argp = (void __user *)arg;
Thomas Huth800c1062013-09-12 10:33:45 +02002413 int idx;
Avi Kivitybc923cc2010-05-13 12:21:46 +03002414 long r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002415
Avi Kivity93736622010-05-13 12:35:17 +03002416 switch (ioctl) {
Jens Freimann47b43c52014-11-11 20:57:06 +01002417 case KVM_S390_IRQ: {
2418 struct kvm_s390_irq s390irq;
2419
2420 r = -EFAULT;
2421 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
2422 break;
2423 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
2424 break;
2425 }
Avi Kivity93736622010-05-13 12:35:17 +03002426 case KVM_S390_INTERRUPT: {
Carsten Otteba5c1e92008-03-25 18:47:26 +01002427 struct kvm_s390_interrupt s390int;
Jens Freimann383d0b02014-07-29 15:11:49 +02002428 struct kvm_s390_irq s390irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002429
Avi Kivity93736622010-05-13 12:35:17 +03002430 r = -EFAULT;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002431 if (copy_from_user(&s390int, argp, sizeof(s390int)))
Avi Kivity93736622010-05-13 12:35:17 +03002432 break;
Jens Freimann383d0b02014-07-29 15:11:49 +02002433 if (s390int_to_s390irq(&s390int, &s390irq))
2434 return -EINVAL;
2435 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
Avi Kivity93736622010-05-13 12:35:17 +03002436 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002437 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002438 case KVM_S390_STORE_STATUS:
Thomas Huth800c1062013-09-12 10:33:45 +02002439 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002440 r = kvm_s390_vcpu_store_status(vcpu, arg);
Thomas Huth800c1062013-09-12 10:33:45 +02002441 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002442 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002443 case KVM_S390_SET_INITIAL_PSW: {
2444 psw_t psw;
2445
Avi Kivitybc923cc2010-05-13 12:21:46 +03002446 r = -EFAULT;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002447 if (copy_from_user(&psw, argp, sizeof(psw)))
Avi Kivitybc923cc2010-05-13 12:21:46 +03002448 break;
2449 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
2450 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002451 }
2452 case KVM_S390_INITIAL_RESET:
Avi Kivitybc923cc2010-05-13 12:21:46 +03002453 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
2454 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02002455 case KVM_SET_ONE_REG:
2456 case KVM_GET_ONE_REG: {
2457 struct kvm_one_reg reg;
2458 r = -EFAULT;
2459 if (copy_from_user(&reg, argp, sizeof(reg)))
2460 break;
2461 if (ioctl == KVM_SET_ONE_REG)
2462 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
2463 else
2464 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
2465 break;
2466 }
Carsten Otte27e03932012-01-04 10:25:21 +01002467#ifdef CONFIG_KVM_S390_UCONTROL
2468 case KVM_S390_UCAS_MAP: {
2469 struct kvm_s390_ucas_mapping ucasmap;
2470
2471 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2472 r = -EFAULT;
2473 break;
2474 }
2475
2476 if (!kvm_is_ucontrol(vcpu->kvm)) {
2477 r = -EINVAL;
2478 break;
2479 }
2480
2481 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
2482 ucasmap.vcpu_addr, ucasmap.length);
2483 break;
2484 }
2485 case KVM_S390_UCAS_UNMAP: {
2486 struct kvm_s390_ucas_mapping ucasmap;
2487
2488 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2489 r = -EFAULT;
2490 break;
2491 }
2492
2493 if (!kvm_is_ucontrol(vcpu->kvm)) {
2494 r = -EINVAL;
2495 break;
2496 }
2497
2498 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
2499 ucasmap.length);
2500 break;
2501 }
2502#endif
Carsten Otteccc79102012-01-04 10:25:26 +01002503 case KVM_S390_VCPU_FAULT: {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002504 r = gmap_fault(vcpu->arch.gmap, arg, 0);
Carsten Otteccc79102012-01-04 10:25:26 +01002505 break;
2506 }
Cornelia Huckd6712df2012-12-20 15:32:11 +01002507 case KVM_ENABLE_CAP:
2508 {
2509 struct kvm_enable_cap cap;
2510 r = -EFAULT;
2511 if (copy_from_user(&cap, argp, sizeof(cap)))
2512 break;
2513 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2514 break;
2515 }
Thomas Huth41408c22015-02-06 15:01:21 +01002516 case KVM_S390_MEM_OP: {
2517 struct kvm_s390_mem_op mem_op;
2518
2519 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
2520 r = kvm_s390_guest_mem_op(vcpu, &mem_op);
2521 else
2522 r = -EFAULT;
2523 break;
2524 }
Jens Freimann816c7662014-11-24 17:13:46 +01002525 case KVM_S390_SET_IRQ_STATE: {
2526 struct kvm_s390_irq_state irq_state;
2527
2528 r = -EFAULT;
2529 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2530 break;
2531 if (irq_state.len > VCPU_IRQS_MAX_BUF ||
2532 irq_state.len == 0 ||
2533 irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
2534 r = -EINVAL;
2535 break;
2536 }
2537 r = kvm_s390_set_irq_state(vcpu,
2538 (void __user *) irq_state.buf,
2539 irq_state.len);
2540 break;
2541 }
2542 case KVM_S390_GET_IRQ_STATE: {
2543 struct kvm_s390_irq_state irq_state;
2544
2545 r = -EFAULT;
2546 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2547 break;
2548 if (irq_state.len == 0) {
2549 r = -EINVAL;
2550 break;
2551 }
2552 r = kvm_s390_get_irq_state(vcpu,
2553 (__u8 __user *) irq_state.buf,
2554 irq_state.len);
2555 break;
2556 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002557 default:
Carsten Otte3e6afcf2012-01-04 10:25:30 +01002558 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002559 }
Avi Kivitybc923cc2010-05-13 12:21:46 +03002560 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002561}
2562
Carsten Otte5b1c1492012-01-04 10:25:23 +01002563int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2564{
2565#ifdef CONFIG_KVM_S390_UCONTROL
2566 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
2567 && (kvm_is_ucontrol(vcpu->kvm))) {
2568 vmf->page = virt_to_page(vcpu->arch.sie_block);
2569 get_page(vmf->page);
2570 return 0;
2571 }
2572#endif
2573 return VM_FAULT_SIGBUS;
2574}
2575
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05302576int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2577 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09002578{
2579 return 0;
2580}
2581
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002582/* Section: memory related */
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002583int kvm_arch_prepare_memory_region(struct kvm *kvm,
2584 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002585 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09002586 enum kvm_mr_change change)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002587{
Nick Wangdd2887e2013-03-25 17:22:57 +01002588 /* A few sanity checks. We can have memory slots which have to be
2589 located/ended at a segment boundary (1MB). The memory in userland is
2590 ok to be fragmented into various different vmas. It is okay to mmap()
2591 and munmap() stuff in this slot after doing this call at any time */
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002592
Carsten Otte598841c2011-07-24 10:48:21 +02002593 if (mem->userspace_addr & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002594 return -EINVAL;
2595
Carsten Otte598841c2011-07-24 10:48:21 +02002596 if (mem->memory_size & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002597 return -EINVAL;
2598
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002599 return 0;
2600}
2601
2602void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002603 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002604 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02002605 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002606 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002607{
Carsten Ottef7850c92011-07-24 10:48:23 +02002608 int rc;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002609
Christian Borntraeger2cef4de2013-03-25 17:22:48 +01002610 /* If the basics of the memslot do not change, we do not want
2611 * to update the gmap. Every update causes several unnecessary
2612 * segment translation exceptions. This is usually handled just
2613 * fine by the normal fault handler + gmap, but it will also
2614 * cause faults on the prefix page of running guest CPUs.
2615 */
2616 if (old->userspace_addr == mem->userspace_addr &&
2617 old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
2618 old->npages * PAGE_SIZE == mem->memory_size)
2619 return;
Carsten Otte598841c2011-07-24 10:48:21 +02002620
2621 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
2622 mem->guest_phys_addr, mem->memory_size);
2623 if (rc)
David Hildenbrandea2cdd22015-05-20 13:24:02 +02002624 pr_warn("failed to commit memory region\n");
Carsten Otte598841c2011-07-24 10:48:21 +02002625 return;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002626}
2627
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002628static int __init kvm_s390_init(void)
2629{
Michael Mueller9d8d5782015-02-02 15:42:51 +01002630 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002631}
2632
2633static void __exit kvm_s390_exit(void)
2634{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002635 kvm_exit();
2636}
2637
2638module_init(kvm_s390_init);
2639module_exit(kvm_s390_exit);
Cornelia Huck566af942013-05-27 18:42:33 +02002640
2641/*
2642 * Enable autoloading of the kvm module.
2643 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
2644 * since x86 takes a different approach.
2645 */
2646#include <linux/miscdevice.h>
2647MODULE_ALIAS_MISCDEV(KVM_MINOR);
2648MODULE_ALIAS("devname:kvm");