blob: be1f0288443edae18396a79eb2353ce9c99854d9 [file] [log] [blame]
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * hosting zSeries kernel virtual machines
Heiko Carstensb0c632d2008-03-25 18:47:20 +01003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2008, 2009
Heiko Carstensb0c632d2008-03-25 18:47:20 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 * Heiko Carstens <heiko.carstens@de.ibm.com>
Christian Ehrhardt628eb9b2009-05-25 13:40:51 +020013 * Christian Ehrhardt <ehrhardt@de.ibm.com>
Jason J. Herne15f36eb2012-08-02 10:10:17 -040014 * Jason J. Herne <jjherne@us.ibm.com>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010015 */
16
17#include <linux/compiler.h>
18#include <linux/err.h>
19#include <linux/fs.h>
Christian Borntraegerca872302009-05-12 17:21:49 +020020#include <linux/hrtimer.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010021#include <linux/init.h>
22#include <linux/kvm.h>
23#include <linux/kvm_host.h>
24#include <linux/module.h>
Tony Krowiaka374e892014-09-03 10:13:53 +020025#include <linux/random.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010026#include <linux/slab.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010027#include <linux/timer.h>
Thomas Huth41408c282015-02-06 15:01:21 +010028#include <linux/vmalloc.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010029#include <asm/asm-offsets.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010030#include <asm/lowcore.h>
Fan Zhangfdf03652015-05-13 10:58:41 +020031#include <asm/etr.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010032#include <asm/pgtable.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010033#include <asm/gmap.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010034#include <asm/nmi.h>
David Howellsa0616cd2012-03-28 18:30:02 +010035#include <asm/switch_to.h>
Jens Freimann6d3da242013-07-03 15:18:35 +020036#include <asm/isc.h>
Christian Borntraeger1526bf92012-05-15 14:15:25 +020037#include <asm/sclp.h>
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010038#include "kvm-s390.h"
Heiko Carstensb0c632d2008-03-25 18:47:20 +010039#include "gaccess.h"
40
David Hildenbrandea2cdd22015-05-20 13:24:02 +020041#define KMSG_COMPONENT "kvm-s390"
42#undef pr_fmt
43#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
44
Cornelia Huck5786fff2012-07-23 17:20:29 +020045#define CREATE_TRACE_POINTS
46#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020047#include "trace-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020048
Thomas Huth41408c282015-02-06 15:01:21 +010049#define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
Jens Freimann816c7662014-11-24 17:13:46 +010050#define LOCAL_IRQS 32
51#define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
52 (KVM_MAX_VCPUS + LOCAL_IRQS))
Thomas Huth41408c282015-02-06 15:01:21 +010053
Heiko Carstensb0c632d2008-03-25 18:47:20 +010054#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
55
56struct kvm_stats_debugfs_item debugfs_entries[] = {
57 { "userspace_handled", VCPU_STAT(exit_userspace) },
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +020058 { "exit_null", VCPU_STAT(exit_null) },
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010059 { "exit_validity", VCPU_STAT(exit_validity) },
60 { "exit_stop_request", VCPU_STAT(exit_stop_request) },
61 { "exit_external_request", VCPU_STAT(exit_external_request) },
62 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010063 { "exit_instruction", VCPU_STAT(exit_instruction) },
64 { "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
65 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
Paolo Bonzinif7819512015-02-04 18:20:58 +010066 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +020067 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
David Hildenbrandce2e4f02014-07-11 10:00:43 +020068 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020069 { "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010070 { "instruction_lctl", VCPU_STAT(instruction_lctl) },
David Hildenbrandaba07502014-01-23 10:47:13 +010071 { "instruction_stctl", VCPU_STAT(instruction_stctl) },
72 { "instruction_stctg", VCPU_STAT(instruction_stctg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010073 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020074 { "deliver_external_call", VCPU_STAT(deliver_external_call) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010075 { "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
76 { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
77 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
78 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
79 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
80 { "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
81 { "exit_wait_state", VCPU_STAT(exit_wait_state) },
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020082 { "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010083 { "instruction_stidp", VCPU_STAT(instruction_stidp) },
84 { "instruction_spx", VCPU_STAT(instruction_spx) },
85 { "instruction_stpx", VCPU_STAT(instruction_stpx) },
86 { "instruction_stap", VCPU_STAT(instruction_stap) },
87 { "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
Heiko Carstens8a2422342014-01-10 14:33:28 +010088 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010089 { "instruction_stsch", VCPU_STAT(instruction_stsch) },
90 { "instruction_chsc", VCPU_STAT(instruction_chsc) },
Konstantin Weitzb31288f2013-04-17 17:36:29 +020091 { "instruction_essa", VCPU_STAT(instruction_essa) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010092 { "instruction_stsi", VCPU_STAT(instruction_stsi) },
93 { "instruction_stfl", VCPU_STAT(instruction_stfl) },
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +020094 { "instruction_tprot", VCPU_STAT(instruction_tprot) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010095 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
Cornelia Huckbd59d3a2011-11-17 11:00:42 +010096 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020097 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010098 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +020099 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
100 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100101 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200102 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
103 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
Eric Farmancd7b4b62015-02-12 09:06:34 -0500104 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100105 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
106 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
107 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200108 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
109 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
110 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
Christian Borntraeger388186b2011-10-30 15:17:03 +0100111 { "diagnose_10", VCPU_STAT(diagnose_10) },
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100112 { "diagnose_44", VCPU_STAT(diagnose_44) },
Konstantin Weitz41628d32012-04-25 15:30:38 +0200113 { "diagnose_9c", VCPU_STAT(diagnose_9c) },
Christian Borntraeger175a5c92015-07-07 15:19:32 +0200114 { "diagnose_258", VCPU_STAT(diagnose_258) },
115 { "diagnose_308", VCPU_STAT(diagnose_308) },
116 { "diagnose_500", VCPU_STAT(diagnose_500) },
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100117 { NULL }
118};
119
Michael Mueller9d8d5782015-02-02 15:42:51 +0100120/* upper facilities limit for kvm */
121unsigned long kvm_s390_fac_list_mask[] = {
Christian Borntraegera3ed8da2015-03-18 13:54:31 +0100122 0xffe6fffbfcfdfc40UL,
Guenther Hutzl53df84f2015-02-18 11:13:03 +0100123 0x005e800000000000UL,
Michael Mueller9d8d5782015-02-02 15:42:51 +0100124};
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100125
Michael Mueller9d8d5782015-02-02 15:42:51 +0100126unsigned long kvm_s390_fac_list_mask_size(void)
Michael Mueller78c4b592013-07-26 15:04:04 +0200127{
Michael Mueller9d8d5782015-02-02 15:42:51 +0100128 BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
129 return ARRAY_SIZE(kvm_s390_fac_list_mask);
Michael Mueller78c4b592013-07-26 15:04:04 +0200130}
131
Michael Mueller9d8d5782015-02-02 15:42:51 +0100132static struct gmap_notifier gmap_notifier;
Christian Borntraeger78f26132015-07-22 15:50:58 +0200133debug_info_t *kvm_s390_dbf;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100134
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100135/* Section: not file related */
Radim Krčmář13a34e02014-08-28 15:13:03 +0200136int kvm_arch_hardware_enable(void)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100137{
138 /* every s390 is virtualization enabled ;-) */
Alexander Graf10474ae2009-09-15 11:37:46 +0200139 return 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100140}
141
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200142static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address);
143
Fan Zhangfdf03652015-05-13 10:58:41 +0200144/*
145 * This callback is executed during stop_machine(). All CPUs are therefore
146 * temporarily stopped. In order not to change guest behavior, we have to
147 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
148 * so a CPU won't be stopped while calculating with the epoch.
149 */
150static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
151 void *v)
152{
153 struct kvm *kvm;
154 struct kvm_vcpu *vcpu;
155 int i;
156 unsigned long long *delta = v;
157
158 list_for_each_entry(kvm, &vm_list, vm_list) {
159 kvm->arch.epoch -= *delta;
160 kvm_for_each_vcpu(i, vcpu, kvm) {
161 vcpu->arch.sie_block->epoch -= *delta;
162 }
163 }
164 return NOTIFY_OK;
165}
166
167static struct notifier_block kvm_clock_notifier = {
168 .notifier_call = kvm_clock_sync,
169};
170
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100171int kvm_arch_hardware_setup(void)
172{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200173 gmap_notifier.notifier_call = kvm_gmap_notifier;
174 gmap_register_ipte_notifier(&gmap_notifier);
Fan Zhangfdf03652015-05-13 10:58:41 +0200175 atomic_notifier_chain_register(&s390_epoch_delta_notifier,
176 &kvm_clock_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100177 return 0;
178}
179
180void kvm_arch_hardware_unsetup(void)
181{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200182 gmap_unregister_ipte_notifier(&gmap_notifier);
Fan Zhangfdf03652015-05-13 10:58:41 +0200183 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
184 &kvm_clock_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100185}
186
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100187int kvm_arch_init(void *opaque)
188{
Christian Borntraeger78f26132015-07-22 15:50:58 +0200189 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
190 if (!kvm_s390_dbf)
191 return -ENOMEM;
192
193 if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
194 debug_unregister(kvm_s390_dbf);
195 return -ENOMEM;
196 }
197
Cornelia Huck84877d92014-09-02 10:27:35 +0100198 /* Register floating interrupt controller interface. */
199 return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100200}
201
Christian Borntraeger78f26132015-07-22 15:50:58 +0200202void kvm_arch_exit(void)
203{
204 debug_unregister(kvm_s390_dbf);
205}
206
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100207/* Section: device related */
208long kvm_arch_dev_ioctl(struct file *filp,
209 unsigned int ioctl, unsigned long arg)
210{
211 if (ioctl == KVM_S390_ENABLE_SIE)
212 return s390_enable_sie();
213 return -EINVAL;
214}
215
Alexander Graf784aa3d2014-07-14 18:27:35 +0200216int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100217{
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100218 int r;
219
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200220 switch (ext) {
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100221 case KVM_CAP_S390_PSW:
Christian Borntraegerb6cf8782011-09-20 17:07:29 +0200222 case KVM_CAP_S390_GMAP:
Christian Borntraeger52e16b12011-11-17 11:00:44 +0100223 case KVM_CAP_SYNC_MMU:
Carsten Otte1efd0f52012-01-04 10:25:29 +0100224#ifdef CONFIG_KVM_S390_UCONTROL
225 case KVM_CAP_S390_UCONTROL:
226#endif
Dominik Dingel3c038e62013-10-07 17:11:48 +0200227 case KVM_CAP_ASYNC_PF:
Christian Borntraeger60b413c2012-01-11 11:20:31 +0100228 case KVM_CAP_SYNC_REGS:
Carsten Otte14eebd92012-05-15 14:15:26 +0200229 case KVM_CAP_ONE_REG:
Cornelia Huckd6712df2012-12-20 15:32:11 +0100230 case KVM_CAP_ENABLE_CAP:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100231 case KVM_CAP_S390_CSS_SUPPORT:
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100232 case KVM_CAP_IOEVENTFD:
Jens Freimannc05c4182013-10-07 16:13:45 +0200233 case KVM_CAP_DEVICE_CTRL:
Cornelia Huckd938dc52013-10-23 18:26:34 +0200234 case KVM_CAP_ENABLE_CAP_VM:
Cornelia Huck78599d92014-07-15 09:54:39 +0200235 case KVM_CAP_S390_IRQCHIP:
Dominik Dingelf2061652014-04-09 13:13:00 +0200236 case KVM_CAP_VM_ATTRIBUTES:
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200237 case KVM_CAP_MP_STATE:
Jens Freimann47b43c52014-11-11 20:57:06 +0100238 case KVM_CAP_S390_INJECT_IRQ:
David Hildenbrand2444b352014-10-09 14:10:13 +0200239 case KVM_CAP_S390_USER_SIGP:
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100240 case KVM_CAP_S390_USER_STSI:
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400241 case KVM_CAP_S390_SKEYS:
Jens Freimann816c7662014-11-24 17:13:46 +0100242 case KVM_CAP_S390_IRQ_STATE:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100243 r = 1;
244 break;
Thomas Huth41408c282015-02-06 15:01:21 +0100245 case KVM_CAP_S390_MEM_OP:
246 r = MEM_OP_MAX_SIZE;
247 break;
Christian Borntraegere726b1b2012-05-02 10:50:38 +0200248 case KVM_CAP_NR_VCPUS:
249 case KVM_CAP_MAX_VCPUS:
Eugene (jno) Dvurechenskife0edcb2015-04-22 18:37:40 +0200250 r = sclp.has_esca ? KVM_S390_ESCA_CPU_SLOTS
251 : KVM_S390_BSCA_CPU_SLOTS;
Christian Borntraegere726b1b2012-05-02 10:50:38 +0200252 break;
Nick Wange1e2e602013-03-25 17:22:58 +0100253 case KVM_CAP_NR_MEMSLOTS:
254 r = KVM_USER_MEM_SLOTS;
255 break;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200256 case KVM_CAP_S390_COW:
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100257 r = MACHINE_HAS_ESOP;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200258 break;
Eric Farman68c55752014-06-09 10:57:26 -0400259 case KVM_CAP_S390_VECTOR_REGISTERS:
260 r = MACHINE_HAS_VX;
261 break;
Fan Zhangc6e5f162016-01-07 18:24:29 +0800262 case KVM_CAP_S390_RI:
263 r = test_facility(64);
264 break;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200265 default:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100266 r = 0;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200267 }
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100268 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100269}
270
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400271static void kvm_s390_sync_dirty_log(struct kvm *kvm,
272 struct kvm_memory_slot *memslot)
273{
274 gfn_t cur_gfn, last_gfn;
275 unsigned long address;
276 struct gmap *gmap = kvm->arch.gmap;
277
278 down_read(&gmap->mm->mmap_sem);
279 /* Loop over all guest pages */
280 last_gfn = memslot->base_gfn + memslot->npages;
281 for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
282 address = gfn_to_hva_memslot(memslot, cur_gfn);
283
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100284 if (test_and_clear_guest_dirty(gmap->mm, address))
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400285 mark_page_dirty(kvm, cur_gfn);
286 }
287 up_read(&gmap->mm->mmap_sem);
288}
289
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100290/* Section: vm related */
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +0200291static void sca_del_vcpu(struct kvm_vcpu *vcpu);
292
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100293/*
294 * Get (and clear) the dirty memory log for a memory slot.
295 */
296int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
297 struct kvm_dirty_log *log)
298{
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400299 int r;
300 unsigned long n;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200301 struct kvm_memslots *slots;
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400302 struct kvm_memory_slot *memslot;
303 int is_dirty = 0;
304
305 mutex_lock(&kvm->slots_lock);
306
307 r = -EINVAL;
308 if (log->slot >= KVM_USER_MEM_SLOTS)
309 goto out;
310
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200311 slots = kvm_memslots(kvm);
312 memslot = id_to_memslot(slots, log->slot);
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400313 r = -ENOENT;
314 if (!memslot->dirty_bitmap)
315 goto out;
316
317 kvm_s390_sync_dirty_log(kvm, memslot);
318 r = kvm_get_dirty_log(kvm, log, &is_dirty);
319 if (r)
320 goto out;
321
322 /* Clear the dirty log */
323 if (is_dirty) {
324 n = kvm_dirty_bitmap_bytes(memslot);
325 memset(memslot->dirty_bitmap, 0, n);
326 }
327 r = 0;
328out:
329 mutex_unlock(&kvm->slots_lock);
330 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100331}
332
Cornelia Huckd938dc52013-10-23 18:26:34 +0200333static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
334{
335 int r;
336
337 if (cap->flags)
338 return -EINVAL;
339
340 switch (cap->cap) {
Cornelia Huck84223592013-07-15 13:36:01 +0200341 case KVM_CAP_S390_IRQCHIP:
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200342 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
Cornelia Huck84223592013-07-15 13:36:01 +0200343 kvm->arch.use_irqchip = 1;
344 r = 0;
345 break;
David Hildenbrand2444b352014-10-09 14:10:13 +0200346 case KVM_CAP_S390_USER_SIGP:
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200347 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
David Hildenbrand2444b352014-10-09 14:10:13 +0200348 kvm->arch.user_sigp = 1;
349 r = 0;
350 break;
Eric Farman68c55752014-06-09 10:57:26 -0400351 case KVM_CAP_S390_VECTOR_REGISTERS:
David Hildenbrand5967c172015-11-06 12:08:48 +0100352 mutex_lock(&kvm->lock);
353 if (atomic_read(&kvm->online_vcpus)) {
354 r = -EBUSY;
355 } else if (MACHINE_HAS_VX) {
Michael Mueller18280d82015-03-16 16:05:41 +0100356 set_kvm_facility(kvm->arch.model.fac->mask, 129);
357 set_kvm_facility(kvm->arch.model.fac->list, 129);
358 r = 0;
359 } else
360 r = -EINVAL;
David Hildenbrand5967c172015-11-06 12:08:48 +0100361 mutex_unlock(&kvm->lock);
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200362 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
363 r ? "(not available)" : "(success)");
Eric Farman68c55752014-06-09 10:57:26 -0400364 break;
Fan Zhangc6e5f162016-01-07 18:24:29 +0800365 case KVM_CAP_S390_RI:
366 r = -EINVAL;
367 mutex_lock(&kvm->lock);
368 if (atomic_read(&kvm->online_vcpus)) {
369 r = -EBUSY;
370 } else if (test_facility(64)) {
371 set_kvm_facility(kvm->arch.model.fac->mask, 64);
372 set_kvm_facility(kvm->arch.model.fac->list, 64);
373 r = 0;
374 }
375 mutex_unlock(&kvm->lock);
376 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
377 r ? "(not available)" : "(success)");
378 break;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100379 case KVM_CAP_S390_USER_STSI:
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200380 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100381 kvm->arch.user_stsi = 1;
382 r = 0;
383 break;
Cornelia Huckd938dc52013-10-23 18:26:34 +0200384 default:
385 r = -EINVAL;
386 break;
387 }
388 return r;
389}
390
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100391static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
392{
393 int ret;
394
395 switch (attr->attr) {
396 case KVM_S390_VM_MEM_LIMIT_SIZE:
397 ret = 0;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200398 VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
Dominik Dingela3a92c32014-12-01 17:24:42 +0100399 kvm->arch.mem_limit);
400 if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100401 ret = -EFAULT;
402 break;
403 default:
404 ret = -ENXIO;
405 break;
406 }
407 return ret;
408}
409
410static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200411{
412 int ret;
413 unsigned int idx;
414 switch (attr->attr) {
415 case KVM_S390_VM_MEM_ENABLE_CMMA:
Dominik Dingele6db1d62015-05-07 15:41:57 +0200416 /* enable CMMA only for z10 and later (EDAT_1) */
417 ret = -EINVAL;
418 if (!MACHINE_IS_LPAR || !MACHINE_HAS_EDAT1)
419 break;
420
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200421 ret = -EBUSY;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200422 VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200423 mutex_lock(&kvm->lock);
424 if (atomic_read(&kvm->online_vcpus) == 0) {
425 kvm->arch.use_cmma = 1;
426 ret = 0;
427 }
428 mutex_unlock(&kvm->lock);
429 break;
430 case KVM_S390_VM_MEM_CLR_CMMA:
Dominik Dingelc3489152015-06-18 13:17:11 +0200431 ret = -EINVAL;
432 if (!kvm->arch.use_cmma)
433 break;
434
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200435 VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200436 mutex_lock(&kvm->lock);
437 idx = srcu_read_lock(&kvm->srcu);
Dominik Dingela13cff32014-10-23 12:07:14 +0200438 s390_reset_cmma(kvm->arch.gmap->mm);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200439 srcu_read_unlock(&kvm->srcu, idx);
440 mutex_unlock(&kvm->lock);
441 ret = 0;
442 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100443 case KVM_S390_VM_MEM_LIMIT_SIZE: {
444 unsigned long new_limit;
445
446 if (kvm_is_ucontrol(kvm))
447 return -EINVAL;
448
449 if (get_user(new_limit, (u64 __user *)attr->addr))
450 return -EFAULT;
451
Dominik Dingela3a92c32014-12-01 17:24:42 +0100452 if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
453 new_limit > kvm->arch.mem_limit)
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100454 return -E2BIG;
455
Dominik Dingela3a92c32014-12-01 17:24:42 +0100456 if (!new_limit)
457 return -EINVAL;
458
459 /* gmap_alloc takes last usable address */
460 if (new_limit != KVM_S390_NO_MEM_LIMIT)
461 new_limit -= 1;
462
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100463 ret = -EBUSY;
464 mutex_lock(&kvm->lock);
465 if (atomic_read(&kvm->online_vcpus) == 0) {
466 /* gmap_alloc will round the limit up */
467 struct gmap *new = gmap_alloc(current->mm, new_limit);
468
469 if (!new) {
470 ret = -ENOMEM;
471 } else {
472 gmap_free(kvm->arch.gmap);
473 new->private = kvm;
474 kvm->arch.gmap = new;
475 ret = 0;
476 }
477 }
478 mutex_unlock(&kvm->lock);
Dominik Dingela3a92c32014-12-01 17:24:42 +0100479 VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
480 VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
481 (void *) kvm->arch.gmap->asce);
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100482 break;
483 }
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200484 default:
485 ret = -ENXIO;
486 break;
487 }
488 return ret;
489}
490
Tony Krowiaka374e892014-09-03 10:13:53 +0200491static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
492
493static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
494{
495 struct kvm_vcpu *vcpu;
496 int i;
497
Michael Mueller9d8d5782015-02-02 15:42:51 +0100498 if (!test_kvm_facility(kvm, 76))
Tony Krowiaka374e892014-09-03 10:13:53 +0200499 return -EINVAL;
500
501 mutex_lock(&kvm->lock);
502 switch (attr->attr) {
503 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
504 get_random_bytes(
505 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
506 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
507 kvm->arch.crypto.aes_kw = 1;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200508 VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200509 break;
510 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
511 get_random_bytes(
512 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
513 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
514 kvm->arch.crypto.dea_kw = 1;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200515 VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200516 break;
517 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
518 kvm->arch.crypto.aes_kw = 0;
519 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
520 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200521 VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200522 break;
523 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
524 kvm->arch.crypto.dea_kw = 0;
525 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
526 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200527 VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200528 break;
529 default:
530 mutex_unlock(&kvm->lock);
531 return -ENXIO;
532 }
533
534 kvm_for_each_vcpu(i, vcpu, kvm) {
535 kvm_s390_vcpu_crypto_setup(vcpu);
536 exit_sie(vcpu);
537 }
538 mutex_unlock(&kvm->lock);
539 return 0;
540}
541
Jason J. Herne72f25022014-11-25 09:46:02 -0500542static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
543{
544 u8 gtod_high;
545
546 if (copy_from_user(&gtod_high, (void __user *)attr->addr,
547 sizeof(gtod_high)))
548 return -EFAULT;
549
550 if (gtod_high != 0)
551 return -EINVAL;
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200552 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
Jason J. Herne72f25022014-11-25 09:46:02 -0500553
554 return 0;
555}
556
557static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
558{
David Hildenbrand5a3d8832015-09-29 16:27:24 +0200559 u64 gtod;
Jason J. Herne72f25022014-11-25 09:46:02 -0500560
561 if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
562 return -EFAULT;
563
David Hildenbrand25ed1672015-05-12 09:49:14 +0200564 kvm_s390_set_tod_clock(kvm, gtod);
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200565 VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
Jason J. Herne72f25022014-11-25 09:46:02 -0500566 return 0;
567}
568
569static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
570{
571 int ret;
572
573 if (attr->flags)
574 return -EINVAL;
575
576 switch (attr->attr) {
577 case KVM_S390_VM_TOD_HIGH:
578 ret = kvm_s390_set_tod_high(kvm, attr);
579 break;
580 case KVM_S390_VM_TOD_LOW:
581 ret = kvm_s390_set_tod_low(kvm, attr);
582 break;
583 default:
584 ret = -ENXIO;
585 break;
586 }
587 return ret;
588}
589
590static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
591{
592 u8 gtod_high = 0;
593
594 if (copy_to_user((void __user *)attr->addr, &gtod_high,
595 sizeof(gtod_high)))
596 return -EFAULT;
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200597 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
Jason J. Herne72f25022014-11-25 09:46:02 -0500598
599 return 0;
600}
601
602static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
603{
David Hildenbrand5a3d8832015-09-29 16:27:24 +0200604 u64 gtod;
Jason J. Herne72f25022014-11-25 09:46:02 -0500605
David Hildenbrand60417fc2015-09-29 16:20:36 +0200606 gtod = kvm_s390_get_tod_clock_fast(kvm);
Jason J. Herne72f25022014-11-25 09:46:02 -0500607 if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
608 return -EFAULT;
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200609 VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
Jason J. Herne72f25022014-11-25 09:46:02 -0500610
611 return 0;
612}
613
614static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
615{
616 int ret;
617
618 if (attr->flags)
619 return -EINVAL;
620
621 switch (attr->attr) {
622 case KVM_S390_VM_TOD_HIGH:
623 ret = kvm_s390_get_tod_high(kvm, attr);
624 break;
625 case KVM_S390_VM_TOD_LOW:
626 ret = kvm_s390_get_tod_low(kvm, attr);
627 break;
628 default:
629 ret = -ENXIO;
630 break;
631 }
632 return ret;
633}
634
Michael Mueller658b6ed2015-02-02 15:49:35 +0100635static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
636{
637 struct kvm_s390_vm_cpu_processor *proc;
638 int ret = 0;
639
640 mutex_lock(&kvm->lock);
641 if (atomic_read(&kvm->online_vcpus)) {
642 ret = -EBUSY;
643 goto out;
644 }
645 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
646 if (!proc) {
647 ret = -ENOMEM;
648 goto out;
649 }
650 if (!copy_from_user(proc, (void __user *)attr->addr,
651 sizeof(*proc))) {
652 memcpy(&kvm->arch.model.cpu_id, &proc->cpuid,
653 sizeof(struct cpuid));
654 kvm->arch.model.ibc = proc->ibc;
Michael Mueller981467c2015-02-24 13:51:04 +0100655 memcpy(kvm->arch.model.fac->list, proc->fac_list,
Michael Mueller658b6ed2015-02-02 15:49:35 +0100656 S390_ARCH_FAC_LIST_SIZE_BYTE);
657 } else
658 ret = -EFAULT;
659 kfree(proc);
660out:
661 mutex_unlock(&kvm->lock);
662 return ret;
663}
664
665static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
666{
667 int ret = -ENXIO;
668
669 switch (attr->attr) {
670 case KVM_S390_VM_CPU_PROCESSOR:
671 ret = kvm_s390_set_processor(kvm, attr);
672 break;
673 }
674 return ret;
675}
676
677static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
678{
679 struct kvm_s390_vm_cpu_processor *proc;
680 int ret = 0;
681
682 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
683 if (!proc) {
684 ret = -ENOMEM;
685 goto out;
686 }
687 memcpy(&proc->cpuid, &kvm->arch.model.cpu_id, sizeof(struct cpuid));
688 proc->ibc = kvm->arch.model.ibc;
Michael Mueller981467c2015-02-24 13:51:04 +0100689 memcpy(&proc->fac_list, kvm->arch.model.fac->list, S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100690 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
691 ret = -EFAULT;
692 kfree(proc);
693out:
694 return ret;
695}
696
697static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
698{
699 struct kvm_s390_vm_cpu_machine *mach;
700 int ret = 0;
701
702 mach = kzalloc(sizeof(*mach), GFP_KERNEL);
703 if (!mach) {
704 ret = -ENOMEM;
705 goto out;
706 }
707 get_cpu_id((struct cpuid *) &mach->cpuid);
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200708 mach->ibc = sclp.ibc;
Michael Mueller981467c2015-02-24 13:51:04 +0100709 memcpy(&mach->fac_mask, kvm->arch.model.fac->mask,
710 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100711 memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +0100712 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100713 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
714 ret = -EFAULT;
715 kfree(mach);
716out:
717 return ret;
718}
719
720static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
721{
722 int ret = -ENXIO;
723
724 switch (attr->attr) {
725 case KVM_S390_VM_CPU_PROCESSOR:
726 ret = kvm_s390_get_processor(kvm, attr);
727 break;
728 case KVM_S390_VM_CPU_MACHINE:
729 ret = kvm_s390_get_machine(kvm, attr);
730 break;
731 }
732 return ret;
733}
734
Dominik Dingelf2061652014-04-09 13:13:00 +0200735static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
736{
737 int ret;
738
739 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200740 case KVM_S390_VM_MEM_CTRL:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100741 ret = kvm_s390_set_mem_control(kvm, attr);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200742 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500743 case KVM_S390_VM_TOD:
744 ret = kvm_s390_set_tod(kvm, attr);
745 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100746 case KVM_S390_VM_CPU_MODEL:
747 ret = kvm_s390_set_cpu_model(kvm, attr);
748 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200749 case KVM_S390_VM_CRYPTO:
750 ret = kvm_s390_vm_set_crypto(kvm, attr);
751 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200752 default:
753 ret = -ENXIO;
754 break;
755 }
756
757 return ret;
758}
759
760static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
761{
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100762 int ret;
763
764 switch (attr->group) {
765 case KVM_S390_VM_MEM_CTRL:
766 ret = kvm_s390_get_mem_control(kvm, attr);
767 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500768 case KVM_S390_VM_TOD:
769 ret = kvm_s390_get_tod(kvm, attr);
770 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100771 case KVM_S390_VM_CPU_MODEL:
772 ret = kvm_s390_get_cpu_model(kvm, attr);
773 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100774 default:
775 ret = -ENXIO;
776 break;
777 }
778
779 return ret;
Dominik Dingelf2061652014-04-09 13:13:00 +0200780}
781
782static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
783{
784 int ret;
785
786 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200787 case KVM_S390_VM_MEM_CTRL:
788 switch (attr->attr) {
789 case KVM_S390_VM_MEM_ENABLE_CMMA:
790 case KVM_S390_VM_MEM_CLR_CMMA:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100791 case KVM_S390_VM_MEM_LIMIT_SIZE:
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200792 ret = 0;
793 break;
794 default:
795 ret = -ENXIO;
796 break;
797 }
798 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500799 case KVM_S390_VM_TOD:
800 switch (attr->attr) {
801 case KVM_S390_VM_TOD_LOW:
802 case KVM_S390_VM_TOD_HIGH:
803 ret = 0;
804 break;
805 default:
806 ret = -ENXIO;
807 break;
808 }
809 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100810 case KVM_S390_VM_CPU_MODEL:
811 switch (attr->attr) {
812 case KVM_S390_VM_CPU_PROCESSOR:
813 case KVM_S390_VM_CPU_MACHINE:
814 ret = 0;
815 break;
816 default:
817 ret = -ENXIO;
818 break;
819 }
820 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200821 case KVM_S390_VM_CRYPTO:
822 switch (attr->attr) {
823 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
824 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
825 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
826 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
827 ret = 0;
828 break;
829 default:
830 ret = -ENXIO;
831 break;
832 }
833 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200834 default:
835 ret = -ENXIO;
836 break;
837 }
838
839 return ret;
840}
841
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400842static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
843{
844 uint8_t *keys;
845 uint64_t hva;
846 unsigned long curkey;
847 int i, r = 0;
848
849 if (args->flags != 0)
850 return -EINVAL;
851
852 /* Is this guest using storage keys? */
853 if (!mm_use_skey(current->mm))
854 return KVM_S390_GET_SKEYS_NONE;
855
856 /* Enforce sane limit on memory allocation */
857 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
858 return -EINVAL;
859
860 keys = kmalloc_array(args->count, sizeof(uint8_t),
861 GFP_KERNEL | __GFP_NOWARN);
862 if (!keys)
863 keys = vmalloc(sizeof(uint8_t) * args->count);
864 if (!keys)
865 return -ENOMEM;
866
867 for (i = 0; i < args->count; i++) {
868 hva = gfn_to_hva(kvm, args->start_gfn + i);
869 if (kvm_is_error_hva(hva)) {
870 r = -EFAULT;
871 goto out;
872 }
873
874 curkey = get_guest_storage_key(current->mm, hva);
875 if (IS_ERR_VALUE(curkey)) {
876 r = curkey;
877 goto out;
878 }
879 keys[i] = curkey;
880 }
881
882 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
883 sizeof(uint8_t) * args->count);
884 if (r)
885 r = -EFAULT;
886out:
887 kvfree(keys);
888 return r;
889}
890
891static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
892{
893 uint8_t *keys;
894 uint64_t hva;
895 int i, r = 0;
896
897 if (args->flags != 0)
898 return -EINVAL;
899
900 /* Enforce sane limit on memory allocation */
901 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
902 return -EINVAL;
903
904 keys = kmalloc_array(args->count, sizeof(uint8_t),
905 GFP_KERNEL | __GFP_NOWARN);
906 if (!keys)
907 keys = vmalloc(sizeof(uint8_t) * args->count);
908 if (!keys)
909 return -ENOMEM;
910
911 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
912 sizeof(uint8_t) * args->count);
913 if (r) {
914 r = -EFAULT;
915 goto out;
916 }
917
918 /* Enable storage key handling for the guest */
Dominik Dingel14d4a422015-05-07 15:16:13 +0200919 r = s390_enable_skey();
920 if (r)
921 goto out;
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400922
923 for (i = 0; i < args->count; i++) {
924 hva = gfn_to_hva(kvm, args->start_gfn + i);
925 if (kvm_is_error_hva(hva)) {
926 r = -EFAULT;
927 goto out;
928 }
929
930 /* Lowest order bit is reserved */
931 if (keys[i] & 0x01) {
932 r = -EINVAL;
933 goto out;
934 }
935
936 r = set_guest_storage_key(current->mm, hva,
937 (unsigned long)keys[i], 0);
938 if (r)
939 goto out;
940 }
941out:
942 kvfree(keys);
943 return r;
944}
945
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100946long kvm_arch_vm_ioctl(struct file *filp,
947 unsigned int ioctl, unsigned long arg)
948{
949 struct kvm *kvm = filp->private_data;
950 void __user *argp = (void __user *)arg;
Dominik Dingelf2061652014-04-09 13:13:00 +0200951 struct kvm_device_attr attr;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100952 int r;
953
954 switch (ioctl) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100955 case KVM_S390_INTERRUPT: {
956 struct kvm_s390_interrupt s390int;
957
958 r = -EFAULT;
959 if (copy_from_user(&s390int, argp, sizeof(s390int)))
960 break;
961 r = kvm_s390_inject_vm(kvm, &s390int);
962 break;
963 }
Cornelia Huckd938dc52013-10-23 18:26:34 +0200964 case KVM_ENABLE_CAP: {
965 struct kvm_enable_cap cap;
966 r = -EFAULT;
967 if (copy_from_user(&cap, argp, sizeof(cap)))
968 break;
969 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
970 break;
971 }
Cornelia Huck84223592013-07-15 13:36:01 +0200972 case KVM_CREATE_IRQCHIP: {
973 struct kvm_irq_routing_entry routing;
974
975 r = -EINVAL;
976 if (kvm->arch.use_irqchip) {
977 /* Set up dummy routing. */
978 memset(&routing, 0, sizeof(routing));
Nicholas Krause152b2832015-08-06 13:05:54 -0400979 r = kvm_set_irq_routing(kvm, &routing, 0, 0);
Cornelia Huck84223592013-07-15 13:36:01 +0200980 }
981 break;
982 }
Dominik Dingelf2061652014-04-09 13:13:00 +0200983 case KVM_SET_DEVICE_ATTR: {
984 r = -EFAULT;
985 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
986 break;
987 r = kvm_s390_vm_set_attr(kvm, &attr);
988 break;
989 }
990 case KVM_GET_DEVICE_ATTR: {
991 r = -EFAULT;
992 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
993 break;
994 r = kvm_s390_vm_get_attr(kvm, &attr);
995 break;
996 }
997 case KVM_HAS_DEVICE_ATTR: {
998 r = -EFAULT;
999 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1000 break;
1001 r = kvm_s390_vm_has_attr(kvm, &attr);
1002 break;
1003 }
Jason J. Herne30ee2a92014-09-23 09:23:01 -04001004 case KVM_S390_GET_SKEYS: {
1005 struct kvm_s390_skeys args;
1006
1007 r = -EFAULT;
1008 if (copy_from_user(&args, argp,
1009 sizeof(struct kvm_s390_skeys)))
1010 break;
1011 r = kvm_s390_get_skeys(kvm, &args);
1012 break;
1013 }
1014 case KVM_S390_SET_SKEYS: {
1015 struct kvm_s390_skeys args;
1016
1017 r = -EFAULT;
1018 if (copy_from_user(&args, argp,
1019 sizeof(struct kvm_s390_skeys)))
1020 break;
1021 r = kvm_s390_set_skeys(kvm, &args);
1022 break;
1023 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001024 default:
Avi Kivity367e1312009-08-26 14:57:07 +03001025 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001026 }
1027
1028 return r;
1029}
1030
Tony Krowiak45c9b472015-01-13 11:33:26 -05001031static int kvm_s390_query_ap_config(u8 *config)
1032{
1033 u32 fcn_code = 0x04000000UL;
Christian Borntraeger86044c82015-02-26 13:53:47 +01001034 u32 cc = 0;
Tony Krowiak45c9b472015-01-13 11:33:26 -05001035
Christian Borntraeger86044c82015-02-26 13:53:47 +01001036 memset(config, 0, 128);
Tony Krowiak45c9b472015-01-13 11:33:26 -05001037 asm volatile(
1038 "lgr 0,%1\n"
1039 "lgr 2,%2\n"
1040 ".long 0xb2af0000\n" /* PQAP(QCI) */
Christian Borntraeger86044c82015-02-26 13:53:47 +01001041 "0: ipm %0\n"
Tony Krowiak45c9b472015-01-13 11:33:26 -05001042 "srl %0,28\n"
Christian Borntraeger86044c82015-02-26 13:53:47 +01001043 "1:\n"
1044 EX_TABLE(0b, 1b)
1045 : "+r" (cc)
Tony Krowiak45c9b472015-01-13 11:33:26 -05001046 : "r" (fcn_code), "r" (config)
1047 : "cc", "0", "2", "memory"
1048 );
1049
1050 return cc;
1051}
1052
1053static int kvm_s390_apxa_installed(void)
1054{
1055 u8 config[128];
1056 int cc;
1057
Heiko Carstensa6aacc32015-11-24 14:28:12 +01001058 if (test_facility(12)) {
Tony Krowiak45c9b472015-01-13 11:33:26 -05001059 cc = kvm_s390_query_ap_config(config);
1060
1061 if (cc)
1062 pr_err("PQAP(QCI) failed with cc=%d", cc);
1063 else
1064 return config[0] & 0x40;
1065 }
1066
1067 return 0;
1068}
1069
1070static void kvm_s390_set_crycb_format(struct kvm *kvm)
1071{
1072 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
1073
1074 if (kvm_s390_apxa_installed())
1075 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
1076 else
1077 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
1078}
1079
Michael Mueller9d8d5782015-02-02 15:42:51 +01001080static void kvm_s390_get_cpu_id(struct cpuid *cpu_id)
1081{
1082 get_cpu_id(cpu_id);
1083 cpu_id->version = 0xff;
1084}
1085
Tony Krowiak5102ee82014-06-27 14:46:01 -04001086static int kvm_s390_crypto_init(struct kvm *kvm)
1087{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001088 if (!test_kvm_facility(kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -04001089 return 0;
1090
1091 kvm->arch.crypto.crycb = kzalloc(sizeof(*kvm->arch.crypto.crycb),
1092 GFP_KERNEL | GFP_DMA);
1093 if (!kvm->arch.crypto.crycb)
1094 return -ENOMEM;
1095
Tony Krowiak45c9b472015-01-13 11:33:26 -05001096 kvm_s390_set_crycb_format(kvm);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001097
Tony Krowiaked6f76b2015-02-24 14:06:57 -05001098 /* Enable AES/DEA protected key functions by default */
1099 kvm->arch.crypto.aes_kw = 1;
1100 kvm->arch.crypto.dea_kw = 1;
1101 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1102 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1103 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1104 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
Tony Krowiaka374e892014-09-03 10:13:53 +02001105
Tony Krowiak5102ee82014-06-27 14:46:01 -04001106 return 0;
1107}
1108
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001109static void sca_dispose(struct kvm *kvm)
1110{
1111 if (kvm->arch.use_esca)
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001112 free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001113 else
1114 free_page((unsigned long)(kvm->arch.sca));
1115 kvm->arch.sca = NULL;
1116}
1117
Carsten Ottee08b9632012-01-04 10:25:20 +01001118int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001119{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001120 int i, rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001121 char debug_name[16];
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001122 static unsigned long sca_offset;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001123
Carsten Ottee08b9632012-01-04 10:25:20 +01001124 rc = -EINVAL;
1125#ifdef CONFIG_KVM_S390_UCONTROL
1126 if (type & ~KVM_VM_S390_UCONTROL)
1127 goto out_err;
1128 if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1129 goto out_err;
1130#else
1131 if (type)
1132 goto out_err;
1133#endif
1134
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001135 rc = s390_enable_sie();
1136 if (rc)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001137 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001138
Carsten Otteb2904112011-10-18 12:27:13 +02001139 rc = -ENOMEM;
1140
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001141 kvm->arch.use_esca = 0; /* start with basic SCA */
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001142 rwlock_init(&kvm->arch.sca_lock);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +02001143 kvm->arch.sca = (struct bsca_block *) get_zeroed_page(GFP_KERNEL);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001144 if (!kvm->arch.sca)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001145 goto out_err;
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001146 spin_lock(&kvm_lock);
David Hildenbrandc5c2c392015-10-26 08:41:29 +01001147 sca_offset += 16;
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +02001148 if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
David Hildenbrandc5c2c392015-10-26 08:41:29 +01001149 sca_offset = 0;
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +02001150 kvm->arch.sca = (struct bsca_block *)
1151 ((char *) kvm->arch.sca + sca_offset);
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001152 spin_unlock(&kvm_lock);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001153
1154 sprintf(debug_name, "kvm-%u", current->pid);
1155
Christian Borntraeger1cb9cf72015-07-20 15:04:48 +02001156 kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001157 if (!kvm->arch.dbf)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001158 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001159
Michael Mueller9d8d5782015-02-02 15:42:51 +01001160 /*
1161 * The architectural maximum amount of facilities is 16 kbit. To store
1162 * this amount, 2 kbyte of memory is required. Thus we need a full
Michael Mueller981467c2015-02-24 13:51:04 +01001163 * page to hold the guest facility list (arch.model.fac->list) and the
1164 * facility mask (arch.model.fac->mask). Its address size has to be
Michael Mueller9d8d5782015-02-02 15:42:51 +01001165 * 31 bits and word aligned.
1166 */
1167 kvm->arch.model.fac =
Michael Mueller981467c2015-02-24 13:51:04 +01001168 (struct kvm_s390_fac *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001169 if (!kvm->arch.model.fac)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001170 goto out_err;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001171
Michael Muellerfb5bf932015-02-27 14:25:10 +01001172 /* Populate the facility mask initially. */
Michael Mueller981467c2015-02-24 13:51:04 +01001173 memcpy(kvm->arch.model.fac->mask, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +01001174 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001175 for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
1176 if (i < kvm_s390_fac_list_mask_size())
Michael Mueller981467c2015-02-24 13:51:04 +01001177 kvm->arch.model.fac->mask[i] &= kvm_s390_fac_list_mask[i];
Michael Mueller9d8d5782015-02-02 15:42:51 +01001178 else
Michael Mueller981467c2015-02-24 13:51:04 +01001179 kvm->arch.model.fac->mask[i] = 0UL;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001180 }
1181
Michael Mueller981467c2015-02-24 13:51:04 +01001182 /* Populate the facility list initially. */
1183 memcpy(kvm->arch.model.fac->list, kvm->arch.model.fac->mask,
1184 S390_ARCH_FAC_LIST_SIZE_BYTE);
1185
Michael Mueller9d8d5782015-02-02 15:42:51 +01001186 kvm_s390_get_cpu_id(&kvm->arch.model.cpu_id);
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001187 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001188
Tony Krowiak5102ee82014-06-27 14:46:01 -04001189 if (kvm_s390_crypto_init(kvm) < 0)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001190 goto out_err;
Tony Krowiak5102ee82014-06-27 14:46:01 -04001191
Carsten Otteba5c1e92008-03-25 18:47:26 +01001192 spin_lock_init(&kvm->arch.float_int.lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001193 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1194 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
Heiko Carstens8a2422342014-01-10 14:33:28 +01001195 init_waitqueue_head(&kvm->arch.ipte_wq);
Thomas Hutha6b7e452014-10-01 14:48:42 +02001196 mutex_init(&kvm->arch.ipte_mutex);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001197
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001198 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
Christian Borntraeger78f26132015-07-22 15:50:58 +02001199 VM_EVENT(kvm, 3, "vm created with type %lu", type);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001200
Carsten Ottee08b9632012-01-04 10:25:20 +01001201 if (type & KVM_VM_S390_UCONTROL) {
1202 kvm->arch.gmap = NULL;
Dominik Dingela3a92c32014-12-01 17:24:42 +01001203 kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
Carsten Ottee08b9632012-01-04 10:25:20 +01001204 } else {
Guenther Hutzl32e6b232014-12-01 17:24:42 +01001205 if (sclp.hamax == U64_MAX)
1206 kvm->arch.mem_limit = TASK_MAX_SIZE;
1207 else
1208 kvm->arch.mem_limit = min_t(unsigned long, TASK_MAX_SIZE,
1209 sclp.hamax + 1);
Dominik Dingela3a92c32014-12-01 17:24:42 +01001210 kvm->arch.gmap = gmap_alloc(current->mm, kvm->arch.mem_limit - 1);
Carsten Ottee08b9632012-01-04 10:25:20 +01001211 if (!kvm->arch.gmap)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001212 goto out_err;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001213 kvm->arch.gmap->private = kvm;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001214 kvm->arch.gmap->pfault_enabled = 0;
Carsten Ottee08b9632012-01-04 10:25:20 +01001215 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001216
1217 kvm->arch.css_support = 0;
Cornelia Huck84223592013-07-15 13:36:01 +02001218 kvm->arch.use_irqchip = 0;
Jason J. Herne72f25022014-11-25 09:46:02 -05001219 kvm->arch.epoch = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001220
David Hildenbrand8ad35752014-03-14 11:00:21 +01001221 spin_lock_init(&kvm->arch.start_stop_lock);
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001222 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
David Hildenbrand8ad35752014-03-14 11:00:21 +01001223
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001224 return 0;
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001225out_err:
Dominik Dingel40f5b732015-03-12 13:55:53 +01001226 kfree(kvm->arch.crypto.crycb);
1227 free_page((unsigned long)kvm->arch.model.fac);
1228 debug_unregister(kvm->arch.dbf);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001229 sca_dispose(kvm);
Christian Borntraeger78f26132015-07-22 15:50:58 +02001230 KVM_EVENT(3, "creation of vm failed: %d", rc);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001231 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001232}
1233
Christian Borntraegerd329c032008-11-26 14:50:27 +01001234void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1235{
1236 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
Cornelia Huckade38c32012-07-23 17:20:30 +02001237 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001238 kvm_s390_clear_local_irqs(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001239 kvm_clear_async_pf_completion_queue(vcpu);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +02001240 if (!kvm_is_ucontrol(vcpu->kvm))
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001241 sca_del_vcpu(vcpu);
Carsten Otte27e03932012-01-04 10:25:21 +01001242
1243 if (kvm_is_ucontrol(vcpu->kvm))
1244 gmap_free(vcpu->arch.gmap);
1245
Dominik Dingele6db1d62015-05-07 15:41:57 +02001246 if (vcpu->kvm->arch.use_cmma)
Dominik Dingelb31605c2014-03-25 13:47:11 +01001247 kvm_s390_vcpu_unsetup_cmma(vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001248 free_page((unsigned long)(vcpu->arch.sie_block));
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001249
Christian Borntraeger6692cef2008-11-26 14:51:08 +01001250 kvm_vcpu_uninit(vcpu);
Michael Muellerb110fea2013-06-12 13:54:54 +02001251 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001252}
1253
1254static void kvm_free_vcpus(struct kvm *kvm)
1255{
1256 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001257 struct kvm_vcpu *vcpu;
Christian Borntraegerd329c032008-11-26 14:50:27 +01001258
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001259 kvm_for_each_vcpu(i, vcpu, kvm)
1260 kvm_arch_vcpu_destroy(vcpu);
1261
1262 mutex_lock(&kvm->lock);
1263 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1264 kvm->vcpus[i] = NULL;
1265
1266 atomic_set(&kvm->online_vcpus, 0);
1267 mutex_unlock(&kvm->lock);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001268}
1269
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001270void kvm_arch_destroy_vm(struct kvm *kvm)
1271{
Christian Borntraegerd329c032008-11-26 14:50:27 +01001272 kvm_free_vcpus(kvm);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001273 free_page((unsigned long)kvm->arch.model.fac);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001274 sca_dispose(kvm);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001275 debug_unregister(kvm->arch.dbf);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001276 kfree(kvm->arch.crypto.crycb);
Carsten Otte27e03932012-01-04 10:25:21 +01001277 if (!kvm_is_ucontrol(kvm))
1278 gmap_free(kvm->arch.gmap);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001279 kvm_s390_destroy_adapters(kvm);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001280 kvm_s390_clear_float_irqs(kvm);
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001281 KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001282}
1283
1284/* Section: vcpu related */
Dominik Dingeldafd0322014-12-02 16:53:21 +01001285static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1286{
1287 vcpu->arch.gmap = gmap_alloc(current->mm, -1UL);
1288 if (!vcpu->arch.gmap)
1289 return -ENOMEM;
1290 vcpu->arch.gmap->private = vcpu->kvm;
1291
1292 return 0;
1293}
1294
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001295static void sca_del_vcpu(struct kvm_vcpu *vcpu)
1296{
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001297 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001298 if (vcpu->kvm->arch.use_esca) {
1299 struct esca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001300
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001301 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
David Hildenbrand10ce32d2015-10-12 12:41:41 +02001302 sca->cpu[vcpu->vcpu_id].sda = 0;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001303 } else {
1304 struct bsca_block *sca = vcpu->kvm->arch.sca;
1305
1306 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
David Hildenbrand10ce32d2015-10-12 12:41:41 +02001307 sca->cpu[vcpu->vcpu_id].sda = 0;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001308 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001309 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001310}
1311
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001312static void sca_add_vcpu(struct kvm_vcpu *vcpu)
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001313{
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001314 read_lock(&vcpu->kvm->arch.sca_lock);
1315 if (vcpu->kvm->arch.use_esca) {
1316 struct esca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001317
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001318 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001319 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1320 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
David Hildenbrand25508822015-10-12 16:27:23 +02001321 vcpu->arch.sie_block->ecb2 |= 0x04U;
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001322 set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001323 } else {
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001324 struct bsca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001325
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001326 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001327 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1328 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001329 set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001330 }
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001331 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001332}
1333
1334/* Basic SCA to Extended SCA data copy routines */
1335static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
1336{
1337 d->sda = s->sda;
1338 d->sigp_ctrl.c = s->sigp_ctrl.c;
1339 d->sigp_ctrl.scn = s->sigp_ctrl.scn;
1340}
1341
1342static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
1343{
1344 int i;
1345
1346 d->ipte_control = s->ipte_control;
1347 d->mcn[0] = s->mcn;
1348 for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
1349 sca_copy_entry(&d->cpu[i], &s->cpu[i]);
1350}
1351
1352static int sca_switch_to_extended(struct kvm *kvm)
1353{
1354 struct bsca_block *old_sca = kvm->arch.sca;
1355 struct esca_block *new_sca;
1356 struct kvm_vcpu *vcpu;
1357 unsigned int vcpu_idx;
1358 u32 scaol, scaoh;
1359
1360 new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
1361 if (!new_sca)
1362 return -ENOMEM;
1363
1364 scaoh = (u32)((u64)(new_sca) >> 32);
1365 scaol = (u32)(u64)(new_sca) & ~0x3fU;
1366
1367 kvm_s390_vcpu_block_all(kvm);
1368 write_lock(&kvm->arch.sca_lock);
1369
1370 sca_copy_b_to_e(new_sca, old_sca);
1371
1372 kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
1373 vcpu->arch.sie_block->scaoh = scaoh;
1374 vcpu->arch.sie_block->scaol = scaol;
1375 vcpu->arch.sie_block->ecb2 |= 0x04U;
1376 }
1377 kvm->arch.sca = new_sca;
1378 kvm->arch.use_esca = 1;
1379
1380 write_unlock(&kvm->arch.sca_lock);
1381 kvm_s390_vcpu_unblock_all(kvm);
1382
1383 free_page((unsigned long)old_sca);
1384
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001385 VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
1386 old_sca, kvm->arch.sca);
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001387 return 0;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001388}
1389
1390static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
1391{
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001392 int rc;
1393
1394 if (id < KVM_S390_BSCA_CPU_SLOTS)
1395 return true;
1396 if (!sclp.has_esca)
1397 return false;
1398
1399 mutex_lock(&kvm->lock);
1400 rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
1401 mutex_unlock(&kvm->lock);
1402
1403 return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001404}
1405
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001406int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1407{
Dominik Dingel3c038e62013-10-07 17:11:48 +02001408 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1409 kvm_clear_async_pf_completion_queue(vcpu);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001410 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
1411 KVM_SYNC_GPRS |
Christian Borntraeger9eed07352012-02-06 10:59:07 +01001412 KVM_SYNC_ACRS |
David Hildenbrandb028ee32014-07-17 10:47:43 +02001413 KVM_SYNC_CRS |
1414 KVM_SYNC_ARCH0 |
1415 KVM_SYNC_PFAULT;
Fan Zhangc6e5f162016-01-07 18:24:29 +08001416 if (test_kvm_facility(vcpu->kvm, 64))
1417 vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
Eric Farman68c55752014-06-09 10:57:26 -04001418 if (test_kvm_facility(vcpu->kvm, 129))
1419 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
Dominik Dingeldafd0322014-12-02 16:53:21 +01001420
1421 if (kvm_is_ucontrol(vcpu->kvm))
1422 return __kvm_ucontrol_vcpu_init(vcpu);
1423
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001424 return 0;
1425}
1426
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001427void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1428{
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001429 /* Save host register state */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02001430 save_fpu_regs();
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001431 vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
1432 vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
Hendrik Brueckner96b2d7a2015-06-12 13:53:51 +02001433
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001434 /* Depending on MACHINE_HAS_VX, data stored to vrs either
1435 * has vector register or floating point register format.
1436 */
1437 current->thread.fpu.regs = vcpu->run->s.regs.vrs;
1438 current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001439 if (test_fp_ctl(current->thread.fpu.fpc))
Hendrik Brueckner96b2d7a2015-06-12 13:53:51 +02001440 /* User space provided an invalid FPC, let's clear it */
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001441 current->thread.fpu.fpc = 0;
1442
1443 save_access_regs(vcpu->arch.host_acrs);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001444 restore_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001445 gmap_enable(vcpu->arch.gmap);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001446 atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001447}
1448
1449void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1450{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001451 atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001452 gmap_disable(vcpu->arch.gmap);
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001453
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001454 /* Save guest register state */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02001455 save_fpu_regs();
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001456 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001457
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001458 /* Restore host register state */
1459 current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
1460 current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001461
1462 save_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001463 restore_access_regs(vcpu->arch.host_acrs);
1464}
1465
1466static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1467{
1468 /* this equals initial cpu reset in pop, but we don't switch to ESA */
1469 vcpu->arch.sie_block->gpsw.mask = 0UL;
1470 vcpu->arch.sie_block->gpsw.addr = 0UL;
Christian Borntraeger8d26cf72012-01-11 11:19:32 +01001471 kvm_s390_set_prefix(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001472 vcpu->arch.sie_block->cputm = 0UL;
1473 vcpu->arch.sie_block->ckc = 0UL;
1474 vcpu->arch.sie_block->todpr = 0;
1475 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1476 vcpu->arch.sie_block->gcr[0] = 0xE0UL;
1477 vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001478 /* make sure the new fpc will be lazily loaded */
1479 save_fpu_regs();
1480 current->thread.fpu.fpc = 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001481 vcpu->arch.sie_block->gbea = 1;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001482 vcpu->arch.sie_block->pp = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001483 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1484 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001485 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
1486 kvm_s390_vcpu_stop(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001487 kvm_s390_clear_local_irqs(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001488}
1489
Dominik Dingel31928aa2014-12-04 15:47:07 +01001490void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001491{
Jason J. Herne72f25022014-11-25 09:46:02 -05001492 mutex_lock(&vcpu->kvm->lock);
Fan Zhangfdf03652015-05-13 10:58:41 +02001493 preempt_disable();
Jason J. Herne72f25022014-11-25 09:46:02 -05001494 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
Fan Zhangfdf03652015-05-13 10:58:41 +02001495 preempt_enable();
Jason J. Herne72f25022014-11-25 09:46:02 -05001496 mutex_unlock(&vcpu->kvm->lock);
David Hildenbrand25508822015-10-12 16:27:23 +02001497 if (!kvm_is_ucontrol(vcpu->kvm)) {
Dominik Dingeldafd0322014-12-02 16:53:21 +01001498 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001499 sca_add_vcpu(vcpu);
David Hildenbrand25508822015-10-12 16:27:23 +02001500 }
1501
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001502}
1503
Tony Krowiak5102ee82014-06-27 14:46:01 -04001504static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
1505{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001506 if (!test_kvm_facility(vcpu->kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -04001507 return;
1508
Tony Krowiaka374e892014-09-03 10:13:53 +02001509 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1510
1511 if (vcpu->kvm->arch.crypto.aes_kw)
1512 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1513 if (vcpu->kvm->arch.crypto.dea_kw)
1514 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1515
Tony Krowiak5102ee82014-06-27 14:46:01 -04001516 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
1517}
1518
Dominik Dingelb31605c2014-03-25 13:47:11 +01001519void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1520{
1521 free_page(vcpu->arch.sie_block->cbrlo);
1522 vcpu->arch.sie_block->cbrlo = 0;
1523}
1524
1525int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1526{
1527 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1528 if (!vcpu->arch.sie_block->cbrlo)
1529 return -ENOMEM;
1530
1531 vcpu->arch.sie_block->ecb2 |= 0x80;
1532 vcpu->arch.sie_block->ecb2 &= ~0x08;
1533 return 0;
1534}
1535
Michael Mueller91520f12015-02-27 14:32:11 +01001536static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
1537{
1538 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
1539
1540 vcpu->arch.cpu_id = model->cpu_id;
1541 vcpu->arch.sie_block->ibc = model->ibc;
1542 vcpu->arch.sie_block->fac = (int) (long) model->fac->list;
1543}
1544
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001545int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1546{
Dominik Dingelb31605c2014-03-25 13:47:11 +01001547 int rc = 0;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001548
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001549 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
1550 CPUSTAT_SM |
Guenther Hutzla4a4f192015-03-31 14:39:49 +02001551 CPUSTAT_STOPPED);
1552
Guenther Hutzl53df84f2015-02-18 11:13:03 +01001553 if (test_kvm_facility(vcpu->kvm, 78))
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001554 atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
Guenther Hutzl53df84f2015-02-18 11:13:03 +01001555 else if (test_kvm_facility(vcpu->kvm, 8))
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001556 atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
Guenther Hutzla4a4f192015-03-31 14:39:49 +02001557
Michael Mueller91520f12015-02-27 14:32:11 +01001558 kvm_s390_vcpu_setup_model(vcpu);
1559
Christian Borntraegerfc345312010-06-17 23:16:20 +02001560 vcpu->arch.sie_block->ecb = 6;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001561 if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73))
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001562 vcpu->arch.sie_block->ecb |= 0x10;
1563
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +02001564 vcpu->arch.sie_block->ecb2 = 8;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001565 vcpu->arch.sie_block->eca = 0xC1002000U;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001566 if (sclp.has_siif)
Heiko Carstens217a4402013-12-30 12:54:14 +01001567 vcpu->arch.sie_block->eca |= 1;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001568 if (sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001569 vcpu->arch.sie_block->eca |= 0x10000000U;
Fan Zhangc6e5f162016-01-07 18:24:29 +08001570 if (test_kvm_facility(vcpu->kvm, 64))
1571 vcpu->arch.sie_block->ecb3 |= 0x01;
Michael Mueller18280d82015-03-16 16:05:41 +01001572 if (test_kvm_facility(vcpu->kvm, 129)) {
Eric Farman13211ea2014-04-30 13:39:46 -04001573 vcpu->arch.sie_block->eca |= 0x00020000;
1574 vcpu->arch.sie_block->ecd |= 0x20000000;
1575 }
Fan Zhangc6e5f162016-01-07 18:24:29 +08001576 vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
Thomas Huth492d8642015-02-10 16:11:01 +01001577 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
Matthew Rosato5a5e6532013-01-29 11:48:20 -05001578
Dominik Dingele6db1d62015-05-07 15:41:57 +02001579 if (vcpu->kvm->arch.use_cmma) {
Dominik Dingelb31605c2014-03-25 13:47:11 +01001580 rc = kvm_s390_vcpu_setup_cmma(vcpu);
1581 if (rc)
1582 return rc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001583 }
David Hildenbrand0ac96caf2014-12-12 15:17:31 +01001584 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Christian Borntraegerca872302009-05-12 17:21:49 +02001585 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001586
Tony Krowiak5102ee82014-06-27 14:46:01 -04001587 kvm_s390_vcpu_crypto_setup(vcpu);
1588
Dominik Dingelb31605c2014-03-25 13:47:11 +01001589 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001590}
1591
1592struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1593 unsigned int id)
1594{
Carsten Otte4d475552011-10-18 12:27:12 +02001595 struct kvm_vcpu *vcpu;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001596 struct sie_page *sie_page;
Carsten Otte4d475552011-10-18 12:27:12 +02001597 int rc = -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001598
David Hildenbrand42158252015-10-12 12:57:22 +02001599 if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
Carsten Otte4d475552011-10-18 12:27:12 +02001600 goto out;
1601
1602 rc = -ENOMEM;
1603
Michael Muellerb110fea2013-06-12 13:54:54 +02001604 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001605 if (!vcpu)
Carsten Otte4d475552011-10-18 12:27:12 +02001606 goto out;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001607
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001608 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
1609 if (!sie_page)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001610 goto out_free_cpu;
1611
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001612 vcpu->arch.sie_block = &sie_page->sie_block;
1613 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
1614
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001615 vcpu->arch.sie_block->icpua = id;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001616 spin_lock_init(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001617 vcpu->arch.local_int.float_int = &kvm->arch.float_int;
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001618 vcpu->arch.local_int.wq = &vcpu->wq;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01001619 vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001620
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001621 rc = kvm_vcpu_init(vcpu, kvm, id);
1622 if (rc)
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001623 goto out_free_sie_block;
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001624 VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001625 vcpu->arch.sie_block);
Cornelia Huckade38c32012-07-23 17:20:30 +02001626 trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001627
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001628 return vcpu;
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001629out_free_sie_block:
1630 free_page((unsigned long)(vcpu->arch.sie_block));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001631out_free_cpu:
Michael Muellerb110fea2013-06-12 13:54:54 +02001632 kmem_cache_free(kvm_vcpu_cache, vcpu);
Carsten Otte4d475552011-10-18 12:27:12 +02001633out:
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001634 return ERR_PTR(rc);
1635}
1636
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001637int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1638{
David Hildenbrand9a022062014-08-05 17:40:47 +02001639 return kvm_s390_vcpu_has_irq(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001640}
1641
Christian Borntraeger27406cd2015-04-14 12:17:34 +02001642void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001643{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001644 atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
David Hildenbrand61a6df52015-05-12 08:41:40 +02001645 exit_sie(vcpu);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001646}
1647
Christian Borntraeger27406cd2015-04-14 12:17:34 +02001648void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001649{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001650 atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001651}
1652
Christian Borntraeger8e236542015-04-09 13:49:04 +02001653static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
1654{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001655 atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
David Hildenbrand61a6df52015-05-12 08:41:40 +02001656 exit_sie(vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001657}
1658
1659static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
1660{
Jason J. Herne9bf9fde2015-09-16 09:13:50 -04001661 atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001662}
1663
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001664/*
1665 * Kick a guest cpu out of SIE and wait until SIE is not running.
1666 * If the CPU is not running (e.g. waiting as idle) the function will
1667 * return immediately. */
1668void exit_sie(struct kvm_vcpu *vcpu)
1669{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001670 atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001671 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
1672 cpu_relax();
1673}
1674
Christian Borntraeger8e236542015-04-09 13:49:04 +02001675/* Kick a guest cpu out of SIE to process a request synchronously */
1676void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001677{
Christian Borntraeger8e236542015-04-09 13:49:04 +02001678 kvm_make_request(req, vcpu);
1679 kvm_s390_vcpu_request(vcpu);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001680}
1681
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001682static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
1683{
1684 int i;
1685 struct kvm *kvm = gmap->private;
1686 struct kvm_vcpu *vcpu;
1687
1688 kvm_for_each_vcpu(i, vcpu, kvm) {
1689 /* match against both prefix pages */
Michael Muellerfda902c2014-05-13 16:58:30 +02001690 if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001691 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001692 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001693 }
1694 }
1695}
1696
Christoffer Dallb6d33832012-03-08 16:44:24 -05001697int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1698{
1699 /* kvm common code refers to this, but never calls it */
1700 BUG();
1701 return 0;
1702}
1703
Carsten Otte14eebd92012-05-15 14:15:26 +02001704static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
1705 struct kvm_one_reg *reg)
1706{
1707 int r = -EINVAL;
1708
1709 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001710 case KVM_REG_S390_TODPR:
1711 r = put_user(vcpu->arch.sie_block->todpr,
1712 (u32 __user *)reg->addr);
1713 break;
1714 case KVM_REG_S390_EPOCHDIFF:
1715 r = put_user(vcpu->arch.sie_block->epoch,
1716 (u64 __user *)reg->addr);
1717 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001718 case KVM_REG_S390_CPU_TIMER:
1719 r = put_user(vcpu->arch.sie_block->cputm,
1720 (u64 __user *)reg->addr);
1721 break;
1722 case KVM_REG_S390_CLOCK_COMP:
1723 r = put_user(vcpu->arch.sie_block->ckc,
1724 (u64 __user *)reg->addr);
1725 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001726 case KVM_REG_S390_PFTOKEN:
1727 r = put_user(vcpu->arch.pfault_token,
1728 (u64 __user *)reg->addr);
1729 break;
1730 case KVM_REG_S390_PFCOMPARE:
1731 r = put_user(vcpu->arch.pfault_compare,
1732 (u64 __user *)reg->addr);
1733 break;
1734 case KVM_REG_S390_PFSELECT:
1735 r = put_user(vcpu->arch.pfault_select,
1736 (u64 __user *)reg->addr);
1737 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001738 case KVM_REG_S390_PP:
1739 r = put_user(vcpu->arch.sie_block->pp,
1740 (u64 __user *)reg->addr);
1741 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001742 case KVM_REG_S390_GBEA:
1743 r = put_user(vcpu->arch.sie_block->gbea,
1744 (u64 __user *)reg->addr);
1745 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001746 default:
1747 break;
1748 }
1749
1750 return r;
1751}
1752
1753static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
1754 struct kvm_one_reg *reg)
1755{
1756 int r = -EINVAL;
1757
1758 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001759 case KVM_REG_S390_TODPR:
1760 r = get_user(vcpu->arch.sie_block->todpr,
1761 (u32 __user *)reg->addr);
1762 break;
1763 case KVM_REG_S390_EPOCHDIFF:
1764 r = get_user(vcpu->arch.sie_block->epoch,
1765 (u64 __user *)reg->addr);
1766 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001767 case KVM_REG_S390_CPU_TIMER:
1768 r = get_user(vcpu->arch.sie_block->cputm,
1769 (u64 __user *)reg->addr);
1770 break;
1771 case KVM_REG_S390_CLOCK_COMP:
1772 r = get_user(vcpu->arch.sie_block->ckc,
1773 (u64 __user *)reg->addr);
1774 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001775 case KVM_REG_S390_PFTOKEN:
1776 r = get_user(vcpu->arch.pfault_token,
1777 (u64 __user *)reg->addr);
David Hildenbrand9fbd8082014-10-09 15:01:38 +02001778 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1779 kvm_clear_async_pf_completion_queue(vcpu);
Dominik Dingel536336c2013-09-30 10:55:33 +02001780 break;
1781 case KVM_REG_S390_PFCOMPARE:
1782 r = get_user(vcpu->arch.pfault_compare,
1783 (u64 __user *)reg->addr);
1784 break;
1785 case KVM_REG_S390_PFSELECT:
1786 r = get_user(vcpu->arch.pfault_select,
1787 (u64 __user *)reg->addr);
1788 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001789 case KVM_REG_S390_PP:
1790 r = get_user(vcpu->arch.sie_block->pp,
1791 (u64 __user *)reg->addr);
1792 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001793 case KVM_REG_S390_GBEA:
1794 r = get_user(vcpu->arch.sie_block->gbea,
1795 (u64 __user *)reg->addr);
1796 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001797 default:
1798 break;
1799 }
1800
1801 return r;
1802}
Christoffer Dallb6d33832012-03-08 16:44:24 -05001803
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001804static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
1805{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001806 kvm_s390_vcpu_initial_reset(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001807 return 0;
1808}
1809
1810int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1811{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001812 memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001813 return 0;
1814}
1815
1816int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1817{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001818 memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001819 return 0;
1820}
1821
1822int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1823 struct kvm_sregs *sregs)
1824{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001825 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001826 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
Christian Borntraeger59674c12012-01-11 11:20:33 +01001827 restore_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001828 return 0;
1829}
1830
1831int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1832 struct kvm_sregs *sregs)
1833{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001834 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001835 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001836 return 0;
1837}
1838
1839int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1840{
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001841 /* make sure the new values will be lazily loaded */
1842 save_fpu_regs();
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001843 if (test_fp_ctl(fpu->fpc))
1844 return -EINVAL;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001845 current->thread.fpu.fpc = fpu->fpc;
1846 if (MACHINE_HAS_VX)
1847 convert_fp_to_vx(current->thread.fpu.vxrs, (freg_t *)fpu->fprs);
1848 else
1849 memcpy(current->thread.fpu.fprs, &fpu->fprs, sizeof(fpu->fprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001850 return 0;
1851}
1852
1853int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1854{
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001855 /* make sure we have the latest values */
1856 save_fpu_regs();
1857 if (MACHINE_HAS_VX)
1858 convert_vx_to_fp((freg_t *)fpu->fprs, current->thread.fpu.vxrs);
1859 else
1860 memcpy(fpu->fprs, current->thread.fpu.fprs, sizeof(fpu->fprs));
1861 fpu->fpc = current->thread.fpu.fpc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001862 return 0;
1863}
1864
1865static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
1866{
1867 int rc = 0;
1868
David Hildenbrand7a42fdc2014-05-05 16:26:19 +02001869 if (!is_vcpu_stopped(vcpu))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001870 rc = -EBUSY;
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001871 else {
1872 vcpu->run->psw_mask = psw.mask;
1873 vcpu->run->psw_addr = psw.addr;
1874 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001875 return rc;
1876}
1877
1878int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1879 struct kvm_translation *tr)
1880{
1881 return -EINVAL; /* not implemented yet */
1882}
1883
David Hildenbrand27291e22014-01-23 12:26:52 +01001884#define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
1885 KVM_GUESTDBG_USE_HW_BP | \
1886 KVM_GUESTDBG_ENABLE)
1887
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001888int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1889 struct kvm_guest_debug *dbg)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001890{
David Hildenbrand27291e22014-01-23 12:26:52 +01001891 int rc = 0;
1892
1893 vcpu->guest_debug = 0;
1894 kvm_s390_clear_bp_data(vcpu);
1895
David Hildenbrand2de3bfc2014-05-20 17:25:20 +02001896 if (dbg->control & ~VALID_GUESTDBG_FLAGS)
David Hildenbrand27291e22014-01-23 12:26:52 +01001897 return -EINVAL;
1898
1899 if (dbg->control & KVM_GUESTDBG_ENABLE) {
1900 vcpu->guest_debug = dbg->control;
1901 /* enforce guest PER */
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001902 atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand27291e22014-01-23 12:26:52 +01001903
1904 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
1905 rc = kvm_s390_import_bp_data(vcpu, dbg);
1906 } else {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001907 atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand27291e22014-01-23 12:26:52 +01001908 vcpu->arch.guestdbg.last_bp = 0;
1909 }
1910
1911 if (rc) {
1912 vcpu->guest_debug = 0;
1913 kvm_s390_clear_bp_data(vcpu);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001914 atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand27291e22014-01-23 12:26:52 +01001915 }
1916
1917 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001918}
1919
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001920int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1921 struct kvm_mp_state *mp_state)
1922{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001923 /* CHECK_STOP and LOAD are not supported yet */
1924 return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
1925 KVM_MP_STATE_OPERATING;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001926}
1927
1928int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1929 struct kvm_mp_state *mp_state)
1930{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001931 int rc = 0;
1932
1933 /* user space knows about this interface - let it control the state */
1934 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
1935
1936 switch (mp_state->mp_state) {
1937 case KVM_MP_STATE_STOPPED:
1938 kvm_s390_vcpu_stop(vcpu);
1939 break;
1940 case KVM_MP_STATE_OPERATING:
1941 kvm_s390_vcpu_start(vcpu);
1942 break;
1943 case KVM_MP_STATE_LOAD:
1944 case KVM_MP_STATE_CHECK_STOP:
1945 /* fall through - CHECK_STOP and LOAD are not supported yet */
1946 default:
1947 rc = -ENXIO;
1948 }
1949
1950 return rc;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001951}
1952
David Hildenbrand8ad35752014-03-14 11:00:21 +01001953static bool ibs_enabled(struct kvm_vcpu *vcpu)
1954{
1955 return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
1956}
1957
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001958static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
1959{
David Hildenbrand8ad35752014-03-14 11:00:21 +01001960retry:
Christian Borntraeger8e236542015-04-09 13:49:04 +02001961 kvm_s390_vcpu_request_handled(vcpu);
Christian Borntraeger586b7cc2015-07-28 15:03:05 +02001962 if (!vcpu->requests)
1963 return 0;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001964 /*
1965 * We use MMU_RELOAD just to re-arm the ipte notifier for the
1966 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
1967 * This ensures that the ipte instruction for this request has
1968 * already finished. We might race against a second unmapper that
1969 * wants to set the blocking bit. Lets just retry the request loop.
1970 */
David Hildenbrand8ad35752014-03-14 11:00:21 +01001971 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001972 int rc;
1973 rc = gmap_ipte_notify(vcpu->arch.gmap,
Michael Muellerfda902c2014-05-13 16:58:30 +02001974 kvm_s390_get_prefix(vcpu),
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001975 PAGE_SIZE * 2);
1976 if (rc)
1977 return rc;
David Hildenbrand8ad35752014-03-14 11:00:21 +01001978 goto retry;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001979 }
David Hildenbrand8ad35752014-03-14 11:00:21 +01001980
David Hildenbrandd3d692c2014-07-29 08:53:36 +02001981 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
1982 vcpu->arch.sie_block->ihcpu = 0xffff;
1983 goto retry;
1984 }
1985
David Hildenbrand8ad35752014-03-14 11:00:21 +01001986 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
1987 if (!ibs_enabled(vcpu)) {
1988 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001989 atomic_or(CPUSTAT_IBS,
David Hildenbrand8ad35752014-03-14 11:00:21 +01001990 &vcpu->arch.sie_block->cpuflags);
1991 }
1992 goto retry;
1993 }
1994
1995 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
1996 if (ibs_enabled(vcpu)) {
1997 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001998 atomic_andnot(CPUSTAT_IBS,
David Hildenbrand8ad35752014-03-14 11:00:21 +01001999 &vcpu->arch.sie_block->cpuflags);
2000 }
2001 goto retry;
2002 }
2003
David Hildenbrand0759d062014-05-13 16:54:32 +02002004 /* nothing to do, just clear the request */
2005 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
2006
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002007 return 0;
2008}
2009
David Hildenbrand25ed1672015-05-12 09:49:14 +02002010void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
2011{
2012 struct kvm_vcpu *vcpu;
2013 int i;
2014
2015 mutex_lock(&kvm->lock);
2016 preempt_disable();
2017 kvm->arch.epoch = tod - get_tod_clock();
2018 kvm_s390_vcpu_block_all(kvm);
2019 kvm_for_each_vcpu(i, vcpu, kvm)
2020 vcpu->arch.sie_block->epoch = kvm->arch.epoch;
2021 kvm_s390_vcpu_unblock_all(kvm);
2022 preempt_enable();
2023 mutex_unlock(&kvm->lock);
2024}
2025
Thomas Huthfa576c52014-05-06 17:20:16 +02002026/**
2027 * kvm_arch_fault_in_page - fault-in guest page if necessary
2028 * @vcpu: The corresponding virtual cpu
2029 * @gpa: Guest physical address
2030 * @writable: Whether the page should be writable or not
2031 *
2032 * Make sure that a guest page has been faulted-in on the host.
2033 *
2034 * Return: Zero on success, negative error code otherwise.
2035 */
2036long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002037{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002038 return gmap_fault(vcpu->arch.gmap, gpa,
2039 writable ? FAULT_FLAG_WRITE : 0);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002040}
2041
Dominik Dingel3c038e62013-10-07 17:11:48 +02002042static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
2043 unsigned long token)
2044{
2045 struct kvm_s390_interrupt inti;
Jens Freimann383d0b02014-07-29 15:11:49 +02002046 struct kvm_s390_irq irq;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002047
2048 if (start_token) {
Jens Freimann383d0b02014-07-29 15:11:49 +02002049 irq.u.ext.ext_params2 = token;
2050 irq.type = KVM_S390_INT_PFAULT_INIT;
2051 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
Dominik Dingel3c038e62013-10-07 17:11:48 +02002052 } else {
2053 inti.type = KVM_S390_INT_PFAULT_DONE;
Jens Freimann383d0b02014-07-29 15:11:49 +02002054 inti.parm64 = token;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002055 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
2056 }
2057}
2058
2059void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
2060 struct kvm_async_pf *work)
2061{
2062 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
2063 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
2064}
2065
2066void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
2067 struct kvm_async_pf *work)
2068{
2069 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
2070 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
2071}
2072
2073void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
2074 struct kvm_async_pf *work)
2075{
2076 /* s390 will always inject the page directly */
2077}
2078
2079bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
2080{
2081 /*
2082 * s390 will always inject the page directly,
2083 * but we still want check_async_completion to cleanup
2084 */
2085 return true;
2086}
2087
2088static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
2089{
2090 hva_t hva;
2091 struct kvm_arch_async_pf arch;
2092 int rc;
2093
2094 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
2095 return 0;
2096 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
2097 vcpu->arch.pfault_compare)
2098 return 0;
2099 if (psw_extint_disabled(vcpu))
2100 return 0;
David Hildenbrand9a022062014-08-05 17:40:47 +02002101 if (kvm_s390_vcpu_has_irq(vcpu, 0))
Dominik Dingel3c038e62013-10-07 17:11:48 +02002102 return 0;
2103 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
2104 return 0;
2105 if (!vcpu->arch.gmap->pfault_enabled)
2106 return 0;
2107
Heiko Carstens81480cc2014-01-01 16:36:07 +01002108 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
2109 hva += current->thread.gmap_addr & ~PAGE_MASK;
2110 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
Dominik Dingel3c038e62013-10-07 17:11:48 +02002111 return 0;
2112
2113 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
2114 return rc;
2115}
2116
Thomas Huth3fb4c402013-09-12 10:33:43 +02002117static int vcpu_pre_run(struct kvm_vcpu *vcpu)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002118{
Thomas Huth3fb4c402013-09-12 10:33:43 +02002119 int rc, cpuflags;
Carsten Ottee168bf82012-01-04 10:25:22 +01002120
Dominik Dingel3c038e62013-10-07 17:11:48 +02002121 /*
2122 * On s390 notifications for arriving pages will be delivered directly
2123 * to the guest but the house keeping for completed pfaults is
2124 * handled outside the worker.
2125 */
2126 kvm_check_async_pf_completion(vcpu);
2127
Christian Borntraeger7ec7c8c2015-12-02 14:27:03 +01002128 vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
2129 vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002130
2131 if (need_resched())
2132 schedule();
2133
Martin Schwidefskyd3a73ac2014-04-15 12:55:07 +02002134 if (test_cpu_flag(CIF_MCCK_PENDING))
Christian Borntraeger71cde582008-05-21 13:37:34 +02002135 s390_handle_mcck();
2136
Jens Freimann79395032014-04-17 10:10:30 +02002137 if (!kvm_is_ucontrol(vcpu->kvm)) {
2138 rc = kvm_s390_deliver_pending_interrupts(vcpu);
2139 if (rc)
2140 return rc;
2141 }
Carsten Otte0ff31862008-05-21 13:37:37 +02002142
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002143 rc = kvm_s390_handle_requests(vcpu);
2144 if (rc)
2145 return rc;
2146
David Hildenbrand27291e22014-01-23 12:26:52 +01002147 if (guestdbg_enabled(vcpu)) {
2148 kvm_s390_backup_guest_per_regs(vcpu);
2149 kvm_s390_patch_guest_per_regs(vcpu);
2150 }
2151
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002152 vcpu->arch.sie_block->icptcode = 0;
Thomas Huth3fb4c402013-09-12 10:33:43 +02002153 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
2154 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
2155 trace_kvm_s390_sie_enter(vcpu, cpuflags);
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02002156
Thomas Huth3fb4c402013-09-12 10:33:43 +02002157 return 0;
2158}
2159
Thomas Huth492d8642015-02-10 16:11:01 +01002160static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
2161{
2162 psw_t *psw = &vcpu->arch.sie_block->gpsw;
2163 u8 opcode;
2164 int rc;
2165
2166 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
2167 trace_kvm_s390_sie_fault(vcpu);
2168
2169 /*
2170 * We want to inject an addressing exception, which is defined as a
2171 * suppressing or terminating exception. However, since we came here
2172 * by a DAT access exception, the PSW still points to the faulting
2173 * instruction since DAT exceptions are nullifying. So we've got
2174 * to look up the current opcode to get the length of the instruction
2175 * to be able to forward the PSW.
2176 */
Alexander Yarygin8ae04b82015-01-19 13:24:51 +03002177 rc = read_guest(vcpu, psw->addr, 0, &opcode, 1);
Thomas Huth492d8642015-02-10 16:11:01 +01002178 if (rc)
2179 return kvm_s390_inject_prog_cond(vcpu, rc);
2180 psw->addr = __rewind_psw(*psw, -insn_length(opcode));
2181
2182 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
2183}
2184
Thomas Huth3fb4c402013-09-12 10:33:43 +02002185static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
2186{
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02002187 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
2188 vcpu->arch.sie_block->icptcode);
2189 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
2190
David Hildenbrand27291e22014-01-23 12:26:52 +01002191 if (guestdbg_enabled(vcpu))
2192 kvm_s390_restore_guest_per_regs(vcpu);
2193
Christian Borntraeger7ec7c8c2015-12-02 14:27:03 +01002194 vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
2195 vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
David Hildenbrand71f116b2015-10-19 16:24:28 +02002196
2197 if (vcpu->arch.sie_block->icptcode > 0) {
2198 int rc = kvm_handle_sie_intercept(vcpu);
2199
2200 if (rc != -EOPNOTSUPP)
2201 return rc;
2202 vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
2203 vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
2204 vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
2205 vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
2206 return -EREMOTE;
2207 } else if (exit_reason != -EFAULT) {
2208 vcpu->stat.exit_null++;
2209 return 0;
Thomas Huth210b16072013-09-19 16:26:18 +02002210 } else if (kvm_is_ucontrol(vcpu->kvm)) {
2211 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
2212 vcpu->run->s390_ucontrol.trans_exc_code =
2213 current->thread.gmap_addr;
2214 vcpu->run->s390_ucontrol.pgm_code = 0x10;
David Hildenbrand71f116b2015-10-19 16:24:28 +02002215 return -EREMOTE;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002216 } else if (current->thread.gmap_pfault) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02002217 trace_kvm_s390_major_guest_pfault(vcpu);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002218 current->thread.gmap_pfault = 0;
David Hildenbrand71f116b2015-10-19 16:24:28 +02002219 if (kvm_arch_setup_async_pf(vcpu))
2220 return 0;
2221 return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002222 }
David Hildenbrand71f116b2015-10-19 16:24:28 +02002223 return vcpu_post_run_fault_in_sie(vcpu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002224}
2225
2226static int __vcpu_run(struct kvm_vcpu *vcpu)
2227{
2228 int rc, exit_reason;
2229
Thomas Huth800c1062013-09-12 10:33:45 +02002230 /*
2231 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2232 * ning the guest), so that memslots (and other stuff) are protected
2233 */
2234 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2235
Thomas Hutha76ccff2013-09-12 10:33:44 +02002236 do {
2237 rc = vcpu_pre_run(vcpu);
2238 if (rc)
2239 break;
Thomas Huth3fb4c402013-09-12 10:33:43 +02002240
Thomas Huth800c1062013-09-12 10:33:45 +02002241 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Thomas Hutha76ccff2013-09-12 10:33:44 +02002242 /*
2243 * As PF_VCPU will be used in fault handler, between
2244 * guest_enter and guest_exit should be no uaccess.
2245 */
Christian Borntraeger0097d122015-04-30 13:43:30 +02002246 local_irq_disable();
2247 __kvm_guest_enter();
2248 local_irq_enable();
Thomas Hutha76ccff2013-09-12 10:33:44 +02002249 exit_reason = sie64a(vcpu->arch.sie_block,
2250 vcpu->run->s.regs.gprs);
Christian Borntraeger0097d122015-04-30 13:43:30 +02002251 local_irq_disable();
2252 __kvm_guest_exit();
2253 local_irq_enable();
Thomas Huth800c1062013-09-12 10:33:45 +02002254 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002255
Thomas Hutha76ccff2013-09-12 10:33:44 +02002256 rc = vcpu_post_run(vcpu, exit_reason);
David Hildenbrand27291e22014-01-23 12:26:52 +01002257 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002258
Thomas Huth800c1062013-09-12 10:33:45 +02002259 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Carsten Ottee168bf82012-01-04 10:25:22 +01002260 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002261}
2262
David Hildenbrandb028ee32014-07-17 10:47:43 +02002263static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2264{
2265 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2266 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2267 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2268 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2269 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2270 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002271 /* some control register changes require a tlb flush */
2272 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002273 }
2274 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
2275 vcpu->arch.sie_block->cputm = kvm_run->s.regs.cputm;
2276 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2277 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2278 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2279 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2280 }
2281 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2282 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2283 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2284 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
David Hildenbrand9fbd8082014-10-09 15:01:38 +02002285 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
2286 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002287 }
2288 kvm_run->kvm_dirty_regs = 0;
2289}
2290
2291static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2292{
2293 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
2294 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
2295 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
2296 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
2297 kvm_run->s.regs.cputm = vcpu->arch.sie_block->cputm;
2298 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
2299 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
2300 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
2301 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
2302 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
2303 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
2304 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
2305}
2306
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002307int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2308{
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002309 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002310 sigset_t sigsaved;
2311
David Hildenbrand27291e22014-01-23 12:26:52 +01002312 if (guestdbg_exit_pending(vcpu)) {
2313 kvm_s390_prepare_debug_exit(vcpu);
2314 return 0;
2315 }
2316
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002317 if (vcpu->sigset_active)
2318 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2319
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002320 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
2321 kvm_s390_vcpu_start(vcpu);
2322 } else if (is_vcpu_stopped(vcpu)) {
David Hildenbrandea2cdd22015-05-20 13:24:02 +02002323 pr_err_ratelimited("can't run stopped vcpu %d\n",
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002324 vcpu->vcpu_id);
2325 return -EINVAL;
2326 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002327
David Hildenbrandb028ee32014-07-17 10:47:43 +02002328 sync_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002329
Heiko Carstensdab4079d2009-06-12 10:26:32 +02002330 might_fault();
Thomas Hutha76ccff2013-09-12 10:33:44 +02002331 rc = __vcpu_run(vcpu);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +02002332
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002333 if (signal_pending(current) && !rc) {
2334 kvm_run->exit_reason = KVM_EXIT_INTR;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002335 rc = -EINTR;
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002336 }
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002337
David Hildenbrand27291e22014-01-23 12:26:52 +01002338 if (guestdbg_exit_pending(vcpu) && !rc) {
2339 kvm_s390_prepare_debug_exit(vcpu);
2340 rc = 0;
2341 }
2342
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002343 if (rc == -EREMOTE) {
David Hildenbrand71f116b2015-10-19 16:24:28 +02002344 /* userspace support is needed, kvm_run has been prepared */
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002345 rc = 0;
2346 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002347
David Hildenbrandb028ee32014-07-17 10:47:43 +02002348 store_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002349
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002350 if (vcpu->sigset_active)
2351 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2352
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002353 vcpu->stat.exit_userspace++;
Heiko Carstens7e8e6ab2008-04-04 15:12:35 +02002354 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002355}
2356
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002357/*
2358 * store status at address
2359 * we use have two special cases:
2360 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2361 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2362 */
Heiko Carstensd0bce602014-01-01 16:45:58 +01002363int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002364{
Carsten Otte092670c2011-07-24 10:48:22 +02002365 unsigned char archmode = 1;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002366 freg_t fprs[NUM_FPRS];
Michael Muellerfda902c2014-05-13 16:58:30 +02002367 unsigned int px;
Thomas Huth178bd782013-11-13 20:28:18 +01002368 u64 clkcomp;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002369 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002370
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002371 px = kvm_s390_get_prefix(vcpu);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002372 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
2373 if (write_guest_abs(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002374 return -EFAULT;
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002375 gpa = 0;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002376 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
2377 if (write_guest_real(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002378 return -EFAULT;
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002379 gpa = px;
2380 } else
2381 gpa -= __LC_FPREGS_SAVE_AREA;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002382
2383 /* manually convert vector registers if necessary */
2384 if (MACHINE_HAS_VX) {
2385 convert_vx_to_fp(fprs, current->thread.fpu.vxrs);
2386 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
2387 fprs, 128);
2388 } else {
2389 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
2390 vcpu->run->s.regs.vrs, 128);
2391 }
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002392 rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002393 vcpu->run->s.regs.gprs, 128);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002394 rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002395 &vcpu->arch.sie_block->gpsw, 16);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002396 rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
Michael Muellerfda902c2014-05-13 16:58:30 +02002397 &px, 4);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002398 rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002399 &vcpu->run->s.regs.fpc, 4);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002400 rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002401 &vcpu->arch.sie_block->todpr, 4);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002402 rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002403 &vcpu->arch.sie_block->cputm, 8);
Thomas Huth178bd782013-11-13 20:28:18 +01002404 clkcomp = vcpu->arch.sie_block->ckc >> 8;
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002405 rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002406 &clkcomp, 8);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002407 rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002408 &vcpu->run->s.regs.acrs, 64);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002409 rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002410 &vcpu->arch.sie_block->gcr, 128);
2411 return rc ? -EFAULT : 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002412}
2413
Thomas Huthe8798922013-11-06 15:46:33 +01002414int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2415{
2416 /*
2417 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
2418 * copying in vcpu load/put. Lets update our copies before we save
2419 * it into the save area
2420 */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02002421 save_fpu_regs();
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002422 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
Thomas Huthe8798922013-11-06 15:46:33 +01002423 save_access_regs(vcpu->run->s.regs.acrs);
2424
2425 return kvm_s390_store_status_unloaded(vcpu, addr);
2426}
2427
Eric Farmanbc17de72014-04-14 16:01:09 -04002428/*
2429 * store additional status at address
2430 */
2431int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
2432 unsigned long gpa)
2433{
2434 /* Only bits 0-53 are used for address formation */
2435 if (!(gpa & ~0x3ff))
2436 return 0;
2437
2438 return write_guest_abs(vcpu, gpa & ~0x3ff,
2439 (void *)&vcpu->run->s.regs.vrs, 512);
2440}
2441
2442int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr)
2443{
2444 if (!test_kvm_facility(vcpu->kvm, 129))
2445 return 0;
2446
2447 /*
2448 * The guest VXRS are in the host VXRs due to the lazy
Hendrik Brueckner9977e882015-06-10 12:53:42 +02002449 * copying in vcpu load/put. We can simply call save_fpu_regs()
2450 * to save the current register state because we are in the
2451 * middle of a load/put cycle.
2452 *
2453 * Let's update our copies before we save it into the save area.
Eric Farmanbc17de72014-04-14 16:01:09 -04002454 */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02002455 save_fpu_regs();
Eric Farmanbc17de72014-04-14 16:01:09 -04002456
2457 return kvm_s390_store_adtl_status_unloaded(vcpu, addr);
2458}
2459
David Hildenbrand8ad35752014-03-14 11:00:21 +01002460static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2461{
2462 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02002463 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002464}
2465
2466static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
2467{
2468 unsigned int i;
2469 struct kvm_vcpu *vcpu;
2470
2471 kvm_for_each_vcpu(i, vcpu, kvm) {
2472 __disable_ibs_on_vcpu(vcpu);
2473 }
2474}
2475
2476static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2477{
2478 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02002479 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002480}
2481
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002482void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
2483{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002484 int i, online_vcpus, started_vcpus = 0;
2485
2486 if (!is_vcpu_stopped(vcpu))
2487 return;
2488
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002489 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002490 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002491 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002492 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2493
2494 for (i = 0; i < online_vcpus; i++) {
2495 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
2496 started_vcpus++;
2497 }
2498
2499 if (started_vcpus == 0) {
2500 /* we're the only active VCPU -> speed it up */
2501 __enable_ibs_on_vcpu(vcpu);
2502 } else if (started_vcpus == 1) {
2503 /*
2504 * As we are starting a second VCPU, we have to disable
2505 * the IBS facility on all VCPUs to remove potentially
2506 * oustanding ENABLE requests.
2507 */
2508 __disable_ibs_on_all_vcpus(vcpu->kvm);
2509 }
2510
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002511 atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002512 /*
2513 * Another VCPU might have used IBS while we were offline.
2514 * Let's play safe and flush the VCPU at startup.
2515 */
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002516 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002517 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002518 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002519}
2520
2521void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
2522{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002523 int i, online_vcpus, started_vcpus = 0;
2524 struct kvm_vcpu *started_vcpu = NULL;
2525
2526 if (is_vcpu_stopped(vcpu))
2527 return;
2528
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002529 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002530 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002531 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002532 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2533
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002534 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
David Hildenbrand6cddd432014-10-15 16:48:53 +02002535 kvm_s390_clear_stop_irq(vcpu);
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002536
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002537 atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002538 __disable_ibs_on_vcpu(vcpu);
2539
2540 for (i = 0; i < online_vcpus; i++) {
2541 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
2542 started_vcpus++;
2543 started_vcpu = vcpu->kvm->vcpus[i];
2544 }
2545 }
2546
2547 if (started_vcpus == 1) {
2548 /*
2549 * As we only have one VCPU left, we want to enable the
2550 * IBS facility for that VCPU to speed it up.
2551 */
2552 __enable_ibs_on_vcpu(started_vcpu);
2553 }
2554
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002555 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002556 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002557}
2558
Cornelia Huckd6712df2012-12-20 15:32:11 +01002559static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2560 struct kvm_enable_cap *cap)
2561{
2562 int r;
2563
2564 if (cap->flags)
2565 return -EINVAL;
2566
2567 switch (cap->cap) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002568 case KVM_CAP_S390_CSS_SUPPORT:
2569 if (!vcpu->kvm->arch.css_support) {
2570 vcpu->kvm->arch.css_support = 1;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +02002571 VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002572 trace_kvm_s390_enable_css(vcpu->kvm);
2573 }
2574 r = 0;
2575 break;
Cornelia Huckd6712df2012-12-20 15:32:11 +01002576 default:
2577 r = -EINVAL;
2578 break;
2579 }
2580 return r;
2581}
2582
Thomas Huth41408c282015-02-06 15:01:21 +01002583static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
2584 struct kvm_s390_mem_op *mop)
2585{
2586 void __user *uaddr = (void __user *)mop->buf;
2587 void *tmpbuf = NULL;
2588 int r, srcu_idx;
2589 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
2590 | KVM_S390_MEMOP_F_CHECK_ONLY;
2591
2592 if (mop->flags & ~supported_flags)
2593 return -EINVAL;
2594
2595 if (mop->size > MEM_OP_MAX_SIZE)
2596 return -E2BIG;
2597
2598 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
2599 tmpbuf = vmalloc(mop->size);
2600 if (!tmpbuf)
2601 return -ENOMEM;
2602 }
2603
2604 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2605
2606 switch (mop->op) {
2607 case KVM_S390_MEMOP_LOGICAL_READ:
2608 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
2609 r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, false);
2610 break;
2611 }
2612 r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2613 if (r == 0) {
2614 if (copy_to_user(uaddr, tmpbuf, mop->size))
2615 r = -EFAULT;
2616 }
2617 break;
2618 case KVM_S390_MEMOP_LOGICAL_WRITE:
2619 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
2620 r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, true);
2621 break;
2622 }
2623 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
2624 r = -EFAULT;
2625 break;
2626 }
2627 r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2628 break;
2629 default:
2630 r = -EINVAL;
2631 }
2632
2633 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2634
2635 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
2636 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
2637
2638 vfree(tmpbuf);
2639 return r;
2640}
2641
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002642long kvm_arch_vcpu_ioctl(struct file *filp,
2643 unsigned int ioctl, unsigned long arg)
2644{
2645 struct kvm_vcpu *vcpu = filp->private_data;
2646 void __user *argp = (void __user *)arg;
Thomas Huth800c1062013-09-12 10:33:45 +02002647 int idx;
Avi Kivitybc923cc2010-05-13 12:21:46 +03002648 long r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002649
Avi Kivity93736622010-05-13 12:35:17 +03002650 switch (ioctl) {
Jens Freimann47b43c52014-11-11 20:57:06 +01002651 case KVM_S390_IRQ: {
2652 struct kvm_s390_irq s390irq;
2653
2654 r = -EFAULT;
2655 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
2656 break;
2657 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
2658 break;
2659 }
Avi Kivity93736622010-05-13 12:35:17 +03002660 case KVM_S390_INTERRUPT: {
Carsten Otteba5c1e92008-03-25 18:47:26 +01002661 struct kvm_s390_interrupt s390int;
Jens Freimann383d0b02014-07-29 15:11:49 +02002662 struct kvm_s390_irq s390irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002663
Avi Kivity93736622010-05-13 12:35:17 +03002664 r = -EFAULT;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002665 if (copy_from_user(&s390int, argp, sizeof(s390int)))
Avi Kivity93736622010-05-13 12:35:17 +03002666 break;
Jens Freimann383d0b02014-07-29 15:11:49 +02002667 if (s390int_to_s390irq(&s390int, &s390irq))
2668 return -EINVAL;
2669 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
Avi Kivity93736622010-05-13 12:35:17 +03002670 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002671 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002672 case KVM_S390_STORE_STATUS:
Thomas Huth800c1062013-09-12 10:33:45 +02002673 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002674 r = kvm_s390_vcpu_store_status(vcpu, arg);
Thomas Huth800c1062013-09-12 10:33:45 +02002675 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002676 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002677 case KVM_S390_SET_INITIAL_PSW: {
2678 psw_t psw;
2679
Avi Kivitybc923cc2010-05-13 12:21:46 +03002680 r = -EFAULT;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002681 if (copy_from_user(&psw, argp, sizeof(psw)))
Avi Kivitybc923cc2010-05-13 12:21:46 +03002682 break;
2683 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
2684 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002685 }
2686 case KVM_S390_INITIAL_RESET:
Avi Kivitybc923cc2010-05-13 12:21:46 +03002687 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
2688 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02002689 case KVM_SET_ONE_REG:
2690 case KVM_GET_ONE_REG: {
2691 struct kvm_one_reg reg;
2692 r = -EFAULT;
2693 if (copy_from_user(&reg, argp, sizeof(reg)))
2694 break;
2695 if (ioctl == KVM_SET_ONE_REG)
2696 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
2697 else
2698 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
2699 break;
2700 }
Carsten Otte27e03932012-01-04 10:25:21 +01002701#ifdef CONFIG_KVM_S390_UCONTROL
2702 case KVM_S390_UCAS_MAP: {
2703 struct kvm_s390_ucas_mapping ucasmap;
2704
2705 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2706 r = -EFAULT;
2707 break;
2708 }
2709
2710 if (!kvm_is_ucontrol(vcpu->kvm)) {
2711 r = -EINVAL;
2712 break;
2713 }
2714
2715 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
2716 ucasmap.vcpu_addr, ucasmap.length);
2717 break;
2718 }
2719 case KVM_S390_UCAS_UNMAP: {
2720 struct kvm_s390_ucas_mapping ucasmap;
2721
2722 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2723 r = -EFAULT;
2724 break;
2725 }
2726
2727 if (!kvm_is_ucontrol(vcpu->kvm)) {
2728 r = -EINVAL;
2729 break;
2730 }
2731
2732 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
2733 ucasmap.length);
2734 break;
2735 }
2736#endif
Carsten Otteccc79102012-01-04 10:25:26 +01002737 case KVM_S390_VCPU_FAULT: {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002738 r = gmap_fault(vcpu->arch.gmap, arg, 0);
Carsten Otteccc79102012-01-04 10:25:26 +01002739 break;
2740 }
Cornelia Huckd6712df2012-12-20 15:32:11 +01002741 case KVM_ENABLE_CAP:
2742 {
2743 struct kvm_enable_cap cap;
2744 r = -EFAULT;
2745 if (copy_from_user(&cap, argp, sizeof(cap)))
2746 break;
2747 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2748 break;
2749 }
Thomas Huth41408c282015-02-06 15:01:21 +01002750 case KVM_S390_MEM_OP: {
2751 struct kvm_s390_mem_op mem_op;
2752
2753 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
2754 r = kvm_s390_guest_mem_op(vcpu, &mem_op);
2755 else
2756 r = -EFAULT;
2757 break;
2758 }
Jens Freimann816c7662014-11-24 17:13:46 +01002759 case KVM_S390_SET_IRQ_STATE: {
2760 struct kvm_s390_irq_state irq_state;
2761
2762 r = -EFAULT;
2763 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2764 break;
2765 if (irq_state.len > VCPU_IRQS_MAX_BUF ||
2766 irq_state.len == 0 ||
2767 irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
2768 r = -EINVAL;
2769 break;
2770 }
2771 r = kvm_s390_set_irq_state(vcpu,
2772 (void __user *) irq_state.buf,
2773 irq_state.len);
2774 break;
2775 }
2776 case KVM_S390_GET_IRQ_STATE: {
2777 struct kvm_s390_irq_state irq_state;
2778
2779 r = -EFAULT;
2780 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2781 break;
2782 if (irq_state.len == 0) {
2783 r = -EINVAL;
2784 break;
2785 }
2786 r = kvm_s390_get_irq_state(vcpu,
2787 (__u8 __user *) irq_state.buf,
2788 irq_state.len);
2789 break;
2790 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002791 default:
Carsten Otte3e6afcf2012-01-04 10:25:30 +01002792 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002793 }
Avi Kivitybc923cc2010-05-13 12:21:46 +03002794 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002795}
2796
Carsten Otte5b1c1492012-01-04 10:25:23 +01002797int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2798{
2799#ifdef CONFIG_KVM_S390_UCONTROL
2800 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
2801 && (kvm_is_ucontrol(vcpu->kvm))) {
2802 vmf->page = virt_to_page(vcpu->arch.sie_block);
2803 get_page(vmf->page);
2804 return 0;
2805 }
2806#endif
2807 return VM_FAULT_SIGBUS;
2808}
2809
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05302810int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2811 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09002812{
2813 return 0;
2814}
2815
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002816/* Section: memory related */
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002817int kvm_arch_prepare_memory_region(struct kvm *kvm,
2818 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002819 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09002820 enum kvm_mr_change change)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002821{
Nick Wangdd2887e2013-03-25 17:22:57 +01002822 /* A few sanity checks. We can have memory slots which have to be
2823 located/ended at a segment boundary (1MB). The memory in userland is
2824 ok to be fragmented into various different vmas. It is okay to mmap()
2825 and munmap() stuff in this slot after doing this call at any time */
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002826
Carsten Otte598841c2011-07-24 10:48:21 +02002827 if (mem->userspace_addr & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002828 return -EINVAL;
2829
Carsten Otte598841c2011-07-24 10:48:21 +02002830 if (mem->memory_size & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002831 return -EINVAL;
2832
Dominik Dingela3a92c32014-12-01 17:24:42 +01002833 if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
2834 return -EINVAL;
2835
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002836 return 0;
2837}
2838
2839void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002840 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002841 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02002842 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002843 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002844{
Carsten Ottef7850c92011-07-24 10:48:23 +02002845 int rc;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002846
Christian Borntraeger2cef4de2013-03-25 17:22:48 +01002847 /* If the basics of the memslot do not change, we do not want
2848 * to update the gmap. Every update causes several unnecessary
2849 * segment translation exceptions. This is usually handled just
2850 * fine by the normal fault handler + gmap, but it will also
2851 * cause faults on the prefix page of running guest CPUs.
2852 */
2853 if (old->userspace_addr == mem->userspace_addr &&
2854 old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
2855 old->npages * PAGE_SIZE == mem->memory_size)
2856 return;
Carsten Otte598841c2011-07-24 10:48:21 +02002857
2858 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
2859 mem->guest_phys_addr, mem->memory_size);
2860 if (rc)
David Hildenbrandea2cdd22015-05-20 13:24:02 +02002861 pr_warn("failed to commit memory region\n");
Carsten Otte598841c2011-07-24 10:48:21 +02002862 return;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002863}
2864
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002865static int __init kvm_s390_init(void)
2866{
David Hildenbrand07197fd2015-01-30 16:01:38 +01002867 if (!sclp.has_sief2) {
2868 pr_info("SIE not available\n");
2869 return -ENODEV;
2870 }
2871
Michael Mueller9d8d5782015-02-02 15:42:51 +01002872 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002873}
2874
2875static void __exit kvm_s390_exit(void)
2876{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002877 kvm_exit();
2878}
2879
2880module_init(kvm_s390_init);
2881module_exit(kvm_s390_exit);
Cornelia Huck566af942013-05-27 18:42:33 +02002882
2883/*
2884 * Enable autoloading of the kvm module.
2885 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
2886 * since x86 takes a different approach.
2887 */
2888#include <linux/miscdevice.h>
2889MODULE_ALIAS_MISCDEV(KVM_MINOR);
2890MODULE_ALIAS("devname:kvm");