xref: /openbmc/linux/arch/s390/kvm/kvm-s390.c (revision 20c922f04b17aa51a75e514eca8fcbfa337a002d)
1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b0c632dbSHeiko Carstens /*
3bb64da9aSChristian Borntraeger  * hosting IBM Z kernel virtual machines (s390x)
4b0c632dbSHeiko Carstens  *
5a37cb07aSChristian Borntraeger  * Copyright IBM Corp. 2008, 2018
6b0c632dbSHeiko Carstens  *
7b0c632dbSHeiko Carstens  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8b0c632dbSHeiko Carstens  *               Christian Borntraeger <borntraeger@de.ibm.com>
9b0c632dbSHeiko Carstens  *               Heiko Carstens <heiko.carstens@de.ibm.com>
10628eb9b8SChristian Ehrhardt  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
1115f36ebdSJason J. Herne  *               Jason J. Herne <jjherne@us.ibm.com>
12b0c632dbSHeiko Carstens  */
13b0c632dbSHeiko Carstens 
14b0c632dbSHeiko Carstens #include <linux/compiler.h>
15b0c632dbSHeiko Carstens #include <linux/err.h>
16b0c632dbSHeiko Carstens #include <linux/fs.h>
17ca872302SChristian Borntraeger #include <linux/hrtimer.h>
18b0c632dbSHeiko Carstens #include <linux/init.h>
19b0c632dbSHeiko Carstens #include <linux/kvm.h>
20b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
21b2d73b2aSMartin Schwidefsky #include <linux/mman.h>
22b0c632dbSHeiko Carstens #include <linux/module.h>
23d3217967SPaul Gortmaker #include <linux/moduleparam.h>
24a374e892STony Krowiak #include <linux/random.h>
25b0c632dbSHeiko Carstens #include <linux/slab.h>
26ba5c1e9bSCarsten Otte #include <linux/timer.h>
2741408c28SThomas Huth #include <linux/vmalloc.h>
2815c9705fSDavid Hildenbrand #include <linux/bitmap.h>
29174cd4b1SIngo Molnar #include <linux/sched/signal.h>
30190df4a2SClaudio Imbrenda #include <linux/string.h>
31174cd4b1SIngo Molnar 
32cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
33b0c632dbSHeiko Carstens #include <asm/lowcore.h>
34fd5ada04SMartin Schwidefsky #include <asm/stp.h>
35b0c632dbSHeiko Carstens #include <asm/pgtable.h>
361e133ab2SMartin Schwidefsky #include <asm/gmap.h>
37f5daba1dSHeiko Carstens #include <asm/nmi.h>
38a0616cdeSDavid Howells #include <asm/switch_to.h>
396d3da241SJens Freimann #include <asm/isc.h>
401526bf9cSChristian Borntraeger #include <asm/sclp.h>
410a763c78SDavid Hildenbrand #include <asm/cpacf.h>
42221bb8a4SLinus Torvalds #include <asm/timex.h>
438f2abe6aSChristian Borntraeger #include "kvm-s390.h"
44b0c632dbSHeiko Carstens #include "gaccess.h"
45b0c632dbSHeiko Carstens 
46ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390"
47ea2cdd27SDavid Hildenbrand #undef pr_fmt
48ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
49ea2cdd27SDavid Hildenbrand 
505786fffaSCornelia Huck #define CREATE_TRACE_POINTS
515786fffaSCornelia Huck #include "trace.h"
52ade38c31SCornelia Huck #include "trace-s390.h"
535786fffaSCornelia Huck 
5441408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
55816c7667SJens Freimann #define LOCAL_IRQS 32
56816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
57816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
5841408c28SThomas Huth 
59b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
60ccc40c53SChristian Borntraeger #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
61b0c632dbSHeiko Carstens 
62b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
63b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
640eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
658f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
668f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
678f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
68a5e0aceaSChristian Borntraeger 	{ "exit_io_request", VCPU_STAT(exit_io_request) },
698f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
70ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
719ec6de19SAlexander Yarygin 	{ "exit_pei", VCPU_STAT(exit_pei) },
72ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
73ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
74a011eeb2SJanosch Frank 	{ "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
75f7819512SPaolo Bonzini 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
7662bea5bfSPaolo Bonzini 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
773491caf2SChristian Borntraeger 	{ "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
78ce2e4f0bSDavid Hildenbrand 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
79f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
80ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
81aba07508SDavid Hildenbrand 	{ "instruction_stctl", VCPU_STAT(instruction_stctl) },
82aba07508SDavid Hildenbrand 	{ "instruction_stctg", VCPU_STAT(instruction_stctg) },
83ccc40c53SChristian Borntraeger 	{ "deliver_ckc", VCPU_STAT(deliver_ckc) },
84ccc40c53SChristian Borntraeger 	{ "deliver_cputm", VCPU_STAT(deliver_cputm) },
85ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
867697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
87ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
88ccc40c53SChristian Borntraeger 	{ "deliver_virtio", VCPU_STAT(deliver_virtio) },
89ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
90ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
91ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
92ccc40c53SChristian Borntraeger 	{ "deliver_program", VCPU_STAT(deliver_program) },
93ccc40c53SChristian Borntraeger 	{ "deliver_io", VCPU_STAT(deliver_io) },
9432de0749SQingFeng Hao 	{ "deliver_machine_check", VCPU_STAT(deliver_machine_check) },
95ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
96ccc40c53SChristian Borntraeger 	{ "inject_ckc", VCPU_STAT(inject_ckc) },
97ccc40c53SChristian Borntraeger 	{ "inject_cputm", VCPU_STAT(inject_cputm) },
98ccc40c53SChristian Borntraeger 	{ "inject_external_call", VCPU_STAT(inject_external_call) },
99ccc40c53SChristian Borntraeger 	{ "inject_float_mchk", VM_STAT(inject_float_mchk) },
100ccc40c53SChristian Borntraeger 	{ "inject_emergency_signal", VCPU_STAT(inject_emergency_signal) },
101ccc40c53SChristian Borntraeger 	{ "inject_io", VM_STAT(inject_io) },
102ccc40c53SChristian Borntraeger 	{ "inject_mchk", VCPU_STAT(inject_mchk) },
103ccc40c53SChristian Borntraeger 	{ "inject_pfault_done", VM_STAT(inject_pfault_done) },
104ccc40c53SChristian Borntraeger 	{ "inject_program", VCPU_STAT(inject_program) },
105ccc40c53SChristian Borntraeger 	{ "inject_restart", VCPU_STAT(inject_restart) },
106ccc40c53SChristian Borntraeger 	{ "inject_service_signal", VM_STAT(inject_service_signal) },
107ccc40c53SChristian Borntraeger 	{ "inject_set_prefix", VCPU_STAT(inject_set_prefix) },
108ccc40c53SChristian Borntraeger 	{ "inject_stop_signal", VCPU_STAT(inject_stop_signal) },
109ccc40c53SChristian Borntraeger 	{ "inject_pfault_init", VCPU_STAT(inject_pfault_init) },
110ccc40c53SChristian Borntraeger 	{ "inject_virtio", VM_STAT(inject_virtio) },
111a37cb07aSChristian Borntraeger 	{ "instruction_epsw", VCPU_STAT(instruction_epsw) },
112a37cb07aSChristian Borntraeger 	{ "instruction_gs", VCPU_STAT(instruction_gs) },
113a37cb07aSChristian Borntraeger 	{ "instruction_io_other", VCPU_STAT(instruction_io_other) },
114a37cb07aSChristian Borntraeger 	{ "instruction_lpsw", VCPU_STAT(instruction_lpsw) },
115a37cb07aSChristian Borntraeger 	{ "instruction_lpswe", VCPU_STAT(instruction_lpswe) },
11669d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
117a37cb07aSChristian Borntraeger 	{ "instruction_ptff", VCPU_STAT(instruction_ptff) },
118453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
119a37cb07aSChristian Borntraeger 	{ "instruction_sck", VCPU_STAT(instruction_sck) },
120a37cb07aSChristian Borntraeger 	{ "instruction_sckpf", VCPU_STAT(instruction_sckpf) },
121453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
122453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
123453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
124a37cb07aSChristian Borntraeger 	{ "instruction_iske", VCPU_STAT(instruction_iske) },
125a37cb07aSChristian Borntraeger 	{ "instruction_ri", VCPU_STAT(instruction_ri) },
126a37cb07aSChristian Borntraeger 	{ "instruction_rrbe", VCPU_STAT(instruction_rrbe) },
127a37cb07aSChristian Borntraeger 	{ "instruction_sske", VCPU_STAT(instruction_sske) },
1288a242234SHeiko Carstens 	{ "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
129b31288faSKonstantin Weitz 	{ "instruction_essa", VCPU_STAT(instruction_essa) },
130453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
131453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
132a37cb07aSChristian Borntraeger 	{ "instruction_tb", VCPU_STAT(instruction_tb) },
133a37cb07aSChristian Borntraeger 	{ "instruction_tpi", VCPU_STAT(instruction_tpi) },
134bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
135a37cb07aSChristian Borntraeger 	{ "instruction_tsch", VCPU_STAT(instruction_tsch) },
13695ca2cb5SJanosch Frank 	{ "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
137a3508fbeSDavid Hildenbrand 	{ "instruction_sie", VCPU_STAT(instruction_sie) },
1385288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
139bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
1407697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
1415288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
14242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
14342cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
1445288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
14542cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
14642cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
147cd7b4b61SEric Farman 	{ "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
1485288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
1495288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
1505288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
15142cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
15242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
15342cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
154866c138cSChristian Borntraeger 	{ "instruction_diag_10", VCPU_STAT(diagnose_10) },
155866c138cSChristian Borntraeger 	{ "instruction_diag_44", VCPU_STAT(diagnose_44) },
156866c138cSChristian Borntraeger 	{ "instruction_diag_9c", VCPU_STAT(diagnose_9c) },
157866c138cSChristian Borntraeger 	{ "instruction_diag_258", VCPU_STAT(diagnose_258) },
158866c138cSChristian Borntraeger 	{ "instruction_diag_308", VCPU_STAT(diagnose_308) },
159866c138cSChristian Borntraeger 	{ "instruction_diag_500", VCPU_STAT(diagnose_500) },
160a37cb07aSChristian Borntraeger 	{ "instruction_diag_other", VCPU_STAT(diagnose_other) },
161b0c632dbSHeiko Carstens 	{ NULL }
162b0c632dbSHeiko Carstens };
163b0c632dbSHeiko Carstens 
1648fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext {
1658fa1696eSCollin L. Walling 	__u8 epoch_idx;
1668fa1696eSCollin L. Walling 	__u64 tod;
1678fa1696eSCollin L. Walling 	__u8 reserved[7];
1688fa1696eSCollin L. Walling } __packed;
1698fa1696eSCollin L. Walling 
170a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
171a411edf1SDavid Hildenbrand static int nested;
172a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
173a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
174a411edf1SDavid Hildenbrand 
175b0c632dbSHeiko Carstens 
176c3b9e3e1SChristian Borntraeger /*
177c3b9e3e1SChristian Borntraeger  * For now we handle at most 16 double words as this is what the s390 base
178c3b9e3e1SChristian Borntraeger  * kernel handles and stores in the prefix page. If we ever need to go beyond
179c3b9e3e1SChristian Borntraeger  * this, this requires changes to code, but the external uapi can stay.
180c3b9e3e1SChristian Borntraeger  */
181c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16
182c3b9e3e1SChristian Borntraeger 
183c3b9e3e1SChristian Borntraeger /*
184c3b9e3e1SChristian Borntraeger  * Base feature mask that defines default mask for facilities. Consists of the
185c3b9e3e1SChristian Borntraeger  * defines in FACILITIES_KVM and the non-hypervisor managed bits.
186c3b9e3e1SChristian Borntraeger  */
187c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
188c3b9e3e1SChristian Borntraeger /*
189c3b9e3e1SChristian Borntraeger  * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
190c3b9e3e1SChristian Borntraeger  * and defines the facilities that can be enabled via a cpu model.
191c3b9e3e1SChristian Borntraeger  */
192c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
193c3b9e3e1SChristian Borntraeger 
194c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void)
19578c4b59fSMichael Mueller {
196c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
197c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
198c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
199c3b9e3e1SChristian Borntraeger 		sizeof(S390_lowcore.stfle_fac_list));
200c3b9e3e1SChristian Borntraeger 
201c3b9e3e1SChristian Borntraeger 	return SIZE_INTERNAL;
20278c4b59fSMichael Mueller }
20378c4b59fSMichael Mueller 
20415c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
20515c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
2060a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
2070a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
20815c9705fSDavid Hildenbrand 
2099d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
210a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
21178f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
2129d8d5786SMichael Mueller 
213b0c632dbSHeiko Carstens /* Section: not file related */
21413a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
215b0c632dbSHeiko Carstens {
216b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
21710474ae8SAlexander Graf 	return 0;
218b0c632dbSHeiko Carstens }
219b0c632dbSHeiko Carstens 
220414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
221414d3b07SMartin Schwidefsky 			      unsigned long end);
2222c70fe44SChristian Borntraeger 
2231575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
2241575767eSDavid Hildenbrand {
2251575767eSDavid Hildenbrand 	u8 delta_idx = 0;
2261575767eSDavid Hildenbrand 
2271575767eSDavid Hildenbrand 	/*
2281575767eSDavid Hildenbrand 	 * The TOD jumps by delta, we have to compensate this by adding
2291575767eSDavid Hildenbrand 	 * -delta to the epoch.
2301575767eSDavid Hildenbrand 	 */
2311575767eSDavid Hildenbrand 	delta = -delta;
2321575767eSDavid Hildenbrand 
2331575767eSDavid Hildenbrand 	/* sign-extension - we're adding to signed values below */
2341575767eSDavid Hildenbrand 	if ((s64)delta < 0)
2351575767eSDavid Hildenbrand 		delta_idx = -1;
2361575767eSDavid Hildenbrand 
2371575767eSDavid Hildenbrand 	scb->epoch += delta;
2381575767eSDavid Hildenbrand 	if (scb->ecd & ECD_MEF) {
2391575767eSDavid Hildenbrand 		scb->epdx += delta_idx;
2401575767eSDavid Hildenbrand 		if (scb->epoch < delta)
2411575767eSDavid Hildenbrand 			scb->epdx += 1;
2421575767eSDavid Hildenbrand 	}
2431575767eSDavid Hildenbrand }
2441575767eSDavid Hildenbrand 
245fdf03650SFan Zhang /*
246fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
247fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
248fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
249fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
250fdf03650SFan Zhang  */
251fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
252fdf03650SFan Zhang 			  void *v)
253fdf03650SFan Zhang {
254fdf03650SFan Zhang 	struct kvm *kvm;
255fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
256fdf03650SFan Zhang 	int i;
257fdf03650SFan Zhang 	unsigned long long *delta = v;
258fdf03650SFan Zhang 
259fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
260fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
2611575767eSDavid Hildenbrand 			kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
2621575767eSDavid Hildenbrand 			if (i == 0) {
2631575767eSDavid Hildenbrand 				kvm->arch.epoch = vcpu->arch.sie_block->epoch;
2641575767eSDavid Hildenbrand 				kvm->arch.epdx = vcpu->arch.sie_block->epdx;
2651575767eSDavid Hildenbrand 			}
266db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
267db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
26891473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
2691575767eSDavid Hildenbrand 				kvm_clock_sync_scb(vcpu->arch.vsie_block,
2701575767eSDavid Hildenbrand 						   *delta);
271fdf03650SFan Zhang 		}
272fdf03650SFan Zhang 	}
273fdf03650SFan Zhang 	return NOTIFY_OK;
274fdf03650SFan Zhang }
275fdf03650SFan Zhang 
276fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
277fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
278fdf03650SFan Zhang };
279fdf03650SFan Zhang 
280b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
281b0c632dbSHeiko Carstens {
2822c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
283b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
284a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
285a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
286fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
287fdf03650SFan Zhang 				       &kvm_clock_notifier);
288b0c632dbSHeiko Carstens 	return 0;
289b0c632dbSHeiko Carstens }
290b0c632dbSHeiko Carstens 
291b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
292b0c632dbSHeiko Carstens {
293b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
294a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
295fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
296fdf03650SFan Zhang 					 &kvm_clock_notifier);
297b0c632dbSHeiko Carstens }
298b0c632dbSHeiko Carstens 
29922be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
30022be5a13SDavid Hildenbrand {
30122be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
30222be5a13SDavid Hildenbrand }
30322be5a13SDavid Hildenbrand 
3040a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
3050a763c78SDavid Hildenbrand {
3060a763c78SDavid Hildenbrand 	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
307d051ae53SHeiko Carstens 	int cc;
3080a763c78SDavid Hildenbrand 
3090a763c78SDavid Hildenbrand 	asm volatile(
3100a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
3110a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
3120a763c78SDavid Hildenbrand 		"	ipm	%0\n"
3130a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
3140a763c78SDavid Hildenbrand 		: "=d" (cc)
3150a763c78SDavid Hildenbrand 		: "d" (r0)
3160a763c78SDavid Hildenbrand 		: "cc");
3170a763c78SDavid Hildenbrand 	return cc == 0;
3180a763c78SDavid Hildenbrand }
3190a763c78SDavid Hildenbrand 
32022be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
32122be5a13SDavid Hildenbrand {
3220a763c78SDavid Hildenbrand 	int i;
3230a763c78SDavid Hildenbrand 
3240a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
3250a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
3260a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
3270a763c78SDavid Hildenbrand 	}
3280a763c78SDavid Hildenbrand 
3290a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
330221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
331221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
332221bb8a4SLinus Torvalds 		     PTFF_QAF);
3330a763c78SDavid Hildenbrand 
3340a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
33569c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
33669c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
33769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
33869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
33969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
34069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
34169c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
34269c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
34369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
34469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
3450a763c78SDavid Hildenbrand 	}
3460a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
34769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
34869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
3490a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
35069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
35169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
35269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
35369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
35469c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
35569c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
35669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
35769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
3580a763c78SDavid Hildenbrand 	}
3590a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
360985a9d20SHarald Freudenberger 		__cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
36169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
3620a763c78SDavid Hildenbrand 
363e000b8e0SJason J. Herne 	if (test_facility(146)) /* MSA8 */
364e000b8e0SJason J. Herne 		__cpacf_query(CPACF_KMA, (cpacf_mask_t *)
365e000b8e0SJason J. Herne 			      kvm_s390_available_subfunc.kma);
366e000b8e0SJason J. Herne 
36722be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
36822be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
369a3508fbeSDavid Hildenbrand 	/*
370a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
371a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
372a3508fbeSDavid Hildenbrand 	 */
373a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
374a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
375a3508fbeSDavid Hildenbrand 		return;
376a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
37719c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
37819c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
3790615a326SDavid Hildenbrand 	if (sclp.has_siif)
3800615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
38177d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
38277d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
383a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
384a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
3855630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
3865630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
38713ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
38813ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
3897fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
3907fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
391730cd632SFarhan Ali 	if (sclp.has_kss)
392730cd632SFarhan Ali 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
3935d3876a8SDavid Hildenbrand 	/*
3945d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
3955d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
3965d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
3975d3876a8SDavid Hildenbrand 	 *
3985d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
3995d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
4005d3876a8SDavid Hildenbrand 	 *
4015d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
4025d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
4035d3876a8SDavid Hildenbrand 	 *
4045d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
4055d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
4065d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
4075d3876a8SDavid Hildenbrand 	 *
4085d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
4095d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
4105d3876a8SDavid Hildenbrand 	 */
41122be5a13SDavid Hildenbrand }
41222be5a13SDavid Hildenbrand 
413b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
414b0c632dbSHeiko Carstens {
41578f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
41678f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
41778f26131SChristian Borntraeger 		return -ENOMEM;
41878f26131SChristian Borntraeger 
41978f26131SChristian Borntraeger 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
42078f26131SChristian Borntraeger 		debug_unregister(kvm_s390_dbf);
42178f26131SChristian Borntraeger 		return -ENOMEM;
42278f26131SChristian Borntraeger 	}
42378f26131SChristian Borntraeger 
42422be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
42522be5a13SDavid Hildenbrand 
42684877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
42784877d93SCornelia Huck 	return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
428b0c632dbSHeiko Carstens }
429b0c632dbSHeiko Carstens 
43078f26131SChristian Borntraeger void kvm_arch_exit(void)
43178f26131SChristian Borntraeger {
43278f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
43378f26131SChristian Borntraeger }
43478f26131SChristian Borntraeger 
435b0c632dbSHeiko Carstens /* Section: device related */
436b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
437b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
438b0c632dbSHeiko Carstens {
439b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
440b0c632dbSHeiko Carstens 		return s390_enable_sie();
441b0c632dbSHeiko Carstens 	return -EINVAL;
442b0c632dbSHeiko Carstens }
443b0c632dbSHeiko Carstens 
444784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
445b0c632dbSHeiko Carstens {
446d7b0b5ebSCarsten Otte 	int r;
447d7b0b5ebSCarsten Otte 
4482bd0ac4eSCarsten Otte 	switch (ext) {
449d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
450b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
45152e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
4521efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
4531efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
4541efd0f59SCarsten Otte #endif
4553c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
45660b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
45714eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
458d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
459fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
46010ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
461c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
462d938dc55SCornelia Huck 	case KVM_CAP_ENABLE_CAP_VM:
46378599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
464f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
4656352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
466460df4c1SPaolo Bonzini 	case KVM_CAP_IMMEDIATE_EXIT:
46747b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
4682444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
469e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
47030ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
471816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
4726502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
4734036e387SClaudio Imbrenda 	case KVM_CAP_S390_CMMA_MIGRATION:
47447a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
475da9a1446SChristian Borntraeger 	case KVM_CAP_S390_AIS_MIGRATION:
476d7b0b5ebSCarsten Otte 		r = 1;
477d7b0b5ebSCarsten Otte 		break;
47841408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
47941408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
48041408c28SThomas Huth 		break;
481e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
482e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
48376a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
484a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
485a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
486a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
48776a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
488e726b1bdSChristian Borntraeger 		break;
489e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
490e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
491e1e2e605SNick Wang 		break;
4921526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
493abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
4941526bf9cSChristian Borntraeger 		break;
49568c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
49668c55750SEric Farman 		r = MACHINE_HAS_VX;
49768c55750SEric Farman 		break;
498c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
499c6e5f166SFan Zhang 		r = test_facility(64);
500c6e5f166SFan Zhang 		break;
5014e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
5024e0b1ab7SFan Zhang 		r = test_facility(133);
5034e0b1ab7SFan Zhang 		break;
50435b3fde6SChristian Borntraeger 	case KVM_CAP_S390_BPB:
50535b3fde6SChristian Borntraeger 		r = test_facility(82);
50635b3fde6SChristian Borntraeger 		break;
5072bd0ac4eSCarsten Otte 	default:
508d7b0b5ebSCarsten Otte 		r = 0;
509b0c632dbSHeiko Carstens 	}
510d7b0b5ebSCarsten Otte 	return r;
5112bd0ac4eSCarsten Otte }
512b0c632dbSHeiko Carstens 
51315f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm,
51415f36ebdSJason J. Herne 					struct kvm_memory_slot *memslot)
51515f36ebdSJason J. Herne {
51615f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
51715f36ebdSJason J. Herne 	unsigned long address;
51815f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
51915f36ebdSJason J. Herne 
52015f36ebdSJason J. Herne 	/* Loop over all guest pages */
52115f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
52215f36ebdSJason J. Herne 	for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
52315f36ebdSJason J. Herne 		address = gfn_to_hva_memslot(memslot, cur_gfn);
52415f36ebdSJason J. Herne 
5251e133ab2SMartin Schwidefsky 		if (test_and_clear_guest_dirty(gmap->mm, address))
52615f36ebdSJason J. Herne 			mark_page_dirty(kvm, cur_gfn);
5271763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
5281763f8d0SChristian Borntraeger 			return;
52970c88a00SChristian Borntraeger 		cond_resched();
53015f36ebdSJason J. Herne 	}
53115f36ebdSJason J. Herne }
53215f36ebdSJason J. Herne 
533b0c632dbSHeiko Carstens /* Section: vm related */
534a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
535a6e2f683SEugene (jno) Dvurechenski 
536b0c632dbSHeiko Carstens /*
537b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
538b0c632dbSHeiko Carstens  */
539b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
540b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
541b0c632dbSHeiko Carstens {
54215f36ebdSJason J. Herne 	int r;
54315f36ebdSJason J. Herne 	unsigned long n;
5449f6b8029SPaolo Bonzini 	struct kvm_memslots *slots;
54515f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
54615f36ebdSJason J. Herne 	int is_dirty = 0;
54715f36ebdSJason J. Herne 
548e1e8a962SJanosch Frank 	if (kvm_is_ucontrol(kvm))
549e1e8a962SJanosch Frank 		return -EINVAL;
550e1e8a962SJanosch Frank 
55115f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
55215f36ebdSJason J. Herne 
55315f36ebdSJason J. Herne 	r = -EINVAL;
55415f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
55515f36ebdSJason J. Herne 		goto out;
55615f36ebdSJason J. Herne 
5579f6b8029SPaolo Bonzini 	slots = kvm_memslots(kvm);
5589f6b8029SPaolo Bonzini 	memslot = id_to_memslot(slots, log->slot);
55915f36ebdSJason J. Herne 	r = -ENOENT;
56015f36ebdSJason J. Herne 	if (!memslot->dirty_bitmap)
56115f36ebdSJason J. Herne 		goto out;
56215f36ebdSJason J. Herne 
56315f36ebdSJason J. Herne 	kvm_s390_sync_dirty_log(kvm, memslot);
56415f36ebdSJason J. Herne 	r = kvm_get_dirty_log(kvm, log, &is_dirty);
56515f36ebdSJason J. Herne 	if (r)
56615f36ebdSJason J. Herne 		goto out;
56715f36ebdSJason J. Herne 
56815f36ebdSJason J. Herne 	/* Clear the dirty log */
56915f36ebdSJason J. Herne 	if (is_dirty) {
57015f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
57115f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
57215f36ebdSJason J. Herne 	}
57315f36ebdSJason J. Herne 	r = 0;
57415f36ebdSJason J. Herne out:
57515f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
57615f36ebdSJason J. Herne 	return r;
577b0c632dbSHeiko Carstens }
578b0c632dbSHeiko Carstens 
5796502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
5806502a34cSDavid Hildenbrand {
5816502a34cSDavid Hildenbrand 	unsigned int i;
5826502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
5836502a34cSDavid Hildenbrand 
5846502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
5856502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
5866502a34cSDavid Hildenbrand 	}
5876502a34cSDavid Hildenbrand }
5886502a34cSDavid Hildenbrand 
589d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
590d938dc55SCornelia Huck {
591d938dc55SCornelia Huck 	int r;
592d938dc55SCornelia Huck 
593d938dc55SCornelia Huck 	if (cap->flags)
594d938dc55SCornelia Huck 		return -EINVAL;
595d938dc55SCornelia Huck 
596d938dc55SCornelia Huck 	switch (cap->cap) {
59784223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
598c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
59984223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
60084223598SCornelia Huck 		r = 0;
60184223598SCornelia Huck 		break;
6022444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
603c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
6042444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
6052444b352SDavid Hildenbrand 		r = 0;
6062444b352SDavid Hildenbrand 		break;
60768c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
6085967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
609a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
6105967c17bSDavid Hildenbrand 			r = -EBUSY;
6115967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
612c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
613c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
6142f87d942SGuenther Hutzl 			if (test_facility(134)) {
6152f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
6162f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_list, 134);
6172f87d942SGuenther Hutzl 			}
61853743aa7SMaxim Samoylov 			if (test_facility(135)) {
61953743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
62053743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_list, 135);
62153743aa7SMaxim Samoylov 			}
62218280d8bSMichael Mueller 			r = 0;
62318280d8bSMichael Mueller 		} else
62418280d8bSMichael Mueller 			r = -EINVAL;
6255967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
626c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
627c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
62868c55750SEric Farman 		break;
629c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
630c6e5f166SFan Zhang 		r = -EINVAL;
631c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
632a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
633c6e5f166SFan Zhang 			r = -EBUSY;
634c6e5f166SFan Zhang 		} else if (test_facility(64)) {
635c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
636c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
637c6e5f166SFan Zhang 			r = 0;
638c6e5f166SFan Zhang 		}
639c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
640c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
641c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
642c6e5f166SFan Zhang 		break;
64347a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
64447a4693eSYi Min Zhao 		mutex_lock(&kvm->lock);
64547a4693eSYi Min Zhao 		if (kvm->created_vcpus) {
64647a4693eSYi Min Zhao 			r = -EBUSY;
64747a4693eSYi Min Zhao 		} else {
64847a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_mask, 72);
64947a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_list, 72);
65047a4693eSYi Min Zhao 			r = 0;
65147a4693eSYi Min Zhao 		}
65247a4693eSYi Min Zhao 		mutex_unlock(&kvm->lock);
65347a4693eSYi Min Zhao 		VM_EVENT(kvm, 3, "ENABLE: AIS %s",
65447a4693eSYi Min Zhao 			 r ? "(not available)" : "(success)");
65547a4693eSYi Min Zhao 		break;
6564e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
6574e0b1ab7SFan Zhang 		r = -EINVAL;
6584e0b1ab7SFan Zhang 		mutex_lock(&kvm->lock);
659241e3ec0SChristian Borntraeger 		if (kvm->created_vcpus) {
6604e0b1ab7SFan Zhang 			r = -EBUSY;
6614e0b1ab7SFan Zhang 		} else if (test_facility(133)) {
6624e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_mask, 133);
6634e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_list, 133);
6644e0b1ab7SFan Zhang 			r = 0;
6654e0b1ab7SFan Zhang 		}
6664e0b1ab7SFan Zhang 		mutex_unlock(&kvm->lock);
6674e0b1ab7SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
6684e0b1ab7SFan Zhang 			 r ? "(not available)" : "(success)");
6694e0b1ab7SFan Zhang 		break;
670e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
671c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
672e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
673e44fc8c9SEkaterina Tumanova 		r = 0;
674e44fc8c9SEkaterina Tumanova 		break;
6756502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
6766502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
6776502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
6786502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
6796502a34cSDavid Hildenbrand 		r = 0;
6806502a34cSDavid Hildenbrand 		break;
681d938dc55SCornelia Huck 	default:
682d938dc55SCornelia Huck 		r = -EINVAL;
683d938dc55SCornelia Huck 		break;
684d938dc55SCornelia Huck 	}
685d938dc55SCornelia Huck 	return r;
686d938dc55SCornelia Huck }
687d938dc55SCornelia Huck 
6888c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
6898c0a7ce6SDominik Dingel {
6908c0a7ce6SDominik Dingel 	int ret;
6918c0a7ce6SDominik Dingel 
6928c0a7ce6SDominik Dingel 	switch (attr->attr) {
6938c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
6948c0a7ce6SDominik Dingel 		ret = 0;
695c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
696a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
697a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
6988c0a7ce6SDominik Dingel 			ret = -EFAULT;
6998c0a7ce6SDominik Dingel 		break;
7008c0a7ce6SDominik Dingel 	default:
7018c0a7ce6SDominik Dingel 		ret = -ENXIO;
7028c0a7ce6SDominik Dingel 		break;
7038c0a7ce6SDominik Dingel 	}
7048c0a7ce6SDominik Dingel 	return ret;
7058c0a7ce6SDominik Dingel }
7068c0a7ce6SDominik Dingel 
7078c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
7084f718eabSDominik Dingel {
7094f718eabSDominik Dingel 	int ret;
7104f718eabSDominik Dingel 	unsigned int idx;
7114f718eabSDominik Dingel 	switch (attr->attr) {
7124f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
713f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
714c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
715e6db1d61SDominik Dingel 			break;
716e6db1d61SDominik Dingel 
7174f718eabSDominik Dingel 		ret = -EBUSY;
718c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
7194f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
720a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
7214f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
722c9f0a2b8SJanosch Frank 			/* Not compatible with cmma. */
723c9f0a2b8SJanosch Frank 			kvm->arch.use_pfmfi = 0;
7244f718eabSDominik Dingel 			ret = 0;
7254f718eabSDominik Dingel 		}
7264f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
7274f718eabSDominik Dingel 		break;
7284f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
729f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
730f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
731f9cbd9b0SDavid Hildenbrand 			break;
732c3489155SDominik Dingel 		ret = -EINVAL;
733c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
734c3489155SDominik Dingel 			break;
735c3489155SDominik Dingel 
736c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
7374f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
7384f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
739a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
7404f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
7414f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
7424f718eabSDominik Dingel 		ret = 0;
7434f718eabSDominik Dingel 		break;
7448c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
7458c0a7ce6SDominik Dingel 		unsigned long new_limit;
7468c0a7ce6SDominik Dingel 
7478c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
7488c0a7ce6SDominik Dingel 			return -EINVAL;
7498c0a7ce6SDominik Dingel 
7508c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
7518c0a7ce6SDominik Dingel 			return -EFAULT;
7528c0a7ce6SDominik Dingel 
753a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
754a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
7558c0a7ce6SDominik Dingel 			return -E2BIG;
7568c0a7ce6SDominik Dingel 
757a3a92c31SDominik Dingel 		if (!new_limit)
758a3a92c31SDominik Dingel 			return -EINVAL;
759a3a92c31SDominik Dingel 
7606ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
761a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
762a3a92c31SDominik Dingel 			new_limit -= 1;
763a3a92c31SDominik Dingel 
7648c0a7ce6SDominik Dingel 		ret = -EBUSY;
7658c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
766a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
7676ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
7686ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
7698c0a7ce6SDominik Dingel 
7708c0a7ce6SDominik Dingel 			if (!new) {
7718c0a7ce6SDominik Dingel 				ret = -ENOMEM;
7728c0a7ce6SDominik Dingel 			} else {
7736ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
7748c0a7ce6SDominik Dingel 				new->private = kvm;
7758c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
7768c0a7ce6SDominik Dingel 				ret = 0;
7778c0a7ce6SDominik Dingel 			}
7788c0a7ce6SDominik Dingel 		}
7798c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
780a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
781a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
782a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
7838c0a7ce6SDominik Dingel 		break;
7848c0a7ce6SDominik Dingel 	}
7854f718eabSDominik Dingel 	default:
7864f718eabSDominik Dingel 		ret = -ENXIO;
7874f718eabSDominik Dingel 		break;
7884f718eabSDominik Dingel 	}
7894f718eabSDominik Dingel 	return ret;
7904f718eabSDominik Dingel }
7914f718eabSDominik Dingel 
792a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
793a374e892STony Krowiak 
794*20c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
795a374e892STony Krowiak {
796a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
797a374e892STony Krowiak 	int i;
798a374e892STony Krowiak 
799*20c922f0STony Krowiak 	kvm_s390_vcpu_block_all(kvm);
800*20c922f0STony Krowiak 
801*20c922f0STony Krowiak 	kvm_for_each_vcpu(i, vcpu, kvm)
802*20c922f0STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
803*20c922f0STony Krowiak 
804*20c922f0STony Krowiak 	kvm_s390_vcpu_unblock_all(kvm);
805*20c922f0STony Krowiak }
806*20c922f0STony Krowiak 
807*20c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
808*20c922f0STony Krowiak {
8099d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
810a374e892STony Krowiak 		return -EINVAL;
811a374e892STony Krowiak 
812a374e892STony Krowiak 	mutex_lock(&kvm->lock);
813a374e892STony Krowiak 	switch (attr->attr) {
814a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
815a374e892STony Krowiak 		get_random_bytes(
816a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
817a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
818a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
819c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
820a374e892STony Krowiak 		break;
821a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
822a374e892STony Krowiak 		get_random_bytes(
823a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
824a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
825a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
826c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
827a374e892STony Krowiak 		break;
828a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
829a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
830a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
831a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
832c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
833a374e892STony Krowiak 		break;
834a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
835a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
836a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
837a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
838c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
839a374e892STony Krowiak 		break;
840a374e892STony Krowiak 	default:
841a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
842a374e892STony Krowiak 		return -ENXIO;
843a374e892STony Krowiak 	}
844a374e892STony Krowiak 
845*20c922f0STony Krowiak 	kvm_s390_vcpu_crypto_reset_all(kvm);
846a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
847a374e892STony Krowiak 	return 0;
848a374e892STony Krowiak }
849a374e892STony Krowiak 
850190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
851190df4a2SClaudio Imbrenda {
852190df4a2SClaudio Imbrenda 	int cx;
853190df4a2SClaudio Imbrenda 	struct kvm_vcpu *vcpu;
854190df4a2SClaudio Imbrenda 
855190df4a2SClaudio Imbrenda 	kvm_for_each_vcpu(cx, vcpu, kvm)
856190df4a2SClaudio Imbrenda 		kvm_s390_sync_request(req, vcpu);
857190df4a2SClaudio Imbrenda }
858190df4a2SClaudio Imbrenda 
859190df4a2SClaudio Imbrenda /*
860190df4a2SClaudio Imbrenda  * Must be called with kvm->srcu held to avoid races on memslots, and with
8611de1ea7eSChristian Borntraeger  * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
862190df4a2SClaudio Imbrenda  */
863190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm)
864190df4a2SClaudio Imbrenda {
865190df4a2SClaudio Imbrenda 	struct kvm_s390_migration_state *mgs;
866190df4a2SClaudio Imbrenda 	struct kvm_memory_slot *ms;
867190df4a2SClaudio Imbrenda 	/* should be the only one */
868190df4a2SClaudio Imbrenda 	struct kvm_memslots *slots;
869190df4a2SClaudio Imbrenda 	unsigned long ram_pages;
870190df4a2SClaudio Imbrenda 	int slotnr;
871190df4a2SClaudio Imbrenda 
872190df4a2SClaudio Imbrenda 	/* migration mode already enabled */
873190df4a2SClaudio Imbrenda 	if (kvm->arch.migration_state)
874190df4a2SClaudio Imbrenda 		return 0;
875190df4a2SClaudio Imbrenda 
876190df4a2SClaudio Imbrenda 	slots = kvm_memslots(kvm);
877190df4a2SClaudio Imbrenda 	if (!slots || !slots->used_slots)
878190df4a2SClaudio Imbrenda 		return -EINVAL;
879190df4a2SClaudio Imbrenda 
880190df4a2SClaudio Imbrenda 	mgs = kzalloc(sizeof(*mgs), GFP_KERNEL);
881190df4a2SClaudio Imbrenda 	if (!mgs)
882190df4a2SClaudio Imbrenda 		return -ENOMEM;
883190df4a2SClaudio Imbrenda 	kvm->arch.migration_state = mgs;
884190df4a2SClaudio Imbrenda 
885190df4a2SClaudio Imbrenda 	if (kvm->arch.use_cmma) {
886190df4a2SClaudio Imbrenda 		/*
88732aa144fSChristian Borntraeger 		 * Get the first slot. They are reverse sorted by base_gfn, so
88832aa144fSChristian Borntraeger 		 * the first slot is also the one at the end of the address
88932aa144fSChristian Borntraeger 		 * space. We have verified above that at least one slot is
89032aa144fSChristian Borntraeger 		 * present.
891190df4a2SClaudio Imbrenda 		 */
89232aa144fSChristian Borntraeger 		ms = slots->memslots;
893190df4a2SClaudio Imbrenda 		/* round up so we only use full longs */
894190df4a2SClaudio Imbrenda 		ram_pages = roundup(ms->base_gfn + ms->npages, BITS_PER_LONG);
895190df4a2SClaudio Imbrenda 		/* allocate enough bytes to store all the bits */
896190df4a2SClaudio Imbrenda 		mgs->pgste_bitmap = vmalloc(ram_pages / 8);
897190df4a2SClaudio Imbrenda 		if (!mgs->pgste_bitmap) {
898190df4a2SClaudio Imbrenda 			kfree(mgs);
899190df4a2SClaudio Imbrenda 			kvm->arch.migration_state = NULL;
900190df4a2SClaudio Imbrenda 			return -ENOMEM;
901190df4a2SClaudio Imbrenda 		}
902190df4a2SClaudio Imbrenda 
903190df4a2SClaudio Imbrenda 		mgs->bitmap_size = ram_pages;
904190df4a2SClaudio Imbrenda 		atomic64_set(&mgs->dirty_pages, ram_pages);
905190df4a2SClaudio Imbrenda 		/* mark all the pages in active slots as dirty */
906190df4a2SClaudio Imbrenda 		for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
907190df4a2SClaudio Imbrenda 			ms = slots->memslots + slotnr;
908190df4a2SClaudio Imbrenda 			bitmap_set(mgs->pgste_bitmap, ms->base_gfn, ms->npages);
909190df4a2SClaudio Imbrenda 		}
910190df4a2SClaudio Imbrenda 
911190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
912190df4a2SClaudio Imbrenda 	}
913190df4a2SClaudio Imbrenda 	return 0;
914190df4a2SClaudio Imbrenda }
915190df4a2SClaudio Imbrenda 
916190df4a2SClaudio Imbrenda /*
9171de1ea7eSChristian Borntraeger  * Must be called with kvm->slots_lock to avoid races with ourselves and
918190df4a2SClaudio Imbrenda  * kvm_s390_vm_start_migration.
919190df4a2SClaudio Imbrenda  */
920190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm)
921190df4a2SClaudio Imbrenda {
922190df4a2SClaudio Imbrenda 	struct kvm_s390_migration_state *mgs;
923190df4a2SClaudio Imbrenda 
924190df4a2SClaudio Imbrenda 	/* migration mode already disabled */
925190df4a2SClaudio Imbrenda 	if (!kvm->arch.migration_state)
926190df4a2SClaudio Imbrenda 		return 0;
927190df4a2SClaudio Imbrenda 	mgs = kvm->arch.migration_state;
928190df4a2SClaudio Imbrenda 	kvm->arch.migration_state = NULL;
929190df4a2SClaudio Imbrenda 
930190df4a2SClaudio Imbrenda 	if (kvm->arch.use_cmma) {
931190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
9321de1ea7eSChristian Borntraeger 		/* We have to wait for the essa emulation to finish */
9331de1ea7eSChristian Borntraeger 		synchronize_srcu(&kvm->srcu);
934190df4a2SClaudio Imbrenda 		vfree(mgs->pgste_bitmap);
935190df4a2SClaudio Imbrenda 	}
936190df4a2SClaudio Imbrenda 	kfree(mgs);
937190df4a2SClaudio Imbrenda 	return 0;
938190df4a2SClaudio Imbrenda }
939190df4a2SClaudio Imbrenda 
940190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm,
941190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
942190df4a2SClaudio Imbrenda {
9431de1ea7eSChristian Borntraeger 	int res = -ENXIO;
944190df4a2SClaudio Imbrenda 
9451de1ea7eSChristian Borntraeger 	mutex_lock(&kvm->slots_lock);
946190df4a2SClaudio Imbrenda 	switch (attr->attr) {
947190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_START:
948190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_start_migration(kvm);
949190df4a2SClaudio Imbrenda 		break;
950190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_STOP:
951190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_stop_migration(kvm);
952190df4a2SClaudio Imbrenda 		break;
953190df4a2SClaudio Imbrenda 	default:
954190df4a2SClaudio Imbrenda 		break;
955190df4a2SClaudio Imbrenda 	}
9561de1ea7eSChristian Borntraeger 	mutex_unlock(&kvm->slots_lock);
957190df4a2SClaudio Imbrenda 
958190df4a2SClaudio Imbrenda 	return res;
959190df4a2SClaudio Imbrenda }
960190df4a2SClaudio Imbrenda 
961190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm,
962190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
963190df4a2SClaudio Imbrenda {
964190df4a2SClaudio Imbrenda 	u64 mig = (kvm->arch.migration_state != NULL);
965190df4a2SClaudio Imbrenda 
966190df4a2SClaudio Imbrenda 	if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
967190df4a2SClaudio Imbrenda 		return -ENXIO;
968190df4a2SClaudio Imbrenda 
969190df4a2SClaudio Imbrenda 	if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
970190df4a2SClaudio Imbrenda 		return -EFAULT;
971190df4a2SClaudio Imbrenda 	return 0;
972190df4a2SClaudio Imbrenda }
973190df4a2SClaudio Imbrenda 
9748fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
9758fa1696eSCollin L. Walling {
9768fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
9778fa1696eSCollin L. Walling 
9788fa1696eSCollin L. Walling 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
9798fa1696eSCollin L. Walling 		return -EFAULT;
9808fa1696eSCollin L. Walling 
9810e7def5fSDavid Hildenbrand 	if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
9828fa1696eSCollin L. Walling 		return -EINVAL;
9830e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
9848fa1696eSCollin L. Walling 
9858fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
9868fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
9878fa1696eSCollin L. Walling 
9888fa1696eSCollin L. Walling 	return 0;
9898fa1696eSCollin L. Walling }
9908fa1696eSCollin L. Walling 
99172f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
99272f25020SJason J. Herne {
99372f25020SJason J. Herne 	u8 gtod_high;
99472f25020SJason J. Herne 
99572f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
99672f25020SJason J. Herne 					   sizeof(gtod_high)))
99772f25020SJason J. Herne 		return -EFAULT;
99872f25020SJason J. Herne 
99972f25020SJason J. Herne 	if (gtod_high != 0)
100072f25020SJason J. Herne 		return -EINVAL;
100158c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
100272f25020SJason J. Herne 
100372f25020SJason J. Herne 	return 0;
100472f25020SJason J. Herne }
100572f25020SJason J. Herne 
100672f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
100772f25020SJason J. Herne {
10080e7def5fSDavid Hildenbrand 	struct kvm_s390_vm_tod_clock gtod = { 0 };
100972f25020SJason J. Herne 
10100e7def5fSDavid Hildenbrand 	if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
10110e7def5fSDavid Hildenbrand 			   sizeof(gtod.tod)))
101272f25020SJason J. Herne 		return -EFAULT;
101372f25020SJason J. Herne 
10140e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
10150e7def5fSDavid Hildenbrand 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
101672f25020SJason J. Herne 	return 0;
101772f25020SJason J. Herne }
101872f25020SJason J. Herne 
101972f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
102072f25020SJason J. Herne {
102172f25020SJason J. Herne 	int ret;
102272f25020SJason J. Herne 
102372f25020SJason J. Herne 	if (attr->flags)
102472f25020SJason J. Herne 		return -EINVAL;
102572f25020SJason J. Herne 
102672f25020SJason J. Herne 	switch (attr->attr) {
10278fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
10288fa1696eSCollin L. Walling 		ret = kvm_s390_set_tod_ext(kvm, attr);
10298fa1696eSCollin L. Walling 		break;
103072f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
103172f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
103272f25020SJason J. Herne 		break;
103372f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
103472f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
103572f25020SJason J. Herne 		break;
103672f25020SJason J. Herne 	default:
103772f25020SJason J. Herne 		ret = -ENXIO;
103872f25020SJason J. Herne 		break;
103972f25020SJason J. Herne 	}
104072f25020SJason J. Herne 	return ret;
104172f25020SJason J. Herne }
104272f25020SJason J. Herne 
10438fa1696eSCollin L. Walling static void kvm_s390_get_tod_clock_ext(struct kvm *kvm,
10448fa1696eSCollin L. Walling 					struct kvm_s390_vm_tod_clock *gtod)
10458fa1696eSCollin L. Walling {
10468fa1696eSCollin L. Walling 	struct kvm_s390_tod_clock_ext htod;
10478fa1696eSCollin L. Walling 
10488fa1696eSCollin L. Walling 	preempt_disable();
10498fa1696eSCollin L. Walling 
10508fa1696eSCollin L. Walling 	get_tod_clock_ext((char *)&htod);
10518fa1696eSCollin L. Walling 
10528fa1696eSCollin L. Walling 	gtod->tod = htod.tod + kvm->arch.epoch;
10538fa1696eSCollin L. Walling 	gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx;
10548fa1696eSCollin L. Walling 
10558fa1696eSCollin L. Walling 	if (gtod->tod < htod.tod)
10568fa1696eSCollin L. Walling 		gtod->epoch_idx += 1;
10578fa1696eSCollin L. Walling 
10588fa1696eSCollin L. Walling 	preempt_enable();
10598fa1696eSCollin L. Walling }
10608fa1696eSCollin L. Walling 
10618fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
10628fa1696eSCollin L. Walling {
10638fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
10648fa1696eSCollin L. Walling 
10658fa1696eSCollin L. Walling 	memset(&gtod, 0, sizeof(gtod));
10668fa1696eSCollin L. Walling 
10678fa1696eSCollin L. Walling 	if (test_kvm_facility(kvm, 139))
10688fa1696eSCollin L. Walling 		kvm_s390_get_tod_clock_ext(kvm, &gtod);
10698fa1696eSCollin L. Walling 	else
10708fa1696eSCollin L. Walling 		gtod.tod = kvm_s390_get_tod_clock_fast(kvm);
10718fa1696eSCollin L. Walling 
10728fa1696eSCollin L. Walling 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
10738fa1696eSCollin L. Walling 		return -EFAULT;
10748fa1696eSCollin L. Walling 
10758fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
10768fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
10778fa1696eSCollin L. Walling 	return 0;
10788fa1696eSCollin L. Walling }
10798fa1696eSCollin L. Walling 
108072f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
108172f25020SJason J. Herne {
108272f25020SJason J. Herne 	u8 gtod_high = 0;
108372f25020SJason J. Herne 
108472f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
108572f25020SJason J. Herne 					 sizeof(gtod_high)))
108672f25020SJason J. Herne 		return -EFAULT;
108758c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
108872f25020SJason J. Herne 
108972f25020SJason J. Herne 	return 0;
109072f25020SJason J. Herne }
109172f25020SJason J. Herne 
109272f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
109372f25020SJason J. Herne {
10945a3d883aSDavid Hildenbrand 	u64 gtod;
109572f25020SJason J. Herne 
109660417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
109772f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
109872f25020SJason J. Herne 		return -EFAULT;
109958c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
110072f25020SJason J. Herne 
110172f25020SJason J. Herne 	return 0;
110272f25020SJason J. Herne }
110372f25020SJason J. Herne 
110472f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
110572f25020SJason J. Herne {
110672f25020SJason J. Herne 	int ret;
110772f25020SJason J. Herne 
110872f25020SJason J. Herne 	if (attr->flags)
110972f25020SJason J. Herne 		return -EINVAL;
111072f25020SJason J. Herne 
111172f25020SJason J. Herne 	switch (attr->attr) {
11128fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
11138fa1696eSCollin L. Walling 		ret = kvm_s390_get_tod_ext(kvm, attr);
11148fa1696eSCollin L. Walling 		break;
111572f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
111672f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
111772f25020SJason J. Herne 		break;
111872f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
111972f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
112072f25020SJason J. Herne 		break;
112172f25020SJason J. Herne 	default:
112272f25020SJason J. Herne 		ret = -ENXIO;
112372f25020SJason J. Herne 		break;
112472f25020SJason J. Herne 	}
112572f25020SJason J. Herne 	return ret;
112672f25020SJason J. Herne }
112772f25020SJason J. Herne 
1128658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1129658b6edaSMichael Mueller {
1130658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1131053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
1132658b6edaSMichael Mueller 	int ret = 0;
1133658b6edaSMichael Mueller 
1134658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
1135a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
1136658b6edaSMichael Mueller 		ret = -EBUSY;
1137658b6edaSMichael Mueller 		goto out;
1138658b6edaSMichael Mueller 	}
1139658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1140658b6edaSMichael Mueller 	if (!proc) {
1141658b6edaSMichael Mueller 		ret = -ENOMEM;
1142658b6edaSMichael Mueller 		goto out;
1143658b6edaSMichael Mueller 	}
1144658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
1145658b6edaSMichael Mueller 			    sizeof(*proc))) {
11469bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
1147053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
1148053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
11490487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
1150053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
1151053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
1152053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
1153053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
1154053dd230SDavid Hildenbrand 			else
1155658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
1156053dd230SDavid Hildenbrand 		}
1157c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
1158658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
1159a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1160a8c39dd7SChristian Borntraeger 			 kvm->arch.model.ibc,
1161a8c39dd7SChristian Borntraeger 			 kvm->arch.model.cpuid);
1162a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1163a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[0],
1164a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[1],
1165a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[2]);
1166658b6edaSMichael Mueller 	} else
1167658b6edaSMichael Mueller 		ret = -EFAULT;
1168658b6edaSMichael Mueller 	kfree(proc);
1169658b6edaSMichael Mueller out:
1170658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
1171658b6edaSMichael Mueller 	return ret;
1172658b6edaSMichael Mueller }
1173658b6edaSMichael Mueller 
117415c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
117515c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
117615c9705fSDavid Hildenbrand {
117715c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
117815c9705fSDavid Hildenbrand 
117915c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
118015c9705fSDavid Hildenbrand 		return -EFAULT;
118115c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
118215c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
118315c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
118415c9705fSDavid Hildenbrand 		return -EINVAL;
118515c9705fSDavid Hildenbrand 
118615c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
11872f8311c9SChristian Borntraeger 	if (kvm->created_vcpus) {
11882f8311c9SChristian Borntraeger 		mutex_unlock(&kvm->lock);
11892f8311c9SChristian Borntraeger 		return -EBUSY;
11902f8311c9SChristian Borntraeger 	}
119115c9705fSDavid Hildenbrand 	bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
119215c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
119315c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
11942f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
11952f8311c9SChristian Borntraeger 			 data.feat[0],
11962f8311c9SChristian Borntraeger 			 data.feat[1],
11972f8311c9SChristian Borntraeger 			 data.feat[2]);
11982f8311c9SChristian Borntraeger 	return 0;
119915c9705fSDavid Hildenbrand }
120015c9705fSDavid Hildenbrand 
12010a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
12020a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
12030a763c78SDavid Hildenbrand {
12040a763c78SDavid Hildenbrand 	/*
12050a763c78SDavid Hildenbrand 	 * Once supported by kernel + hw, we have to store the subfunctions
12060a763c78SDavid Hildenbrand 	 * in kvm->arch and remember that user space configured them.
12070a763c78SDavid Hildenbrand 	 */
12080a763c78SDavid Hildenbrand 	return -ENXIO;
12090a763c78SDavid Hildenbrand }
12100a763c78SDavid Hildenbrand 
1211658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1212658b6edaSMichael Mueller {
1213658b6edaSMichael Mueller 	int ret = -ENXIO;
1214658b6edaSMichael Mueller 
1215658b6edaSMichael Mueller 	switch (attr->attr) {
1216658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1217658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
1218658b6edaSMichael Mueller 		break;
121915c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
122015c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
122115c9705fSDavid Hildenbrand 		break;
12220a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
12230a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
12240a763c78SDavid Hildenbrand 		break;
1225658b6edaSMichael Mueller 	}
1226658b6edaSMichael Mueller 	return ret;
1227658b6edaSMichael Mueller }
1228658b6edaSMichael Mueller 
1229658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1230658b6edaSMichael Mueller {
1231658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1232658b6edaSMichael Mueller 	int ret = 0;
1233658b6edaSMichael Mueller 
1234658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1235658b6edaSMichael Mueller 	if (!proc) {
1236658b6edaSMichael Mueller 		ret = -ENOMEM;
1237658b6edaSMichael Mueller 		goto out;
1238658b6edaSMichael Mueller 	}
12399bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
1240658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
1241c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1242c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1243a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1244a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1245a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1246a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1247a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[0],
1248a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[1],
1249a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[2]);
1250658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1251658b6edaSMichael Mueller 		ret = -EFAULT;
1252658b6edaSMichael Mueller 	kfree(proc);
1253658b6edaSMichael Mueller out:
1254658b6edaSMichael Mueller 	return ret;
1255658b6edaSMichael Mueller }
1256658b6edaSMichael Mueller 
1257658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1258658b6edaSMichael Mueller {
1259658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
1260658b6edaSMichael Mueller 	int ret = 0;
1261658b6edaSMichael Mueller 
1262658b6edaSMichael Mueller 	mach = kzalloc(sizeof(*mach), GFP_KERNEL);
1263658b6edaSMichael Mueller 	if (!mach) {
1264658b6edaSMichael Mueller 		ret = -ENOMEM;
1265658b6edaSMichael Mueller 		goto out;
1266658b6edaSMichael Mueller 	}
1267658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
126837c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
1269c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1270981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1271658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
127204478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
1273a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
1274a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1275a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1276a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
1277a8c39dd7SChristian Borntraeger 		 mach->fac_mask[0],
1278a8c39dd7SChristian Borntraeger 		 mach->fac_mask[1],
1279a8c39dd7SChristian Borntraeger 		 mach->fac_mask[2]);
1280a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
1281a8c39dd7SChristian Borntraeger 		 mach->fac_list[0],
1282a8c39dd7SChristian Borntraeger 		 mach->fac_list[1],
1283a8c39dd7SChristian Borntraeger 		 mach->fac_list[2]);
1284658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1285658b6edaSMichael Mueller 		ret = -EFAULT;
1286658b6edaSMichael Mueller 	kfree(mach);
1287658b6edaSMichael Mueller out:
1288658b6edaSMichael Mueller 	return ret;
1289658b6edaSMichael Mueller }
1290658b6edaSMichael Mueller 
129115c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
129215c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
129315c9705fSDavid Hildenbrand {
129415c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
129515c9705fSDavid Hildenbrand 
129615c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
129715c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
129815c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
129915c9705fSDavid Hildenbrand 		return -EFAULT;
13002f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
13012f8311c9SChristian Borntraeger 			 data.feat[0],
13022f8311c9SChristian Borntraeger 			 data.feat[1],
13032f8311c9SChristian Borntraeger 			 data.feat[2]);
130415c9705fSDavid Hildenbrand 	return 0;
130515c9705fSDavid Hildenbrand }
130615c9705fSDavid Hildenbrand 
130715c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
130815c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
130915c9705fSDavid Hildenbrand {
131015c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
131115c9705fSDavid Hildenbrand 
131215c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat,
131315c9705fSDavid Hildenbrand 		    kvm_s390_available_cpu_feat,
131415c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
131515c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
131615c9705fSDavid Hildenbrand 		return -EFAULT;
13172f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host feat:  0x%16.16llx.0x%16.16llx.0x%16.16llx",
13182f8311c9SChristian Borntraeger 			 data.feat[0],
13192f8311c9SChristian Borntraeger 			 data.feat[1],
13202f8311c9SChristian Borntraeger 			 data.feat[2]);
132115c9705fSDavid Hildenbrand 	return 0;
132215c9705fSDavid Hildenbrand }
132315c9705fSDavid Hildenbrand 
13240a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
13250a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
13260a763c78SDavid Hildenbrand {
13270a763c78SDavid Hildenbrand 	/*
13280a763c78SDavid Hildenbrand 	 * Once we can actually configure subfunctions (kernel + hw support),
13290a763c78SDavid Hildenbrand 	 * we have to check if they were already set by user space, if so copy
13300a763c78SDavid Hildenbrand 	 * them from kvm->arch.
13310a763c78SDavid Hildenbrand 	 */
13320a763c78SDavid Hildenbrand 	return -ENXIO;
13330a763c78SDavid Hildenbrand }
13340a763c78SDavid Hildenbrand 
13350a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
13360a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
13370a763c78SDavid Hildenbrand {
13380a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
13390a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
13400a763c78SDavid Hildenbrand 		return -EFAULT;
13410a763c78SDavid Hildenbrand 	return 0;
13420a763c78SDavid Hildenbrand }
1343658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1344658b6edaSMichael Mueller {
1345658b6edaSMichael Mueller 	int ret = -ENXIO;
1346658b6edaSMichael Mueller 
1347658b6edaSMichael Mueller 	switch (attr->attr) {
1348658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1349658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
1350658b6edaSMichael Mueller 		break;
1351658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
1352658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
1353658b6edaSMichael Mueller 		break;
135415c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
135515c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
135615c9705fSDavid Hildenbrand 		break;
135715c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
135815c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
135915c9705fSDavid Hildenbrand 		break;
13600a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
13610a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
13620a763c78SDavid Hildenbrand 		break;
13630a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
13640a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
13650a763c78SDavid Hildenbrand 		break;
1366658b6edaSMichael Mueller 	}
1367658b6edaSMichael Mueller 	return ret;
1368658b6edaSMichael Mueller }
1369658b6edaSMichael Mueller 
1370f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1371f2061656SDominik Dingel {
1372f2061656SDominik Dingel 	int ret;
1373f2061656SDominik Dingel 
1374f2061656SDominik Dingel 	switch (attr->group) {
13754f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
13768c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
13774f718eabSDominik Dingel 		break;
137872f25020SJason J. Herne 	case KVM_S390_VM_TOD:
137972f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
138072f25020SJason J. Herne 		break;
1381658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1382658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1383658b6edaSMichael Mueller 		break;
1384a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1385a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1386a374e892STony Krowiak 		break;
1387190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1388190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_set_migration(kvm, attr);
1389190df4a2SClaudio Imbrenda 		break;
1390f2061656SDominik Dingel 	default:
1391f2061656SDominik Dingel 		ret = -ENXIO;
1392f2061656SDominik Dingel 		break;
1393f2061656SDominik Dingel 	}
1394f2061656SDominik Dingel 
1395f2061656SDominik Dingel 	return ret;
1396f2061656SDominik Dingel }
1397f2061656SDominik Dingel 
1398f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1399f2061656SDominik Dingel {
14008c0a7ce6SDominik Dingel 	int ret;
14018c0a7ce6SDominik Dingel 
14028c0a7ce6SDominik Dingel 	switch (attr->group) {
14038c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
14048c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
14058c0a7ce6SDominik Dingel 		break;
140672f25020SJason J. Herne 	case KVM_S390_VM_TOD:
140772f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
140872f25020SJason J. Herne 		break;
1409658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1410658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1411658b6edaSMichael Mueller 		break;
1412190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1413190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_get_migration(kvm, attr);
1414190df4a2SClaudio Imbrenda 		break;
14158c0a7ce6SDominik Dingel 	default:
14168c0a7ce6SDominik Dingel 		ret = -ENXIO;
14178c0a7ce6SDominik Dingel 		break;
14188c0a7ce6SDominik Dingel 	}
14198c0a7ce6SDominik Dingel 
14208c0a7ce6SDominik Dingel 	return ret;
1421f2061656SDominik Dingel }
1422f2061656SDominik Dingel 
1423f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1424f2061656SDominik Dingel {
1425f2061656SDominik Dingel 	int ret;
1426f2061656SDominik Dingel 
1427f2061656SDominik Dingel 	switch (attr->group) {
14284f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
14294f718eabSDominik Dingel 		switch (attr->attr) {
14304f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
14314f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1432f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1433f9cbd9b0SDavid Hildenbrand 			break;
14348c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
14354f718eabSDominik Dingel 			ret = 0;
14364f718eabSDominik Dingel 			break;
14374f718eabSDominik Dingel 		default:
14384f718eabSDominik Dingel 			ret = -ENXIO;
14394f718eabSDominik Dingel 			break;
14404f718eabSDominik Dingel 		}
14414f718eabSDominik Dingel 		break;
144272f25020SJason J. Herne 	case KVM_S390_VM_TOD:
144372f25020SJason J. Herne 		switch (attr->attr) {
144472f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
144572f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
144672f25020SJason J. Herne 			ret = 0;
144772f25020SJason J. Herne 			break;
144872f25020SJason J. Herne 		default:
144972f25020SJason J. Herne 			ret = -ENXIO;
145072f25020SJason J. Herne 			break;
145172f25020SJason J. Herne 		}
145272f25020SJason J. Herne 		break;
1453658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1454658b6edaSMichael Mueller 		switch (attr->attr) {
1455658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1456658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
145715c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
145815c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
14590a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1460658b6edaSMichael Mueller 			ret = 0;
1461658b6edaSMichael Mueller 			break;
14620a763c78SDavid Hildenbrand 		/* configuring subfunctions is not supported yet */
14630a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1464658b6edaSMichael Mueller 		default:
1465658b6edaSMichael Mueller 			ret = -ENXIO;
1466658b6edaSMichael Mueller 			break;
1467658b6edaSMichael Mueller 		}
1468658b6edaSMichael Mueller 		break;
1469a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1470a374e892STony Krowiak 		switch (attr->attr) {
1471a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1472a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1473a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1474a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1475a374e892STony Krowiak 			ret = 0;
1476a374e892STony Krowiak 			break;
1477a374e892STony Krowiak 		default:
1478a374e892STony Krowiak 			ret = -ENXIO;
1479a374e892STony Krowiak 			break;
1480a374e892STony Krowiak 		}
1481a374e892STony Krowiak 		break;
1482190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1483190df4a2SClaudio Imbrenda 		ret = 0;
1484190df4a2SClaudio Imbrenda 		break;
1485f2061656SDominik Dingel 	default:
1486f2061656SDominik Dingel 		ret = -ENXIO;
1487f2061656SDominik Dingel 		break;
1488f2061656SDominik Dingel 	}
1489f2061656SDominik Dingel 
1490f2061656SDominik Dingel 	return ret;
1491f2061656SDominik Dingel }
1492f2061656SDominik Dingel 
149330ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
149430ee2a98SJason J. Herne {
149530ee2a98SJason J. Herne 	uint8_t *keys;
149630ee2a98SJason J. Herne 	uint64_t hva;
14974f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
149830ee2a98SJason J. Herne 
149930ee2a98SJason J. Herne 	if (args->flags != 0)
150030ee2a98SJason J. Herne 		return -EINVAL;
150130ee2a98SJason J. Herne 
150230ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
150355531b74SJanosch Frank 	if (!mm_uses_skeys(current->mm))
150430ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
150530ee2a98SJason J. Herne 
150630ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
150730ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
150830ee2a98SJason J. Herne 		return -EINVAL;
150930ee2a98SJason J. Herne 
1510752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
151130ee2a98SJason J. Herne 	if (!keys)
151230ee2a98SJason J. Herne 		return -ENOMEM;
151330ee2a98SJason J. Herne 
1514d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
15154f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
151630ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
151730ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
151830ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
151930ee2a98SJason J. Herne 			r = -EFAULT;
1520d3ed1ceeSMartin Schwidefsky 			break;
152130ee2a98SJason J. Herne 		}
152230ee2a98SJason J. Herne 
1523154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
1524154c8c19SDavid Hildenbrand 		if (r)
1525d3ed1ceeSMartin Schwidefsky 			break;
152630ee2a98SJason J. Herne 	}
15274f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1528d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
152930ee2a98SJason J. Herne 
1530d3ed1ceeSMartin Schwidefsky 	if (!r) {
153130ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
153230ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
153330ee2a98SJason J. Herne 		if (r)
153430ee2a98SJason J. Herne 			r = -EFAULT;
1535d3ed1ceeSMartin Schwidefsky 	}
1536d3ed1ceeSMartin Schwidefsky 
153730ee2a98SJason J. Herne 	kvfree(keys);
153830ee2a98SJason J. Herne 	return r;
153930ee2a98SJason J. Herne }
154030ee2a98SJason J. Herne 
154130ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
154230ee2a98SJason J. Herne {
154330ee2a98SJason J. Herne 	uint8_t *keys;
154430ee2a98SJason J. Herne 	uint64_t hva;
15454f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
154630ee2a98SJason J. Herne 
154730ee2a98SJason J. Herne 	if (args->flags != 0)
154830ee2a98SJason J. Herne 		return -EINVAL;
154930ee2a98SJason J. Herne 
155030ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
155130ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
155230ee2a98SJason J. Herne 		return -EINVAL;
155330ee2a98SJason J. Herne 
1554752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
155530ee2a98SJason J. Herne 	if (!keys)
155630ee2a98SJason J. Herne 		return -ENOMEM;
155730ee2a98SJason J. Herne 
155830ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
155930ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
156030ee2a98SJason J. Herne 	if (r) {
156130ee2a98SJason J. Herne 		r = -EFAULT;
156230ee2a98SJason J. Herne 		goto out;
156330ee2a98SJason J. Herne 	}
156430ee2a98SJason J. Herne 
156530ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
156614d4a425SDominik Dingel 	r = s390_enable_skey();
156714d4a425SDominik Dingel 	if (r)
156814d4a425SDominik Dingel 		goto out;
156930ee2a98SJason J. Herne 
1570d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
15714f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
157230ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
157330ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
157430ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
157530ee2a98SJason J. Herne 			r = -EFAULT;
1576d3ed1ceeSMartin Schwidefsky 			break;
157730ee2a98SJason J. Herne 		}
157830ee2a98SJason J. Herne 
157930ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
158030ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
158130ee2a98SJason J. Herne 			r = -EINVAL;
1582d3ed1ceeSMartin Schwidefsky 			break;
158330ee2a98SJason J. Herne 		}
158430ee2a98SJason J. Herne 
1585fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
158630ee2a98SJason J. Herne 		if (r)
1587d3ed1ceeSMartin Schwidefsky 			break;
158830ee2a98SJason J. Herne 	}
15894f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1590d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
159130ee2a98SJason J. Herne out:
159230ee2a98SJason J. Herne 	kvfree(keys);
159330ee2a98SJason J. Herne 	return r;
159430ee2a98SJason J. Herne }
159530ee2a98SJason J. Herne 
15964036e387SClaudio Imbrenda /*
15974036e387SClaudio Imbrenda  * Base address and length must be sent at the start of each block, therefore
15984036e387SClaudio Imbrenda  * it's cheaper to send some clean data, as long as it's less than the size of
15994036e387SClaudio Imbrenda  * two longs.
16004036e387SClaudio Imbrenda  */
16014036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
16024036e387SClaudio Imbrenda /* for consistency */
16034036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
16044036e387SClaudio Imbrenda 
16054036e387SClaudio Imbrenda /*
16064036e387SClaudio Imbrenda  * This function searches for the next page with dirty CMMA attributes, and
16074036e387SClaudio Imbrenda  * saves the attributes in the buffer up to either the end of the buffer or
16084036e387SClaudio Imbrenda  * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
16094036e387SClaudio Imbrenda  * no trailing clean bytes are saved.
16104036e387SClaudio Imbrenda  * In case no dirty bits were found, or if CMMA was not enabled or used, the
16114036e387SClaudio Imbrenda  * output buffer will indicate 0 as length.
16124036e387SClaudio Imbrenda  */
16134036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm,
16144036e387SClaudio Imbrenda 				  struct kvm_s390_cmma_log *args)
16154036e387SClaudio Imbrenda {
16164036e387SClaudio Imbrenda 	struct kvm_s390_migration_state *s = kvm->arch.migration_state;
16174036e387SClaudio Imbrenda 	unsigned long bufsize, hva, pgstev, i, next, cur;
16184036e387SClaudio Imbrenda 	int srcu_idx, peek, r = 0, rr;
16194036e387SClaudio Imbrenda 	u8 *res;
16204036e387SClaudio Imbrenda 
16214036e387SClaudio Imbrenda 	cur = args->start_gfn;
16224036e387SClaudio Imbrenda 	i = next = pgstev = 0;
16234036e387SClaudio Imbrenda 
16244036e387SClaudio Imbrenda 	if (unlikely(!kvm->arch.use_cmma))
16254036e387SClaudio Imbrenda 		return -ENXIO;
16264036e387SClaudio Imbrenda 	/* Invalid/unsupported flags were specified */
16274036e387SClaudio Imbrenda 	if (args->flags & ~KVM_S390_CMMA_PEEK)
16284036e387SClaudio Imbrenda 		return -EINVAL;
16294036e387SClaudio Imbrenda 	/* Migration mode query, and we are not doing a migration */
16304036e387SClaudio Imbrenda 	peek = !!(args->flags & KVM_S390_CMMA_PEEK);
16314036e387SClaudio Imbrenda 	if (!peek && !s)
16324036e387SClaudio Imbrenda 		return -EINVAL;
16334036e387SClaudio Imbrenda 	/* CMMA is disabled or was not used, or the buffer has length zero */
16344036e387SClaudio Imbrenda 	bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
1635c9f0a2b8SJanosch Frank 	if (!bufsize || !kvm->mm->context.uses_cmm) {
16364036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
16374036e387SClaudio Imbrenda 		return 0;
16384036e387SClaudio Imbrenda 	}
16394036e387SClaudio Imbrenda 
16404036e387SClaudio Imbrenda 	if (!peek) {
16414036e387SClaudio Imbrenda 		/* We are not peeking, and there are no dirty pages */
16424036e387SClaudio Imbrenda 		if (!atomic64_read(&s->dirty_pages)) {
16434036e387SClaudio Imbrenda 			memset(args, 0, sizeof(*args));
16444036e387SClaudio Imbrenda 			return 0;
16454036e387SClaudio Imbrenda 		}
16464036e387SClaudio Imbrenda 		cur = find_next_bit(s->pgste_bitmap, s->bitmap_size,
16474036e387SClaudio Imbrenda 				    args->start_gfn);
16484036e387SClaudio Imbrenda 		if (cur >= s->bitmap_size)	/* nothing found, loop back */
16494036e387SClaudio Imbrenda 			cur = find_next_bit(s->pgste_bitmap, s->bitmap_size, 0);
16504036e387SClaudio Imbrenda 		if (cur >= s->bitmap_size) {	/* again! (very unlikely) */
16514036e387SClaudio Imbrenda 			memset(args, 0, sizeof(*args));
16524036e387SClaudio Imbrenda 			return 0;
16534036e387SClaudio Imbrenda 		}
16544036e387SClaudio Imbrenda 		next = find_next_bit(s->pgste_bitmap, s->bitmap_size, cur + 1);
16554036e387SClaudio Imbrenda 	}
16564036e387SClaudio Imbrenda 
16574036e387SClaudio Imbrenda 	res = vmalloc(bufsize);
16584036e387SClaudio Imbrenda 	if (!res)
16594036e387SClaudio Imbrenda 		return -ENOMEM;
16604036e387SClaudio Imbrenda 
16614036e387SClaudio Imbrenda 	args->start_gfn = cur;
16624036e387SClaudio Imbrenda 
16634036e387SClaudio Imbrenda 	down_read(&kvm->mm->mmap_sem);
16644036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
16654036e387SClaudio Imbrenda 	while (i < bufsize) {
16664036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur);
16674036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
16684036e387SClaudio Imbrenda 			r = -EFAULT;
16694036e387SClaudio Imbrenda 			break;
16704036e387SClaudio Imbrenda 		}
16714036e387SClaudio Imbrenda 		/* decrement only if we actually flipped the bit to 0 */
16724036e387SClaudio Imbrenda 		if (!peek && test_and_clear_bit(cur, s->pgste_bitmap))
16734036e387SClaudio Imbrenda 			atomic64_dec(&s->dirty_pages);
16744036e387SClaudio Imbrenda 		r = get_pgste(kvm->mm, hva, &pgstev);
16754036e387SClaudio Imbrenda 		if (r < 0)
16764036e387SClaudio Imbrenda 			pgstev = 0;
16774036e387SClaudio Imbrenda 		/* save the value */
16781bab1c02SClaudio Imbrenda 		res[i++] = (pgstev >> 24) & 0x43;
16794036e387SClaudio Imbrenda 		/*
16804036e387SClaudio Imbrenda 		 * if the next bit is too far away, stop.
16814036e387SClaudio Imbrenda 		 * if we reached the previous "next", find the next one
16824036e387SClaudio Imbrenda 		 */
16834036e387SClaudio Imbrenda 		if (!peek) {
16844036e387SClaudio Imbrenda 			if (next > cur + KVM_S390_MAX_BIT_DISTANCE)
16854036e387SClaudio Imbrenda 				break;
16864036e387SClaudio Imbrenda 			if (cur == next)
16874036e387SClaudio Imbrenda 				next = find_next_bit(s->pgste_bitmap,
16884036e387SClaudio Imbrenda 						     s->bitmap_size, cur + 1);
16894036e387SClaudio Imbrenda 		/* reached the end of the bitmap or of the buffer, stop */
16904036e387SClaudio Imbrenda 			if ((next >= s->bitmap_size) ||
16914036e387SClaudio Imbrenda 			    (next >= args->start_gfn + bufsize))
16924036e387SClaudio Imbrenda 				break;
16934036e387SClaudio Imbrenda 		}
16944036e387SClaudio Imbrenda 		cur++;
16954036e387SClaudio Imbrenda 	}
16964036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
16974036e387SClaudio Imbrenda 	up_read(&kvm->mm->mmap_sem);
16984036e387SClaudio Imbrenda 	args->count = i;
16994036e387SClaudio Imbrenda 	args->remaining = s ? atomic64_read(&s->dirty_pages) : 0;
17004036e387SClaudio Imbrenda 
17014036e387SClaudio Imbrenda 	rr = copy_to_user((void __user *)args->values, res, args->count);
17024036e387SClaudio Imbrenda 	if (rr)
17034036e387SClaudio Imbrenda 		r = -EFAULT;
17044036e387SClaudio Imbrenda 
17054036e387SClaudio Imbrenda 	vfree(res);
17064036e387SClaudio Imbrenda 	return r;
17074036e387SClaudio Imbrenda }
17084036e387SClaudio Imbrenda 
17094036e387SClaudio Imbrenda /*
17104036e387SClaudio Imbrenda  * This function sets the CMMA attributes for the given pages. If the input
17114036e387SClaudio Imbrenda  * buffer has zero length, no action is taken, otherwise the attributes are
1712c9f0a2b8SJanosch Frank  * set and the mm->context.uses_cmm flag is set.
17134036e387SClaudio Imbrenda  */
17144036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm,
17154036e387SClaudio Imbrenda 				  const struct kvm_s390_cmma_log *args)
17164036e387SClaudio Imbrenda {
17174036e387SClaudio Imbrenda 	unsigned long hva, mask, pgstev, i;
17184036e387SClaudio Imbrenda 	uint8_t *bits;
17194036e387SClaudio Imbrenda 	int srcu_idx, r = 0;
17204036e387SClaudio Imbrenda 
17214036e387SClaudio Imbrenda 	mask = args->mask;
17224036e387SClaudio Imbrenda 
17234036e387SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
17244036e387SClaudio Imbrenda 		return -ENXIO;
17254036e387SClaudio Imbrenda 	/* invalid/unsupported flags */
17264036e387SClaudio Imbrenda 	if (args->flags != 0)
17274036e387SClaudio Imbrenda 		return -EINVAL;
17284036e387SClaudio Imbrenda 	/* Enforce sane limit on memory allocation */
17294036e387SClaudio Imbrenda 	if (args->count > KVM_S390_CMMA_SIZE_MAX)
17304036e387SClaudio Imbrenda 		return -EINVAL;
17314036e387SClaudio Imbrenda 	/* Nothing to do */
17324036e387SClaudio Imbrenda 	if (args->count == 0)
17334036e387SClaudio Imbrenda 		return 0;
17344036e387SClaudio Imbrenda 
17354036e387SClaudio Imbrenda 	bits = vmalloc(sizeof(*bits) * args->count);
17364036e387SClaudio Imbrenda 	if (!bits)
17374036e387SClaudio Imbrenda 		return -ENOMEM;
17384036e387SClaudio Imbrenda 
17394036e387SClaudio Imbrenda 	r = copy_from_user(bits, (void __user *)args->values, args->count);
17404036e387SClaudio Imbrenda 	if (r) {
17414036e387SClaudio Imbrenda 		r = -EFAULT;
17424036e387SClaudio Imbrenda 		goto out;
17434036e387SClaudio Imbrenda 	}
17444036e387SClaudio Imbrenda 
17454036e387SClaudio Imbrenda 	down_read(&kvm->mm->mmap_sem);
17464036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
17474036e387SClaudio Imbrenda 	for (i = 0; i < args->count; i++) {
17484036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, args->start_gfn + i);
17494036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
17504036e387SClaudio Imbrenda 			r = -EFAULT;
17514036e387SClaudio Imbrenda 			break;
17524036e387SClaudio Imbrenda 		}
17534036e387SClaudio Imbrenda 
17544036e387SClaudio Imbrenda 		pgstev = bits[i];
17554036e387SClaudio Imbrenda 		pgstev = pgstev << 24;
17561bab1c02SClaudio Imbrenda 		mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
17574036e387SClaudio Imbrenda 		set_pgste_bits(kvm->mm, hva, mask, pgstev);
17584036e387SClaudio Imbrenda 	}
17594036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
17604036e387SClaudio Imbrenda 	up_read(&kvm->mm->mmap_sem);
17614036e387SClaudio Imbrenda 
1762c9f0a2b8SJanosch Frank 	if (!kvm->mm->context.uses_cmm) {
17634036e387SClaudio Imbrenda 		down_write(&kvm->mm->mmap_sem);
1764c9f0a2b8SJanosch Frank 		kvm->mm->context.uses_cmm = 1;
17654036e387SClaudio Imbrenda 		up_write(&kvm->mm->mmap_sem);
17664036e387SClaudio Imbrenda 	}
17674036e387SClaudio Imbrenda out:
17684036e387SClaudio Imbrenda 	vfree(bits);
17694036e387SClaudio Imbrenda 	return r;
17704036e387SClaudio Imbrenda }
17714036e387SClaudio Imbrenda 
1772b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
1773b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
1774b0c632dbSHeiko Carstens {
1775b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
1776b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
1777f2061656SDominik Dingel 	struct kvm_device_attr attr;
1778b0c632dbSHeiko Carstens 	int r;
1779b0c632dbSHeiko Carstens 
1780b0c632dbSHeiko Carstens 	switch (ioctl) {
1781ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
1782ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
1783ba5c1e9bSCarsten Otte 
1784ba5c1e9bSCarsten Otte 		r = -EFAULT;
1785ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
1786ba5c1e9bSCarsten Otte 			break;
1787ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
1788ba5c1e9bSCarsten Otte 		break;
1789ba5c1e9bSCarsten Otte 	}
1790d938dc55SCornelia Huck 	case KVM_ENABLE_CAP: {
1791d938dc55SCornelia Huck 		struct kvm_enable_cap cap;
1792d938dc55SCornelia Huck 		r = -EFAULT;
1793d938dc55SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
1794d938dc55SCornelia Huck 			break;
1795d938dc55SCornelia Huck 		r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1796d938dc55SCornelia Huck 		break;
1797d938dc55SCornelia Huck 	}
179884223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
179984223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
180084223598SCornelia Huck 
180184223598SCornelia Huck 		r = -EINVAL;
180284223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
180384223598SCornelia Huck 			/* Set up dummy routing. */
180484223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
1805152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
180684223598SCornelia Huck 		}
180784223598SCornelia Huck 		break;
180884223598SCornelia Huck 	}
1809f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
1810f2061656SDominik Dingel 		r = -EFAULT;
1811f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1812f2061656SDominik Dingel 			break;
1813f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
1814f2061656SDominik Dingel 		break;
1815f2061656SDominik Dingel 	}
1816f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
1817f2061656SDominik Dingel 		r = -EFAULT;
1818f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1819f2061656SDominik Dingel 			break;
1820f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
1821f2061656SDominik Dingel 		break;
1822f2061656SDominik Dingel 	}
1823f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
1824f2061656SDominik Dingel 		r = -EFAULT;
1825f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1826f2061656SDominik Dingel 			break;
1827f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
1828f2061656SDominik Dingel 		break;
1829f2061656SDominik Dingel 	}
183030ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
183130ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
183230ee2a98SJason J. Herne 
183330ee2a98SJason J. Herne 		r = -EFAULT;
183430ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
183530ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
183630ee2a98SJason J. Herne 			break;
183730ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
183830ee2a98SJason J. Herne 		break;
183930ee2a98SJason J. Herne 	}
184030ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
184130ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
184230ee2a98SJason J. Herne 
184330ee2a98SJason J. Herne 		r = -EFAULT;
184430ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
184530ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
184630ee2a98SJason J. Herne 			break;
184730ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
184830ee2a98SJason J. Herne 		break;
184930ee2a98SJason J. Herne 	}
18504036e387SClaudio Imbrenda 	case KVM_S390_GET_CMMA_BITS: {
18514036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
18524036e387SClaudio Imbrenda 
18534036e387SClaudio Imbrenda 		r = -EFAULT;
18544036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
18554036e387SClaudio Imbrenda 			break;
18561de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
18574036e387SClaudio Imbrenda 		r = kvm_s390_get_cmma_bits(kvm, &args);
18581de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
18594036e387SClaudio Imbrenda 		if (!r) {
18604036e387SClaudio Imbrenda 			r = copy_to_user(argp, &args, sizeof(args));
18614036e387SClaudio Imbrenda 			if (r)
18624036e387SClaudio Imbrenda 				r = -EFAULT;
18634036e387SClaudio Imbrenda 		}
18644036e387SClaudio Imbrenda 		break;
18654036e387SClaudio Imbrenda 	}
18664036e387SClaudio Imbrenda 	case KVM_S390_SET_CMMA_BITS: {
18674036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
18684036e387SClaudio Imbrenda 
18694036e387SClaudio Imbrenda 		r = -EFAULT;
18704036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
18714036e387SClaudio Imbrenda 			break;
18721de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
18734036e387SClaudio Imbrenda 		r = kvm_s390_set_cmma_bits(kvm, &args);
18741de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
18754036e387SClaudio Imbrenda 		break;
18764036e387SClaudio Imbrenda 	}
1877b0c632dbSHeiko Carstens 	default:
1878367e1319SAvi Kivity 		r = -ENOTTY;
1879b0c632dbSHeiko Carstens 	}
1880b0c632dbSHeiko Carstens 
1881b0c632dbSHeiko Carstens 	return r;
1882b0c632dbSHeiko Carstens }
1883b0c632dbSHeiko Carstens 
188445c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config)
188545c9b47cSTony Krowiak {
188645c9b47cSTony Krowiak 	u32 fcn_code = 0x04000000UL;
188786044c8cSChristian Borntraeger 	u32 cc = 0;
188845c9b47cSTony Krowiak 
188986044c8cSChristian Borntraeger 	memset(config, 0, 128);
189045c9b47cSTony Krowiak 	asm volatile(
189145c9b47cSTony Krowiak 		"lgr 0,%1\n"
189245c9b47cSTony Krowiak 		"lgr 2,%2\n"
189345c9b47cSTony Krowiak 		".long 0xb2af0000\n"		/* PQAP(QCI) */
189486044c8cSChristian Borntraeger 		"0: ipm %0\n"
189545c9b47cSTony Krowiak 		"srl %0,28\n"
189686044c8cSChristian Borntraeger 		"1:\n"
189786044c8cSChristian Borntraeger 		EX_TABLE(0b, 1b)
189886044c8cSChristian Borntraeger 		: "+r" (cc)
189945c9b47cSTony Krowiak 		: "r" (fcn_code), "r" (config)
190045c9b47cSTony Krowiak 		: "cc", "0", "2", "memory"
190145c9b47cSTony Krowiak 	);
190245c9b47cSTony Krowiak 
190345c9b47cSTony Krowiak 	return cc;
190445c9b47cSTony Krowiak }
190545c9b47cSTony Krowiak 
190645c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
190745c9b47cSTony Krowiak {
190845c9b47cSTony Krowiak 	u8 config[128];
190945c9b47cSTony Krowiak 	int cc;
191045c9b47cSTony Krowiak 
1911a6aacc3fSHeiko Carstens 	if (test_facility(12)) {
191245c9b47cSTony Krowiak 		cc = kvm_s390_query_ap_config(config);
191345c9b47cSTony Krowiak 
191445c9b47cSTony Krowiak 		if (cc)
191545c9b47cSTony Krowiak 			pr_err("PQAP(QCI) failed with cc=%d", cc);
191645c9b47cSTony Krowiak 		else
191745c9b47cSTony Krowiak 			return config[0] & 0x40;
191845c9b47cSTony Krowiak 	}
191945c9b47cSTony Krowiak 
192045c9b47cSTony Krowiak 	return 0;
192145c9b47cSTony Krowiak }
192245c9b47cSTony Krowiak 
192345c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
192445c9b47cSTony Krowiak {
192545c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
192645c9b47cSTony Krowiak 
192745c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
192845c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
192945c9b47cSTony Krowiak 	else
193045c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
193145c9b47cSTony Krowiak }
193245c9b47cSTony Krowiak 
19339bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
19349d8d5786SMichael Mueller {
19359bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
19369bb0ec09SDavid Hildenbrand 
19379bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
19389bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
19399bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
19409d8d5786SMichael Mueller }
19419d8d5786SMichael Mueller 
1942c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
19435102ee87STony Krowiak {
19449d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
1945c54f0d6aSDavid Hildenbrand 		return;
19465102ee87STony Krowiak 
1947c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
194845c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
19495102ee87STony Krowiak 
1950ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
1951ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
1952ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
1953ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1954ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1955ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1956ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
19575102ee87STony Krowiak }
19585102ee87STony Krowiak 
19597d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
19607d43bafcSEugene (jno) Dvurechenski {
19617d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
19625e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
19637d43bafcSEugene (jno) Dvurechenski 	else
19647d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
19657d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
19667d43bafcSEugene (jno) Dvurechenski }
19677d43bafcSEugene (jno) Dvurechenski 
1968e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1969b0c632dbSHeiko Carstens {
197076a6dd72SDavid Hildenbrand 	gfp_t alloc_flags = GFP_KERNEL;
19719d8d5786SMichael Mueller 	int i, rc;
1972b0c632dbSHeiko Carstens 	char debug_name[16];
1973f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
1974b0c632dbSHeiko Carstens 
1975e08b9637SCarsten Otte 	rc = -EINVAL;
1976e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1977e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
1978e08b9637SCarsten Otte 		goto out_err;
1979e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1980e08b9637SCarsten Otte 		goto out_err;
1981e08b9637SCarsten Otte #else
1982e08b9637SCarsten Otte 	if (type)
1983e08b9637SCarsten Otte 		goto out_err;
1984e08b9637SCarsten Otte #endif
1985e08b9637SCarsten Otte 
1986b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
1987b0c632dbSHeiko Carstens 	if (rc)
1988d89f5effSJan Kiszka 		goto out_err;
1989b0c632dbSHeiko Carstens 
1990b290411aSCarsten Otte 	rc = -ENOMEM;
1991b290411aSCarsten Otte 
19927d43bafcSEugene (jno) Dvurechenski 	kvm->arch.use_esca = 0; /* start with basic SCA */
199376a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
199476a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
19955e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
199676a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
1997b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
1998d89f5effSJan Kiszka 		goto out_err;
1999f6c137ffSChristian Borntraeger 	spin_lock(&kvm_lock);
2000c5c2c393SDavid Hildenbrand 	sca_offset += 16;
2001bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
2002c5c2c393SDavid Hildenbrand 		sca_offset = 0;
2003bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
2004bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
2005f6c137ffSChristian Borntraeger 	spin_unlock(&kvm_lock);
2006b0c632dbSHeiko Carstens 
2007b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
2008b0c632dbSHeiko Carstens 
20091cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
2010b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
201140f5b735SDominik Dingel 		goto out_err;
2012b0c632dbSHeiko Carstens 
201319114bebSMichael Mueller 	BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
2014c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
2015c54f0d6aSDavid Hildenbrand 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
2016c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
201740f5b735SDominik Dingel 		goto out_err;
20189d8d5786SMichael Mueller 
2019c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
2020c3b9e3e1SChristian Borntraeger 
2021c3b9e3e1SChristian Borntraeger 	for (i = 0; i < kvm_s390_fac_size(); i++) {
2022c3b9e3e1SChristian Borntraeger 		kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] &
2023c3b9e3e1SChristian Borntraeger 					      (kvm_s390_fac_base[i] |
2024c3b9e3e1SChristian Borntraeger 					       kvm_s390_fac_ext[i]);
2025c3b9e3e1SChristian Borntraeger 		kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] &
2026c3b9e3e1SChristian Borntraeger 					      kvm_s390_fac_base[i];
2027c3b9e3e1SChristian Borntraeger 	}
2028981467c9SMichael Mueller 
20291935222dSDavid Hildenbrand 	/* we are always in czam mode - even on pre z14 machines */
20301935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_mask, 138);
20311935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_list, 138);
20321935222dSDavid Hildenbrand 	/* we emulate STHYI in kvm */
203395ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
203495ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
20351bab1c02SClaudio Imbrenda 	if (MACHINE_HAS_TLB_GUEST) {
20361bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_mask, 147);
20371bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_list, 147);
20381bab1c02SClaudio Imbrenda 	}
203995ca2cb5SJanosch Frank 
20409bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
204137c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
20429d8d5786SMichael Mueller 
2043c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
20445102ee87STony Krowiak 
204551978393SFei Li 	mutex_init(&kvm->arch.float_int.ais_lock);
204651978393SFei Li 	kvm->arch.float_int.simm = 0;
204751978393SFei Li 	kvm->arch.float_int.nimm = 0;
2048ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
20496d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
20506d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
20518a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
2052a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
2053ba5c1e9bSCarsten Otte 
2054b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
205578f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
2056b0c632dbSHeiko Carstens 
2057e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
2058e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
2059a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
2060e08b9637SCarsten Otte 	} else {
206132e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
2062ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = TASK_SIZE_MAX;
206332e6b236SGuenther Hutzl 		else
2064ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
206532e6b236SGuenther Hutzl 						    sclp.hamax + 1);
20666ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
2067598841caSCarsten Otte 		if (!kvm->arch.gmap)
206840f5b735SDominik Dingel 			goto out_err;
20692c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
207024eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
2071e08b9637SCarsten Otte 	}
2072fa6b7fe9SCornelia Huck 
2073fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
207484223598SCornelia Huck 	kvm->arch.use_irqchip = 0;
2075c9f0a2b8SJanosch Frank 	kvm->arch.use_pfmfi = sclp.has_pfmfi;
207655531b74SJanosch Frank 	kvm->arch.use_skf = sclp.has_skey;
207772f25020SJason J. Herne 	kvm->arch.epoch = 0;
2078fa6b7fe9SCornelia Huck 
20798ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
2080a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
2081d7c5cb01SMichael Mueller 	kvm_s390_gisa_init(kvm);
20828335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
20838ad35755SDavid Hildenbrand 
2084d89f5effSJan Kiszka 	return 0;
2085d89f5effSJan Kiszka out_err:
2086c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
208740f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
20887d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
208978f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
2090d89f5effSJan Kiszka 	return rc;
2091b0c632dbSHeiko Carstens }
2092b0c632dbSHeiko Carstens 
2093235539b4SLuiz Capitulino bool kvm_arch_has_vcpu_debugfs(void)
2094235539b4SLuiz Capitulino {
2095235539b4SLuiz Capitulino 	return false;
2096235539b4SLuiz Capitulino }
2097235539b4SLuiz Capitulino 
2098235539b4SLuiz Capitulino int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
2099235539b4SLuiz Capitulino {
2100235539b4SLuiz Capitulino 	return 0;
2101235539b4SLuiz Capitulino }
2102235539b4SLuiz Capitulino 
2103d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
2104d329c035SChristian Borntraeger {
2105d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2106ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
210767335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
21083c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
2109bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
2110a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
211127e0393fSCarsten Otte 
211227e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
21136ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
211427e0393fSCarsten Otte 
2115e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
2116b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
2117d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2118b31288faSKonstantin Weitz 
21196692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
2120b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
2121d329c035SChristian Borntraeger }
2122d329c035SChristian Borntraeger 
2123d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
2124d329c035SChristian Borntraeger {
2125d329c035SChristian Borntraeger 	unsigned int i;
2126988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
2127d329c035SChristian Borntraeger 
2128988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
2129988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
2130988a2caeSGleb Natapov 
2131988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
2132988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2133d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
2134988a2caeSGleb Natapov 
2135988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
2136988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
2137d329c035SChristian Borntraeger }
2138d329c035SChristian Borntraeger 
2139b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
2140b0c632dbSHeiko Carstens {
2141d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
21427d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
2143d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
2144d7c5cb01SMichael Mueller 	kvm_s390_gisa_destroy(kvm);
2145c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
214627e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
21476ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
2148841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
214967335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
2150a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
2151190df4a2SClaudio Imbrenda 	if (kvm->arch.migration_state) {
2152190df4a2SClaudio Imbrenda 		vfree(kvm->arch.migration_state->pgste_bitmap);
2153190df4a2SClaudio Imbrenda 		kfree(kvm->arch.migration_state);
2154190df4a2SClaudio Imbrenda 	}
21558335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2156b0c632dbSHeiko Carstens }
2157b0c632dbSHeiko Carstens 
2158b0c632dbSHeiko Carstens /* Section: vcpu related */
2159dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2160b0c632dbSHeiko Carstens {
21616ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
216227e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
216327e0393fSCarsten Otte 		return -ENOMEM;
21642c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
2165dafd032aSDominik Dingel 
216627e0393fSCarsten Otte 	return 0;
216727e0393fSCarsten Otte }
216827e0393fSCarsten Otte 
2169a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2170a6e2f683SEugene (jno) Dvurechenski {
2171a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
2172a6940674SDavid Hildenbrand 		return;
21735e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
21747d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
21757d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
21767d43bafcSEugene (jno) Dvurechenski 
21777d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
21787d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
21797d43bafcSEugene (jno) Dvurechenski 	} else {
2180bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2181a6e2f683SEugene (jno) Dvurechenski 
2182a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2183a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
2184a6e2f683SEugene (jno) Dvurechenski 	}
21855e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
21867d43bafcSEugene (jno) Dvurechenski }
2187a6e2f683SEugene (jno) Dvurechenski 
2188eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2189a6e2f683SEugene (jno) Dvurechenski {
2190a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2191a6940674SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2192a6940674SDavid Hildenbrand 
2193a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
2194a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2195a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2196f07afa04SDavid Hildenbrand 		return;
2197a6940674SDavid Hildenbrand 	}
2198eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
2199eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
2200eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
22017d43bafcSEugene (jno) Dvurechenski 
2202eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
22037d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
22047d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
22050c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2206eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
22077d43bafcSEugene (jno) Dvurechenski 	} else {
2208eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2209a6e2f683SEugene (jno) Dvurechenski 
2210eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2211a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2212a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2213eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2214a6e2f683SEugene (jno) Dvurechenski 	}
2215eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
22165e044315SEugene (jno) Dvurechenski }
22175e044315SEugene (jno) Dvurechenski 
22185e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
22195e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
22205e044315SEugene (jno) Dvurechenski {
22215e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
22225e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
22235e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
22245e044315SEugene (jno) Dvurechenski }
22255e044315SEugene (jno) Dvurechenski 
22265e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
22275e044315SEugene (jno) Dvurechenski {
22285e044315SEugene (jno) Dvurechenski 	int i;
22295e044315SEugene (jno) Dvurechenski 
22305e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
22315e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
22325e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
22335e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
22345e044315SEugene (jno) Dvurechenski }
22355e044315SEugene (jno) Dvurechenski 
22365e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
22375e044315SEugene (jno) Dvurechenski {
22385e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
22395e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
22405e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
22415e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
22425e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
22435e044315SEugene (jno) Dvurechenski 
22445e044315SEugene (jno) Dvurechenski 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
22455e044315SEugene (jno) Dvurechenski 	if (!new_sca)
22465e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
22475e044315SEugene (jno) Dvurechenski 
22485e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
22495e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
22505e044315SEugene (jno) Dvurechenski 
22515e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
22525e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
22535e044315SEugene (jno) Dvurechenski 
22545e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
22555e044315SEugene (jno) Dvurechenski 
22565e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
22575e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
22585e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
22590c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
22605e044315SEugene (jno) Dvurechenski 	}
22615e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
22625e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
22635e044315SEugene (jno) Dvurechenski 
22645e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
22655e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
22665e044315SEugene (jno) Dvurechenski 
22675e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
22685e044315SEugene (jno) Dvurechenski 
22698335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
22708335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
22715e044315SEugene (jno) Dvurechenski 	return 0;
22727d43bafcSEugene (jno) Dvurechenski }
2273a6e2f683SEugene (jno) Dvurechenski 
2274a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2275a6e2f683SEugene (jno) Dvurechenski {
22765e044315SEugene (jno) Dvurechenski 	int rc;
22775e044315SEugene (jno) Dvurechenski 
2278a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2279a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
2280a6940674SDavid Hildenbrand 			return true;
2281a6940674SDavid Hildenbrand 		return false;
2282a6940674SDavid Hildenbrand 	}
22835e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
22845e044315SEugene (jno) Dvurechenski 		return true;
228576a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
22865e044315SEugene (jno) Dvurechenski 		return false;
22875e044315SEugene (jno) Dvurechenski 
22885e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
22895e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
22905e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
22915e044315SEugene (jno) Dvurechenski 
22925e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
2293a6e2f683SEugene (jno) Dvurechenski }
2294a6e2f683SEugene (jno) Dvurechenski 
2295dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
2296dafd032aSDominik Dingel {
2297dafd032aSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
2298dafd032aSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
229959674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
230059674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
23019eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
2302b028ee3eSDavid Hildenbrand 				    KVM_SYNC_CRS |
2303b028ee3eSDavid Hildenbrand 				    KVM_SYNC_ARCH0 |
2304b028ee3eSDavid Hildenbrand 				    KVM_SYNC_PFAULT;
230575a4615cSJulius Niedworok 	kvm_s390_set_prefix(vcpu, 0);
2306c6e5f166SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 64))
2307c6e5f166SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
230835b3fde6SChristian Borntraeger 	if (test_kvm_facility(vcpu->kvm, 82))
230935b3fde6SChristian Borntraeger 		vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
23104e0b1ab7SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 133))
23114e0b1ab7SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
2312f6aa6dc4SDavid Hildenbrand 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
2313f6aa6dc4SDavid Hildenbrand 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
2314f6aa6dc4SDavid Hildenbrand 	 */
2315f6aa6dc4SDavid Hildenbrand 	if (MACHINE_HAS_VX)
231668c55750SEric Farman 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
23176fd8e67dSDavid Hildenbrand 	else
23186fd8e67dSDavid Hildenbrand 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
2319dafd032aSDominik Dingel 
2320dafd032aSDominik Dingel 	if (kvm_is_ucontrol(vcpu->kvm))
2321dafd032aSDominik Dingel 		return __kvm_ucontrol_vcpu_init(vcpu);
2322dafd032aSDominik Dingel 
2323b0c632dbSHeiko Carstens 	return 0;
2324b0c632dbSHeiko Carstens }
2325b0c632dbSHeiko Carstens 
2326db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2327db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2328db0758b2SDavid Hildenbrand {
2329db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
23309c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2331db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
23329c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2333db0758b2SDavid Hildenbrand }
2334db0758b2SDavid Hildenbrand 
2335db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2336db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2337db0758b2SDavid Hildenbrand {
2338db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
23399c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2340db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2341db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
23429c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2343db0758b2SDavid Hildenbrand }
2344db0758b2SDavid Hildenbrand 
2345db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2346db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2347db0758b2SDavid Hildenbrand {
2348db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
2349db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
2350db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
2351db0758b2SDavid Hildenbrand }
2352db0758b2SDavid Hildenbrand 
2353db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2354db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2355db0758b2SDavid Hildenbrand {
2356db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
2357db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
2358db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
2359db0758b2SDavid Hildenbrand }
2360db0758b2SDavid Hildenbrand 
2361db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2362db0758b2SDavid Hildenbrand {
2363db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2364db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
2365db0758b2SDavid Hildenbrand 	preempt_enable();
2366db0758b2SDavid Hildenbrand }
2367db0758b2SDavid Hildenbrand 
2368db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2369db0758b2SDavid Hildenbrand {
2370db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2371db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
2372db0758b2SDavid Hildenbrand 	preempt_enable();
2373db0758b2SDavid Hildenbrand }
2374db0758b2SDavid Hildenbrand 
23754287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
23764287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
23774287f247SDavid Hildenbrand {
2378db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
23799c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2380db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
2381db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
23824287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
23839c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2384db0758b2SDavid Hildenbrand 	preempt_enable();
23854287f247SDavid Hildenbrand }
23864287f247SDavid Hildenbrand 
2387db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
23884287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
23894287f247SDavid Hildenbrand {
23909c23a131SDavid Hildenbrand 	unsigned int seq;
2391db0758b2SDavid Hildenbrand 	__u64 value;
2392db0758b2SDavid Hildenbrand 
2393db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
23944287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
2395db0758b2SDavid Hildenbrand 
23969c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
23979c23a131SDavid Hildenbrand 	do {
23989c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
23999c23a131SDavid Hildenbrand 		/*
24009c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
24019c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
24029c23a131SDavid Hildenbrand 		 */
24039c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
2404db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
24059c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
24069c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
2407db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
24089c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
24099c23a131SDavid Hildenbrand 	preempt_enable();
2410db0758b2SDavid Hildenbrand 	return value;
24114287f247SDavid Hildenbrand }
24124287f247SDavid Hildenbrand 
2413b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2414b0c632dbSHeiko Carstens {
24159977e886SHendrik Brueckner 
241637d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
2417ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
24185ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2419db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
242001a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
2421b0c632dbSHeiko Carstens }
2422b0c632dbSHeiko Carstens 
2423b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2424b0c632dbSHeiko Carstens {
242501a745acSDavid Hildenbrand 	vcpu->cpu = -1;
24265ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2427db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
24289daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
242937d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
243037d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
24319977e886SHendrik Brueckner 
2432b0c632dbSHeiko Carstens }
2433b0c632dbSHeiko Carstens 
2434b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
2435b0c632dbSHeiko Carstens {
2436b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
2437b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
2438b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
24398d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
24404287f247SDavid Hildenbrand 	kvm_s390_set_cpu_timer(vcpu, 0);
2441b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
2442b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
2443b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
2444b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
2445b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
24469abc2a08SDavid Hildenbrand 	/* make sure the new fpc will be lazily loaded */
24479abc2a08SDavid Hildenbrand 	save_fpu_regs();
24489abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = 0;
2449b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
2450672550fbSChristian Borntraeger 	vcpu->arch.sie_block->pp = 0;
245135b3fde6SChristian Borntraeger 	vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
24523c038e6bSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
24533c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
24546352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
24556852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
24562ed10cc1SJens Freimann 	kvm_s390_clear_local_irqs(vcpu);
2457b0c632dbSHeiko Carstens }
2458b0c632dbSHeiko Carstens 
245931928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
246042897d86SMarcelo Tosatti {
246172f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
2462fdf03650SFan Zhang 	preempt_disable();
246372f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
2464d16b52cbSDavid Hildenbrand 	vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
2465fdf03650SFan Zhang 	preempt_enable();
246672f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
246725508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
2468dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
2469eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
247025508824SDavid Hildenbrand 	}
24716502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
24726502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
247337d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
247437d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
247542897d86SMarcelo Tosatti }
247642897d86SMarcelo Tosatti 
24775102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
24785102ee87STony Krowiak {
24799d8d5786SMichael Mueller 	if (!test_kvm_facility(vcpu->kvm, 76))
24805102ee87STony Krowiak 		return;
24815102ee87STony Krowiak 
2482a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
2483a374e892STony Krowiak 
2484a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.aes_kw)
2485a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
2486a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
2487a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
2488a374e892STony Krowiak 
24895102ee87STony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
24905102ee87STony Krowiak }
24915102ee87STony Krowiak 
2492b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
2493b31605c1SDominik Dingel {
2494b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
2495b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
2496b31605c1SDominik Dingel }
2497b31605c1SDominik Dingel 
2498b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
2499b31605c1SDominik Dingel {
2500b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
2501b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
2502b31605c1SDominik Dingel 		return -ENOMEM;
2503b31605c1SDominik Dingel 	return 0;
2504b31605c1SDominik Dingel }
2505b31605c1SDominik Dingel 
250691520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
250791520f1aSMichael Mueller {
250891520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
250991520f1aSMichael Mueller 
251091520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
251180bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
2512c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
251391520f1aSMichael Mueller }
251491520f1aSMichael Mueller 
2515b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
2516b0c632dbSHeiko Carstens {
2517b31605c1SDominik Dingel 	int rc = 0;
2518b31288faSKonstantin Weitz 
25199e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
25209e6dabefSCornelia Huck 						    CPUSTAT_SM |
2521a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
2522a4a4f191SGuenther Hutzl 
252353df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
2524ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
252553df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
2526ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
2527a4a4f191SGuenther Hutzl 
252891520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
252991520f1aSMichael Mueller 
2530bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
2531bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
25320c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
2533bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
25340c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_SRSI;
2535f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
25360c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_TE;
25377feb6bb8SMichael Mueller 
2538c9f0a2b8SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
25390c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
2540cd1836f5SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 130))
25410c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
25420c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
254348ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
25440c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_CEI;
254511ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
25460c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_IB;
254737c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
25480c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SII;
254937c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
25500c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SIGPI;
255118280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
25520c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_VX;
25530c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
255413211ea7SEric Farman 	}
25558fa1696eSCollin L. Walling 	if (test_kvm_facility(vcpu->kvm, 139))
25568fa1696eSCollin L. Walling 		vcpu->arch.sie_block->ecd |= ECD_MEF;
25578fa1696eSCollin L. Walling 
2558d7c5cb01SMichael Mueller 	if (vcpu->arch.sie_block->gd) {
2559d7c5cb01SMichael Mueller 		vcpu->arch.sie_block->eca |= ECA_AIV;
2560d7c5cb01SMichael Mueller 		VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
2561d7c5cb01SMichael Mueller 			   vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
2562d7c5cb01SMichael Mueller 	}
25634e0b1ab7SFan Zhang 	vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
25644e0b1ab7SFan Zhang 					| SDNXC;
2565c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
2566730cd632SFarhan Ali 
2567730cd632SFarhan Ali 	if (sclp.has_kss)
2568ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
2569730cd632SFarhan Ali 	else
2570492d8642SThomas Huth 		vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
25715a5e6536SMatthew Rosato 
2572e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
2573b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
2574b31605c1SDominik Dingel 		if (rc)
2575b31605c1SDominik Dingel 			return rc;
2576b31288faSKonstantin Weitz 	}
25770ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2578ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
25799d8d5786SMichael Mueller 
25805102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
25815102ee87STony Krowiak 
2582b31605c1SDominik Dingel 	return rc;
2583b0c632dbSHeiko Carstens }
2584b0c632dbSHeiko Carstens 
2585b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
2586b0c632dbSHeiko Carstens 				      unsigned int id)
2587b0c632dbSHeiko Carstens {
25884d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
25897feb6bb8SMichael Mueller 	struct sie_page *sie_page;
25904d47555aSCarsten Otte 	int rc = -EINVAL;
2591b0c632dbSHeiko Carstens 
25924215825eSDavid Hildenbrand 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
25934d47555aSCarsten Otte 		goto out;
25944d47555aSCarsten Otte 
25954d47555aSCarsten Otte 	rc = -ENOMEM;
25964d47555aSCarsten Otte 
2597b110feafSMichael Mueller 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
2598b0c632dbSHeiko Carstens 	if (!vcpu)
25994d47555aSCarsten Otte 		goto out;
2600b0c632dbSHeiko Carstens 
2601da72ca4dSQingFeng Hao 	BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
26027feb6bb8SMichael Mueller 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
26037feb6bb8SMichael Mueller 	if (!sie_page)
2604b0c632dbSHeiko Carstens 		goto out_free_cpu;
2605b0c632dbSHeiko Carstens 
26067feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
26077feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
26087feb6bb8SMichael Mueller 
2609efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
2610efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
2611efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
2612efed1104SDavid Hildenbrand 
2613b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
2614ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
2615d7c5cb01SMichael Mueller 	vcpu->arch.sie_block->gd = (u32)(u64)kvm->arch.gisa;
26164b9f9525SMichael Mueller 	if (vcpu->arch.sie_block->gd && sclp.has_gisaf)
26174b9f9525SMichael Mueller 		vcpu->arch.sie_block->gd |= GISA_FORMAT1;
26189c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
2619ba5c1e9bSCarsten Otte 
2620b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
2621b0c632dbSHeiko Carstens 	if (rc)
26229abc2a08SDavid Hildenbrand 		goto out_free_sie_block;
26238335713aSChristian Borntraeger 	VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
2624b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
2625ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
2626b0c632dbSHeiko Carstens 
2627b0c632dbSHeiko Carstens 	return vcpu;
26287b06bf2fSWei Yongjun out_free_sie_block:
26297b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
2630b0c632dbSHeiko Carstens out_free_cpu:
2631b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
26324d47555aSCarsten Otte out:
2633b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
2634b0c632dbSHeiko Carstens }
2635b0c632dbSHeiko Carstens 
2636b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
2637b0c632dbSHeiko Carstens {
26389a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
2639b0c632dbSHeiko Carstens }
2640b0c632dbSHeiko Carstens 
2641199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
2642199b5763SLongpeng(Mike) {
26430546c63dSLongpeng(Mike) 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
2644199b5763SLongpeng(Mike) }
2645199b5763SLongpeng(Mike) 
264627406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
264749b99e1eSChristian Borntraeger {
2648805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
264961a6df54SDavid Hildenbrand 	exit_sie(vcpu);
265049b99e1eSChristian Borntraeger }
265149b99e1eSChristian Borntraeger 
265227406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
265349b99e1eSChristian Borntraeger {
2654805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
265549b99e1eSChristian Borntraeger }
265649b99e1eSChristian Borntraeger 
26578e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
26588e236546SChristian Borntraeger {
2659805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
266061a6df54SDavid Hildenbrand 	exit_sie(vcpu);
26618e236546SChristian Borntraeger }
26628e236546SChristian Borntraeger 
26638e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
26648e236546SChristian Borntraeger {
26659bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
26668e236546SChristian Borntraeger }
26678e236546SChristian Borntraeger 
266849b99e1eSChristian Borntraeger /*
266949b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
267049b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
267149b99e1eSChristian Borntraeger  * return immediately. */
267249b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
267349b99e1eSChristian Borntraeger {
2674ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
267549b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
267649b99e1eSChristian Borntraeger 		cpu_relax();
267749b99e1eSChristian Borntraeger }
267849b99e1eSChristian Borntraeger 
26798e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
26808e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
268149b99e1eSChristian Borntraeger {
26828e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
26838e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
268449b99e1eSChristian Borntraeger }
268549b99e1eSChristian Borntraeger 
2686414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
2687414d3b07SMartin Schwidefsky 			      unsigned long end)
26882c70fe44SChristian Borntraeger {
26892c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
26902c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
2691414d3b07SMartin Schwidefsky 	unsigned long prefix;
2692414d3b07SMartin Schwidefsky 	int i;
26932c70fe44SChristian Borntraeger 
269465d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
269565d0b0d4SDavid Hildenbrand 		return;
2696414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
2697414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
2698414d3b07SMartin Schwidefsky 		return;
26992c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
27002c70fe44SChristian Borntraeger 		/* match against both prefix pages */
2701414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
2702414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
2703414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
2704414d3b07SMartin Schwidefsky 				   start, end);
27058e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
27062c70fe44SChristian Borntraeger 		}
27072c70fe44SChristian Borntraeger 	}
27082c70fe44SChristian Borntraeger }
27092c70fe44SChristian Borntraeger 
2710b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
2711b6d33834SChristoffer Dall {
2712b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
2713b6d33834SChristoffer Dall 	BUG();
2714b6d33834SChristoffer Dall 	return 0;
2715b6d33834SChristoffer Dall }
2716b6d33834SChristoffer Dall 
271714eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
271814eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
271914eebd91SCarsten Otte {
272014eebd91SCarsten Otte 	int r = -EINVAL;
272114eebd91SCarsten Otte 
272214eebd91SCarsten Otte 	switch (reg->id) {
272329b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
272429b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
272529b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
272629b7c71bSCarsten Otte 		break;
272729b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
272829b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
272929b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
273029b7c71bSCarsten Otte 		break;
273146a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
27324287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
273346a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
273446a6dd1cSJason J. herne 		break;
273546a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
273646a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
273746a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
273846a6dd1cSJason J. herne 		break;
2739536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2740536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
2741536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2742536336c2SDominik Dingel 		break;
2743536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2744536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
2745536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2746536336c2SDominik Dingel 		break;
2747536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2748536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
2749536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2750536336c2SDominik Dingel 		break;
2751672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2752672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
2753672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2754672550fbSChristian Borntraeger 		break;
2755afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2756afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
2757afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2758afa45ff5SChristian Borntraeger 		break;
275914eebd91SCarsten Otte 	default:
276014eebd91SCarsten Otte 		break;
276114eebd91SCarsten Otte 	}
276214eebd91SCarsten Otte 
276314eebd91SCarsten Otte 	return r;
276414eebd91SCarsten Otte }
276514eebd91SCarsten Otte 
276614eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
276714eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
276814eebd91SCarsten Otte {
276914eebd91SCarsten Otte 	int r = -EINVAL;
27704287f247SDavid Hildenbrand 	__u64 val;
277114eebd91SCarsten Otte 
277214eebd91SCarsten Otte 	switch (reg->id) {
277329b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
277429b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
277529b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
277629b7c71bSCarsten Otte 		break;
277729b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
277829b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
277929b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
278029b7c71bSCarsten Otte 		break;
278146a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
27824287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
27834287f247SDavid Hildenbrand 		if (!r)
27844287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
278546a6dd1cSJason J. herne 		break;
278646a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
278746a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
278846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
278946a6dd1cSJason J. herne 		break;
2790536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2791536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
2792536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
27939fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
27949fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2795536336c2SDominik Dingel 		break;
2796536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2797536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
2798536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2799536336c2SDominik Dingel 		break;
2800536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2801536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
2802536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2803536336c2SDominik Dingel 		break;
2804672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2805672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
2806672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2807672550fbSChristian Borntraeger 		break;
2808afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2809afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
2810afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2811afa45ff5SChristian Borntraeger 		break;
281214eebd91SCarsten Otte 	default:
281314eebd91SCarsten Otte 		break;
281414eebd91SCarsten Otte 	}
281514eebd91SCarsten Otte 
281614eebd91SCarsten Otte 	return r;
281714eebd91SCarsten Otte }
2818b6d33834SChristoffer Dall 
2819b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
2820b0c632dbSHeiko Carstens {
2821b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
2822b0c632dbSHeiko Carstens 	return 0;
2823b0c632dbSHeiko Carstens }
2824b0c632dbSHeiko Carstens 
2825b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2826b0c632dbSHeiko Carstens {
2827875656feSChristoffer Dall 	vcpu_load(vcpu);
28285a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
2829875656feSChristoffer Dall 	vcpu_put(vcpu);
2830b0c632dbSHeiko Carstens 	return 0;
2831b0c632dbSHeiko Carstens }
2832b0c632dbSHeiko Carstens 
2833b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2834b0c632dbSHeiko Carstens {
28351fc9b76bSChristoffer Dall 	vcpu_load(vcpu);
28365a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
28371fc9b76bSChristoffer Dall 	vcpu_put(vcpu);
2838b0c632dbSHeiko Carstens 	return 0;
2839b0c632dbSHeiko Carstens }
2840b0c632dbSHeiko Carstens 
2841b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2842b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2843b0c632dbSHeiko Carstens {
2844b4ef9d4eSChristoffer Dall 	vcpu_load(vcpu);
2845b4ef9d4eSChristoffer Dall 
284659674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
2847b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
2848b4ef9d4eSChristoffer Dall 
2849b4ef9d4eSChristoffer Dall 	vcpu_put(vcpu);
2850b0c632dbSHeiko Carstens 	return 0;
2851b0c632dbSHeiko Carstens }
2852b0c632dbSHeiko Carstens 
2853b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2854b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2855b0c632dbSHeiko Carstens {
2856bcdec41cSChristoffer Dall 	vcpu_load(vcpu);
2857bcdec41cSChristoffer Dall 
285859674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
2859b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
2860bcdec41cSChristoffer Dall 
2861bcdec41cSChristoffer Dall 	vcpu_put(vcpu);
2862b0c632dbSHeiko Carstens 	return 0;
2863b0c632dbSHeiko Carstens }
2864b0c632dbSHeiko Carstens 
2865b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2866b0c632dbSHeiko Carstens {
28676a96bc7fSChristoffer Dall 	int ret = 0;
28686a96bc7fSChristoffer Dall 
28696a96bc7fSChristoffer Dall 	vcpu_load(vcpu);
28706a96bc7fSChristoffer Dall 
28716a96bc7fSChristoffer Dall 	if (test_fp_ctl(fpu->fpc)) {
28726a96bc7fSChristoffer Dall 		ret = -EINVAL;
28736a96bc7fSChristoffer Dall 		goto out;
28746a96bc7fSChristoffer Dall 	}
2875e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
28769abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2877a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
2878a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
28799abc2a08SDavid Hildenbrand 	else
2880a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
28816a96bc7fSChristoffer Dall 
28826a96bc7fSChristoffer Dall out:
28836a96bc7fSChristoffer Dall 	vcpu_put(vcpu);
28846a96bc7fSChristoffer Dall 	return ret;
2885b0c632dbSHeiko Carstens }
2886b0c632dbSHeiko Carstens 
2887b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2888b0c632dbSHeiko Carstens {
28891393123eSChristoffer Dall 	vcpu_load(vcpu);
28901393123eSChristoffer Dall 
28919abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
28929abc2a08SDavid Hildenbrand 	save_fpu_regs();
28939abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2894a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
2895a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
28969abc2a08SDavid Hildenbrand 	else
2897a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
2898e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
28991393123eSChristoffer Dall 
29001393123eSChristoffer Dall 	vcpu_put(vcpu);
2901b0c632dbSHeiko Carstens 	return 0;
2902b0c632dbSHeiko Carstens }
2903b0c632dbSHeiko Carstens 
2904b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
2905b0c632dbSHeiko Carstens {
2906b0c632dbSHeiko Carstens 	int rc = 0;
2907b0c632dbSHeiko Carstens 
29087a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
2909b0c632dbSHeiko Carstens 		rc = -EBUSY;
2910d7b0b5ebSCarsten Otte 	else {
2911d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
2912d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
2913d7b0b5ebSCarsten Otte 	}
2914b0c632dbSHeiko Carstens 	return rc;
2915b0c632dbSHeiko Carstens }
2916b0c632dbSHeiko Carstens 
2917b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2918b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
2919b0c632dbSHeiko Carstens {
2920b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
2921b0c632dbSHeiko Carstens }
2922b0c632dbSHeiko Carstens 
292327291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
292427291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
292527291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
292627291e21SDavid Hildenbrand 
2927d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2928d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
2929b0c632dbSHeiko Carstens {
293027291e21SDavid Hildenbrand 	int rc = 0;
293127291e21SDavid Hildenbrand 
293266b56562SChristoffer Dall 	vcpu_load(vcpu);
293366b56562SChristoffer Dall 
293427291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
293527291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
293627291e21SDavid Hildenbrand 
293766b56562SChristoffer Dall 	if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
293866b56562SChristoffer Dall 		rc = -EINVAL;
293966b56562SChristoffer Dall 		goto out;
294066b56562SChristoffer Dall 	}
294166b56562SChristoffer Dall 	if (!sclp.has_gpere) {
294266b56562SChristoffer Dall 		rc = -EINVAL;
294366b56562SChristoffer Dall 		goto out;
294466b56562SChristoffer Dall 	}
294527291e21SDavid Hildenbrand 
294627291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
294727291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
294827291e21SDavid Hildenbrand 		/* enforce guest PER */
2949ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
295027291e21SDavid Hildenbrand 
295127291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
295227291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
295327291e21SDavid Hildenbrand 	} else {
29549daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
295527291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
295627291e21SDavid Hildenbrand 	}
295727291e21SDavid Hildenbrand 
295827291e21SDavid Hildenbrand 	if (rc) {
295927291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
296027291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
29619daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
296227291e21SDavid Hildenbrand 	}
296327291e21SDavid Hildenbrand 
296466b56562SChristoffer Dall out:
296566b56562SChristoffer Dall 	vcpu_put(vcpu);
296627291e21SDavid Hildenbrand 	return rc;
2967b0c632dbSHeiko Carstens }
2968b0c632dbSHeiko Carstens 
296962d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
297062d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
297162d9f0dbSMarcelo Tosatti {
2972fd232561SChristoffer Dall 	int ret;
2973fd232561SChristoffer Dall 
2974fd232561SChristoffer Dall 	vcpu_load(vcpu);
2975fd232561SChristoffer Dall 
29766352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
2977fd232561SChristoffer Dall 	ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
29786352e4d2SDavid Hildenbrand 				      KVM_MP_STATE_OPERATING;
2979fd232561SChristoffer Dall 
2980fd232561SChristoffer Dall 	vcpu_put(vcpu);
2981fd232561SChristoffer Dall 	return ret;
298262d9f0dbSMarcelo Tosatti }
298362d9f0dbSMarcelo Tosatti 
298462d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
298562d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
298662d9f0dbSMarcelo Tosatti {
29876352e4d2SDavid Hildenbrand 	int rc = 0;
29886352e4d2SDavid Hildenbrand 
2989e83dff5eSChristoffer Dall 	vcpu_load(vcpu);
2990e83dff5eSChristoffer Dall 
29916352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
29926352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
29936352e4d2SDavid Hildenbrand 
29946352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
29956352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
29966352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
29976352e4d2SDavid Hildenbrand 		break;
29986352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
29996352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
30006352e4d2SDavid Hildenbrand 		break;
30016352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
30026352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
30036352e4d2SDavid Hildenbrand 		/* fall through - CHECK_STOP and LOAD are not supported yet */
30046352e4d2SDavid Hildenbrand 	default:
30056352e4d2SDavid Hildenbrand 		rc = -ENXIO;
30066352e4d2SDavid Hildenbrand 	}
30076352e4d2SDavid Hildenbrand 
3008e83dff5eSChristoffer Dall 	vcpu_put(vcpu);
30096352e4d2SDavid Hildenbrand 	return rc;
301062d9f0dbSMarcelo Tosatti }
301162d9f0dbSMarcelo Tosatti 
30128ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
30138ad35755SDavid Hildenbrand {
30148d5fb0dcSDavid Hildenbrand 	return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
30158ad35755SDavid Hildenbrand }
30168ad35755SDavid Hildenbrand 
30172c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
30182c70fe44SChristian Borntraeger {
30198ad35755SDavid Hildenbrand retry:
30208e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
30212fa6e1e1SRadim Krčmář 	if (!kvm_request_pending(vcpu))
3022586b7ccdSChristian Borntraeger 		return 0;
30232c70fe44SChristian Borntraeger 	/*
30242c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
3025b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
30262c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
30272c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
30282c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
30292c70fe44SChristian Borntraeger 	 */
30308ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
30312c70fe44SChristian Borntraeger 		int rc;
3032b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
3033fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
3034b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
3035aca411a4SJulius Niedworok 		if (rc) {
3036aca411a4SJulius Niedworok 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
30372c70fe44SChristian Borntraeger 			return rc;
3038aca411a4SJulius Niedworok 		}
30398ad35755SDavid Hildenbrand 		goto retry;
30402c70fe44SChristian Borntraeger 	}
30418ad35755SDavid Hildenbrand 
3042d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
3043d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
3044d3d692c8SDavid Hildenbrand 		goto retry;
3045d3d692c8SDavid Hildenbrand 	}
3046d3d692c8SDavid Hildenbrand 
30478ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
30488ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
30498ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
3050ef8f4f49SDavid Hildenbrand 			kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
30518ad35755SDavid Hildenbrand 		}
30528ad35755SDavid Hildenbrand 		goto retry;
30538ad35755SDavid Hildenbrand 	}
30548ad35755SDavid Hildenbrand 
30558ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
30568ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
30578ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
30589daecfc6SDavid Hildenbrand 			kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
30598ad35755SDavid Hildenbrand 		}
30608ad35755SDavid Hildenbrand 		goto retry;
30618ad35755SDavid Hildenbrand 	}
30628ad35755SDavid Hildenbrand 
30636502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
30646502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
30656502a34cSDavid Hildenbrand 		goto retry;
30666502a34cSDavid Hildenbrand 	}
30676502a34cSDavid Hildenbrand 
3068190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
3069190df4a2SClaudio Imbrenda 		/*
3070c9f0a2b8SJanosch Frank 		 * Disable CMM virtualization; we will emulate the ESSA
3071190df4a2SClaudio Imbrenda 		 * instruction manually, in order to provide additional
3072190df4a2SClaudio Imbrenda 		 * functionalities needed for live migration.
3073190df4a2SClaudio Imbrenda 		 */
3074190df4a2SClaudio Imbrenda 		vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
3075190df4a2SClaudio Imbrenda 		goto retry;
3076190df4a2SClaudio Imbrenda 	}
3077190df4a2SClaudio Imbrenda 
3078190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
3079190df4a2SClaudio Imbrenda 		/*
3080c9f0a2b8SJanosch Frank 		 * Re-enable CMM virtualization if CMMA is available and
3081c9f0a2b8SJanosch Frank 		 * CMM has been used.
3082190df4a2SClaudio Imbrenda 		 */
3083190df4a2SClaudio Imbrenda 		if ((vcpu->kvm->arch.use_cmma) &&
3084c9f0a2b8SJanosch Frank 		    (vcpu->kvm->mm->context.uses_cmm))
3085190df4a2SClaudio Imbrenda 			vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
3086190df4a2SClaudio Imbrenda 		goto retry;
3087190df4a2SClaudio Imbrenda 	}
3088190df4a2SClaudio Imbrenda 
30890759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
309072875d8aSRadim Krčmář 	kvm_clear_request(KVM_REQ_UNHALT, vcpu);
30910759d068SDavid Hildenbrand 
30922c70fe44SChristian Borntraeger 	return 0;
30932c70fe44SChristian Borntraeger }
30942c70fe44SChristian Borntraeger 
30950e7def5fSDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm,
30968fa1696eSCollin L. Walling 			    const struct kvm_s390_vm_tod_clock *gtod)
30978fa1696eSCollin L. Walling {
30988fa1696eSCollin L. Walling 	struct kvm_vcpu *vcpu;
30998fa1696eSCollin L. Walling 	struct kvm_s390_tod_clock_ext htod;
31008fa1696eSCollin L. Walling 	int i;
31018fa1696eSCollin L. Walling 
31028fa1696eSCollin L. Walling 	mutex_lock(&kvm->lock);
31038fa1696eSCollin L. Walling 	preempt_disable();
31048fa1696eSCollin L. Walling 
31058fa1696eSCollin L. Walling 	get_tod_clock_ext((char *)&htod);
31068fa1696eSCollin L. Walling 
31078fa1696eSCollin L. Walling 	kvm->arch.epoch = gtod->tod - htod.tod;
31080e7def5fSDavid Hildenbrand 	kvm->arch.epdx = 0;
31090e7def5fSDavid Hildenbrand 	if (test_kvm_facility(kvm, 139)) {
31108fa1696eSCollin L. Walling 		kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx;
31118fa1696eSCollin L. Walling 		if (kvm->arch.epoch > gtod->tod)
31128fa1696eSCollin L. Walling 			kvm->arch.epdx -= 1;
31130e7def5fSDavid Hildenbrand 	}
31148fa1696eSCollin L. Walling 
31158fa1696eSCollin L. Walling 	kvm_s390_vcpu_block_all(kvm);
31168fa1696eSCollin L. Walling 	kvm_for_each_vcpu(i, vcpu, kvm) {
31178fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
31188fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epdx  = kvm->arch.epdx;
31198fa1696eSCollin L. Walling 	}
31208fa1696eSCollin L. Walling 
31218fa1696eSCollin L. Walling 	kvm_s390_vcpu_unblock_all(kvm);
31228fa1696eSCollin L. Walling 	preempt_enable();
31238fa1696eSCollin L. Walling 	mutex_unlock(&kvm->lock);
31248fa1696eSCollin L. Walling }
31258fa1696eSCollin L. Walling 
3126fa576c58SThomas Huth /**
3127fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
3128fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
3129fa576c58SThomas Huth  * @gpa: Guest physical address
3130fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
3131fa576c58SThomas Huth  *
3132fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
3133fa576c58SThomas Huth  *
3134fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
3135fa576c58SThomas Huth  */
3136fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
313724eb3a82SDominik Dingel {
3138527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
3139527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
314024eb3a82SDominik Dingel }
314124eb3a82SDominik Dingel 
31423c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
31433c038e6bSDominik Dingel 				      unsigned long token)
31443c038e6bSDominik Dingel {
31453c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
3146383d0b05SJens Freimann 	struct kvm_s390_irq irq;
31473c038e6bSDominik Dingel 
31483c038e6bSDominik Dingel 	if (start_token) {
3149383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
3150383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
3151383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
31523c038e6bSDominik Dingel 	} else {
31533c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
3154383d0b05SJens Freimann 		inti.parm64 = token;
31553c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
31563c038e6bSDominik Dingel 	}
31573c038e6bSDominik Dingel }
31583c038e6bSDominik Dingel 
31593c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
31603c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
31613c038e6bSDominik Dingel {
31623c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
31633c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
31643c038e6bSDominik Dingel }
31653c038e6bSDominik Dingel 
31663c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
31673c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
31683c038e6bSDominik Dingel {
31693c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
31703c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
31713c038e6bSDominik Dingel }
31723c038e6bSDominik Dingel 
31733c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
31743c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
31753c038e6bSDominik Dingel {
31763c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
31773c038e6bSDominik Dingel }
31783c038e6bSDominik Dingel 
31793c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
31803c038e6bSDominik Dingel {
31813c038e6bSDominik Dingel 	/*
31823c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
31833c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
31843c038e6bSDominik Dingel 	 */
31853c038e6bSDominik Dingel 	return true;
31863c038e6bSDominik Dingel }
31873c038e6bSDominik Dingel 
31883c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
31893c038e6bSDominik Dingel {
31903c038e6bSDominik Dingel 	hva_t hva;
31913c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
31923c038e6bSDominik Dingel 	int rc;
31933c038e6bSDominik Dingel 
31943c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
31953c038e6bSDominik Dingel 		return 0;
31963c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
31973c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
31983c038e6bSDominik Dingel 		return 0;
31993c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
32003c038e6bSDominik Dingel 		return 0;
32019a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
32023c038e6bSDominik Dingel 		return 0;
32033c038e6bSDominik Dingel 	if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
32043c038e6bSDominik Dingel 		return 0;
32053c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
32063c038e6bSDominik Dingel 		return 0;
32073c038e6bSDominik Dingel 
320881480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
320981480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
321081480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
32113c038e6bSDominik Dingel 		return 0;
32123c038e6bSDominik Dingel 
32133c038e6bSDominik Dingel 	rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
32143c038e6bSDominik Dingel 	return rc;
32153c038e6bSDominik Dingel }
32163c038e6bSDominik Dingel 
32173fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
3218b0c632dbSHeiko Carstens {
32193fb4c40fSThomas Huth 	int rc, cpuflags;
3220e168bf8dSCarsten Otte 
32213c038e6bSDominik Dingel 	/*
32223c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
32233c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
32243c038e6bSDominik Dingel 	 * handled outside the worker.
32253c038e6bSDominik Dingel 	 */
32263c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
32273c038e6bSDominik Dingel 
32287ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
32297ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
3230b0c632dbSHeiko Carstens 
3231b0c632dbSHeiko Carstens 	if (need_resched())
3232b0c632dbSHeiko Carstens 		schedule();
3233b0c632dbSHeiko Carstens 
3234d3a73acbSMartin Schwidefsky 	if (test_cpu_flag(CIF_MCCK_PENDING))
323571cde587SChristian Borntraeger 		s390_handle_mcck();
323671cde587SChristian Borntraeger 
323779395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
323879395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
323979395031SJens Freimann 		if (rc)
324079395031SJens Freimann 			return rc;
324179395031SJens Freimann 	}
32420ff31867SCarsten Otte 
32432c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
32442c70fe44SChristian Borntraeger 	if (rc)
32452c70fe44SChristian Borntraeger 		return rc;
32462c70fe44SChristian Borntraeger 
324727291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
324827291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
324927291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
325027291e21SDavid Hildenbrand 	}
325127291e21SDavid Hildenbrand 
3252b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
32533fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
32543fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
32553fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
32562b29a9fdSDominik Dingel 
32573fb4c40fSThomas Huth 	return 0;
32583fb4c40fSThomas Huth }
32593fb4c40fSThomas Huth 
3260492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
3261492d8642SThomas Huth {
326256317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
326356317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
326456317920SDavid Hildenbrand 	};
326556317920SDavid Hildenbrand 	u8 opcode, ilen;
3266492d8642SThomas Huth 	int rc;
3267492d8642SThomas Huth 
3268492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
3269492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
3270492d8642SThomas Huth 
3271492d8642SThomas Huth 	/*
3272492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
3273492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
3274492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
3275492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
3276492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
3277492d8642SThomas Huth 	 * to be able to forward the PSW.
3278492d8642SThomas Huth 	 */
32793fa8cad7SDavid Hildenbrand 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
328056317920SDavid Hildenbrand 	ilen = insn_length(opcode);
32819b0d721aSDavid Hildenbrand 	if (rc < 0) {
32829b0d721aSDavid Hildenbrand 		return rc;
32839b0d721aSDavid Hildenbrand 	} else if (rc) {
32849b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
32859b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
32869b0d721aSDavid Hildenbrand 		 * nullification if necessary.
32879b0d721aSDavid Hildenbrand 		 */
32889b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
32899b0d721aSDavid Hildenbrand 		ilen = 4;
32909b0d721aSDavid Hildenbrand 	}
329156317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
329256317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
329356317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
3294492d8642SThomas Huth }
3295492d8642SThomas Huth 
32963fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
32973fb4c40fSThomas Huth {
32984d62fcc0SQingFeng Hao 	struct mcck_volatile_info *mcck_info;
32994d62fcc0SQingFeng Hao 	struct sie_page *sie_page;
33004d62fcc0SQingFeng Hao 
33012b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
33022b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
33032b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
33042b29a9fdSDominik Dingel 
330527291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
330627291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
330727291e21SDavid Hildenbrand 
33087ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
33097ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
331071f116bfSDavid Hildenbrand 
33114d62fcc0SQingFeng Hao 	if (exit_reason == -EINTR) {
33124d62fcc0SQingFeng Hao 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
33134d62fcc0SQingFeng Hao 		sie_page = container_of(vcpu->arch.sie_block,
33144d62fcc0SQingFeng Hao 					struct sie_page, sie_block);
33154d62fcc0SQingFeng Hao 		mcck_info = &sie_page->mcck_info;
33164d62fcc0SQingFeng Hao 		kvm_s390_reinject_machine_check(vcpu, mcck_info);
33174d62fcc0SQingFeng Hao 		return 0;
33184d62fcc0SQingFeng Hao 	}
33194d62fcc0SQingFeng Hao 
332071f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
332171f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
332271f116bfSDavid Hildenbrand 
332371f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
332471f116bfSDavid Hildenbrand 			return rc;
332571f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
332671f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
332771f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
332871f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
332971f116bfSDavid Hildenbrand 		return -EREMOTE;
333071f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
333171f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
333271f116bfSDavid Hildenbrand 		return 0;
3333210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
3334210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
3335210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
3336210b1607SThomas Huth 						current->thread.gmap_addr;
3337210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
333871f116bfSDavid Hildenbrand 		return -EREMOTE;
333924eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
33403c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
334124eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
334271f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
334371f116bfSDavid Hildenbrand 			return 0;
334471f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
3345fa576c58SThomas Huth 	}
334671f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
33473fb4c40fSThomas Huth }
33483fb4c40fSThomas Huth 
33493fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
33503fb4c40fSThomas Huth {
33513fb4c40fSThomas Huth 	int rc, exit_reason;
33523fb4c40fSThomas Huth 
3353800c1065SThomas Huth 	/*
3354800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
3355800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
3356800c1065SThomas Huth 	 */
3357800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
3358800c1065SThomas Huth 
3359a76ccff6SThomas Huth 	do {
33603fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
33613fb4c40fSThomas Huth 		if (rc)
3362a76ccff6SThomas Huth 			break;
33633fb4c40fSThomas Huth 
3364800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
33653fb4c40fSThomas Huth 		/*
3366a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
3367a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
33683fb4c40fSThomas Huth 		 */
33690097d12eSChristian Borntraeger 		local_irq_disable();
33706edaa530SPaolo Bonzini 		guest_enter_irqoff();
3371db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
33720097d12eSChristian Borntraeger 		local_irq_enable();
3373a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
3374a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
33750097d12eSChristian Borntraeger 		local_irq_disable();
3376db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
33776edaa530SPaolo Bonzini 		guest_exit_irqoff();
33780097d12eSChristian Borntraeger 		local_irq_enable();
3379800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
33803fb4c40fSThomas Huth 
33813fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
338227291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
33833fb4c40fSThomas Huth 
3384800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
3385e168bf8dSCarsten Otte 	return rc;
3386b0c632dbSHeiko Carstens }
3387b0c632dbSHeiko Carstens 
3388b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3389b028ee3eSDavid Hildenbrand {
33904d5f2c04SChristian Borntraeger 	struct runtime_instr_cb *riccb;
33914e0b1ab7SFan Zhang 	struct gs_cb *gscb;
33924d5f2c04SChristian Borntraeger 
33934d5f2c04SChristian Borntraeger 	riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
33944e0b1ab7SFan Zhang 	gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
3395b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
3396b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
3397b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
3398b028ee3eSDavid Hildenbrand 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
3399b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
3400b028ee3eSDavid Hildenbrand 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
3401d3d692c8SDavid Hildenbrand 		/* some control register changes require a tlb flush */
3402d3d692c8SDavid Hildenbrand 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3403b028ee3eSDavid Hildenbrand 	}
3404b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
34054287f247SDavid Hildenbrand 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
3406b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
3407b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
3408b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
3409b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
3410b028ee3eSDavid Hildenbrand 	}
3411b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
3412b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
3413b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
3414b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
34159fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
34169fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
3417b028ee3eSDavid Hildenbrand 	}
341880cd8763SFan Zhang 	/*
341980cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
342080cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
342180cd8763SFan Zhang 	 */
342280cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
34234d5f2c04SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 64) &&
3424bb59c2daSAlice Frosi 	    riccb->v &&
34250c9d8683SDavid Hildenbrand 	    !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
34264d5f2c04SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
34270c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
342880cd8763SFan Zhang 	}
34294e0b1ab7SFan Zhang 	/*
34304e0b1ab7SFan Zhang 	 * If userspace sets the gscb (e.g. after migration) to non-zero,
34314e0b1ab7SFan Zhang 	 * we should enable GS here instead of doing the lazy enablement.
34324e0b1ab7SFan Zhang 	 */
34334e0b1ab7SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
34344e0b1ab7SFan Zhang 	    test_kvm_facility(vcpu->kvm, 133) &&
34354e0b1ab7SFan Zhang 	    gscb->gssm &&
34364e0b1ab7SFan Zhang 	    !vcpu->arch.gs_enabled) {
34374e0b1ab7SFan Zhang 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
34384e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecb |= ECB_GS;
34394e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
34404e0b1ab7SFan Zhang 		vcpu->arch.gs_enabled = 1;
344180cd8763SFan Zhang 	}
344235b3fde6SChristian Borntraeger 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
344335b3fde6SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 82)) {
344435b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
344535b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
344635b3fde6SChristian Borntraeger 	}
344731d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->arch.host_acrs);
344831d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
3449e1788bb9SChristian Borntraeger 	/* save host (userspace) fprs/vrs */
3450e1788bb9SChristian Borntraeger 	save_fpu_regs();
3451e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
3452e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
3453e1788bb9SChristian Borntraeger 	if (MACHINE_HAS_VX)
3454e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
3455e1788bb9SChristian Borntraeger 	else
3456e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
3457e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
3458e1788bb9SChristian Borntraeger 	if (test_fp_ctl(current->thread.fpu.fpc))
3459e1788bb9SChristian Borntraeger 		/* User space provided an invalid FPC, let's clear it */
3460e1788bb9SChristian Borntraeger 		current->thread.fpu.fpc = 0;
34614e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
34624e0b1ab7SFan Zhang 		preempt_disable();
34634e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
34644e0b1ab7SFan Zhang 		if (current->thread.gs_cb) {
34654e0b1ab7SFan Zhang 			vcpu->arch.host_gscb = current->thread.gs_cb;
34664e0b1ab7SFan Zhang 			save_gs_cb(vcpu->arch.host_gscb);
34674e0b1ab7SFan Zhang 		}
34684e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled) {
34694e0b1ab7SFan Zhang 			current->thread.gs_cb = (struct gs_cb *)
34704e0b1ab7SFan Zhang 						&vcpu->run->s.regs.gscb;
34714e0b1ab7SFan Zhang 			restore_gs_cb(current->thread.gs_cb);
34724e0b1ab7SFan Zhang 		}
34734e0b1ab7SFan Zhang 		preempt_enable();
34744e0b1ab7SFan Zhang 	}
347580cd8763SFan Zhang 
3476b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
3477b028ee3eSDavid Hildenbrand }
3478b028ee3eSDavid Hildenbrand 
3479b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3480b028ee3eSDavid Hildenbrand {
3481b028ee3eSDavid Hildenbrand 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
3482b028ee3eSDavid Hildenbrand 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
3483b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
3484b028ee3eSDavid Hildenbrand 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
34854287f247SDavid Hildenbrand 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
3486b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
3487b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
3488b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
3489b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
3490b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
3491b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
3492b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
349335b3fde6SChristian Borntraeger 	kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
349431d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
349531d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->arch.host_acrs);
3496e1788bb9SChristian Borntraeger 	/* Save guest register state */
3497e1788bb9SChristian Borntraeger 	save_fpu_regs();
3498e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3499e1788bb9SChristian Borntraeger 	/* Restore will be done lazily at return */
3500e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
3501e1788bb9SChristian Borntraeger 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
35024e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
35034e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
35044e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled)
35054e0b1ab7SFan Zhang 			save_gs_cb(current->thread.gs_cb);
35064e0b1ab7SFan Zhang 		preempt_disable();
35074e0b1ab7SFan Zhang 		current->thread.gs_cb = vcpu->arch.host_gscb;
35084e0b1ab7SFan Zhang 		restore_gs_cb(vcpu->arch.host_gscb);
35094e0b1ab7SFan Zhang 		preempt_enable();
35104e0b1ab7SFan Zhang 		if (!vcpu->arch.host_gscb)
35114e0b1ab7SFan Zhang 			__ctl_clear_bit(2, 4);
35124e0b1ab7SFan Zhang 		vcpu->arch.host_gscb = NULL;
35134e0b1ab7SFan Zhang 	}
3514e1788bb9SChristian Borntraeger 
3515b028ee3eSDavid Hildenbrand }
3516b028ee3eSDavid Hildenbrand 
3517b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3518b0c632dbSHeiko Carstens {
35198f2abe6aSChristian Borntraeger 	int rc;
3520b0c632dbSHeiko Carstens 
3521460df4c1SPaolo Bonzini 	if (kvm_run->immediate_exit)
3522460df4c1SPaolo Bonzini 		return -EINTR;
3523460df4c1SPaolo Bonzini 
3524accb757dSChristoffer Dall 	vcpu_load(vcpu);
3525accb757dSChristoffer Dall 
352627291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
352727291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
3528accb757dSChristoffer Dall 		rc = 0;
3529accb757dSChristoffer Dall 		goto out;
353027291e21SDavid Hildenbrand 	}
353127291e21SDavid Hildenbrand 
353220b7035cSJan H. Schönherr 	kvm_sigset_activate(vcpu);
3533b0c632dbSHeiko Carstens 
35346352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
35356852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
35366352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
3537ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
35386352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
3539accb757dSChristoffer Dall 		rc = -EINVAL;
3540accb757dSChristoffer Dall 		goto out;
35416352e4d2SDavid Hildenbrand 	}
3542b0c632dbSHeiko Carstens 
3543b028ee3eSDavid Hildenbrand 	sync_regs(vcpu, kvm_run);
3544db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
3545d7b0b5ebSCarsten Otte 
3546dab4079dSHeiko Carstens 	might_fault();
3547e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
35489ace903dSChristian Ehrhardt 
3549b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
3550b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
35518f2abe6aSChristian Borntraeger 		rc = -EINTR;
3552b1d16c49SChristian Ehrhardt 	}
35538f2abe6aSChristian Borntraeger 
355427291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
355527291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
355627291e21SDavid Hildenbrand 		rc = 0;
355727291e21SDavid Hildenbrand 	}
355827291e21SDavid Hildenbrand 
35598f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
356071f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
35618f2abe6aSChristian Borntraeger 		rc = 0;
35628f2abe6aSChristian Borntraeger 	}
35638f2abe6aSChristian Borntraeger 
3564db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
3565b028ee3eSDavid Hildenbrand 	store_regs(vcpu, kvm_run);
3566d7b0b5ebSCarsten Otte 
356720b7035cSJan H. Schönherr 	kvm_sigset_deactivate(vcpu);
3568b0c632dbSHeiko Carstens 
3569b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
3570accb757dSChristoffer Dall out:
3571accb757dSChristoffer Dall 	vcpu_put(vcpu);
35727e8e6ab4SHeiko Carstens 	return rc;
3573b0c632dbSHeiko Carstens }
3574b0c632dbSHeiko Carstens 
3575b0c632dbSHeiko Carstens /*
3576b0c632dbSHeiko Carstens  * store status at address
3577b0c632dbSHeiko Carstens  * we use have two special cases:
3578b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
3579b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
3580b0c632dbSHeiko Carstens  */
3581d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
3582b0c632dbSHeiko Carstens {
3583092670cdSCarsten Otte 	unsigned char archmode = 1;
35849abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
3585fda902cbSMichael Mueller 	unsigned int px;
35864287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
3587d0bce605SHeiko Carstens 	int rc;
3588b0c632dbSHeiko Carstens 
3589d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
3590d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
3591d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
3592b0c632dbSHeiko Carstens 			return -EFAULT;
3593d9a3a09aSMartin Schwidefsky 		gpa = 0;
3594d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
3595d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
3596b0c632dbSHeiko Carstens 			return -EFAULT;
3597d9a3a09aSMartin Schwidefsky 		gpa = px;
3598d9a3a09aSMartin Schwidefsky 	} else
3599d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
36009abc2a08SDavid Hildenbrand 
36019abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
36029abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
36039522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
3604d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
36059abc2a08SDavid Hildenbrand 				     fprs, 128);
36069abc2a08SDavid Hildenbrand 	} else {
36079abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
36086fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
36099abc2a08SDavid Hildenbrand 	}
3610d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
3611d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
3612d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
3613d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
3614d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
3615fda902cbSMichael Mueller 			      &px, 4);
3616d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
36179abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
3618d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
3619d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
36204287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
3621d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
36224287f247SDavid Hildenbrand 			      &cputm, 8);
3623178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
3624d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
3625d0bce605SHeiko Carstens 			      &clkcomp, 8);
3626d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
3627d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
3628d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
3629d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
3630d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
3631b0c632dbSHeiko Carstens }
3632b0c632dbSHeiko Carstens 
3633e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
3634e879892cSThomas Huth {
3635e879892cSThomas Huth 	/*
3636e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
363731d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
3638e879892cSThomas Huth 	 * it into the save area
3639e879892cSThomas Huth 	 */
3640d0164ee2SHendrik Brueckner 	save_fpu_regs();
36419abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3642e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
3643e879892cSThomas Huth 
3644e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
3645e879892cSThomas Huth }
3646e879892cSThomas Huth 
36478ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
36488ad35755SDavid Hildenbrand {
36498ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
36508e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
36518ad35755SDavid Hildenbrand }
36528ad35755SDavid Hildenbrand 
36538ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
36548ad35755SDavid Hildenbrand {
36558ad35755SDavid Hildenbrand 	unsigned int i;
36568ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
36578ad35755SDavid Hildenbrand 
36588ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
36598ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
36608ad35755SDavid Hildenbrand 	}
36618ad35755SDavid Hildenbrand }
36628ad35755SDavid Hildenbrand 
36638ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
36648ad35755SDavid Hildenbrand {
366509a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
366609a400e7SDavid Hildenbrand 		return;
36678ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
36688e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
36698ad35755SDavid Hildenbrand }
36708ad35755SDavid Hildenbrand 
36716852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
36726852d7b6SDavid Hildenbrand {
36738ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
36748ad35755SDavid Hildenbrand 
36758ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
36768ad35755SDavid Hildenbrand 		return;
36778ad35755SDavid Hildenbrand 
36786852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
36798ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
3680433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
36818ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
36828ad35755SDavid Hildenbrand 
36838ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
36848ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
36858ad35755SDavid Hildenbrand 			started_vcpus++;
36868ad35755SDavid Hildenbrand 	}
36878ad35755SDavid Hildenbrand 
36888ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
36898ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
36908ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
36918ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
36928ad35755SDavid Hildenbrand 		/*
36938ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
36948ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
36958ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
36968ad35755SDavid Hildenbrand 		 */
36978ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
36988ad35755SDavid Hildenbrand 	}
36998ad35755SDavid Hildenbrand 
37009daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
37018ad35755SDavid Hildenbrand 	/*
37028ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
37038ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
37048ad35755SDavid Hildenbrand 	 */
3705d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3706433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
37078ad35755SDavid Hildenbrand 	return;
37086852d7b6SDavid Hildenbrand }
37096852d7b6SDavid Hildenbrand 
37106852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
37116852d7b6SDavid Hildenbrand {
37128ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
37138ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
37148ad35755SDavid Hildenbrand 
37158ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
37168ad35755SDavid Hildenbrand 		return;
37178ad35755SDavid Hildenbrand 
37186852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
37198ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
3720433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
37218ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
37228ad35755SDavid Hildenbrand 
372332f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
37246cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
372532f5ff63SDavid Hildenbrand 
3726ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
37278ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
37288ad35755SDavid Hildenbrand 
37298ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
37308ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
37318ad35755SDavid Hildenbrand 			started_vcpus++;
37328ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
37338ad35755SDavid Hildenbrand 		}
37348ad35755SDavid Hildenbrand 	}
37358ad35755SDavid Hildenbrand 
37368ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
37378ad35755SDavid Hildenbrand 		/*
37388ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
37398ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
37408ad35755SDavid Hildenbrand 		 */
37418ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
37428ad35755SDavid Hildenbrand 	}
37438ad35755SDavid Hildenbrand 
3744433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
37458ad35755SDavid Hildenbrand 	return;
37466852d7b6SDavid Hildenbrand }
37476852d7b6SDavid Hildenbrand 
3748d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
3749d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
3750d6712df9SCornelia Huck {
3751d6712df9SCornelia Huck 	int r;
3752d6712df9SCornelia Huck 
3753d6712df9SCornelia Huck 	if (cap->flags)
3754d6712df9SCornelia Huck 		return -EINVAL;
3755d6712df9SCornelia Huck 
3756d6712df9SCornelia Huck 	switch (cap->cap) {
3757fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
3758fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
3759fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
3760c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
3761fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
3762fa6b7fe9SCornelia Huck 		}
3763fa6b7fe9SCornelia Huck 		r = 0;
3764fa6b7fe9SCornelia Huck 		break;
3765d6712df9SCornelia Huck 	default:
3766d6712df9SCornelia Huck 		r = -EINVAL;
3767d6712df9SCornelia Huck 		break;
3768d6712df9SCornelia Huck 	}
3769d6712df9SCornelia Huck 	return r;
3770d6712df9SCornelia Huck }
3771d6712df9SCornelia Huck 
377241408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
377341408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
377441408c28SThomas Huth {
377541408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
377641408c28SThomas Huth 	void *tmpbuf = NULL;
377741408c28SThomas Huth 	int r, srcu_idx;
377841408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
377941408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
378041408c28SThomas Huth 
378141408c28SThomas Huth 	if (mop->flags & ~supported_flags)
378241408c28SThomas Huth 		return -EINVAL;
378341408c28SThomas Huth 
378441408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
378541408c28SThomas Huth 		return -E2BIG;
378641408c28SThomas Huth 
378741408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
378841408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
378941408c28SThomas Huth 		if (!tmpbuf)
379041408c28SThomas Huth 			return -ENOMEM;
379141408c28SThomas Huth 	}
379241408c28SThomas Huth 
379341408c28SThomas Huth 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
379441408c28SThomas Huth 
379541408c28SThomas Huth 	switch (mop->op) {
379641408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
379741408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
379892c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
379992c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
380041408c28SThomas Huth 			break;
380141408c28SThomas Huth 		}
380241408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
380341408c28SThomas Huth 		if (r == 0) {
380441408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
380541408c28SThomas Huth 				r = -EFAULT;
380641408c28SThomas Huth 		}
380741408c28SThomas Huth 		break;
380841408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
380941408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
381092c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
381192c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
381241408c28SThomas Huth 			break;
381341408c28SThomas Huth 		}
381441408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
381541408c28SThomas Huth 			r = -EFAULT;
381641408c28SThomas Huth 			break;
381741408c28SThomas Huth 		}
381841408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
381941408c28SThomas Huth 		break;
382041408c28SThomas Huth 	default:
382141408c28SThomas Huth 		r = -EINVAL;
382241408c28SThomas Huth 	}
382341408c28SThomas Huth 
382441408c28SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
382541408c28SThomas Huth 
382641408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
382741408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
382841408c28SThomas Huth 
382941408c28SThomas Huth 	vfree(tmpbuf);
383041408c28SThomas Huth 	return r;
383141408c28SThomas Huth }
383241408c28SThomas Huth 
38335cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp,
3834b0c632dbSHeiko Carstens 			       unsigned int ioctl, unsigned long arg)
3835b0c632dbSHeiko Carstens {
3836b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
3837b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
3838b0c632dbSHeiko Carstens 
383993736624SAvi Kivity 	switch (ioctl) {
384047b43c52SJens Freimann 	case KVM_S390_IRQ: {
384147b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
384247b43c52SJens Freimann 
384347b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
38449b062471SChristoffer Dall 			return -EFAULT;
38459b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
384647b43c52SJens Freimann 	}
384793736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
3848ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
3849383d0b05SJens Freimann 		struct kvm_s390_irq s390irq;
3850ba5c1e9bSCarsten Otte 
3851ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
38529b062471SChristoffer Dall 			return -EFAULT;
3853383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
3854383d0b05SJens Freimann 			return -EINVAL;
38559b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
3856ba5c1e9bSCarsten Otte 	}
38579b062471SChristoffer Dall 	}
38585cb0944cSPaolo Bonzini 	return -ENOIOCTLCMD;
38595cb0944cSPaolo Bonzini }
38605cb0944cSPaolo Bonzini 
38615cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp,
38625cb0944cSPaolo Bonzini 			 unsigned int ioctl, unsigned long arg)
38635cb0944cSPaolo Bonzini {
38645cb0944cSPaolo Bonzini 	struct kvm_vcpu *vcpu = filp->private_data;
38655cb0944cSPaolo Bonzini 	void __user *argp = (void __user *)arg;
38665cb0944cSPaolo Bonzini 	int idx;
38675cb0944cSPaolo Bonzini 	long r;
38689b062471SChristoffer Dall 
38699b062471SChristoffer Dall 	vcpu_load(vcpu);
38709b062471SChristoffer Dall 
38719b062471SChristoffer Dall 	switch (ioctl) {
3872b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
3873800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
3874bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
3875800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
3876bc923cc9SAvi Kivity 		break;
3877b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
3878b0c632dbSHeiko Carstens 		psw_t psw;
3879b0c632dbSHeiko Carstens 
3880bc923cc9SAvi Kivity 		r = -EFAULT;
3881b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
3882bc923cc9SAvi Kivity 			break;
3883bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
3884bc923cc9SAvi Kivity 		break;
3885b0c632dbSHeiko Carstens 	}
3886b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
3887bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
3888bc923cc9SAvi Kivity 		break;
388914eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
389014eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
389114eebd91SCarsten Otte 		struct kvm_one_reg reg;
389214eebd91SCarsten Otte 		r = -EFAULT;
389314eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
389414eebd91SCarsten Otte 			break;
389514eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
389614eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
389714eebd91SCarsten Otte 		else
389814eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
389914eebd91SCarsten Otte 		break;
390014eebd91SCarsten Otte 	}
390127e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
390227e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
390327e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
390427e0393fSCarsten Otte 
390527e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
390627e0393fSCarsten Otte 			r = -EFAULT;
390727e0393fSCarsten Otte 			break;
390827e0393fSCarsten Otte 		}
390927e0393fSCarsten Otte 
391027e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
391127e0393fSCarsten Otte 			r = -EINVAL;
391227e0393fSCarsten Otte 			break;
391327e0393fSCarsten Otte 		}
391427e0393fSCarsten Otte 
391527e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
391627e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
391727e0393fSCarsten Otte 		break;
391827e0393fSCarsten Otte 	}
391927e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
392027e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
392127e0393fSCarsten Otte 
392227e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
392327e0393fSCarsten Otte 			r = -EFAULT;
392427e0393fSCarsten Otte 			break;
392527e0393fSCarsten Otte 		}
392627e0393fSCarsten Otte 
392727e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
392827e0393fSCarsten Otte 			r = -EINVAL;
392927e0393fSCarsten Otte 			break;
393027e0393fSCarsten Otte 		}
393127e0393fSCarsten Otte 
393227e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
393327e0393fSCarsten Otte 			ucasmap.length);
393427e0393fSCarsten Otte 		break;
393527e0393fSCarsten Otte 	}
393627e0393fSCarsten Otte #endif
3937ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
3938527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
3939ccc7910fSCarsten Otte 		break;
3940ccc7910fSCarsten Otte 	}
3941d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
3942d6712df9SCornelia Huck 	{
3943d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
3944d6712df9SCornelia Huck 		r = -EFAULT;
3945d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
3946d6712df9SCornelia Huck 			break;
3947d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
3948d6712df9SCornelia Huck 		break;
3949d6712df9SCornelia Huck 	}
395041408c28SThomas Huth 	case KVM_S390_MEM_OP: {
395141408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
395241408c28SThomas Huth 
395341408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
395441408c28SThomas Huth 			r = kvm_s390_guest_mem_op(vcpu, &mem_op);
395541408c28SThomas Huth 		else
395641408c28SThomas Huth 			r = -EFAULT;
395741408c28SThomas Huth 		break;
395841408c28SThomas Huth 	}
3959816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
3960816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3961816c7667SJens Freimann 
3962816c7667SJens Freimann 		r = -EFAULT;
3963816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3964816c7667SJens Freimann 			break;
3965816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
3966816c7667SJens Freimann 		    irq_state.len == 0 ||
3967816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
3968816c7667SJens Freimann 			r = -EINVAL;
3969816c7667SJens Freimann 			break;
3970816c7667SJens Freimann 		}
3971bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
3972816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
3973816c7667SJens Freimann 					   (void __user *) irq_state.buf,
3974816c7667SJens Freimann 					   irq_state.len);
3975816c7667SJens Freimann 		break;
3976816c7667SJens Freimann 	}
3977816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
3978816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3979816c7667SJens Freimann 
3980816c7667SJens Freimann 		r = -EFAULT;
3981816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3982816c7667SJens Freimann 			break;
3983816c7667SJens Freimann 		if (irq_state.len == 0) {
3984816c7667SJens Freimann 			r = -EINVAL;
3985816c7667SJens Freimann 			break;
3986816c7667SJens Freimann 		}
3987bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
3988816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
3989816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
3990816c7667SJens Freimann 					   irq_state.len);
3991816c7667SJens Freimann 		break;
3992816c7667SJens Freimann 	}
3993b0c632dbSHeiko Carstens 	default:
39943e6afcf1SCarsten Otte 		r = -ENOTTY;
3995b0c632dbSHeiko Carstens 	}
39969b062471SChristoffer Dall 
39979b062471SChristoffer Dall 	vcpu_put(vcpu);
3998bc923cc9SAvi Kivity 	return r;
3999b0c632dbSHeiko Carstens }
4000b0c632dbSHeiko Carstens 
40015b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
40025b1c1493SCarsten Otte {
40035b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
40045b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
40055b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
40065b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
40075b1c1493SCarsten Otte 		get_page(vmf->page);
40085b1c1493SCarsten Otte 		return 0;
40095b1c1493SCarsten Otte 	}
40105b1c1493SCarsten Otte #endif
40115b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
40125b1c1493SCarsten Otte }
40135b1c1493SCarsten Otte 
40145587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
40155587027cSAneesh Kumar K.V 			    unsigned long npages)
4016db3fe4ebSTakuya Yoshikawa {
4017db3fe4ebSTakuya Yoshikawa 	return 0;
4018db3fe4ebSTakuya Yoshikawa }
4019db3fe4ebSTakuya Yoshikawa 
4020b0c632dbSHeiko Carstens /* Section: memory related */
4021f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
4022f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
402309170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
40247b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
4025b0c632dbSHeiko Carstens {
4026dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
4027dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
4028dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
4029dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
4030b0c632dbSHeiko Carstens 
4031598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
4032b0c632dbSHeiko Carstens 		return -EINVAL;
4033b0c632dbSHeiko Carstens 
4034598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
4035b0c632dbSHeiko Carstens 		return -EINVAL;
4036b0c632dbSHeiko Carstens 
4037a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
4038a3a92c31SDominik Dingel 		return -EINVAL;
4039a3a92c31SDominik Dingel 
4040f7784b8eSMarcelo Tosatti 	return 0;
4041f7784b8eSMarcelo Tosatti }
4042f7784b8eSMarcelo Tosatti 
4043f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
404409170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
40458482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
4046f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
40478482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
4048f7784b8eSMarcelo Tosatti {
4049f7850c92SCarsten Otte 	int rc;
4050f7784b8eSMarcelo Tosatti 
40512cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
40522cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
40532cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
40542cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
40552cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
40562cef4debSChristian Borntraeger 	 */
40572cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
40582cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
40592cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
40602cef4debSChristian Borntraeger 		return;
4061598841caSCarsten Otte 
4062598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
4063598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
4064598841caSCarsten Otte 	if (rc)
4065ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
4066598841caSCarsten Otte 	return;
4067b0c632dbSHeiko Carstens }
4068b0c632dbSHeiko Carstens 
406960a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
407060a37709SAlexander Yarygin {
407160a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
407260a37709SAlexander Yarygin 
407360a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
407460a37709SAlexander Yarygin }
407560a37709SAlexander Yarygin 
40763491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
40773491caf2SChristian Borntraeger {
40783491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
40793491caf2SChristian Borntraeger }
40803491caf2SChristian Borntraeger 
4081b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
4082b0c632dbSHeiko Carstens {
408360a37709SAlexander Yarygin 	int i;
408460a37709SAlexander Yarygin 
408507197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
408607197fd0SDavid Hildenbrand 		pr_info("SIE not available\n");
408707197fd0SDavid Hildenbrand 		return -ENODEV;
408807197fd0SDavid Hildenbrand 	}
408907197fd0SDavid Hildenbrand 
409060a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
4091c3b9e3e1SChristian Borntraeger 		kvm_s390_fac_base[i] |=
409260a37709SAlexander Yarygin 			S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
409360a37709SAlexander Yarygin 
40949d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
4095b0c632dbSHeiko Carstens }
4096b0c632dbSHeiko Carstens 
4097b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
4098b0c632dbSHeiko Carstens {
4099b0c632dbSHeiko Carstens 	kvm_exit();
4100b0c632dbSHeiko Carstens }
4101b0c632dbSHeiko Carstens 
4102b0c632dbSHeiko Carstens module_init(kvm_s390_init);
4103b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
4104566af940SCornelia Huck 
4105566af940SCornelia Huck /*
4106566af940SCornelia Huck  * Enable autoloading of the kvm module.
4107566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
4108566af940SCornelia Huck  * since x86 takes a different approach.
4109566af940SCornelia Huck  */
4110566af940SCornelia Huck #include <linux/miscdevice.h>
4111566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
4112566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
4113