xref: /openbmc/linux/arch/s390/kvm/kvm-s390.c (revision bc784ccee5eb9ae1e737927eb9d8a0fbf7601abc)
1b0c632dbSHeiko Carstens /*
2a53c8fabSHeiko Carstens  * hosting zSeries kernel virtual machines
3b0c632dbSHeiko Carstens  *
4628eb9b8SChristian Ehrhardt  * Copyright IBM Corp. 2008, 2009
5b0c632dbSHeiko Carstens  *
6b0c632dbSHeiko Carstens  * This program is free software; you can redistribute it and/or modify
7b0c632dbSHeiko Carstens  * it under the terms of the GNU General Public License (version 2 only)
8b0c632dbSHeiko Carstens  * as published by the Free Software Foundation.
9b0c632dbSHeiko Carstens  *
10b0c632dbSHeiko Carstens  *    Author(s): Carsten Otte <cotte@de.ibm.com>
11b0c632dbSHeiko Carstens  *               Christian Borntraeger <borntraeger@de.ibm.com>
12b0c632dbSHeiko Carstens  *               Heiko Carstens <heiko.carstens@de.ibm.com>
13628eb9b8SChristian Ehrhardt  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
1415f36ebdSJason J. Herne  *               Jason J. Herne <jjherne@us.ibm.com>
15b0c632dbSHeiko Carstens  */
16b0c632dbSHeiko Carstens 
17b0c632dbSHeiko Carstens #include <linux/compiler.h>
18b0c632dbSHeiko Carstens #include <linux/err.h>
19b0c632dbSHeiko Carstens #include <linux/fs.h>
20ca872302SChristian Borntraeger #include <linux/hrtimer.h>
21b0c632dbSHeiko Carstens #include <linux/init.h>
22b0c632dbSHeiko Carstens #include <linux/kvm.h>
23b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
24b0c632dbSHeiko Carstens #include <linux/module.h>
25a374e892STony Krowiak #include <linux/random.h>
26b0c632dbSHeiko Carstens #include <linux/slab.h>
27ba5c1e9bSCarsten Otte #include <linux/timer.h>
2841408c28SThomas Huth #include <linux/vmalloc.h>
29cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
30b0c632dbSHeiko Carstens #include <asm/lowcore.h>
31fdf03650SFan Zhang #include <asm/etr.h>
32b0c632dbSHeiko Carstens #include <asm/pgtable.h>
33f5daba1dSHeiko Carstens #include <asm/nmi.h>
34a0616cdeSDavid Howells #include <asm/switch_to.h>
356d3da241SJens Freimann #include <asm/isc.h>
361526bf9cSChristian Borntraeger #include <asm/sclp.h>
378f2abe6aSChristian Borntraeger #include "kvm-s390.h"
38b0c632dbSHeiko Carstens #include "gaccess.h"
39b0c632dbSHeiko Carstens 
40ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390"
41ea2cdd27SDavid Hildenbrand #undef pr_fmt
42ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
43ea2cdd27SDavid Hildenbrand 
445786fffaSCornelia Huck #define CREATE_TRACE_POINTS
455786fffaSCornelia Huck #include "trace.h"
46ade38c31SCornelia Huck #include "trace-s390.h"
475786fffaSCornelia Huck 
4841408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
49816c7667SJens Freimann #define LOCAL_IRQS 32
50816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
51816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
5241408c28SThomas Huth 
53b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
54b0c632dbSHeiko Carstens 
55b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
56b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
570eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
588f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
598f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
608f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
618f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
62ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
63ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
64ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
65f7819512SPaolo Bonzini 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
6662bea5bfSPaolo Bonzini 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
67ce2e4f0bSDavid Hildenbrand 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
68f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
69ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
70aba07508SDavid Hildenbrand 	{ "instruction_stctl", VCPU_STAT(instruction_stctl) },
71aba07508SDavid Hildenbrand 	{ "instruction_stctg", VCPU_STAT(instruction_stctg) },
72ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
737697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
74ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
75ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
76ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
77ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
78ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
79ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
80ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
8169d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
82453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
83453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
84453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
85453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
86453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
878a242234SHeiko Carstens 	{ "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
88453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
89453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
90b31288faSKonstantin Weitz 	{ "instruction_essa", VCPU_STAT(instruction_essa) },
91453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
92453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
93bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
945288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
95bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
967697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
975288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
9842cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
9942cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
1005288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
10142cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
10242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
103cd7b4b61SEric Farman 	{ "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
1045288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
1055288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
1065288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
10742cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
10842cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
10942cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
110388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
111e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
11241628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
113175a5c9eSChristian Borntraeger 	{ "diagnose_258", VCPU_STAT(diagnose_258) },
114175a5c9eSChristian Borntraeger 	{ "diagnose_308", VCPU_STAT(diagnose_308) },
115175a5c9eSChristian Borntraeger 	{ "diagnose_500", VCPU_STAT(diagnose_500) },
116b0c632dbSHeiko Carstens 	{ NULL }
117b0c632dbSHeiko Carstens };
118b0c632dbSHeiko Carstens 
1199d8d5786SMichael Mueller /* upper facilities limit for kvm */
1209d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask[] = {
121a3ed8daeSChristian Borntraeger 	0xffe6fffbfcfdfc40UL,
12253df84f8SGuenther Hutzl 	0x005e800000000000UL,
1239d8d5786SMichael Mueller };
124b0c632dbSHeiko Carstens 
1259d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void)
12678c4b59fSMichael Mueller {
1279d8d5786SMichael Mueller 	BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
1289d8d5786SMichael Mueller 	return ARRAY_SIZE(kvm_s390_fac_list_mask);
12978c4b59fSMichael Mueller }
13078c4b59fSMichael Mueller 
1319d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
13278f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
1339d8d5786SMichael Mueller 
134b0c632dbSHeiko Carstens /* Section: not file related */
13513a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
136b0c632dbSHeiko Carstens {
137b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
13810474ae8SAlexander Graf 	return 0;
139b0c632dbSHeiko Carstens }
140b0c632dbSHeiko Carstens 
1412c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address);
1422c70fe44SChristian Borntraeger 
143fdf03650SFan Zhang /*
144fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
145fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
146fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
147fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
148fdf03650SFan Zhang  */
149fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
150fdf03650SFan Zhang 			  void *v)
151fdf03650SFan Zhang {
152fdf03650SFan Zhang 	struct kvm *kvm;
153fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
154fdf03650SFan Zhang 	int i;
155fdf03650SFan Zhang 	unsigned long long *delta = v;
156fdf03650SFan Zhang 
157fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
158fdf03650SFan Zhang 		kvm->arch.epoch -= *delta;
159fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
160fdf03650SFan Zhang 			vcpu->arch.sie_block->epoch -= *delta;
161fdf03650SFan Zhang 		}
162fdf03650SFan Zhang 	}
163fdf03650SFan Zhang 	return NOTIFY_OK;
164fdf03650SFan Zhang }
165fdf03650SFan Zhang 
166fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
167fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
168fdf03650SFan Zhang };
169fdf03650SFan Zhang 
170b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
171b0c632dbSHeiko Carstens {
1722c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
1732c70fe44SChristian Borntraeger 	gmap_register_ipte_notifier(&gmap_notifier);
174fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
175fdf03650SFan Zhang 				       &kvm_clock_notifier);
176b0c632dbSHeiko Carstens 	return 0;
177b0c632dbSHeiko Carstens }
178b0c632dbSHeiko Carstens 
179b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
180b0c632dbSHeiko Carstens {
1812c70fe44SChristian Borntraeger 	gmap_unregister_ipte_notifier(&gmap_notifier);
182fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
183fdf03650SFan Zhang 					 &kvm_clock_notifier);
184b0c632dbSHeiko Carstens }
185b0c632dbSHeiko Carstens 
186b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
187b0c632dbSHeiko Carstens {
18878f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
18978f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
19078f26131SChristian Borntraeger 		return -ENOMEM;
19178f26131SChristian Borntraeger 
19278f26131SChristian Borntraeger 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
19378f26131SChristian Borntraeger 		debug_unregister(kvm_s390_dbf);
19478f26131SChristian Borntraeger 		return -ENOMEM;
19578f26131SChristian Borntraeger 	}
19678f26131SChristian Borntraeger 
19784877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
19884877d93SCornelia Huck 	return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
199b0c632dbSHeiko Carstens }
200b0c632dbSHeiko Carstens 
20178f26131SChristian Borntraeger void kvm_arch_exit(void)
20278f26131SChristian Borntraeger {
20378f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
20478f26131SChristian Borntraeger }
20578f26131SChristian Borntraeger 
206b0c632dbSHeiko Carstens /* Section: device related */
207b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
208b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
209b0c632dbSHeiko Carstens {
210b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
211b0c632dbSHeiko Carstens 		return s390_enable_sie();
212b0c632dbSHeiko Carstens 	return -EINVAL;
213b0c632dbSHeiko Carstens }
214b0c632dbSHeiko Carstens 
215784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
216b0c632dbSHeiko Carstens {
217d7b0b5ebSCarsten Otte 	int r;
218d7b0b5ebSCarsten Otte 
2192bd0ac4eSCarsten Otte 	switch (ext) {
220d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
221b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
22252e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
2231efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
2241efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
2251efd0f59SCarsten Otte #endif
2263c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
22760b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
22814eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
229d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
230fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
23110ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
232c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
233d938dc55SCornelia Huck 	case KVM_CAP_ENABLE_CAP_VM:
23478599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
235f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
2366352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
23747b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
2382444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
239e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
24030ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
241816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
242d7b0b5ebSCarsten Otte 		r = 1;
243d7b0b5ebSCarsten Otte 		break;
24441408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
24541408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
24641408c28SThomas Huth 		break;
247e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
248e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
249e726b1bdSChristian Borntraeger 		r = KVM_MAX_VCPUS;
250e726b1bdSChristian Borntraeger 		break;
251e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
252e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
253e1e2e605SNick Wang 		break;
2541526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
255abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
2561526bf9cSChristian Borntraeger 		break;
25768c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
25868c55750SEric Farman 		r = MACHINE_HAS_VX;
25968c55750SEric Farman 		break;
2602bd0ac4eSCarsten Otte 	default:
261d7b0b5ebSCarsten Otte 		r = 0;
262b0c632dbSHeiko Carstens 	}
263d7b0b5ebSCarsten Otte 	return r;
2642bd0ac4eSCarsten Otte }
265b0c632dbSHeiko Carstens 
26615f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm,
26715f36ebdSJason J. Herne 					struct kvm_memory_slot *memslot)
26815f36ebdSJason J. Herne {
26915f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
27015f36ebdSJason J. Herne 	unsigned long address;
27115f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
27215f36ebdSJason J. Herne 
27315f36ebdSJason J. Herne 	down_read(&gmap->mm->mmap_sem);
27415f36ebdSJason J. Herne 	/* Loop over all guest pages */
27515f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
27615f36ebdSJason J. Herne 	for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
27715f36ebdSJason J. Herne 		address = gfn_to_hva_memslot(memslot, cur_gfn);
27815f36ebdSJason J. Herne 
27915f36ebdSJason J. Herne 		if (gmap_test_and_clear_dirty(address, gmap))
28015f36ebdSJason J. Herne 			mark_page_dirty(kvm, cur_gfn);
28115f36ebdSJason J. Herne 	}
28215f36ebdSJason J. Herne 	up_read(&gmap->mm->mmap_sem);
28315f36ebdSJason J. Herne }
28415f36ebdSJason J. Herne 
285b0c632dbSHeiko Carstens /* Section: vm related */
286a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
287a6e2f683SEugene (jno) Dvurechenski 
288b0c632dbSHeiko Carstens /*
289b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
290b0c632dbSHeiko Carstens  */
291b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
292b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
293b0c632dbSHeiko Carstens {
29415f36ebdSJason J. Herne 	int r;
29515f36ebdSJason J. Herne 	unsigned long n;
2969f6b8029SPaolo Bonzini 	struct kvm_memslots *slots;
29715f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
29815f36ebdSJason J. Herne 	int is_dirty = 0;
29915f36ebdSJason J. Herne 
30015f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
30115f36ebdSJason J. Herne 
30215f36ebdSJason J. Herne 	r = -EINVAL;
30315f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
30415f36ebdSJason J. Herne 		goto out;
30515f36ebdSJason J. Herne 
3069f6b8029SPaolo Bonzini 	slots = kvm_memslots(kvm);
3079f6b8029SPaolo Bonzini 	memslot = id_to_memslot(slots, log->slot);
30815f36ebdSJason J. Herne 	r = -ENOENT;
30915f36ebdSJason J. Herne 	if (!memslot->dirty_bitmap)
31015f36ebdSJason J. Herne 		goto out;
31115f36ebdSJason J. Herne 
31215f36ebdSJason J. Herne 	kvm_s390_sync_dirty_log(kvm, memslot);
31315f36ebdSJason J. Herne 	r = kvm_get_dirty_log(kvm, log, &is_dirty);
31415f36ebdSJason J. Herne 	if (r)
31515f36ebdSJason J. Herne 		goto out;
31615f36ebdSJason J. Herne 
31715f36ebdSJason J. Herne 	/* Clear the dirty log */
31815f36ebdSJason J. Herne 	if (is_dirty) {
31915f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
32015f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
32115f36ebdSJason J. Herne 	}
32215f36ebdSJason J. Herne 	r = 0;
32315f36ebdSJason J. Herne out:
32415f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
32515f36ebdSJason J. Herne 	return r;
326b0c632dbSHeiko Carstens }
327b0c632dbSHeiko Carstens 
328d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
329d938dc55SCornelia Huck {
330d938dc55SCornelia Huck 	int r;
331d938dc55SCornelia Huck 
332d938dc55SCornelia Huck 	if (cap->flags)
333d938dc55SCornelia Huck 		return -EINVAL;
334d938dc55SCornelia Huck 
335d938dc55SCornelia Huck 	switch (cap->cap) {
33684223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
337c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
33884223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
33984223598SCornelia Huck 		r = 0;
34084223598SCornelia Huck 		break;
3412444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
342c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
3432444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
3442444b352SDavid Hildenbrand 		r = 0;
3452444b352SDavid Hildenbrand 		break;
34668c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
3475967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
3485967c17bSDavid Hildenbrand 		if (atomic_read(&kvm->online_vcpus)) {
3495967c17bSDavid Hildenbrand 			r = -EBUSY;
3505967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
35118280d8bSMichael Mueller 			set_kvm_facility(kvm->arch.model.fac->mask, 129);
35218280d8bSMichael Mueller 			set_kvm_facility(kvm->arch.model.fac->list, 129);
35318280d8bSMichael Mueller 			r = 0;
35418280d8bSMichael Mueller 		} else
35518280d8bSMichael Mueller 			r = -EINVAL;
3565967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
357c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
358c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
35968c55750SEric Farman 		break;
360e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
361c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
362e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
363e44fc8c9SEkaterina Tumanova 		r = 0;
364e44fc8c9SEkaterina Tumanova 		break;
365d938dc55SCornelia Huck 	default:
366d938dc55SCornelia Huck 		r = -EINVAL;
367d938dc55SCornelia Huck 		break;
368d938dc55SCornelia Huck 	}
369d938dc55SCornelia Huck 	return r;
370d938dc55SCornelia Huck }
371d938dc55SCornelia Huck 
3728c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
3738c0a7ce6SDominik Dingel {
3748c0a7ce6SDominik Dingel 	int ret;
3758c0a7ce6SDominik Dingel 
3768c0a7ce6SDominik Dingel 	switch (attr->attr) {
3778c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
3788c0a7ce6SDominik Dingel 		ret = 0;
379c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
380c92ea7b9SChristian Borntraeger 			 kvm->arch.gmap->asce_end);
3818c0a7ce6SDominik Dingel 		if (put_user(kvm->arch.gmap->asce_end, (u64 __user *)attr->addr))
3828c0a7ce6SDominik Dingel 			ret = -EFAULT;
3838c0a7ce6SDominik Dingel 		break;
3848c0a7ce6SDominik Dingel 	default:
3858c0a7ce6SDominik Dingel 		ret = -ENXIO;
3868c0a7ce6SDominik Dingel 		break;
3878c0a7ce6SDominik Dingel 	}
3888c0a7ce6SDominik Dingel 	return ret;
3898c0a7ce6SDominik Dingel }
3908c0a7ce6SDominik Dingel 
3918c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
3924f718eabSDominik Dingel {
3934f718eabSDominik Dingel 	int ret;
3944f718eabSDominik Dingel 	unsigned int idx;
3954f718eabSDominik Dingel 	switch (attr->attr) {
3964f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
397e6db1d61SDominik Dingel 		/* enable CMMA only for z10 and later (EDAT_1) */
398e6db1d61SDominik Dingel 		ret = -EINVAL;
399e6db1d61SDominik Dingel 		if (!MACHINE_IS_LPAR || !MACHINE_HAS_EDAT1)
400e6db1d61SDominik Dingel 			break;
401e6db1d61SDominik Dingel 
4024f718eabSDominik Dingel 		ret = -EBUSY;
403c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
4044f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
4054f718eabSDominik Dingel 		if (atomic_read(&kvm->online_vcpus) == 0) {
4064f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
4074f718eabSDominik Dingel 			ret = 0;
4084f718eabSDominik Dingel 		}
4094f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
4104f718eabSDominik Dingel 		break;
4114f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
412c3489155SDominik Dingel 		ret = -EINVAL;
413c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
414c3489155SDominik Dingel 			break;
415c3489155SDominik Dingel 
416c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
4174f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
4184f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
419a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
4204f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
4214f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
4224f718eabSDominik Dingel 		ret = 0;
4234f718eabSDominik Dingel 		break;
4248c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
4258c0a7ce6SDominik Dingel 		unsigned long new_limit;
4268c0a7ce6SDominik Dingel 
4278c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
4288c0a7ce6SDominik Dingel 			return -EINVAL;
4298c0a7ce6SDominik Dingel 
4308c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
4318c0a7ce6SDominik Dingel 			return -EFAULT;
4328c0a7ce6SDominik Dingel 
4338c0a7ce6SDominik Dingel 		if (new_limit > kvm->arch.gmap->asce_end)
4348c0a7ce6SDominik Dingel 			return -E2BIG;
4358c0a7ce6SDominik Dingel 
4368c0a7ce6SDominik Dingel 		ret = -EBUSY;
4378c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
4388c0a7ce6SDominik Dingel 		if (atomic_read(&kvm->online_vcpus) == 0) {
4398c0a7ce6SDominik Dingel 			/* gmap_alloc will round the limit up */
4408c0a7ce6SDominik Dingel 			struct gmap *new = gmap_alloc(current->mm, new_limit);
4418c0a7ce6SDominik Dingel 
4428c0a7ce6SDominik Dingel 			if (!new) {
4438c0a7ce6SDominik Dingel 				ret = -ENOMEM;
4448c0a7ce6SDominik Dingel 			} else {
4458c0a7ce6SDominik Dingel 				gmap_free(kvm->arch.gmap);
4468c0a7ce6SDominik Dingel 				new->private = kvm;
4478c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
4488c0a7ce6SDominik Dingel 				ret = 0;
4498c0a7ce6SDominik Dingel 			}
4508c0a7ce6SDominik Dingel 		}
4518c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
452c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: max guest memory: %lu bytes", new_limit);
4538c0a7ce6SDominik Dingel 		break;
4548c0a7ce6SDominik Dingel 	}
4554f718eabSDominik Dingel 	default:
4564f718eabSDominik Dingel 		ret = -ENXIO;
4574f718eabSDominik Dingel 		break;
4584f718eabSDominik Dingel 	}
4594f718eabSDominik Dingel 	return ret;
4604f718eabSDominik Dingel }
4614f718eabSDominik Dingel 
462a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
463a374e892STony Krowiak 
464a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
465a374e892STony Krowiak {
466a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
467a374e892STony Krowiak 	int i;
468a374e892STony Krowiak 
4699d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
470a374e892STony Krowiak 		return -EINVAL;
471a374e892STony Krowiak 
472a374e892STony Krowiak 	mutex_lock(&kvm->lock);
473a374e892STony Krowiak 	switch (attr->attr) {
474a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
475a374e892STony Krowiak 		get_random_bytes(
476a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
477a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
478a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
479c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
480a374e892STony Krowiak 		break;
481a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
482a374e892STony Krowiak 		get_random_bytes(
483a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
484a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
485a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
486c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
487a374e892STony Krowiak 		break;
488a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
489a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
490a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
491a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
492c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
493a374e892STony Krowiak 		break;
494a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
495a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
496a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
497a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
498c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
499a374e892STony Krowiak 		break;
500a374e892STony Krowiak 	default:
501a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
502a374e892STony Krowiak 		return -ENXIO;
503a374e892STony Krowiak 	}
504a374e892STony Krowiak 
505a374e892STony Krowiak 	kvm_for_each_vcpu(i, vcpu, kvm) {
506a374e892STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
507a374e892STony Krowiak 		exit_sie(vcpu);
508a374e892STony Krowiak 	}
509a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
510a374e892STony Krowiak 	return 0;
511a374e892STony Krowiak }
512a374e892STony Krowiak 
51372f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
51472f25020SJason J. Herne {
51572f25020SJason J. Herne 	u8 gtod_high;
51672f25020SJason J. Herne 
51772f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
51872f25020SJason J. Herne 					   sizeof(gtod_high)))
51972f25020SJason J. Herne 		return -EFAULT;
52072f25020SJason J. Herne 
52172f25020SJason J. Herne 	if (gtod_high != 0)
52272f25020SJason J. Herne 		return -EINVAL;
52358c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
52472f25020SJason J. Herne 
52572f25020SJason J. Herne 	return 0;
52672f25020SJason J. Herne }
52772f25020SJason J. Herne 
52872f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
52972f25020SJason J. Herne {
5305a3d883aSDavid Hildenbrand 	u64 gtod;
53172f25020SJason J. Herne 
53272f25020SJason J. Herne 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
53372f25020SJason J. Herne 		return -EFAULT;
53472f25020SJason J. Herne 
53525ed1675SDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, gtod);
53658c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
53772f25020SJason J. Herne 	return 0;
53872f25020SJason J. Herne }
53972f25020SJason J. Herne 
54072f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
54172f25020SJason J. Herne {
54272f25020SJason J. Herne 	int ret;
54372f25020SJason J. Herne 
54472f25020SJason J. Herne 	if (attr->flags)
54572f25020SJason J. Herne 		return -EINVAL;
54672f25020SJason J. Herne 
54772f25020SJason J. Herne 	switch (attr->attr) {
54872f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
54972f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
55072f25020SJason J. Herne 		break;
55172f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
55272f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
55372f25020SJason J. Herne 		break;
55472f25020SJason J. Herne 	default:
55572f25020SJason J. Herne 		ret = -ENXIO;
55672f25020SJason J. Herne 		break;
55772f25020SJason J. Herne 	}
55872f25020SJason J. Herne 	return ret;
55972f25020SJason J. Herne }
56072f25020SJason J. Herne 
56172f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
56272f25020SJason J. Herne {
56372f25020SJason J. Herne 	u8 gtod_high = 0;
56472f25020SJason J. Herne 
56572f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
56672f25020SJason J. Herne 					 sizeof(gtod_high)))
56772f25020SJason J. Herne 		return -EFAULT;
56858c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
56972f25020SJason J. Herne 
57072f25020SJason J. Herne 	return 0;
57172f25020SJason J. Herne }
57272f25020SJason J. Herne 
57372f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
57472f25020SJason J. Herne {
5755a3d883aSDavid Hildenbrand 	u64 gtod;
57672f25020SJason J. Herne 
57760417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
57872f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
57972f25020SJason J. Herne 		return -EFAULT;
58058c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
58172f25020SJason J. Herne 
58272f25020SJason J. Herne 	return 0;
58372f25020SJason J. Herne }
58472f25020SJason J. Herne 
58572f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
58672f25020SJason J. Herne {
58772f25020SJason J. Herne 	int ret;
58872f25020SJason J. Herne 
58972f25020SJason J. Herne 	if (attr->flags)
59072f25020SJason J. Herne 		return -EINVAL;
59172f25020SJason J. Herne 
59272f25020SJason J. Herne 	switch (attr->attr) {
59372f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
59472f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
59572f25020SJason J. Herne 		break;
59672f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
59772f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
59872f25020SJason J. Herne 		break;
59972f25020SJason J. Herne 	default:
60072f25020SJason J. Herne 		ret = -ENXIO;
60172f25020SJason J. Herne 		break;
60272f25020SJason J. Herne 	}
60372f25020SJason J. Herne 	return ret;
60472f25020SJason J. Herne }
60572f25020SJason J. Herne 
606658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
607658b6edaSMichael Mueller {
608658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
609658b6edaSMichael Mueller 	int ret = 0;
610658b6edaSMichael Mueller 
611658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
612658b6edaSMichael Mueller 	if (atomic_read(&kvm->online_vcpus)) {
613658b6edaSMichael Mueller 		ret = -EBUSY;
614658b6edaSMichael Mueller 		goto out;
615658b6edaSMichael Mueller 	}
616658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
617658b6edaSMichael Mueller 	if (!proc) {
618658b6edaSMichael Mueller 		ret = -ENOMEM;
619658b6edaSMichael Mueller 		goto out;
620658b6edaSMichael Mueller 	}
621658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
622658b6edaSMichael Mueller 			    sizeof(*proc))) {
623658b6edaSMichael Mueller 		memcpy(&kvm->arch.model.cpu_id, &proc->cpuid,
624658b6edaSMichael Mueller 		       sizeof(struct cpuid));
625658b6edaSMichael Mueller 		kvm->arch.model.ibc = proc->ibc;
626981467c9SMichael Mueller 		memcpy(kvm->arch.model.fac->list, proc->fac_list,
627658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
628658b6edaSMichael Mueller 	} else
629658b6edaSMichael Mueller 		ret = -EFAULT;
630658b6edaSMichael Mueller 	kfree(proc);
631658b6edaSMichael Mueller out:
632658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
633658b6edaSMichael Mueller 	return ret;
634658b6edaSMichael Mueller }
635658b6edaSMichael Mueller 
636658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
637658b6edaSMichael Mueller {
638658b6edaSMichael Mueller 	int ret = -ENXIO;
639658b6edaSMichael Mueller 
640658b6edaSMichael Mueller 	switch (attr->attr) {
641658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
642658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
643658b6edaSMichael Mueller 		break;
644658b6edaSMichael Mueller 	}
645658b6edaSMichael Mueller 	return ret;
646658b6edaSMichael Mueller }
647658b6edaSMichael Mueller 
648658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
649658b6edaSMichael Mueller {
650658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
651658b6edaSMichael Mueller 	int ret = 0;
652658b6edaSMichael Mueller 
653658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
654658b6edaSMichael Mueller 	if (!proc) {
655658b6edaSMichael Mueller 		ret = -ENOMEM;
656658b6edaSMichael Mueller 		goto out;
657658b6edaSMichael Mueller 	}
658658b6edaSMichael Mueller 	memcpy(&proc->cpuid, &kvm->arch.model.cpu_id, sizeof(struct cpuid));
659658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
660981467c9SMichael Mueller 	memcpy(&proc->fac_list, kvm->arch.model.fac->list, S390_ARCH_FAC_LIST_SIZE_BYTE);
661658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
662658b6edaSMichael Mueller 		ret = -EFAULT;
663658b6edaSMichael Mueller 	kfree(proc);
664658b6edaSMichael Mueller out:
665658b6edaSMichael Mueller 	return ret;
666658b6edaSMichael Mueller }
667658b6edaSMichael Mueller 
668658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
669658b6edaSMichael Mueller {
670658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
671658b6edaSMichael Mueller 	int ret = 0;
672658b6edaSMichael Mueller 
673658b6edaSMichael Mueller 	mach = kzalloc(sizeof(*mach), GFP_KERNEL);
674658b6edaSMichael Mueller 	if (!mach) {
675658b6edaSMichael Mueller 		ret = -ENOMEM;
676658b6edaSMichael Mueller 		goto out;
677658b6edaSMichael Mueller 	}
678658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
67937c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
680981467c9SMichael Mueller 	memcpy(&mach->fac_mask, kvm->arch.model.fac->mask,
681981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
682658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
68394422ee8SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
684658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
685658b6edaSMichael Mueller 		ret = -EFAULT;
686658b6edaSMichael Mueller 	kfree(mach);
687658b6edaSMichael Mueller out:
688658b6edaSMichael Mueller 	return ret;
689658b6edaSMichael Mueller }
690658b6edaSMichael Mueller 
691658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
692658b6edaSMichael Mueller {
693658b6edaSMichael Mueller 	int ret = -ENXIO;
694658b6edaSMichael Mueller 
695658b6edaSMichael Mueller 	switch (attr->attr) {
696658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
697658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
698658b6edaSMichael Mueller 		break;
699658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
700658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
701658b6edaSMichael Mueller 		break;
702658b6edaSMichael Mueller 	}
703658b6edaSMichael Mueller 	return ret;
704658b6edaSMichael Mueller }
705658b6edaSMichael Mueller 
706f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
707f2061656SDominik Dingel {
708f2061656SDominik Dingel 	int ret;
709f2061656SDominik Dingel 
710f2061656SDominik Dingel 	switch (attr->group) {
7114f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
7128c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
7134f718eabSDominik Dingel 		break;
71472f25020SJason J. Herne 	case KVM_S390_VM_TOD:
71572f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
71672f25020SJason J. Herne 		break;
717658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
718658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
719658b6edaSMichael Mueller 		break;
720a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
721a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
722a374e892STony Krowiak 		break;
723f2061656SDominik Dingel 	default:
724f2061656SDominik Dingel 		ret = -ENXIO;
725f2061656SDominik Dingel 		break;
726f2061656SDominik Dingel 	}
727f2061656SDominik Dingel 
728f2061656SDominik Dingel 	return ret;
729f2061656SDominik Dingel }
730f2061656SDominik Dingel 
731f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
732f2061656SDominik Dingel {
7338c0a7ce6SDominik Dingel 	int ret;
7348c0a7ce6SDominik Dingel 
7358c0a7ce6SDominik Dingel 	switch (attr->group) {
7368c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
7378c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
7388c0a7ce6SDominik Dingel 		break;
73972f25020SJason J. Herne 	case KVM_S390_VM_TOD:
74072f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
74172f25020SJason J. Herne 		break;
742658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
743658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
744658b6edaSMichael Mueller 		break;
7458c0a7ce6SDominik Dingel 	default:
7468c0a7ce6SDominik Dingel 		ret = -ENXIO;
7478c0a7ce6SDominik Dingel 		break;
7488c0a7ce6SDominik Dingel 	}
7498c0a7ce6SDominik Dingel 
7508c0a7ce6SDominik Dingel 	return ret;
751f2061656SDominik Dingel }
752f2061656SDominik Dingel 
753f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
754f2061656SDominik Dingel {
755f2061656SDominik Dingel 	int ret;
756f2061656SDominik Dingel 
757f2061656SDominik Dingel 	switch (attr->group) {
7584f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
7594f718eabSDominik Dingel 		switch (attr->attr) {
7604f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
7614f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
7628c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
7634f718eabSDominik Dingel 			ret = 0;
7644f718eabSDominik Dingel 			break;
7654f718eabSDominik Dingel 		default:
7664f718eabSDominik Dingel 			ret = -ENXIO;
7674f718eabSDominik Dingel 			break;
7684f718eabSDominik Dingel 		}
7694f718eabSDominik Dingel 		break;
77072f25020SJason J. Herne 	case KVM_S390_VM_TOD:
77172f25020SJason J. Herne 		switch (attr->attr) {
77272f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
77372f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
77472f25020SJason J. Herne 			ret = 0;
77572f25020SJason J. Herne 			break;
77672f25020SJason J. Herne 		default:
77772f25020SJason J. Herne 			ret = -ENXIO;
77872f25020SJason J. Herne 			break;
77972f25020SJason J. Herne 		}
78072f25020SJason J. Herne 		break;
781658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
782658b6edaSMichael Mueller 		switch (attr->attr) {
783658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
784658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
785658b6edaSMichael Mueller 			ret = 0;
786658b6edaSMichael Mueller 			break;
787658b6edaSMichael Mueller 		default:
788658b6edaSMichael Mueller 			ret = -ENXIO;
789658b6edaSMichael Mueller 			break;
790658b6edaSMichael Mueller 		}
791658b6edaSMichael Mueller 		break;
792a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
793a374e892STony Krowiak 		switch (attr->attr) {
794a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
795a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
796a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
797a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
798a374e892STony Krowiak 			ret = 0;
799a374e892STony Krowiak 			break;
800a374e892STony Krowiak 		default:
801a374e892STony Krowiak 			ret = -ENXIO;
802a374e892STony Krowiak 			break;
803a374e892STony Krowiak 		}
804a374e892STony Krowiak 		break;
805f2061656SDominik Dingel 	default:
806f2061656SDominik Dingel 		ret = -ENXIO;
807f2061656SDominik Dingel 		break;
808f2061656SDominik Dingel 	}
809f2061656SDominik Dingel 
810f2061656SDominik Dingel 	return ret;
811f2061656SDominik Dingel }
812f2061656SDominik Dingel 
81330ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
81430ee2a98SJason J. Herne {
81530ee2a98SJason J. Herne 	uint8_t *keys;
81630ee2a98SJason J. Herne 	uint64_t hva;
81730ee2a98SJason J. Herne 	unsigned long curkey;
81830ee2a98SJason J. Herne 	int i, r = 0;
81930ee2a98SJason J. Herne 
82030ee2a98SJason J. Herne 	if (args->flags != 0)
82130ee2a98SJason J. Herne 		return -EINVAL;
82230ee2a98SJason J. Herne 
82330ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
82430ee2a98SJason J. Herne 	if (!mm_use_skey(current->mm))
82530ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
82630ee2a98SJason J. Herne 
82730ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
82830ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
82930ee2a98SJason J. Herne 		return -EINVAL;
83030ee2a98SJason J. Herne 
83130ee2a98SJason J. Herne 	keys = kmalloc_array(args->count, sizeof(uint8_t),
83230ee2a98SJason J. Herne 			     GFP_KERNEL | __GFP_NOWARN);
83330ee2a98SJason J. Herne 	if (!keys)
83430ee2a98SJason J. Herne 		keys = vmalloc(sizeof(uint8_t) * args->count);
83530ee2a98SJason J. Herne 	if (!keys)
83630ee2a98SJason J. Herne 		return -ENOMEM;
83730ee2a98SJason J. Herne 
83830ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
83930ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
84030ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
84130ee2a98SJason J. Herne 			r = -EFAULT;
84230ee2a98SJason J. Herne 			goto out;
84330ee2a98SJason J. Herne 		}
84430ee2a98SJason J. Herne 
84530ee2a98SJason J. Herne 		curkey = get_guest_storage_key(current->mm, hva);
84630ee2a98SJason J. Herne 		if (IS_ERR_VALUE(curkey)) {
84730ee2a98SJason J. Herne 			r = curkey;
84830ee2a98SJason J. Herne 			goto out;
84930ee2a98SJason J. Herne 		}
85030ee2a98SJason J. Herne 		keys[i] = curkey;
85130ee2a98SJason J. Herne 	}
85230ee2a98SJason J. Herne 
85330ee2a98SJason J. Herne 	r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
85430ee2a98SJason J. Herne 			 sizeof(uint8_t) * args->count);
85530ee2a98SJason J. Herne 	if (r)
85630ee2a98SJason J. Herne 		r = -EFAULT;
85730ee2a98SJason J. Herne out:
85830ee2a98SJason J. Herne 	kvfree(keys);
85930ee2a98SJason J. Herne 	return r;
86030ee2a98SJason J. Herne }
86130ee2a98SJason J. Herne 
86230ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
86330ee2a98SJason J. Herne {
86430ee2a98SJason J. Herne 	uint8_t *keys;
86530ee2a98SJason J. Herne 	uint64_t hva;
86630ee2a98SJason J. Herne 	int i, r = 0;
86730ee2a98SJason J. Herne 
86830ee2a98SJason J. Herne 	if (args->flags != 0)
86930ee2a98SJason J. Herne 		return -EINVAL;
87030ee2a98SJason J. Herne 
87130ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
87230ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
87330ee2a98SJason J. Herne 		return -EINVAL;
87430ee2a98SJason J. Herne 
87530ee2a98SJason J. Herne 	keys = kmalloc_array(args->count, sizeof(uint8_t),
87630ee2a98SJason J. Herne 			     GFP_KERNEL | __GFP_NOWARN);
87730ee2a98SJason J. Herne 	if (!keys)
87830ee2a98SJason J. Herne 		keys = vmalloc(sizeof(uint8_t) * args->count);
87930ee2a98SJason J. Herne 	if (!keys)
88030ee2a98SJason J. Herne 		return -ENOMEM;
88130ee2a98SJason J. Herne 
88230ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
88330ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
88430ee2a98SJason J. Herne 	if (r) {
88530ee2a98SJason J. Herne 		r = -EFAULT;
88630ee2a98SJason J. Herne 		goto out;
88730ee2a98SJason J. Herne 	}
88830ee2a98SJason J. Herne 
88930ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
89014d4a425SDominik Dingel 	r = s390_enable_skey();
89114d4a425SDominik Dingel 	if (r)
89214d4a425SDominik Dingel 		goto out;
89330ee2a98SJason J. Herne 
89430ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
89530ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
89630ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
89730ee2a98SJason J. Herne 			r = -EFAULT;
89830ee2a98SJason J. Herne 			goto out;
89930ee2a98SJason J. Herne 		}
90030ee2a98SJason J. Herne 
90130ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
90230ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
90330ee2a98SJason J. Herne 			r = -EINVAL;
90430ee2a98SJason J. Herne 			goto out;
90530ee2a98SJason J. Herne 		}
90630ee2a98SJason J. Herne 
90730ee2a98SJason J. Herne 		r = set_guest_storage_key(current->mm, hva,
90830ee2a98SJason J. Herne 					  (unsigned long)keys[i], 0);
90930ee2a98SJason J. Herne 		if (r)
91030ee2a98SJason J. Herne 			goto out;
91130ee2a98SJason J. Herne 	}
91230ee2a98SJason J. Herne out:
91330ee2a98SJason J. Herne 	kvfree(keys);
91430ee2a98SJason J. Herne 	return r;
91530ee2a98SJason J. Herne }
91630ee2a98SJason J. Herne 
917b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
918b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
919b0c632dbSHeiko Carstens {
920b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
921b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
922f2061656SDominik Dingel 	struct kvm_device_attr attr;
923b0c632dbSHeiko Carstens 	int r;
924b0c632dbSHeiko Carstens 
925b0c632dbSHeiko Carstens 	switch (ioctl) {
926ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
927ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
928ba5c1e9bSCarsten Otte 
929ba5c1e9bSCarsten Otte 		r = -EFAULT;
930ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
931ba5c1e9bSCarsten Otte 			break;
932ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
933ba5c1e9bSCarsten Otte 		break;
934ba5c1e9bSCarsten Otte 	}
935d938dc55SCornelia Huck 	case KVM_ENABLE_CAP: {
936d938dc55SCornelia Huck 		struct kvm_enable_cap cap;
937d938dc55SCornelia Huck 		r = -EFAULT;
938d938dc55SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
939d938dc55SCornelia Huck 			break;
940d938dc55SCornelia Huck 		r = kvm_vm_ioctl_enable_cap(kvm, &cap);
941d938dc55SCornelia Huck 		break;
942d938dc55SCornelia Huck 	}
94384223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
94484223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
94584223598SCornelia Huck 
94684223598SCornelia Huck 		r = -EINVAL;
94784223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
94884223598SCornelia Huck 			/* Set up dummy routing. */
94984223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
950152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
95184223598SCornelia Huck 		}
95284223598SCornelia Huck 		break;
95384223598SCornelia Huck 	}
954f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
955f2061656SDominik Dingel 		r = -EFAULT;
956f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
957f2061656SDominik Dingel 			break;
958f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
959f2061656SDominik Dingel 		break;
960f2061656SDominik Dingel 	}
961f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
962f2061656SDominik Dingel 		r = -EFAULT;
963f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
964f2061656SDominik Dingel 			break;
965f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
966f2061656SDominik Dingel 		break;
967f2061656SDominik Dingel 	}
968f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
969f2061656SDominik Dingel 		r = -EFAULT;
970f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
971f2061656SDominik Dingel 			break;
972f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
973f2061656SDominik Dingel 		break;
974f2061656SDominik Dingel 	}
97530ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
97630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
97730ee2a98SJason J. Herne 
97830ee2a98SJason J. Herne 		r = -EFAULT;
97930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
98030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
98130ee2a98SJason J. Herne 			break;
98230ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
98330ee2a98SJason J. Herne 		break;
98430ee2a98SJason J. Herne 	}
98530ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
98630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
98730ee2a98SJason J. Herne 
98830ee2a98SJason J. Herne 		r = -EFAULT;
98930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
99030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
99130ee2a98SJason J. Herne 			break;
99230ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
99330ee2a98SJason J. Herne 		break;
99430ee2a98SJason J. Herne 	}
995b0c632dbSHeiko Carstens 	default:
996367e1319SAvi Kivity 		r = -ENOTTY;
997b0c632dbSHeiko Carstens 	}
998b0c632dbSHeiko Carstens 
999b0c632dbSHeiko Carstens 	return r;
1000b0c632dbSHeiko Carstens }
1001b0c632dbSHeiko Carstens 
100245c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config)
100345c9b47cSTony Krowiak {
100445c9b47cSTony Krowiak 	u32 fcn_code = 0x04000000UL;
100586044c8cSChristian Borntraeger 	u32 cc = 0;
100645c9b47cSTony Krowiak 
100786044c8cSChristian Borntraeger 	memset(config, 0, 128);
100845c9b47cSTony Krowiak 	asm volatile(
100945c9b47cSTony Krowiak 		"lgr 0,%1\n"
101045c9b47cSTony Krowiak 		"lgr 2,%2\n"
101145c9b47cSTony Krowiak 		".long 0xb2af0000\n"		/* PQAP(QCI) */
101286044c8cSChristian Borntraeger 		"0: ipm %0\n"
101345c9b47cSTony Krowiak 		"srl %0,28\n"
101486044c8cSChristian Borntraeger 		"1:\n"
101586044c8cSChristian Borntraeger 		EX_TABLE(0b, 1b)
101686044c8cSChristian Borntraeger 		: "+r" (cc)
101745c9b47cSTony Krowiak 		: "r" (fcn_code), "r" (config)
101845c9b47cSTony Krowiak 		: "cc", "0", "2", "memory"
101945c9b47cSTony Krowiak 	);
102045c9b47cSTony Krowiak 
102145c9b47cSTony Krowiak 	return cc;
102245c9b47cSTony Krowiak }
102345c9b47cSTony Krowiak 
102445c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
102545c9b47cSTony Krowiak {
102645c9b47cSTony Krowiak 	u8 config[128];
102745c9b47cSTony Krowiak 	int cc;
102845c9b47cSTony Krowiak 
102945c9b47cSTony Krowiak 	if (test_facility(2) && test_facility(12)) {
103045c9b47cSTony Krowiak 		cc = kvm_s390_query_ap_config(config);
103145c9b47cSTony Krowiak 
103245c9b47cSTony Krowiak 		if (cc)
103345c9b47cSTony Krowiak 			pr_err("PQAP(QCI) failed with cc=%d", cc);
103445c9b47cSTony Krowiak 		else
103545c9b47cSTony Krowiak 			return config[0] & 0x40;
103645c9b47cSTony Krowiak 	}
103745c9b47cSTony Krowiak 
103845c9b47cSTony Krowiak 	return 0;
103945c9b47cSTony Krowiak }
104045c9b47cSTony Krowiak 
104145c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
104245c9b47cSTony Krowiak {
104345c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
104445c9b47cSTony Krowiak 
104545c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
104645c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
104745c9b47cSTony Krowiak 	else
104845c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
104945c9b47cSTony Krowiak }
105045c9b47cSTony Krowiak 
10519d8d5786SMichael Mueller static void kvm_s390_get_cpu_id(struct cpuid *cpu_id)
10529d8d5786SMichael Mueller {
10539d8d5786SMichael Mueller 	get_cpu_id(cpu_id);
10549d8d5786SMichael Mueller 	cpu_id->version = 0xff;
10559d8d5786SMichael Mueller }
10569d8d5786SMichael Mueller 
10575102ee87STony Krowiak static int kvm_s390_crypto_init(struct kvm *kvm)
10585102ee87STony Krowiak {
10599d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
10605102ee87STony Krowiak 		return 0;
10615102ee87STony Krowiak 
10625102ee87STony Krowiak 	kvm->arch.crypto.crycb = kzalloc(sizeof(*kvm->arch.crypto.crycb),
10635102ee87STony Krowiak 					 GFP_KERNEL | GFP_DMA);
10645102ee87STony Krowiak 	if (!kvm->arch.crypto.crycb)
10655102ee87STony Krowiak 		return -ENOMEM;
10665102ee87STony Krowiak 
106745c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
10685102ee87STony Krowiak 
1069ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
1070ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
1071ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
1072ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1073ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1074ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1075ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
1076a374e892STony Krowiak 
10775102ee87STony Krowiak 	return 0;
10785102ee87STony Krowiak }
10795102ee87STony Krowiak 
1080e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1081b0c632dbSHeiko Carstens {
10829d8d5786SMichael Mueller 	int i, rc;
1083b0c632dbSHeiko Carstens 	char debug_name[16];
1084f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
1085b0c632dbSHeiko Carstens 
1086e08b9637SCarsten Otte 	rc = -EINVAL;
1087e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1088e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
1089e08b9637SCarsten Otte 		goto out_err;
1090e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1091e08b9637SCarsten Otte 		goto out_err;
1092e08b9637SCarsten Otte #else
1093e08b9637SCarsten Otte 	if (type)
1094e08b9637SCarsten Otte 		goto out_err;
1095e08b9637SCarsten Otte #endif
1096e08b9637SCarsten Otte 
1097b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
1098b0c632dbSHeiko Carstens 	if (rc)
1099d89f5effSJan Kiszka 		goto out_err;
1100b0c632dbSHeiko Carstens 
1101b290411aSCarsten Otte 	rc = -ENOMEM;
1102b290411aSCarsten Otte 
1103*bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(GFP_KERNEL);
1104b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
1105d89f5effSJan Kiszka 		goto out_err;
1106f6c137ffSChristian Borntraeger 	spin_lock(&kvm_lock);
1107c5c2c393SDavid Hildenbrand 	sca_offset += 16;
1108*bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
1109c5c2c393SDavid Hildenbrand 		sca_offset = 0;
1110*bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
1111*bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
1112f6c137ffSChristian Borntraeger 	spin_unlock(&kvm_lock);
1113b0c632dbSHeiko Carstens 
1114b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
1115b0c632dbSHeiko Carstens 
11161cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
1117b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
111840f5b735SDominik Dingel 		goto out_err;
1119b0c632dbSHeiko Carstens 
11209d8d5786SMichael Mueller 	/*
11219d8d5786SMichael Mueller 	 * The architectural maximum amount of facilities is 16 kbit. To store
11229d8d5786SMichael Mueller 	 * this amount, 2 kbyte of memory is required. Thus we need a full
1123981467c9SMichael Mueller 	 * page to hold the guest facility list (arch.model.fac->list) and the
1124981467c9SMichael Mueller 	 * facility mask (arch.model.fac->mask). Its address size has to be
11259d8d5786SMichael Mueller 	 * 31 bits and word aligned.
11269d8d5786SMichael Mueller 	 */
11279d8d5786SMichael Mueller 	kvm->arch.model.fac =
1128981467c9SMichael Mueller 		(struct kvm_s390_fac *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
11299d8d5786SMichael Mueller 	if (!kvm->arch.model.fac)
113040f5b735SDominik Dingel 		goto out_err;
11319d8d5786SMichael Mueller 
1132fb5bf93fSMichael Mueller 	/* Populate the facility mask initially. */
1133981467c9SMichael Mueller 	memcpy(kvm->arch.model.fac->mask, S390_lowcore.stfle_fac_list,
113494422ee8SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
11359d8d5786SMichael Mueller 	for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
11369d8d5786SMichael Mueller 		if (i < kvm_s390_fac_list_mask_size())
1137981467c9SMichael Mueller 			kvm->arch.model.fac->mask[i] &= kvm_s390_fac_list_mask[i];
11389d8d5786SMichael Mueller 		else
1139981467c9SMichael Mueller 			kvm->arch.model.fac->mask[i] = 0UL;
11409d8d5786SMichael Mueller 	}
11419d8d5786SMichael Mueller 
1142981467c9SMichael Mueller 	/* Populate the facility list initially. */
1143981467c9SMichael Mueller 	memcpy(kvm->arch.model.fac->list, kvm->arch.model.fac->mask,
1144981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1145981467c9SMichael Mueller 
11469d8d5786SMichael Mueller 	kvm_s390_get_cpu_id(&kvm->arch.model.cpu_id);
114737c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
11489d8d5786SMichael Mueller 
11495102ee87STony Krowiak 	if (kvm_s390_crypto_init(kvm) < 0)
115040f5b735SDominik Dingel 		goto out_err;
11515102ee87STony Krowiak 
1152ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
11536d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
11546d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
11558a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
1156a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
1157ba5c1e9bSCarsten Otte 
1158b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
115978f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
1160b0c632dbSHeiko Carstens 
1161e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
1162e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
1163e08b9637SCarsten Otte 	} else {
11640349985aSChristian Borntraeger 		kvm->arch.gmap = gmap_alloc(current->mm, (1UL << 44) - 1);
1165598841caSCarsten Otte 		if (!kvm->arch.gmap)
116640f5b735SDominik Dingel 			goto out_err;
11672c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
116824eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
1169e08b9637SCarsten Otte 	}
1170fa6b7fe9SCornelia Huck 
1171fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
117284223598SCornelia Huck 	kvm->arch.use_irqchip = 0;
117372f25020SJason J. Herne 	kvm->arch.epoch = 0;
1174fa6b7fe9SCornelia Huck 
11758ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
117678f26131SChristian Borntraeger 	KVM_EVENT(3, "vm 0x%p created by pid %u", kvm, current->pid);
11778ad35755SDavid Hildenbrand 
1178d89f5effSJan Kiszka 	return 0;
1179d89f5effSJan Kiszka out_err:
118040f5b735SDominik Dingel 	kfree(kvm->arch.crypto.crycb);
118140f5b735SDominik Dingel 	free_page((unsigned long)kvm->arch.model.fac);
118240f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
118340f5b735SDominik Dingel 	free_page((unsigned long)(kvm->arch.sca));
118478f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
1185d89f5effSJan Kiszka 	return rc;
1186b0c632dbSHeiko Carstens }
1187b0c632dbSHeiko Carstens 
1188d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1189d329c035SChristian Borntraeger {
1190d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
1191ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
119267335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
11933c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
1194*bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
1195a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
1196abf4a71eSCarsten Otte 	smp_mb();
119727e0393fSCarsten Otte 
119827e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
119927e0393fSCarsten Otte 		gmap_free(vcpu->arch.gmap);
120027e0393fSCarsten Otte 
1201e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
1202b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
1203d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
1204b31288faSKonstantin Weitz 
12056692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
1206b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
1207d329c035SChristian Borntraeger }
1208d329c035SChristian Borntraeger 
1209d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
1210d329c035SChristian Borntraeger {
1211d329c035SChristian Borntraeger 	unsigned int i;
1212988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
1213d329c035SChristian Borntraeger 
1214988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
1215988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
1216988a2caeSGleb Natapov 
1217988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
1218988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1219d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
1220988a2caeSGleb Natapov 
1221988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
1222988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
1223d329c035SChristian Borntraeger }
1224d329c035SChristian Borntraeger 
1225b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
1226b0c632dbSHeiko Carstens {
1227d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
12289d8d5786SMichael Mueller 	free_page((unsigned long)kvm->arch.model.fac);
1229b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
1230d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
12315102ee87STony Krowiak 	kfree(kvm->arch.crypto.crycb);
123227e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
1233598841caSCarsten Otte 		gmap_free(kvm->arch.gmap);
1234841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
123567335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
123678f26131SChristian Borntraeger 	KVM_EVENT(3, "vm 0x%p destroyed", kvm);
1237b0c632dbSHeiko Carstens }
1238b0c632dbSHeiko Carstens 
1239b0c632dbSHeiko Carstens /* Section: vcpu related */
1240dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1241b0c632dbSHeiko Carstens {
1242c6c956b8SMartin Schwidefsky 	vcpu->arch.gmap = gmap_alloc(current->mm, -1UL);
124327e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
124427e0393fSCarsten Otte 		return -ENOMEM;
12452c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
1246dafd032aSDominik Dingel 
124727e0393fSCarsten Otte 	return 0;
124827e0393fSCarsten Otte }
124927e0393fSCarsten Otte 
1250a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
1251a6e2f683SEugene (jno) Dvurechenski {
1252*bc784cceSEugene (jno) Dvurechenski 	struct bsca_block *sca = vcpu->kvm->arch.sca;
1253a6e2f683SEugene (jno) Dvurechenski 
1254a6e2f683SEugene (jno) Dvurechenski 	clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
1255a6e2f683SEugene (jno) Dvurechenski 	if (sca->cpu[vcpu->vcpu_id].sda == (__u64) vcpu->arch.sie_block)
1256a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
1257a6e2f683SEugene (jno) Dvurechenski }
1258a6e2f683SEugene (jno) Dvurechenski 
1259a6e2f683SEugene (jno) Dvurechenski static void sca_add_vcpu(struct kvm_vcpu *vcpu, struct kvm *kvm,
1260a6e2f683SEugene (jno) Dvurechenski 			unsigned int id)
1261a6e2f683SEugene (jno) Dvurechenski {
1262*bc784cceSEugene (jno) Dvurechenski 	struct bsca_block *sca = kvm->arch.sca;
1263a6e2f683SEugene (jno) Dvurechenski 
1264a6e2f683SEugene (jno) Dvurechenski 	if (!sca->cpu[id].sda)
1265a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[id].sda = (__u64) vcpu->arch.sie_block;
1266a6e2f683SEugene (jno) Dvurechenski 	vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1267a6e2f683SEugene (jno) Dvurechenski 	vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
1268a6e2f683SEugene (jno) Dvurechenski 	set_bit_inv(id, (unsigned long *) &sca->mcn);
1269a6e2f683SEugene (jno) Dvurechenski }
1270a6e2f683SEugene (jno) Dvurechenski 
1271a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
1272a6e2f683SEugene (jno) Dvurechenski {
1273a6e2f683SEugene (jno) Dvurechenski 	return id < KVM_MAX_VCPUS;
1274a6e2f683SEugene (jno) Dvurechenski }
1275a6e2f683SEugene (jno) Dvurechenski 
1276dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1277dafd032aSDominik Dingel {
1278dafd032aSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1279dafd032aSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
128059674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
128159674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
12829eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
1283b028ee3eSDavid Hildenbrand 				    KVM_SYNC_CRS |
1284b028ee3eSDavid Hildenbrand 				    KVM_SYNC_ARCH0 |
1285b028ee3eSDavid Hildenbrand 				    KVM_SYNC_PFAULT;
128668c55750SEric Farman 	if (test_kvm_facility(vcpu->kvm, 129))
128768c55750SEric Farman 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
1288dafd032aSDominik Dingel 
1289dafd032aSDominik Dingel 	if (kvm_is_ucontrol(vcpu->kvm))
1290dafd032aSDominik Dingel 		return __kvm_ucontrol_vcpu_init(vcpu);
1291dafd032aSDominik Dingel 
1292b0c632dbSHeiko Carstens 	return 0;
1293b0c632dbSHeiko Carstens }
1294b0c632dbSHeiko Carstens 
12959977e886SHendrik Brueckner /*
12969977e886SHendrik Brueckner  * Backs up the current FP/VX register save area on a particular
12979977e886SHendrik Brueckner  * destination.  Used to switch between different register save
12989977e886SHendrik Brueckner  * areas.
12999977e886SHendrik Brueckner  */
13009977e886SHendrik Brueckner static inline void save_fpu_to(struct fpu *dst)
13019977e886SHendrik Brueckner {
13029977e886SHendrik Brueckner 	dst->fpc = current->thread.fpu.fpc;
13039977e886SHendrik Brueckner 	dst->regs = current->thread.fpu.regs;
13049977e886SHendrik Brueckner }
13059977e886SHendrik Brueckner 
13069977e886SHendrik Brueckner /*
13079977e886SHendrik Brueckner  * Switches the FP/VX register save area from which to lazy
13089977e886SHendrik Brueckner  * restore register contents.
13099977e886SHendrik Brueckner  */
13109977e886SHendrik Brueckner static inline void load_fpu_from(struct fpu *from)
13119977e886SHendrik Brueckner {
13129977e886SHendrik Brueckner 	current->thread.fpu.fpc = from->fpc;
13139977e886SHendrik Brueckner 	current->thread.fpu.regs = from->regs;
13149977e886SHendrik Brueckner }
13159977e886SHendrik Brueckner 
1316b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1317b0c632dbSHeiko Carstens {
13189977e886SHendrik Brueckner 	/* Save host register state */
1319d0164ee2SHendrik Brueckner 	save_fpu_regs();
13209977e886SHendrik Brueckner 	save_fpu_to(&vcpu->arch.host_fpregs);
132196b2d7a8SHendrik Brueckner 
132218280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
13239977e886SHendrik Brueckner 		current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
13249977e886SHendrik Brueckner 		/*
13259977e886SHendrik Brueckner 		 * Use the register save area in the SIE-control block
13269977e886SHendrik Brueckner 		 * for register restore and save in kvm_arch_vcpu_put()
13279977e886SHendrik Brueckner 		 */
13289977e886SHendrik Brueckner 		current->thread.fpu.vxrs =
13299977e886SHendrik Brueckner 			(__vector128 *)&vcpu->run->s.regs.vrs;
13309977e886SHendrik Brueckner 	} else
13319977e886SHendrik Brueckner 		load_fpu_from(&vcpu->arch.guest_fpregs);
13329977e886SHendrik Brueckner 
13339977e886SHendrik Brueckner 	if (test_fp_ctl(current->thread.fpu.fpc))
133496b2d7a8SHendrik Brueckner 		/* User space provided an invalid FPC, let's clear it */
13359977e886SHendrik Brueckner 		current->thread.fpu.fpc = 0;
13369977e886SHendrik Brueckner 
13379977e886SHendrik Brueckner 	save_access_regs(vcpu->arch.host_acrs);
133859674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
1339480e5926SChristian Borntraeger 	gmap_enable(vcpu->arch.gmap);
1340805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
1341b0c632dbSHeiko Carstens }
1342b0c632dbSHeiko Carstens 
1343b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1344b0c632dbSHeiko Carstens {
1345805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
1346480e5926SChristian Borntraeger 	gmap_disable(vcpu->arch.gmap);
13479977e886SHendrik Brueckner 
1348d0164ee2SHendrik Brueckner 	save_fpu_regs();
13499977e886SHendrik Brueckner 
135018280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129))
13519977e886SHendrik Brueckner 		/*
13529977e886SHendrik Brueckner 		 * kvm_arch_vcpu_load() set up the register save area to
13539977e886SHendrik Brueckner 		 * the &vcpu->run->s.regs.vrs and, thus, the vector registers
13549977e886SHendrik Brueckner 		 * are already saved.  Only the floating-point control must be
13559977e886SHendrik Brueckner 		 * copied.
13569977e886SHendrik Brueckner 		 */
13579977e886SHendrik Brueckner 		vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
135868c55750SEric Farman 	else
13599977e886SHendrik Brueckner 		save_fpu_to(&vcpu->arch.guest_fpregs);
13609977e886SHendrik Brueckner 	load_fpu_from(&vcpu->arch.host_fpregs);
13619977e886SHendrik Brueckner 
13629977e886SHendrik Brueckner 	save_access_regs(vcpu->run->s.regs.acrs);
1363b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.host_acrs);
1364b0c632dbSHeiko Carstens }
1365b0c632dbSHeiko Carstens 
1366b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1367b0c632dbSHeiko Carstens {
1368b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
1369b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
1370b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
13718d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
1372b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->cputm     = 0UL;
1373b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
1374b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
1375b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1376b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
1377b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
1378b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = 0;
1379b0c632dbSHeiko Carstens 	asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
1380b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
1381672550fbSChristian Borntraeger 	vcpu->arch.sie_block->pp = 0;
13823c038e6bSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
13833c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
13846352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
13856852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
13862ed10cc1SJens Freimann 	kvm_s390_clear_local_irqs(vcpu);
1387b0c632dbSHeiko Carstens }
1388b0c632dbSHeiko Carstens 
138931928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
139042897d86SMarcelo Tosatti {
139172f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
1392fdf03650SFan Zhang 	preempt_disable();
139372f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
1394fdf03650SFan Zhang 	preempt_enable();
139572f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
1396dafd032aSDominik Dingel 	if (!kvm_is_ucontrol(vcpu->kvm))
1397dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
139842897d86SMarcelo Tosatti }
139942897d86SMarcelo Tosatti 
14005102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
14015102ee87STony Krowiak {
14029d8d5786SMichael Mueller 	if (!test_kvm_facility(vcpu->kvm, 76))
14035102ee87STony Krowiak 		return;
14045102ee87STony Krowiak 
1405a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1406a374e892STony Krowiak 
1407a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.aes_kw)
1408a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1409a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
1410a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1411a374e892STony Krowiak 
14125102ee87STony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
14135102ee87STony Krowiak }
14145102ee87STony Krowiak 
1415b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1416b31605c1SDominik Dingel {
1417b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
1418b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
1419b31605c1SDominik Dingel }
1420b31605c1SDominik Dingel 
1421b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1422b31605c1SDominik Dingel {
1423b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1424b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
1425b31605c1SDominik Dingel 		return -ENOMEM;
1426b31605c1SDominik Dingel 
1427b31605c1SDominik Dingel 	vcpu->arch.sie_block->ecb2 |= 0x80;
1428b31605c1SDominik Dingel 	vcpu->arch.sie_block->ecb2 &= ~0x08;
1429b31605c1SDominik Dingel 	return 0;
1430b31605c1SDominik Dingel }
1431b31605c1SDominik Dingel 
143291520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
143391520f1aSMichael Mueller {
143491520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
143591520f1aSMichael Mueller 
143691520f1aSMichael Mueller 	vcpu->arch.cpu_id = model->cpu_id;
143791520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
143891520f1aSMichael Mueller 	vcpu->arch.sie_block->fac = (int) (long) model->fac->list;
143991520f1aSMichael Mueller }
144091520f1aSMichael Mueller 
1441b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1442b0c632dbSHeiko Carstens {
1443b31605c1SDominik Dingel 	int rc = 0;
1444b31288faSKonstantin Weitz 
14459e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
14469e6dabefSCornelia Huck 						    CPUSTAT_SM |
1447a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
1448a4a4f191SGuenther Hutzl 
144953df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
1450805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
145153df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
1452805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
1453a4a4f191SGuenther Hutzl 
145491520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
145591520f1aSMichael Mueller 
1456fc34531dSChristian Borntraeger 	vcpu->arch.sie_block->ecb   = 6;
14579d8d5786SMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73))
14587feb6bb8SMichael Mueller 		vcpu->arch.sie_block->ecb |= 0x10;
14597feb6bb8SMichael Mueller 
146069d0d3a3SChristian Borntraeger 	vcpu->arch.sie_block->ecb2  = 8;
1461ea5f4969SDavid Hildenbrand 	vcpu->arch.sie_block->eca   = 0xC1002000U;
146237c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
1463217a4406SHeiko Carstens 		vcpu->arch.sie_block->eca |= 1;
146437c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
1465ea5f4969SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= 0x10000000U;
146618280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
146713211ea7SEric Farman 		vcpu->arch.sie_block->eca |= 0x00020000;
146813211ea7SEric Farman 		vcpu->arch.sie_block->ecd |= 0x20000000;
146913211ea7SEric Farman 	}
1470492d8642SThomas Huth 	vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
14715a5e6536SMatthew Rosato 
1472e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
1473b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
1474b31605c1SDominik Dingel 		if (rc)
1475b31605c1SDominik Dingel 			return rc;
1476b31288faSKonstantin Weitz 	}
14770ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1478ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
14799d8d5786SMichael Mueller 
14805102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
14815102ee87STony Krowiak 
1482b31605c1SDominik Dingel 	return rc;
1483b0c632dbSHeiko Carstens }
1484b0c632dbSHeiko Carstens 
1485b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1486b0c632dbSHeiko Carstens 				      unsigned int id)
1487b0c632dbSHeiko Carstens {
14884d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
14897feb6bb8SMichael Mueller 	struct sie_page *sie_page;
14904d47555aSCarsten Otte 	int rc = -EINVAL;
1491b0c632dbSHeiko Carstens 
1492a6e2f683SEugene (jno) Dvurechenski 	if (!sca_can_add_vcpu(kvm, id))
14934d47555aSCarsten Otte 		goto out;
14944d47555aSCarsten Otte 
14954d47555aSCarsten Otte 	rc = -ENOMEM;
14964d47555aSCarsten Otte 
1497b110feafSMichael Mueller 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1498b0c632dbSHeiko Carstens 	if (!vcpu)
14994d47555aSCarsten Otte 		goto out;
1500b0c632dbSHeiko Carstens 
15017feb6bb8SMichael Mueller 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
15027feb6bb8SMichael Mueller 	if (!sie_page)
1503b0c632dbSHeiko Carstens 		goto out_free_cpu;
1504b0c632dbSHeiko Carstens 
15057feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
15067feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
15077feb6bb8SMichael Mueller 
1508b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
150958f9460bSCarsten Otte 	if (!kvm_is_ucontrol(kvm)) {
151058f9460bSCarsten Otte 		if (!kvm->arch.sca) {
151158f9460bSCarsten Otte 			WARN_ON_ONCE(1);
151258f9460bSCarsten Otte 			goto out_free_cpu;
151358f9460bSCarsten Otte 		}
1514a6e2f683SEugene (jno) Dvurechenski 		sca_add_vcpu(vcpu, kvm, id);
151558f9460bSCarsten Otte 	}
1516b0c632dbSHeiko Carstens 
1517ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
1518ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
1519d0321a24SChristian Borntraeger 	vcpu->arch.local_int.wq = &vcpu->wq;
15205288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
1521ba5c1e9bSCarsten Otte 
15229977e886SHendrik Brueckner 	/*
15239977e886SHendrik Brueckner 	 * Allocate a save area for floating-point registers.  If the vector
15249977e886SHendrik Brueckner 	 * extension is available, register contents are saved in the SIE
15259977e886SHendrik Brueckner 	 * control block.  The allocated save area is still required in
15269977e886SHendrik Brueckner 	 * particular places, for example, in kvm_s390_vcpu_store_status().
15279977e886SHendrik Brueckner 	 */
15289977e886SHendrik Brueckner 	vcpu->arch.guest_fpregs.fprs = kzalloc(sizeof(freg_t) * __NUM_FPRS,
15299977e886SHendrik Brueckner 					       GFP_KERNEL);
15309977e886SHendrik Brueckner 	if (!vcpu->arch.guest_fpregs.fprs) {
15319977e886SHendrik Brueckner 		rc = -ENOMEM;
15329977e886SHendrik Brueckner 		goto out_free_sie_block;
15339977e886SHendrik Brueckner 	}
15349977e886SHendrik Brueckner 
1535b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
1536b0c632dbSHeiko Carstens 	if (rc)
15377b06bf2fSWei Yongjun 		goto out_free_sie_block;
1538b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
1539b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
1540ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
1541b0c632dbSHeiko Carstens 
1542b0c632dbSHeiko Carstens 	return vcpu;
15437b06bf2fSWei Yongjun out_free_sie_block:
15447b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
1545b0c632dbSHeiko Carstens out_free_cpu:
1546b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
15474d47555aSCarsten Otte out:
1548b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
1549b0c632dbSHeiko Carstens }
1550b0c632dbSHeiko Carstens 
1551b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1552b0c632dbSHeiko Carstens {
15539a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
1554b0c632dbSHeiko Carstens }
1555b0c632dbSHeiko Carstens 
155627406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
155749b99e1eSChristian Borntraeger {
1558805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
155961a6df54SDavid Hildenbrand 	exit_sie(vcpu);
156049b99e1eSChristian Borntraeger }
156149b99e1eSChristian Borntraeger 
156227406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
156349b99e1eSChristian Borntraeger {
1564805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
156549b99e1eSChristian Borntraeger }
156649b99e1eSChristian Borntraeger 
15678e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
15688e236546SChristian Borntraeger {
1569805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
157061a6df54SDavid Hildenbrand 	exit_sie(vcpu);
15718e236546SChristian Borntraeger }
15728e236546SChristian Borntraeger 
15738e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
15748e236546SChristian Borntraeger {
15759bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
15768e236546SChristian Borntraeger }
15778e236546SChristian Borntraeger 
157849b99e1eSChristian Borntraeger /*
157949b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
158049b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
158149b99e1eSChristian Borntraeger  * return immediately. */
158249b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
158349b99e1eSChristian Borntraeger {
1584805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
158549b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
158649b99e1eSChristian Borntraeger 		cpu_relax();
158749b99e1eSChristian Borntraeger }
158849b99e1eSChristian Borntraeger 
15898e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
15908e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
159149b99e1eSChristian Borntraeger {
15928e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
15938e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
159449b99e1eSChristian Borntraeger }
159549b99e1eSChristian Borntraeger 
15962c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
15972c70fe44SChristian Borntraeger {
15982c70fe44SChristian Borntraeger 	int i;
15992c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
16002c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
16012c70fe44SChristian Borntraeger 
16022c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
16032c70fe44SChristian Borntraeger 		/* match against both prefix pages */
1604fda902cbSMichael Mueller 		if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) {
16052c70fe44SChristian Borntraeger 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
16068e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
16072c70fe44SChristian Borntraeger 		}
16082c70fe44SChristian Borntraeger 	}
16092c70fe44SChristian Borntraeger }
16102c70fe44SChristian Borntraeger 
1611b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1612b6d33834SChristoffer Dall {
1613b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
1614b6d33834SChristoffer Dall 	BUG();
1615b6d33834SChristoffer Dall 	return 0;
1616b6d33834SChristoffer Dall }
1617b6d33834SChristoffer Dall 
161814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
161914eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
162014eebd91SCarsten Otte {
162114eebd91SCarsten Otte 	int r = -EINVAL;
162214eebd91SCarsten Otte 
162314eebd91SCarsten Otte 	switch (reg->id) {
162429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
162529b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
162629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
162729b7c71bSCarsten Otte 		break;
162829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
162929b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
163029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
163129b7c71bSCarsten Otte 		break;
163246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
163346a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->cputm,
163446a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
163546a6dd1cSJason J. herne 		break;
163646a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
163746a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
163846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
163946a6dd1cSJason J. herne 		break;
1640536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
1641536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
1642536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1643536336c2SDominik Dingel 		break;
1644536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
1645536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
1646536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1647536336c2SDominik Dingel 		break;
1648536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
1649536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
1650536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1651536336c2SDominik Dingel 		break;
1652672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
1653672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
1654672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
1655672550fbSChristian Borntraeger 		break;
1656afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
1657afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
1658afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
1659afa45ff5SChristian Borntraeger 		break;
166014eebd91SCarsten Otte 	default:
166114eebd91SCarsten Otte 		break;
166214eebd91SCarsten Otte 	}
166314eebd91SCarsten Otte 
166414eebd91SCarsten Otte 	return r;
166514eebd91SCarsten Otte }
166614eebd91SCarsten Otte 
166714eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
166814eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
166914eebd91SCarsten Otte {
167014eebd91SCarsten Otte 	int r = -EINVAL;
167114eebd91SCarsten Otte 
167214eebd91SCarsten Otte 	switch (reg->id) {
167329b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
167429b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
167529b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
167629b7c71bSCarsten Otte 		break;
167729b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
167829b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
167929b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
168029b7c71bSCarsten Otte 		break;
168146a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
168246a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->cputm,
168346a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
168446a6dd1cSJason J. herne 		break;
168546a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
168646a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
168746a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
168846a6dd1cSJason J. herne 		break;
1689536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
1690536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
1691536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
16929fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
16939fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
1694536336c2SDominik Dingel 		break;
1695536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
1696536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
1697536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1698536336c2SDominik Dingel 		break;
1699536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
1700536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
1701536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1702536336c2SDominik Dingel 		break;
1703672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
1704672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
1705672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
1706672550fbSChristian Borntraeger 		break;
1707afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
1708afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
1709afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
1710afa45ff5SChristian Borntraeger 		break;
171114eebd91SCarsten Otte 	default:
171214eebd91SCarsten Otte 		break;
171314eebd91SCarsten Otte 	}
171414eebd91SCarsten Otte 
171514eebd91SCarsten Otte 	return r;
171614eebd91SCarsten Otte }
1717b6d33834SChristoffer Dall 
1718b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
1719b0c632dbSHeiko Carstens {
1720b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
1721b0c632dbSHeiko Carstens 	return 0;
1722b0c632dbSHeiko Carstens }
1723b0c632dbSHeiko Carstens 
1724b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1725b0c632dbSHeiko Carstens {
17265a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
1727b0c632dbSHeiko Carstens 	return 0;
1728b0c632dbSHeiko Carstens }
1729b0c632dbSHeiko Carstens 
1730b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1731b0c632dbSHeiko Carstens {
17325a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
1733b0c632dbSHeiko Carstens 	return 0;
1734b0c632dbSHeiko Carstens }
1735b0c632dbSHeiko Carstens 
1736b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1737b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
1738b0c632dbSHeiko Carstens {
173959674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
1740b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
174159674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
1742b0c632dbSHeiko Carstens 	return 0;
1743b0c632dbSHeiko Carstens }
1744b0c632dbSHeiko Carstens 
1745b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1746b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
1747b0c632dbSHeiko Carstens {
174859674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
1749b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
1750b0c632dbSHeiko Carstens 	return 0;
1751b0c632dbSHeiko Carstens }
1752b0c632dbSHeiko Carstens 
1753b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1754b0c632dbSHeiko Carstens {
17554725c860SMartin Schwidefsky 	if (test_fp_ctl(fpu->fpc))
17564725c860SMartin Schwidefsky 		return -EINVAL;
17579977e886SHendrik Brueckner 	memcpy(vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
17584725c860SMartin Schwidefsky 	vcpu->arch.guest_fpregs.fpc = fpu->fpc;
1759d0164ee2SHendrik Brueckner 	save_fpu_regs();
17609977e886SHendrik Brueckner 	load_fpu_from(&vcpu->arch.guest_fpregs);
1761b0c632dbSHeiko Carstens 	return 0;
1762b0c632dbSHeiko Carstens }
1763b0c632dbSHeiko Carstens 
1764b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1765b0c632dbSHeiko Carstens {
17669977e886SHendrik Brueckner 	memcpy(&fpu->fprs, vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
1767b0c632dbSHeiko Carstens 	fpu->fpc = vcpu->arch.guest_fpregs.fpc;
1768b0c632dbSHeiko Carstens 	return 0;
1769b0c632dbSHeiko Carstens }
1770b0c632dbSHeiko Carstens 
1771b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
1772b0c632dbSHeiko Carstens {
1773b0c632dbSHeiko Carstens 	int rc = 0;
1774b0c632dbSHeiko Carstens 
17757a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
1776b0c632dbSHeiko Carstens 		rc = -EBUSY;
1777d7b0b5ebSCarsten Otte 	else {
1778d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
1779d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
1780d7b0b5ebSCarsten Otte 	}
1781b0c632dbSHeiko Carstens 	return rc;
1782b0c632dbSHeiko Carstens }
1783b0c632dbSHeiko Carstens 
1784b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1785b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
1786b0c632dbSHeiko Carstens {
1787b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
1788b0c632dbSHeiko Carstens }
1789b0c632dbSHeiko Carstens 
179027291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
179127291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
179227291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
179327291e21SDavid Hildenbrand 
1794d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1795d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
1796b0c632dbSHeiko Carstens {
179727291e21SDavid Hildenbrand 	int rc = 0;
179827291e21SDavid Hildenbrand 
179927291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
180027291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
180127291e21SDavid Hildenbrand 
18022de3bfc2SDavid Hildenbrand 	if (dbg->control & ~VALID_GUESTDBG_FLAGS)
180327291e21SDavid Hildenbrand 		return -EINVAL;
180427291e21SDavid Hildenbrand 
180527291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
180627291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
180727291e21SDavid Hildenbrand 		/* enforce guest PER */
1808805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
180927291e21SDavid Hildenbrand 
181027291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
181127291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
181227291e21SDavid Hildenbrand 	} else {
1813805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
181427291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
181527291e21SDavid Hildenbrand 	}
181627291e21SDavid Hildenbrand 
181727291e21SDavid Hildenbrand 	if (rc) {
181827291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
181927291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
1820805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
182127291e21SDavid Hildenbrand 	}
182227291e21SDavid Hildenbrand 
182327291e21SDavid Hildenbrand 	return rc;
1824b0c632dbSHeiko Carstens }
1825b0c632dbSHeiko Carstens 
182662d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
182762d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
182862d9f0dbSMarcelo Tosatti {
18296352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
18306352e4d2SDavid Hildenbrand 	return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
18316352e4d2SDavid Hildenbrand 				       KVM_MP_STATE_OPERATING;
183262d9f0dbSMarcelo Tosatti }
183362d9f0dbSMarcelo Tosatti 
183462d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
183562d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
183662d9f0dbSMarcelo Tosatti {
18376352e4d2SDavid Hildenbrand 	int rc = 0;
18386352e4d2SDavid Hildenbrand 
18396352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
18406352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
18416352e4d2SDavid Hildenbrand 
18426352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
18436352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
18446352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
18456352e4d2SDavid Hildenbrand 		break;
18466352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
18476352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
18486352e4d2SDavid Hildenbrand 		break;
18496352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
18506352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
18516352e4d2SDavid Hildenbrand 		/* fall through - CHECK_STOP and LOAD are not supported yet */
18526352e4d2SDavid Hildenbrand 	default:
18536352e4d2SDavid Hildenbrand 		rc = -ENXIO;
18546352e4d2SDavid Hildenbrand 	}
18556352e4d2SDavid Hildenbrand 
18566352e4d2SDavid Hildenbrand 	return rc;
185762d9f0dbSMarcelo Tosatti }
185862d9f0dbSMarcelo Tosatti 
18598ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
18608ad35755SDavid Hildenbrand {
18618ad35755SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
18628ad35755SDavid Hildenbrand }
18638ad35755SDavid Hildenbrand 
18642c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
18652c70fe44SChristian Borntraeger {
18668ad35755SDavid Hildenbrand retry:
18678e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
1868586b7ccdSChristian Borntraeger 	if (!vcpu->requests)
1869586b7ccdSChristian Borntraeger 		return 0;
18702c70fe44SChristian Borntraeger 	/*
18712c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
18722c70fe44SChristian Borntraeger 	 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
18732c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
18742c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
18752c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
18762c70fe44SChristian Borntraeger 	 */
18778ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
18782c70fe44SChristian Borntraeger 		int rc;
18792c70fe44SChristian Borntraeger 		rc = gmap_ipte_notify(vcpu->arch.gmap,
1880fda902cbSMichael Mueller 				      kvm_s390_get_prefix(vcpu),
18812c70fe44SChristian Borntraeger 				      PAGE_SIZE * 2);
18822c70fe44SChristian Borntraeger 		if (rc)
18832c70fe44SChristian Borntraeger 			return rc;
18848ad35755SDavid Hildenbrand 		goto retry;
18852c70fe44SChristian Borntraeger 	}
18868ad35755SDavid Hildenbrand 
1887d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
1888d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
1889d3d692c8SDavid Hildenbrand 		goto retry;
1890d3d692c8SDavid Hildenbrand 	}
1891d3d692c8SDavid Hildenbrand 
18928ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
18938ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
18948ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
1895805de8f4SPeter Zijlstra 			atomic_or(CPUSTAT_IBS,
18968ad35755SDavid Hildenbrand 					&vcpu->arch.sie_block->cpuflags);
18978ad35755SDavid Hildenbrand 		}
18988ad35755SDavid Hildenbrand 		goto retry;
18998ad35755SDavid Hildenbrand 	}
19008ad35755SDavid Hildenbrand 
19018ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
19028ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
19038ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
1904805de8f4SPeter Zijlstra 			atomic_andnot(CPUSTAT_IBS,
19058ad35755SDavid Hildenbrand 					  &vcpu->arch.sie_block->cpuflags);
19068ad35755SDavid Hildenbrand 		}
19078ad35755SDavid Hildenbrand 		goto retry;
19088ad35755SDavid Hildenbrand 	}
19098ad35755SDavid Hildenbrand 
19100759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
19110759d068SDavid Hildenbrand 	clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
19120759d068SDavid Hildenbrand 
19132c70fe44SChristian Borntraeger 	return 0;
19142c70fe44SChristian Borntraeger }
19152c70fe44SChristian Borntraeger 
191625ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
191725ed1675SDavid Hildenbrand {
191825ed1675SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
191925ed1675SDavid Hildenbrand 	int i;
192025ed1675SDavid Hildenbrand 
192125ed1675SDavid Hildenbrand 	mutex_lock(&kvm->lock);
192225ed1675SDavid Hildenbrand 	preempt_disable();
192325ed1675SDavid Hildenbrand 	kvm->arch.epoch = tod - get_tod_clock();
192425ed1675SDavid Hildenbrand 	kvm_s390_vcpu_block_all(kvm);
192525ed1675SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm)
192625ed1675SDavid Hildenbrand 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
192725ed1675SDavid Hildenbrand 	kvm_s390_vcpu_unblock_all(kvm);
192825ed1675SDavid Hildenbrand 	preempt_enable();
192925ed1675SDavid Hildenbrand 	mutex_unlock(&kvm->lock);
193025ed1675SDavid Hildenbrand }
193125ed1675SDavid Hildenbrand 
1932fa576c58SThomas Huth /**
1933fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
1934fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
1935fa576c58SThomas Huth  * @gpa: Guest physical address
1936fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
1937fa576c58SThomas Huth  *
1938fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
1939fa576c58SThomas Huth  *
1940fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
1941fa576c58SThomas Huth  */
1942fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
194324eb3a82SDominik Dingel {
1944527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
1945527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
194624eb3a82SDominik Dingel }
194724eb3a82SDominik Dingel 
19483c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
19493c038e6bSDominik Dingel 				      unsigned long token)
19503c038e6bSDominik Dingel {
19513c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
1952383d0b05SJens Freimann 	struct kvm_s390_irq irq;
19533c038e6bSDominik Dingel 
19543c038e6bSDominik Dingel 	if (start_token) {
1955383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
1956383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
1957383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
19583c038e6bSDominik Dingel 	} else {
19593c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
1960383d0b05SJens Freimann 		inti.parm64 = token;
19613c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
19623c038e6bSDominik Dingel 	}
19633c038e6bSDominik Dingel }
19643c038e6bSDominik Dingel 
19653c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
19663c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
19673c038e6bSDominik Dingel {
19683c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
19693c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
19703c038e6bSDominik Dingel }
19713c038e6bSDominik Dingel 
19723c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
19733c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
19743c038e6bSDominik Dingel {
19753c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
19763c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
19773c038e6bSDominik Dingel }
19783c038e6bSDominik Dingel 
19793c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
19803c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
19813c038e6bSDominik Dingel {
19823c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
19833c038e6bSDominik Dingel }
19843c038e6bSDominik Dingel 
19853c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
19863c038e6bSDominik Dingel {
19873c038e6bSDominik Dingel 	/*
19883c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
19893c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
19903c038e6bSDominik Dingel 	 */
19913c038e6bSDominik Dingel 	return true;
19923c038e6bSDominik Dingel }
19933c038e6bSDominik Dingel 
19943c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
19953c038e6bSDominik Dingel {
19963c038e6bSDominik Dingel 	hva_t hva;
19973c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
19983c038e6bSDominik Dingel 	int rc;
19993c038e6bSDominik Dingel 
20003c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
20013c038e6bSDominik Dingel 		return 0;
20023c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
20033c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
20043c038e6bSDominik Dingel 		return 0;
20053c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
20063c038e6bSDominik Dingel 		return 0;
20079a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
20083c038e6bSDominik Dingel 		return 0;
20093c038e6bSDominik Dingel 	if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
20103c038e6bSDominik Dingel 		return 0;
20113c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
20123c038e6bSDominik Dingel 		return 0;
20133c038e6bSDominik Dingel 
201481480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
201581480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
201681480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
20173c038e6bSDominik Dingel 		return 0;
20183c038e6bSDominik Dingel 
20193c038e6bSDominik Dingel 	rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
20203c038e6bSDominik Dingel 	return rc;
20213c038e6bSDominik Dingel }
20223c038e6bSDominik Dingel 
20233fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
2024b0c632dbSHeiko Carstens {
20253fb4c40fSThomas Huth 	int rc, cpuflags;
2026e168bf8dSCarsten Otte 
20273c038e6bSDominik Dingel 	/*
20283c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
20293c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
20303c038e6bSDominik Dingel 	 * handled outside the worker.
20313c038e6bSDominik Dingel 	 */
20323c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
20333c038e6bSDominik Dingel 
20345a32c1afSChristian Borntraeger 	memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
2035b0c632dbSHeiko Carstens 
2036b0c632dbSHeiko Carstens 	if (need_resched())
2037b0c632dbSHeiko Carstens 		schedule();
2038b0c632dbSHeiko Carstens 
2039d3a73acbSMartin Schwidefsky 	if (test_cpu_flag(CIF_MCCK_PENDING))
204071cde587SChristian Borntraeger 		s390_handle_mcck();
204171cde587SChristian Borntraeger 
204279395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
204379395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
204479395031SJens Freimann 		if (rc)
204579395031SJens Freimann 			return rc;
204679395031SJens Freimann 	}
20470ff31867SCarsten Otte 
20482c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
20492c70fe44SChristian Borntraeger 	if (rc)
20502c70fe44SChristian Borntraeger 		return rc;
20512c70fe44SChristian Borntraeger 
205227291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
205327291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
205427291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
205527291e21SDavid Hildenbrand 	}
205627291e21SDavid Hildenbrand 
2057b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
20583fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
20593fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
20603fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
20612b29a9fdSDominik Dingel 
20623fb4c40fSThomas Huth 	return 0;
20633fb4c40fSThomas Huth }
20643fb4c40fSThomas Huth 
2065492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
2066492d8642SThomas Huth {
2067492d8642SThomas Huth 	psw_t *psw = &vcpu->arch.sie_block->gpsw;
2068492d8642SThomas Huth 	u8 opcode;
2069492d8642SThomas Huth 	int rc;
2070492d8642SThomas Huth 
2071492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
2072492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
2073492d8642SThomas Huth 
2074492d8642SThomas Huth 	/*
2075492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
2076492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
2077492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
2078492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
2079492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
2080492d8642SThomas Huth 	 * to be able to forward the PSW.
2081492d8642SThomas Huth 	 */
20828ae04b8fSAlexander Yarygin 	rc = read_guest(vcpu, psw->addr, 0, &opcode, 1);
2083492d8642SThomas Huth 	if (rc)
2084492d8642SThomas Huth 		return kvm_s390_inject_prog_cond(vcpu, rc);
2085492d8642SThomas Huth 	psw->addr = __rewind_psw(*psw, -insn_length(opcode));
2086492d8642SThomas Huth 
2087492d8642SThomas Huth 	return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
2088492d8642SThomas Huth }
2089492d8642SThomas Huth 
20903fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
20913fb4c40fSThomas Huth {
20922b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
20932b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
20942b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
20952b29a9fdSDominik Dingel 
209627291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
209727291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
209827291e21SDavid Hildenbrand 
209971f116bfSDavid Hildenbrand 	memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
210071f116bfSDavid Hildenbrand 
210171f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
210271f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
210371f116bfSDavid Hildenbrand 
210471f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
210571f116bfSDavid Hildenbrand 			return rc;
210671f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
210771f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
210871f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
210971f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
211071f116bfSDavid Hildenbrand 		return -EREMOTE;
211171f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
211271f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
211371f116bfSDavid Hildenbrand 		return 0;
2114210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
2115210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
2116210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
2117210b1607SThomas Huth 						current->thread.gmap_addr;
2118210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
211971f116bfSDavid Hildenbrand 		return -EREMOTE;
212024eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
21213c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
212224eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
212371f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
212471f116bfSDavid Hildenbrand 			return 0;
212571f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
2126fa576c58SThomas Huth 	}
212771f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
21283fb4c40fSThomas Huth }
21293fb4c40fSThomas Huth 
21303fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
21313fb4c40fSThomas Huth {
21323fb4c40fSThomas Huth 	int rc, exit_reason;
21333fb4c40fSThomas Huth 
2134800c1065SThomas Huth 	/*
2135800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2136800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
2137800c1065SThomas Huth 	 */
2138800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2139800c1065SThomas Huth 
2140a76ccff6SThomas Huth 	do {
21413fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
21423fb4c40fSThomas Huth 		if (rc)
2143a76ccff6SThomas Huth 			break;
21443fb4c40fSThomas Huth 
2145800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
21463fb4c40fSThomas Huth 		/*
2147a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
2148a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
21493fb4c40fSThomas Huth 		 */
21500097d12eSChristian Borntraeger 		local_irq_disable();
21510097d12eSChristian Borntraeger 		__kvm_guest_enter();
21520097d12eSChristian Borntraeger 		local_irq_enable();
2153a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
2154a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
21550097d12eSChristian Borntraeger 		local_irq_disable();
21560097d12eSChristian Borntraeger 		__kvm_guest_exit();
21570097d12eSChristian Borntraeger 		local_irq_enable();
2158800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
21593fb4c40fSThomas Huth 
21603fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
216127291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
21623fb4c40fSThomas Huth 
2163800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2164e168bf8dSCarsten Otte 	return rc;
2165b0c632dbSHeiko Carstens }
2166b0c632dbSHeiko Carstens 
2167b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2168b028ee3eSDavid Hildenbrand {
2169b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2170b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2171b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2172b028ee3eSDavid Hildenbrand 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2173b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2174b028ee3eSDavid Hildenbrand 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
2175d3d692c8SDavid Hildenbrand 		/* some control register changes require a tlb flush */
2176d3d692c8SDavid Hildenbrand 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2177b028ee3eSDavid Hildenbrand 	}
2178b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
2179b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->cputm = kvm_run->s.regs.cputm;
2180b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2181b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2182b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2183b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2184b028ee3eSDavid Hildenbrand 	}
2185b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2186b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2187b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2188b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
21899fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
21909fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2191b028ee3eSDavid Hildenbrand 	}
2192b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
2193b028ee3eSDavid Hildenbrand }
2194b028ee3eSDavid Hildenbrand 
2195b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2196b028ee3eSDavid Hildenbrand {
2197b028ee3eSDavid Hildenbrand 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
2198b028ee3eSDavid Hildenbrand 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
2199b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
2200b028ee3eSDavid Hildenbrand 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
2201b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.cputm = vcpu->arch.sie_block->cputm;
2202b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
2203b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
2204b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
2205b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
2206b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
2207b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
2208b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
2209b028ee3eSDavid Hildenbrand }
2210b028ee3eSDavid Hildenbrand 
2211b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2212b0c632dbSHeiko Carstens {
22138f2abe6aSChristian Borntraeger 	int rc;
2214b0c632dbSHeiko Carstens 	sigset_t sigsaved;
2215b0c632dbSHeiko Carstens 
221627291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
221727291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
221827291e21SDavid Hildenbrand 		return 0;
221927291e21SDavid Hildenbrand 	}
222027291e21SDavid Hildenbrand 
2221b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
2222b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2223b0c632dbSHeiko Carstens 
22246352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
22256852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
22266352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
2227ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
22286352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
22296352e4d2SDavid Hildenbrand 		return -EINVAL;
22306352e4d2SDavid Hildenbrand 	}
2231b0c632dbSHeiko Carstens 
2232b028ee3eSDavid Hildenbrand 	sync_regs(vcpu, kvm_run);
2233d7b0b5ebSCarsten Otte 
2234dab4079dSHeiko Carstens 	might_fault();
2235e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
22369ace903dSChristian Ehrhardt 
2237b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
2238b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
22398f2abe6aSChristian Borntraeger 		rc = -EINTR;
2240b1d16c49SChristian Ehrhardt 	}
22418f2abe6aSChristian Borntraeger 
224227291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
224327291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
224427291e21SDavid Hildenbrand 		rc = 0;
224527291e21SDavid Hildenbrand 	}
224627291e21SDavid Hildenbrand 
22478f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
224871f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
22498f2abe6aSChristian Borntraeger 		rc = 0;
22508f2abe6aSChristian Borntraeger 	}
22518f2abe6aSChristian Borntraeger 
2252b028ee3eSDavid Hildenbrand 	store_regs(vcpu, kvm_run);
2253d7b0b5ebSCarsten Otte 
2254b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
2255b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2256b0c632dbSHeiko Carstens 
2257b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
22587e8e6ab4SHeiko Carstens 	return rc;
2259b0c632dbSHeiko Carstens }
2260b0c632dbSHeiko Carstens 
2261b0c632dbSHeiko Carstens /*
2262b0c632dbSHeiko Carstens  * store status at address
2263b0c632dbSHeiko Carstens  * we use have two special cases:
2264b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2265b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2266b0c632dbSHeiko Carstens  */
2267d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
2268b0c632dbSHeiko Carstens {
2269092670cdSCarsten Otte 	unsigned char archmode = 1;
2270fda902cbSMichael Mueller 	unsigned int px;
2271178bd789SThomas Huth 	u64 clkcomp;
2272d0bce605SHeiko Carstens 	int rc;
2273b0c632dbSHeiko Carstens 
2274d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
2275d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
2276b0c632dbSHeiko Carstens 			return -EFAULT;
2277d0bce605SHeiko Carstens 		gpa = SAVE_AREA_BASE;
2278d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
2279d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
2280b0c632dbSHeiko Carstens 			return -EFAULT;
2281d0bce605SHeiko Carstens 		gpa = kvm_s390_real_to_abs(vcpu, SAVE_AREA_BASE);
2282d0bce605SHeiko Carstens 	}
2283d0bce605SHeiko Carstens 	rc = write_guest_abs(vcpu, gpa + offsetof(struct save_area, fp_regs),
2284d0bce605SHeiko Carstens 			     vcpu->arch.guest_fpregs.fprs, 128);
2285d0bce605SHeiko Carstens 	rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, gp_regs),
2286d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
2287d0bce605SHeiko Carstens 	rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, psw),
2288d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
2289fda902cbSMichael Mueller 	px = kvm_s390_get_prefix(vcpu);
2290d0bce605SHeiko Carstens 	rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, pref_reg),
2291fda902cbSMichael Mueller 			      &px, 4);
2292d0bce605SHeiko Carstens 	rc |= write_guest_abs(vcpu,
2293d0bce605SHeiko Carstens 			      gpa + offsetof(struct save_area, fp_ctrl_reg),
2294d0bce605SHeiko Carstens 			      &vcpu->arch.guest_fpregs.fpc, 4);
2295d0bce605SHeiko Carstens 	rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, tod_reg),
2296d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
2297d0bce605SHeiko Carstens 	rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, timer),
2298d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->cputm, 8);
2299178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
2300d0bce605SHeiko Carstens 	rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, clk_cmp),
2301d0bce605SHeiko Carstens 			      &clkcomp, 8);
2302d0bce605SHeiko Carstens 	rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, acc_regs),
2303d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
2304d0bce605SHeiko Carstens 	rc |= write_guest_abs(vcpu, gpa + offsetof(struct save_area, ctrl_regs),
2305d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
2306d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
2307b0c632dbSHeiko Carstens }
2308b0c632dbSHeiko Carstens 
2309e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2310e879892cSThomas Huth {
2311e879892cSThomas Huth 	/*
2312e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
2313e879892cSThomas Huth 	 * copying in vcpu load/put. Lets update our copies before we save
2314e879892cSThomas Huth 	 * it into the save area
2315e879892cSThomas Huth 	 */
2316d0164ee2SHendrik Brueckner 	save_fpu_regs();
23179977e886SHendrik Brueckner 	if (test_kvm_facility(vcpu->kvm, 129)) {
23189977e886SHendrik Brueckner 		/*
23199977e886SHendrik Brueckner 		 * If the vector extension is available, the vector registers
23209977e886SHendrik Brueckner 		 * which overlaps with floating-point registers are saved in
23219977e886SHendrik Brueckner 		 * the SIE-control block.  Hence, extract the floating-point
23229977e886SHendrik Brueckner 		 * registers and the FPC value and store them in the
23239977e886SHendrik Brueckner 		 * guest_fpregs structure.
23249977e886SHendrik Brueckner 		 */
23259977e886SHendrik Brueckner 		vcpu->arch.guest_fpregs.fpc = current->thread.fpu.fpc;
23269977e886SHendrik Brueckner 		convert_vx_to_fp(vcpu->arch.guest_fpregs.fprs,
23279977e886SHendrik Brueckner 				 current->thread.fpu.vxrs);
23289977e886SHendrik Brueckner 	} else
23299977e886SHendrik Brueckner 		save_fpu_to(&vcpu->arch.guest_fpregs);
2330e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
2331e879892cSThomas Huth 
2332e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
2333e879892cSThomas Huth }
2334e879892cSThomas Huth 
2335bc17de7cSEric Farman /*
2336bc17de7cSEric Farman  * store additional status at address
2337bc17de7cSEric Farman  */
2338bc17de7cSEric Farman int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
2339bc17de7cSEric Farman 					unsigned long gpa)
2340bc17de7cSEric Farman {
2341bc17de7cSEric Farman 	/* Only bits 0-53 are used for address formation */
2342bc17de7cSEric Farman 	if (!(gpa & ~0x3ff))
2343bc17de7cSEric Farman 		return 0;
2344bc17de7cSEric Farman 
2345bc17de7cSEric Farman 	return write_guest_abs(vcpu, gpa & ~0x3ff,
2346bc17de7cSEric Farman 			       (void *)&vcpu->run->s.regs.vrs, 512);
2347bc17de7cSEric Farman }
2348bc17de7cSEric Farman 
2349bc17de7cSEric Farman int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr)
2350bc17de7cSEric Farman {
2351bc17de7cSEric Farman 	if (!test_kvm_facility(vcpu->kvm, 129))
2352bc17de7cSEric Farman 		return 0;
2353bc17de7cSEric Farman 
2354bc17de7cSEric Farman 	/*
2355bc17de7cSEric Farman 	 * The guest VXRS are in the host VXRs due to the lazy
23569977e886SHendrik Brueckner 	 * copying in vcpu load/put. We can simply call save_fpu_regs()
23579977e886SHendrik Brueckner 	 * to save the current register state because we are in the
23589977e886SHendrik Brueckner 	 * middle of a load/put cycle.
23599977e886SHendrik Brueckner 	 *
23609977e886SHendrik Brueckner 	 * Let's update our copies before we save it into the save area.
2361bc17de7cSEric Farman 	 */
2362d0164ee2SHendrik Brueckner 	save_fpu_regs();
2363bc17de7cSEric Farman 
2364bc17de7cSEric Farman 	return kvm_s390_store_adtl_status_unloaded(vcpu, addr);
2365bc17de7cSEric Farman }
2366bc17de7cSEric Farman 
23678ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
23688ad35755SDavid Hildenbrand {
23698ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
23708e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
23718ad35755SDavid Hildenbrand }
23728ad35755SDavid Hildenbrand 
23738ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
23748ad35755SDavid Hildenbrand {
23758ad35755SDavid Hildenbrand 	unsigned int i;
23768ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
23778ad35755SDavid Hildenbrand 
23788ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
23798ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
23808ad35755SDavid Hildenbrand 	}
23818ad35755SDavid Hildenbrand }
23828ad35755SDavid Hildenbrand 
23838ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
23848ad35755SDavid Hildenbrand {
23858ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
23868e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
23878ad35755SDavid Hildenbrand }
23888ad35755SDavid Hildenbrand 
23896852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
23906852d7b6SDavid Hildenbrand {
23918ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
23928ad35755SDavid Hildenbrand 
23938ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
23948ad35755SDavid Hildenbrand 		return;
23958ad35755SDavid Hildenbrand 
23966852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
23978ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
2398433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
23998ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
24008ad35755SDavid Hildenbrand 
24018ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
24028ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
24038ad35755SDavid Hildenbrand 			started_vcpus++;
24048ad35755SDavid Hildenbrand 	}
24058ad35755SDavid Hildenbrand 
24068ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
24078ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
24088ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
24098ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
24108ad35755SDavid Hildenbrand 		/*
24118ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
24128ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
24138ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
24148ad35755SDavid Hildenbrand 		 */
24158ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
24168ad35755SDavid Hildenbrand 	}
24178ad35755SDavid Hildenbrand 
2418805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
24198ad35755SDavid Hildenbrand 	/*
24208ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
24218ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
24228ad35755SDavid Hildenbrand 	 */
2423d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2424433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
24258ad35755SDavid Hildenbrand 	return;
24266852d7b6SDavid Hildenbrand }
24276852d7b6SDavid Hildenbrand 
24286852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
24296852d7b6SDavid Hildenbrand {
24308ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
24318ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
24328ad35755SDavid Hildenbrand 
24338ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
24348ad35755SDavid Hildenbrand 		return;
24358ad35755SDavid Hildenbrand 
24366852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
24378ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
2438433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
24398ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
24408ad35755SDavid Hildenbrand 
244132f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
24426cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
244332f5ff63SDavid Hildenbrand 
2444805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
24458ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
24468ad35755SDavid Hildenbrand 
24478ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
24488ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
24498ad35755SDavid Hildenbrand 			started_vcpus++;
24508ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
24518ad35755SDavid Hildenbrand 		}
24528ad35755SDavid Hildenbrand 	}
24538ad35755SDavid Hildenbrand 
24548ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
24558ad35755SDavid Hildenbrand 		/*
24568ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
24578ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
24588ad35755SDavid Hildenbrand 		 */
24598ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
24608ad35755SDavid Hildenbrand 	}
24618ad35755SDavid Hildenbrand 
2462433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
24638ad35755SDavid Hildenbrand 	return;
24646852d7b6SDavid Hildenbrand }
24656852d7b6SDavid Hildenbrand 
2466d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2467d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
2468d6712df9SCornelia Huck {
2469d6712df9SCornelia Huck 	int r;
2470d6712df9SCornelia Huck 
2471d6712df9SCornelia Huck 	if (cap->flags)
2472d6712df9SCornelia Huck 		return -EINVAL;
2473d6712df9SCornelia Huck 
2474d6712df9SCornelia Huck 	switch (cap->cap) {
2475fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
2476fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
2477fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
2478c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
2479fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
2480fa6b7fe9SCornelia Huck 		}
2481fa6b7fe9SCornelia Huck 		r = 0;
2482fa6b7fe9SCornelia Huck 		break;
2483d6712df9SCornelia Huck 	default:
2484d6712df9SCornelia Huck 		r = -EINVAL;
2485d6712df9SCornelia Huck 		break;
2486d6712df9SCornelia Huck 	}
2487d6712df9SCornelia Huck 	return r;
2488d6712df9SCornelia Huck }
2489d6712df9SCornelia Huck 
249041408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
249141408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
249241408c28SThomas Huth {
249341408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
249441408c28SThomas Huth 	void *tmpbuf = NULL;
249541408c28SThomas Huth 	int r, srcu_idx;
249641408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
249741408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
249841408c28SThomas Huth 
249941408c28SThomas Huth 	if (mop->flags & ~supported_flags)
250041408c28SThomas Huth 		return -EINVAL;
250141408c28SThomas Huth 
250241408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
250341408c28SThomas Huth 		return -E2BIG;
250441408c28SThomas Huth 
250541408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
250641408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
250741408c28SThomas Huth 		if (!tmpbuf)
250841408c28SThomas Huth 			return -ENOMEM;
250941408c28SThomas Huth 	}
251041408c28SThomas Huth 
251141408c28SThomas Huth 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
251241408c28SThomas Huth 
251341408c28SThomas Huth 	switch (mop->op) {
251441408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
251541408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
251641408c28SThomas Huth 			r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, false);
251741408c28SThomas Huth 			break;
251841408c28SThomas Huth 		}
251941408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
252041408c28SThomas Huth 		if (r == 0) {
252141408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
252241408c28SThomas Huth 				r = -EFAULT;
252341408c28SThomas Huth 		}
252441408c28SThomas Huth 		break;
252541408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
252641408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
252741408c28SThomas Huth 			r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, true);
252841408c28SThomas Huth 			break;
252941408c28SThomas Huth 		}
253041408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
253141408c28SThomas Huth 			r = -EFAULT;
253241408c28SThomas Huth 			break;
253341408c28SThomas Huth 		}
253441408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
253541408c28SThomas Huth 		break;
253641408c28SThomas Huth 	default:
253741408c28SThomas Huth 		r = -EINVAL;
253841408c28SThomas Huth 	}
253941408c28SThomas Huth 
254041408c28SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
254141408c28SThomas Huth 
254241408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
254341408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
254441408c28SThomas Huth 
254541408c28SThomas Huth 	vfree(tmpbuf);
254641408c28SThomas Huth 	return r;
254741408c28SThomas Huth }
254841408c28SThomas Huth 
2549b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
2550b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
2551b0c632dbSHeiko Carstens {
2552b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
2553b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
2554800c1065SThomas Huth 	int idx;
2555bc923cc9SAvi Kivity 	long r;
2556b0c632dbSHeiko Carstens 
255793736624SAvi Kivity 	switch (ioctl) {
255847b43c52SJens Freimann 	case KVM_S390_IRQ: {
255947b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
256047b43c52SJens Freimann 
256147b43c52SJens Freimann 		r = -EFAULT;
256247b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
256347b43c52SJens Freimann 			break;
256447b43c52SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
256547b43c52SJens Freimann 		break;
256647b43c52SJens Freimann 	}
256793736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
2568ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
2569383d0b05SJens Freimann 		struct kvm_s390_irq s390irq;
2570ba5c1e9bSCarsten Otte 
257193736624SAvi Kivity 		r = -EFAULT;
2572ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
257393736624SAvi Kivity 			break;
2574383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
2575383d0b05SJens Freimann 			return -EINVAL;
2576383d0b05SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
257793736624SAvi Kivity 		break;
2578ba5c1e9bSCarsten Otte 	}
2579b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
2580800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
2581bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
2582800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
2583bc923cc9SAvi Kivity 		break;
2584b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
2585b0c632dbSHeiko Carstens 		psw_t psw;
2586b0c632dbSHeiko Carstens 
2587bc923cc9SAvi Kivity 		r = -EFAULT;
2588b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
2589bc923cc9SAvi Kivity 			break;
2590bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
2591bc923cc9SAvi Kivity 		break;
2592b0c632dbSHeiko Carstens 	}
2593b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
2594bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
2595bc923cc9SAvi Kivity 		break;
259614eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
259714eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
259814eebd91SCarsten Otte 		struct kvm_one_reg reg;
259914eebd91SCarsten Otte 		r = -EFAULT;
260014eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
260114eebd91SCarsten Otte 			break;
260214eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
260314eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
260414eebd91SCarsten Otte 		else
260514eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
260614eebd91SCarsten Otte 		break;
260714eebd91SCarsten Otte 	}
260827e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
260927e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
261027e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
261127e0393fSCarsten Otte 
261227e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
261327e0393fSCarsten Otte 			r = -EFAULT;
261427e0393fSCarsten Otte 			break;
261527e0393fSCarsten Otte 		}
261627e0393fSCarsten Otte 
261727e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
261827e0393fSCarsten Otte 			r = -EINVAL;
261927e0393fSCarsten Otte 			break;
262027e0393fSCarsten Otte 		}
262127e0393fSCarsten Otte 
262227e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
262327e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
262427e0393fSCarsten Otte 		break;
262527e0393fSCarsten Otte 	}
262627e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
262727e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
262827e0393fSCarsten Otte 
262927e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
263027e0393fSCarsten Otte 			r = -EFAULT;
263127e0393fSCarsten Otte 			break;
263227e0393fSCarsten Otte 		}
263327e0393fSCarsten Otte 
263427e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
263527e0393fSCarsten Otte 			r = -EINVAL;
263627e0393fSCarsten Otte 			break;
263727e0393fSCarsten Otte 		}
263827e0393fSCarsten Otte 
263927e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
264027e0393fSCarsten Otte 			ucasmap.length);
264127e0393fSCarsten Otte 		break;
264227e0393fSCarsten Otte 	}
264327e0393fSCarsten Otte #endif
2644ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
2645527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
2646ccc7910fSCarsten Otte 		break;
2647ccc7910fSCarsten Otte 	}
2648d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
2649d6712df9SCornelia Huck 	{
2650d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
2651d6712df9SCornelia Huck 		r = -EFAULT;
2652d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
2653d6712df9SCornelia Huck 			break;
2654d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2655d6712df9SCornelia Huck 		break;
2656d6712df9SCornelia Huck 	}
265741408c28SThomas Huth 	case KVM_S390_MEM_OP: {
265841408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
265941408c28SThomas Huth 
266041408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
266141408c28SThomas Huth 			r = kvm_s390_guest_mem_op(vcpu, &mem_op);
266241408c28SThomas Huth 		else
266341408c28SThomas Huth 			r = -EFAULT;
266441408c28SThomas Huth 		break;
266541408c28SThomas Huth 	}
2666816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
2667816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
2668816c7667SJens Freimann 
2669816c7667SJens Freimann 		r = -EFAULT;
2670816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2671816c7667SJens Freimann 			break;
2672816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
2673816c7667SJens Freimann 		    irq_state.len == 0 ||
2674816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
2675816c7667SJens Freimann 			r = -EINVAL;
2676816c7667SJens Freimann 			break;
2677816c7667SJens Freimann 		}
2678816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
2679816c7667SJens Freimann 					   (void __user *) irq_state.buf,
2680816c7667SJens Freimann 					   irq_state.len);
2681816c7667SJens Freimann 		break;
2682816c7667SJens Freimann 	}
2683816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
2684816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
2685816c7667SJens Freimann 
2686816c7667SJens Freimann 		r = -EFAULT;
2687816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2688816c7667SJens Freimann 			break;
2689816c7667SJens Freimann 		if (irq_state.len == 0) {
2690816c7667SJens Freimann 			r = -EINVAL;
2691816c7667SJens Freimann 			break;
2692816c7667SJens Freimann 		}
2693816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
2694816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
2695816c7667SJens Freimann 					   irq_state.len);
2696816c7667SJens Freimann 		break;
2697816c7667SJens Freimann 	}
2698b0c632dbSHeiko Carstens 	default:
26993e6afcf1SCarsten Otte 		r = -ENOTTY;
2700b0c632dbSHeiko Carstens 	}
2701bc923cc9SAvi Kivity 	return r;
2702b0c632dbSHeiko Carstens }
2703b0c632dbSHeiko Carstens 
27045b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
27055b1c1493SCarsten Otte {
27065b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
27075b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
27085b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
27095b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
27105b1c1493SCarsten Otte 		get_page(vmf->page);
27115b1c1493SCarsten Otte 		return 0;
27125b1c1493SCarsten Otte 	}
27135b1c1493SCarsten Otte #endif
27145b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
27155b1c1493SCarsten Otte }
27165b1c1493SCarsten Otte 
27175587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
27185587027cSAneesh Kumar K.V 			    unsigned long npages)
2719db3fe4ebSTakuya Yoshikawa {
2720db3fe4ebSTakuya Yoshikawa 	return 0;
2721db3fe4ebSTakuya Yoshikawa }
2722db3fe4ebSTakuya Yoshikawa 
2723b0c632dbSHeiko Carstens /* Section: memory related */
2724f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
2725f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
272609170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
27277b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
2728b0c632dbSHeiko Carstens {
2729dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
2730dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
2731dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
2732dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
2733b0c632dbSHeiko Carstens 
2734598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
2735b0c632dbSHeiko Carstens 		return -EINVAL;
2736b0c632dbSHeiko Carstens 
2737598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
2738b0c632dbSHeiko Carstens 		return -EINVAL;
2739b0c632dbSHeiko Carstens 
2740f7784b8eSMarcelo Tosatti 	return 0;
2741f7784b8eSMarcelo Tosatti }
2742f7784b8eSMarcelo Tosatti 
2743f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
274409170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
27458482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
2746f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
27478482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
2748f7784b8eSMarcelo Tosatti {
2749f7850c92SCarsten Otte 	int rc;
2750f7784b8eSMarcelo Tosatti 
27512cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
27522cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
27532cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
27542cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
27552cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
27562cef4debSChristian Borntraeger 	 */
27572cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
27582cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
27592cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
27602cef4debSChristian Borntraeger 		return;
2761598841caSCarsten Otte 
2762598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
2763598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
2764598841caSCarsten Otte 	if (rc)
2765ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
2766598841caSCarsten Otte 	return;
2767b0c632dbSHeiko Carstens }
2768b0c632dbSHeiko Carstens 
2769b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
2770b0c632dbSHeiko Carstens {
27719d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
2772b0c632dbSHeiko Carstens }
2773b0c632dbSHeiko Carstens 
2774b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
2775b0c632dbSHeiko Carstens {
2776b0c632dbSHeiko Carstens 	kvm_exit();
2777b0c632dbSHeiko Carstens }
2778b0c632dbSHeiko Carstens 
2779b0c632dbSHeiko Carstens module_init(kvm_s390_init);
2780b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
2781566af940SCornelia Huck 
2782566af940SCornelia Huck /*
2783566af940SCornelia Huck  * Enable autoloading of the kvm module.
2784566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
2785566af940SCornelia Huck  * since x86 takes a different approach.
2786566af940SCornelia Huck  */
2787566af940SCornelia Huck #include <linux/miscdevice.h>
2788566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
2789566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
2790