blob: 0d2f24210198078c4276a5e8b0f030fb97c600b6 [file] [log] [blame]
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * hosting zSeries kernel virtual machines
Heiko Carstensb0c632d2008-03-25 18:47:20 +01003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2008, 2009
Heiko Carstensb0c632d2008-03-25 18:47:20 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 * Heiko Carstens <heiko.carstens@de.ibm.com>
Christian Ehrhardt628eb9b2009-05-25 13:40:51 +020013 * Christian Ehrhardt <ehrhardt@de.ibm.com>
Jason J. Herne15f36eb2012-08-02 10:10:17 -040014 * Jason J. Herne <jjherne@us.ibm.com>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010015 */
16
17#include <linux/compiler.h>
18#include <linux/err.h>
19#include <linux/fs.h>
Christian Borntraegerca872302009-05-12 17:21:49 +020020#include <linux/hrtimer.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010021#include <linux/init.h>
22#include <linux/kvm.h>
23#include <linux/kvm_host.h>
24#include <linux/module.h>
Tony Krowiaka374e892014-09-03 10:13:53 +020025#include <linux/random.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010026#include <linux/slab.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010027#include <linux/timer.h>
Thomas Huth41408c22015-02-06 15:01:21 +010028#include <linux/vmalloc.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010029#include <asm/asm-offsets.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010030#include <asm/lowcore.h>
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 Huth41408c22015-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 Huth41408c22015-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;
David Hildenbranddb0758b2016-02-15 09:42:25 +0100162 if (vcpu->arch.cputm_enabled)
163 vcpu->arch.cputm_start += *delta;
Fan Zhangfdf03652015-05-13 10:58:41 +0200164 }
165 }
166 return NOTIFY_OK;
167}
168
169static struct notifier_block kvm_clock_notifier = {
170 .notifier_call = kvm_clock_sync,
171};
172
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100173int kvm_arch_hardware_setup(void)
174{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200175 gmap_notifier.notifier_call = kvm_gmap_notifier;
176 gmap_register_ipte_notifier(&gmap_notifier);
Fan Zhangfdf03652015-05-13 10:58:41 +0200177 atomic_notifier_chain_register(&s390_epoch_delta_notifier,
178 &kvm_clock_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100179 return 0;
180}
181
182void kvm_arch_hardware_unsetup(void)
183{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200184 gmap_unregister_ipte_notifier(&gmap_notifier);
Fan Zhangfdf03652015-05-13 10:58:41 +0200185 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
186 &kvm_clock_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100187}
188
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100189int kvm_arch_init(void *opaque)
190{
Christian Borntraeger78f26132015-07-22 15:50:58 +0200191 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
192 if (!kvm_s390_dbf)
193 return -ENOMEM;
194
195 if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
196 debug_unregister(kvm_s390_dbf);
197 return -ENOMEM;
198 }
199
Cornelia Huck84877d92014-09-02 10:27:35 +0100200 /* Register floating interrupt controller interface. */
201 return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100202}
203
Christian Borntraeger78f26132015-07-22 15:50:58 +0200204void kvm_arch_exit(void)
205{
206 debug_unregister(kvm_s390_dbf);
207}
208
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100209/* Section: device related */
210long kvm_arch_dev_ioctl(struct file *filp,
211 unsigned int ioctl, unsigned long arg)
212{
213 if (ioctl == KVM_S390_ENABLE_SIE)
214 return s390_enable_sie();
215 return -EINVAL;
216}
217
Alexander Graf784aa3d2014-07-14 18:27:35 +0200218int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100219{
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100220 int r;
221
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200222 switch (ext) {
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100223 case KVM_CAP_S390_PSW:
Christian Borntraegerb6cf8782011-09-20 17:07:29 +0200224 case KVM_CAP_S390_GMAP:
Christian Borntraeger52e16b12011-11-17 11:00:44 +0100225 case KVM_CAP_SYNC_MMU:
Carsten Otte1efd0f52012-01-04 10:25:29 +0100226#ifdef CONFIG_KVM_S390_UCONTROL
227 case KVM_CAP_S390_UCONTROL:
228#endif
Dominik Dingel3c038e62013-10-07 17:11:48 +0200229 case KVM_CAP_ASYNC_PF:
Christian Borntraeger60b413c2012-01-11 11:20:31 +0100230 case KVM_CAP_SYNC_REGS:
Carsten Otte14eebd92012-05-15 14:15:26 +0200231 case KVM_CAP_ONE_REG:
Cornelia Huckd6712df2012-12-20 15:32:11 +0100232 case KVM_CAP_ENABLE_CAP:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100233 case KVM_CAP_S390_CSS_SUPPORT:
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100234 case KVM_CAP_IOEVENTFD:
Jens Freimannc05c4182013-10-07 16:13:45 +0200235 case KVM_CAP_DEVICE_CTRL:
Cornelia Huckd938dc52013-10-23 18:26:34 +0200236 case KVM_CAP_ENABLE_CAP_VM:
Cornelia Huck78599d92014-07-15 09:54:39 +0200237 case KVM_CAP_S390_IRQCHIP:
Dominik Dingelf2061652014-04-09 13:13:00 +0200238 case KVM_CAP_VM_ATTRIBUTES:
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200239 case KVM_CAP_MP_STATE:
Jens Freimann47b43c52014-11-11 20:57:06 +0100240 case KVM_CAP_S390_INJECT_IRQ:
David Hildenbrand2444b352014-10-09 14:10:13 +0200241 case KVM_CAP_S390_USER_SIGP:
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100242 case KVM_CAP_S390_USER_STSI:
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400243 case KVM_CAP_S390_SKEYS:
Jens Freimann816c7662014-11-24 17:13:46 +0100244 case KVM_CAP_S390_IRQ_STATE:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100245 r = 1;
246 break;
Thomas Huth41408c22015-02-06 15:01:21 +0100247 case KVM_CAP_S390_MEM_OP:
248 r = MEM_OP_MAX_SIZE;
249 break;
Christian Borntraegere726b1b2012-05-02 10:50:38 +0200250 case KVM_CAP_NR_VCPUS:
251 case KVM_CAP_MAX_VCPUS:
Eugene (jno) Dvurechenskife0edcb2015-04-22 18:37:40 +0200252 r = sclp.has_esca ? KVM_S390_ESCA_CPU_SLOTS
253 : KVM_S390_BSCA_CPU_SLOTS;
Christian Borntraegere726b1b2012-05-02 10:50:38 +0200254 break;
Nick Wange1e2e602013-03-25 17:22:58 +0100255 case KVM_CAP_NR_MEMSLOTS:
256 r = KVM_USER_MEM_SLOTS;
257 break;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200258 case KVM_CAP_S390_COW:
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100259 r = MACHINE_HAS_ESOP;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200260 break;
Eric Farman68c55752014-06-09 10:57:26 -0400261 case KVM_CAP_S390_VECTOR_REGISTERS:
262 r = MACHINE_HAS_VX;
263 break;
Fan Zhangc6e5f162016-01-07 18:24:29 +0800264 case KVM_CAP_S390_RI:
265 r = test_facility(64);
266 break;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200267 default:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100268 r = 0;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200269 }
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100270 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100271}
272
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400273static void kvm_s390_sync_dirty_log(struct kvm *kvm,
274 struct kvm_memory_slot *memslot)
275{
276 gfn_t cur_gfn, last_gfn;
277 unsigned long address;
278 struct gmap *gmap = kvm->arch.gmap;
279
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400280 /* Loop over all guest pages */
281 last_gfn = memslot->base_gfn + memslot->npages;
282 for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
283 address = gfn_to_hva_memslot(memslot, cur_gfn);
284
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100285 if (test_and_clear_guest_dirty(gmap->mm, address))
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400286 mark_page_dirty(kvm, cur_gfn);
Christian Borntraeger1763f8d2016-02-03 11:12:34 +0100287 if (fatal_signal_pending(current))
288 return;
Christian Borntraeger70c88a02016-02-02 15:15:56 +0100289 cond_resched();
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400290 }
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400291}
292
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100293/* Section: vm related */
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +0200294static void sca_del_vcpu(struct kvm_vcpu *vcpu);
295
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100296/*
297 * Get (and clear) the dirty memory log for a memory slot.
298 */
299int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
300 struct kvm_dirty_log *log)
301{
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400302 int r;
303 unsigned long n;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200304 struct kvm_memslots *slots;
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400305 struct kvm_memory_slot *memslot;
306 int is_dirty = 0;
307
308 mutex_lock(&kvm->slots_lock);
309
310 r = -EINVAL;
311 if (log->slot >= KVM_USER_MEM_SLOTS)
312 goto out;
313
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200314 slots = kvm_memslots(kvm);
315 memslot = id_to_memslot(slots, log->slot);
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400316 r = -ENOENT;
317 if (!memslot->dirty_bitmap)
318 goto out;
319
320 kvm_s390_sync_dirty_log(kvm, memslot);
321 r = kvm_get_dirty_log(kvm, log, &is_dirty);
322 if (r)
323 goto out;
324
325 /* Clear the dirty log */
326 if (is_dirty) {
327 n = kvm_dirty_bitmap_bytes(memslot);
328 memset(memslot->dirty_bitmap, 0, n);
329 }
330 r = 0;
331out:
332 mutex_unlock(&kvm->slots_lock);
333 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100334}
335
Cornelia Huckd938dc52013-10-23 18:26:34 +0200336static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
337{
338 int r;
339
340 if (cap->flags)
341 return -EINVAL;
342
343 switch (cap->cap) {
Cornelia Huck84223592013-07-15 13:36:01 +0200344 case KVM_CAP_S390_IRQCHIP:
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200345 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
Cornelia Huck84223592013-07-15 13:36:01 +0200346 kvm->arch.use_irqchip = 1;
347 r = 0;
348 break;
David Hildenbrand2444b352014-10-09 14:10:13 +0200349 case KVM_CAP_S390_USER_SIGP:
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200350 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
David Hildenbrand2444b352014-10-09 14:10:13 +0200351 kvm->arch.user_sigp = 1;
352 r = 0;
353 break;
Eric Farman68c55752014-06-09 10:57:26 -0400354 case KVM_CAP_S390_VECTOR_REGISTERS:
David Hildenbrand5967c172015-11-06 12:08:48 +0100355 mutex_lock(&kvm->lock);
356 if (atomic_read(&kvm->online_vcpus)) {
357 r = -EBUSY;
358 } else if (MACHINE_HAS_VX) {
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100359 set_kvm_facility(kvm->arch.model.fac_mask, 129);
360 set_kvm_facility(kvm->arch.model.fac_list, 129);
Michael Mueller18280d82015-03-16 16:05:41 +0100361 r = 0;
362 } else
363 r = -EINVAL;
David Hildenbrand5967c172015-11-06 12:08:48 +0100364 mutex_unlock(&kvm->lock);
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200365 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
366 r ? "(not available)" : "(success)");
Eric Farman68c55752014-06-09 10:57:26 -0400367 break;
Fan Zhangc6e5f162016-01-07 18:24:29 +0800368 case KVM_CAP_S390_RI:
369 r = -EINVAL;
370 mutex_lock(&kvm->lock);
371 if (atomic_read(&kvm->online_vcpus)) {
372 r = -EBUSY;
373 } else if (test_facility(64)) {
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100374 set_kvm_facility(kvm->arch.model.fac_mask, 64);
375 set_kvm_facility(kvm->arch.model.fac_list, 64);
Fan Zhangc6e5f162016-01-07 18:24:29 +0800376 r = 0;
377 }
378 mutex_unlock(&kvm->lock);
379 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
380 r ? "(not available)" : "(success)");
381 break;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100382 case KVM_CAP_S390_USER_STSI:
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200383 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100384 kvm->arch.user_stsi = 1;
385 r = 0;
386 break;
Cornelia Huckd938dc52013-10-23 18:26:34 +0200387 default:
388 r = -EINVAL;
389 break;
390 }
391 return r;
392}
393
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100394static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
395{
396 int ret;
397
398 switch (attr->attr) {
399 case KVM_S390_VM_MEM_LIMIT_SIZE:
400 ret = 0;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200401 VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
Dominik Dingela3a92c32014-12-01 17:24:42 +0100402 kvm->arch.mem_limit);
403 if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100404 ret = -EFAULT;
405 break;
406 default:
407 ret = -ENXIO;
408 break;
409 }
410 return ret;
411}
412
413static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200414{
415 int ret;
416 unsigned int idx;
417 switch (attr->attr) {
418 case KVM_S390_VM_MEM_ENABLE_CMMA:
Dominik Dingele6db1d62015-05-07 15:41:57 +0200419 /* enable CMMA only for z10 and later (EDAT_1) */
420 ret = -EINVAL;
421 if (!MACHINE_IS_LPAR || !MACHINE_HAS_EDAT1)
422 break;
423
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200424 ret = -EBUSY;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200425 VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200426 mutex_lock(&kvm->lock);
427 if (atomic_read(&kvm->online_vcpus) == 0) {
428 kvm->arch.use_cmma = 1;
429 ret = 0;
430 }
431 mutex_unlock(&kvm->lock);
432 break;
433 case KVM_S390_VM_MEM_CLR_CMMA:
Dominik Dingelc3489152015-06-18 13:17:11 +0200434 ret = -EINVAL;
435 if (!kvm->arch.use_cmma)
436 break;
437
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200438 VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200439 mutex_lock(&kvm->lock);
440 idx = srcu_read_lock(&kvm->srcu);
Dominik Dingela13cff32014-10-23 12:07:14 +0200441 s390_reset_cmma(kvm->arch.gmap->mm);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200442 srcu_read_unlock(&kvm->srcu, idx);
443 mutex_unlock(&kvm->lock);
444 ret = 0;
445 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100446 case KVM_S390_VM_MEM_LIMIT_SIZE: {
447 unsigned long new_limit;
448
449 if (kvm_is_ucontrol(kvm))
450 return -EINVAL;
451
452 if (get_user(new_limit, (u64 __user *)attr->addr))
453 return -EFAULT;
454
Dominik Dingela3a92c32014-12-01 17:24:42 +0100455 if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
456 new_limit > kvm->arch.mem_limit)
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100457 return -E2BIG;
458
Dominik Dingela3a92c32014-12-01 17:24:42 +0100459 if (!new_limit)
460 return -EINVAL;
461
462 /* gmap_alloc takes last usable address */
463 if (new_limit != KVM_S390_NO_MEM_LIMIT)
464 new_limit -= 1;
465
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100466 ret = -EBUSY;
467 mutex_lock(&kvm->lock);
468 if (atomic_read(&kvm->online_vcpus) == 0) {
469 /* gmap_alloc will round the limit up */
470 struct gmap *new = gmap_alloc(current->mm, new_limit);
471
472 if (!new) {
473 ret = -ENOMEM;
474 } else {
475 gmap_free(kvm->arch.gmap);
476 new->private = kvm;
477 kvm->arch.gmap = new;
478 ret = 0;
479 }
480 }
481 mutex_unlock(&kvm->lock);
Dominik Dingela3a92c32014-12-01 17:24:42 +0100482 VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
483 VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
484 (void *) kvm->arch.gmap->asce);
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100485 break;
486 }
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200487 default:
488 ret = -ENXIO;
489 break;
490 }
491 return ret;
492}
493
Tony Krowiaka374e892014-09-03 10:13:53 +0200494static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
495
496static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
497{
498 struct kvm_vcpu *vcpu;
499 int i;
500
Michael Mueller9d8d5782015-02-02 15:42:51 +0100501 if (!test_kvm_facility(kvm, 76))
Tony Krowiaka374e892014-09-03 10:13:53 +0200502 return -EINVAL;
503
504 mutex_lock(&kvm->lock);
505 switch (attr->attr) {
506 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
507 get_random_bytes(
508 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
509 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
510 kvm->arch.crypto.aes_kw = 1;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200511 VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200512 break;
513 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
514 get_random_bytes(
515 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
516 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
517 kvm->arch.crypto.dea_kw = 1;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200518 VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200519 break;
520 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
521 kvm->arch.crypto.aes_kw = 0;
522 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
523 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200524 VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200525 break;
526 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
527 kvm->arch.crypto.dea_kw = 0;
528 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
529 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200530 VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200531 break;
532 default:
533 mutex_unlock(&kvm->lock);
534 return -ENXIO;
535 }
536
537 kvm_for_each_vcpu(i, vcpu, kvm) {
538 kvm_s390_vcpu_crypto_setup(vcpu);
539 exit_sie(vcpu);
540 }
541 mutex_unlock(&kvm->lock);
542 return 0;
543}
544
Jason J. Herne72f25022014-11-25 09:46:02 -0500545static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
546{
547 u8 gtod_high;
548
549 if (copy_from_user(&gtod_high, (void __user *)attr->addr,
550 sizeof(gtod_high)))
551 return -EFAULT;
552
553 if (gtod_high != 0)
554 return -EINVAL;
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200555 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
Jason J. Herne72f25022014-11-25 09:46:02 -0500556
557 return 0;
558}
559
560static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
561{
David Hildenbrand5a3d8832015-09-29 16:27:24 +0200562 u64 gtod;
Jason J. Herne72f25022014-11-25 09:46:02 -0500563
564 if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
565 return -EFAULT;
566
David Hildenbrand25ed1672015-05-12 09:49:14 +0200567 kvm_s390_set_tod_clock(kvm, gtod);
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200568 VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
Jason J. Herne72f25022014-11-25 09:46:02 -0500569 return 0;
570}
571
572static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
573{
574 int ret;
575
576 if (attr->flags)
577 return -EINVAL;
578
579 switch (attr->attr) {
580 case KVM_S390_VM_TOD_HIGH:
581 ret = kvm_s390_set_tod_high(kvm, attr);
582 break;
583 case KVM_S390_VM_TOD_LOW:
584 ret = kvm_s390_set_tod_low(kvm, attr);
585 break;
586 default:
587 ret = -ENXIO;
588 break;
589 }
590 return ret;
591}
592
593static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
594{
595 u8 gtod_high = 0;
596
597 if (copy_to_user((void __user *)attr->addr, &gtod_high,
598 sizeof(gtod_high)))
599 return -EFAULT;
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200600 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
Jason J. Herne72f25022014-11-25 09:46:02 -0500601
602 return 0;
603}
604
605static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
606{
David Hildenbrand5a3d8832015-09-29 16:27:24 +0200607 u64 gtod;
Jason J. Herne72f25022014-11-25 09:46:02 -0500608
David Hildenbrand60417fc2015-09-29 16:20:36 +0200609 gtod = kvm_s390_get_tod_clock_fast(kvm);
Jason J. Herne72f25022014-11-25 09:46:02 -0500610 if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
611 return -EFAULT;
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200612 VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
Jason J. Herne72f25022014-11-25 09:46:02 -0500613
614 return 0;
615}
616
617static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
618{
619 int ret;
620
621 if (attr->flags)
622 return -EINVAL;
623
624 switch (attr->attr) {
625 case KVM_S390_VM_TOD_HIGH:
626 ret = kvm_s390_get_tod_high(kvm, attr);
627 break;
628 case KVM_S390_VM_TOD_LOW:
629 ret = kvm_s390_get_tod_low(kvm, attr);
630 break;
631 default:
632 ret = -ENXIO;
633 break;
634 }
635 return ret;
636}
637
Michael Mueller658b6ed2015-02-02 15:49:35 +0100638static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
639{
640 struct kvm_s390_vm_cpu_processor *proc;
641 int ret = 0;
642
643 mutex_lock(&kvm->lock);
644 if (atomic_read(&kvm->online_vcpus)) {
645 ret = -EBUSY;
646 goto out;
647 }
648 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
649 if (!proc) {
650 ret = -ENOMEM;
651 goto out;
652 }
653 if (!copy_from_user(proc, (void __user *)attr->addr,
654 sizeof(*proc))) {
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200655 kvm->arch.model.cpuid = proc->cpuid;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100656 kvm->arch.model.ibc = proc->ibc;
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100657 memcpy(kvm->arch.model.fac_list, proc->fac_list,
Michael Mueller658b6ed2015-02-02 15:49:35 +0100658 S390_ARCH_FAC_LIST_SIZE_BYTE);
659 } else
660 ret = -EFAULT;
661 kfree(proc);
662out:
663 mutex_unlock(&kvm->lock);
664 return ret;
665}
666
667static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
668{
669 int ret = -ENXIO;
670
671 switch (attr->attr) {
672 case KVM_S390_VM_CPU_PROCESSOR:
673 ret = kvm_s390_set_processor(kvm, attr);
674 break;
675 }
676 return ret;
677}
678
679static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
680{
681 struct kvm_s390_vm_cpu_processor *proc;
682 int ret = 0;
683
684 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
685 if (!proc) {
686 ret = -ENOMEM;
687 goto out;
688 }
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200689 proc->cpuid = kvm->arch.model.cpuid;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100690 proc->ibc = kvm->arch.model.ibc;
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100691 memcpy(&proc->fac_list, kvm->arch.model.fac_list,
692 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100693 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
694 ret = -EFAULT;
695 kfree(proc);
696out:
697 return ret;
698}
699
700static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
701{
702 struct kvm_s390_vm_cpu_machine *mach;
703 int ret = 0;
704
705 mach = kzalloc(sizeof(*mach), GFP_KERNEL);
706 if (!mach) {
707 ret = -ENOMEM;
708 goto out;
709 }
710 get_cpu_id((struct cpuid *) &mach->cpuid);
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200711 mach->ibc = sclp.ibc;
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100712 memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
Michael Mueller981467c2015-02-24 13:51:04 +0100713 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100714 memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +0100715 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100716 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
717 ret = -EFAULT;
718 kfree(mach);
719out:
720 return ret;
721}
722
723static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
724{
725 int ret = -ENXIO;
726
727 switch (attr->attr) {
728 case KVM_S390_VM_CPU_PROCESSOR:
729 ret = kvm_s390_get_processor(kvm, attr);
730 break;
731 case KVM_S390_VM_CPU_MACHINE:
732 ret = kvm_s390_get_machine(kvm, attr);
733 break;
734 }
735 return ret;
736}
737
Dominik Dingelf2061652014-04-09 13:13:00 +0200738static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
739{
740 int ret;
741
742 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200743 case KVM_S390_VM_MEM_CTRL:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100744 ret = kvm_s390_set_mem_control(kvm, attr);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200745 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500746 case KVM_S390_VM_TOD:
747 ret = kvm_s390_set_tod(kvm, attr);
748 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100749 case KVM_S390_VM_CPU_MODEL:
750 ret = kvm_s390_set_cpu_model(kvm, attr);
751 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200752 case KVM_S390_VM_CRYPTO:
753 ret = kvm_s390_vm_set_crypto(kvm, attr);
754 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200755 default:
756 ret = -ENXIO;
757 break;
758 }
759
760 return ret;
761}
762
763static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
764{
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100765 int ret;
766
767 switch (attr->group) {
768 case KVM_S390_VM_MEM_CTRL:
769 ret = kvm_s390_get_mem_control(kvm, attr);
770 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500771 case KVM_S390_VM_TOD:
772 ret = kvm_s390_get_tod(kvm, attr);
773 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100774 case KVM_S390_VM_CPU_MODEL:
775 ret = kvm_s390_get_cpu_model(kvm, attr);
776 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100777 default:
778 ret = -ENXIO;
779 break;
780 }
781
782 return ret;
Dominik Dingelf2061652014-04-09 13:13:00 +0200783}
784
785static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
786{
787 int ret;
788
789 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200790 case KVM_S390_VM_MEM_CTRL:
791 switch (attr->attr) {
792 case KVM_S390_VM_MEM_ENABLE_CMMA:
793 case KVM_S390_VM_MEM_CLR_CMMA:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100794 case KVM_S390_VM_MEM_LIMIT_SIZE:
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200795 ret = 0;
796 break;
797 default:
798 ret = -ENXIO;
799 break;
800 }
801 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500802 case KVM_S390_VM_TOD:
803 switch (attr->attr) {
804 case KVM_S390_VM_TOD_LOW:
805 case KVM_S390_VM_TOD_HIGH:
806 ret = 0;
807 break;
808 default:
809 ret = -ENXIO;
810 break;
811 }
812 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100813 case KVM_S390_VM_CPU_MODEL:
814 switch (attr->attr) {
815 case KVM_S390_VM_CPU_PROCESSOR:
816 case KVM_S390_VM_CPU_MACHINE:
817 ret = 0;
818 break;
819 default:
820 ret = -ENXIO;
821 break;
822 }
823 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200824 case KVM_S390_VM_CRYPTO:
825 switch (attr->attr) {
826 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
827 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
828 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
829 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
830 ret = 0;
831 break;
832 default:
833 ret = -ENXIO;
834 break;
835 }
836 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200837 default:
838 ret = -ENXIO;
839 break;
840 }
841
842 return ret;
843}
844
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400845static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
846{
847 uint8_t *keys;
848 uint64_t hva;
849 unsigned long curkey;
850 int i, r = 0;
851
852 if (args->flags != 0)
853 return -EINVAL;
854
855 /* Is this guest using storage keys? */
856 if (!mm_use_skey(current->mm))
857 return KVM_S390_GET_SKEYS_NONE;
858
859 /* Enforce sane limit on memory allocation */
860 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
861 return -EINVAL;
862
863 keys = kmalloc_array(args->count, sizeof(uint8_t),
864 GFP_KERNEL | __GFP_NOWARN);
865 if (!keys)
866 keys = vmalloc(sizeof(uint8_t) * args->count);
867 if (!keys)
868 return -ENOMEM;
869
870 for (i = 0; i < args->count; i++) {
871 hva = gfn_to_hva(kvm, args->start_gfn + i);
872 if (kvm_is_error_hva(hva)) {
873 r = -EFAULT;
874 goto out;
875 }
876
877 curkey = get_guest_storage_key(current->mm, hva);
878 if (IS_ERR_VALUE(curkey)) {
879 r = curkey;
880 goto out;
881 }
882 keys[i] = curkey;
883 }
884
885 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
886 sizeof(uint8_t) * args->count);
887 if (r)
888 r = -EFAULT;
889out:
890 kvfree(keys);
891 return r;
892}
893
894static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
895{
896 uint8_t *keys;
897 uint64_t hva;
898 int i, r = 0;
899
900 if (args->flags != 0)
901 return -EINVAL;
902
903 /* Enforce sane limit on memory allocation */
904 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
905 return -EINVAL;
906
907 keys = kmalloc_array(args->count, sizeof(uint8_t),
908 GFP_KERNEL | __GFP_NOWARN);
909 if (!keys)
910 keys = vmalloc(sizeof(uint8_t) * args->count);
911 if (!keys)
912 return -ENOMEM;
913
914 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
915 sizeof(uint8_t) * args->count);
916 if (r) {
917 r = -EFAULT;
918 goto out;
919 }
920
921 /* Enable storage key handling for the guest */
Dominik Dingel14d4a422015-05-07 15:16:13 +0200922 r = s390_enable_skey();
923 if (r)
924 goto out;
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400925
926 for (i = 0; i < args->count; i++) {
927 hva = gfn_to_hva(kvm, args->start_gfn + i);
928 if (kvm_is_error_hva(hva)) {
929 r = -EFAULT;
930 goto out;
931 }
932
933 /* Lowest order bit is reserved */
934 if (keys[i] & 0x01) {
935 r = -EINVAL;
936 goto out;
937 }
938
939 r = set_guest_storage_key(current->mm, hva,
940 (unsigned long)keys[i], 0);
941 if (r)
942 goto out;
943 }
944out:
945 kvfree(keys);
946 return r;
947}
948
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100949long kvm_arch_vm_ioctl(struct file *filp,
950 unsigned int ioctl, unsigned long arg)
951{
952 struct kvm *kvm = filp->private_data;
953 void __user *argp = (void __user *)arg;
Dominik Dingelf2061652014-04-09 13:13:00 +0200954 struct kvm_device_attr attr;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100955 int r;
956
957 switch (ioctl) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100958 case KVM_S390_INTERRUPT: {
959 struct kvm_s390_interrupt s390int;
960
961 r = -EFAULT;
962 if (copy_from_user(&s390int, argp, sizeof(s390int)))
963 break;
964 r = kvm_s390_inject_vm(kvm, &s390int);
965 break;
966 }
Cornelia Huckd938dc52013-10-23 18:26:34 +0200967 case KVM_ENABLE_CAP: {
968 struct kvm_enable_cap cap;
969 r = -EFAULT;
970 if (copy_from_user(&cap, argp, sizeof(cap)))
971 break;
972 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
973 break;
974 }
Cornelia Huck84223592013-07-15 13:36:01 +0200975 case KVM_CREATE_IRQCHIP: {
976 struct kvm_irq_routing_entry routing;
977
978 r = -EINVAL;
979 if (kvm->arch.use_irqchip) {
980 /* Set up dummy routing. */
981 memset(&routing, 0, sizeof(routing));
Nicholas Krause152b2832015-08-06 13:05:54 -0400982 r = kvm_set_irq_routing(kvm, &routing, 0, 0);
Cornelia Huck84223592013-07-15 13:36:01 +0200983 }
984 break;
985 }
Dominik Dingelf2061652014-04-09 13:13:00 +0200986 case KVM_SET_DEVICE_ATTR: {
987 r = -EFAULT;
988 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
989 break;
990 r = kvm_s390_vm_set_attr(kvm, &attr);
991 break;
992 }
993 case KVM_GET_DEVICE_ATTR: {
994 r = -EFAULT;
995 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
996 break;
997 r = kvm_s390_vm_get_attr(kvm, &attr);
998 break;
999 }
1000 case KVM_HAS_DEVICE_ATTR: {
1001 r = -EFAULT;
1002 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1003 break;
1004 r = kvm_s390_vm_has_attr(kvm, &attr);
1005 break;
1006 }
Jason J. Herne30ee2a92014-09-23 09:23:01 -04001007 case KVM_S390_GET_SKEYS: {
1008 struct kvm_s390_skeys args;
1009
1010 r = -EFAULT;
1011 if (copy_from_user(&args, argp,
1012 sizeof(struct kvm_s390_skeys)))
1013 break;
1014 r = kvm_s390_get_skeys(kvm, &args);
1015 break;
1016 }
1017 case KVM_S390_SET_SKEYS: {
1018 struct kvm_s390_skeys args;
1019
1020 r = -EFAULT;
1021 if (copy_from_user(&args, argp,
1022 sizeof(struct kvm_s390_skeys)))
1023 break;
1024 r = kvm_s390_set_skeys(kvm, &args);
1025 break;
1026 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001027 default:
Avi Kivity367e1312009-08-26 14:57:07 +03001028 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001029 }
1030
1031 return r;
1032}
1033
Tony Krowiak45c9b472015-01-13 11:33:26 -05001034static int kvm_s390_query_ap_config(u8 *config)
1035{
1036 u32 fcn_code = 0x04000000UL;
Christian Borntraeger86044c82015-02-26 13:53:47 +01001037 u32 cc = 0;
Tony Krowiak45c9b472015-01-13 11:33:26 -05001038
Christian Borntraeger86044c82015-02-26 13:53:47 +01001039 memset(config, 0, 128);
Tony Krowiak45c9b472015-01-13 11:33:26 -05001040 asm volatile(
1041 "lgr 0,%1\n"
1042 "lgr 2,%2\n"
1043 ".long 0xb2af0000\n" /* PQAP(QCI) */
Christian Borntraeger86044c82015-02-26 13:53:47 +01001044 "0: ipm %0\n"
Tony Krowiak45c9b472015-01-13 11:33:26 -05001045 "srl %0,28\n"
Christian Borntraeger86044c82015-02-26 13:53:47 +01001046 "1:\n"
1047 EX_TABLE(0b, 1b)
1048 : "+r" (cc)
Tony Krowiak45c9b472015-01-13 11:33:26 -05001049 : "r" (fcn_code), "r" (config)
1050 : "cc", "0", "2", "memory"
1051 );
1052
1053 return cc;
1054}
1055
1056static int kvm_s390_apxa_installed(void)
1057{
1058 u8 config[128];
1059 int cc;
1060
Heiko Carstensa6aacc32015-11-24 14:28:12 +01001061 if (test_facility(12)) {
Tony Krowiak45c9b472015-01-13 11:33:26 -05001062 cc = kvm_s390_query_ap_config(config);
1063
1064 if (cc)
1065 pr_err("PQAP(QCI) failed with cc=%d", cc);
1066 else
1067 return config[0] & 0x40;
1068 }
1069
1070 return 0;
1071}
1072
1073static void kvm_s390_set_crycb_format(struct kvm *kvm)
1074{
1075 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
1076
1077 if (kvm_s390_apxa_installed())
1078 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
1079 else
1080 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
1081}
1082
David Hildenbrand9bb0ec02016-04-04 14:27:51 +02001083static u64 kvm_s390_get_initial_cpuid(void)
Michael Mueller9d8d5782015-02-02 15:42:51 +01001084{
David Hildenbrand9bb0ec02016-04-04 14:27:51 +02001085 struct cpuid cpuid;
1086
1087 get_cpu_id(&cpuid);
1088 cpuid.version = 0xff;
1089 return *((u64 *) &cpuid);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001090}
1091
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001092static void kvm_s390_crypto_init(struct kvm *kvm)
Tony Krowiak5102ee82014-06-27 14:46:01 -04001093{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001094 if (!test_kvm_facility(kvm, 76))
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001095 return;
Tony Krowiak5102ee82014-06-27 14:46:01 -04001096
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001097 kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
Tony Krowiak45c9b472015-01-13 11:33:26 -05001098 kvm_s390_set_crycb_format(kvm);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001099
Tony Krowiaked6f76b2015-02-24 14:06:57 -05001100 /* Enable AES/DEA protected key functions by default */
1101 kvm->arch.crypto.aes_kw = 1;
1102 kvm->arch.crypto.dea_kw = 1;
1103 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1104 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1105 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1106 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
Tony Krowiak5102ee82014-06-27 14:46:01 -04001107}
1108
Eugene (jno) Dvurechenski7d43baf2015-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) Dvurechenski7d43baf2015-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) Dvurechenski7d43baf2015-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
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001160 kvm->arch.sie_page2 =
1161 (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1162 if (!kvm->arch.sie_page2)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001163 goto out_err;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001164
Michael Muellerfb5bf932015-02-27 14:25:10 +01001165 /* Populate the facility mask initially. */
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001166 memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +01001167 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001168 for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
1169 if (i < kvm_s390_fac_list_mask_size())
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001170 kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i];
Michael Mueller9d8d5782015-02-02 15:42:51 +01001171 else
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001172 kvm->arch.model.fac_mask[i] = 0UL;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001173 }
1174
Michael Mueller981467c2015-02-24 13:51:04 +01001175 /* Populate the facility list initially. */
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001176 kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
1177 memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask,
Michael Mueller981467c2015-02-24 13:51:04 +01001178 S390_ARCH_FAC_LIST_SIZE_BYTE);
1179
David Hildenbrand9bb0ec02016-04-04 14:27:51 +02001180 kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001181 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001182
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001183 kvm_s390_crypto_init(kvm);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001184
Carsten Otteba5c1e92008-03-25 18:47:26 +01001185 spin_lock_init(&kvm->arch.float_int.lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001186 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1187 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
Heiko Carstens8a2422342014-01-10 14:33:28 +01001188 init_waitqueue_head(&kvm->arch.ipte_wq);
Thomas Hutha6b7e452014-10-01 14:48:42 +02001189 mutex_init(&kvm->arch.ipte_mutex);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001190
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001191 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
Christian Borntraeger78f26132015-07-22 15:50:58 +02001192 VM_EVENT(kvm, 3, "vm created with type %lu", type);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001193
Carsten Ottee08b9632012-01-04 10:25:20 +01001194 if (type & KVM_VM_S390_UCONTROL) {
1195 kvm->arch.gmap = NULL;
Dominik Dingela3a92c32014-12-01 17:24:42 +01001196 kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
Carsten Ottee08b9632012-01-04 10:25:20 +01001197 } else {
Guenther Hutzl32e6b232014-12-01 17:24:42 +01001198 if (sclp.hamax == U64_MAX)
1199 kvm->arch.mem_limit = TASK_MAX_SIZE;
1200 else
1201 kvm->arch.mem_limit = min_t(unsigned long, TASK_MAX_SIZE,
1202 sclp.hamax + 1);
Dominik Dingela3a92c32014-12-01 17:24:42 +01001203 kvm->arch.gmap = gmap_alloc(current->mm, kvm->arch.mem_limit - 1);
Carsten Ottee08b9632012-01-04 10:25:20 +01001204 if (!kvm->arch.gmap)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001205 goto out_err;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001206 kvm->arch.gmap->private = kvm;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001207 kvm->arch.gmap->pfault_enabled = 0;
Carsten Ottee08b9632012-01-04 10:25:20 +01001208 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001209
1210 kvm->arch.css_support = 0;
Cornelia Huck84223592013-07-15 13:36:01 +02001211 kvm->arch.use_irqchip = 0;
Jason J. Herne72f25022014-11-25 09:46:02 -05001212 kvm->arch.epoch = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001213
David Hildenbrand8ad35752014-03-14 11:00:21 +01001214 spin_lock_init(&kvm->arch.start_stop_lock);
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001215 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
David Hildenbrand8ad35752014-03-14 11:00:21 +01001216
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001217 return 0;
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001218out_err:
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001219 free_page((unsigned long)kvm->arch.sie_page2);
Dominik Dingel40f5b732015-03-12 13:55:53 +01001220 debug_unregister(kvm->arch.dbf);
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001221 sca_dispose(kvm);
Christian Borntraeger78f26132015-07-22 15:50:58 +02001222 KVM_EVENT(3, "creation of vm failed: %d", rc);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001223 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001224}
1225
Christian Borntraegerd329c032008-11-26 14:50:27 +01001226void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1227{
1228 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
Cornelia Huckade38c32012-07-23 17:20:30 +02001229 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001230 kvm_s390_clear_local_irqs(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001231 kvm_clear_async_pf_completion_queue(vcpu);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +02001232 if (!kvm_is_ucontrol(vcpu->kvm))
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001233 sca_del_vcpu(vcpu);
Carsten Otte27e03932012-01-04 10:25:21 +01001234
1235 if (kvm_is_ucontrol(vcpu->kvm))
1236 gmap_free(vcpu->arch.gmap);
1237
Dominik Dingele6db1d62015-05-07 15:41:57 +02001238 if (vcpu->kvm->arch.use_cmma)
Dominik Dingelb31605c2014-03-25 13:47:11 +01001239 kvm_s390_vcpu_unsetup_cmma(vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001240 free_page((unsigned long)(vcpu->arch.sie_block));
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001241
Christian Borntraeger6692cef2008-11-26 14:51:08 +01001242 kvm_vcpu_uninit(vcpu);
Michael Muellerb110fea2013-06-12 13:54:54 +02001243 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001244}
1245
1246static void kvm_free_vcpus(struct kvm *kvm)
1247{
1248 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001249 struct kvm_vcpu *vcpu;
Christian Borntraegerd329c032008-11-26 14:50:27 +01001250
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001251 kvm_for_each_vcpu(i, vcpu, kvm)
1252 kvm_arch_vcpu_destroy(vcpu);
1253
1254 mutex_lock(&kvm->lock);
1255 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1256 kvm->vcpus[i] = NULL;
1257
1258 atomic_set(&kvm->online_vcpus, 0);
1259 mutex_unlock(&kvm->lock);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001260}
1261
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001262void kvm_arch_destroy_vm(struct kvm *kvm)
1263{
Christian Borntraegerd329c032008-11-26 14:50:27 +01001264 kvm_free_vcpus(kvm);
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001265 sca_dispose(kvm);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001266 debug_unregister(kvm->arch.dbf);
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001267 free_page((unsigned long)kvm->arch.sie_page2);
Carsten Otte27e03932012-01-04 10:25:21 +01001268 if (!kvm_is_ucontrol(kvm))
1269 gmap_free(kvm->arch.gmap);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001270 kvm_s390_destroy_adapters(kvm);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001271 kvm_s390_clear_float_irqs(kvm);
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001272 KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001273}
1274
1275/* Section: vcpu related */
Dominik Dingeldafd0322014-12-02 16:53:21 +01001276static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1277{
1278 vcpu->arch.gmap = gmap_alloc(current->mm, -1UL);
1279 if (!vcpu->arch.gmap)
1280 return -ENOMEM;
1281 vcpu->arch.gmap->private = vcpu->kvm;
1282
1283 return 0;
1284}
1285
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001286static void sca_del_vcpu(struct kvm_vcpu *vcpu)
1287{
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001288 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001289 if (vcpu->kvm->arch.use_esca) {
1290 struct esca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001291
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001292 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
David Hildenbrand10ce32d2015-10-12 12:41:41 +02001293 sca->cpu[vcpu->vcpu_id].sda = 0;
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001294 } else {
1295 struct bsca_block *sca = vcpu->kvm->arch.sca;
1296
1297 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
David Hildenbrand10ce32d2015-10-12 12:41:41 +02001298 sca->cpu[vcpu->vcpu_id].sda = 0;
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001299 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001300 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001301}
1302
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001303static void sca_add_vcpu(struct kvm_vcpu *vcpu)
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001304{
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001305 read_lock(&vcpu->kvm->arch.sca_lock);
1306 if (vcpu->kvm->arch.use_esca) {
1307 struct esca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001308
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001309 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001310 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1311 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
David Hildenbrand25508822015-10-12 16:27:23 +02001312 vcpu->arch.sie_block->ecb2 |= 0x04U;
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001313 set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001314 } else {
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001315 struct bsca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001316
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001317 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001318 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1319 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001320 set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +02001321 }
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001322 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001323}
1324
1325/* Basic SCA to Extended SCA data copy routines */
1326static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
1327{
1328 d->sda = s->sda;
1329 d->sigp_ctrl.c = s->sigp_ctrl.c;
1330 d->sigp_ctrl.scn = s->sigp_ctrl.scn;
1331}
1332
1333static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
1334{
1335 int i;
1336
1337 d->ipte_control = s->ipte_control;
1338 d->mcn[0] = s->mcn;
1339 for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
1340 sca_copy_entry(&d->cpu[i], &s->cpu[i]);
1341}
1342
1343static int sca_switch_to_extended(struct kvm *kvm)
1344{
1345 struct bsca_block *old_sca = kvm->arch.sca;
1346 struct esca_block *new_sca;
1347 struct kvm_vcpu *vcpu;
1348 unsigned int vcpu_idx;
1349 u32 scaol, scaoh;
1350
1351 new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
1352 if (!new_sca)
1353 return -ENOMEM;
1354
1355 scaoh = (u32)((u64)(new_sca) >> 32);
1356 scaol = (u32)(u64)(new_sca) & ~0x3fU;
1357
1358 kvm_s390_vcpu_block_all(kvm);
1359 write_lock(&kvm->arch.sca_lock);
1360
1361 sca_copy_b_to_e(new_sca, old_sca);
1362
1363 kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
1364 vcpu->arch.sie_block->scaoh = scaoh;
1365 vcpu->arch.sie_block->scaol = scaol;
1366 vcpu->arch.sie_block->ecb2 |= 0x04U;
1367 }
1368 kvm->arch.sca = new_sca;
1369 kvm->arch.use_esca = 1;
1370
1371 write_unlock(&kvm->arch.sca_lock);
1372 kvm_s390_vcpu_unblock_all(kvm);
1373
1374 free_page((unsigned long)old_sca);
1375
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001376 VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
1377 old_sca, kvm->arch.sca);
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001378 return 0;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001379}
1380
1381static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
1382{
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001383 int rc;
1384
1385 if (id < KVM_S390_BSCA_CPU_SLOTS)
1386 return true;
1387 if (!sclp.has_esca)
1388 return false;
1389
1390 mutex_lock(&kvm->lock);
1391 rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
1392 mutex_unlock(&kvm->lock);
1393
1394 return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001395}
1396
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001397int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1398{
Dominik Dingel3c038e62013-10-07 17:11:48 +02001399 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1400 kvm_clear_async_pf_completion_queue(vcpu);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001401 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
1402 KVM_SYNC_GPRS |
Christian Borntraeger9eed07352012-02-06 10:59:07 +01001403 KVM_SYNC_ACRS |
David Hildenbrandb028ee32014-07-17 10:47:43 +02001404 KVM_SYNC_CRS |
1405 KVM_SYNC_ARCH0 |
1406 KVM_SYNC_PFAULT;
Fan Zhangc6e5f162016-01-07 18:24:29 +08001407 if (test_kvm_facility(vcpu->kvm, 64))
1408 vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
David Hildenbrandf6aa6dc2016-01-15 14:11:46 +01001409 /* fprs can be synchronized via vrs, even if the guest has no vx. With
1410 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
1411 */
1412 if (MACHINE_HAS_VX)
Eric Farman68c55752014-06-09 10:57:26 -04001413 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
David Hildenbrand6fd8e672016-01-18 14:46:34 +01001414 else
1415 vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
Dominik Dingeldafd0322014-12-02 16:53:21 +01001416
1417 if (kvm_is_ucontrol(vcpu->kvm))
1418 return __kvm_ucontrol_vcpu_init(vcpu);
1419
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001420 return 0;
1421}
1422
David Hildenbranddb0758b2016-02-15 09:42:25 +01001423/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1424static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1425{
1426 WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
David Hildenbrand9c23a132016-02-17 21:53:33 +01001427 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001428 vcpu->arch.cputm_start = get_tod_clock_fast();
David Hildenbrand9c23a132016-02-17 21:53:33 +01001429 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001430}
1431
1432/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1433static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1434{
1435 WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
David Hildenbrand9c23a132016-02-17 21:53:33 +01001436 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001437 vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
1438 vcpu->arch.cputm_start = 0;
David Hildenbrand9c23a132016-02-17 21:53:33 +01001439 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001440}
1441
1442/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1443static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1444{
1445 WARN_ON_ONCE(vcpu->arch.cputm_enabled);
1446 vcpu->arch.cputm_enabled = true;
1447 __start_cpu_timer_accounting(vcpu);
1448}
1449
1450/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1451static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1452{
1453 WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
1454 __stop_cpu_timer_accounting(vcpu);
1455 vcpu->arch.cputm_enabled = false;
1456}
1457
1458static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1459{
1460 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1461 __enable_cpu_timer_accounting(vcpu);
1462 preempt_enable();
1463}
1464
1465static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1466{
1467 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1468 __disable_cpu_timer_accounting(vcpu);
1469 preempt_enable();
1470}
1471
David Hildenbrand4287f242016-02-15 09:40:12 +01001472/* set the cpu timer - may only be called from the VCPU thread itself */
1473void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
1474{
David Hildenbranddb0758b2016-02-15 09:42:25 +01001475 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
David Hildenbrand9c23a132016-02-17 21:53:33 +01001476 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001477 if (vcpu->arch.cputm_enabled)
1478 vcpu->arch.cputm_start = get_tod_clock_fast();
David Hildenbrand4287f242016-02-15 09:40:12 +01001479 vcpu->arch.sie_block->cputm = cputm;
David Hildenbrand9c23a132016-02-17 21:53:33 +01001480 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001481 preempt_enable();
David Hildenbrand4287f242016-02-15 09:40:12 +01001482}
1483
David Hildenbranddb0758b2016-02-15 09:42:25 +01001484/* update and get the cpu timer - can also be called from other VCPU threads */
David Hildenbrand4287f242016-02-15 09:40:12 +01001485__u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
1486{
David Hildenbrand9c23a132016-02-17 21:53:33 +01001487 unsigned int seq;
David Hildenbranddb0758b2016-02-15 09:42:25 +01001488 __u64 value;
David Hildenbranddb0758b2016-02-15 09:42:25 +01001489
1490 if (unlikely(!vcpu->arch.cputm_enabled))
1491 return vcpu->arch.sie_block->cputm;
1492
David Hildenbrand9c23a132016-02-17 21:53:33 +01001493 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1494 do {
1495 seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
1496 /*
1497 * If the writer would ever execute a read in the critical
1498 * section, e.g. in irq context, we have a deadlock.
1499 */
1500 WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
1501 value = vcpu->arch.sie_block->cputm;
1502 /* if cputm_start is 0, accounting is being started/stopped */
1503 if (likely(vcpu->arch.cputm_start))
1504 value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
1505 } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
1506 preempt_enable();
David Hildenbranddb0758b2016-02-15 09:42:25 +01001507 return value;
David Hildenbrand4287f242016-02-15 09:40:12 +01001508}
1509
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001510void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1511{
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001512 /* Save host register state */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02001513 save_fpu_regs();
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001514 vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
1515 vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
Hendrik Brueckner96b2d7a2015-06-12 13:53:51 +02001516
David Hildenbrand6fd8e672016-01-18 14:46:34 +01001517 if (MACHINE_HAS_VX)
1518 current->thread.fpu.regs = vcpu->run->s.regs.vrs;
1519 else
1520 current->thread.fpu.regs = vcpu->run->s.regs.fprs;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001521 current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001522 if (test_fp_ctl(current->thread.fpu.fpc))
Hendrik Brueckner96b2d7a2015-06-12 13:53:51 +02001523 /* User space provided an invalid FPC, let's clear it */
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001524 current->thread.fpu.fpc = 0;
1525
1526 save_access_regs(vcpu->arch.host_acrs);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001527 restore_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001528 gmap_enable(vcpu->arch.gmap);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001529 atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand5ebda312016-02-22 13:52:27 +01001530 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
David Hildenbranddb0758b2016-02-15 09:42:25 +01001531 __start_cpu_timer_accounting(vcpu);
David Hildenbrand01a745a2016-02-12 20:41:56 +01001532 vcpu->cpu = cpu;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001533}
1534
1535void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1536{
David Hildenbrand01a745a2016-02-12 20:41:56 +01001537 vcpu->cpu = -1;
David Hildenbrand5ebda312016-02-22 13:52:27 +01001538 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
David Hildenbranddb0758b2016-02-15 09:42:25 +01001539 __stop_cpu_timer_accounting(vcpu);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001540 atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001541 gmap_disable(vcpu->arch.gmap);
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001542
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001543 /* Save guest register state */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02001544 save_fpu_regs();
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001545 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001546
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001547 /* Restore host register state */
1548 current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
1549 current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001550
1551 save_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001552 restore_access_regs(vcpu->arch.host_acrs);
1553}
1554
1555static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1556{
1557 /* this equals initial cpu reset in pop, but we don't switch to ESA */
1558 vcpu->arch.sie_block->gpsw.mask = 0UL;
1559 vcpu->arch.sie_block->gpsw.addr = 0UL;
Christian Borntraeger8d26cf72012-01-11 11:19:32 +01001560 kvm_s390_set_prefix(vcpu, 0);
David Hildenbrand4287f242016-02-15 09:40:12 +01001561 kvm_s390_set_cpu_timer(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001562 vcpu->arch.sie_block->ckc = 0UL;
1563 vcpu->arch.sie_block->todpr = 0;
1564 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1565 vcpu->arch.sie_block->gcr[0] = 0xE0UL;
1566 vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001567 /* make sure the new fpc will be lazily loaded */
1568 save_fpu_regs();
1569 current->thread.fpu.fpc = 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001570 vcpu->arch.sie_block->gbea = 1;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001571 vcpu->arch.sie_block->pp = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001572 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1573 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001574 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
1575 kvm_s390_vcpu_stop(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001576 kvm_s390_clear_local_irqs(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001577}
1578
Dominik Dingel31928aa2014-12-04 15:47:07 +01001579void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001580{
Jason J. Herne72f25022014-11-25 09:46:02 -05001581 mutex_lock(&vcpu->kvm->lock);
Fan Zhangfdf03652015-05-13 10:58:41 +02001582 preempt_disable();
Jason J. Herne72f25022014-11-25 09:46:02 -05001583 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
Fan Zhangfdf03652015-05-13 10:58:41 +02001584 preempt_enable();
Jason J. Herne72f25022014-11-25 09:46:02 -05001585 mutex_unlock(&vcpu->kvm->lock);
David Hildenbrand25508822015-10-12 16:27:23 +02001586 if (!kvm_is_ucontrol(vcpu->kvm)) {
Dominik Dingeldafd0322014-12-02 16:53:21 +01001587 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001588 sca_add_vcpu(vcpu);
David Hildenbrand25508822015-10-12 16:27:23 +02001589 }
1590
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001591}
1592
Tony Krowiak5102ee82014-06-27 14:46:01 -04001593static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
1594{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001595 if (!test_kvm_facility(vcpu->kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -04001596 return;
1597
Tony Krowiaka374e892014-09-03 10:13:53 +02001598 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1599
1600 if (vcpu->kvm->arch.crypto.aes_kw)
1601 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1602 if (vcpu->kvm->arch.crypto.dea_kw)
1603 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1604
Tony Krowiak5102ee82014-06-27 14:46:01 -04001605 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
1606}
1607
Dominik Dingelb31605c2014-03-25 13:47:11 +01001608void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1609{
1610 free_page(vcpu->arch.sie_block->cbrlo);
1611 vcpu->arch.sie_block->cbrlo = 0;
1612}
1613
1614int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1615{
1616 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1617 if (!vcpu->arch.sie_block->cbrlo)
1618 return -ENOMEM;
1619
1620 vcpu->arch.sie_block->ecb2 |= 0x80;
1621 vcpu->arch.sie_block->ecb2 &= ~0x08;
1622 return 0;
1623}
1624
Michael Mueller91520f12015-02-27 14:32:11 +01001625static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
1626{
1627 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
1628
Michael Mueller91520f12015-02-27 14:32:11 +01001629 vcpu->arch.sie_block->ibc = model->ibc;
David Hildenbrand80bc79d2015-12-02 09:43:29 +01001630 if (test_kvm_facility(vcpu->kvm, 7))
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001631 vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
Michael Mueller91520f12015-02-27 14:32:11 +01001632}
1633
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001634int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1635{
Dominik Dingelb31605c2014-03-25 13:47:11 +01001636 int rc = 0;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001637
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001638 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
1639 CPUSTAT_SM |
Guenther Hutzla4a4f192015-03-31 14:39:49 +02001640 CPUSTAT_STOPPED);
1641
Guenther Hutzl53df84f2015-02-18 11:13:03 +01001642 if (test_kvm_facility(vcpu->kvm, 78))
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001643 atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
Guenther Hutzl53df84f2015-02-18 11:13:03 +01001644 else if (test_kvm_facility(vcpu->kvm, 8))
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001645 atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
Guenther Hutzla4a4f192015-03-31 14:39:49 +02001646
Michael Mueller91520f12015-02-27 14:32:11 +01001647 kvm_s390_vcpu_setup_model(vcpu);
1648
David Hildenbrandbd50e8e2016-03-04 12:23:55 +01001649 vcpu->arch.sie_block->ecb = 0x02;
1650 if (test_kvm_facility(vcpu->kvm, 9))
1651 vcpu->arch.sie_block->ecb |= 0x04;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001652 if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73))
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001653 vcpu->arch.sie_block->ecb |= 0x10;
1654
David Hildenbrandd6af0b42016-03-04 11:55:56 +01001655 if (test_kvm_facility(vcpu->kvm, 8))
1656 vcpu->arch.sie_block->ecb2 |= 0x08;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001657 vcpu->arch.sie_block->eca = 0xC1002000U;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001658 if (sclp.has_siif)
Heiko Carstens217a4402013-12-30 12:54:14 +01001659 vcpu->arch.sie_block->eca |= 1;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001660 if (sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001661 vcpu->arch.sie_block->eca |= 0x10000000U;
Fan Zhangc6e5f162016-01-07 18:24:29 +08001662 if (test_kvm_facility(vcpu->kvm, 64))
1663 vcpu->arch.sie_block->ecb3 |= 0x01;
Michael Mueller18280d82015-03-16 16:05:41 +01001664 if (test_kvm_facility(vcpu->kvm, 129)) {
Eric Farman13211ea2014-04-30 13:39:46 -04001665 vcpu->arch.sie_block->eca |= 0x00020000;
1666 vcpu->arch.sie_block->ecd |= 0x20000000;
1667 }
Fan Zhangc6e5f162016-01-07 18:24:29 +08001668 vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
Thomas Huth492d8642015-02-10 16:11:01 +01001669 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
Matthew Rosato5a5e6532013-01-29 11:48:20 -05001670
Dominik Dingele6db1d62015-05-07 15:41:57 +02001671 if (vcpu->kvm->arch.use_cmma) {
Dominik Dingelb31605c2014-03-25 13:47:11 +01001672 rc = kvm_s390_vcpu_setup_cmma(vcpu);
1673 if (rc)
1674 return rc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001675 }
David Hildenbrand0ac96ca2014-12-12 15:17:31 +01001676 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Christian Borntraegerca872302009-05-12 17:21:49 +02001677 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001678
Tony Krowiak5102ee82014-06-27 14:46:01 -04001679 kvm_s390_vcpu_crypto_setup(vcpu);
1680
Dominik Dingelb31605c2014-03-25 13:47:11 +01001681 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001682}
1683
1684struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1685 unsigned int id)
1686{
Carsten Otte4d475552011-10-18 12:27:12 +02001687 struct kvm_vcpu *vcpu;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001688 struct sie_page *sie_page;
Carsten Otte4d475552011-10-18 12:27:12 +02001689 int rc = -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001690
David Hildenbrand42158252015-10-12 12:57:22 +02001691 if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
Carsten Otte4d475552011-10-18 12:27:12 +02001692 goto out;
1693
1694 rc = -ENOMEM;
1695
Michael Muellerb110fea2013-06-12 13:54:54 +02001696 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001697 if (!vcpu)
Carsten Otte4d475552011-10-18 12:27:12 +02001698 goto out;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001699
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001700 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
1701 if (!sie_page)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001702 goto out_free_cpu;
1703
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001704 vcpu->arch.sie_block = &sie_page->sie_block;
1705 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
1706
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001707 vcpu->arch.sie_block->icpua = id;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001708 spin_lock_init(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001709 vcpu->arch.local_int.float_int = &kvm->arch.float_int;
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001710 vcpu->arch.local_int.wq = &vcpu->wq;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01001711 vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
David Hildenbrand9c23a132016-02-17 21:53:33 +01001712 seqcount_init(&vcpu->arch.cputm_seqcount);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001713
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001714 rc = kvm_vcpu_init(vcpu, kvm, id);
1715 if (rc)
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001716 goto out_free_sie_block;
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001717 VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001718 vcpu->arch.sie_block);
Cornelia Huckade38c32012-07-23 17:20:30 +02001719 trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001720
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001721 return vcpu;
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001722out_free_sie_block:
1723 free_page((unsigned long)(vcpu->arch.sie_block));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001724out_free_cpu:
Michael Muellerb110fea2013-06-12 13:54:54 +02001725 kmem_cache_free(kvm_vcpu_cache, vcpu);
Carsten Otte4d475552011-10-18 12:27:12 +02001726out:
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001727 return ERR_PTR(rc);
1728}
1729
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001730int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1731{
David Hildenbrand9a022062014-08-05 17:40:47 +02001732 return kvm_s390_vcpu_has_irq(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001733}
1734
Christian Borntraeger27406cd2015-04-14 12:17:34 +02001735void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001736{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001737 atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
David Hildenbrand61a6df52015-05-12 08:41:40 +02001738 exit_sie(vcpu);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001739}
1740
Christian Borntraeger27406cd2015-04-14 12:17:34 +02001741void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001742{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001743 atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001744}
1745
Christian Borntraeger8e236542015-04-09 13:49:04 +02001746static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
1747{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001748 atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
David Hildenbrand61a6df52015-05-12 08:41:40 +02001749 exit_sie(vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001750}
1751
1752static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
1753{
Jason J. Herne9bf9fde2015-09-16 09:13:50 -04001754 atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001755}
1756
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001757/*
1758 * Kick a guest cpu out of SIE and wait until SIE is not running.
1759 * If the CPU is not running (e.g. waiting as idle) the function will
1760 * return immediately. */
1761void exit_sie(struct kvm_vcpu *vcpu)
1762{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001763 atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001764 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
1765 cpu_relax();
1766}
1767
Christian Borntraeger8e236542015-04-09 13:49:04 +02001768/* Kick a guest cpu out of SIE to process a request synchronously */
1769void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001770{
Christian Borntraeger8e236542015-04-09 13:49:04 +02001771 kvm_make_request(req, vcpu);
1772 kvm_s390_vcpu_request(vcpu);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001773}
1774
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001775static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
1776{
1777 int i;
1778 struct kvm *kvm = gmap->private;
1779 struct kvm_vcpu *vcpu;
1780
1781 kvm_for_each_vcpu(i, vcpu, kvm) {
1782 /* match against both prefix pages */
Michael Muellerfda902c2014-05-13 16:58:30 +02001783 if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001784 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001785 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001786 }
1787 }
1788}
1789
Christoffer Dallb6d33832012-03-08 16:44:24 -05001790int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1791{
1792 /* kvm common code refers to this, but never calls it */
1793 BUG();
1794 return 0;
1795}
1796
Carsten Otte14eebd92012-05-15 14:15:26 +02001797static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
1798 struct kvm_one_reg *reg)
1799{
1800 int r = -EINVAL;
1801
1802 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001803 case KVM_REG_S390_TODPR:
1804 r = put_user(vcpu->arch.sie_block->todpr,
1805 (u32 __user *)reg->addr);
1806 break;
1807 case KVM_REG_S390_EPOCHDIFF:
1808 r = put_user(vcpu->arch.sie_block->epoch,
1809 (u64 __user *)reg->addr);
1810 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001811 case KVM_REG_S390_CPU_TIMER:
David Hildenbrand4287f242016-02-15 09:40:12 +01001812 r = put_user(kvm_s390_get_cpu_timer(vcpu),
Jason J. herne46a6dd12012-05-15 14:15:28 +02001813 (u64 __user *)reg->addr);
1814 break;
1815 case KVM_REG_S390_CLOCK_COMP:
1816 r = put_user(vcpu->arch.sie_block->ckc,
1817 (u64 __user *)reg->addr);
1818 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001819 case KVM_REG_S390_PFTOKEN:
1820 r = put_user(vcpu->arch.pfault_token,
1821 (u64 __user *)reg->addr);
1822 break;
1823 case KVM_REG_S390_PFCOMPARE:
1824 r = put_user(vcpu->arch.pfault_compare,
1825 (u64 __user *)reg->addr);
1826 break;
1827 case KVM_REG_S390_PFSELECT:
1828 r = put_user(vcpu->arch.pfault_select,
1829 (u64 __user *)reg->addr);
1830 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001831 case KVM_REG_S390_PP:
1832 r = put_user(vcpu->arch.sie_block->pp,
1833 (u64 __user *)reg->addr);
1834 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001835 case KVM_REG_S390_GBEA:
1836 r = put_user(vcpu->arch.sie_block->gbea,
1837 (u64 __user *)reg->addr);
1838 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001839 default:
1840 break;
1841 }
1842
1843 return r;
1844}
1845
1846static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
1847 struct kvm_one_reg *reg)
1848{
1849 int r = -EINVAL;
David Hildenbrand4287f242016-02-15 09:40:12 +01001850 __u64 val;
Carsten Otte14eebd92012-05-15 14:15:26 +02001851
1852 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001853 case KVM_REG_S390_TODPR:
1854 r = get_user(vcpu->arch.sie_block->todpr,
1855 (u32 __user *)reg->addr);
1856 break;
1857 case KVM_REG_S390_EPOCHDIFF:
1858 r = get_user(vcpu->arch.sie_block->epoch,
1859 (u64 __user *)reg->addr);
1860 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001861 case KVM_REG_S390_CPU_TIMER:
David Hildenbrand4287f242016-02-15 09:40:12 +01001862 r = get_user(val, (u64 __user *)reg->addr);
1863 if (!r)
1864 kvm_s390_set_cpu_timer(vcpu, val);
Jason J. herne46a6dd12012-05-15 14:15:28 +02001865 break;
1866 case KVM_REG_S390_CLOCK_COMP:
1867 r = get_user(vcpu->arch.sie_block->ckc,
1868 (u64 __user *)reg->addr);
1869 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001870 case KVM_REG_S390_PFTOKEN:
1871 r = get_user(vcpu->arch.pfault_token,
1872 (u64 __user *)reg->addr);
David Hildenbrand9fbd8082014-10-09 15:01:38 +02001873 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1874 kvm_clear_async_pf_completion_queue(vcpu);
Dominik Dingel536336c2013-09-30 10:55:33 +02001875 break;
1876 case KVM_REG_S390_PFCOMPARE:
1877 r = get_user(vcpu->arch.pfault_compare,
1878 (u64 __user *)reg->addr);
1879 break;
1880 case KVM_REG_S390_PFSELECT:
1881 r = get_user(vcpu->arch.pfault_select,
1882 (u64 __user *)reg->addr);
1883 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001884 case KVM_REG_S390_PP:
1885 r = get_user(vcpu->arch.sie_block->pp,
1886 (u64 __user *)reg->addr);
1887 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001888 case KVM_REG_S390_GBEA:
1889 r = get_user(vcpu->arch.sie_block->gbea,
1890 (u64 __user *)reg->addr);
1891 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001892 default:
1893 break;
1894 }
1895
1896 return r;
1897}
Christoffer Dallb6d33832012-03-08 16:44:24 -05001898
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001899static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
1900{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001901 kvm_s390_vcpu_initial_reset(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001902 return 0;
1903}
1904
1905int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1906{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001907 memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001908 return 0;
1909}
1910
1911int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1912{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001913 memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001914 return 0;
1915}
1916
1917int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1918 struct kvm_sregs *sregs)
1919{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001920 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001921 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
Christian Borntraeger59674c12012-01-11 11:20:33 +01001922 restore_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001923 return 0;
1924}
1925
1926int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1927 struct kvm_sregs *sregs)
1928{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001929 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001930 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001931 return 0;
1932}
1933
1934int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1935{
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001936 /* make sure the new values will be lazily loaded */
1937 save_fpu_regs();
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001938 if (test_fp_ctl(fpu->fpc))
1939 return -EINVAL;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001940 current->thread.fpu.fpc = fpu->fpc;
1941 if (MACHINE_HAS_VX)
1942 convert_fp_to_vx(current->thread.fpu.vxrs, (freg_t *)fpu->fprs);
1943 else
1944 memcpy(current->thread.fpu.fprs, &fpu->fprs, sizeof(fpu->fprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001945 return 0;
1946}
1947
1948int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1949{
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001950 /* make sure we have the latest values */
1951 save_fpu_regs();
1952 if (MACHINE_HAS_VX)
1953 convert_vx_to_fp((freg_t *)fpu->fprs, current->thread.fpu.vxrs);
1954 else
1955 memcpy(fpu->fprs, current->thread.fpu.fprs, sizeof(fpu->fprs));
1956 fpu->fpc = current->thread.fpu.fpc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001957 return 0;
1958}
1959
1960static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
1961{
1962 int rc = 0;
1963
David Hildenbrand7a42fdc2014-05-05 16:26:19 +02001964 if (!is_vcpu_stopped(vcpu))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001965 rc = -EBUSY;
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001966 else {
1967 vcpu->run->psw_mask = psw.mask;
1968 vcpu->run->psw_addr = psw.addr;
1969 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001970 return rc;
1971}
1972
1973int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1974 struct kvm_translation *tr)
1975{
1976 return -EINVAL; /* not implemented yet */
1977}
1978
David Hildenbrand27291e22014-01-23 12:26:52 +01001979#define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
1980 KVM_GUESTDBG_USE_HW_BP | \
1981 KVM_GUESTDBG_ENABLE)
1982
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001983int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1984 struct kvm_guest_debug *dbg)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001985{
David Hildenbrand27291e22014-01-23 12:26:52 +01001986 int rc = 0;
1987
1988 vcpu->guest_debug = 0;
1989 kvm_s390_clear_bp_data(vcpu);
1990
David Hildenbrand2de3bfc2014-05-20 17:25:20 +02001991 if (dbg->control & ~VALID_GUESTDBG_FLAGS)
David Hildenbrand27291e22014-01-23 12:26:52 +01001992 return -EINVAL;
1993
1994 if (dbg->control & KVM_GUESTDBG_ENABLE) {
1995 vcpu->guest_debug = dbg->control;
1996 /* enforce guest PER */
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001997 atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand27291e22014-01-23 12:26:52 +01001998
1999 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
2000 rc = kvm_s390_import_bp_data(vcpu, dbg);
2001 } else {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002002 atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand27291e22014-01-23 12:26:52 +01002003 vcpu->arch.guestdbg.last_bp = 0;
2004 }
2005
2006 if (rc) {
2007 vcpu->guest_debug = 0;
2008 kvm_s390_clear_bp_data(vcpu);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002009 atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand27291e22014-01-23 12:26:52 +01002010 }
2011
2012 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002013}
2014
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002015int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
2016 struct kvm_mp_state *mp_state)
2017{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002018 /* CHECK_STOP and LOAD are not supported yet */
2019 return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
2020 KVM_MP_STATE_OPERATING;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002021}
2022
2023int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
2024 struct kvm_mp_state *mp_state)
2025{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002026 int rc = 0;
2027
2028 /* user space knows about this interface - let it control the state */
2029 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
2030
2031 switch (mp_state->mp_state) {
2032 case KVM_MP_STATE_STOPPED:
2033 kvm_s390_vcpu_stop(vcpu);
2034 break;
2035 case KVM_MP_STATE_OPERATING:
2036 kvm_s390_vcpu_start(vcpu);
2037 break;
2038 case KVM_MP_STATE_LOAD:
2039 case KVM_MP_STATE_CHECK_STOP:
2040 /* fall through - CHECK_STOP and LOAD are not supported yet */
2041 default:
2042 rc = -ENXIO;
2043 }
2044
2045 return rc;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002046}
2047
David Hildenbrand8ad35752014-03-14 11:00:21 +01002048static bool ibs_enabled(struct kvm_vcpu *vcpu)
2049{
2050 return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
2051}
2052
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002053static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
2054{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002055retry:
Christian Borntraeger8e236542015-04-09 13:49:04 +02002056 kvm_s390_vcpu_request_handled(vcpu);
Christian Borntraeger586b7cc2015-07-28 15:03:05 +02002057 if (!vcpu->requests)
2058 return 0;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002059 /*
2060 * We use MMU_RELOAD just to re-arm the ipte notifier for the
2061 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
2062 * This ensures that the ipte instruction for this request has
2063 * already finished. We might race against a second unmapper that
2064 * wants to set the blocking bit. Lets just retry the request loop.
2065 */
David Hildenbrand8ad35752014-03-14 11:00:21 +01002066 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002067 int rc;
2068 rc = gmap_ipte_notify(vcpu->arch.gmap,
Michael Muellerfda902c2014-05-13 16:58:30 +02002069 kvm_s390_get_prefix(vcpu),
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002070 PAGE_SIZE * 2);
2071 if (rc)
2072 return rc;
David Hildenbrand8ad35752014-03-14 11:00:21 +01002073 goto retry;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002074 }
David Hildenbrand8ad35752014-03-14 11:00:21 +01002075
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002076 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
2077 vcpu->arch.sie_block->ihcpu = 0xffff;
2078 goto retry;
2079 }
2080
David Hildenbrand8ad35752014-03-14 11:00:21 +01002081 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
2082 if (!ibs_enabled(vcpu)) {
2083 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002084 atomic_or(CPUSTAT_IBS,
David Hildenbrand8ad35752014-03-14 11:00:21 +01002085 &vcpu->arch.sie_block->cpuflags);
2086 }
2087 goto retry;
2088 }
2089
2090 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
2091 if (ibs_enabled(vcpu)) {
2092 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002093 atomic_andnot(CPUSTAT_IBS,
David Hildenbrand8ad35752014-03-14 11:00:21 +01002094 &vcpu->arch.sie_block->cpuflags);
2095 }
2096 goto retry;
2097 }
2098
David Hildenbrand0759d062014-05-13 16:54:32 +02002099 /* nothing to do, just clear the request */
2100 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
2101
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002102 return 0;
2103}
2104
David Hildenbrand25ed1672015-05-12 09:49:14 +02002105void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
2106{
2107 struct kvm_vcpu *vcpu;
2108 int i;
2109
2110 mutex_lock(&kvm->lock);
2111 preempt_disable();
2112 kvm->arch.epoch = tod - get_tod_clock();
2113 kvm_s390_vcpu_block_all(kvm);
2114 kvm_for_each_vcpu(i, vcpu, kvm)
2115 vcpu->arch.sie_block->epoch = kvm->arch.epoch;
2116 kvm_s390_vcpu_unblock_all(kvm);
2117 preempt_enable();
2118 mutex_unlock(&kvm->lock);
2119}
2120
Thomas Huthfa576c52014-05-06 17:20:16 +02002121/**
2122 * kvm_arch_fault_in_page - fault-in guest page if necessary
2123 * @vcpu: The corresponding virtual cpu
2124 * @gpa: Guest physical address
2125 * @writable: Whether the page should be writable or not
2126 *
2127 * Make sure that a guest page has been faulted-in on the host.
2128 *
2129 * Return: Zero on success, negative error code otherwise.
2130 */
2131long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002132{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002133 return gmap_fault(vcpu->arch.gmap, gpa,
2134 writable ? FAULT_FLAG_WRITE : 0);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002135}
2136
Dominik Dingel3c038e62013-10-07 17:11:48 +02002137static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
2138 unsigned long token)
2139{
2140 struct kvm_s390_interrupt inti;
Jens Freimann383d0b02014-07-29 15:11:49 +02002141 struct kvm_s390_irq irq;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002142
2143 if (start_token) {
Jens Freimann383d0b02014-07-29 15:11:49 +02002144 irq.u.ext.ext_params2 = token;
2145 irq.type = KVM_S390_INT_PFAULT_INIT;
2146 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
Dominik Dingel3c038e62013-10-07 17:11:48 +02002147 } else {
2148 inti.type = KVM_S390_INT_PFAULT_DONE;
Jens Freimann383d0b02014-07-29 15:11:49 +02002149 inti.parm64 = token;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002150 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
2151 }
2152}
2153
2154void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
2155 struct kvm_async_pf *work)
2156{
2157 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
2158 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
2159}
2160
2161void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
2162 struct kvm_async_pf *work)
2163{
2164 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
2165 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
2166}
2167
2168void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
2169 struct kvm_async_pf *work)
2170{
2171 /* s390 will always inject the page directly */
2172}
2173
2174bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
2175{
2176 /*
2177 * s390 will always inject the page directly,
2178 * but we still want check_async_completion to cleanup
2179 */
2180 return true;
2181}
2182
2183static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
2184{
2185 hva_t hva;
2186 struct kvm_arch_async_pf arch;
2187 int rc;
2188
2189 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
2190 return 0;
2191 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
2192 vcpu->arch.pfault_compare)
2193 return 0;
2194 if (psw_extint_disabled(vcpu))
2195 return 0;
David Hildenbrand9a022062014-08-05 17:40:47 +02002196 if (kvm_s390_vcpu_has_irq(vcpu, 0))
Dominik Dingel3c038e62013-10-07 17:11:48 +02002197 return 0;
2198 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
2199 return 0;
2200 if (!vcpu->arch.gmap->pfault_enabled)
2201 return 0;
2202
Heiko Carstens81480cc2014-01-01 16:36:07 +01002203 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
2204 hva += current->thread.gmap_addr & ~PAGE_MASK;
2205 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
Dominik Dingel3c038e62013-10-07 17:11:48 +02002206 return 0;
2207
2208 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
2209 return rc;
2210}
2211
Thomas Huth3fb4c402013-09-12 10:33:43 +02002212static int vcpu_pre_run(struct kvm_vcpu *vcpu)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002213{
Thomas Huth3fb4c402013-09-12 10:33:43 +02002214 int rc, cpuflags;
Carsten Ottee168bf82012-01-04 10:25:22 +01002215
Dominik Dingel3c038e62013-10-07 17:11:48 +02002216 /*
2217 * On s390 notifications for arriving pages will be delivered directly
2218 * to the guest but the house keeping for completed pfaults is
2219 * handled outside the worker.
2220 */
2221 kvm_check_async_pf_completion(vcpu);
2222
Christian Borntraeger7ec7c8c2015-12-02 14:27:03 +01002223 vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
2224 vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002225
2226 if (need_resched())
2227 schedule();
2228
Martin Schwidefskyd3a73ac2014-04-15 12:55:07 +02002229 if (test_cpu_flag(CIF_MCCK_PENDING))
Christian Borntraeger71cde582008-05-21 13:37:34 +02002230 s390_handle_mcck();
2231
Jens Freimann79395032014-04-17 10:10:30 +02002232 if (!kvm_is_ucontrol(vcpu->kvm)) {
2233 rc = kvm_s390_deliver_pending_interrupts(vcpu);
2234 if (rc)
2235 return rc;
2236 }
Carsten Otte0ff31862008-05-21 13:37:37 +02002237
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002238 rc = kvm_s390_handle_requests(vcpu);
2239 if (rc)
2240 return rc;
2241
David Hildenbrand27291e22014-01-23 12:26:52 +01002242 if (guestdbg_enabled(vcpu)) {
2243 kvm_s390_backup_guest_per_regs(vcpu);
2244 kvm_s390_patch_guest_per_regs(vcpu);
2245 }
2246
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002247 vcpu->arch.sie_block->icptcode = 0;
Thomas Huth3fb4c402013-09-12 10:33:43 +02002248 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
2249 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
2250 trace_kvm_s390_sie_enter(vcpu, cpuflags);
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02002251
Thomas Huth3fb4c402013-09-12 10:33:43 +02002252 return 0;
2253}
2254
Thomas Huth492d8642015-02-10 16:11:01 +01002255static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
2256{
David Hildenbrand56317922016-01-12 17:37:58 +01002257 struct kvm_s390_pgm_info pgm_info = {
2258 .code = PGM_ADDRESSING,
2259 };
2260 u8 opcode, ilen;
Thomas Huth492d8642015-02-10 16:11:01 +01002261 int rc;
2262
2263 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
2264 trace_kvm_s390_sie_fault(vcpu);
2265
2266 /*
2267 * We want to inject an addressing exception, which is defined as a
2268 * suppressing or terminating exception. However, since we came here
2269 * by a DAT access exception, the PSW still points to the faulting
2270 * instruction since DAT exceptions are nullifying. So we've got
2271 * to look up the current opcode to get the length of the instruction
2272 * to be able to forward the PSW.
2273 */
David Hildenbrand65977322015-11-16 16:17:45 +01002274 rc = read_guest_instr(vcpu, &opcode, 1);
David Hildenbrand56317922016-01-12 17:37:58 +01002275 ilen = insn_length(opcode);
David Hildenbrand9b0d7212016-01-12 17:40:54 +01002276 if (rc < 0) {
2277 return rc;
2278 } else if (rc) {
2279 /* Instruction-Fetching Exceptions - we can't detect the ilen.
2280 * Forward by arbitrary ilc, injection will take care of
2281 * nullification if necessary.
2282 */
2283 pgm_info = vcpu->arch.pgm;
2284 ilen = 4;
2285 }
David Hildenbrand56317922016-01-12 17:37:58 +01002286 pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
2287 kvm_s390_forward_psw(vcpu, ilen);
2288 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
Thomas Huth492d8642015-02-10 16:11:01 +01002289}
2290
Thomas Huth3fb4c402013-09-12 10:33:43 +02002291static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
2292{
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02002293 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
2294 vcpu->arch.sie_block->icptcode);
2295 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
2296
David Hildenbrand27291e22014-01-23 12:26:52 +01002297 if (guestdbg_enabled(vcpu))
2298 kvm_s390_restore_guest_per_regs(vcpu);
2299
Christian Borntraeger7ec7c8c2015-12-02 14:27:03 +01002300 vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
2301 vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
David Hildenbrand71f116b2015-10-19 16:24:28 +02002302
2303 if (vcpu->arch.sie_block->icptcode > 0) {
2304 int rc = kvm_handle_sie_intercept(vcpu);
2305
2306 if (rc != -EOPNOTSUPP)
2307 return rc;
2308 vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
2309 vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
2310 vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
2311 vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
2312 return -EREMOTE;
2313 } else if (exit_reason != -EFAULT) {
2314 vcpu->stat.exit_null++;
2315 return 0;
Thomas Huth210b16072013-09-19 16:26:18 +02002316 } else if (kvm_is_ucontrol(vcpu->kvm)) {
2317 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
2318 vcpu->run->s390_ucontrol.trans_exc_code =
2319 current->thread.gmap_addr;
2320 vcpu->run->s390_ucontrol.pgm_code = 0x10;
David Hildenbrand71f116b2015-10-19 16:24:28 +02002321 return -EREMOTE;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002322 } else if (current->thread.gmap_pfault) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02002323 trace_kvm_s390_major_guest_pfault(vcpu);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002324 current->thread.gmap_pfault = 0;
David Hildenbrand71f116b2015-10-19 16:24:28 +02002325 if (kvm_arch_setup_async_pf(vcpu))
2326 return 0;
2327 return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002328 }
David Hildenbrand71f116b2015-10-19 16:24:28 +02002329 return vcpu_post_run_fault_in_sie(vcpu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002330}
2331
2332static int __vcpu_run(struct kvm_vcpu *vcpu)
2333{
2334 int rc, exit_reason;
2335
Thomas Huth800c1062013-09-12 10:33:45 +02002336 /*
2337 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2338 * ning the guest), so that memslots (and other stuff) are protected
2339 */
2340 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2341
Thomas Hutha76ccff2013-09-12 10:33:44 +02002342 do {
2343 rc = vcpu_pre_run(vcpu);
2344 if (rc)
2345 break;
Thomas Huth3fb4c402013-09-12 10:33:43 +02002346
Thomas Huth800c1062013-09-12 10:33:45 +02002347 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Thomas Hutha76ccff2013-09-12 10:33:44 +02002348 /*
2349 * As PF_VCPU will be used in fault handler, between
2350 * guest_enter and guest_exit should be no uaccess.
2351 */
Christian Borntraeger0097d122015-04-30 13:43:30 +02002352 local_irq_disable();
2353 __kvm_guest_enter();
David Hildenbranddb0758b2016-02-15 09:42:25 +01002354 __disable_cpu_timer_accounting(vcpu);
Christian Borntraeger0097d122015-04-30 13:43:30 +02002355 local_irq_enable();
Thomas Hutha76ccff2013-09-12 10:33:44 +02002356 exit_reason = sie64a(vcpu->arch.sie_block,
2357 vcpu->run->s.regs.gprs);
Christian Borntraeger0097d122015-04-30 13:43:30 +02002358 local_irq_disable();
David Hildenbranddb0758b2016-02-15 09:42:25 +01002359 __enable_cpu_timer_accounting(vcpu);
Christian Borntraeger0097d122015-04-30 13:43:30 +02002360 __kvm_guest_exit();
2361 local_irq_enable();
Thomas Huth800c1062013-09-12 10:33:45 +02002362 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002363
Thomas Hutha76ccff2013-09-12 10:33:44 +02002364 rc = vcpu_post_run(vcpu, exit_reason);
David Hildenbrand27291e22014-01-23 12:26:52 +01002365 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002366
Thomas Huth800c1062013-09-12 10:33:45 +02002367 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Carsten Ottee168bf82012-01-04 10:25:22 +01002368 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002369}
2370
David Hildenbrandb028ee32014-07-17 10:47:43 +02002371static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2372{
2373 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2374 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2375 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2376 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2377 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2378 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002379 /* some control register changes require a tlb flush */
2380 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002381 }
2382 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
David Hildenbrand4287f242016-02-15 09:40:12 +01002383 kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002384 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2385 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2386 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2387 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2388 }
2389 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2390 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2391 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2392 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
David Hildenbrand9fbd8082014-10-09 15:01:38 +02002393 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
2394 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002395 }
2396 kvm_run->kvm_dirty_regs = 0;
2397}
2398
2399static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2400{
2401 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
2402 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
2403 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
2404 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
David Hildenbrand4287f242016-02-15 09:40:12 +01002405 kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002406 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
2407 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
2408 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
2409 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
2410 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
2411 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
2412 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
2413}
2414
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002415int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2416{
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002417 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002418 sigset_t sigsaved;
2419
David Hildenbrand27291e22014-01-23 12:26:52 +01002420 if (guestdbg_exit_pending(vcpu)) {
2421 kvm_s390_prepare_debug_exit(vcpu);
2422 return 0;
2423 }
2424
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002425 if (vcpu->sigset_active)
2426 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2427
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002428 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
2429 kvm_s390_vcpu_start(vcpu);
2430 } else if (is_vcpu_stopped(vcpu)) {
David Hildenbrandea2cdd22015-05-20 13:24:02 +02002431 pr_err_ratelimited("can't run stopped vcpu %d\n",
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002432 vcpu->vcpu_id);
2433 return -EINVAL;
2434 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002435
David Hildenbrandb028ee32014-07-17 10:47:43 +02002436 sync_regs(vcpu, kvm_run);
David Hildenbranddb0758b2016-02-15 09:42:25 +01002437 enable_cpu_timer_accounting(vcpu);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002438
Heiko Carstensdab4079d2009-06-12 10:26:32 +02002439 might_fault();
Thomas Hutha76ccff2013-09-12 10:33:44 +02002440 rc = __vcpu_run(vcpu);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +02002441
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002442 if (signal_pending(current) && !rc) {
2443 kvm_run->exit_reason = KVM_EXIT_INTR;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002444 rc = -EINTR;
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002445 }
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002446
David Hildenbrand27291e22014-01-23 12:26:52 +01002447 if (guestdbg_exit_pending(vcpu) && !rc) {
2448 kvm_s390_prepare_debug_exit(vcpu);
2449 rc = 0;
2450 }
2451
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002452 if (rc == -EREMOTE) {
David Hildenbrand71f116b2015-10-19 16:24:28 +02002453 /* userspace support is needed, kvm_run has been prepared */
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002454 rc = 0;
2455 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002456
David Hildenbranddb0758b2016-02-15 09:42:25 +01002457 disable_cpu_timer_accounting(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002458 store_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002459
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002460 if (vcpu->sigset_active)
2461 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2462
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002463 vcpu->stat.exit_userspace++;
Heiko Carstens7e8e6ab2008-04-04 15:12:35 +02002464 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002465}
2466
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002467/*
2468 * store status at address
2469 * we use have two special cases:
2470 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2471 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2472 */
Heiko Carstensd0bce602014-01-01 16:45:58 +01002473int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002474{
Carsten Otte092670c2011-07-24 10:48:22 +02002475 unsigned char archmode = 1;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002476 freg_t fprs[NUM_FPRS];
Michael Muellerfda902c2014-05-13 16:58:30 +02002477 unsigned int px;
David Hildenbrand4287f242016-02-15 09:40:12 +01002478 u64 clkcomp, cputm;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002479 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002480
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002481 px = kvm_s390_get_prefix(vcpu);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002482 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
2483 if (write_guest_abs(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002484 return -EFAULT;
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002485 gpa = 0;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002486 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
2487 if (write_guest_real(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002488 return -EFAULT;
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002489 gpa = px;
2490 } else
2491 gpa -= __LC_FPREGS_SAVE_AREA;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002492
2493 /* manually convert vector registers if necessary */
2494 if (MACHINE_HAS_VX) {
David Hildenbrand9522b372016-03-08 12:24:30 +01002495 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002496 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
2497 fprs, 128);
2498 } else {
2499 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
David Hildenbrand6fd8e672016-01-18 14:46:34 +01002500 vcpu->run->s.regs.fprs, 128);
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002501 }
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002502 rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002503 vcpu->run->s.regs.gprs, 128);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002504 rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002505 &vcpu->arch.sie_block->gpsw, 16);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002506 rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
Michael Muellerfda902c2014-05-13 16:58:30 +02002507 &px, 4);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002508 rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002509 &vcpu->run->s.regs.fpc, 4);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002510 rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002511 &vcpu->arch.sie_block->todpr, 4);
David Hildenbrand4287f242016-02-15 09:40:12 +01002512 cputm = kvm_s390_get_cpu_timer(vcpu);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002513 rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
David Hildenbrand4287f242016-02-15 09:40:12 +01002514 &cputm, 8);
Thomas Huth178bd782013-11-13 20:28:18 +01002515 clkcomp = vcpu->arch.sie_block->ckc >> 8;
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002516 rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002517 &clkcomp, 8);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002518 rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002519 &vcpu->run->s.regs.acrs, 64);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002520 rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002521 &vcpu->arch.sie_block->gcr, 128);
2522 return rc ? -EFAULT : 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002523}
2524
Thomas Huthe8798922013-11-06 15:46:33 +01002525int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2526{
2527 /*
2528 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
2529 * copying in vcpu load/put. Lets update our copies before we save
2530 * it into the save area
2531 */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02002532 save_fpu_regs();
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002533 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
Thomas Huthe8798922013-11-06 15:46:33 +01002534 save_access_regs(vcpu->run->s.regs.acrs);
2535
2536 return kvm_s390_store_status_unloaded(vcpu, addr);
2537}
2538
Eric Farmanbc17de72014-04-14 16:01:09 -04002539/*
2540 * store additional status at address
2541 */
2542int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
2543 unsigned long gpa)
2544{
2545 /* Only bits 0-53 are used for address formation */
2546 if (!(gpa & ~0x3ff))
2547 return 0;
2548
2549 return write_guest_abs(vcpu, gpa & ~0x3ff,
2550 (void *)&vcpu->run->s.regs.vrs, 512);
2551}
2552
2553int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr)
2554{
2555 if (!test_kvm_facility(vcpu->kvm, 129))
2556 return 0;
2557
2558 /*
2559 * The guest VXRS are in the host VXRs due to the lazy
Hendrik Brueckner9977e882015-06-10 12:53:42 +02002560 * copying in vcpu load/put. We can simply call save_fpu_regs()
2561 * to save the current register state because we are in the
2562 * middle of a load/put cycle.
2563 *
2564 * Let's update our copies before we save it into the save area.
Eric Farmanbc17de72014-04-14 16:01:09 -04002565 */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02002566 save_fpu_regs();
Eric Farmanbc17de72014-04-14 16:01:09 -04002567
2568 return kvm_s390_store_adtl_status_unloaded(vcpu, addr);
2569}
2570
David Hildenbrand8ad35752014-03-14 11:00:21 +01002571static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2572{
2573 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02002574 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002575}
2576
2577static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
2578{
2579 unsigned int i;
2580 struct kvm_vcpu *vcpu;
2581
2582 kvm_for_each_vcpu(i, vcpu, kvm) {
2583 __disable_ibs_on_vcpu(vcpu);
2584 }
2585}
2586
2587static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2588{
2589 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02002590 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002591}
2592
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002593void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
2594{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002595 int i, online_vcpus, started_vcpus = 0;
2596
2597 if (!is_vcpu_stopped(vcpu))
2598 return;
2599
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002600 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002601 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002602 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002603 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2604
2605 for (i = 0; i < online_vcpus; i++) {
2606 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
2607 started_vcpus++;
2608 }
2609
2610 if (started_vcpus == 0) {
2611 /* we're the only active VCPU -> speed it up */
2612 __enable_ibs_on_vcpu(vcpu);
2613 } else if (started_vcpus == 1) {
2614 /*
2615 * As we are starting a second VCPU, we have to disable
2616 * the IBS facility on all VCPUs to remove potentially
2617 * oustanding ENABLE requests.
2618 */
2619 __disable_ibs_on_all_vcpus(vcpu->kvm);
2620 }
2621
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002622 atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002623 /*
2624 * Another VCPU might have used IBS while we were offline.
2625 * Let's play safe and flush the VCPU at startup.
2626 */
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002627 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002628 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002629 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002630}
2631
2632void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
2633{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002634 int i, online_vcpus, started_vcpus = 0;
2635 struct kvm_vcpu *started_vcpu = NULL;
2636
2637 if (is_vcpu_stopped(vcpu))
2638 return;
2639
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002640 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002641 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002642 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002643 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2644
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002645 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
David Hildenbrand6cddd432014-10-15 16:48:53 +02002646 kvm_s390_clear_stop_irq(vcpu);
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002647
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002648 atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002649 __disable_ibs_on_vcpu(vcpu);
2650
2651 for (i = 0; i < online_vcpus; i++) {
2652 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
2653 started_vcpus++;
2654 started_vcpu = vcpu->kvm->vcpus[i];
2655 }
2656 }
2657
2658 if (started_vcpus == 1) {
2659 /*
2660 * As we only have one VCPU left, we want to enable the
2661 * IBS facility for that VCPU to speed it up.
2662 */
2663 __enable_ibs_on_vcpu(started_vcpu);
2664 }
2665
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002666 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002667 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002668}
2669
Cornelia Huckd6712df2012-12-20 15:32:11 +01002670static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2671 struct kvm_enable_cap *cap)
2672{
2673 int r;
2674
2675 if (cap->flags)
2676 return -EINVAL;
2677
2678 switch (cap->cap) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002679 case KVM_CAP_S390_CSS_SUPPORT:
2680 if (!vcpu->kvm->arch.css_support) {
2681 vcpu->kvm->arch.css_support = 1;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +02002682 VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002683 trace_kvm_s390_enable_css(vcpu->kvm);
2684 }
2685 r = 0;
2686 break;
Cornelia Huckd6712df2012-12-20 15:32:11 +01002687 default:
2688 r = -EINVAL;
2689 break;
2690 }
2691 return r;
2692}
2693
Thomas Huth41408c22015-02-06 15:01:21 +01002694static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
2695 struct kvm_s390_mem_op *mop)
2696{
2697 void __user *uaddr = (void __user *)mop->buf;
2698 void *tmpbuf = NULL;
2699 int r, srcu_idx;
2700 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
2701 | KVM_S390_MEMOP_F_CHECK_ONLY;
2702
2703 if (mop->flags & ~supported_flags)
2704 return -EINVAL;
2705
2706 if (mop->size > MEM_OP_MAX_SIZE)
2707 return -E2BIG;
2708
2709 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
2710 tmpbuf = vmalloc(mop->size);
2711 if (!tmpbuf)
2712 return -ENOMEM;
2713 }
2714
2715 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2716
2717 switch (mop->op) {
2718 case KVM_S390_MEMOP_LOGICAL_READ:
2719 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
David Hildenbrand92c96322015-11-16 15:42:11 +01002720 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
2721 mop->size, GACC_FETCH);
Thomas Huth41408c22015-02-06 15:01:21 +01002722 break;
2723 }
2724 r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2725 if (r == 0) {
2726 if (copy_to_user(uaddr, tmpbuf, mop->size))
2727 r = -EFAULT;
2728 }
2729 break;
2730 case KVM_S390_MEMOP_LOGICAL_WRITE:
2731 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
David Hildenbrand92c96322015-11-16 15:42:11 +01002732 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
2733 mop->size, GACC_STORE);
Thomas Huth41408c22015-02-06 15:01:21 +01002734 break;
2735 }
2736 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
2737 r = -EFAULT;
2738 break;
2739 }
2740 r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2741 break;
2742 default:
2743 r = -EINVAL;
2744 }
2745
2746 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2747
2748 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
2749 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
2750
2751 vfree(tmpbuf);
2752 return r;
2753}
2754
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002755long kvm_arch_vcpu_ioctl(struct file *filp,
2756 unsigned int ioctl, unsigned long arg)
2757{
2758 struct kvm_vcpu *vcpu = filp->private_data;
2759 void __user *argp = (void __user *)arg;
Thomas Huth800c1062013-09-12 10:33:45 +02002760 int idx;
Avi Kivitybc923cc2010-05-13 12:21:46 +03002761 long r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002762
Avi Kivity93736622010-05-13 12:35:17 +03002763 switch (ioctl) {
Jens Freimann47b43c52014-11-11 20:57:06 +01002764 case KVM_S390_IRQ: {
2765 struct kvm_s390_irq s390irq;
2766
2767 r = -EFAULT;
2768 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
2769 break;
2770 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
2771 break;
2772 }
Avi Kivity93736622010-05-13 12:35:17 +03002773 case KVM_S390_INTERRUPT: {
Carsten Otteba5c1e92008-03-25 18:47:26 +01002774 struct kvm_s390_interrupt s390int;
Jens Freimann383d0b02014-07-29 15:11:49 +02002775 struct kvm_s390_irq s390irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002776
Avi Kivity93736622010-05-13 12:35:17 +03002777 r = -EFAULT;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002778 if (copy_from_user(&s390int, argp, sizeof(s390int)))
Avi Kivity93736622010-05-13 12:35:17 +03002779 break;
Jens Freimann383d0b02014-07-29 15:11:49 +02002780 if (s390int_to_s390irq(&s390int, &s390irq))
2781 return -EINVAL;
2782 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
Avi Kivity93736622010-05-13 12:35:17 +03002783 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002784 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002785 case KVM_S390_STORE_STATUS:
Thomas Huth800c1062013-09-12 10:33:45 +02002786 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002787 r = kvm_s390_vcpu_store_status(vcpu, arg);
Thomas Huth800c1062013-09-12 10:33:45 +02002788 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002789 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002790 case KVM_S390_SET_INITIAL_PSW: {
2791 psw_t psw;
2792
Avi Kivitybc923cc2010-05-13 12:21:46 +03002793 r = -EFAULT;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002794 if (copy_from_user(&psw, argp, sizeof(psw)))
Avi Kivitybc923cc2010-05-13 12:21:46 +03002795 break;
2796 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
2797 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002798 }
2799 case KVM_S390_INITIAL_RESET:
Avi Kivitybc923cc2010-05-13 12:21:46 +03002800 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
2801 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02002802 case KVM_SET_ONE_REG:
2803 case KVM_GET_ONE_REG: {
2804 struct kvm_one_reg reg;
2805 r = -EFAULT;
2806 if (copy_from_user(&reg, argp, sizeof(reg)))
2807 break;
2808 if (ioctl == KVM_SET_ONE_REG)
2809 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
2810 else
2811 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
2812 break;
2813 }
Carsten Otte27e03932012-01-04 10:25:21 +01002814#ifdef CONFIG_KVM_S390_UCONTROL
2815 case KVM_S390_UCAS_MAP: {
2816 struct kvm_s390_ucas_mapping ucasmap;
2817
2818 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2819 r = -EFAULT;
2820 break;
2821 }
2822
2823 if (!kvm_is_ucontrol(vcpu->kvm)) {
2824 r = -EINVAL;
2825 break;
2826 }
2827
2828 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
2829 ucasmap.vcpu_addr, ucasmap.length);
2830 break;
2831 }
2832 case KVM_S390_UCAS_UNMAP: {
2833 struct kvm_s390_ucas_mapping ucasmap;
2834
2835 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2836 r = -EFAULT;
2837 break;
2838 }
2839
2840 if (!kvm_is_ucontrol(vcpu->kvm)) {
2841 r = -EINVAL;
2842 break;
2843 }
2844
2845 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
2846 ucasmap.length);
2847 break;
2848 }
2849#endif
Carsten Otteccc79102012-01-04 10:25:26 +01002850 case KVM_S390_VCPU_FAULT: {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002851 r = gmap_fault(vcpu->arch.gmap, arg, 0);
Carsten Otteccc79102012-01-04 10:25:26 +01002852 break;
2853 }
Cornelia Huckd6712df2012-12-20 15:32:11 +01002854 case KVM_ENABLE_CAP:
2855 {
2856 struct kvm_enable_cap cap;
2857 r = -EFAULT;
2858 if (copy_from_user(&cap, argp, sizeof(cap)))
2859 break;
2860 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2861 break;
2862 }
Thomas Huth41408c22015-02-06 15:01:21 +01002863 case KVM_S390_MEM_OP: {
2864 struct kvm_s390_mem_op mem_op;
2865
2866 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
2867 r = kvm_s390_guest_mem_op(vcpu, &mem_op);
2868 else
2869 r = -EFAULT;
2870 break;
2871 }
Jens Freimann816c7662014-11-24 17:13:46 +01002872 case KVM_S390_SET_IRQ_STATE: {
2873 struct kvm_s390_irq_state irq_state;
2874
2875 r = -EFAULT;
2876 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2877 break;
2878 if (irq_state.len > VCPU_IRQS_MAX_BUF ||
2879 irq_state.len == 0 ||
2880 irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
2881 r = -EINVAL;
2882 break;
2883 }
2884 r = kvm_s390_set_irq_state(vcpu,
2885 (void __user *) irq_state.buf,
2886 irq_state.len);
2887 break;
2888 }
2889 case KVM_S390_GET_IRQ_STATE: {
2890 struct kvm_s390_irq_state irq_state;
2891
2892 r = -EFAULT;
2893 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2894 break;
2895 if (irq_state.len == 0) {
2896 r = -EINVAL;
2897 break;
2898 }
2899 r = kvm_s390_get_irq_state(vcpu,
2900 (__u8 __user *) irq_state.buf,
2901 irq_state.len);
2902 break;
2903 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002904 default:
Carsten Otte3e6afcf2012-01-04 10:25:30 +01002905 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002906 }
Avi Kivitybc923cc2010-05-13 12:21:46 +03002907 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002908}
2909
Carsten Otte5b1c1492012-01-04 10:25:23 +01002910int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2911{
2912#ifdef CONFIG_KVM_S390_UCONTROL
2913 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
2914 && (kvm_is_ucontrol(vcpu->kvm))) {
2915 vmf->page = virt_to_page(vcpu->arch.sie_block);
2916 get_page(vmf->page);
2917 return 0;
2918 }
2919#endif
2920 return VM_FAULT_SIGBUS;
2921}
2922
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05302923int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2924 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09002925{
2926 return 0;
2927}
2928
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002929/* Section: memory related */
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002930int kvm_arch_prepare_memory_region(struct kvm *kvm,
2931 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002932 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09002933 enum kvm_mr_change change)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002934{
Nick Wangdd2887e2013-03-25 17:22:57 +01002935 /* A few sanity checks. We can have memory slots which have to be
2936 located/ended at a segment boundary (1MB). The memory in userland is
2937 ok to be fragmented into various different vmas. It is okay to mmap()
2938 and munmap() stuff in this slot after doing this call at any time */
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002939
Carsten Otte598841c2011-07-24 10:48:21 +02002940 if (mem->userspace_addr & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002941 return -EINVAL;
2942
Carsten Otte598841c2011-07-24 10:48:21 +02002943 if (mem->memory_size & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002944 return -EINVAL;
2945
Dominik Dingela3a92c32014-12-01 17:24:42 +01002946 if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
2947 return -EINVAL;
2948
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002949 return 0;
2950}
2951
2952void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002953 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002954 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02002955 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002956 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002957{
Carsten Ottef7850c92011-07-24 10:48:23 +02002958 int rc;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002959
Christian Borntraeger2cef4de2013-03-25 17:22:48 +01002960 /* If the basics of the memslot do not change, we do not want
2961 * to update the gmap. Every update causes several unnecessary
2962 * segment translation exceptions. This is usually handled just
2963 * fine by the normal fault handler + gmap, but it will also
2964 * cause faults on the prefix page of running guest CPUs.
2965 */
2966 if (old->userspace_addr == mem->userspace_addr &&
2967 old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
2968 old->npages * PAGE_SIZE == mem->memory_size)
2969 return;
Carsten Otte598841c2011-07-24 10:48:21 +02002970
2971 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
2972 mem->guest_phys_addr, mem->memory_size);
2973 if (rc)
David Hildenbrandea2cdd22015-05-20 13:24:02 +02002974 pr_warn("failed to commit memory region\n");
Carsten Otte598841c2011-07-24 10:48:21 +02002975 return;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002976}
2977
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002978static int __init kvm_s390_init(void)
2979{
David Hildenbrand07197fd2015-01-30 16:01:38 +01002980 if (!sclp.has_sief2) {
2981 pr_info("SIE not available\n");
2982 return -ENODEV;
2983 }
2984
Michael Mueller9d8d5782015-02-02 15:42:51 +01002985 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002986}
2987
2988static void __exit kvm_s390_exit(void)
2989{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002990 kvm_exit();
2991}
2992
2993module_init(kvm_s390_init);
2994module_exit(kvm_s390_exit);
Cornelia Huck566af942013-05-27 18:42:33 +02002995
2996/*
2997 * Enable autoloading of the kvm module.
2998 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
2999 * since x86 takes a different approach.
3000 */
3001#include <linux/miscdevice.h>
3002MODULE_ALIAS_MISCDEV(KVM_MINOR);
3003MODULE_ALIAS("devname:kvm");