blob: 44297ff53b443177b810dbe06fed6a68dfa05a05 [file] [log] [blame]
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * hosting zSeries kernel virtual machines
Heiko Carstensb0c632d2008-03-25 18:47:20 +01003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2008, 2009
Heiko Carstensb0c632d2008-03-25 18:47:20 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 * Heiko Carstens <heiko.carstens@de.ibm.com>
Christian Ehrhardt628eb9b2009-05-25 13:40:51 +020013 * Christian Ehrhardt <ehrhardt@de.ibm.com>
Jason J. Herne15f36eb2012-08-02 10:10:17 -040014 * Jason J. Herne <jjherne@us.ibm.com>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010015 */
16
17#include <linux/compiler.h>
18#include <linux/err.h>
19#include <linux/fs.h>
Christian Borntraegerca872302009-05-12 17:21:49 +020020#include <linux/hrtimer.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010021#include <linux/init.h>
22#include <linux/kvm.h>
23#include <linux/kvm_host.h>
24#include <linux/module.h>
Tony Krowiaka374e892014-09-03 10:13:53 +020025#include <linux/random.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010026#include <linux/slab.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010027#include <linux/timer.h>
Thomas Huth41408c282015-02-06 15:01:21 +010028#include <linux/vmalloc.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010029#include <asm/asm-offsets.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010030#include <asm/lowcore.h>
Fan Zhangfdf03652015-05-13 10:58:41 +020031#include <asm/etr.h>
Heiko Carstensb0c632d2008-03-25 18:47:20 +010032#include <asm/pgtable.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010033#include <asm/gmap.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010034#include <asm/nmi.h>
David Howellsa0616cd2012-03-28 18:30:02 +010035#include <asm/switch_to.h>
Jens Freimann6d3da242013-07-03 15:18:35 +020036#include <asm/isc.h>
Christian Borntraeger1526bf92012-05-15 14:15:25 +020037#include <asm/sclp.h>
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010038#include "kvm-s390.h"
Heiko Carstensb0c632d2008-03-25 18:47:20 +010039#include "gaccess.h"
40
David Hildenbrandea2cdd22015-05-20 13:24:02 +020041#define KMSG_COMPONENT "kvm-s390"
42#undef pr_fmt
43#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
44
Cornelia Huck5786fff2012-07-23 17:20:29 +020045#define CREATE_TRACE_POINTS
46#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020047#include "trace-s390.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020048
Thomas Huth41408c282015-02-06 15:01:21 +010049#define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
Jens Freimann816c7662014-11-24 17:13:46 +010050#define LOCAL_IRQS 32
51#define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
52 (KVM_MAX_VCPUS + LOCAL_IRQS))
Thomas Huth41408c282015-02-06 15:01:21 +010053
Heiko Carstensb0c632d2008-03-25 18:47:20 +010054#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
55
56struct kvm_stats_debugfs_item debugfs_entries[] = {
57 { "userspace_handled", VCPU_STAT(exit_userspace) },
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +020058 { "exit_null", VCPU_STAT(exit_null) },
Christian Borntraeger8f2abe62008-03-25 18:47:23 +010059 { "exit_validity", VCPU_STAT(exit_validity) },
60 { "exit_stop_request", VCPU_STAT(exit_stop_request) },
61 { "exit_external_request", VCPU_STAT(exit_external_request) },
62 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010063 { "exit_instruction", VCPU_STAT(exit_instruction) },
64 { "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
65 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
Janosch Franka011eeb2016-05-09 14:14:01 +020066 { "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
Paolo Bonzinif7819512015-02-04 18:20:58 +010067 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +020068 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +020069 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
David Hildenbrandce2e4f02014-07-11 10:00:43 +020070 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020071 { "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010072 { "instruction_lctl", VCPU_STAT(instruction_lctl) },
David Hildenbrandaba07502014-01-23 10:47:13 +010073 { "instruction_stctl", VCPU_STAT(instruction_stctl) },
74 { "instruction_stctg", VCPU_STAT(instruction_stctg) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010075 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020076 { "deliver_external_call", VCPU_STAT(deliver_external_call) },
Carsten Otteba5c1e92008-03-25 18:47:26 +010077 { "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
78 { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
79 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
80 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
81 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
82 { "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
83 { "exit_wait_state", VCPU_STAT(exit_wait_state) },
Christian Borntraeger69d0d3a2013-06-12 13:54:53 +020084 { "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010085 { "instruction_stidp", VCPU_STAT(instruction_stidp) },
86 { "instruction_spx", VCPU_STAT(instruction_spx) },
87 { "instruction_stpx", VCPU_STAT(instruction_stpx) },
88 { "instruction_stap", VCPU_STAT(instruction_stap) },
89 { "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
Heiko Carstens8a2422342014-01-10 14:33:28 +010090 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010091 { "instruction_stsch", VCPU_STAT(instruction_stsch) },
92 { "instruction_chsc", VCPU_STAT(instruction_chsc) },
Konstantin Weitzb31288f2013-04-17 17:36:29 +020093 { "instruction_essa", VCPU_STAT(instruction_essa) },
Christian Borntraeger453423d2008-03-25 18:47:29 +010094 { "instruction_stsi", VCPU_STAT(instruction_stsi) },
95 { "instruction_stfl", VCPU_STAT(instruction_stfl) },
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +020096 { "instruction_tprot", VCPU_STAT(instruction_tprot) },
Janosch Frank95ca2cb2016-05-23 15:11:58 +020097 { "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +010098 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
Cornelia Huckbd59d3a2011-11-17 11:00:42 +010099 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +0200100 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100101 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200102 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
103 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100104 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200105 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
106 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
Eric Farmancd7b4b62015-02-12 09:06:34 -0500107 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100108 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
109 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
110 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
David Hildenbrand42cb0c92014-05-23 12:25:11 +0200111 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
112 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
113 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
Christian Borntraeger388186b2011-10-30 15:17:03 +0100114 { "diagnose_10", VCPU_STAT(diagnose_10) },
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100115 { "diagnose_44", VCPU_STAT(diagnose_44) },
Konstantin Weitz41628d32012-04-25 15:30:38 +0200116 { "diagnose_9c", VCPU_STAT(diagnose_9c) },
Christian Borntraeger175a5c92015-07-07 15:19:32 +0200117 { "diagnose_258", VCPU_STAT(diagnose_258) },
118 { "diagnose_308", VCPU_STAT(diagnose_308) },
119 { "diagnose_500", VCPU_STAT(diagnose_500) },
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100120 { NULL }
121};
122
Michael Mueller9d8d5782015-02-02 15:42:51 +0100123/* upper facilities limit for kvm */
Alexander Yarygin60a37702016-04-01 15:38:57 +0300124unsigned long kvm_s390_fac_list_mask[16] = {
125 0xffe6000000000000UL,
126 0x005e000000000000UL,
Michael Mueller9d8d5782015-02-02 15:42:51 +0100127};
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100128
Michael Mueller9d8d5782015-02-02 15:42:51 +0100129unsigned long kvm_s390_fac_list_mask_size(void)
Michael Mueller78c4b592013-07-26 15:04:04 +0200130{
Michael Mueller9d8d5782015-02-02 15:42:51 +0100131 BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
132 return ARRAY_SIZE(kvm_s390_fac_list_mask);
Michael Mueller78c4b592013-07-26 15:04:04 +0200133}
134
Michael Mueller9d8d5782015-02-02 15:42:51 +0100135static struct gmap_notifier gmap_notifier;
Christian Borntraeger78f26132015-07-22 15:50:58 +0200136debug_info_t *kvm_s390_dbf;
Michael Mueller9d8d5782015-02-02 15:42:51 +0100137
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100138/* Section: not file related */
Radim Krčmář13a34e02014-08-28 15:13:03 +0200139int kvm_arch_hardware_enable(void)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100140{
141 /* every s390 is virtualization enabled ;-) */
Alexander Graf10474ae2009-09-15 11:37:46 +0200142 return 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100143}
144
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200145static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address);
146
Fan Zhangfdf03652015-05-13 10:58:41 +0200147/*
148 * This callback is executed during stop_machine(). All CPUs are therefore
149 * temporarily stopped. In order not to change guest behavior, we have to
150 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
151 * so a CPU won't be stopped while calculating with the epoch.
152 */
153static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
154 void *v)
155{
156 struct kvm *kvm;
157 struct kvm_vcpu *vcpu;
158 int i;
159 unsigned long long *delta = v;
160
161 list_for_each_entry(kvm, &vm_list, vm_list) {
162 kvm->arch.epoch -= *delta;
163 kvm_for_each_vcpu(i, vcpu, kvm) {
164 vcpu->arch.sie_block->epoch -= *delta;
David Hildenbranddb0758b2016-02-15 09:42:25 +0100165 if (vcpu->arch.cputm_enabled)
166 vcpu->arch.cputm_start += *delta;
Fan Zhangfdf03652015-05-13 10:58:41 +0200167 }
168 }
169 return NOTIFY_OK;
170}
171
172static struct notifier_block kvm_clock_notifier = {
173 .notifier_call = kvm_clock_sync,
174};
175
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100176int kvm_arch_hardware_setup(void)
177{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200178 gmap_notifier.notifier_call = kvm_gmap_notifier;
179 gmap_register_ipte_notifier(&gmap_notifier);
Fan Zhangfdf03652015-05-13 10:58:41 +0200180 atomic_notifier_chain_register(&s390_epoch_delta_notifier,
181 &kvm_clock_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100182 return 0;
183}
184
185void kvm_arch_hardware_unsetup(void)
186{
Christian Borntraeger2c70fe42013-05-17 14:41:36 +0200187 gmap_unregister_ipte_notifier(&gmap_notifier);
Fan Zhangfdf03652015-05-13 10:58:41 +0200188 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
189 &kvm_clock_notifier);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100190}
191
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100192int kvm_arch_init(void *opaque)
193{
Christian Borntraeger78f26132015-07-22 15:50:58 +0200194 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
195 if (!kvm_s390_dbf)
196 return -ENOMEM;
197
198 if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
199 debug_unregister(kvm_s390_dbf);
200 return -ENOMEM;
201 }
202
Cornelia Huck84877d92014-09-02 10:27:35 +0100203 /* Register floating interrupt controller interface. */
204 return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100205}
206
Christian Borntraeger78f26132015-07-22 15:50:58 +0200207void kvm_arch_exit(void)
208{
209 debug_unregister(kvm_s390_dbf);
210}
211
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100212/* Section: device related */
213long kvm_arch_dev_ioctl(struct file *filp,
214 unsigned int ioctl, unsigned long arg)
215{
216 if (ioctl == KVM_S390_ENABLE_SIE)
217 return s390_enable_sie();
218 return -EINVAL;
219}
220
Alexander Graf784aa3d2014-07-14 18:27:35 +0200221int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100222{
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100223 int r;
224
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200225 switch (ext) {
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100226 case KVM_CAP_S390_PSW:
Christian Borntraegerb6cf8782011-09-20 17:07:29 +0200227 case KVM_CAP_S390_GMAP:
Christian Borntraeger52e16b12011-11-17 11:00:44 +0100228 case KVM_CAP_SYNC_MMU:
Carsten Otte1efd0f52012-01-04 10:25:29 +0100229#ifdef CONFIG_KVM_S390_UCONTROL
230 case KVM_CAP_S390_UCONTROL:
231#endif
Dominik Dingel3c038e62013-10-07 17:11:48 +0200232 case KVM_CAP_ASYNC_PF:
Christian Borntraeger60b413c2012-01-11 11:20:31 +0100233 case KVM_CAP_SYNC_REGS:
Carsten Otte14eebd92012-05-15 14:15:26 +0200234 case KVM_CAP_ONE_REG:
Cornelia Huckd6712df2012-12-20 15:32:11 +0100235 case KVM_CAP_ENABLE_CAP:
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +0100236 case KVM_CAP_S390_CSS_SUPPORT:
Cornelia Huck10ccaa12013-02-28 12:33:21 +0100237 case KVM_CAP_IOEVENTFD:
Jens Freimannc05c4182013-10-07 16:13:45 +0200238 case KVM_CAP_DEVICE_CTRL:
Cornelia Huckd938dc52013-10-23 18:26:34 +0200239 case KVM_CAP_ENABLE_CAP_VM:
Cornelia Huck78599d92014-07-15 09:54:39 +0200240 case KVM_CAP_S390_IRQCHIP:
Dominik Dingelf2061652014-04-09 13:13:00 +0200241 case KVM_CAP_VM_ATTRIBUTES:
David Hildenbrand6352e4d2014-04-10 17:35:00 +0200242 case KVM_CAP_MP_STATE:
Jens Freimann47b43c52014-11-11 20:57:06 +0100243 case KVM_CAP_S390_INJECT_IRQ:
David Hildenbrand2444b352014-10-09 14:10:13 +0200244 case KVM_CAP_S390_USER_SIGP:
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100245 case KVM_CAP_S390_USER_STSI:
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400246 case KVM_CAP_S390_SKEYS:
Jens Freimann816c7662014-11-24 17:13:46 +0100247 case KVM_CAP_S390_IRQ_STATE:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100248 r = 1;
249 break;
Thomas Huth41408c282015-02-06 15:01:21 +0100250 case KVM_CAP_S390_MEM_OP:
251 r = MEM_OP_MAX_SIZE;
252 break;
Christian Borntraegere726b1b2012-05-02 10:50:38 +0200253 case KVM_CAP_NR_VCPUS:
254 case KVM_CAP_MAX_VCPUS:
Eugene (jno) Dvurechenskife0edcb2015-04-22 18:37:40 +0200255 r = sclp.has_esca ? KVM_S390_ESCA_CPU_SLOTS
256 : KVM_S390_BSCA_CPU_SLOTS;
Christian Borntraegere726b1b2012-05-02 10:50:38 +0200257 break;
Nick Wange1e2e602013-03-25 17:22:58 +0100258 case KVM_CAP_NR_MEMSLOTS:
259 r = KVM_USER_MEM_SLOTS;
260 break;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200261 case KVM_CAP_S390_COW:
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100262 r = MACHINE_HAS_ESOP;
Christian Borntraeger1526bf92012-05-15 14:15:25 +0200263 break;
Eric Farman68c55752014-06-09 10:57:26 -0400264 case KVM_CAP_S390_VECTOR_REGISTERS:
265 r = MACHINE_HAS_VX;
266 break;
Fan Zhangc6e5f162016-01-07 18:24:29 +0800267 case KVM_CAP_S390_RI:
268 r = test_facility(64);
269 break;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200270 default:
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100271 r = 0;
Carsten Otte2bd0ac42008-07-25 15:49:13 +0200272 }
Carsten Otted7b0b5e2009-11-19 14:21:16 +0100273 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100274}
275
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400276static void kvm_s390_sync_dirty_log(struct kvm *kvm,
277 struct kvm_memory_slot *memslot)
278{
279 gfn_t cur_gfn, last_gfn;
280 unsigned long address;
281 struct gmap *gmap = kvm->arch.gmap;
282
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400283 /* Loop over all guest pages */
284 last_gfn = memslot->base_gfn + memslot->npages;
285 for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
286 address = gfn_to_hva_memslot(memslot, cur_gfn);
287
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100288 if (test_and_clear_guest_dirty(gmap->mm, address))
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400289 mark_page_dirty(kvm, cur_gfn);
Christian Borntraeger1763f8d2016-02-03 11:12:34 +0100290 if (fatal_signal_pending(current))
291 return;
Christian Borntraeger70c88a02016-02-02 15:15:56 +0100292 cond_resched();
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400293 }
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400294}
295
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100296/* Section: vm related */
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +0200297static void sca_del_vcpu(struct kvm_vcpu *vcpu);
298
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100299/*
300 * Get (and clear) the dirty memory log for a memory slot.
301 */
302int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
303 struct kvm_dirty_log *log)
304{
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400305 int r;
306 unsigned long n;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200307 struct kvm_memslots *slots;
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400308 struct kvm_memory_slot *memslot;
309 int is_dirty = 0;
310
311 mutex_lock(&kvm->slots_lock);
312
313 r = -EINVAL;
314 if (log->slot >= KVM_USER_MEM_SLOTS)
315 goto out;
316
Paolo Bonzini9f6b8022015-05-17 16:20:07 +0200317 slots = kvm_memslots(kvm);
318 memslot = id_to_memslot(slots, log->slot);
Jason J. Herne15f36eb2012-08-02 10:10:17 -0400319 r = -ENOENT;
320 if (!memslot->dirty_bitmap)
321 goto out;
322
323 kvm_s390_sync_dirty_log(kvm, memslot);
324 r = kvm_get_dirty_log(kvm, log, &is_dirty);
325 if (r)
326 goto out;
327
328 /* Clear the dirty log */
329 if (is_dirty) {
330 n = kvm_dirty_bitmap_bytes(memslot);
331 memset(memslot->dirty_bitmap, 0, n);
332 }
333 r = 0;
334out:
335 mutex_unlock(&kvm->slots_lock);
336 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100337}
338
Cornelia Huckd938dc52013-10-23 18:26:34 +0200339static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
340{
341 int r;
342
343 if (cap->flags)
344 return -EINVAL;
345
346 switch (cap->cap) {
Cornelia Huck84223592013-07-15 13:36:01 +0200347 case KVM_CAP_S390_IRQCHIP:
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200348 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
Cornelia Huck84223592013-07-15 13:36:01 +0200349 kvm->arch.use_irqchip = 1;
350 r = 0;
351 break;
David Hildenbrand2444b352014-10-09 14:10:13 +0200352 case KVM_CAP_S390_USER_SIGP:
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200353 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
David Hildenbrand2444b352014-10-09 14:10:13 +0200354 kvm->arch.user_sigp = 1;
355 r = 0;
356 break;
Eric Farman68c55752014-06-09 10:57:26 -0400357 case KVM_CAP_S390_VECTOR_REGISTERS:
David Hildenbrand5967c172015-11-06 12:08:48 +0100358 mutex_lock(&kvm->lock);
359 if (atomic_read(&kvm->online_vcpus)) {
360 r = -EBUSY;
361 } else if (MACHINE_HAS_VX) {
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100362 set_kvm_facility(kvm->arch.model.fac_mask, 129);
363 set_kvm_facility(kvm->arch.model.fac_list, 129);
Michael Mueller18280d82015-03-16 16:05:41 +0100364 r = 0;
365 } else
366 r = -EINVAL;
David Hildenbrand5967c172015-11-06 12:08:48 +0100367 mutex_unlock(&kvm->lock);
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200368 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
369 r ? "(not available)" : "(success)");
Eric Farman68c55752014-06-09 10:57:26 -0400370 break;
Fan Zhangc6e5f162016-01-07 18:24:29 +0800371 case KVM_CAP_S390_RI:
372 r = -EINVAL;
373 mutex_lock(&kvm->lock);
374 if (atomic_read(&kvm->online_vcpus)) {
375 r = -EBUSY;
376 } else if (test_facility(64)) {
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100377 set_kvm_facility(kvm->arch.model.fac_mask, 64);
378 set_kvm_facility(kvm->arch.model.fac_list, 64);
Fan Zhangc6e5f162016-01-07 18:24:29 +0800379 r = 0;
380 }
381 mutex_unlock(&kvm->lock);
382 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
383 r ? "(not available)" : "(success)");
384 break;
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100385 case KVM_CAP_S390_USER_STSI:
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200386 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
Ekaterina Tumanovae44fc8c2015-01-30 16:55:56 +0100387 kvm->arch.user_stsi = 1;
388 r = 0;
389 break;
Cornelia Huckd938dc52013-10-23 18:26:34 +0200390 default:
391 r = -EINVAL;
392 break;
393 }
394 return r;
395}
396
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100397static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
398{
399 int ret;
400
401 switch (attr->attr) {
402 case KVM_S390_VM_MEM_LIMIT_SIZE:
403 ret = 0;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200404 VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
Dominik Dingela3a92c32014-12-01 17:24:42 +0100405 kvm->arch.mem_limit);
406 if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100407 ret = -EFAULT;
408 break;
409 default:
410 ret = -ENXIO;
411 break;
412 }
413 return ret;
414}
415
416static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200417{
418 int ret;
419 unsigned int idx;
420 switch (attr->attr) {
421 case KVM_S390_VM_MEM_ENABLE_CMMA:
Dominik Dingele6db1d62015-05-07 15:41:57 +0200422 /* enable CMMA only for z10 and later (EDAT_1) */
423 ret = -EINVAL;
424 if (!MACHINE_IS_LPAR || !MACHINE_HAS_EDAT1)
425 break;
426
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200427 ret = -EBUSY;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200428 VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200429 mutex_lock(&kvm->lock);
430 if (atomic_read(&kvm->online_vcpus) == 0) {
431 kvm->arch.use_cmma = 1;
432 ret = 0;
433 }
434 mutex_unlock(&kvm->lock);
435 break;
436 case KVM_S390_VM_MEM_CLR_CMMA:
Dominik Dingelc3489152015-06-18 13:17:11 +0200437 ret = -EINVAL;
438 if (!kvm->arch.use_cmma)
439 break;
440
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200441 VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200442 mutex_lock(&kvm->lock);
443 idx = srcu_read_lock(&kvm->srcu);
Dominik Dingela13cff32014-10-23 12:07:14 +0200444 s390_reset_cmma(kvm->arch.gmap->mm);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200445 srcu_read_unlock(&kvm->srcu, idx);
446 mutex_unlock(&kvm->lock);
447 ret = 0;
448 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100449 case KVM_S390_VM_MEM_LIMIT_SIZE: {
450 unsigned long new_limit;
451
452 if (kvm_is_ucontrol(kvm))
453 return -EINVAL;
454
455 if (get_user(new_limit, (u64 __user *)attr->addr))
456 return -EFAULT;
457
Dominik Dingela3a92c32014-12-01 17:24:42 +0100458 if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
459 new_limit > kvm->arch.mem_limit)
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100460 return -E2BIG;
461
Dominik Dingela3a92c32014-12-01 17:24:42 +0100462 if (!new_limit)
463 return -EINVAL;
464
465 /* gmap_alloc takes last usable address */
466 if (new_limit != KVM_S390_NO_MEM_LIMIT)
467 new_limit -= 1;
468
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100469 ret = -EBUSY;
470 mutex_lock(&kvm->lock);
471 if (atomic_read(&kvm->online_vcpus) == 0) {
472 /* gmap_alloc will round the limit up */
473 struct gmap *new = gmap_alloc(current->mm, new_limit);
474
475 if (!new) {
476 ret = -ENOMEM;
477 } else {
478 gmap_free(kvm->arch.gmap);
479 new->private = kvm;
480 kvm->arch.gmap = new;
481 ret = 0;
482 }
483 }
484 mutex_unlock(&kvm->lock);
Dominik Dingela3a92c32014-12-01 17:24:42 +0100485 VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
486 VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
487 (void *) kvm->arch.gmap->asce);
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100488 break;
489 }
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200490 default:
491 ret = -ENXIO;
492 break;
493 }
494 return ret;
495}
496
Tony Krowiaka374e892014-09-03 10:13:53 +0200497static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
498
499static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
500{
501 struct kvm_vcpu *vcpu;
502 int i;
503
Michael Mueller9d8d5782015-02-02 15:42:51 +0100504 if (!test_kvm_facility(kvm, 76))
Tony Krowiaka374e892014-09-03 10:13:53 +0200505 return -EINVAL;
506
507 mutex_lock(&kvm->lock);
508 switch (attr->attr) {
509 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
510 get_random_bytes(
511 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
512 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
513 kvm->arch.crypto.aes_kw = 1;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200514 VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200515 break;
516 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
517 get_random_bytes(
518 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
519 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
520 kvm->arch.crypto.dea_kw = 1;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200521 VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200522 break;
523 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
524 kvm->arch.crypto.aes_kw = 0;
525 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
526 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200527 VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200528 break;
529 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
530 kvm->arch.crypto.dea_kw = 0;
531 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
532 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +0200533 VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
Tony Krowiaka374e892014-09-03 10:13:53 +0200534 break;
535 default:
536 mutex_unlock(&kvm->lock);
537 return -ENXIO;
538 }
539
540 kvm_for_each_vcpu(i, vcpu, kvm) {
541 kvm_s390_vcpu_crypto_setup(vcpu);
542 exit_sie(vcpu);
543 }
544 mutex_unlock(&kvm->lock);
545 return 0;
546}
547
Jason J. Herne72f25022014-11-25 09:46:02 -0500548static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
549{
550 u8 gtod_high;
551
552 if (copy_from_user(&gtod_high, (void __user *)attr->addr,
553 sizeof(gtod_high)))
554 return -EFAULT;
555
556 if (gtod_high != 0)
557 return -EINVAL;
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200558 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
Jason J. Herne72f25022014-11-25 09:46:02 -0500559
560 return 0;
561}
562
563static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
564{
David Hildenbrand5a3d8832015-09-29 16:27:24 +0200565 u64 gtod;
Jason J. Herne72f25022014-11-25 09:46:02 -0500566
567 if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
568 return -EFAULT;
569
David Hildenbrand25ed1672015-05-12 09:49:14 +0200570 kvm_s390_set_tod_clock(kvm, gtod);
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200571 VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
Jason J. Herne72f25022014-11-25 09:46:02 -0500572 return 0;
573}
574
575static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
576{
577 int ret;
578
579 if (attr->flags)
580 return -EINVAL;
581
582 switch (attr->attr) {
583 case KVM_S390_VM_TOD_HIGH:
584 ret = kvm_s390_set_tod_high(kvm, attr);
585 break;
586 case KVM_S390_VM_TOD_LOW:
587 ret = kvm_s390_set_tod_low(kvm, attr);
588 break;
589 default:
590 ret = -ENXIO;
591 break;
592 }
593 return ret;
594}
595
596static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
597{
598 u8 gtod_high = 0;
599
600 if (copy_to_user((void __user *)attr->addr, &gtod_high,
601 sizeof(gtod_high)))
602 return -EFAULT;
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200603 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
Jason J. Herne72f25022014-11-25 09:46:02 -0500604
605 return 0;
606}
607
608static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
609{
David Hildenbrand5a3d8832015-09-29 16:27:24 +0200610 u64 gtod;
Jason J. Herne72f25022014-11-25 09:46:02 -0500611
David Hildenbrand60417fc2015-09-29 16:20:36 +0200612 gtod = kvm_s390_get_tod_clock_fast(kvm);
Jason J. Herne72f25022014-11-25 09:46:02 -0500613 if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
614 return -EFAULT;
Christian Borntraeger58c383c2015-10-12 13:27:29 +0200615 VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
Jason J. Herne72f25022014-11-25 09:46:02 -0500616
617 return 0;
618}
619
620static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
621{
622 int ret;
623
624 if (attr->flags)
625 return -EINVAL;
626
627 switch (attr->attr) {
628 case KVM_S390_VM_TOD_HIGH:
629 ret = kvm_s390_get_tod_high(kvm, attr);
630 break;
631 case KVM_S390_VM_TOD_LOW:
632 ret = kvm_s390_get_tod_low(kvm, attr);
633 break;
634 default:
635 ret = -ENXIO;
636 break;
637 }
638 return ret;
639}
640
Michael Mueller658b6ed2015-02-02 15:49:35 +0100641static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
642{
643 struct kvm_s390_vm_cpu_processor *proc;
David Hildenbrand053dd232016-04-04 13:59:42 +0200644 u16 lowest_ibc, unblocked_ibc;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100645 int ret = 0;
646
647 mutex_lock(&kvm->lock);
648 if (atomic_read(&kvm->online_vcpus)) {
649 ret = -EBUSY;
650 goto out;
651 }
652 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
653 if (!proc) {
654 ret = -ENOMEM;
655 goto out;
656 }
657 if (!copy_from_user(proc, (void __user *)attr->addr,
658 sizeof(*proc))) {
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200659 kvm->arch.model.cpuid = proc->cpuid;
David Hildenbrand053dd232016-04-04 13:59:42 +0200660 lowest_ibc = sclp.ibc >> 16 & 0xfff;
661 unblocked_ibc = sclp.ibc & 0xfff;
662 if (lowest_ibc) {
663 if (proc->ibc > unblocked_ibc)
664 kvm->arch.model.ibc = unblocked_ibc;
665 else if (proc->ibc < lowest_ibc)
666 kvm->arch.model.ibc = lowest_ibc;
667 else
668 kvm->arch.model.ibc = proc->ibc;
669 }
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100670 memcpy(kvm->arch.model.fac_list, proc->fac_list,
Michael Mueller658b6ed2015-02-02 15:49:35 +0100671 S390_ARCH_FAC_LIST_SIZE_BYTE);
672 } else
673 ret = -EFAULT;
674 kfree(proc);
675out:
676 mutex_unlock(&kvm->lock);
677 return ret;
678}
679
680static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
681{
682 int ret = -ENXIO;
683
684 switch (attr->attr) {
685 case KVM_S390_VM_CPU_PROCESSOR:
686 ret = kvm_s390_set_processor(kvm, attr);
687 break;
688 }
689 return ret;
690}
691
692static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
693{
694 struct kvm_s390_vm_cpu_processor *proc;
695 int ret = 0;
696
697 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
698 if (!proc) {
699 ret = -ENOMEM;
700 goto out;
701 }
David Hildenbrand9bb0ec02016-04-04 14:27:51 +0200702 proc->cpuid = kvm->arch.model.cpuid;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100703 proc->ibc = kvm->arch.model.ibc;
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100704 memcpy(&proc->fac_list, kvm->arch.model.fac_list,
705 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100706 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
707 ret = -EFAULT;
708 kfree(proc);
709out:
710 return ret;
711}
712
713static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
714{
715 struct kvm_s390_vm_cpu_machine *mach;
716 int ret = 0;
717
718 mach = kzalloc(sizeof(*mach), GFP_KERNEL);
719 if (!mach) {
720 ret = -ENOMEM;
721 goto out;
722 }
723 get_cpu_id((struct cpuid *) &mach->cpuid);
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200724 mach->ibc = sclp.ibc;
David Hildenbrandc54f0d62015-12-02 08:53:52 +0100725 memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
Michael Mueller981467c2015-02-24 13:51:04 +0100726 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100727 memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +0100728 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller658b6ed2015-02-02 15:49:35 +0100729 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
730 ret = -EFAULT;
731 kfree(mach);
732out:
733 return ret;
734}
735
736static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
737{
738 int ret = -ENXIO;
739
740 switch (attr->attr) {
741 case KVM_S390_VM_CPU_PROCESSOR:
742 ret = kvm_s390_get_processor(kvm, attr);
743 break;
744 case KVM_S390_VM_CPU_MACHINE:
745 ret = kvm_s390_get_machine(kvm, attr);
746 break;
747 }
748 return ret;
749}
750
Dominik Dingelf2061652014-04-09 13:13:00 +0200751static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
752{
753 int ret;
754
755 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200756 case KVM_S390_VM_MEM_CTRL:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100757 ret = kvm_s390_set_mem_control(kvm, attr);
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200758 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500759 case KVM_S390_VM_TOD:
760 ret = kvm_s390_set_tod(kvm, attr);
761 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100762 case KVM_S390_VM_CPU_MODEL:
763 ret = kvm_s390_set_cpu_model(kvm, attr);
764 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200765 case KVM_S390_VM_CRYPTO:
766 ret = kvm_s390_vm_set_crypto(kvm, attr);
767 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200768 default:
769 ret = -ENXIO;
770 break;
771 }
772
773 return ret;
774}
775
776static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
777{
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100778 int ret;
779
780 switch (attr->group) {
781 case KVM_S390_VM_MEM_CTRL:
782 ret = kvm_s390_get_mem_control(kvm, attr);
783 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500784 case KVM_S390_VM_TOD:
785 ret = kvm_s390_get_tod(kvm, attr);
786 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100787 case KVM_S390_VM_CPU_MODEL:
788 ret = kvm_s390_get_cpu_model(kvm, attr);
789 break;
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100790 default:
791 ret = -ENXIO;
792 break;
793 }
794
795 return ret;
Dominik Dingelf2061652014-04-09 13:13:00 +0200796}
797
798static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
799{
800 int ret;
801
802 switch (attr->group) {
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200803 case KVM_S390_VM_MEM_CTRL:
804 switch (attr->attr) {
805 case KVM_S390_VM_MEM_ENABLE_CMMA:
806 case KVM_S390_VM_MEM_CLR_CMMA:
Dominik Dingel8c0a7ce2014-10-31 14:10:41 +0100807 case KVM_S390_VM_MEM_LIMIT_SIZE:
Dominik Dingel4f718ea2014-04-09 13:13:00 +0200808 ret = 0;
809 break;
810 default:
811 ret = -ENXIO;
812 break;
813 }
814 break;
Jason J. Herne72f25022014-11-25 09:46:02 -0500815 case KVM_S390_VM_TOD:
816 switch (attr->attr) {
817 case KVM_S390_VM_TOD_LOW:
818 case KVM_S390_VM_TOD_HIGH:
819 ret = 0;
820 break;
821 default:
822 ret = -ENXIO;
823 break;
824 }
825 break;
Michael Mueller658b6ed2015-02-02 15:49:35 +0100826 case KVM_S390_VM_CPU_MODEL:
827 switch (attr->attr) {
828 case KVM_S390_VM_CPU_PROCESSOR:
829 case KVM_S390_VM_CPU_MACHINE:
830 ret = 0;
831 break;
832 default:
833 ret = -ENXIO;
834 break;
835 }
836 break;
Tony Krowiaka374e892014-09-03 10:13:53 +0200837 case KVM_S390_VM_CRYPTO:
838 switch (attr->attr) {
839 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
840 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
841 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
842 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
843 ret = 0;
844 break;
845 default:
846 ret = -ENXIO;
847 break;
848 }
849 break;
Dominik Dingelf2061652014-04-09 13:13:00 +0200850 default:
851 ret = -ENXIO;
852 break;
853 }
854
855 return ret;
856}
857
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400858static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
859{
860 uint8_t *keys;
861 uint64_t hva;
862 unsigned long curkey;
863 int i, r = 0;
864
865 if (args->flags != 0)
866 return -EINVAL;
867
868 /* Is this guest using storage keys? */
869 if (!mm_use_skey(current->mm))
870 return KVM_S390_GET_SKEYS_NONE;
871
872 /* Enforce sane limit on memory allocation */
873 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
874 return -EINVAL;
875
876 keys = kmalloc_array(args->count, sizeof(uint8_t),
877 GFP_KERNEL | __GFP_NOWARN);
878 if (!keys)
879 keys = vmalloc(sizeof(uint8_t) * args->count);
880 if (!keys)
881 return -ENOMEM;
882
883 for (i = 0; i < args->count; i++) {
884 hva = gfn_to_hva(kvm, args->start_gfn + i);
885 if (kvm_is_error_hva(hva)) {
886 r = -EFAULT;
887 goto out;
888 }
889
890 curkey = get_guest_storage_key(current->mm, hva);
891 if (IS_ERR_VALUE(curkey)) {
892 r = curkey;
893 goto out;
894 }
895 keys[i] = curkey;
896 }
897
898 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
899 sizeof(uint8_t) * args->count);
900 if (r)
901 r = -EFAULT;
902out:
903 kvfree(keys);
904 return r;
905}
906
907static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
908{
909 uint8_t *keys;
910 uint64_t hva;
911 int i, r = 0;
912
913 if (args->flags != 0)
914 return -EINVAL;
915
916 /* Enforce sane limit on memory allocation */
917 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
918 return -EINVAL;
919
920 keys = kmalloc_array(args->count, sizeof(uint8_t),
921 GFP_KERNEL | __GFP_NOWARN);
922 if (!keys)
923 keys = vmalloc(sizeof(uint8_t) * args->count);
924 if (!keys)
925 return -ENOMEM;
926
927 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
928 sizeof(uint8_t) * args->count);
929 if (r) {
930 r = -EFAULT;
931 goto out;
932 }
933
934 /* Enable storage key handling for the guest */
Dominik Dingel14d4a422015-05-07 15:16:13 +0200935 r = s390_enable_skey();
936 if (r)
937 goto out;
Jason J. Herne30ee2a92014-09-23 09:23:01 -0400938
939 for (i = 0; i < args->count; i++) {
940 hva = gfn_to_hva(kvm, args->start_gfn + i);
941 if (kvm_is_error_hva(hva)) {
942 r = -EFAULT;
943 goto out;
944 }
945
946 /* Lowest order bit is reserved */
947 if (keys[i] & 0x01) {
948 r = -EINVAL;
949 goto out;
950 }
951
952 r = set_guest_storage_key(current->mm, hva,
953 (unsigned long)keys[i], 0);
954 if (r)
955 goto out;
956 }
957out:
958 kvfree(keys);
959 return r;
960}
961
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100962long kvm_arch_vm_ioctl(struct file *filp,
963 unsigned int ioctl, unsigned long arg)
964{
965 struct kvm *kvm = filp->private_data;
966 void __user *argp = (void __user *)arg;
Dominik Dingelf2061652014-04-09 13:13:00 +0200967 struct kvm_device_attr attr;
Heiko Carstensb0c632d2008-03-25 18:47:20 +0100968 int r;
969
970 switch (ioctl) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100971 case KVM_S390_INTERRUPT: {
972 struct kvm_s390_interrupt s390int;
973
974 r = -EFAULT;
975 if (copy_from_user(&s390int, argp, sizeof(s390int)))
976 break;
977 r = kvm_s390_inject_vm(kvm, &s390int);
978 break;
979 }
Cornelia Huckd938dc52013-10-23 18:26:34 +0200980 case KVM_ENABLE_CAP: {
981 struct kvm_enable_cap cap;
982 r = -EFAULT;
983 if (copy_from_user(&cap, argp, sizeof(cap)))
984 break;
985 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
986 break;
987 }
Cornelia Huck84223592013-07-15 13:36:01 +0200988 case KVM_CREATE_IRQCHIP: {
989 struct kvm_irq_routing_entry routing;
990
991 r = -EINVAL;
992 if (kvm->arch.use_irqchip) {
993 /* Set up dummy routing. */
994 memset(&routing, 0, sizeof(routing));
Nicholas Krause152b2832015-08-06 13:05:54 -0400995 r = kvm_set_irq_routing(kvm, &routing, 0, 0);
Cornelia Huck84223592013-07-15 13:36:01 +0200996 }
997 break;
998 }
Dominik Dingelf2061652014-04-09 13:13:00 +0200999 case KVM_SET_DEVICE_ATTR: {
1000 r = -EFAULT;
1001 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1002 break;
1003 r = kvm_s390_vm_set_attr(kvm, &attr);
1004 break;
1005 }
1006 case KVM_GET_DEVICE_ATTR: {
1007 r = -EFAULT;
1008 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1009 break;
1010 r = kvm_s390_vm_get_attr(kvm, &attr);
1011 break;
1012 }
1013 case KVM_HAS_DEVICE_ATTR: {
1014 r = -EFAULT;
1015 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1016 break;
1017 r = kvm_s390_vm_has_attr(kvm, &attr);
1018 break;
1019 }
Jason J. Herne30ee2a92014-09-23 09:23:01 -04001020 case KVM_S390_GET_SKEYS: {
1021 struct kvm_s390_skeys args;
1022
1023 r = -EFAULT;
1024 if (copy_from_user(&args, argp,
1025 sizeof(struct kvm_s390_skeys)))
1026 break;
1027 r = kvm_s390_get_skeys(kvm, &args);
1028 break;
1029 }
1030 case KVM_S390_SET_SKEYS: {
1031 struct kvm_s390_skeys args;
1032
1033 r = -EFAULT;
1034 if (copy_from_user(&args, argp,
1035 sizeof(struct kvm_s390_skeys)))
1036 break;
1037 r = kvm_s390_set_skeys(kvm, &args);
1038 break;
1039 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001040 default:
Avi Kivity367e1312009-08-26 14:57:07 +03001041 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001042 }
1043
1044 return r;
1045}
1046
Tony Krowiak45c9b472015-01-13 11:33:26 -05001047static int kvm_s390_query_ap_config(u8 *config)
1048{
1049 u32 fcn_code = 0x04000000UL;
Christian Borntraeger86044c82015-02-26 13:53:47 +01001050 u32 cc = 0;
Tony Krowiak45c9b472015-01-13 11:33:26 -05001051
Christian Borntraeger86044c82015-02-26 13:53:47 +01001052 memset(config, 0, 128);
Tony Krowiak45c9b472015-01-13 11:33:26 -05001053 asm volatile(
1054 "lgr 0,%1\n"
1055 "lgr 2,%2\n"
1056 ".long 0xb2af0000\n" /* PQAP(QCI) */
Christian Borntraeger86044c82015-02-26 13:53:47 +01001057 "0: ipm %0\n"
Tony Krowiak45c9b472015-01-13 11:33:26 -05001058 "srl %0,28\n"
Christian Borntraeger86044c82015-02-26 13:53:47 +01001059 "1:\n"
1060 EX_TABLE(0b, 1b)
1061 : "+r" (cc)
Tony Krowiak45c9b472015-01-13 11:33:26 -05001062 : "r" (fcn_code), "r" (config)
1063 : "cc", "0", "2", "memory"
1064 );
1065
1066 return cc;
1067}
1068
1069static int kvm_s390_apxa_installed(void)
1070{
1071 u8 config[128];
1072 int cc;
1073
Heiko Carstensa6aacc32015-11-24 14:28:12 +01001074 if (test_facility(12)) {
Tony Krowiak45c9b472015-01-13 11:33:26 -05001075 cc = kvm_s390_query_ap_config(config);
1076
1077 if (cc)
1078 pr_err("PQAP(QCI) failed with cc=%d", cc);
1079 else
1080 return config[0] & 0x40;
1081 }
1082
1083 return 0;
1084}
1085
1086static void kvm_s390_set_crycb_format(struct kvm *kvm)
1087{
1088 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
1089
1090 if (kvm_s390_apxa_installed())
1091 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
1092 else
1093 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
1094}
1095
David Hildenbrand9bb0ec02016-04-04 14:27:51 +02001096static u64 kvm_s390_get_initial_cpuid(void)
Michael Mueller9d8d5782015-02-02 15:42:51 +01001097{
David Hildenbrand9bb0ec02016-04-04 14:27:51 +02001098 struct cpuid cpuid;
1099
1100 get_cpu_id(&cpuid);
1101 cpuid.version = 0xff;
1102 return *((u64 *) &cpuid);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001103}
1104
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001105static void kvm_s390_crypto_init(struct kvm *kvm)
Tony Krowiak5102ee82014-06-27 14:46:01 -04001106{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001107 if (!test_kvm_facility(kvm, 76))
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001108 return;
Tony Krowiak5102ee82014-06-27 14:46:01 -04001109
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001110 kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
Tony Krowiak45c9b472015-01-13 11:33:26 -05001111 kvm_s390_set_crycb_format(kvm);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001112
Tony Krowiaked6f76b2015-02-24 14:06:57 -05001113 /* Enable AES/DEA protected key functions by default */
1114 kvm->arch.crypto.aes_kw = 1;
1115 kvm->arch.crypto.dea_kw = 1;
1116 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1117 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1118 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1119 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
Tony Krowiak5102ee82014-06-27 14:46:01 -04001120}
1121
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001122static void sca_dispose(struct kvm *kvm)
1123{
1124 if (kvm->arch.use_esca)
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001125 free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001126 else
1127 free_page((unsigned long)(kvm->arch.sca));
1128 kvm->arch.sca = NULL;
1129}
1130
Carsten Ottee08b9632012-01-04 10:25:20 +01001131int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001132{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001133 int i, rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001134 char debug_name[16];
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001135 static unsigned long sca_offset;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001136
Carsten Ottee08b9632012-01-04 10:25:20 +01001137 rc = -EINVAL;
1138#ifdef CONFIG_KVM_S390_UCONTROL
1139 if (type & ~KVM_VM_S390_UCONTROL)
1140 goto out_err;
1141 if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1142 goto out_err;
1143#else
1144 if (type)
1145 goto out_err;
1146#endif
1147
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001148 rc = s390_enable_sie();
1149 if (rc)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001150 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001151
Carsten Otteb2904112011-10-18 12:27:13 +02001152 rc = -ENOMEM;
1153
Janosch Frank7d0a5e62016-05-10 15:03:42 +02001154 ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500);
1155
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001156 kvm->arch.use_esca = 0; /* start with basic SCA */
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001157 rwlock_init(&kvm->arch.sca_lock);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +02001158 kvm->arch.sca = (struct bsca_block *) get_zeroed_page(GFP_KERNEL);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001159 if (!kvm->arch.sca)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001160 goto out_err;
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001161 spin_lock(&kvm_lock);
David Hildenbrandc5c2c392015-10-26 08:41:29 +01001162 sca_offset += 16;
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +02001163 if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
David Hildenbrandc5c2c392015-10-26 08:41:29 +01001164 sca_offset = 0;
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +02001165 kvm->arch.sca = (struct bsca_block *)
1166 ((char *) kvm->arch.sca + sca_offset);
Christian Borntraegerf6c137f2014-03-19 11:18:29 +01001167 spin_unlock(&kvm_lock);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001168
1169 sprintf(debug_name, "kvm-%u", current->pid);
1170
Christian Borntraeger1cb9cf72015-07-20 15:04:48 +02001171 kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001172 if (!kvm->arch.dbf)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001173 goto out_err;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001174
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001175 kvm->arch.sie_page2 =
1176 (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1177 if (!kvm->arch.sie_page2)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001178 goto out_err;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001179
Michael Muellerfb5bf932015-02-27 14:25:10 +01001180 /* Populate the facility mask initially. */
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001181 memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list,
Michael Mueller94422ee2015-02-26 12:12:40 +01001182 S390_ARCH_FAC_LIST_SIZE_BYTE);
Michael Mueller9d8d5782015-02-02 15:42:51 +01001183 for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
1184 if (i < kvm_s390_fac_list_mask_size())
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001185 kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i];
Michael Mueller9d8d5782015-02-02 15:42:51 +01001186 else
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001187 kvm->arch.model.fac_mask[i] = 0UL;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001188 }
1189
Michael Mueller981467c2015-02-24 13:51:04 +01001190 /* Populate the facility list initially. */
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001191 kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
1192 memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask,
Michael Mueller981467c2015-02-24 13:51:04 +01001193 S390_ARCH_FAC_LIST_SIZE_BYTE);
1194
Janosch Frank95ca2cb2016-05-23 15:11:58 +02001195 set_kvm_facility(kvm->arch.model.fac_mask, 74);
1196 set_kvm_facility(kvm->arch.model.fac_list, 74);
1197
David Hildenbrand9bb0ec02016-04-04 14:27:51 +02001198 kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001199 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001200
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001201 kvm_s390_crypto_init(kvm);
Tony Krowiak5102ee82014-06-27 14:46:01 -04001202
Carsten Otteba5c1e92008-03-25 18:47:26 +01001203 spin_lock_init(&kvm->arch.float_int.lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001204 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1205 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
Heiko Carstens8a2422342014-01-10 14:33:28 +01001206 init_waitqueue_head(&kvm->arch.ipte_wq);
Thomas Hutha6b7e452014-10-01 14:48:42 +02001207 mutex_init(&kvm->arch.ipte_mutex);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001208
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001209 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
Christian Borntraeger78f26132015-07-22 15:50:58 +02001210 VM_EVENT(kvm, 3, "vm created with type %lu", type);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001211
Carsten Ottee08b9632012-01-04 10:25:20 +01001212 if (type & KVM_VM_S390_UCONTROL) {
1213 kvm->arch.gmap = NULL;
Dominik Dingela3a92c32014-12-01 17:24:42 +01001214 kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
Carsten Ottee08b9632012-01-04 10:25:20 +01001215 } else {
Guenther Hutzl32e6b232014-12-01 17:24:42 +01001216 if (sclp.hamax == U64_MAX)
1217 kvm->arch.mem_limit = TASK_MAX_SIZE;
1218 else
1219 kvm->arch.mem_limit = min_t(unsigned long, TASK_MAX_SIZE,
1220 sclp.hamax + 1);
Dominik Dingela3a92c32014-12-01 17:24:42 +01001221 kvm->arch.gmap = gmap_alloc(current->mm, kvm->arch.mem_limit - 1);
Carsten Ottee08b9632012-01-04 10:25:20 +01001222 if (!kvm->arch.gmap)
Dominik Dingel40f5b732015-03-12 13:55:53 +01001223 goto out_err;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001224 kvm->arch.gmap->private = kvm;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02001225 kvm->arch.gmap->pfault_enabled = 0;
Carsten Ottee08b9632012-01-04 10:25:20 +01001226 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001227
1228 kvm->arch.css_support = 0;
Cornelia Huck84223592013-07-15 13:36:01 +02001229 kvm->arch.use_irqchip = 0;
Jason J. Herne72f25022014-11-25 09:46:02 -05001230 kvm->arch.epoch = 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001231
David Hildenbrand8ad35752014-03-14 11:00:21 +01001232 spin_lock_init(&kvm->arch.start_stop_lock);
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001233 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
David Hildenbrand8ad35752014-03-14 11:00:21 +01001234
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001235 return 0;
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001236out_err:
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001237 free_page((unsigned long)kvm->arch.sie_page2);
Dominik Dingel40f5b732015-03-12 13:55:53 +01001238 debug_unregister(kvm->arch.dbf);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001239 sca_dispose(kvm);
Christian Borntraeger78f26132015-07-22 15:50:58 +02001240 KVM_EVENT(3, "creation of vm failed: %d", rc);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001241 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001242}
1243
Christian Borntraegerd329c032008-11-26 14:50:27 +01001244void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1245{
1246 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
Cornelia Huckade38c32012-07-23 17:20:30 +02001247 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001248 kvm_s390_clear_local_irqs(vcpu);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001249 kvm_clear_async_pf_completion_queue(vcpu);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +02001250 if (!kvm_is_ucontrol(vcpu->kvm))
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001251 sca_del_vcpu(vcpu);
Carsten Otte27e03932012-01-04 10:25:21 +01001252
1253 if (kvm_is_ucontrol(vcpu->kvm))
1254 gmap_free(vcpu->arch.gmap);
1255
Dominik Dingele6db1d62015-05-07 15:41:57 +02001256 if (vcpu->kvm->arch.use_cmma)
Dominik Dingelb31605c2014-03-25 13:47:11 +01001257 kvm_s390_vcpu_unsetup_cmma(vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001258 free_page((unsigned long)(vcpu->arch.sie_block));
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001259
Christian Borntraeger6692cef2008-11-26 14:51:08 +01001260 kvm_vcpu_uninit(vcpu);
Michael Muellerb110fea2013-06-12 13:54:54 +02001261 kmem_cache_free(kvm_vcpu_cache, vcpu);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001262}
1263
1264static void kvm_free_vcpus(struct kvm *kvm)
1265{
1266 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001267 struct kvm_vcpu *vcpu;
Christian Borntraegerd329c032008-11-26 14:50:27 +01001268
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001269 kvm_for_each_vcpu(i, vcpu, kvm)
1270 kvm_arch_vcpu_destroy(vcpu);
1271
1272 mutex_lock(&kvm->lock);
1273 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1274 kvm->vcpus[i] = NULL;
1275
1276 atomic_set(&kvm->online_vcpus, 0);
1277 mutex_unlock(&kvm->lock);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001278}
1279
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001280void kvm_arch_destroy_vm(struct kvm *kvm)
1281{
Christian Borntraegerd329c032008-11-26 14:50:27 +01001282 kvm_free_vcpus(kvm);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001283 sca_dispose(kvm);
Christian Borntraegerd329c032008-11-26 14:50:27 +01001284 debug_unregister(kvm->arch.dbf);
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001285 free_page((unsigned long)kvm->arch.sie_page2);
Carsten Otte27e03932012-01-04 10:25:21 +01001286 if (!kvm_is_ucontrol(kvm))
1287 gmap_free(kvm->arch.gmap);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001288 kvm_s390_destroy_adapters(kvm);
Christian Borntraeger67335e62014-03-25 17:09:08 +01001289 kvm_s390_clear_float_irqs(kvm);
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001290 KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001291}
1292
1293/* Section: vcpu related */
Dominik Dingeldafd0322014-12-02 16:53:21 +01001294static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1295{
1296 vcpu->arch.gmap = gmap_alloc(current->mm, -1UL);
1297 if (!vcpu->arch.gmap)
1298 return -ENOMEM;
1299 vcpu->arch.gmap->private = vcpu->kvm;
1300
1301 return 0;
1302}
1303
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001304static void sca_del_vcpu(struct kvm_vcpu *vcpu)
1305{
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001306 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001307 if (vcpu->kvm->arch.use_esca) {
1308 struct esca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001309
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001310 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
David Hildenbrand10ce32d2015-10-12 12:41:41 +02001311 sca->cpu[vcpu->vcpu_id].sda = 0;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001312 } else {
1313 struct bsca_block *sca = vcpu->kvm->arch.sca;
1314
1315 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
David Hildenbrand10ce32d2015-10-12 12:41:41 +02001316 sca->cpu[vcpu->vcpu_id].sda = 0;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001317 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001318 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001319}
1320
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001321static void sca_add_vcpu(struct kvm_vcpu *vcpu)
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001322{
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001323 read_lock(&vcpu->kvm->arch.sca_lock);
1324 if (vcpu->kvm->arch.use_esca) {
1325 struct esca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001326
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001327 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001328 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1329 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
David Hildenbrand25508822015-10-12 16:27:23 +02001330 vcpu->arch.sie_block->ecb2 |= 0x04U;
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001331 set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001332 } else {
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001333 struct bsca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001334
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001335 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001336 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1337 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001338 set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +02001339 }
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001340 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001341}
1342
1343/* Basic SCA to Extended SCA data copy routines */
1344static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
1345{
1346 d->sda = s->sda;
1347 d->sigp_ctrl.c = s->sigp_ctrl.c;
1348 d->sigp_ctrl.scn = s->sigp_ctrl.scn;
1349}
1350
1351static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
1352{
1353 int i;
1354
1355 d->ipte_control = s->ipte_control;
1356 d->mcn[0] = s->mcn;
1357 for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
1358 sca_copy_entry(&d->cpu[i], &s->cpu[i]);
1359}
1360
1361static int sca_switch_to_extended(struct kvm *kvm)
1362{
1363 struct bsca_block *old_sca = kvm->arch.sca;
1364 struct esca_block *new_sca;
1365 struct kvm_vcpu *vcpu;
1366 unsigned int vcpu_idx;
1367 u32 scaol, scaoh;
1368
1369 new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
1370 if (!new_sca)
1371 return -ENOMEM;
1372
1373 scaoh = (u32)((u64)(new_sca) >> 32);
1374 scaol = (u32)(u64)(new_sca) & ~0x3fU;
1375
1376 kvm_s390_vcpu_block_all(kvm);
1377 write_lock(&kvm->arch.sca_lock);
1378
1379 sca_copy_b_to_e(new_sca, old_sca);
1380
1381 kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
1382 vcpu->arch.sie_block->scaoh = scaoh;
1383 vcpu->arch.sie_block->scaol = scaol;
1384 vcpu->arch.sie_block->ecb2 |= 0x04U;
1385 }
1386 kvm->arch.sca = new_sca;
1387 kvm->arch.use_esca = 1;
1388
1389 write_unlock(&kvm->arch.sca_lock);
1390 kvm_s390_vcpu_unblock_all(kvm);
1391
1392 free_page((unsigned long)old_sca);
1393
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001394 VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
1395 old_sca, kvm->arch.sca);
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001396 return 0;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001397}
1398
1399static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
1400{
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +02001401 int rc;
1402
1403 if (id < KVM_S390_BSCA_CPU_SLOTS)
1404 return true;
1405 if (!sclp.has_esca)
1406 return false;
1407
1408 mutex_lock(&kvm->lock);
1409 rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
1410 mutex_unlock(&kvm->lock);
1411
1412 return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
Eugene (jno) Dvurechenskia6e2f682015-04-21 15:31:59 +02001413}
1414
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001415int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1416{
Dominik Dingel3c038e62013-10-07 17:11:48 +02001417 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1418 kvm_clear_async_pf_completion_queue(vcpu);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001419 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
1420 KVM_SYNC_GPRS |
Christian Borntraeger9eed07352012-02-06 10:59:07 +01001421 KVM_SYNC_ACRS |
David Hildenbrandb028ee32014-07-17 10:47:43 +02001422 KVM_SYNC_CRS |
1423 KVM_SYNC_ARCH0 |
1424 KVM_SYNC_PFAULT;
Fan Zhangc6e5f162016-01-07 18:24:29 +08001425 if (test_kvm_facility(vcpu->kvm, 64))
1426 vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
David Hildenbrandf6aa6dc2016-01-15 14:11:46 +01001427 /* fprs can be synchronized via vrs, even if the guest has no vx. With
1428 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
1429 */
1430 if (MACHINE_HAS_VX)
Eric Farman68c55752014-06-09 10:57:26 -04001431 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
David Hildenbrand6fd8e672016-01-18 14:46:34 +01001432 else
1433 vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
Dominik Dingeldafd0322014-12-02 16:53:21 +01001434
1435 if (kvm_is_ucontrol(vcpu->kvm))
1436 return __kvm_ucontrol_vcpu_init(vcpu);
1437
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001438 return 0;
1439}
1440
David Hildenbranddb0758b2016-02-15 09:42:25 +01001441/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1442static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1443{
1444 WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
David Hildenbrand9c23a132016-02-17 21:53:33 +01001445 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001446 vcpu->arch.cputm_start = get_tod_clock_fast();
David Hildenbrand9c23a132016-02-17 21:53:33 +01001447 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001448}
1449
1450/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1451static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1452{
1453 WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
David Hildenbrand9c23a132016-02-17 21:53:33 +01001454 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001455 vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
1456 vcpu->arch.cputm_start = 0;
David Hildenbrand9c23a132016-02-17 21:53:33 +01001457 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001458}
1459
1460/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1461static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1462{
1463 WARN_ON_ONCE(vcpu->arch.cputm_enabled);
1464 vcpu->arch.cputm_enabled = true;
1465 __start_cpu_timer_accounting(vcpu);
1466}
1467
1468/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1469static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1470{
1471 WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
1472 __stop_cpu_timer_accounting(vcpu);
1473 vcpu->arch.cputm_enabled = false;
1474}
1475
1476static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1477{
1478 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1479 __enable_cpu_timer_accounting(vcpu);
1480 preempt_enable();
1481}
1482
1483static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1484{
1485 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1486 __disable_cpu_timer_accounting(vcpu);
1487 preempt_enable();
1488}
1489
David Hildenbrand4287f242016-02-15 09:40:12 +01001490/* set the cpu timer - may only be called from the VCPU thread itself */
1491void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
1492{
David Hildenbranddb0758b2016-02-15 09:42:25 +01001493 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
David Hildenbrand9c23a132016-02-17 21:53:33 +01001494 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001495 if (vcpu->arch.cputm_enabled)
1496 vcpu->arch.cputm_start = get_tod_clock_fast();
David Hildenbrand4287f242016-02-15 09:40:12 +01001497 vcpu->arch.sie_block->cputm = cputm;
David Hildenbrand9c23a132016-02-17 21:53:33 +01001498 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
David Hildenbranddb0758b2016-02-15 09:42:25 +01001499 preempt_enable();
David Hildenbrand4287f242016-02-15 09:40:12 +01001500}
1501
David Hildenbranddb0758b2016-02-15 09:42:25 +01001502/* update and get the cpu timer - can also be called from other VCPU threads */
David Hildenbrand4287f242016-02-15 09:40:12 +01001503__u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
1504{
David Hildenbrand9c23a132016-02-17 21:53:33 +01001505 unsigned int seq;
David Hildenbranddb0758b2016-02-15 09:42:25 +01001506 __u64 value;
David Hildenbranddb0758b2016-02-15 09:42:25 +01001507
1508 if (unlikely(!vcpu->arch.cputm_enabled))
1509 return vcpu->arch.sie_block->cputm;
1510
David Hildenbrand9c23a132016-02-17 21:53:33 +01001511 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1512 do {
1513 seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
1514 /*
1515 * If the writer would ever execute a read in the critical
1516 * section, e.g. in irq context, we have a deadlock.
1517 */
1518 WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
1519 value = vcpu->arch.sie_block->cputm;
1520 /* if cputm_start is 0, accounting is being started/stopped */
1521 if (likely(vcpu->arch.cputm_start))
1522 value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
1523 } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
1524 preempt_enable();
David Hildenbranddb0758b2016-02-15 09:42:25 +01001525 return value;
David Hildenbrand4287f242016-02-15 09:40:12 +01001526}
1527
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001528void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1529{
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001530 /* Save host register state */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02001531 save_fpu_regs();
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001532 vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
1533 vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
Hendrik Brueckner96b2d7a2015-06-12 13:53:51 +02001534
David Hildenbrand6fd8e672016-01-18 14:46:34 +01001535 if (MACHINE_HAS_VX)
1536 current->thread.fpu.regs = vcpu->run->s.regs.vrs;
1537 else
1538 current->thread.fpu.regs = vcpu->run->s.regs.fprs;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001539 current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001540 if (test_fp_ctl(current->thread.fpu.fpc))
Hendrik Brueckner96b2d7a2015-06-12 13:53:51 +02001541 /* User space provided an invalid FPC, let's clear it */
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001542 current->thread.fpu.fpc = 0;
1543
1544 save_access_regs(vcpu->arch.host_acrs);
Christian Borntraeger59674c12012-01-11 11:20:33 +01001545 restore_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001546 gmap_enable(vcpu->arch.gmap);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001547 atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand5ebda312016-02-22 13:52:27 +01001548 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
David Hildenbranddb0758b2016-02-15 09:42:25 +01001549 __start_cpu_timer_accounting(vcpu);
David Hildenbrand01a745a2016-02-12 20:41:56 +01001550 vcpu->cpu = cpu;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001551}
1552
1553void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1554{
David Hildenbrand01a745a2016-02-12 20:41:56 +01001555 vcpu->cpu = -1;
David Hildenbrand5ebda312016-02-22 13:52:27 +01001556 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
David Hildenbranddb0758b2016-02-15 09:42:25 +01001557 __stop_cpu_timer_accounting(vcpu);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001558 atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger480e5922011-09-20 17:07:28 +02001559 gmap_disable(vcpu->arch.gmap);
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001560
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001561 /* Save guest register state */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02001562 save_fpu_regs();
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001563 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001564
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001565 /* Restore host register state */
1566 current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
1567 current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
Hendrik Brueckner9977e882015-06-10 12:53:42 +02001568
1569 save_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001570 restore_access_regs(vcpu->arch.host_acrs);
1571}
1572
1573static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1574{
1575 /* this equals initial cpu reset in pop, but we don't switch to ESA */
1576 vcpu->arch.sie_block->gpsw.mask = 0UL;
1577 vcpu->arch.sie_block->gpsw.addr = 0UL;
Christian Borntraeger8d26cf72012-01-11 11:19:32 +01001578 kvm_s390_set_prefix(vcpu, 0);
David Hildenbrand4287f242016-02-15 09:40:12 +01001579 kvm_s390_set_cpu_timer(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001580 vcpu->arch.sie_block->ckc = 0UL;
1581 vcpu->arch.sie_block->todpr = 0;
1582 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1583 vcpu->arch.sie_block->gcr[0] = 0xE0UL;
1584 vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001585 /* make sure the new fpc will be lazily loaded */
1586 save_fpu_regs();
1587 current->thread.fpu.fpc = 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001588 vcpu->arch.sie_block->gbea = 1;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001589 vcpu->arch.sie_block->pp = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001590 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1591 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrand6352e4d2014-04-10 17:35:00 +02001592 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
1593 kvm_s390_vcpu_stop(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001594 kvm_s390_clear_local_irqs(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001595}
1596
Dominik Dingel31928aa2014-12-04 15:47:07 +01001597void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001598{
Jason J. Herne72f25022014-11-25 09:46:02 -05001599 mutex_lock(&vcpu->kvm->lock);
Fan Zhangfdf03652015-05-13 10:58:41 +02001600 preempt_disable();
Jason J. Herne72f25022014-11-25 09:46:02 -05001601 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
Fan Zhangfdf03652015-05-13 10:58:41 +02001602 preempt_enable();
Jason J. Herne72f25022014-11-25 09:46:02 -05001603 mutex_unlock(&vcpu->kvm->lock);
David Hildenbrand25508822015-10-12 16:27:23 +02001604 if (!kvm_is_ucontrol(vcpu->kvm)) {
Dominik Dingeldafd0322014-12-02 16:53:21 +01001605 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
David Hildenbrandeaa78f32015-10-12 16:29:01 +02001606 sca_add_vcpu(vcpu);
David Hildenbrand25508822015-10-12 16:27:23 +02001607 }
1608
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001609}
1610
Tony Krowiak5102ee82014-06-27 14:46:01 -04001611static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
1612{
Michael Mueller9d8d5782015-02-02 15:42:51 +01001613 if (!test_kvm_facility(vcpu->kvm, 76))
Tony Krowiak5102ee82014-06-27 14:46:01 -04001614 return;
1615
Tony Krowiaka374e892014-09-03 10:13:53 +02001616 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1617
1618 if (vcpu->kvm->arch.crypto.aes_kw)
1619 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1620 if (vcpu->kvm->arch.crypto.dea_kw)
1621 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1622
Tony Krowiak5102ee82014-06-27 14:46:01 -04001623 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
1624}
1625
Dominik Dingelb31605c2014-03-25 13:47:11 +01001626void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1627{
1628 free_page(vcpu->arch.sie_block->cbrlo);
1629 vcpu->arch.sie_block->cbrlo = 0;
1630}
1631
1632int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1633{
1634 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1635 if (!vcpu->arch.sie_block->cbrlo)
1636 return -ENOMEM;
1637
1638 vcpu->arch.sie_block->ecb2 |= 0x80;
1639 vcpu->arch.sie_block->ecb2 &= ~0x08;
1640 return 0;
1641}
1642
Michael Mueller91520f12015-02-27 14:32:11 +01001643static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
1644{
1645 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
1646
Michael Mueller91520f12015-02-27 14:32:11 +01001647 vcpu->arch.sie_block->ibc = model->ibc;
David Hildenbrand80bc79d2015-12-02 09:43:29 +01001648 if (test_kvm_facility(vcpu->kvm, 7))
David Hildenbrandc54f0d62015-12-02 08:53:52 +01001649 vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
Michael Mueller91520f12015-02-27 14:32:11 +01001650}
1651
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001652int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1653{
Dominik Dingelb31605c2014-03-25 13:47:11 +01001654 int rc = 0;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001655
Cornelia Huck9e6dabe2011-11-17 11:00:41 +01001656 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
1657 CPUSTAT_SM |
Guenther Hutzla4a4f192015-03-31 14:39:49 +02001658 CPUSTAT_STOPPED);
1659
Guenther Hutzl53df84f2015-02-18 11:13:03 +01001660 if (test_kvm_facility(vcpu->kvm, 78))
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001661 atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
Guenther Hutzl53df84f2015-02-18 11:13:03 +01001662 else if (test_kvm_facility(vcpu->kvm, 8))
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001663 atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
Guenther Hutzla4a4f192015-03-31 14:39:49 +02001664
Michael Mueller91520f12015-02-27 14:32:11 +01001665 kvm_s390_vcpu_setup_model(vcpu);
1666
David Hildenbrandbd50e8e2016-03-04 12:23:55 +01001667 vcpu->arch.sie_block->ecb = 0x02;
1668 if (test_kvm_facility(vcpu->kvm, 9))
1669 vcpu->arch.sie_block->ecb |= 0x04;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001670 if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73))
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001671 vcpu->arch.sie_block->ecb |= 0x10;
1672
David Hildenbrandd6af0b42016-03-04 11:55:56 +01001673 if (test_kvm_facility(vcpu->kvm, 8))
1674 vcpu->arch.sie_block->ecb2 |= 0x08;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001675 vcpu->arch.sie_block->eca = 0xC1002000U;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001676 if (sclp.has_siif)
Heiko Carstens217a4402013-12-30 12:54:14 +01001677 vcpu->arch.sie_block->eca |= 1;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001678 if (sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001679 vcpu->arch.sie_block->eca |= 0x10000000U;
Fan Zhangc6e5f162016-01-07 18:24:29 +08001680 if (test_kvm_facility(vcpu->kvm, 64))
1681 vcpu->arch.sie_block->ecb3 |= 0x01;
Michael Mueller18280d82015-03-16 16:05:41 +01001682 if (test_kvm_facility(vcpu->kvm, 129)) {
Eric Farman13211ea2014-04-30 13:39:46 -04001683 vcpu->arch.sie_block->eca |= 0x00020000;
1684 vcpu->arch.sie_block->ecd |= 0x20000000;
1685 }
Fan Zhangc6e5f162016-01-07 18:24:29 +08001686 vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
Thomas Huth492d8642015-02-10 16:11:01 +01001687 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
Janosch Frank95ca2cb2016-05-23 15:11:58 +02001688 if (test_kvm_facility(vcpu->kvm, 74))
1689 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
Matthew Rosato5a5e6532013-01-29 11:48:20 -05001690
Dominik Dingele6db1d62015-05-07 15:41:57 +02001691 if (vcpu->kvm->arch.use_cmma) {
Dominik Dingelb31605c2014-03-25 13:47:11 +01001692 rc = kvm_s390_vcpu_setup_cmma(vcpu);
1693 if (rc)
1694 return rc;
Konstantin Weitzb31288f2013-04-17 17:36:29 +02001695 }
David Hildenbrand0ac96caf2014-12-12 15:17:31 +01001696 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Christian Borntraegerca872302009-05-12 17:21:49 +02001697 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
Michael Mueller9d8d5782015-02-02 15:42:51 +01001698
Tony Krowiak5102ee82014-06-27 14:46:01 -04001699 kvm_s390_vcpu_crypto_setup(vcpu);
1700
Dominik Dingelb31605c2014-03-25 13:47:11 +01001701 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001702}
1703
1704struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1705 unsigned int id)
1706{
Carsten Otte4d475552011-10-18 12:27:12 +02001707 struct kvm_vcpu *vcpu;
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001708 struct sie_page *sie_page;
Carsten Otte4d475552011-10-18 12:27:12 +02001709 int rc = -EINVAL;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001710
David Hildenbrand42158252015-10-12 12:57:22 +02001711 if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
Carsten Otte4d475552011-10-18 12:27:12 +02001712 goto out;
1713
1714 rc = -ENOMEM;
1715
Michael Muellerb110fea2013-06-12 13:54:54 +02001716 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001717 if (!vcpu)
Carsten Otte4d475552011-10-18 12:27:12 +02001718 goto out;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001719
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001720 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
1721 if (!sie_page)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001722 goto out_free_cpu;
1723
Michael Mueller7feb6bb2013-06-28 13:30:24 +02001724 vcpu->arch.sie_block = &sie_page->sie_block;
1725 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
1726
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001727 vcpu->arch.sie_block->icpua = id;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001728 spin_lock_init(&vcpu->arch.local_int.lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001729 vcpu->arch.local_int.float_int = &kvm->arch.float_int;
Christian Borntraegerd0321a22013-06-12 13:54:55 +02001730 vcpu->arch.local_int.wq = &vcpu->wq;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +01001731 vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
David Hildenbrand9c23a132016-02-17 21:53:33 +01001732 seqcount_init(&vcpu->arch.cputm_seqcount);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001733
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001734 rc = kvm_vcpu_init(vcpu, kvm, id);
1735 if (rc)
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001736 goto out_free_sie_block;
Christian Borntraeger8335713a2015-12-08 16:55:27 +01001737 VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001738 vcpu->arch.sie_block);
Cornelia Huckade38c32012-07-23 17:20:30 +02001739 trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001740
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001741 return vcpu;
Wei Yongjun7b06bf22010-03-09 14:37:53 +08001742out_free_sie_block:
1743 free_page((unsigned long)(vcpu->arch.sie_block));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001744out_free_cpu:
Michael Muellerb110fea2013-06-12 13:54:54 +02001745 kmem_cache_free(kvm_vcpu_cache, vcpu);
Carsten Otte4d475552011-10-18 12:27:12 +02001746out:
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001747 return ERR_PTR(rc);
1748}
1749
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001750int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1751{
David Hildenbrand9a022062014-08-05 17:40:47 +02001752 return kvm_s390_vcpu_has_irq(vcpu, 0);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001753}
1754
Christian Borntraeger27406cd2015-04-14 12:17:34 +02001755void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001756{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001757 atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
David Hildenbrand61a6df52015-05-12 08:41:40 +02001758 exit_sie(vcpu);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001759}
1760
Christian Borntraeger27406cd2015-04-14 12:17:34 +02001761void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001762{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001763 atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001764}
1765
Christian Borntraeger8e236542015-04-09 13:49:04 +02001766static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
1767{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001768 atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
David Hildenbrand61a6df52015-05-12 08:41:40 +02001769 exit_sie(vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001770}
1771
1772static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
1773{
Jason J. Herne9bf9fde2015-09-16 09:13:50 -04001774 atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001775}
1776
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001777/*
1778 * Kick a guest cpu out of SIE and wait until SIE is not running.
1779 * If the CPU is not running (e.g. waiting as idle) the function will
1780 * return immediately. */
1781void exit_sie(struct kvm_vcpu *vcpu)
1782{
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001783 atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001784 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
1785 cpu_relax();
1786}
1787
Christian Borntraeger8e236542015-04-09 13:49:04 +02001788/* Kick a guest cpu out of SIE to process a request synchronously */
1789void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001790{
Christian Borntraeger8e236542015-04-09 13:49:04 +02001791 kvm_make_request(req, vcpu);
1792 kvm_s390_vcpu_request(vcpu);
Christian Borntraeger49b99e12013-05-17 14:41:35 +02001793}
1794
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001795static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
1796{
1797 int i;
1798 struct kvm *kvm = gmap->private;
1799 struct kvm_vcpu *vcpu;
1800
1801 kvm_for_each_vcpu(i, vcpu, kvm) {
1802 /* match against both prefix pages */
Michael Muellerfda902c2014-05-13 16:58:30 +02001803 if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001804 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
Christian Borntraeger8e236542015-04-09 13:49:04 +02001805 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02001806 }
1807 }
1808}
1809
Christoffer Dallb6d33832012-03-08 16:44:24 -05001810int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1811{
1812 /* kvm common code refers to this, but never calls it */
1813 BUG();
1814 return 0;
1815}
1816
Carsten Otte14eebd92012-05-15 14:15:26 +02001817static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
1818 struct kvm_one_reg *reg)
1819{
1820 int r = -EINVAL;
1821
1822 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001823 case KVM_REG_S390_TODPR:
1824 r = put_user(vcpu->arch.sie_block->todpr,
1825 (u32 __user *)reg->addr);
1826 break;
1827 case KVM_REG_S390_EPOCHDIFF:
1828 r = put_user(vcpu->arch.sie_block->epoch,
1829 (u64 __user *)reg->addr);
1830 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001831 case KVM_REG_S390_CPU_TIMER:
David Hildenbrand4287f242016-02-15 09:40:12 +01001832 r = put_user(kvm_s390_get_cpu_timer(vcpu),
Jason J. herne46a6dd12012-05-15 14:15:28 +02001833 (u64 __user *)reg->addr);
1834 break;
1835 case KVM_REG_S390_CLOCK_COMP:
1836 r = put_user(vcpu->arch.sie_block->ckc,
1837 (u64 __user *)reg->addr);
1838 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001839 case KVM_REG_S390_PFTOKEN:
1840 r = put_user(vcpu->arch.pfault_token,
1841 (u64 __user *)reg->addr);
1842 break;
1843 case KVM_REG_S390_PFCOMPARE:
1844 r = put_user(vcpu->arch.pfault_compare,
1845 (u64 __user *)reg->addr);
1846 break;
1847 case KVM_REG_S390_PFSELECT:
1848 r = put_user(vcpu->arch.pfault_select,
1849 (u64 __user *)reg->addr);
1850 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001851 case KVM_REG_S390_PP:
1852 r = put_user(vcpu->arch.sie_block->pp,
1853 (u64 __user *)reg->addr);
1854 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001855 case KVM_REG_S390_GBEA:
1856 r = put_user(vcpu->arch.sie_block->gbea,
1857 (u64 __user *)reg->addr);
1858 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001859 default:
1860 break;
1861 }
1862
1863 return r;
1864}
1865
1866static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
1867 struct kvm_one_reg *reg)
1868{
1869 int r = -EINVAL;
David Hildenbrand4287f242016-02-15 09:40:12 +01001870 __u64 val;
Carsten Otte14eebd92012-05-15 14:15:26 +02001871
1872 switch (reg->id) {
Carsten Otte29b7c712012-05-15 14:15:27 +02001873 case KVM_REG_S390_TODPR:
1874 r = get_user(vcpu->arch.sie_block->todpr,
1875 (u32 __user *)reg->addr);
1876 break;
1877 case KVM_REG_S390_EPOCHDIFF:
1878 r = get_user(vcpu->arch.sie_block->epoch,
1879 (u64 __user *)reg->addr);
1880 break;
Jason J. herne46a6dd12012-05-15 14:15:28 +02001881 case KVM_REG_S390_CPU_TIMER:
David Hildenbrand4287f242016-02-15 09:40:12 +01001882 r = get_user(val, (u64 __user *)reg->addr);
1883 if (!r)
1884 kvm_s390_set_cpu_timer(vcpu, val);
Jason J. herne46a6dd12012-05-15 14:15:28 +02001885 break;
1886 case KVM_REG_S390_CLOCK_COMP:
1887 r = get_user(vcpu->arch.sie_block->ckc,
1888 (u64 __user *)reg->addr);
1889 break;
Dominik Dingel536336c2013-09-30 10:55:33 +02001890 case KVM_REG_S390_PFTOKEN:
1891 r = get_user(vcpu->arch.pfault_token,
1892 (u64 __user *)reg->addr);
David Hildenbrand9fbd8082014-10-09 15:01:38 +02001893 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
1894 kvm_clear_async_pf_completion_queue(vcpu);
Dominik Dingel536336c2013-09-30 10:55:33 +02001895 break;
1896 case KVM_REG_S390_PFCOMPARE:
1897 r = get_user(vcpu->arch.pfault_compare,
1898 (u64 __user *)reg->addr);
1899 break;
1900 case KVM_REG_S390_PFSELECT:
1901 r = get_user(vcpu->arch.pfault_select,
1902 (u64 __user *)reg->addr);
1903 break;
Christian Borntraeger672550f2014-02-10 15:32:19 +01001904 case KVM_REG_S390_PP:
1905 r = get_user(vcpu->arch.sie_block->pp,
1906 (u64 __user *)reg->addr);
1907 break;
Christian Borntraegerafa45ff2014-02-10 15:39:23 +01001908 case KVM_REG_S390_GBEA:
1909 r = get_user(vcpu->arch.sie_block->gbea,
1910 (u64 __user *)reg->addr);
1911 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02001912 default:
1913 break;
1914 }
1915
1916 return r;
1917}
Christoffer Dallb6d33832012-03-08 16:44:24 -05001918
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001919static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
1920{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001921 kvm_s390_vcpu_initial_reset(vcpu);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001922 return 0;
1923}
1924
1925int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1926{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001927 memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001928 return 0;
1929}
1930
1931int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1932{
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +01001933 memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001934 return 0;
1935}
1936
1937int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1938 struct kvm_sregs *sregs)
1939{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001940 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001941 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
Christian Borntraeger59674c12012-01-11 11:20:33 +01001942 restore_access_regs(vcpu->run->s.regs.acrs);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001943 return 0;
1944}
1945
1946int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1947 struct kvm_sregs *sregs)
1948{
Christian Borntraeger59674c12012-01-11 11:20:33 +01001949 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001950 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001951 return 0;
1952}
1953
1954int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1955{
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001956 /* make sure the new values will be lazily loaded */
1957 save_fpu_regs();
Martin Schwidefsky4725c862013-10-15 16:08:34 +02001958 if (test_fp_ctl(fpu->fpc))
1959 return -EINVAL;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001960 current->thread.fpu.fpc = fpu->fpc;
1961 if (MACHINE_HAS_VX)
1962 convert_fp_to_vx(current->thread.fpu.vxrs, (freg_t *)fpu->fprs);
1963 else
1964 memcpy(current->thread.fpu.fprs, &fpu->fprs, sizeof(fpu->fprs));
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001965 return 0;
1966}
1967
1968int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1969{
David Hildenbrand9abc2a02016-01-14 22:12:47 +01001970 /* make sure we have the latest values */
1971 save_fpu_regs();
1972 if (MACHINE_HAS_VX)
1973 convert_vx_to_fp((freg_t *)fpu->fprs, current->thread.fpu.vxrs);
1974 else
1975 memcpy(fpu->fprs, current->thread.fpu.fprs, sizeof(fpu->fprs));
1976 fpu->fpc = current->thread.fpu.fpc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001977 return 0;
1978}
1979
1980static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
1981{
1982 int rc = 0;
1983
David Hildenbrand7a42fdc2014-05-05 16:26:19 +02001984 if (!is_vcpu_stopped(vcpu))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001985 rc = -EBUSY;
Carsten Otted7b0b5e2009-11-19 14:21:16 +01001986 else {
1987 vcpu->run->psw_mask = psw.mask;
1988 vcpu->run->psw_addr = psw.addr;
1989 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01001990 return rc;
1991}
1992
1993int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1994 struct kvm_translation *tr)
1995{
1996 return -EINVAL; /* not implemented yet */
1997}
1998
David Hildenbrand27291e22014-01-23 12:26:52 +01001999#define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
2000 KVM_GUESTDBG_USE_HW_BP | \
2001 KVM_GUESTDBG_ENABLE)
2002
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002003int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2004 struct kvm_guest_debug *dbg)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002005{
David Hildenbrand27291e22014-01-23 12:26:52 +01002006 int rc = 0;
2007
2008 vcpu->guest_debug = 0;
2009 kvm_s390_clear_bp_data(vcpu);
2010
David Hildenbrand2de3bfc2014-05-20 17:25:20 +02002011 if (dbg->control & ~VALID_GUESTDBG_FLAGS)
David Hildenbrand27291e22014-01-23 12:26:52 +01002012 return -EINVAL;
2013
2014 if (dbg->control & KVM_GUESTDBG_ENABLE) {
2015 vcpu->guest_debug = dbg->control;
2016 /* enforce guest PER */
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002017 atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand27291e22014-01-23 12:26:52 +01002018
2019 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
2020 rc = kvm_s390_import_bp_data(vcpu, dbg);
2021 } else {
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002022 atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand27291e22014-01-23 12:26:52 +01002023 vcpu->arch.guestdbg.last_bp = 0;
2024 }
2025
2026 if (rc) {
2027 vcpu->guest_debug = 0;
2028 kvm_s390_clear_bp_data(vcpu);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002029 atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand27291e22014-01-23 12:26:52 +01002030 }
2031
2032 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002033}
2034
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002035int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
2036 struct kvm_mp_state *mp_state)
2037{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002038 /* CHECK_STOP and LOAD are not supported yet */
2039 return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
2040 KVM_MP_STATE_OPERATING;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002041}
2042
2043int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
2044 struct kvm_mp_state *mp_state)
2045{
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002046 int rc = 0;
2047
2048 /* user space knows about this interface - let it control the state */
2049 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
2050
2051 switch (mp_state->mp_state) {
2052 case KVM_MP_STATE_STOPPED:
2053 kvm_s390_vcpu_stop(vcpu);
2054 break;
2055 case KVM_MP_STATE_OPERATING:
2056 kvm_s390_vcpu_start(vcpu);
2057 break;
2058 case KVM_MP_STATE_LOAD:
2059 case KVM_MP_STATE_CHECK_STOP:
2060 /* fall through - CHECK_STOP and LOAD are not supported yet */
2061 default:
2062 rc = -ENXIO;
2063 }
2064
2065 return rc;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002066}
2067
David Hildenbrand8ad35752014-03-14 11:00:21 +01002068static bool ibs_enabled(struct kvm_vcpu *vcpu)
2069{
2070 return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
2071}
2072
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002073static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
2074{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002075retry:
Christian Borntraeger8e236542015-04-09 13:49:04 +02002076 kvm_s390_vcpu_request_handled(vcpu);
Christian Borntraeger586b7cc2015-07-28 15:03:05 +02002077 if (!vcpu->requests)
2078 return 0;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002079 /*
2080 * We use MMU_RELOAD just to re-arm the ipte notifier for the
2081 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
2082 * This ensures that the ipte instruction for this request has
2083 * already finished. We might race against a second unmapper that
2084 * wants to set the blocking bit. Lets just retry the request loop.
2085 */
David Hildenbrand8ad35752014-03-14 11:00:21 +01002086 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002087 int rc;
2088 rc = gmap_ipte_notify(vcpu->arch.gmap,
Michael Muellerfda902c2014-05-13 16:58:30 +02002089 kvm_s390_get_prefix(vcpu),
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002090 PAGE_SIZE * 2);
2091 if (rc)
2092 return rc;
David Hildenbrand8ad35752014-03-14 11:00:21 +01002093 goto retry;
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002094 }
David Hildenbrand8ad35752014-03-14 11:00:21 +01002095
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002096 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
2097 vcpu->arch.sie_block->ihcpu = 0xffff;
2098 goto retry;
2099 }
2100
David Hildenbrand8ad35752014-03-14 11:00:21 +01002101 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
2102 if (!ibs_enabled(vcpu)) {
2103 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002104 atomic_or(CPUSTAT_IBS,
David Hildenbrand8ad35752014-03-14 11:00:21 +01002105 &vcpu->arch.sie_block->cpuflags);
2106 }
2107 goto retry;
2108 }
2109
2110 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
2111 if (ibs_enabled(vcpu)) {
2112 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002113 atomic_andnot(CPUSTAT_IBS,
David Hildenbrand8ad35752014-03-14 11:00:21 +01002114 &vcpu->arch.sie_block->cpuflags);
2115 }
2116 goto retry;
2117 }
2118
David Hildenbrand0759d062014-05-13 16:54:32 +02002119 /* nothing to do, just clear the request */
2120 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
2121
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002122 return 0;
2123}
2124
David Hildenbrand25ed1672015-05-12 09:49:14 +02002125void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
2126{
2127 struct kvm_vcpu *vcpu;
2128 int i;
2129
2130 mutex_lock(&kvm->lock);
2131 preempt_disable();
2132 kvm->arch.epoch = tod - get_tod_clock();
2133 kvm_s390_vcpu_block_all(kvm);
2134 kvm_for_each_vcpu(i, vcpu, kvm)
2135 vcpu->arch.sie_block->epoch = kvm->arch.epoch;
2136 kvm_s390_vcpu_unblock_all(kvm);
2137 preempt_enable();
2138 mutex_unlock(&kvm->lock);
2139}
2140
Thomas Huthfa576c52014-05-06 17:20:16 +02002141/**
2142 * kvm_arch_fault_in_page - fault-in guest page if necessary
2143 * @vcpu: The corresponding virtual cpu
2144 * @gpa: Guest physical address
2145 * @writable: Whether the page should be writable or not
2146 *
2147 * Make sure that a guest page has been faulted-in on the host.
2148 *
2149 * Return: Zero on success, negative error code otherwise.
2150 */
2151long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002152{
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002153 return gmap_fault(vcpu->arch.gmap, gpa,
2154 writable ? FAULT_FLAG_WRITE : 0);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002155}
2156
Dominik Dingel3c038e62013-10-07 17:11:48 +02002157static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
2158 unsigned long token)
2159{
2160 struct kvm_s390_interrupt inti;
Jens Freimann383d0b02014-07-29 15:11:49 +02002161 struct kvm_s390_irq irq;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002162
2163 if (start_token) {
Jens Freimann383d0b02014-07-29 15:11:49 +02002164 irq.u.ext.ext_params2 = token;
2165 irq.type = KVM_S390_INT_PFAULT_INIT;
2166 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
Dominik Dingel3c038e62013-10-07 17:11:48 +02002167 } else {
2168 inti.type = KVM_S390_INT_PFAULT_DONE;
Jens Freimann383d0b02014-07-29 15:11:49 +02002169 inti.parm64 = token;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002170 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
2171 }
2172}
2173
2174void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
2175 struct kvm_async_pf *work)
2176{
2177 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
2178 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
2179}
2180
2181void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
2182 struct kvm_async_pf *work)
2183{
2184 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
2185 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
2186}
2187
2188void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
2189 struct kvm_async_pf *work)
2190{
2191 /* s390 will always inject the page directly */
2192}
2193
2194bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
2195{
2196 /*
2197 * s390 will always inject the page directly,
2198 * but we still want check_async_completion to cleanup
2199 */
2200 return true;
2201}
2202
2203static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
2204{
2205 hva_t hva;
2206 struct kvm_arch_async_pf arch;
2207 int rc;
2208
2209 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
2210 return 0;
2211 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
2212 vcpu->arch.pfault_compare)
2213 return 0;
2214 if (psw_extint_disabled(vcpu))
2215 return 0;
David Hildenbrand9a022062014-08-05 17:40:47 +02002216 if (kvm_s390_vcpu_has_irq(vcpu, 0))
Dominik Dingel3c038e62013-10-07 17:11:48 +02002217 return 0;
2218 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
2219 return 0;
2220 if (!vcpu->arch.gmap->pfault_enabled)
2221 return 0;
2222
Heiko Carstens81480cc2014-01-01 16:36:07 +01002223 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
2224 hva += current->thread.gmap_addr & ~PAGE_MASK;
2225 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
Dominik Dingel3c038e62013-10-07 17:11:48 +02002226 return 0;
2227
2228 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
2229 return rc;
2230}
2231
Thomas Huth3fb4c402013-09-12 10:33:43 +02002232static int vcpu_pre_run(struct kvm_vcpu *vcpu)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002233{
Thomas Huth3fb4c402013-09-12 10:33:43 +02002234 int rc, cpuflags;
Carsten Ottee168bf82012-01-04 10:25:22 +01002235
Dominik Dingel3c038e62013-10-07 17:11:48 +02002236 /*
2237 * On s390 notifications for arriving pages will be delivered directly
2238 * to the guest but the house keeping for completed pfaults is
2239 * handled outside the worker.
2240 */
2241 kvm_check_async_pf_completion(vcpu);
2242
Christian Borntraeger7ec7c8c2015-12-02 14:27:03 +01002243 vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
2244 vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002245
2246 if (need_resched())
2247 schedule();
2248
Martin Schwidefskyd3a73ac2014-04-15 12:55:07 +02002249 if (test_cpu_flag(CIF_MCCK_PENDING))
Christian Borntraeger71cde582008-05-21 13:37:34 +02002250 s390_handle_mcck();
2251
Jens Freimann79395032014-04-17 10:10:30 +02002252 if (!kvm_is_ucontrol(vcpu->kvm)) {
2253 rc = kvm_s390_deliver_pending_interrupts(vcpu);
2254 if (rc)
2255 return rc;
2256 }
Carsten Otte0ff31862008-05-21 13:37:37 +02002257
Christian Borntraeger2c70fe42013-05-17 14:41:36 +02002258 rc = kvm_s390_handle_requests(vcpu);
2259 if (rc)
2260 return rc;
2261
David Hildenbrand27291e22014-01-23 12:26:52 +01002262 if (guestdbg_enabled(vcpu)) {
2263 kvm_s390_backup_guest_per_regs(vcpu);
2264 kvm_s390_patch_guest_per_regs(vcpu);
2265 }
2266
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002267 vcpu->arch.sie_block->icptcode = 0;
Thomas Huth3fb4c402013-09-12 10:33:43 +02002268 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
2269 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
2270 trace_kvm_s390_sie_enter(vcpu, cpuflags);
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02002271
Thomas Huth3fb4c402013-09-12 10:33:43 +02002272 return 0;
2273}
2274
Thomas Huth492d8642015-02-10 16:11:01 +01002275static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
2276{
David Hildenbrand56317922016-01-12 17:37:58 +01002277 struct kvm_s390_pgm_info pgm_info = {
2278 .code = PGM_ADDRESSING,
2279 };
2280 u8 opcode, ilen;
Thomas Huth492d8642015-02-10 16:11:01 +01002281 int rc;
2282
2283 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
2284 trace_kvm_s390_sie_fault(vcpu);
2285
2286 /*
2287 * We want to inject an addressing exception, which is defined as a
2288 * suppressing or terminating exception. However, since we came here
2289 * by a DAT access exception, the PSW still points to the faulting
2290 * instruction since DAT exceptions are nullifying. So we've got
2291 * to look up the current opcode to get the length of the instruction
2292 * to be able to forward the PSW.
2293 */
David Hildenbrand65977322015-11-16 16:17:45 +01002294 rc = read_guest_instr(vcpu, &opcode, 1);
David Hildenbrand56317922016-01-12 17:37:58 +01002295 ilen = insn_length(opcode);
David Hildenbrand9b0d7212016-01-12 17:40:54 +01002296 if (rc < 0) {
2297 return rc;
2298 } else if (rc) {
2299 /* Instruction-Fetching Exceptions - we can't detect the ilen.
2300 * Forward by arbitrary ilc, injection will take care of
2301 * nullification if necessary.
2302 */
2303 pgm_info = vcpu->arch.pgm;
2304 ilen = 4;
2305 }
David Hildenbrand56317922016-01-12 17:37:58 +01002306 pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
2307 kvm_s390_forward_psw(vcpu, ilen);
2308 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
Thomas Huth492d8642015-02-10 16:11:01 +01002309}
2310
Thomas Huth3fb4c402013-09-12 10:33:43 +02002311static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
2312{
Dominik Dingel2b29a9f2013-07-26 15:04:00 +02002313 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
2314 vcpu->arch.sie_block->icptcode);
2315 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
2316
David Hildenbrand27291e22014-01-23 12:26:52 +01002317 if (guestdbg_enabled(vcpu))
2318 kvm_s390_restore_guest_per_regs(vcpu);
2319
Christian Borntraeger7ec7c8c2015-12-02 14:27:03 +01002320 vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
2321 vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
David Hildenbrand71f116b2015-10-19 16:24:28 +02002322
2323 if (vcpu->arch.sie_block->icptcode > 0) {
2324 int rc = kvm_handle_sie_intercept(vcpu);
2325
2326 if (rc != -EOPNOTSUPP)
2327 return rc;
2328 vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
2329 vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
2330 vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
2331 vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
2332 return -EREMOTE;
2333 } else if (exit_reason != -EFAULT) {
2334 vcpu->stat.exit_null++;
2335 return 0;
Thomas Huth210b16072013-09-19 16:26:18 +02002336 } else if (kvm_is_ucontrol(vcpu->kvm)) {
2337 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
2338 vcpu->run->s390_ucontrol.trans_exc_code =
2339 current->thread.gmap_addr;
2340 vcpu->run->s390_ucontrol.pgm_code = 0x10;
David Hildenbrand71f116b2015-10-19 16:24:28 +02002341 return -EREMOTE;
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002342 } else if (current->thread.gmap_pfault) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02002343 trace_kvm_s390_major_guest_pfault(vcpu);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002344 current->thread.gmap_pfault = 0;
David Hildenbrand71f116b2015-10-19 16:24:28 +02002345 if (kvm_arch_setup_async_pf(vcpu))
2346 return 0;
2347 return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
Dominik Dingel24eb3a82013-06-17 16:25:18 +02002348 }
David Hildenbrand71f116b2015-10-19 16:24:28 +02002349 return vcpu_post_run_fault_in_sie(vcpu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002350}
2351
2352static int __vcpu_run(struct kvm_vcpu *vcpu)
2353{
2354 int rc, exit_reason;
2355
Thomas Huth800c1062013-09-12 10:33:45 +02002356 /*
2357 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2358 * ning the guest), so that memslots (and other stuff) are protected
2359 */
2360 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2361
Thomas Hutha76ccff2013-09-12 10:33:44 +02002362 do {
2363 rc = vcpu_pre_run(vcpu);
2364 if (rc)
2365 break;
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);
Thomas Hutha76ccff2013-09-12 10:33:44 +02002368 /*
2369 * As PF_VCPU will be used in fault handler, between
2370 * guest_enter and guest_exit should be no uaccess.
2371 */
Christian Borntraeger0097d122015-04-30 13:43:30 +02002372 local_irq_disable();
2373 __kvm_guest_enter();
David Hildenbranddb0758b2016-02-15 09:42:25 +01002374 __disable_cpu_timer_accounting(vcpu);
Christian Borntraeger0097d122015-04-30 13:43:30 +02002375 local_irq_enable();
Thomas Hutha76ccff2013-09-12 10:33:44 +02002376 exit_reason = sie64a(vcpu->arch.sie_block,
2377 vcpu->run->s.regs.gprs);
Christian Borntraeger0097d122015-04-30 13:43:30 +02002378 local_irq_disable();
David Hildenbranddb0758b2016-02-15 09:42:25 +01002379 __enable_cpu_timer_accounting(vcpu);
Christian Borntraeger0097d122015-04-30 13:43:30 +02002380 __kvm_guest_exit();
2381 local_irq_enable();
Thomas Huth800c1062013-09-12 10:33:45 +02002382 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002383
Thomas Hutha76ccff2013-09-12 10:33:44 +02002384 rc = vcpu_post_run(vcpu, exit_reason);
David Hildenbrand27291e22014-01-23 12:26:52 +01002385 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
Thomas Huth3fb4c402013-09-12 10:33:43 +02002386
Thomas Huth800c1062013-09-12 10:33:45 +02002387 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Carsten Ottee168bf82012-01-04 10:25:22 +01002388 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002389}
2390
David Hildenbrandb028ee32014-07-17 10:47:43 +02002391static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2392{
2393 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2394 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2395 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2396 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2397 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2398 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002399 /* some control register changes require a tlb flush */
2400 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002401 }
2402 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
David Hildenbrand4287f242016-02-15 09:40:12 +01002403 kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002404 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2405 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2406 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2407 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2408 }
2409 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2410 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2411 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2412 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
David Hildenbrand9fbd8082014-10-09 15:01:38 +02002413 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
2414 kvm_clear_async_pf_completion_queue(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002415 }
2416 kvm_run->kvm_dirty_regs = 0;
2417}
2418
2419static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2420{
2421 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
2422 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
2423 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
2424 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
David Hildenbrand4287f242016-02-15 09:40:12 +01002425 kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002426 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
2427 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
2428 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
2429 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
2430 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
2431 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
2432 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
2433}
2434
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002435int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2436{
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002437 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002438 sigset_t sigsaved;
2439
David Hildenbrand27291e22014-01-23 12:26:52 +01002440 if (guestdbg_exit_pending(vcpu)) {
2441 kvm_s390_prepare_debug_exit(vcpu);
2442 return 0;
2443 }
2444
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002445 if (vcpu->sigset_active)
2446 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2447
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002448 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
2449 kvm_s390_vcpu_start(vcpu);
2450 } else if (is_vcpu_stopped(vcpu)) {
David Hildenbrandea2cdd22015-05-20 13:24:02 +02002451 pr_err_ratelimited("can't run stopped vcpu %d\n",
David Hildenbrand6352e4d2014-04-10 17:35:00 +02002452 vcpu->vcpu_id);
2453 return -EINVAL;
2454 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002455
David Hildenbrandb028ee32014-07-17 10:47:43 +02002456 sync_regs(vcpu, kvm_run);
David Hildenbranddb0758b2016-02-15 09:42:25 +01002457 enable_cpu_timer_accounting(vcpu);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002458
Heiko Carstensdab4079d2009-06-12 10:26:32 +02002459 might_fault();
Thomas Hutha76ccff2013-09-12 10:33:44 +02002460 rc = __vcpu_run(vcpu);
Christian Ehrhardt9ace9032009-05-20 15:34:55 +02002461
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002462 if (signal_pending(current) && !rc) {
2463 kvm_run->exit_reason = KVM_EXIT_INTR;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002464 rc = -EINTR;
Christian Ehrhardtb1d16c42009-05-20 15:34:56 +02002465 }
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002466
David Hildenbrand27291e22014-01-23 12:26:52 +01002467 if (guestdbg_exit_pending(vcpu) && !rc) {
2468 kvm_s390_prepare_debug_exit(vcpu);
2469 rc = 0;
2470 }
2471
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002472 if (rc == -EREMOTE) {
David Hildenbrand71f116b2015-10-19 16:24:28 +02002473 /* userspace support is needed, kvm_run has been prepared */
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01002474 rc = 0;
2475 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002476
David Hildenbranddb0758b2016-02-15 09:42:25 +01002477 disable_cpu_timer_accounting(vcpu);
David Hildenbrandb028ee32014-07-17 10:47:43 +02002478 store_regs(vcpu, kvm_run);
Carsten Otted7b0b5e2009-11-19 14:21:16 +01002479
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002480 if (vcpu->sigset_active)
2481 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2482
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002483 vcpu->stat.exit_userspace++;
Heiko Carstens7e8e6ab2008-04-04 15:12:35 +02002484 return rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002485}
2486
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002487/*
2488 * store status at address
2489 * we use have two special cases:
2490 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2491 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2492 */
Heiko Carstensd0bce602014-01-01 16:45:58 +01002493int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002494{
Carsten Otte092670c2011-07-24 10:48:22 +02002495 unsigned char archmode = 1;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002496 freg_t fprs[NUM_FPRS];
Michael Muellerfda902c2014-05-13 16:58:30 +02002497 unsigned int px;
David Hildenbrand4287f242016-02-15 09:40:12 +01002498 u64 clkcomp, cputm;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002499 int rc;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002500
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002501 px = kvm_s390_get_prefix(vcpu);
Heiko Carstensd0bce602014-01-01 16:45:58 +01002502 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
2503 if (write_guest_abs(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002504 return -EFAULT;
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002505 gpa = 0;
Heiko Carstensd0bce602014-01-01 16:45:58 +01002506 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
2507 if (write_guest_real(vcpu, 163, &archmode, 1))
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002508 return -EFAULT;
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002509 gpa = px;
2510 } else
2511 gpa -= __LC_FPREGS_SAVE_AREA;
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002512
2513 /* manually convert vector registers if necessary */
2514 if (MACHINE_HAS_VX) {
David Hildenbrand9522b372016-03-08 12:24:30 +01002515 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002516 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
2517 fprs, 128);
2518 } else {
2519 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
David Hildenbrand6fd8e672016-01-18 14:46:34 +01002520 vcpu->run->s.regs.fprs, 128);
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002521 }
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002522 rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002523 vcpu->run->s.regs.gprs, 128);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002524 rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002525 &vcpu->arch.sie_block->gpsw, 16);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002526 rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
Michael Muellerfda902c2014-05-13 16:58:30 +02002527 &px, 4);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002528 rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002529 &vcpu->run->s.regs.fpc, 4);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002530 rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002531 &vcpu->arch.sie_block->todpr, 4);
David Hildenbrand4287f242016-02-15 09:40:12 +01002532 cputm = kvm_s390_get_cpu_timer(vcpu);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002533 rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
David Hildenbrand4287f242016-02-15 09:40:12 +01002534 &cputm, 8);
Thomas Huth178bd782013-11-13 20:28:18 +01002535 clkcomp = vcpu->arch.sie_block->ckc >> 8;
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002536 rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002537 &clkcomp, 8);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002538 rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002539 &vcpu->run->s.regs.acrs, 64);
Martin Schwidefskyd9a3a092015-10-23 09:02:32 +02002540 rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
Heiko Carstensd0bce602014-01-01 16:45:58 +01002541 &vcpu->arch.sie_block->gcr, 128);
2542 return rc ? -EFAULT : 0;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002543}
2544
Thomas Huthe8798922013-11-06 15:46:33 +01002545int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2546{
2547 /*
2548 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
2549 * copying in vcpu load/put. Lets update our copies before we save
2550 * it into the save area
2551 */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02002552 save_fpu_regs();
David Hildenbrand9abc2a02016-01-14 22:12:47 +01002553 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
Thomas Huthe8798922013-11-06 15:46:33 +01002554 save_access_regs(vcpu->run->s.regs.acrs);
2555
2556 return kvm_s390_store_status_unloaded(vcpu, addr);
2557}
2558
Eric Farmanbc17de72014-04-14 16:01:09 -04002559/*
2560 * store additional status at address
2561 */
2562int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
2563 unsigned long gpa)
2564{
2565 /* Only bits 0-53 are used for address formation */
2566 if (!(gpa & ~0x3ff))
2567 return 0;
2568
2569 return write_guest_abs(vcpu, gpa & ~0x3ff,
2570 (void *)&vcpu->run->s.regs.vrs, 512);
2571}
2572
2573int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr)
2574{
2575 if (!test_kvm_facility(vcpu->kvm, 129))
2576 return 0;
2577
2578 /*
2579 * The guest VXRS are in the host VXRs due to the lazy
Hendrik Brueckner9977e882015-06-10 12:53:42 +02002580 * copying in vcpu load/put. We can simply call save_fpu_regs()
2581 * to save the current register state because we are in the
2582 * middle of a load/put cycle.
2583 *
2584 * Let's update our copies before we save it into the save area.
Eric Farmanbc17de72014-04-14 16:01:09 -04002585 */
Hendrik Bruecknerd0164ee2015-06-29 16:43:06 +02002586 save_fpu_regs();
Eric Farmanbc17de72014-04-14 16:01:09 -04002587
2588 return kvm_s390_store_adtl_status_unloaded(vcpu, addr);
2589}
2590
David Hildenbrand8ad35752014-03-14 11:00:21 +01002591static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2592{
2593 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02002594 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002595}
2596
2597static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
2598{
2599 unsigned int i;
2600 struct kvm_vcpu *vcpu;
2601
2602 kvm_for_each_vcpu(i, vcpu, kvm) {
2603 __disable_ibs_on_vcpu(vcpu);
2604 }
2605}
2606
2607static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
2608{
2609 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
Christian Borntraeger8e236542015-04-09 13:49:04 +02002610 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002611}
2612
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002613void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
2614{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002615 int i, online_vcpus, started_vcpus = 0;
2616
2617 if (!is_vcpu_stopped(vcpu))
2618 return;
2619
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002620 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002621 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002622 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002623 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2624
2625 for (i = 0; i < online_vcpus; i++) {
2626 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
2627 started_vcpus++;
2628 }
2629
2630 if (started_vcpus == 0) {
2631 /* we're the only active VCPU -> speed it up */
2632 __enable_ibs_on_vcpu(vcpu);
2633 } else if (started_vcpus == 1) {
2634 /*
2635 * As we are starting a second VCPU, we have to disable
2636 * the IBS facility on all VCPUs to remove potentially
2637 * oustanding ENABLE requests.
2638 */
2639 __disable_ibs_on_all_vcpus(vcpu->kvm);
2640 }
2641
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002642 atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002643 /*
2644 * Another VCPU might have used IBS while we were offline.
2645 * Let's play safe and flush the VCPU at startup.
2646 */
David Hildenbrandd3d692c2014-07-29 08:53:36 +02002647 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002648 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002649 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002650}
2651
2652void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
2653{
David Hildenbrand8ad35752014-03-14 11:00:21 +01002654 int i, online_vcpus, started_vcpus = 0;
2655 struct kvm_vcpu *started_vcpu = NULL;
2656
2657 if (is_vcpu_stopped(vcpu))
2658 return;
2659
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002660 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002661 /* Only one cpu at a time may enter/leave the STOPPED state. */
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002662 spin_lock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002663 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
2664
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002665 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
David Hildenbrand6cddd432014-10-15 16:48:53 +02002666 kvm_s390_clear_stop_irq(vcpu);
David Hildenbrand32f5ff632014-04-14 12:40:03 +02002667
Peter Zijlstra805de8f42015-04-24 01:12:32 +02002668 atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002669 __disable_ibs_on_vcpu(vcpu);
2670
2671 for (i = 0; i < online_vcpus; i++) {
2672 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
2673 started_vcpus++;
2674 started_vcpu = vcpu->kvm->vcpus[i];
2675 }
2676 }
2677
2678 if (started_vcpus == 1) {
2679 /*
2680 * As we only have one VCPU left, we want to enable the
2681 * IBS facility for that VCPU to speed it up.
2682 */
2683 __enable_ibs_on_vcpu(started_vcpu);
2684 }
2685
David Hildenbrand433b9ee2014-05-06 16:11:14 +02002686 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
David Hildenbrand8ad35752014-03-14 11:00:21 +01002687 return;
David Hildenbrand6852d7b2014-03-14 10:59:29 +01002688}
2689
Cornelia Huckd6712df2012-12-20 15:32:11 +01002690static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2691 struct kvm_enable_cap *cap)
2692{
2693 int r;
2694
2695 if (cap->flags)
2696 return -EINVAL;
2697
2698 switch (cap->cap) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002699 case KVM_CAP_S390_CSS_SUPPORT:
2700 if (!vcpu->kvm->arch.css_support) {
2701 vcpu->kvm->arch.css_support = 1;
Christian Borntraegerc92ea7b2015-07-22 15:52:10 +02002702 VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01002703 trace_kvm_s390_enable_css(vcpu->kvm);
2704 }
2705 r = 0;
2706 break;
Cornelia Huckd6712df2012-12-20 15:32:11 +01002707 default:
2708 r = -EINVAL;
2709 break;
2710 }
2711 return r;
2712}
2713
Thomas Huth41408c282015-02-06 15:01:21 +01002714static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
2715 struct kvm_s390_mem_op *mop)
2716{
2717 void __user *uaddr = (void __user *)mop->buf;
2718 void *tmpbuf = NULL;
2719 int r, srcu_idx;
2720 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
2721 | KVM_S390_MEMOP_F_CHECK_ONLY;
2722
2723 if (mop->flags & ~supported_flags)
2724 return -EINVAL;
2725
2726 if (mop->size > MEM_OP_MAX_SIZE)
2727 return -E2BIG;
2728
2729 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
2730 tmpbuf = vmalloc(mop->size);
2731 if (!tmpbuf)
2732 return -ENOMEM;
2733 }
2734
2735 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2736
2737 switch (mop->op) {
2738 case KVM_S390_MEMOP_LOGICAL_READ:
2739 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
David Hildenbrand92c96322015-11-16 15:42:11 +01002740 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
2741 mop->size, GACC_FETCH);
Thomas Huth41408c282015-02-06 15:01:21 +01002742 break;
2743 }
2744 r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2745 if (r == 0) {
2746 if (copy_to_user(uaddr, tmpbuf, mop->size))
2747 r = -EFAULT;
2748 }
2749 break;
2750 case KVM_S390_MEMOP_LOGICAL_WRITE:
2751 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
David Hildenbrand92c96322015-11-16 15:42:11 +01002752 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
2753 mop->size, GACC_STORE);
Thomas Huth41408c282015-02-06 15:01:21 +01002754 break;
2755 }
2756 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
2757 r = -EFAULT;
2758 break;
2759 }
2760 r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
2761 break;
2762 default:
2763 r = -EINVAL;
2764 }
2765
2766 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2767
2768 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
2769 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
2770
2771 vfree(tmpbuf);
2772 return r;
2773}
2774
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002775long kvm_arch_vcpu_ioctl(struct file *filp,
2776 unsigned int ioctl, unsigned long arg)
2777{
2778 struct kvm_vcpu *vcpu = filp->private_data;
2779 void __user *argp = (void __user *)arg;
Thomas Huth800c1062013-09-12 10:33:45 +02002780 int idx;
Avi Kivitybc923cc2010-05-13 12:21:46 +03002781 long r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002782
Avi Kivity93736622010-05-13 12:35:17 +03002783 switch (ioctl) {
Jens Freimann47b43c52014-11-11 20:57:06 +01002784 case KVM_S390_IRQ: {
2785 struct kvm_s390_irq s390irq;
2786
2787 r = -EFAULT;
2788 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
2789 break;
2790 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
2791 break;
2792 }
Avi Kivity93736622010-05-13 12:35:17 +03002793 case KVM_S390_INTERRUPT: {
Carsten Otteba5c1e92008-03-25 18:47:26 +01002794 struct kvm_s390_interrupt s390int;
Jens Freimann383d0b02014-07-29 15:11:49 +02002795 struct kvm_s390_irq s390irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002796
Avi Kivity93736622010-05-13 12:35:17 +03002797 r = -EFAULT;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002798 if (copy_from_user(&s390int, argp, sizeof(s390int)))
Avi Kivity93736622010-05-13 12:35:17 +03002799 break;
Jens Freimann383d0b02014-07-29 15:11:49 +02002800 if (s390int_to_s390irq(&s390int, &s390irq))
2801 return -EINVAL;
2802 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
Avi Kivity93736622010-05-13 12:35:17 +03002803 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01002804 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002805 case KVM_S390_STORE_STATUS:
Thomas Huth800c1062013-09-12 10:33:45 +02002806 idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002807 r = kvm_s390_vcpu_store_status(vcpu, arg);
Thomas Huth800c1062013-09-12 10:33:45 +02002808 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivitybc923cc2010-05-13 12:21:46 +03002809 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002810 case KVM_S390_SET_INITIAL_PSW: {
2811 psw_t psw;
2812
Avi Kivitybc923cc2010-05-13 12:21:46 +03002813 r = -EFAULT;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002814 if (copy_from_user(&psw, argp, sizeof(psw)))
Avi Kivitybc923cc2010-05-13 12:21:46 +03002815 break;
2816 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
2817 break;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002818 }
2819 case KVM_S390_INITIAL_RESET:
Avi Kivitybc923cc2010-05-13 12:21:46 +03002820 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
2821 break;
Carsten Otte14eebd92012-05-15 14:15:26 +02002822 case KVM_SET_ONE_REG:
2823 case KVM_GET_ONE_REG: {
2824 struct kvm_one_reg reg;
2825 r = -EFAULT;
2826 if (copy_from_user(&reg, argp, sizeof(reg)))
2827 break;
2828 if (ioctl == KVM_SET_ONE_REG)
2829 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
2830 else
2831 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
2832 break;
2833 }
Carsten Otte27e03932012-01-04 10:25:21 +01002834#ifdef CONFIG_KVM_S390_UCONTROL
2835 case KVM_S390_UCAS_MAP: {
2836 struct kvm_s390_ucas_mapping ucasmap;
2837
2838 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2839 r = -EFAULT;
2840 break;
2841 }
2842
2843 if (!kvm_is_ucontrol(vcpu->kvm)) {
2844 r = -EINVAL;
2845 break;
2846 }
2847
2848 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
2849 ucasmap.vcpu_addr, ucasmap.length);
2850 break;
2851 }
2852 case KVM_S390_UCAS_UNMAP: {
2853 struct kvm_s390_ucas_mapping ucasmap;
2854
2855 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
2856 r = -EFAULT;
2857 break;
2858 }
2859
2860 if (!kvm_is_ucontrol(vcpu->kvm)) {
2861 r = -EINVAL;
2862 break;
2863 }
2864
2865 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
2866 ucasmap.length);
2867 break;
2868 }
2869#endif
Carsten Otteccc79102012-01-04 10:25:26 +01002870 case KVM_S390_VCPU_FAULT: {
Martin Schwidefsky527e30b2014-04-30 16:04:25 +02002871 r = gmap_fault(vcpu->arch.gmap, arg, 0);
Carsten Otteccc79102012-01-04 10:25:26 +01002872 break;
2873 }
Cornelia Huckd6712df2012-12-20 15:32:11 +01002874 case KVM_ENABLE_CAP:
2875 {
2876 struct kvm_enable_cap cap;
2877 r = -EFAULT;
2878 if (copy_from_user(&cap, argp, sizeof(cap)))
2879 break;
2880 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2881 break;
2882 }
Thomas Huth41408c282015-02-06 15:01:21 +01002883 case KVM_S390_MEM_OP: {
2884 struct kvm_s390_mem_op mem_op;
2885
2886 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
2887 r = kvm_s390_guest_mem_op(vcpu, &mem_op);
2888 else
2889 r = -EFAULT;
2890 break;
2891 }
Jens Freimann816c7662014-11-24 17:13:46 +01002892 case KVM_S390_SET_IRQ_STATE: {
2893 struct kvm_s390_irq_state irq_state;
2894
2895 r = -EFAULT;
2896 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2897 break;
2898 if (irq_state.len > VCPU_IRQS_MAX_BUF ||
2899 irq_state.len == 0 ||
2900 irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
2901 r = -EINVAL;
2902 break;
2903 }
2904 r = kvm_s390_set_irq_state(vcpu,
2905 (void __user *) irq_state.buf,
2906 irq_state.len);
2907 break;
2908 }
2909 case KVM_S390_GET_IRQ_STATE: {
2910 struct kvm_s390_irq_state irq_state;
2911
2912 r = -EFAULT;
2913 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2914 break;
2915 if (irq_state.len == 0) {
2916 r = -EINVAL;
2917 break;
2918 }
2919 r = kvm_s390_get_irq_state(vcpu,
2920 (__u8 __user *) irq_state.buf,
2921 irq_state.len);
2922 break;
2923 }
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002924 default:
Carsten Otte3e6afcf2012-01-04 10:25:30 +01002925 r = -ENOTTY;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002926 }
Avi Kivitybc923cc2010-05-13 12:21:46 +03002927 return r;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002928}
2929
Carsten Otte5b1c1492012-01-04 10:25:23 +01002930int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2931{
2932#ifdef CONFIG_KVM_S390_UCONTROL
2933 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
2934 && (kvm_is_ucontrol(vcpu->kvm))) {
2935 vmf->page = virt_to_page(vcpu->arch.sie_block);
2936 get_page(vmf->page);
2937 return 0;
2938 }
2939#endif
2940 return VM_FAULT_SIGBUS;
2941}
2942
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05302943int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
2944 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09002945{
2946 return 0;
2947}
2948
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002949/* Section: memory related */
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002950int kvm_arch_prepare_memory_region(struct kvm *kvm,
2951 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002952 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09002953 enum kvm_mr_change change)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002954{
Nick Wangdd2887e2013-03-25 17:22:57 +01002955 /* A few sanity checks. We can have memory slots which have to be
2956 located/ended at a segment boundary (1MB). The memory in userland is
2957 ok to be fragmented into various different vmas. It is okay to mmap()
2958 and munmap() stuff in this slot after doing this call at any time */
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002959
Carsten Otte598841c2011-07-24 10:48:21 +02002960 if (mem->userspace_addr & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002961 return -EINVAL;
2962
Carsten Otte598841c2011-07-24 10:48:21 +02002963 if (mem->memory_size & 0xffffful)
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002964 return -EINVAL;
2965
Dominik Dingela3a92c32014-12-01 17:24:42 +01002966 if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
2967 return -EINVAL;
2968
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002969 return 0;
2970}
2971
2972void kvm_arch_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002973 const struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002974 const struct kvm_memory_slot *old,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02002975 const struct kvm_memory_slot *new,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002976 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002977{
Carsten Ottef7850c92011-07-24 10:48:23 +02002978 int rc;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02002979
Christian Borntraeger2cef4de2013-03-25 17:22:48 +01002980 /* If the basics of the memslot do not change, we do not want
2981 * to update the gmap. Every update causes several unnecessary
2982 * segment translation exceptions. This is usually handled just
2983 * fine by the normal fault handler + gmap, but it will also
2984 * cause faults on the prefix page of running guest CPUs.
2985 */
2986 if (old->userspace_addr == mem->userspace_addr &&
2987 old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
2988 old->npages * PAGE_SIZE == mem->memory_size)
2989 return;
Carsten Otte598841c2011-07-24 10:48:21 +02002990
2991 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
2992 mem->guest_phys_addr, mem->memory_size);
2993 if (rc)
David Hildenbrandea2cdd22015-05-20 13:24:02 +02002994 pr_warn("failed to commit memory region\n");
Carsten Otte598841c2011-07-24 10:48:21 +02002995 return;
Heiko Carstensb0c632d2008-03-25 18:47:20 +01002996}
2997
Alexander Yarygin60a37702016-04-01 15:38:57 +03002998static inline unsigned long nonhyp_mask(int i)
2999{
3000 unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
3001
3002 return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
3003}
3004
Christian Borntraeger3491caf2016-05-13 12:16:35 +02003005void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
3006{
3007 vcpu->valid_wakeup = false;
3008}
3009
Heiko Carstensb0c632d2008-03-25 18:47:20 +01003010static int __init kvm_s390_init(void)
3011{
Alexander Yarygin60a37702016-04-01 15:38:57 +03003012 int i;
3013
David Hildenbrand07197fd2015-01-30 16:01:38 +01003014 if (!sclp.has_sief2) {
3015 pr_info("SIE not available\n");
3016 return -ENODEV;
3017 }
3018
Alexander Yarygin60a37702016-04-01 15:38:57 +03003019 for (i = 0; i < 16; i++)
3020 kvm_s390_fac_list_mask[i] |=
3021 S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
3022
Michael Mueller9d8d5782015-02-02 15:42:51 +01003023 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
Heiko Carstensb0c632d2008-03-25 18:47:20 +01003024}
3025
3026static void __exit kvm_s390_exit(void)
3027{
Heiko Carstensb0c632d2008-03-25 18:47:20 +01003028 kvm_exit();
3029}
3030
3031module_init(kvm_s390_init);
3032module_exit(kvm_s390_exit);
Cornelia Huck566af942013-05-27 18:42:33 +02003033
3034/*
3035 * Enable autoloading of the kvm module.
3036 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
3037 * since x86 takes a different approach.
3038 */
3039#include <linux/miscdevice.h>
3040MODULE_ALIAS_MISCDEV(KVM_MINOR);
3041MODULE_ALIAS("devname:kvm");