xref: /openbmc/linux/arch/s390/kvm/kvm-s390.c (revision fe0ef00304639cae82df7c9ad6a15286bd5f876e)
1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b0c632dbSHeiko Carstens /*
3bb64da9aSChristian Borntraeger  * hosting IBM Z kernel virtual machines (s390x)
4b0c632dbSHeiko Carstens  *
53e6c5568SJanosch Frank  * Copyright IBM Corp. 2008, 2020
6b0c632dbSHeiko Carstens  *
7b0c632dbSHeiko Carstens  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8b0c632dbSHeiko Carstens  *               Christian Borntraeger <borntraeger@de.ibm.com>
9628eb9b8SChristian Ehrhardt  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
1015f36ebdSJason J. Herne  *               Jason J. Herne <jjherne@us.ibm.com>
11b0c632dbSHeiko Carstens  */
12b0c632dbSHeiko Carstens 
137aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390"
147aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
157aedd9d4SMichael Mueller 
16b0c632dbSHeiko Carstens #include <linux/compiler.h>
17b0c632dbSHeiko Carstens #include <linux/err.h>
18b0c632dbSHeiko Carstens #include <linux/fs.h>
19ca872302SChristian Borntraeger #include <linux/hrtimer.h>
20b0c632dbSHeiko Carstens #include <linux/init.h>
21b0c632dbSHeiko Carstens #include <linux/kvm.h>
22b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
23b2d73b2aSMartin Schwidefsky #include <linux/mman.h>
24b0c632dbSHeiko Carstens #include <linux/module.h>
25d3217967SPaul Gortmaker #include <linux/moduleparam.h>
26a374e892STony Krowiak #include <linux/random.h>
27b0c632dbSHeiko Carstens #include <linux/slab.h>
28ba5c1e9bSCarsten Otte #include <linux/timer.h>
2941408c28SThomas Huth #include <linux/vmalloc.h>
3015c9705fSDavid Hildenbrand #include <linux/bitmap.h>
31174cd4b1SIngo Molnar #include <linux/sched/signal.h>
32190df4a2SClaudio Imbrenda #include <linux/string.h>
3365fddcfcSMike Rapoport #include <linux/pgtable.h>
34ca2fd060SClaudio Imbrenda #include <linux/mmu_notifier.h>
35174cd4b1SIngo Molnar 
36cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
37b0c632dbSHeiko Carstens #include <asm/lowcore.h>
38fd5ada04SMartin Schwidefsky #include <asm/stp.h>
391e133ab2SMartin Schwidefsky #include <asm/gmap.h>
40f5daba1dSHeiko Carstens #include <asm/nmi.h>
41a0616cdeSDavid Howells #include <asm/switch_to.h>
426d3da241SJens Freimann #include <asm/isc.h>
431526bf9cSChristian Borntraeger #include <asm/sclp.h>
440a763c78SDavid Hildenbrand #include <asm/cpacf.h>
45221bb8a4SLinus Torvalds #include <asm/timex.h>
46e585b24aSTony Krowiak #include <asm/ap.h>
4729b40f10SJanosch Frank #include <asm/uv.h>
4856e62a73SSven Schnelle #include <asm/fpu/api.h>
498f2abe6aSChristian Borntraeger #include "kvm-s390.h"
50b0c632dbSHeiko Carstens #include "gaccess.h"
5198b1d33dSMatthew Rosato #include "pci.h"
52b0c632dbSHeiko Carstens 
535786fffaSCornelia Huck #define CREATE_TRACE_POINTS
545786fffaSCornelia Huck #include "trace.h"
55ade38c31SCornelia Huck #include "trace-s390.h"
565786fffaSCornelia Huck 
5741408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
58816c7667SJens Freimann #define LOCAL_IRQS 32
59816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
60816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
6141408c28SThomas Huth 
62fcfe1baeSJing Zhang const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
63fcfe1baeSJing Zhang 	KVM_GENERIC_VM_STATS(),
64fcfe1baeSJing Zhang 	STATS_DESC_COUNTER(VM, inject_io),
65fcfe1baeSJing Zhang 	STATS_DESC_COUNTER(VM, inject_float_mchk),
66fcfe1baeSJing Zhang 	STATS_DESC_COUNTER(VM, inject_pfault_done),
67fcfe1baeSJing Zhang 	STATS_DESC_COUNTER(VM, inject_service_signal),
6873f91b00SMatthew Rosato 	STATS_DESC_COUNTER(VM, inject_virtio),
6973f91b00SMatthew Rosato 	STATS_DESC_COUNTER(VM, aen_forward)
70fcfe1baeSJing Zhang };
71fcfe1baeSJing Zhang 
72fcfe1baeSJing Zhang const struct kvm_stats_header kvm_vm_stats_header = {
73fcfe1baeSJing Zhang 	.name_size = KVM_STATS_NAME_SIZE,
74fcfe1baeSJing Zhang 	.num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
75fcfe1baeSJing Zhang 	.id_offset = sizeof(struct kvm_stats_header),
76fcfe1baeSJing Zhang 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
77fcfe1baeSJing Zhang 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
78fcfe1baeSJing Zhang 		       sizeof(kvm_vm_stats_desc),
79fcfe1baeSJing Zhang };
80fcfe1baeSJing Zhang 
81ce55c049SJing Zhang const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
82ce55c049SJing Zhang 	KVM_GENERIC_VCPU_STATS(),
83ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_userspace),
84ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_null),
85ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_external_request),
86ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_io_request),
87ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_external_interrupt),
88ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_stop_request),
89ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_validity),
90ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_instruction),
91ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_pei),
92ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, halt_no_poll_steal),
93ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_lctl),
94ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_lctlg),
95ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stctl),
96ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stctg),
97ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_program_interruption),
98ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_instr_and_program),
99ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_operation_exception),
100ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_ckc),
101ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_cputm),
102ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_external_call),
103ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_emergency_signal),
104ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_service_signal),
105ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_virtio),
106ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_stop_signal),
107ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_prefix_signal),
108ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_restart_signal),
109ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_program),
110ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_io),
111ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_machine_check),
112ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_wait_state),
113ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_ckc),
114ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_cputm),
115ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_external_call),
116ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_emergency_signal),
117ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_mchk),
118ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_pfault_init),
119ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_program),
120ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_restart),
121ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_set_prefix),
122ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_stop_signal),
123ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_epsw),
124ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_gs),
125ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_io_other),
126ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_lpsw),
127ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_lpswe),
128ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_pfmf),
129ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_ptff),
130ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sck),
131ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sckpf),
132ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stidp),
133ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_spx),
134ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stpx),
135ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stap),
136ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_iske),
137ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_ri),
138ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_rrbe),
139ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sske),
140ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock),
141ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stsi),
142ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stfl),
143ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_tb),
144ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_tpi),
145ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_tprot),
146ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_tsch),
147ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sie),
148ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_essa),
149ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sthyi),
150ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_sense),
151ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running),
152ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call),
153ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency),
154ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency),
155ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_start),
156ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_stop),
157ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status),
158ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status),
159ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status),
160ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_arch),
161ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix),
162ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_restart),
163ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset),
164ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset),
165ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown),
166bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_10),
167bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_44),
168bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c),
169bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, diag_9c_ignored),
170bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, diag_9c_forward),
171bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_258),
172bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_308),
173bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_500),
174bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_other),
175ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, pfault_sync)
176ce55c049SJing Zhang };
177ce55c049SJing Zhang 
178ce55c049SJing Zhang const struct kvm_stats_header kvm_vcpu_stats_header = {
179ce55c049SJing Zhang 	.name_size = KVM_STATS_NAME_SIZE,
180ce55c049SJing Zhang 	.num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
181ce55c049SJing Zhang 	.id_offset = sizeof(struct kvm_stats_header),
182ce55c049SJing Zhang 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
183ce55c049SJing Zhang 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
184ce55c049SJing Zhang 		       sizeof(kvm_vcpu_stats_desc),
185ce55c049SJing Zhang };
186ce55c049SJing Zhang 
187a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
188a411edf1SDavid Hildenbrand static int nested;
189a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
190a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
191a411edf1SDavid Hildenbrand 
192a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */
193a4499382SJanosch Frank static int hpage;
194a4499382SJanosch Frank module_param(hpage, int, 0444);
195a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support");
196b0c632dbSHeiko Carstens 
1978b905d28SChristian Borntraeger /* maximum percentage of steal time for polling.  >100 is treated like 100 */
1988b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10;
1998b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644);
200b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
2018b905d28SChristian Borntraeger 
202cc674ef2SMichael Mueller /* if set to true, the GISA will be initialized and used if available */
203cc674ef2SMichael Mueller static bool use_gisa  = true;
204cc674ef2SMichael Mueller module_param(use_gisa, bool, 0644);
205cc674ef2SMichael Mueller MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it.");
206cc674ef2SMichael Mueller 
20787e28a15SPierre Morel /* maximum diag9c forwarding per second */
20887e28a15SPierre Morel unsigned int diag9c_forwarding_hz;
20987e28a15SPierre Morel module_param(diag9c_forwarding_hz, uint, 0644);
21087e28a15SPierre Morel MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off");
21187e28a15SPierre Morel 
212c3b9e3e1SChristian Borntraeger /*
213c3b9e3e1SChristian Borntraeger  * For now we handle at most 16 double words as this is what the s390 base
214c3b9e3e1SChristian Borntraeger  * kernel handles and stores in the prefix page. If we ever need to go beyond
215c3b9e3e1SChristian Borntraeger  * this, this requires changes to code, but the external uapi can stay.
216c3b9e3e1SChristian Borntraeger  */
217c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16
218c3b9e3e1SChristian Borntraeger 
219c3b9e3e1SChristian Borntraeger /*
220c3b9e3e1SChristian Borntraeger  * Base feature mask that defines default mask for facilities. Consists of the
221c3b9e3e1SChristian Borntraeger  * defines in FACILITIES_KVM and the non-hypervisor managed bits.
222c3b9e3e1SChristian Borntraeger  */
223c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
224c3b9e3e1SChristian Borntraeger /*
225c3b9e3e1SChristian Borntraeger  * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
226c3b9e3e1SChristian Borntraeger  * and defines the facilities that can be enabled via a cpu model.
227c3b9e3e1SChristian Borntraeger  */
228c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
229c3b9e3e1SChristian Borntraeger 
230c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void)
23178c4b59fSMichael Mueller {
232c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
233c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
234c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
23517e89e13SSven Schnelle 		sizeof(stfle_fac_list));
236c3b9e3e1SChristian Borntraeger 
237c3b9e3e1SChristian Borntraeger 	return SIZE_INTERNAL;
23878c4b59fSMichael Mueller }
23978c4b59fSMichael Mueller 
24015c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
24115c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
2420a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
2430a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
24415c9705fSDavid Hildenbrand 
2459d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
246a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
24778f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
2483e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv;
2499d8d5786SMichael Mueller 
250b0c632dbSHeiko Carstens /* Section: not file related */
25113a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
252b0c632dbSHeiko Carstens {
253b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
25410474ae8SAlexander Graf 	return 0;
255b0c632dbSHeiko Carstens }
256b0c632dbSHeiko Carstens 
257b9904085SSean Christopherson int kvm_arch_check_processor_compat(void *opaque)
258f257d6dcSSean Christopherson {
259f257d6dcSSean Christopherson 	return 0;
260f257d6dcSSean Christopherson }
261f257d6dcSSean Christopherson 
26229b40f10SJanosch Frank /* forward declarations */
263414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
264414d3b07SMartin Schwidefsky 			      unsigned long end);
26529b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm);
2662c70fe44SChristian Borntraeger 
2671575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
2681575767eSDavid Hildenbrand {
2691575767eSDavid Hildenbrand 	u8 delta_idx = 0;
2701575767eSDavid Hildenbrand 
2711575767eSDavid Hildenbrand 	/*
2721575767eSDavid Hildenbrand 	 * The TOD jumps by delta, we have to compensate this by adding
2731575767eSDavid Hildenbrand 	 * -delta to the epoch.
2741575767eSDavid Hildenbrand 	 */
2751575767eSDavid Hildenbrand 	delta = -delta;
2761575767eSDavid Hildenbrand 
2771575767eSDavid Hildenbrand 	/* sign-extension - we're adding to signed values below */
2781575767eSDavid Hildenbrand 	if ((s64)delta < 0)
2791575767eSDavid Hildenbrand 		delta_idx = -1;
2801575767eSDavid Hildenbrand 
2811575767eSDavid Hildenbrand 	scb->epoch += delta;
2821575767eSDavid Hildenbrand 	if (scb->ecd & ECD_MEF) {
2831575767eSDavid Hildenbrand 		scb->epdx += delta_idx;
2841575767eSDavid Hildenbrand 		if (scb->epoch < delta)
2851575767eSDavid Hildenbrand 			scb->epdx += 1;
2861575767eSDavid Hildenbrand 	}
2871575767eSDavid Hildenbrand }
2881575767eSDavid Hildenbrand 
289fdf03650SFan Zhang /*
290fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
291fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
292fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
293fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
294fdf03650SFan Zhang  */
295fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
296fdf03650SFan Zhang 			  void *v)
297fdf03650SFan Zhang {
298fdf03650SFan Zhang 	struct kvm *kvm;
299fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
30046808a4cSMarc Zyngier 	unsigned long i;
301fdf03650SFan Zhang 	unsigned long long *delta = v;
302fdf03650SFan Zhang 
303fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
304fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
3051575767eSDavid Hildenbrand 			kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
3061575767eSDavid Hildenbrand 			if (i == 0) {
3071575767eSDavid Hildenbrand 				kvm->arch.epoch = vcpu->arch.sie_block->epoch;
3081575767eSDavid Hildenbrand 				kvm->arch.epdx = vcpu->arch.sie_block->epdx;
3091575767eSDavid Hildenbrand 			}
310db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
311db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
31291473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
3131575767eSDavid Hildenbrand 				kvm_clock_sync_scb(vcpu->arch.vsie_block,
3141575767eSDavid Hildenbrand 						   *delta);
315fdf03650SFan Zhang 		}
316fdf03650SFan Zhang 	}
317fdf03650SFan Zhang 	return NOTIFY_OK;
318fdf03650SFan Zhang }
319fdf03650SFan Zhang 
320fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
321fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
322fdf03650SFan Zhang };
323fdf03650SFan Zhang 
324b9904085SSean Christopherson int kvm_arch_hardware_setup(void *opaque)
325b0c632dbSHeiko Carstens {
3262c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
327b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
328a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
329a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
330fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
331fdf03650SFan Zhang 				       &kvm_clock_notifier);
332b0c632dbSHeiko Carstens 	return 0;
333b0c632dbSHeiko Carstens }
334b0c632dbSHeiko Carstens 
335b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
336b0c632dbSHeiko Carstens {
337b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
338a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
339fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
340fdf03650SFan Zhang 					 &kvm_clock_notifier);
341b0c632dbSHeiko Carstens }
342b0c632dbSHeiko Carstens 
34322be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
34422be5a13SDavid Hildenbrand {
34522be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
34622be5a13SDavid Hildenbrand }
34722be5a13SDavid Hildenbrand 
3480a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
3490a763c78SDavid Hildenbrand {
3504fa3b91bSHeiko Carstens 	unsigned long function = (unsigned long)nr | 0x100;
351d051ae53SHeiko Carstens 	int cc;
3520a763c78SDavid Hildenbrand 
3530a763c78SDavid Hildenbrand 	asm volatile(
3544fa3b91bSHeiko Carstens 		"	lgr	0,%[function]\n"
3550a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
3560a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
3570a763c78SDavid Hildenbrand 		"	ipm	%0\n"
3580a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
3590a763c78SDavid Hildenbrand 		: "=d" (cc)
3604fa3b91bSHeiko Carstens 		: [function] "d" (function)
3614fa3b91bSHeiko Carstens 		: "cc", "0");
3620a763c78SDavid Hildenbrand 	return cc == 0;
3630a763c78SDavid Hildenbrand }
3640a763c78SDavid Hildenbrand 
365d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
366d6681397SChristian Borntraeger {
367d6681397SChristian Borntraeger 	asm volatile(
3684fa3b91bSHeiko Carstens 		"	lghi	0,0\n"
3694fa3b91bSHeiko Carstens 		"	lgr	1,%[query]\n"
3704fa3b91bSHeiko Carstens 		/* Parameter registers are ignored */
371d6681397SChristian Borntraeger 		"	.insn	rrf,%[opc] << 16,2,4,6,0\n"
372b1c41ac3SHeiko Carstens 		:
3734fa3b91bSHeiko Carstens 		: [query] "d" ((unsigned long)query), [opc] "i" (opcode)
3744fa3b91bSHeiko Carstens 		: "cc", "memory", "0", "1");
375d6681397SChristian Borntraeger }
376d6681397SChristian Borntraeger 
377173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938
3784f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939
379173aec2dSChristian Borntraeger 
38022be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
38122be5a13SDavid Hildenbrand {
3820a763c78SDavid Hildenbrand 	int i;
3830a763c78SDavid Hildenbrand 
3840a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
3850a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
3860a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
3870a763c78SDavid Hildenbrand 	}
3880a763c78SDavid Hildenbrand 
3890a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
390221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
391221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
392221bb8a4SLinus Torvalds 		     PTFF_QAF);
3930a763c78SDavid Hildenbrand 
3940a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
39569c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
39669c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
39769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
39869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
39969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
40069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
40169c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
40269c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
40369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
40469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
4050a763c78SDavid Hildenbrand 	}
4060a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
40769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
40869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
4090a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
41069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
41169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
41269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
41369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
41469c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
41569c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
41669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
41769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
4180a763c78SDavid Hildenbrand 	}
4190a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
420985a9d20SHarald Freudenberger 		__cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
42169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
4220a763c78SDavid Hildenbrand 
423e000b8e0SJason J. Herne 	if (test_facility(146)) /* MSA8 */
424e000b8e0SJason J. Herne 		__cpacf_query(CPACF_KMA, (cpacf_mask_t *)
425e000b8e0SJason J. Herne 			      kvm_s390_available_subfunc.kma);
426e000b8e0SJason J. Herne 
42713209ad0SChristian Borntraeger 	if (test_facility(155)) /* MSA9 */
42813209ad0SChristian Borntraeger 		__cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
42913209ad0SChristian Borntraeger 			      kvm_s390_available_subfunc.kdsa);
43013209ad0SChristian Borntraeger 
431173aec2dSChristian Borntraeger 	if (test_facility(150)) /* SORTL */
432173aec2dSChristian Borntraeger 		__insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl);
433173aec2dSChristian Borntraeger 
4344f45b90eSChristian Borntraeger 	if (test_facility(151)) /* DFLTCC */
4354f45b90eSChristian Borntraeger 		__insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
4364f45b90eSChristian Borntraeger 
43722be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
43822be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
439a3508fbeSDavid Hildenbrand 	/*
440a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
441a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
442a3508fbeSDavid Hildenbrand 	 */
443a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
444a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
445a3508fbeSDavid Hildenbrand 		return;
446a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
44719c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
44819c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
4490615a326SDavid Hildenbrand 	if (sclp.has_siif)
4500615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
45177d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
45277d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
453a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
454a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
4555630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
4565630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
45713ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
45813ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
4597fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
4607fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
461730cd632SFarhan Ali 	if (sclp.has_kss)
462730cd632SFarhan Ali 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
4635d3876a8SDavid Hildenbrand 	/*
4645d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
4655d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
4665d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
4675d3876a8SDavid Hildenbrand 	 *
4685d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
4695d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
4705d3876a8SDavid Hildenbrand 	 *
4715d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
4725d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
4735d3876a8SDavid Hildenbrand 	 *
4745d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
4755d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
4765d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
4775d3876a8SDavid Hildenbrand 	 *
4785d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
4795d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
4805d3876a8SDavid Hildenbrand 	 */
48122be5a13SDavid Hildenbrand }
48222be5a13SDavid Hildenbrand 
483b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
484b0c632dbSHeiko Carstens {
485f76f6371SJanosch Frank 	int rc = -ENOMEM;
486308c3e66SMichael Mueller 
48778f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
48878f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
48978f26131SChristian Borntraeger 		return -ENOMEM;
49078f26131SChristian Borntraeger 
4913e6c5568SJanosch Frank 	kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
4923e6c5568SJanosch Frank 	if (!kvm_s390_dbf_uv)
4933e6c5568SJanosch Frank 		goto out;
4943e6c5568SJanosch Frank 
4953e6c5568SJanosch Frank 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
4963e6c5568SJanosch Frank 	    debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
497f76f6371SJanosch Frank 		goto out;
49878f26131SChristian Borntraeger 
49922be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
50022be5a13SDavid Hildenbrand 
50184877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
502308c3e66SMichael Mueller 	rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
503308c3e66SMichael Mueller 	if (rc) {
5048d43d570SMichael Mueller 		pr_err("A FLIC registration call failed with rc=%d\n", rc);
505f76f6371SJanosch Frank 		goto out;
506308c3e66SMichael Mueller 	}
507b1d1e76eSMichael Mueller 
508189e7d87SMatthew Rosato 	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
50998b1d33dSMatthew Rosato 		rc = kvm_s390_pci_init();
51098b1d33dSMatthew Rosato 		if (rc) {
51198b1d33dSMatthew Rosato 			pr_err("Unable to allocate AIFT for PCI\n");
51298b1d33dSMatthew Rosato 			goto out;
51398b1d33dSMatthew Rosato 		}
51498b1d33dSMatthew Rosato 	}
51598b1d33dSMatthew Rosato 
516b1d1e76eSMichael Mueller 	rc = kvm_s390_gib_init(GAL_ISC);
517b1d1e76eSMichael Mueller 	if (rc)
518f76f6371SJanosch Frank 		goto out;
519b1d1e76eSMichael Mueller 
520308c3e66SMichael Mueller 	return 0;
521308c3e66SMichael Mueller 
522f76f6371SJanosch Frank out:
523f76f6371SJanosch Frank 	kvm_arch_exit();
524308c3e66SMichael Mueller 	return rc;
525b0c632dbSHeiko Carstens }
526b0c632dbSHeiko Carstens 
52778f26131SChristian Borntraeger void kvm_arch_exit(void)
52878f26131SChristian Borntraeger {
5291282c21eSMichael Mueller 	kvm_s390_gib_destroy();
530189e7d87SMatthew Rosato 	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
53198b1d33dSMatthew Rosato 		kvm_s390_pci_exit();
53278f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
5333e6c5568SJanosch Frank 	debug_unregister(kvm_s390_dbf_uv);
53478f26131SChristian Borntraeger }
53578f26131SChristian Borntraeger 
536b0c632dbSHeiko Carstens /* Section: device related */
537b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
538b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
539b0c632dbSHeiko Carstens {
540b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
541b0c632dbSHeiko Carstens 		return s390_enable_sie();
542b0c632dbSHeiko Carstens 	return -EINVAL;
543b0c632dbSHeiko Carstens }
544b0c632dbSHeiko Carstens 
545784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
546b0c632dbSHeiko Carstens {
547d7b0b5ebSCarsten Otte 	int r;
548d7b0b5ebSCarsten Otte 
5492bd0ac4eSCarsten Otte 	switch (ext) {
550d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
551b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
55252e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
5531efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
5541efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
5551efd0f59SCarsten Otte #endif
5563c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
55760b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
55814eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
559d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
560fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
56110ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
562c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
56378599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
564f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
5656352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
566460df4c1SPaolo Bonzini 	case KVM_CAP_IMMEDIATE_EXIT:
56747b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
5682444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
569e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
57030ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
571816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
5726502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
5734036e387SClaudio Imbrenda 	case KVM_CAP_S390_CMMA_MIGRATION:
57447a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
575da9a1446SChristian Borntraeger 	case KVM_CAP_S390_AIS_MIGRATION:
5767de3f142SJanosch Frank 	case KVM_CAP_S390_VCPU_RESETS:
577b9b2782cSPeter Xu 	case KVM_CAP_SET_GUEST_DEBUG:
57823a60f83SCollin Walling 	case KVM_CAP_S390_DIAG318:
579d004079eSJanis Schoetterl-Glausch 	case KVM_CAP_S390_MEM_OP_EXTENSION:
580d7b0b5ebSCarsten Otte 		r = 1;
581d7b0b5ebSCarsten Otte 		break;
582a43b80b7SMaxim Levitsky 	case KVM_CAP_SET_GUEST_DEBUG2:
583a43b80b7SMaxim Levitsky 		r = KVM_GUESTDBG_VALID_MASK;
584a43b80b7SMaxim Levitsky 		break;
585a4499382SJanosch Frank 	case KVM_CAP_S390_HPAGE_1M:
586a4499382SJanosch Frank 		r = 0;
58740ebdb8eSJanosch Frank 		if (hpage && !kvm_is_ucontrol(kvm))
588a4499382SJanosch Frank 			r = 1;
589a4499382SJanosch Frank 		break;
59041408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
59141408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
59241408c28SThomas Huth 		break;
593e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
594e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
595a86cb413SThomas Huth 	case KVM_CAP_MAX_VCPU_ID:
59676a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
597a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
598a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
599a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
60076a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
60182cc27efSVitaly Kuznetsov 		if (ext == KVM_CAP_NR_VCPUS)
60282cc27efSVitaly Kuznetsov 			r = min_t(unsigned int, num_online_cpus(), r);
603e726b1bdSChristian Borntraeger 		break;
6041526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
605abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
6061526bf9cSChristian Borntraeger 		break;
60768c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
60868c55750SEric Farman 		r = MACHINE_HAS_VX;
60968c55750SEric Farman 		break;
610c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
611c6e5f166SFan Zhang 		r = test_facility(64);
612c6e5f166SFan Zhang 		break;
6134e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
6144e0b1ab7SFan Zhang 		r = test_facility(133);
6154e0b1ab7SFan Zhang 		break;
61635b3fde6SChristian Borntraeger 	case KVM_CAP_S390_BPB:
61735b3fde6SChristian Borntraeger 		r = test_facility(82);
61835b3fde6SChristian Borntraeger 		break;
61913da9ae1SChristian Borntraeger 	case KVM_CAP_S390_PROTECTED:
62013da9ae1SChristian Borntraeger 		r = is_prot_virt_host();
62113da9ae1SChristian Borntraeger 		break;
622e9bf3acbSJanosch Frank 	case KVM_CAP_S390_PROTECTED_DUMP: {
623e9bf3acbSJanosch Frank 		u64 pv_cmds_dump[] = {
624e9bf3acbSJanosch Frank 			BIT_UVC_CMD_DUMP_INIT,
625e9bf3acbSJanosch Frank 			BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE,
626e9bf3acbSJanosch Frank 			BIT_UVC_CMD_DUMP_CPU,
627e9bf3acbSJanosch Frank 			BIT_UVC_CMD_DUMP_COMPLETE,
628e9bf3acbSJanosch Frank 		};
629e9bf3acbSJanosch Frank 		int i;
630e9bf3acbSJanosch Frank 
631e9bf3acbSJanosch Frank 		r = is_prot_virt_host();
632e9bf3acbSJanosch Frank 
633e9bf3acbSJanosch Frank 		for (i = 0; i < ARRAY_SIZE(pv_cmds_dump); i++) {
634e9bf3acbSJanosch Frank 			if (!test_bit_inv(pv_cmds_dump[i],
635e9bf3acbSJanosch Frank 					  (unsigned long *)&uv_info.inst_calls_list)) {
636e9bf3acbSJanosch Frank 				r = 0;
637e9bf3acbSJanosch Frank 				break;
638e9bf3acbSJanosch Frank 			}
639e9bf3acbSJanosch Frank 		}
640e9bf3acbSJanosch Frank 		break;
641e9bf3acbSJanosch Frank 	}
642db1c875eSMatthew Rosato 	case KVM_CAP_S390_ZPCI_OP:
643db1c875eSMatthew Rosato 		r = kvm_s390_pci_interp_allowed();
644db1c875eSMatthew Rosato 		break;
645f5ecfee9SPierre Morel 	case KVM_CAP_S390_CPU_TOPOLOGY:
646f5ecfee9SPierre Morel 		r = test_facility(11);
647f5ecfee9SPierre Morel 		break;
6482bd0ac4eSCarsten Otte 	default:
649d7b0b5ebSCarsten Otte 		r = 0;
650b0c632dbSHeiko Carstens 	}
651d7b0b5ebSCarsten Otte 	return r;
6522bd0ac4eSCarsten Otte }
653b0c632dbSHeiko Carstens 
6540dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
65515f36ebdSJason J. Herne {
6560959e168SJanosch Frank 	int i;
65715f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
6580959e168SJanosch Frank 	unsigned long gaddr, vmaddr;
65915f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
6600959e168SJanosch Frank 	DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
66115f36ebdSJason J. Herne 
6620959e168SJanosch Frank 	/* Loop over all guest segments */
6630959e168SJanosch Frank 	cur_gfn = memslot->base_gfn;
66415f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
6650959e168SJanosch Frank 	for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
6660959e168SJanosch Frank 		gaddr = gfn_to_gpa(cur_gfn);
6670959e168SJanosch Frank 		vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
6680959e168SJanosch Frank 		if (kvm_is_error_hva(vmaddr))
6690959e168SJanosch Frank 			continue;
67015f36ebdSJason J. Herne 
6710959e168SJanosch Frank 		bitmap_zero(bitmap, _PAGE_ENTRIES);
6720959e168SJanosch Frank 		gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
6730959e168SJanosch Frank 		for (i = 0; i < _PAGE_ENTRIES; i++) {
6740959e168SJanosch Frank 			if (test_bit(i, bitmap))
6750959e168SJanosch Frank 				mark_page_dirty(kvm, cur_gfn + i);
6760959e168SJanosch Frank 		}
6770959e168SJanosch Frank 
6781763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
6791763f8d0SChristian Borntraeger 			return;
68070c88a00SChristian Borntraeger 		cond_resched();
68115f36ebdSJason J. Herne 	}
68215f36ebdSJason J. Herne }
68315f36ebdSJason J. Herne 
684b0c632dbSHeiko Carstens /* Section: vm related */
685a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
686a6e2f683SEugene (jno) Dvurechenski 
687b0c632dbSHeiko Carstens /*
688b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
689b0c632dbSHeiko Carstens  */
690b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
691b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
692b0c632dbSHeiko Carstens {
69315f36ebdSJason J. Herne 	int r;
69415f36ebdSJason J. Herne 	unsigned long n;
69515f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
6962a49f61dSSean Christopherson 	int is_dirty;
69715f36ebdSJason J. Herne 
698e1e8a962SJanosch Frank 	if (kvm_is_ucontrol(kvm))
699e1e8a962SJanosch Frank 		return -EINVAL;
700e1e8a962SJanosch Frank 
70115f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
70215f36ebdSJason J. Herne 
70315f36ebdSJason J. Herne 	r = -EINVAL;
70415f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
70515f36ebdSJason J. Herne 		goto out;
70615f36ebdSJason J. Herne 
7072a49f61dSSean Christopherson 	r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
70815f36ebdSJason J. Herne 	if (r)
70915f36ebdSJason J. Herne 		goto out;
71015f36ebdSJason J. Herne 
71115f36ebdSJason J. Herne 	/* Clear the dirty log */
71215f36ebdSJason J. Herne 	if (is_dirty) {
71315f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
71415f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
71515f36ebdSJason J. Herne 	}
71615f36ebdSJason J. Herne 	r = 0;
71715f36ebdSJason J. Herne out:
71815f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
71915f36ebdSJason J. Herne 	return r;
720b0c632dbSHeiko Carstens }
721b0c632dbSHeiko Carstens 
7226502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
7236502a34cSDavid Hildenbrand {
72446808a4cSMarc Zyngier 	unsigned long i;
7256502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
7266502a34cSDavid Hildenbrand 
7276502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
7286502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
7296502a34cSDavid Hildenbrand 	}
7306502a34cSDavid Hildenbrand }
7316502a34cSDavid Hildenbrand 
732e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
733d938dc55SCornelia Huck {
734d938dc55SCornelia Huck 	int r;
735d938dc55SCornelia Huck 
736d938dc55SCornelia Huck 	if (cap->flags)
737d938dc55SCornelia Huck 		return -EINVAL;
738d938dc55SCornelia Huck 
739d938dc55SCornelia Huck 	switch (cap->cap) {
74084223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
741c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
74284223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
74384223598SCornelia Huck 		r = 0;
74484223598SCornelia Huck 		break;
7452444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
746c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
7472444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
7482444b352SDavid Hildenbrand 		r = 0;
7492444b352SDavid Hildenbrand 		break;
75068c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
7515967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
752a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
7535967c17bSDavid Hildenbrand 			r = -EBUSY;
7545967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
755c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
756c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
7572f87d942SGuenther Hutzl 			if (test_facility(134)) {
7582f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
7592f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_list, 134);
7602f87d942SGuenther Hutzl 			}
76153743aa7SMaxim Samoylov 			if (test_facility(135)) {
76253743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
76353743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_list, 135);
76453743aa7SMaxim Samoylov 			}
7657832e91cSChristian Borntraeger 			if (test_facility(148)) {
7667832e91cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 148);
7677832e91cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 148);
7687832e91cSChristian Borntraeger 			}
769d5cb6ab1SChristian Borntraeger 			if (test_facility(152)) {
770d5cb6ab1SChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 152);
771d5cb6ab1SChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 152);
772d5cb6ab1SChristian Borntraeger 			}
7731f703d2cSChristian Borntraeger 			if (test_facility(192)) {
7741f703d2cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 192);
7751f703d2cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 192);
7761f703d2cSChristian Borntraeger 			}
77718280d8bSMichael Mueller 			r = 0;
77818280d8bSMichael Mueller 		} else
77918280d8bSMichael Mueller 			r = -EINVAL;
7805967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
781c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
782c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
78368c55750SEric Farman 		break;
784c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
785c6e5f166SFan Zhang 		r = -EINVAL;
786c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
787a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
788c6e5f166SFan Zhang 			r = -EBUSY;
789c6e5f166SFan Zhang 		} else if (test_facility(64)) {
790c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
791c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
792c6e5f166SFan Zhang 			r = 0;
793c6e5f166SFan Zhang 		}
794c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
795c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
796c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
797c6e5f166SFan Zhang 		break;
79847a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
79947a4693eSYi Min Zhao 		mutex_lock(&kvm->lock);
80047a4693eSYi Min Zhao 		if (kvm->created_vcpus) {
80147a4693eSYi Min Zhao 			r = -EBUSY;
80247a4693eSYi Min Zhao 		} else {
80347a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_mask, 72);
80447a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_list, 72);
80547a4693eSYi Min Zhao 			r = 0;
80647a4693eSYi Min Zhao 		}
80747a4693eSYi Min Zhao 		mutex_unlock(&kvm->lock);
80847a4693eSYi Min Zhao 		VM_EVENT(kvm, 3, "ENABLE: AIS %s",
80947a4693eSYi Min Zhao 			 r ? "(not available)" : "(success)");
81047a4693eSYi Min Zhao 		break;
8114e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
8124e0b1ab7SFan Zhang 		r = -EINVAL;
8134e0b1ab7SFan Zhang 		mutex_lock(&kvm->lock);
814241e3ec0SChristian Borntraeger 		if (kvm->created_vcpus) {
8154e0b1ab7SFan Zhang 			r = -EBUSY;
8164e0b1ab7SFan Zhang 		} else if (test_facility(133)) {
8174e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_mask, 133);
8184e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_list, 133);
8194e0b1ab7SFan Zhang 			r = 0;
8204e0b1ab7SFan Zhang 		}
8214e0b1ab7SFan Zhang 		mutex_unlock(&kvm->lock);
8224e0b1ab7SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
8234e0b1ab7SFan Zhang 			 r ? "(not available)" : "(success)");
8244e0b1ab7SFan Zhang 		break;
825a4499382SJanosch Frank 	case KVM_CAP_S390_HPAGE_1M:
826a4499382SJanosch Frank 		mutex_lock(&kvm->lock);
827a4499382SJanosch Frank 		if (kvm->created_vcpus)
828a4499382SJanosch Frank 			r = -EBUSY;
82940ebdb8eSJanosch Frank 		else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
830a4499382SJanosch Frank 			r = -EINVAL;
831a4499382SJanosch Frank 		else {
832a4499382SJanosch Frank 			r = 0;
833d8ed45c5SMichel Lespinasse 			mmap_write_lock(kvm->mm);
834a4499382SJanosch Frank 			kvm->mm->context.allow_gmap_hpage_1m = 1;
835d8ed45c5SMichel Lespinasse 			mmap_write_unlock(kvm->mm);
836a4499382SJanosch Frank 			/*
837a4499382SJanosch Frank 			 * We might have to create fake 4k page
838a4499382SJanosch Frank 			 * tables. To avoid that the hardware works on
839a4499382SJanosch Frank 			 * stale PGSTEs, we emulate these instructions.
840a4499382SJanosch Frank 			 */
841a4499382SJanosch Frank 			kvm->arch.use_skf = 0;
842a4499382SJanosch Frank 			kvm->arch.use_pfmfi = 0;
843a4499382SJanosch Frank 		}
844a4499382SJanosch Frank 		mutex_unlock(&kvm->lock);
845a4499382SJanosch Frank 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
846a4499382SJanosch Frank 			 r ? "(not available)" : "(success)");
847a4499382SJanosch Frank 		break;
848e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
849c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
850e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
851e44fc8c9SEkaterina Tumanova 		r = 0;
852e44fc8c9SEkaterina Tumanova 		break;
8536502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
8546502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
8556502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
8566502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
8576502a34cSDavid Hildenbrand 		r = 0;
8586502a34cSDavid Hildenbrand 		break;
859f5ecfee9SPierre Morel 	case KVM_CAP_S390_CPU_TOPOLOGY:
860f5ecfee9SPierre Morel 		r = -EINVAL;
861f5ecfee9SPierre Morel 		mutex_lock(&kvm->lock);
862f5ecfee9SPierre Morel 		if (kvm->created_vcpus) {
863f5ecfee9SPierre Morel 			r = -EBUSY;
864f5ecfee9SPierre Morel 		} else if (test_facility(11)) {
865f5ecfee9SPierre Morel 			set_kvm_facility(kvm->arch.model.fac_mask, 11);
866f5ecfee9SPierre Morel 			set_kvm_facility(kvm->arch.model.fac_list, 11);
867f5ecfee9SPierre Morel 			r = 0;
868f5ecfee9SPierre Morel 		}
869f5ecfee9SPierre Morel 		mutex_unlock(&kvm->lock);
870f5ecfee9SPierre Morel 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_CPU_TOPOLOGY %s",
871f5ecfee9SPierre Morel 			 r ? "(not available)" : "(success)");
872f5ecfee9SPierre Morel 		break;
873d938dc55SCornelia Huck 	default:
874d938dc55SCornelia Huck 		r = -EINVAL;
875d938dc55SCornelia Huck 		break;
876d938dc55SCornelia Huck 	}
877d938dc55SCornelia Huck 	return r;
878d938dc55SCornelia Huck }
879d938dc55SCornelia Huck 
8808c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
8818c0a7ce6SDominik Dingel {
8828c0a7ce6SDominik Dingel 	int ret;
8838c0a7ce6SDominik Dingel 
8848c0a7ce6SDominik Dingel 	switch (attr->attr) {
8858c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
8868c0a7ce6SDominik Dingel 		ret = 0;
887c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
888a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
889a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
8908c0a7ce6SDominik Dingel 			ret = -EFAULT;
8918c0a7ce6SDominik Dingel 		break;
8928c0a7ce6SDominik Dingel 	default:
8938c0a7ce6SDominik Dingel 		ret = -ENXIO;
8948c0a7ce6SDominik Dingel 		break;
8958c0a7ce6SDominik Dingel 	}
8968c0a7ce6SDominik Dingel 	return ret;
8978c0a7ce6SDominik Dingel }
8988c0a7ce6SDominik Dingel 
8998c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
9004f718eabSDominik Dingel {
9014f718eabSDominik Dingel 	int ret;
9024f718eabSDominik Dingel 	unsigned int idx;
9034f718eabSDominik Dingel 	switch (attr->attr) {
9044f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
905f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
906c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
907e6db1d61SDominik Dingel 			break;
908e6db1d61SDominik Dingel 
909c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
9104f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
911a4499382SJanosch Frank 		if (kvm->created_vcpus)
912a4499382SJanosch Frank 			ret = -EBUSY;
913a4499382SJanosch Frank 		else if (kvm->mm->context.allow_gmap_hpage_1m)
914a4499382SJanosch Frank 			ret = -EINVAL;
915a4499382SJanosch Frank 		else {
9164f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
917c9f0a2b8SJanosch Frank 			/* Not compatible with cmma. */
918c9f0a2b8SJanosch Frank 			kvm->arch.use_pfmfi = 0;
9194f718eabSDominik Dingel 			ret = 0;
9204f718eabSDominik Dingel 		}
9214f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
9224f718eabSDominik Dingel 		break;
9234f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
924f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
925f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
926f9cbd9b0SDavid Hildenbrand 			break;
927c3489155SDominik Dingel 		ret = -EINVAL;
928c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
929c3489155SDominik Dingel 			break;
930c3489155SDominik Dingel 
931c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
9324f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
9334f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
934a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
9354f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
9364f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
9374f718eabSDominik Dingel 		ret = 0;
9384f718eabSDominik Dingel 		break;
9398c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
9408c0a7ce6SDominik Dingel 		unsigned long new_limit;
9418c0a7ce6SDominik Dingel 
9428c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
9438c0a7ce6SDominik Dingel 			return -EINVAL;
9448c0a7ce6SDominik Dingel 
9458c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
9468c0a7ce6SDominik Dingel 			return -EFAULT;
9478c0a7ce6SDominik Dingel 
948a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
949a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
9508c0a7ce6SDominik Dingel 			return -E2BIG;
9518c0a7ce6SDominik Dingel 
952a3a92c31SDominik Dingel 		if (!new_limit)
953a3a92c31SDominik Dingel 			return -EINVAL;
954a3a92c31SDominik Dingel 
9556ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
956a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
957a3a92c31SDominik Dingel 			new_limit -= 1;
958a3a92c31SDominik Dingel 
9598c0a7ce6SDominik Dingel 		ret = -EBUSY;
9608c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
961a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
9626ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
9636ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
9648c0a7ce6SDominik Dingel 
9658c0a7ce6SDominik Dingel 			if (!new) {
9668c0a7ce6SDominik Dingel 				ret = -ENOMEM;
9678c0a7ce6SDominik Dingel 			} else {
9686ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
9698c0a7ce6SDominik Dingel 				new->private = kvm;
9708c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
9718c0a7ce6SDominik Dingel 				ret = 0;
9728c0a7ce6SDominik Dingel 			}
9738c0a7ce6SDominik Dingel 		}
9748c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
975a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
976a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
977a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
9788c0a7ce6SDominik Dingel 		break;
9798c0a7ce6SDominik Dingel 	}
9804f718eabSDominik Dingel 	default:
9814f718eabSDominik Dingel 		ret = -ENXIO;
9824f718eabSDominik Dingel 		break;
9834f718eabSDominik Dingel 	}
9844f718eabSDominik Dingel 	return ret;
9854f718eabSDominik Dingel }
9864f718eabSDominik Dingel 
987a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
988a374e892STony Krowiak 
98920c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
990a374e892STony Krowiak {
991a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
99246808a4cSMarc Zyngier 	unsigned long i;
993a374e892STony Krowiak 
99420c922f0STony Krowiak 	kvm_s390_vcpu_block_all(kvm);
99520c922f0STony Krowiak 
9963194cdb7SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
99720c922f0STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
9983194cdb7SDavid Hildenbrand 		/* recreate the shadow crycb by leaving the VSIE handler */
9993194cdb7SDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
10003194cdb7SDavid Hildenbrand 	}
100120c922f0STony Krowiak 
100220c922f0STony Krowiak 	kvm_s390_vcpu_unblock_all(kvm);
100320c922f0STony Krowiak }
100420c922f0STony Krowiak 
100520c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
100620c922f0STony Krowiak {
1007a374e892STony Krowiak 	mutex_lock(&kvm->lock);
1008a374e892STony Krowiak 	switch (attr->attr) {
1009a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
10108e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
10118e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
101237940fb0STony Krowiak 			return -EINVAL;
10138e41bd54SChristian Borntraeger 		}
1014a374e892STony Krowiak 		get_random_bytes(
1015a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1016a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1017a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
1018c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
1019a374e892STony Krowiak 		break;
1020a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
10218e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
10228e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
102337940fb0STony Krowiak 			return -EINVAL;
10248e41bd54SChristian Borntraeger 		}
1025a374e892STony Krowiak 		get_random_bytes(
1026a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1027a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
1028a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
1029c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
1030a374e892STony Krowiak 		break;
1031a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
10328e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
10338e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
103437940fb0STony Krowiak 			return -EINVAL;
10358e41bd54SChristian Borntraeger 		}
1036a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
1037a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
1038a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1039c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
1040a374e892STony Krowiak 		break;
1041a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
10428e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
10438e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
104437940fb0STony Krowiak 			return -EINVAL;
10458e41bd54SChristian Borntraeger 		}
1046a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
1047a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
1048a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
1049c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
1050a374e892STony Krowiak 		break;
105137940fb0STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_APIE:
105237940fb0STony Krowiak 		if (!ap_instructions_available()) {
105337940fb0STony Krowiak 			mutex_unlock(&kvm->lock);
105437940fb0STony Krowiak 			return -EOPNOTSUPP;
105537940fb0STony Krowiak 		}
105637940fb0STony Krowiak 		kvm->arch.crypto.apie = 1;
105737940fb0STony Krowiak 		break;
105837940fb0STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_APIE:
105937940fb0STony Krowiak 		if (!ap_instructions_available()) {
106037940fb0STony Krowiak 			mutex_unlock(&kvm->lock);
106137940fb0STony Krowiak 			return -EOPNOTSUPP;
106237940fb0STony Krowiak 		}
106337940fb0STony Krowiak 		kvm->arch.crypto.apie = 0;
106437940fb0STony Krowiak 		break;
1065a374e892STony Krowiak 	default:
1066a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
1067a374e892STony Krowiak 		return -ENXIO;
1068a374e892STony Krowiak 	}
1069a374e892STony Krowiak 
107020c922f0STony Krowiak 	kvm_s390_vcpu_crypto_reset_all(kvm);
1071a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
1072a374e892STony Krowiak 	return 0;
1073a374e892STony Krowiak }
1074a374e892STony Krowiak 
10753f4bbb43SMatthew Rosato static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu)
10763f4bbb43SMatthew Rosato {
10773f4bbb43SMatthew Rosato 	/* Only set the ECB bits after guest requests zPCI interpretation */
10783f4bbb43SMatthew Rosato 	if (!vcpu->kvm->arch.use_zpci_interp)
10793f4bbb43SMatthew Rosato 		return;
10803f4bbb43SMatthew Rosato 
10813f4bbb43SMatthew Rosato 	vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI;
10823f4bbb43SMatthew Rosato 	vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI;
10833f4bbb43SMatthew Rosato }
10843f4bbb43SMatthew Rosato 
10853f4bbb43SMatthew Rosato void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm)
10863f4bbb43SMatthew Rosato {
10873f4bbb43SMatthew Rosato 	struct kvm_vcpu *vcpu;
10883f4bbb43SMatthew Rosato 	unsigned long i;
10893f4bbb43SMatthew Rosato 
10903f4bbb43SMatthew Rosato 	lockdep_assert_held(&kvm->lock);
10913f4bbb43SMatthew Rosato 
10923f4bbb43SMatthew Rosato 	if (!kvm_s390_pci_interp_allowed())
10933f4bbb43SMatthew Rosato 		return;
10943f4bbb43SMatthew Rosato 
10953f4bbb43SMatthew Rosato 	/*
10963f4bbb43SMatthew Rosato 	 * If host is configured for PCI and the necessary facilities are
10973f4bbb43SMatthew Rosato 	 * available, turn on interpretation for the life of this guest
10983f4bbb43SMatthew Rosato 	 */
10993f4bbb43SMatthew Rosato 	kvm->arch.use_zpci_interp = 1;
11003f4bbb43SMatthew Rosato 
11013f4bbb43SMatthew Rosato 	kvm_s390_vcpu_block_all(kvm);
11023f4bbb43SMatthew Rosato 
11033f4bbb43SMatthew Rosato 	kvm_for_each_vcpu(i, vcpu, kvm) {
11043f4bbb43SMatthew Rosato 		kvm_s390_vcpu_pci_setup(vcpu);
11053f4bbb43SMatthew Rosato 		kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
11063f4bbb43SMatthew Rosato 	}
11073f4bbb43SMatthew Rosato 
11083f4bbb43SMatthew Rosato 	kvm_s390_vcpu_unblock_all(kvm);
11093f4bbb43SMatthew Rosato }
11103f4bbb43SMatthew Rosato 
1111190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
1112190df4a2SClaudio Imbrenda {
111346808a4cSMarc Zyngier 	unsigned long cx;
1114190df4a2SClaudio Imbrenda 	struct kvm_vcpu *vcpu;
1115190df4a2SClaudio Imbrenda 
1116190df4a2SClaudio Imbrenda 	kvm_for_each_vcpu(cx, vcpu, kvm)
1117190df4a2SClaudio Imbrenda 		kvm_s390_sync_request(req, vcpu);
1118190df4a2SClaudio Imbrenda }
1119190df4a2SClaudio Imbrenda 
1120190df4a2SClaudio Imbrenda /*
1121190df4a2SClaudio Imbrenda  * Must be called with kvm->srcu held to avoid races on memslots, and with
11221de1ea7eSChristian Borntraeger  * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
1123190df4a2SClaudio Imbrenda  */
1124190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm)
1125190df4a2SClaudio Imbrenda {
1126190df4a2SClaudio Imbrenda 	struct kvm_memory_slot *ms;
1127190df4a2SClaudio Imbrenda 	struct kvm_memslots *slots;
1128afdad616SClaudio Imbrenda 	unsigned long ram_pages = 0;
1129a54d8066SMaciej S. Szmigiero 	int bkt;
1130190df4a2SClaudio Imbrenda 
1131190df4a2SClaudio Imbrenda 	/* migration mode already enabled */
1132afdad616SClaudio Imbrenda 	if (kvm->arch.migration_mode)
1133190df4a2SClaudio Imbrenda 		return 0;
1134190df4a2SClaudio Imbrenda 	slots = kvm_memslots(kvm);
1135a54d8066SMaciej S. Szmigiero 	if (!slots || kvm_memslots_empty(slots))
1136190df4a2SClaudio Imbrenda 		return -EINVAL;
1137190df4a2SClaudio Imbrenda 
1138afdad616SClaudio Imbrenda 	if (!kvm->arch.use_cmma) {
1139afdad616SClaudio Imbrenda 		kvm->arch.migration_mode = 1;
1140afdad616SClaudio Imbrenda 		return 0;
1141190df4a2SClaudio Imbrenda 	}
1142190df4a2SClaudio Imbrenda 	/* mark all the pages in active slots as dirty */
1143a54d8066SMaciej S. Szmigiero 	kvm_for_each_memslot(ms, bkt, slots) {
114413a17cc0SIgor Mammedov 		if (!ms->dirty_bitmap)
114513a17cc0SIgor Mammedov 			return -EINVAL;
1146afdad616SClaudio Imbrenda 		/*
1147afdad616SClaudio Imbrenda 		 * The second half of the bitmap is only used on x86,
1148afdad616SClaudio Imbrenda 		 * and would be wasted otherwise, so we put it to good
1149afdad616SClaudio Imbrenda 		 * use here to keep track of the state of the storage
1150afdad616SClaudio Imbrenda 		 * attributes.
1151afdad616SClaudio Imbrenda 		 */
1152afdad616SClaudio Imbrenda 		memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
1153afdad616SClaudio Imbrenda 		ram_pages += ms->npages;
1154190df4a2SClaudio Imbrenda 	}
1155afdad616SClaudio Imbrenda 	atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
1156afdad616SClaudio Imbrenda 	kvm->arch.migration_mode = 1;
1157190df4a2SClaudio Imbrenda 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
1158190df4a2SClaudio Imbrenda 	return 0;
1159190df4a2SClaudio Imbrenda }
1160190df4a2SClaudio Imbrenda 
1161190df4a2SClaudio Imbrenda /*
11621de1ea7eSChristian Borntraeger  * Must be called with kvm->slots_lock to avoid races with ourselves and
1163190df4a2SClaudio Imbrenda  * kvm_s390_vm_start_migration.
1164190df4a2SClaudio Imbrenda  */
1165190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm)
1166190df4a2SClaudio Imbrenda {
1167190df4a2SClaudio Imbrenda 	/* migration mode already disabled */
1168afdad616SClaudio Imbrenda 	if (!kvm->arch.migration_mode)
1169190df4a2SClaudio Imbrenda 		return 0;
1170afdad616SClaudio Imbrenda 	kvm->arch.migration_mode = 0;
1171afdad616SClaudio Imbrenda 	if (kvm->arch.use_cmma)
1172190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
1173190df4a2SClaudio Imbrenda 	return 0;
1174190df4a2SClaudio Imbrenda }
1175190df4a2SClaudio Imbrenda 
1176190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm,
1177190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
1178190df4a2SClaudio Imbrenda {
11791de1ea7eSChristian Borntraeger 	int res = -ENXIO;
1180190df4a2SClaudio Imbrenda 
11811de1ea7eSChristian Borntraeger 	mutex_lock(&kvm->slots_lock);
1182190df4a2SClaudio Imbrenda 	switch (attr->attr) {
1183190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_START:
1184190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_start_migration(kvm);
1185190df4a2SClaudio Imbrenda 		break;
1186190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_STOP:
1187190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_stop_migration(kvm);
1188190df4a2SClaudio Imbrenda 		break;
1189190df4a2SClaudio Imbrenda 	default:
1190190df4a2SClaudio Imbrenda 		break;
1191190df4a2SClaudio Imbrenda 	}
11921de1ea7eSChristian Borntraeger 	mutex_unlock(&kvm->slots_lock);
1193190df4a2SClaudio Imbrenda 
1194190df4a2SClaudio Imbrenda 	return res;
1195190df4a2SClaudio Imbrenda }
1196190df4a2SClaudio Imbrenda 
1197190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm,
1198190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
1199190df4a2SClaudio Imbrenda {
1200afdad616SClaudio Imbrenda 	u64 mig = kvm->arch.migration_mode;
1201190df4a2SClaudio Imbrenda 
1202190df4a2SClaudio Imbrenda 	if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1203190df4a2SClaudio Imbrenda 		return -ENXIO;
1204190df4a2SClaudio Imbrenda 
1205190df4a2SClaudio Imbrenda 	if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1206190df4a2SClaudio Imbrenda 		return -EFAULT;
1207190df4a2SClaudio Imbrenda 	return 0;
1208190df4a2SClaudio Imbrenda }
1209190df4a2SClaudio Imbrenda 
12108fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
12118fa1696eSCollin L. Walling {
12128fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
12138fa1696eSCollin L. Walling 
12148fa1696eSCollin L. Walling 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
12158fa1696eSCollin L. Walling 		return -EFAULT;
12168fa1696eSCollin L. Walling 
12170e7def5fSDavid Hildenbrand 	if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
12188fa1696eSCollin L. Walling 		return -EINVAL;
12190e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
12208fa1696eSCollin L. Walling 
12218fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
12228fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
12238fa1696eSCollin L. Walling 
12248fa1696eSCollin L. Walling 	return 0;
12258fa1696eSCollin L. Walling }
12268fa1696eSCollin L. Walling 
122772f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
122872f25020SJason J. Herne {
122972f25020SJason J. Herne 	u8 gtod_high;
123072f25020SJason J. Herne 
123172f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
123272f25020SJason J. Herne 					   sizeof(gtod_high)))
123372f25020SJason J. Herne 		return -EFAULT;
123472f25020SJason J. Herne 
123572f25020SJason J. Herne 	if (gtod_high != 0)
123672f25020SJason J. Herne 		return -EINVAL;
123758c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
123872f25020SJason J. Herne 
123972f25020SJason J. Herne 	return 0;
124072f25020SJason J. Herne }
124172f25020SJason J. Herne 
124272f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
124372f25020SJason J. Herne {
12440e7def5fSDavid Hildenbrand 	struct kvm_s390_vm_tod_clock gtod = { 0 };
124572f25020SJason J. Herne 
12460e7def5fSDavid Hildenbrand 	if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
12470e7def5fSDavid Hildenbrand 			   sizeof(gtod.tod)))
124872f25020SJason J. Herne 		return -EFAULT;
124972f25020SJason J. Herne 
12500e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
12510e7def5fSDavid Hildenbrand 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
125272f25020SJason J. Herne 	return 0;
125372f25020SJason J. Herne }
125472f25020SJason J. Herne 
125572f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
125672f25020SJason J. Herne {
125772f25020SJason J. Herne 	int ret;
125872f25020SJason J. Herne 
125972f25020SJason J. Herne 	if (attr->flags)
126072f25020SJason J. Herne 		return -EINVAL;
126172f25020SJason J. Herne 
126272f25020SJason J. Herne 	switch (attr->attr) {
12638fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
12648fa1696eSCollin L. Walling 		ret = kvm_s390_set_tod_ext(kvm, attr);
12658fa1696eSCollin L. Walling 		break;
126672f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
126772f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
126872f25020SJason J. Herne 		break;
126972f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
127072f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
127172f25020SJason J. Herne 		break;
127272f25020SJason J. Herne 	default:
127372f25020SJason J. Herne 		ret = -ENXIO;
127472f25020SJason J. Herne 		break;
127572f25020SJason J. Herne 	}
127672f25020SJason J. Herne 	return ret;
127772f25020SJason J. Herne }
127872f25020SJason J. Herne 
127933d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm,
12808fa1696eSCollin L. Walling 				   struct kvm_s390_vm_tod_clock *gtod)
12818fa1696eSCollin L. Walling {
12822cfd7b73SHeiko Carstens 	union tod_clock clk;
12838fa1696eSCollin L. Walling 
12848fa1696eSCollin L. Walling 	preempt_disable();
12858fa1696eSCollin L. Walling 
12862cfd7b73SHeiko Carstens 	store_tod_clock_ext(&clk);
12878fa1696eSCollin L. Walling 
12882cfd7b73SHeiko Carstens 	gtod->tod = clk.tod + kvm->arch.epoch;
128933d1b272SDavid Hildenbrand 	gtod->epoch_idx = 0;
129033d1b272SDavid Hildenbrand 	if (test_kvm_facility(kvm, 139)) {
12912cfd7b73SHeiko Carstens 		gtod->epoch_idx = clk.ei + kvm->arch.epdx;
12922cfd7b73SHeiko Carstens 		if (gtod->tod < clk.tod)
12938fa1696eSCollin L. Walling 			gtod->epoch_idx += 1;
129433d1b272SDavid Hildenbrand 	}
12958fa1696eSCollin L. Walling 
12968fa1696eSCollin L. Walling 	preempt_enable();
12978fa1696eSCollin L. Walling }
12988fa1696eSCollin L. Walling 
12998fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
13008fa1696eSCollin L. Walling {
13018fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
13028fa1696eSCollin L. Walling 
13038fa1696eSCollin L. Walling 	memset(&gtod, 0, sizeof(gtod));
130433d1b272SDavid Hildenbrand 	kvm_s390_get_tod_clock(kvm, &gtod);
13058fa1696eSCollin L. Walling 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
13068fa1696eSCollin L. Walling 		return -EFAULT;
13078fa1696eSCollin L. Walling 
13088fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
13098fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
13108fa1696eSCollin L. Walling 	return 0;
13118fa1696eSCollin L. Walling }
13128fa1696eSCollin L. Walling 
131372f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
131472f25020SJason J. Herne {
131572f25020SJason J. Herne 	u8 gtod_high = 0;
131672f25020SJason J. Herne 
131772f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
131872f25020SJason J. Herne 					 sizeof(gtod_high)))
131972f25020SJason J. Herne 		return -EFAULT;
132058c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
132172f25020SJason J. Herne 
132272f25020SJason J. Herne 	return 0;
132372f25020SJason J. Herne }
132472f25020SJason J. Herne 
132572f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
132672f25020SJason J. Herne {
13275a3d883aSDavid Hildenbrand 	u64 gtod;
132872f25020SJason J. Herne 
132960417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
133072f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
133172f25020SJason J. Herne 		return -EFAULT;
133258c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
133372f25020SJason J. Herne 
133472f25020SJason J. Herne 	return 0;
133572f25020SJason J. Herne }
133672f25020SJason J. Herne 
133772f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
133872f25020SJason J. Herne {
133972f25020SJason J. Herne 	int ret;
134072f25020SJason J. Herne 
134172f25020SJason J. Herne 	if (attr->flags)
134272f25020SJason J. Herne 		return -EINVAL;
134372f25020SJason J. Herne 
134472f25020SJason J. Herne 	switch (attr->attr) {
13458fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
13468fa1696eSCollin L. Walling 		ret = kvm_s390_get_tod_ext(kvm, attr);
13478fa1696eSCollin L. Walling 		break;
134872f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
134972f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
135072f25020SJason J. Herne 		break;
135172f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
135272f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
135372f25020SJason J. Herne 		break;
135472f25020SJason J. Herne 	default:
135572f25020SJason J. Herne 		ret = -ENXIO;
135672f25020SJason J. Herne 		break;
135772f25020SJason J. Herne 	}
135872f25020SJason J. Herne 	return ret;
135972f25020SJason J. Herne }
136072f25020SJason J. Herne 
1361658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1362658b6edaSMichael Mueller {
1363658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1364053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
1365658b6edaSMichael Mueller 	int ret = 0;
1366658b6edaSMichael Mueller 
1367658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
1368a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
1369658b6edaSMichael Mueller 		ret = -EBUSY;
1370658b6edaSMichael Mueller 		goto out;
1371658b6edaSMichael Mueller 	}
1372c4196218SChristian Borntraeger 	proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1373658b6edaSMichael Mueller 	if (!proc) {
1374658b6edaSMichael Mueller 		ret = -ENOMEM;
1375658b6edaSMichael Mueller 		goto out;
1376658b6edaSMichael Mueller 	}
1377658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
1378658b6edaSMichael Mueller 			    sizeof(*proc))) {
13799bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
1380053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
1381053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
13820487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
1383053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
1384053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
1385053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
1386053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
1387053dd230SDavid Hildenbrand 			else
1388658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
1389053dd230SDavid Hildenbrand 		}
1390c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
1391658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
1392a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1393a8c39dd7SChristian Borntraeger 			 kvm->arch.model.ibc,
1394a8c39dd7SChristian Borntraeger 			 kvm->arch.model.cpuid);
1395a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1396a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[0],
1397a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[1],
1398a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[2]);
1399658b6edaSMichael Mueller 	} else
1400658b6edaSMichael Mueller 		ret = -EFAULT;
1401658b6edaSMichael Mueller 	kfree(proc);
1402658b6edaSMichael Mueller out:
1403658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
1404658b6edaSMichael Mueller 	return ret;
1405658b6edaSMichael Mueller }
1406658b6edaSMichael Mueller 
140715c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
140815c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
140915c9705fSDavid Hildenbrand {
141015c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
141115c9705fSDavid Hildenbrand 
141215c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
141315c9705fSDavid Hildenbrand 		return -EFAULT;
141415c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
141515c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
141615c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
141715c9705fSDavid Hildenbrand 		return -EINVAL;
141815c9705fSDavid Hildenbrand 
141915c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
14202f8311c9SChristian Borntraeger 	if (kvm->created_vcpus) {
14212f8311c9SChristian Borntraeger 		mutex_unlock(&kvm->lock);
14222f8311c9SChristian Borntraeger 		return -EBUSY;
14232f8311c9SChristian Borntraeger 	}
1424da0f8e95SYury Norov 	bitmap_from_arr64(kvm->arch.cpu_feat, data.feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
142515c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
14262f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
14272f8311c9SChristian Borntraeger 			 data.feat[0],
14282f8311c9SChristian Borntraeger 			 data.feat[1],
14292f8311c9SChristian Borntraeger 			 data.feat[2]);
14302f8311c9SChristian Borntraeger 	return 0;
143115c9705fSDavid Hildenbrand }
143215c9705fSDavid Hildenbrand 
14330a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
14340a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
14350a763c78SDavid Hildenbrand {
1436346fa2f8SChristian Borntraeger 	mutex_lock(&kvm->lock);
1437346fa2f8SChristian Borntraeger 	if (kvm->created_vcpus) {
1438346fa2f8SChristian Borntraeger 		mutex_unlock(&kvm->lock);
1439346fa2f8SChristian Borntraeger 		return -EBUSY;
1440346fa2f8SChristian Borntraeger 	}
1441346fa2f8SChristian Borntraeger 
1442346fa2f8SChristian Borntraeger 	if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1443346fa2f8SChristian Borntraeger 			   sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1444346fa2f8SChristian Borntraeger 		mutex_unlock(&kvm->lock);
1445346fa2f8SChristian Borntraeger 		return -EFAULT;
1446346fa2f8SChristian Borntraeger 	}
1447346fa2f8SChristian Borntraeger 	mutex_unlock(&kvm->lock);
1448346fa2f8SChristian Borntraeger 
144911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
145011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
145111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
145211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
145311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
145411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
145511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
145611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
145711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
145811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
145911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
146011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
146111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
146211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
146311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KM     subfunc 0x%16.16lx.%16.16lx",
146411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
146511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
146611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
146711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
146811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
146911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
147011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
147111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
147211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
147311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
147411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
147511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
147611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
147711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
147811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
147911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
148011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
148111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
148211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
148311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
148411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
148511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
148611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
148711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
148811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
148911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
149011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
149111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
149211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
149313209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
149413209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
149513209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1496173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1497173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1498173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1499173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1500173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
15014f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
15024f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
15034f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
15044f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
15054f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
150611ba5961SChristian Borntraeger 
1507346fa2f8SChristian Borntraeger 	return 0;
15080a763c78SDavid Hildenbrand }
15090a763c78SDavid Hildenbrand 
1510658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1511658b6edaSMichael Mueller {
1512658b6edaSMichael Mueller 	int ret = -ENXIO;
1513658b6edaSMichael Mueller 
1514658b6edaSMichael Mueller 	switch (attr->attr) {
1515658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1516658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
1517658b6edaSMichael Mueller 		break;
151815c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
151915c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
152015c9705fSDavid Hildenbrand 		break;
15210a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
15220a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
15230a763c78SDavid Hildenbrand 		break;
1524658b6edaSMichael Mueller 	}
1525658b6edaSMichael Mueller 	return ret;
1526658b6edaSMichael Mueller }
1527658b6edaSMichael Mueller 
1528658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1529658b6edaSMichael Mueller {
1530658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1531658b6edaSMichael Mueller 	int ret = 0;
1532658b6edaSMichael Mueller 
1533c4196218SChristian Borntraeger 	proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1534658b6edaSMichael Mueller 	if (!proc) {
1535658b6edaSMichael Mueller 		ret = -ENOMEM;
1536658b6edaSMichael Mueller 		goto out;
1537658b6edaSMichael Mueller 	}
15389bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
1539658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
1540c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1541c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1542a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1543a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1544a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1545a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1546a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[0],
1547a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[1],
1548a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[2]);
1549658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1550658b6edaSMichael Mueller 		ret = -EFAULT;
1551658b6edaSMichael Mueller 	kfree(proc);
1552658b6edaSMichael Mueller out:
1553658b6edaSMichael Mueller 	return ret;
1554658b6edaSMichael Mueller }
1555658b6edaSMichael Mueller 
1556658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1557658b6edaSMichael Mueller {
1558658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
1559658b6edaSMichael Mueller 	int ret = 0;
1560658b6edaSMichael Mueller 
1561c4196218SChristian Borntraeger 	mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT);
1562658b6edaSMichael Mueller 	if (!mach) {
1563658b6edaSMichael Mueller 		ret = -ENOMEM;
1564658b6edaSMichael Mueller 		goto out;
1565658b6edaSMichael Mueller 	}
1566658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
156737c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
1568c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1569981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
157017e89e13SSven Schnelle 	memcpy((unsigned long *)&mach->fac_list, stfle_fac_list,
157117e89e13SSven Schnelle 	       sizeof(stfle_fac_list));
1572a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
1573a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1574a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1575a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
1576a8c39dd7SChristian Borntraeger 		 mach->fac_mask[0],
1577a8c39dd7SChristian Borntraeger 		 mach->fac_mask[1],
1578a8c39dd7SChristian Borntraeger 		 mach->fac_mask[2]);
1579a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
1580a8c39dd7SChristian Borntraeger 		 mach->fac_list[0],
1581a8c39dd7SChristian Borntraeger 		 mach->fac_list[1],
1582a8c39dd7SChristian Borntraeger 		 mach->fac_list[2]);
1583658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1584658b6edaSMichael Mueller 		ret = -EFAULT;
1585658b6edaSMichael Mueller 	kfree(mach);
1586658b6edaSMichael Mueller out:
1587658b6edaSMichael Mueller 	return ret;
1588658b6edaSMichael Mueller }
1589658b6edaSMichael Mueller 
159015c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
159115c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
159215c9705fSDavid Hildenbrand {
159315c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
159415c9705fSDavid Hildenbrand 
1595da0f8e95SYury Norov 	bitmap_to_arr64(data.feat, kvm->arch.cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
159615c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
159715c9705fSDavid Hildenbrand 		return -EFAULT;
15982f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
15992f8311c9SChristian Borntraeger 			 data.feat[0],
16002f8311c9SChristian Borntraeger 			 data.feat[1],
16012f8311c9SChristian Borntraeger 			 data.feat[2]);
160215c9705fSDavid Hildenbrand 	return 0;
160315c9705fSDavid Hildenbrand }
160415c9705fSDavid Hildenbrand 
160515c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
160615c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
160715c9705fSDavid Hildenbrand {
160815c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
160915c9705fSDavid Hildenbrand 
1610da0f8e95SYury Norov 	bitmap_to_arr64(data.feat, kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
161115c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
161215c9705fSDavid Hildenbrand 		return -EFAULT;
16132f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host feat:  0x%16.16llx.0x%16.16llx.0x%16.16llx",
16142f8311c9SChristian Borntraeger 			 data.feat[0],
16152f8311c9SChristian Borntraeger 			 data.feat[1],
16162f8311c9SChristian Borntraeger 			 data.feat[2]);
161715c9705fSDavid Hildenbrand 	return 0;
161815c9705fSDavid Hildenbrand }
161915c9705fSDavid Hildenbrand 
16200a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
16210a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
16220a763c78SDavid Hildenbrand {
1623346fa2f8SChristian Borntraeger 	if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1624346fa2f8SChristian Borntraeger 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
1625346fa2f8SChristian Borntraeger 		return -EFAULT;
1626346fa2f8SChristian Borntraeger 
162711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
162811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
162911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
163011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
163111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
163211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
163311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
163411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
163511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
163611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
163711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
163811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
163911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
164011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
164111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KM     subfunc 0x%16.16lx.%16.16lx",
164211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
164311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
164411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
164511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
164611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
164711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
164811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
164911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
165011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
165111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
165211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
165311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
165411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
165511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
165611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
165711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
165811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
165911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
166011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
166111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
166211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
166311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
166411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
166511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
166611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
166711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
166811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
166911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
167011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
167113209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
167213209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
167313209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1674173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1675173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1676173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1677173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1678173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
16794f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
16804f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
16814f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
16824f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
16834f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
168411ba5961SChristian Borntraeger 
1685346fa2f8SChristian Borntraeger 	return 0;
16860a763c78SDavid Hildenbrand }
16870a763c78SDavid Hildenbrand 
16880a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
16890a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
16900a763c78SDavid Hildenbrand {
16910a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
16920a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
16930a763c78SDavid Hildenbrand 		return -EFAULT;
169411ba5961SChristian Borntraeger 
169511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
169611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
169711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
169811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
169911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
170011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PTFF   subfunc 0x%16.16lx.%16.16lx",
170111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
170211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
170311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMAC   subfunc 0x%16.16lx.%16.16lx",
170411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
170511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
170611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMC    subfunc 0x%16.16lx.%16.16lx",
170711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
170811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
170911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KM     subfunc 0x%16.16lx.%16.16lx",
171011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
171111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
171211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KIMD   subfunc 0x%16.16lx.%16.16lx",
171311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
171411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
171511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KLMD   subfunc 0x%16.16lx.%16.16lx",
171611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
171711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
171811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PCKMO  subfunc 0x%16.16lx.%16.16lx",
171911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
172011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
172111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMCTR  subfunc 0x%16.16lx.%16.16lx",
172211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
172311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
172411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMF    subfunc 0x%16.16lx.%16.16lx",
172511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
172611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
172711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMO    subfunc 0x%16.16lx.%16.16lx",
172811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
172911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
173011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PCC    subfunc 0x%16.16lx.%16.16lx",
173111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
173211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
173311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PPNO   subfunc 0x%16.16lx.%16.16lx",
173411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
173511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
173611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMA    subfunc 0x%16.16lx.%16.16lx",
173711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
173811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
173913209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KDSA   subfunc 0x%16.16lx.%16.16lx",
174013209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
174113209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
1742173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1743173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1744173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1745173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1746173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
17474f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
17484f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
17494f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
17504f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
17514f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
175211ba5961SChristian Borntraeger 
17530a763c78SDavid Hildenbrand 	return 0;
17540a763c78SDavid Hildenbrand }
1755346fa2f8SChristian Borntraeger 
1756658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1757658b6edaSMichael Mueller {
1758658b6edaSMichael Mueller 	int ret = -ENXIO;
1759658b6edaSMichael Mueller 
1760658b6edaSMichael Mueller 	switch (attr->attr) {
1761658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1762658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
1763658b6edaSMichael Mueller 		break;
1764658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
1765658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
1766658b6edaSMichael Mueller 		break;
176715c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
176815c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
176915c9705fSDavid Hildenbrand 		break;
177015c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
177115c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
177215c9705fSDavid Hildenbrand 		break;
17730a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
17740a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
17750a763c78SDavid Hildenbrand 		break;
17760a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
17770a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
17780a763c78SDavid Hildenbrand 		break;
1779658b6edaSMichael Mueller 	}
1780658b6edaSMichael Mueller 	return ret;
1781658b6edaSMichael Mueller }
1782658b6edaSMichael Mueller 
178324fe0195SPierre Morel /**
178424fe0195SPierre Morel  * kvm_s390_update_topology_change_report - update CPU topology change report
178524fe0195SPierre Morel  * @kvm: guest KVM description
178624fe0195SPierre Morel  * @val: set or clear the MTCR bit
178724fe0195SPierre Morel  *
178824fe0195SPierre Morel  * Updates the Multiprocessor Topology-Change-Report bit to signal
178924fe0195SPierre Morel  * the guest with a topology change.
179024fe0195SPierre Morel  * This is only relevant if the topology facility is present.
179124fe0195SPierre Morel  *
179224fe0195SPierre Morel  * The SCA version, bsca or esca, doesn't matter as offset is the same.
179324fe0195SPierre Morel  */
179424fe0195SPierre Morel static void kvm_s390_update_topology_change_report(struct kvm *kvm, bool val)
179524fe0195SPierre Morel {
179624fe0195SPierre Morel 	union sca_utility new, old;
179724fe0195SPierre Morel 	struct bsca_block *sca;
179824fe0195SPierre Morel 
179924fe0195SPierre Morel 	read_lock(&kvm->arch.sca_lock);
180024fe0195SPierre Morel 	sca = kvm->arch.sca;
180124fe0195SPierre Morel 	do {
180224fe0195SPierre Morel 		old = READ_ONCE(sca->utility);
180324fe0195SPierre Morel 		new = old;
180424fe0195SPierre Morel 		new.mtcr = val;
180524fe0195SPierre Morel 	} while (cmpxchg(&sca->utility.val, old.val, new.val) != old.val);
180624fe0195SPierre Morel 	read_unlock(&kvm->arch.sca_lock);
180724fe0195SPierre Morel }
180824fe0195SPierre Morel 
1809f5ecfee9SPierre Morel static int kvm_s390_set_topo_change_indication(struct kvm *kvm,
1810f5ecfee9SPierre Morel 					       struct kvm_device_attr *attr)
1811f5ecfee9SPierre Morel {
1812f5ecfee9SPierre Morel 	if (!test_kvm_facility(kvm, 11))
1813f5ecfee9SPierre Morel 		return -ENXIO;
1814f5ecfee9SPierre Morel 
1815f5ecfee9SPierre Morel 	kvm_s390_update_topology_change_report(kvm, !!attr->attr);
1816f5ecfee9SPierre Morel 	return 0;
1817f5ecfee9SPierre Morel }
1818f5ecfee9SPierre Morel 
1819f5ecfee9SPierre Morel static int kvm_s390_get_topo_change_indication(struct kvm *kvm,
1820f5ecfee9SPierre Morel 					       struct kvm_device_attr *attr)
1821f5ecfee9SPierre Morel {
1822f5ecfee9SPierre Morel 	u8 topo;
1823f5ecfee9SPierre Morel 
1824f5ecfee9SPierre Morel 	if (!test_kvm_facility(kvm, 11))
1825f5ecfee9SPierre Morel 		return -ENXIO;
1826f5ecfee9SPierre Morel 
1827f5ecfee9SPierre Morel 	read_lock(&kvm->arch.sca_lock);
1828f5ecfee9SPierre Morel 	topo = ((struct bsca_block *)kvm->arch.sca)->utility.mtcr;
1829f5ecfee9SPierre Morel 	read_unlock(&kvm->arch.sca_lock);
1830f5ecfee9SPierre Morel 
1831f5ecfee9SPierre Morel 	return put_user(topo, (u8 __user *)attr->addr);
1832f5ecfee9SPierre Morel }
1833f5ecfee9SPierre Morel 
1834f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1835f2061656SDominik Dingel {
1836f2061656SDominik Dingel 	int ret;
1837f2061656SDominik Dingel 
1838f2061656SDominik Dingel 	switch (attr->group) {
18394f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
18408c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
18414f718eabSDominik Dingel 		break;
184272f25020SJason J. Herne 	case KVM_S390_VM_TOD:
184372f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
184472f25020SJason J. Herne 		break;
1845658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1846658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1847658b6edaSMichael Mueller 		break;
1848a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1849a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1850a374e892STony Krowiak 		break;
1851190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1852190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_set_migration(kvm, attr);
1853190df4a2SClaudio Imbrenda 		break;
1854f5ecfee9SPierre Morel 	case KVM_S390_VM_CPU_TOPOLOGY:
1855f5ecfee9SPierre Morel 		ret = kvm_s390_set_topo_change_indication(kvm, attr);
1856f5ecfee9SPierre Morel 		break;
1857f2061656SDominik Dingel 	default:
1858f2061656SDominik Dingel 		ret = -ENXIO;
1859f2061656SDominik Dingel 		break;
1860f2061656SDominik Dingel 	}
1861f2061656SDominik Dingel 
1862f2061656SDominik Dingel 	return ret;
1863f2061656SDominik Dingel }
1864f2061656SDominik Dingel 
1865f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1866f2061656SDominik Dingel {
18678c0a7ce6SDominik Dingel 	int ret;
18688c0a7ce6SDominik Dingel 
18698c0a7ce6SDominik Dingel 	switch (attr->group) {
18708c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
18718c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
18728c0a7ce6SDominik Dingel 		break;
187372f25020SJason J. Herne 	case KVM_S390_VM_TOD:
187472f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
187572f25020SJason J. Herne 		break;
1876658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1877658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1878658b6edaSMichael Mueller 		break;
1879190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1880190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_get_migration(kvm, attr);
1881190df4a2SClaudio Imbrenda 		break;
1882f5ecfee9SPierre Morel 	case KVM_S390_VM_CPU_TOPOLOGY:
1883f5ecfee9SPierre Morel 		ret = kvm_s390_get_topo_change_indication(kvm, attr);
1884f5ecfee9SPierre Morel 		break;
18858c0a7ce6SDominik Dingel 	default:
18868c0a7ce6SDominik Dingel 		ret = -ENXIO;
18878c0a7ce6SDominik Dingel 		break;
18888c0a7ce6SDominik Dingel 	}
18898c0a7ce6SDominik Dingel 
18908c0a7ce6SDominik Dingel 	return ret;
1891f2061656SDominik Dingel }
1892f2061656SDominik Dingel 
1893f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1894f2061656SDominik Dingel {
1895f2061656SDominik Dingel 	int ret;
1896f2061656SDominik Dingel 
1897f2061656SDominik Dingel 	switch (attr->group) {
18984f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
18994f718eabSDominik Dingel 		switch (attr->attr) {
19004f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
19014f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1902f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1903f9cbd9b0SDavid Hildenbrand 			break;
19048c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
19054f718eabSDominik Dingel 			ret = 0;
19064f718eabSDominik Dingel 			break;
19074f718eabSDominik Dingel 		default:
19084f718eabSDominik Dingel 			ret = -ENXIO;
19094f718eabSDominik Dingel 			break;
19104f718eabSDominik Dingel 		}
19114f718eabSDominik Dingel 		break;
191272f25020SJason J. Herne 	case KVM_S390_VM_TOD:
191372f25020SJason J. Herne 		switch (attr->attr) {
191472f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
191572f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
191672f25020SJason J. Herne 			ret = 0;
191772f25020SJason J. Herne 			break;
191872f25020SJason J. Herne 		default:
191972f25020SJason J. Herne 			ret = -ENXIO;
192072f25020SJason J. Herne 			break;
192172f25020SJason J. Herne 		}
192272f25020SJason J. Herne 		break;
1923658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1924658b6edaSMichael Mueller 		switch (attr->attr) {
1925658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1926658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
192715c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
192815c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
19290a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1930346fa2f8SChristian Borntraeger 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1931658b6edaSMichael Mueller 			ret = 0;
1932658b6edaSMichael Mueller 			break;
1933658b6edaSMichael Mueller 		default:
1934658b6edaSMichael Mueller 			ret = -ENXIO;
1935658b6edaSMichael Mueller 			break;
1936658b6edaSMichael Mueller 		}
1937658b6edaSMichael Mueller 		break;
1938a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1939a374e892STony Krowiak 		switch (attr->attr) {
1940a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1941a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1942a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1943a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1944a374e892STony Krowiak 			ret = 0;
1945a374e892STony Krowiak 			break;
194637940fb0STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_APIE:
194737940fb0STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_APIE:
194837940fb0STony Krowiak 			ret = ap_instructions_available() ? 0 : -ENXIO;
194937940fb0STony Krowiak 			break;
1950a374e892STony Krowiak 		default:
1951a374e892STony Krowiak 			ret = -ENXIO;
1952a374e892STony Krowiak 			break;
1953a374e892STony Krowiak 		}
1954a374e892STony Krowiak 		break;
1955190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1956190df4a2SClaudio Imbrenda 		ret = 0;
1957190df4a2SClaudio Imbrenda 		break;
1958f5ecfee9SPierre Morel 	case KVM_S390_VM_CPU_TOPOLOGY:
1959f5ecfee9SPierre Morel 		ret = test_kvm_facility(kvm, 11) ? 0 : -ENXIO;
1960f5ecfee9SPierre Morel 		break;
1961f2061656SDominik Dingel 	default:
1962f2061656SDominik Dingel 		ret = -ENXIO;
1963f2061656SDominik Dingel 		break;
1964f2061656SDominik Dingel 	}
1965f2061656SDominik Dingel 
1966f2061656SDominik Dingel 	return ret;
1967f2061656SDominik Dingel }
1968f2061656SDominik Dingel 
196930ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
197030ee2a98SJason J. Herne {
197130ee2a98SJason J. Herne 	uint8_t *keys;
197230ee2a98SJason J. Herne 	uint64_t hva;
19734f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
197430ee2a98SJason J. Herne 
197530ee2a98SJason J. Herne 	if (args->flags != 0)
197630ee2a98SJason J. Herne 		return -EINVAL;
197730ee2a98SJason J. Herne 
197830ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
197955531b74SJanosch Frank 	if (!mm_uses_skeys(current->mm))
198030ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
198130ee2a98SJason J. Herne 
198230ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
198330ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
198430ee2a98SJason J. Herne 		return -EINVAL;
198530ee2a98SJason J. Herne 
1986c4196218SChristian Borntraeger 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
198730ee2a98SJason J. Herne 	if (!keys)
198830ee2a98SJason J. Herne 		return -ENOMEM;
198930ee2a98SJason J. Herne 
1990d8ed45c5SMichel Lespinasse 	mmap_read_lock(current->mm);
19914f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
199230ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
199330ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
199430ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
199530ee2a98SJason J. Herne 			r = -EFAULT;
1996d3ed1ceeSMartin Schwidefsky 			break;
199730ee2a98SJason J. Herne 		}
199830ee2a98SJason J. Herne 
1999154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
2000154c8c19SDavid Hildenbrand 		if (r)
2001d3ed1ceeSMartin Schwidefsky 			break;
200230ee2a98SJason J. Herne 	}
20034f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2004d8ed45c5SMichel Lespinasse 	mmap_read_unlock(current->mm);
200530ee2a98SJason J. Herne 
2006d3ed1ceeSMartin Schwidefsky 	if (!r) {
200730ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
200830ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
200930ee2a98SJason J. Herne 		if (r)
201030ee2a98SJason J. Herne 			r = -EFAULT;
2011d3ed1ceeSMartin Schwidefsky 	}
2012d3ed1ceeSMartin Schwidefsky 
201330ee2a98SJason J. Herne 	kvfree(keys);
201430ee2a98SJason J. Herne 	return r;
201530ee2a98SJason J. Herne }
201630ee2a98SJason J. Herne 
201730ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
201830ee2a98SJason J. Herne {
201930ee2a98SJason J. Herne 	uint8_t *keys;
202030ee2a98SJason J. Herne 	uint64_t hva;
20214f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
2022bd096f64SJanosch Frank 	bool unlocked;
202330ee2a98SJason J. Herne 
202430ee2a98SJason J. Herne 	if (args->flags != 0)
202530ee2a98SJason J. Herne 		return -EINVAL;
202630ee2a98SJason J. Herne 
202730ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
202830ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
202930ee2a98SJason J. Herne 		return -EINVAL;
203030ee2a98SJason J. Herne 
2031c4196218SChristian Borntraeger 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
203230ee2a98SJason J. Herne 	if (!keys)
203330ee2a98SJason J. Herne 		return -ENOMEM;
203430ee2a98SJason J. Herne 
203530ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
203630ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
203730ee2a98SJason J. Herne 	if (r) {
203830ee2a98SJason J. Herne 		r = -EFAULT;
203930ee2a98SJason J. Herne 		goto out;
204030ee2a98SJason J. Herne 	}
204130ee2a98SJason J. Herne 
204230ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
204314d4a425SDominik Dingel 	r = s390_enable_skey();
204414d4a425SDominik Dingel 	if (r)
204514d4a425SDominik Dingel 		goto out;
204630ee2a98SJason J. Herne 
2047bd096f64SJanosch Frank 	i = 0;
2048d8ed45c5SMichel Lespinasse 	mmap_read_lock(current->mm);
20494f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
2050bd096f64SJanosch Frank         while (i < args->count) {
2051bd096f64SJanosch Frank 		unlocked = false;
205230ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
205330ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
205430ee2a98SJason J. Herne 			r = -EFAULT;
2055d3ed1ceeSMartin Schwidefsky 			break;
205630ee2a98SJason J. Herne 		}
205730ee2a98SJason J. Herne 
205830ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
205930ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
206030ee2a98SJason J. Herne 			r = -EINVAL;
2061d3ed1ceeSMartin Schwidefsky 			break;
206230ee2a98SJason J. Herne 		}
206330ee2a98SJason J. Herne 
2064fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
2065bd096f64SJanosch Frank 		if (r) {
206664019a2eSPeter Xu 			r = fixup_user_fault(current->mm, hva,
2067bd096f64SJanosch Frank 					     FAULT_FLAG_WRITE, &unlocked);
206830ee2a98SJason J. Herne 			if (r)
2069d3ed1ceeSMartin Schwidefsky 				break;
207030ee2a98SJason J. Herne 		}
2071bd096f64SJanosch Frank 		if (!r)
2072bd096f64SJanosch Frank 			i++;
2073bd096f64SJanosch Frank 	}
20744f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2075d8ed45c5SMichel Lespinasse 	mmap_read_unlock(current->mm);
207630ee2a98SJason J. Herne out:
207730ee2a98SJason J. Herne 	kvfree(keys);
207830ee2a98SJason J. Herne 	return r;
207930ee2a98SJason J. Herne }
208030ee2a98SJason J. Herne 
20814036e387SClaudio Imbrenda /*
20824036e387SClaudio Imbrenda  * Base address and length must be sent at the start of each block, therefore
20834036e387SClaudio Imbrenda  * it's cheaper to send some clean data, as long as it's less than the size of
20844036e387SClaudio Imbrenda  * two longs.
20854036e387SClaudio Imbrenda  */
20864036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
20874036e387SClaudio Imbrenda /* for consistency */
20884036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
20894036e387SClaudio Imbrenda 
2090afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2091afdad616SClaudio Imbrenda 			      u8 *res, unsigned long bufsize)
2092afdad616SClaudio Imbrenda {
2093afdad616SClaudio Imbrenda 	unsigned long pgstev, hva, cur_gfn = args->start_gfn;
2094afdad616SClaudio Imbrenda 
2095afdad616SClaudio Imbrenda 	args->count = 0;
2096afdad616SClaudio Imbrenda 	while (args->count < bufsize) {
2097afdad616SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur_gfn);
2098afdad616SClaudio Imbrenda 		/*
2099afdad616SClaudio Imbrenda 		 * We return an error if the first value was invalid, but we
2100afdad616SClaudio Imbrenda 		 * return successfully if at least one value was copied.
2101afdad616SClaudio Imbrenda 		 */
2102afdad616SClaudio Imbrenda 		if (kvm_is_error_hva(hva))
2103afdad616SClaudio Imbrenda 			return args->count ? 0 : -EFAULT;
2104afdad616SClaudio Imbrenda 		if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2105afdad616SClaudio Imbrenda 			pgstev = 0;
2106afdad616SClaudio Imbrenda 		res[args->count++] = (pgstev >> 24) & 0x43;
2107afdad616SClaudio Imbrenda 		cur_gfn++;
2108afdad616SClaudio Imbrenda 	}
2109afdad616SClaudio Imbrenda 
2110afdad616SClaudio Imbrenda 	return 0;
2111afdad616SClaudio Imbrenda }
2112afdad616SClaudio Imbrenda 
2113c928bfc2SMaciej S. Szmigiero static struct kvm_memory_slot *gfn_to_memslot_approx(struct kvm_memslots *slots,
2114c928bfc2SMaciej S. Szmigiero 						     gfn_t gfn)
2115c928bfc2SMaciej S. Szmigiero {
2116c928bfc2SMaciej S. Szmigiero 	return ____gfn_to_memslot(slots, gfn, true);
2117c928bfc2SMaciej S. Szmigiero }
2118c928bfc2SMaciej S. Szmigiero 
2119afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
2120afdad616SClaudio Imbrenda 					      unsigned long cur_gfn)
2121afdad616SClaudio Imbrenda {
2122c928bfc2SMaciej S. Szmigiero 	struct kvm_memory_slot *ms = gfn_to_memslot_approx(slots, cur_gfn);
2123afdad616SClaudio Imbrenda 	unsigned long ofs = cur_gfn - ms->base_gfn;
2124a54d8066SMaciej S. Szmigiero 	struct rb_node *mnode = &ms->gfn_node[slots->node_idx];
2125afdad616SClaudio Imbrenda 
2126afdad616SClaudio Imbrenda 	if (ms->base_gfn + ms->npages <= cur_gfn) {
2127a54d8066SMaciej S. Szmigiero 		mnode = rb_next(mnode);
2128afdad616SClaudio Imbrenda 		/* If we are above the highest slot, wrap around */
2129a54d8066SMaciej S. Szmigiero 		if (!mnode)
2130a54d8066SMaciej S. Szmigiero 			mnode = rb_first(&slots->gfn_tree);
2131afdad616SClaudio Imbrenda 
2132a54d8066SMaciej S. Szmigiero 		ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]);
2133afdad616SClaudio Imbrenda 		ofs = 0;
2134afdad616SClaudio Imbrenda 	}
2135afdad616SClaudio Imbrenda 	ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
2136a54d8066SMaciej S. Szmigiero 	while (ofs >= ms->npages && (mnode = rb_next(mnode))) {
2137a54d8066SMaciej S. Szmigiero 		ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]);
2138b5c7e7ecSYury Norov 		ofs = find_first_bit(kvm_second_dirty_bitmap(ms), ms->npages);
2139afdad616SClaudio Imbrenda 	}
2140afdad616SClaudio Imbrenda 	return ms->base_gfn + ofs;
2141afdad616SClaudio Imbrenda }
2142afdad616SClaudio Imbrenda 
2143afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2144afdad616SClaudio Imbrenda 			     u8 *res, unsigned long bufsize)
2145afdad616SClaudio Imbrenda {
2146afdad616SClaudio Imbrenda 	unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
2147afdad616SClaudio Imbrenda 	struct kvm_memslots *slots = kvm_memslots(kvm);
2148afdad616SClaudio Imbrenda 	struct kvm_memory_slot *ms;
2149afdad616SClaudio Imbrenda 
2150a54d8066SMaciej S. Szmigiero 	if (unlikely(kvm_memslots_empty(slots)))
21510774a964SSean Christopherson 		return 0;
21520774a964SSean Christopherson 
2153afdad616SClaudio Imbrenda 	cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
2154afdad616SClaudio Imbrenda 	ms = gfn_to_memslot(kvm, cur_gfn);
2155afdad616SClaudio Imbrenda 	args->count = 0;
2156afdad616SClaudio Imbrenda 	args->start_gfn = cur_gfn;
2157afdad616SClaudio Imbrenda 	if (!ms)
2158afdad616SClaudio Imbrenda 		return 0;
2159afdad616SClaudio Imbrenda 	next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
21606a656832SMaciej S. Szmigiero 	mem_end = kvm_s390_get_gfn_end(slots);
2161afdad616SClaudio Imbrenda 
2162afdad616SClaudio Imbrenda 	while (args->count < bufsize) {
2163afdad616SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur_gfn);
2164afdad616SClaudio Imbrenda 		if (kvm_is_error_hva(hva))
2165afdad616SClaudio Imbrenda 			return 0;
2166afdad616SClaudio Imbrenda 		/* Decrement only if we actually flipped the bit to 0 */
2167afdad616SClaudio Imbrenda 		if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
2168afdad616SClaudio Imbrenda 			atomic64_dec(&kvm->arch.cmma_dirty_pages);
2169afdad616SClaudio Imbrenda 		if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2170afdad616SClaudio Imbrenda 			pgstev = 0;
2171afdad616SClaudio Imbrenda 		/* Save the value */
2172afdad616SClaudio Imbrenda 		res[args->count++] = (pgstev >> 24) & 0x43;
2173afdad616SClaudio Imbrenda 		/* If the next bit is too far away, stop. */
2174afdad616SClaudio Imbrenda 		if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
2175afdad616SClaudio Imbrenda 			return 0;
2176afdad616SClaudio Imbrenda 		/* If we reached the previous "next", find the next one */
2177afdad616SClaudio Imbrenda 		if (cur_gfn == next_gfn)
2178afdad616SClaudio Imbrenda 			next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2179afdad616SClaudio Imbrenda 		/* Reached the end of memory or of the buffer, stop */
2180afdad616SClaudio Imbrenda 		if ((next_gfn >= mem_end) ||
2181afdad616SClaudio Imbrenda 		    (next_gfn - args->start_gfn >= bufsize))
2182afdad616SClaudio Imbrenda 			return 0;
2183afdad616SClaudio Imbrenda 		cur_gfn++;
2184afdad616SClaudio Imbrenda 		/* Reached the end of the current memslot, take the next one. */
2185afdad616SClaudio Imbrenda 		if (cur_gfn - ms->base_gfn >= ms->npages) {
2186afdad616SClaudio Imbrenda 			ms = gfn_to_memslot(kvm, cur_gfn);
2187afdad616SClaudio Imbrenda 			if (!ms)
2188afdad616SClaudio Imbrenda 				return 0;
2189afdad616SClaudio Imbrenda 		}
2190afdad616SClaudio Imbrenda 	}
2191afdad616SClaudio Imbrenda 	return 0;
2192afdad616SClaudio Imbrenda }
2193afdad616SClaudio Imbrenda 
2194afdad616SClaudio Imbrenda /*
21954036e387SClaudio Imbrenda  * This function searches for the next page with dirty CMMA attributes, and
21964036e387SClaudio Imbrenda  * saves the attributes in the buffer up to either the end of the buffer or
21974036e387SClaudio Imbrenda  * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
21984036e387SClaudio Imbrenda  * no trailing clean bytes are saved.
21994036e387SClaudio Imbrenda  * In case no dirty bits were found, or if CMMA was not enabled or used, the
22004036e387SClaudio Imbrenda  * output buffer will indicate 0 as length.
22014036e387SClaudio Imbrenda  */
22024036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm,
22034036e387SClaudio Imbrenda 				  struct kvm_s390_cmma_log *args)
22044036e387SClaudio Imbrenda {
2205afdad616SClaudio Imbrenda 	unsigned long bufsize;
2206afdad616SClaudio Imbrenda 	int srcu_idx, peek, ret;
2207afdad616SClaudio Imbrenda 	u8 *values;
22084036e387SClaudio Imbrenda 
2209afdad616SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
22104036e387SClaudio Imbrenda 		return -ENXIO;
22114036e387SClaudio Imbrenda 	/* Invalid/unsupported flags were specified */
22124036e387SClaudio Imbrenda 	if (args->flags & ~KVM_S390_CMMA_PEEK)
22134036e387SClaudio Imbrenda 		return -EINVAL;
22144036e387SClaudio Imbrenda 	/* Migration mode query, and we are not doing a migration */
22154036e387SClaudio Imbrenda 	peek = !!(args->flags & KVM_S390_CMMA_PEEK);
2216afdad616SClaudio Imbrenda 	if (!peek && !kvm->arch.migration_mode)
22174036e387SClaudio Imbrenda 		return -EINVAL;
22184036e387SClaudio Imbrenda 	/* CMMA is disabled or was not used, or the buffer has length zero */
22194036e387SClaudio Imbrenda 	bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
2220c9f0a2b8SJanosch Frank 	if (!bufsize || !kvm->mm->context.uses_cmm) {
22214036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
22224036e387SClaudio Imbrenda 		return 0;
22234036e387SClaudio Imbrenda 	}
22244036e387SClaudio Imbrenda 	/* We are not peeking, and there are no dirty pages */
2225afdad616SClaudio Imbrenda 	if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
22264036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
22274036e387SClaudio Imbrenda 		return 0;
22284036e387SClaudio Imbrenda 	}
22294036e387SClaudio Imbrenda 
2230afdad616SClaudio Imbrenda 	values = vmalloc(bufsize);
2231afdad616SClaudio Imbrenda 	if (!values)
22324036e387SClaudio Imbrenda 		return -ENOMEM;
22334036e387SClaudio Imbrenda 
2234d8ed45c5SMichel Lespinasse 	mmap_read_lock(kvm->mm);
22354036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
2236afdad616SClaudio Imbrenda 	if (peek)
2237afdad616SClaudio Imbrenda 		ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
2238afdad616SClaudio Imbrenda 	else
2239afdad616SClaudio Imbrenda 		ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
22404036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2241d8ed45c5SMichel Lespinasse 	mmap_read_unlock(kvm->mm);
22424036e387SClaudio Imbrenda 
2243afdad616SClaudio Imbrenda 	if (kvm->arch.migration_mode)
2244afdad616SClaudio Imbrenda 		args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2245afdad616SClaudio Imbrenda 	else
2246afdad616SClaudio Imbrenda 		args->remaining = 0;
22474036e387SClaudio Imbrenda 
2248afdad616SClaudio Imbrenda 	if (copy_to_user((void __user *)args->values, values, args->count))
2249afdad616SClaudio Imbrenda 		ret = -EFAULT;
2250afdad616SClaudio Imbrenda 
2251afdad616SClaudio Imbrenda 	vfree(values);
2252afdad616SClaudio Imbrenda 	return ret;
22534036e387SClaudio Imbrenda }
22544036e387SClaudio Imbrenda 
22554036e387SClaudio Imbrenda /*
22564036e387SClaudio Imbrenda  * This function sets the CMMA attributes for the given pages. If the input
22574036e387SClaudio Imbrenda  * buffer has zero length, no action is taken, otherwise the attributes are
2258c9f0a2b8SJanosch Frank  * set and the mm->context.uses_cmm flag is set.
22594036e387SClaudio Imbrenda  */
22604036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm,
22614036e387SClaudio Imbrenda 				  const struct kvm_s390_cmma_log *args)
22624036e387SClaudio Imbrenda {
22634036e387SClaudio Imbrenda 	unsigned long hva, mask, pgstev, i;
22644036e387SClaudio Imbrenda 	uint8_t *bits;
22654036e387SClaudio Imbrenda 	int srcu_idx, r = 0;
22664036e387SClaudio Imbrenda 
22674036e387SClaudio Imbrenda 	mask = args->mask;
22684036e387SClaudio Imbrenda 
22694036e387SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
22704036e387SClaudio Imbrenda 		return -ENXIO;
22714036e387SClaudio Imbrenda 	/* invalid/unsupported flags */
22724036e387SClaudio Imbrenda 	if (args->flags != 0)
22734036e387SClaudio Imbrenda 		return -EINVAL;
22744036e387SClaudio Imbrenda 	/* Enforce sane limit on memory allocation */
22754036e387SClaudio Imbrenda 	if (args->count > KVM_S390_CMMA_SIZE_MAX)
22764036e387SClaudio Imbrenda 		return -EINVAL;
22774036e387SClaudio Imbrenda 	/* Nothing to do */
22784036e387SClaudio Imbrenda 	if (args->count == 0)
22794036e387SClaudio Imbrenda 		return 0;
22804036e387SClaudio Imbrenda 
228142bc47b3SKees Cook 	bits = vmalloc(array_size(sizeof(*bits), args->count));
22824036e387SClaudio Imbrenda 	if (!bits)
22834036e387SClaudio Imbrenda 		return -ENOMEM;
22844036e387SClaudio Imbrenda 
22854036e387SClaudio Imbrenda 	r = copy_from_user(bits, (void __user *)args->values, args->count);
22864036e387SClaudio Imbrenda 	if (r) {
22874036e387SClaudio Imbrenda 		r = -EFAULT;
22884036e387SClaudio Imbrenda 		goto out;
22894036e387SClaudio Imbrenda 	}
22904036e387SClaudio Imbrenda 
2291d8ed45c5SMichel Lespinasse 	mmap_read_lock(kvm->mm);
22924036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
22934036e387SClaudio Imbrenda 	for (i = 0; i < args->count; i++) {
22944036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, args->start_gfn + i);
22954036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
22964036e387SClaudio Imbrenda 			r = -EFAULT;
22974036e387SClaudio Imbrenda 			break;
22984036e387SClaudio Imbrenda 		}
22994036e387SClaudio Imbrenda 
23004036e387SClaudio Imbrenda 		pgstev = bits[i];
23014036e387SClaudio Imbrenda 		pgstev = pgstev << 24;
23021bab1c02SClaudio Imbrenda 		mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
23034036e387SClaudio Imbrenda 		set_pgste_bits(kvm->mm, hva, mask, pgstev);
23044036e387SClaudio Imbrenda 	}
23054036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2306d8ed45c5SMichel Lespinasse 	mmap_read_unlock(kvm->mm);
23074036e387SClaudio Imbrenda 
2308c9f0a2b8SJanosch Frank 	if (!kvm->mm->context.uses_cmm) {
2309d8ed45c5SMichel Lespinasse 		mmap_write_lock(kvm->mm);
2310c9f0a2b8SJanosch Frank 		kvm->mm->context.uses_cmm = 1;
2311d8ed45c5SMichel Lespinasse 		mmap_write_unlock(kvm->mm);
23124036e387SClaudio Imbrenda 	}
23134036e387SClaudio Imbrenda out:
23144036e387SClaudio Imbrenda 	vfree(bits);
23154036e387SClaudio Imbrenda 	return r;
23164036e387SClaudio Imbrenda }
23174036e387SClaudio Imbrenda 
2318be48d86fSClaudio Imbrenda /**
2319be48d86fSClaudio Imbrenda  * kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to
2320be48d86fSClaudio Imbrenda  * non protected.
2321be48d86fSClaudio Imbrenda  * @kvm: the VM whose protected vCPUs are to be converted
2322be48d86fSClaudio Imbrenda  * @rc: return value for the RC field of the UVC (in case of error)
2323be48d86fSClaudio Imbrenda  * @rrc: return value for the RRC field of the UVC (in case of error)
2324be48d86fSClaudio Imbrenda  *
2325be48d86fSClaudio Imbrenda  * Does not stop in case of error, tries to convert as many
2326be48d86fSClaudio Imbrenda  * CPUs as possible. In case of error, the RC and RRC of the last error are
2327be48d86fSClaudio Imbrenda  * returned.
2328be48d86fSClaudio Imbrenda  *
2329be48d86fSClaudio Imbrenda  * Return: 0 in case of success, otherwise -EIO
2330be48d86fSClaudio Imbrenda  */
2331be48d86fSClaudio Imbrenda int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
233229b40f10SJanosch Frank {
233329b40f10SJanosch Frank 	struct kvm_vcpu *vcpu;
233446808a4cSMarc Zyngier 	unsigned long i;
2335be48d86fSClaudio Imbrenda 	u16 _rc, _rrc;
2336be48d86fSClaudio Imbrenda 	int ret = 0;
233729b40f10SJanosch Frank 
233829b40f10SJanosch Frank 	/*
233929b40f10SJanosch Frank 	 * We ignore failures and try to destroy as many CPUs as possible.
234029b40f10SJanosch Frank 	 * At the same time we must not free the assigned resources when
234129b40f10SJanosch Frank 	 * this fails, as the ultravisor has still access to that memory.
234229b40f10SJanosch Frank 	 * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
234329b40f10SJanosch Frank 	 * behind.
234429b40f10SJanosch Frank 	 * We want to return the first failure rc and rrc, though.
234529b40f10SJanosch Frank 	 */
234629b40f10SJanosch Frank 	kvm_for_each_vcpu(i, vcpu, kvm) {
234729b40f10SJanosch Frank 		mutex_lock(&vcpu->mutex);
2348be48d86fSClaudio Imbrenda 		if (kvm_s390_pv_destroy_cpu(vcpu, &_rc, &_rrc) && !ret) {
2349be48d86fSClaudio Imbrenda 			*rc = _rc;
2350be48d86fSClaudio Imbrenda 			*rrc = _rrc;
235129b40f10SJanosch Frank 			ret = -EIO;
235229b40f10SJanosch Frank 		}
235329b40f10SJanosch Frank 		mutex_unlock(&vcpu->mutex);
235429b40f10SJanosch Frank 	}
2355ee6a569dSMichael Mueller 	/* Ensure that we re-enable gisa if the non-PV guest used it but the PV guest did not. */
2356ee6a569dSMichael Mueller 	if (use_gisa)
2357ee6a569dSMichael Mueller 		kvm_s390_gisa_enable(kvm);
235829b40f10SJanosch Frank 	return ret;
235929b40f10SJanosch Frank }
236029b40f10SJanosch Frank 
2361be48d86fSClaudio Imbrenda /**
2362be48d86fSClaudio Imbrenda  * kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM
2363be48d86fSClaudio Imbrenda  * to protected.
2364be48d86fSClaudio Imbrenda  * @kvm: the VM whose protected vCPUs are to be converted
2365be48d86fSClaudio Imbrenda  * @rc: return value for the RC field of the UVC (in case of error)
2366be48d86fSClaudio Imbrenda  * @rrc: return value for the RRC field of the UVC (in case of error)
2367be48d86fSClaudio Imbrenda  *
2368be48d86fSClaudio Imbrenda  * Tries to undo the conversion in case of error.
2369be48d86fSClaudio Imbrenda  *
2370be48d86fSClaudio Imbrenda  * Return: 0 in case of success, otherwise -EIO
2371be48d86fSClaudio Imbrenda  */
237229b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
237329b40f10SJanosch Frank {
237446808a4cSMarc Zyngier 	unsigned long i;
237546808a4cSMarc Zyngier 	int r = 0;
237629b40f10SJanosch Frank 	u16 dummy;
237729b40f10SJanosch Frank 
237829b40f10SJanosch Frank 	struct kvm_vcpu *vcpu;
237929b40f10SJanosch Frank 
2380ee6a569dSMichael Mueller 	/* Disable the GISA if the ultravisor does not support AIV. */
2381ee6a569dSMichael Mueller 	if (!test_bit_inv(BIT_UV_FEAT_AIV, &uv_info.uv_feature_indications))
2382ee6a569dSMichael Mueller 		kvm_s390_gisa_disable(kvm);
2383ee6a569dSMichael Mueller 
238429b40f10SJanosch Frank 	kvm_for_each_vcpu(i, vcpu, kvm) {
238529b40f10SJanosch Frank 		mutex_lock(&vcpu->mutex);
238629b40f10SJanosch Frank 		r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
238729b40f10SJanosch Frank 		mutex_unlock(&vcpu->mutex);
238829b40f10SJanosch Frank 		if (r)
238929b40f10SJanosch Frank 			break;
239029b40f10SJanosch Frank 	}
239129b40f10SJanosch Frank 	if (r)
239229b40f10SJanosch Frank 		kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
239329b40f10SJanosch Frank 	return r;
239429b40f10SJanosch Frank }
239529b40f10SJanosch Frank 
239635d02493SJanosch Frank /*
239735d02493SJanosch Frank  * Here we provide user space with a direct interface to query UV
239835d02493SJanosch Frank  * related data like UV maxima and available features as well as
239935d02493SJanosch Frank  * feature specific data.
240035d02493SJanosch Frank  *
240135d02493SJanosch Frank  * To facilitate future extension of the data structures we'll try to
240235d02493SJanosch Frank  * write data up to the maximum requested length.
240335d02493SJanosch Frank  */
240435d02493SJanosch Frank static ssize_t kvm_s390_handle_pv_info(struct kvm_s390_pv_info *info)
240535d02493SJanosch Frank {
240635d02493SJanosch Frank 	ssize_t len_min;
240735d02493SJanosch Frank 
240835d02493SJanosch Frank 	switch (info->header.id) {
240935d02493SJanosch Frank 	case KVM_PV_INFO_VM: {
241035d02493SJanosch Frank 		len_min =  sizeof(info->header) + sizeof(info->vm);
241135d02493SJanosch Frank 
241235d02493SJanosch Frank 		if (info->header.len_max < len_min)
241335d02493SJanosch Frank 			return -EINVAL;
241435d02493SJanosch Frank 
241535d02493SJanosch Frank 		memcpy(info->vm.inst_calls_list,
241635d02493SJanosch Frank 		       uv_info.inst_calls_list,
241735d02493SJanosch Frank 		       sizeof(uv_info.inst_calls_list));
241835d02493SJanosch Frank 
241935d02493SJanosch Frank 		/* It's max cpuid not max cpus, so it's off by one */
242035d02493SJanosch Frank 		info->vm.max_cpus = uv_info.max_guest_cpu_id + 1;
242135d02493SJanosch Frank 		info->vm.max_guests = uv_info.max_num_sec_conf;
242235d02493SJanosch Frank 		info->vm.max_guest_addr = uv_info.max_sec_stor_addr;
242335d02493SJanosch Frank 		info->vm.feature_indication = uv_info.uv_feature_indications;
242435d02493SJanosch Frank 
242535d02493SJanosch Frank 		return len_min;
242635d02493SJanosch Frank 	}
2427fe9a93e0SJanosch Frank 	case KVM_PV_INFO_DUMP: {
2428fe9a93e0SJanosch Frank 		len_min =  sizeof(info->header) + sizeof(info->dump);
2429fe9a93e0SJanosch Frank 
2430fe9a93e0SJanosch Frank 		if (info->header.len_max < len_min)
2431fe9a93e0SJanosch Frank 			return -EINVAL;
2432fe9a93e0SJanosch Frank 
2433fe9a93e0SJanosch Frank 		info->dump.dump_cpu_buffer_len = uv_info.guest_cpu_stor_len;
2434fe9a93e0SJanosch Frank 		info->dump.dump_config_mem_buffer_per_1m = uv_info.conf_dump_storage_state_len;
2435fe9a93e0SJanosch Frank 		info->dump.dump_config_finalize_len = uv_info.conf_dump_finalize_len;
2436fe9a93e0SJanosch Frank 		return len_min;
2437fe9a93e0SJanosch Frank 	}
243835d02493SJanosch Frank 	default:
243935d02493SJanosch Frank 		return -EINVAL;
244035d02493SJanosch Frank 	}
244135d02493SJanosch Frank }
244235d02493SJanosch Frank 
24430460eb35SJanosch Frank static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd,
24440460eb35SJanosch Frank 			   struct kvm_s390_pv_dmp dmp)
24450460eb35SJanosch Frank {
24460460eb35SJanosch Frank 	int r = -EINVAL;
24470460eb35SJanosch Frank 	void __user *result_buff = (void __user *)dmp.buff_addr;
24480460eb35SJanosch Frank 
24490460eb35SJanosch Frank 	switch (dmp.subcmd) {
24500460eb35SJanosch Frank 	case KVM_PV_DUMP_INIT: {
24510460eb35SJanosch Frank 		if (kvm->arch.pv.dumping)
24520460eb35SJanosch Frank 			break;
24530460eb35SJanosch Frank 
24540460eb35SJanosch Frank 		/*
24550460eb35SJanosch Frank 		 * Block SIE entry as concurrent dump UVCs could lead
24560460eb35SJanosch Frank 		 * to validities.
24570460eb35SJanosch Frank 		 */
24580460eb35SJanosch Frank 		kvm_s390_vcpu_block_all(kvm);
24590460eb35SJanosch Frank 
24600460eb35SJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
24610460eb35SJanosch Frank 				  UVC_CMD_DUMP_INIT, &cmd->rc, &cmd->rrc);
24620460eb35SJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP INIT: rc %x rrc %x",
24630460eb35SJanosch Frank 			     cmd->rc, cmd->rrc);
24640460eb35SJanosch Frank 		if (!r) {
24650460eb35SJanosch Frank 			kvm->arch.pv.dumping = true;
24660460eb35SJanosch Frank 		} else {
24670460eb35SJanosch Frank 			kvm_s390_vcpu_unblock_all(kvm);
24680460eb35SJanosch Frank 			r = -EINVAL;
24690460eb35SJanosch Frank 		}
24700460eb35SJanosch Frank 		break;
24710460eb35SJanosch Frank 	}
24720460eb35SJanosch Frank 	case KVM_PV_DUMP_CONFIG_STOR_STATE: {
24730460eb35SJanosch Frank 		if (!kvm->arch.pv.dumping)
24740460eb35SJanosch Frank 			break;
24750460eb35SJanosch Frank 
24760460eb35SJanosch Frank 		/*
24770460eb35SJanosch Frank 		 * gaddr is an output parameter since we might stop
24780460eb35SJanosch Frank 		 * early. As dmp will be copied back in our caller, we
24790460eb35SJanosch Frank 		 * don't need to do it ourselves.
24800460eb35SJanosch Frank 		 */
24810460eb35SJanosch Frank 		r = kvm_s390_pv_dump_stor_state(kvm, result_buff, &dmp.gaddr, dmp.buff_len,
24820460eb35SJanosch Frank 						&cmd->rc, &cmd->rrc);
24830460eb35SJanosch Frank 		break;
24840460eb35SJanosch Frank 	}
24850460eb35SJanosch Frank 	case KVM_PV_DUMP_COMPLETE: {
24860460eb35SJanosch Frank 		if (!kvm->arch.pv.dumping)
24870460eb35SJanosch Frank 			break;
24880460eb35SJanosch Frank 
24890460eb35SJanosch Frank 		r = -EINVAL;
24900460eb35SJanosch Frank 		if (dmp.buff_len < uv_info.conf_dump_finalize_len)
24910460eb35SJanosch Frank 			break;
24920460eb35SJanosch Frank 
24930460eb35SJanosch Frank 		r = kvm_s390_pv_dump_complete(kvm, result_buff,
24940460eb35SJanosch Frank 					      &cmd->rc, &cmd->rrc);
24950460eb35SJanosch Frank 		break;
24960460eb35SJanosch Frank 	}
24970460eb35SJanosch Frank 	default:
24980460eb35SJanosch Frank 		r = -ENOTTY;
24990460eb35SJanosch Frank 		break;
25000460eb35SJanosch Frank 	}
25010460eb35SJanosch Frank 
25020460eb35SJanosch Frank 	return r;
25030460eb35SJanosch Frank }
25040460eb35SJanosch Frank 
250529b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
250629b40f10SJanosch Frank {
250729b40f10SJanosch Frank 	int r = 0;
250829b40f10SJanosch Frank 	u16 dummy;
250929b40f10SJanosch Frank 	void __user *argp = (void __user *)cmd->data;
251029b40f10SJanosch Frank 
251129b40f10SJanosch Frank 	switch (cmd->cmd) {
251229b40f10SJanosch Frank 	case KVM_PV_ENABLE: {
251329b40f10SJanosch Frank 		r = -EINVAL;
251429b40f10SJanosch Frank 		if (kvm_s390_pv_is_protected(kvm))
251529b40f10SJanosch Frank 			break;
251629b40f10SJanosch Frank 
251729b40f10SJanosch Frank 		/*
251829b40f10SJanosch Frank 		 *  FMT 4 SIE needs esca. As we never switch back to bsca from
251929b40f10SJanosch Frank 		 *  esca, we need no cleanup in the error cases below
252029b40f10SJanosch Frank 		 */
252129b40f10SJanosch Frank 		r = sca_switch_to_extended(kvm);
252229b40f10SJanosch Frank 		if (r)
252329b40f10SJanosch Frank 			break;
252429b40f10SJanosch Frank 
2525d8ed45c5SMichel Lespinasse 		mmap_write_lock(current->mm);
2526fa0c5eabSJanosch Frank 		r = gmap_mark_unmergeable();
2527d8ed45c5SMichel Lespinasse 		mmap_write_unlock(current->mm);
2528fa0c5eabSJanosch Frank 		if (r)
2529fa0c5eabSJanosch Frank 			break;
2530fa0c5eabSJanosch Frank 
253129b40f10SJanosch Frank 		r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
253229b40f10SJanosch Frank 		if (r)
253329b40f10SJanosch Frank 			break;
253429b40f10SJanosch Frank 
253529b40f10SJanosch Frank 		r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
253629b40f10SJanosch Frank 		if (r)
253729b40f10SJanosch Frank 			kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
25380890ddeaSChristian Borntraeger 
25390890ddeaSChristian Borntraeger 		/* we need to block service interrupts from now on */
25400890ddeaSChristian Borntraeger 		set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
254129b40f10SJanosch Frank 		break;
254229b40f10SJanosch Frank 	}
254329b40f10SJanosch Frank 	case KVM_PV_DISABLE: {
254429b40f10SJanosch Frank 		r = -EINVAL;
254529b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
254629b40f10SJanosch Frank 			break;
254729b40f10SJanosch Frank 
254829b40f10SJanosch Frank 		r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
254929b40f10SJanosch Frank 		/*
255029b40f10SJanosch Frank 		 * If a CPU could not be destroyed, destroy VM will also fail.
255129b40f10SJanosch Frank 		 * There is no point in trying to destroy it. Instead return
255229b40f10SJanosch Frank 		 * the rc and rrc from the first CPU that failed destroying.
255329b40f10SJanosch Frank 		 */
255429b40f10SJanosch Frank 		if (r)
255529b40f10SJanosch Frank 			break;
255629b40f10SJanosch Frank 		r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc);
25570890ddeaSChristian Borntraeger 
25580890ddeaSChristian Borntraeger 		/* no need to block service interrupts any more */
25590890ddeaSChristian Borntraeger 		clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
256029b40f10SJanosch Frank 		break;
256129b40f10SJanosch Frank 	}
256229b40f10SJanosch Frank 	case KVM_PV_SET_SEC_PARMS: {
256329b40f10SJanosch Frank 		struct kvm_s390_pv_sec_parm parms = {};
256429b40f10SJanosch Frank 		void *hdr;
256529b40f10SJanosch Frank 
256629b40f10SJanosch Frank 		r = -EINVAL;
256729b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
256829b40f10SJanosch Frank 			break;
256929b40f10SJanosch Frank 
257029b40f10SJanosch Frank 		r = -EFAULT;
257129b40f10SJanosch Frank 		if (copy_from_user(&parms, argp, sizeof(parms)))
257229b40f10SJanosch Frank 			break;
257329b40f10SJanosch Frank 
257429b40f10SJanosch Frank 		/* Currently restricted to 8KB */
257529b40f10SJanosch Frank 		r = -EINVAL;
257629b40f10SJanosch Frank 		if (parms.length > PAGE_SIZE * 2)
257729b40f10SJanosch Frank 			break;
257829b40f10SJanosch Frank 
257929b40f10SJanosch Frank 		r = -ENOMEM;
258029b40f10SJanosch Frank 		hdr = vmalloc(parms.length);
258129b40f10SJanosch Frank 		if (!hdr)
258229b40f10SJanosch Frank 			break;
258329b40f10SJanosch Frank 
258429b40f10SJanosch Frank 		r = -EFAULT;
258529b40f10SJanosch Frank 		if (!copy_from_user(hdr, (void __user *)parms.origin,
258629b40f10SJanosch Frank 				    parms.length))
258729b40f10SJanosch Frank 			r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
258829b40f10SJanosch Frank 						      &cmd->rc, &cmd->rrc);
258929b40f10SJanosch Frank 
259029b40f10SJanosch Frank 		vfree(hdr);
259129b40f10SJanosch Frank 		break;
259229b40f10SJanosch Frank 	}
259329b40f10SJanosch Frank 	case KVM_PV_UNPACK: {
259429b40f10SJanosch Frank 		struct kvm_s390_pv_unp unp = {};
259529b40f10SJanosch Frank 
259629b40f10SJanosch Frank 		r = -EINVAL;
25971ed576a2SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm))
259829b40f10SJanosch Frank 			break;
259929b40f10SJanosch Frank 
260029b40f10SJanosch Frank 		r = -EFAULT;
260129b40f10SJanosch Frank 		if (copy_from_user(&unp, argp, sizeof(unp)))
260229b40f10SJanosch Frank 			break;
260329b40f10SJanosch Frank 
260429b40f10SJanosch Frank 		r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
260529b40f10SJanosch Frank 				       &cmd->rc, &cmd->rrc);
260629b40f10SJanosch Frank 		break;
260729b40f10SJanosch Frank 	}
260829b40f10SJanosch Frank 	case KVM_PV_VERIFY: {
260929b40f10SJanosch Frank 		r = -EINVAL;
261029b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
261129b40f10SJanosch Frank 			break;
261229b40f10SJanosch Frank 
261329b40f10SJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
261429b40f10SJanosch Frank 				  UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
261529b40f10SJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
261629b40f10SJanosch Frank 			     cmd->rrc);
261729b40f10SJanosch Frank 		break;
261829b40f10SJanosch Frank 	}
2619e0d2773dSJanosch Frank 	case KVM_PV_PREP_RESET: {
2620e0d2773dSJanosch Frank 		r = -EINVAL;
2621e0d2773dSJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2622e0d2773dSJanosch Frank 			break;
2623e0d2773dSJanosch Frank 
2624e0d2773dSJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2625e0d2773dSJanosch Frank 				  UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc);
2626e0d2773dSJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x",
2627e0d2773dSJanosch Frank 			     cmd->rc, cmd->rrc);
2628e0d2773dSJanosch Frank 		break;
2629e0d2773dSJanosch Frank 	}
2630e0d2773dSJanosch Frank 	case KVM_PV_UNSHARE_ALL: {
2631e0d2773dSJanosch Frank 		r = -EINVAL;
2632e0d2773dSJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2633e0d2773dSJanosch Frank 			break;
2634e0d2773dSJanosch Frank 
2635e0d2773dSJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2636e0d2773dSJanosch Frank 				  UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc);
2637e0d2773dSJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x",
2638e0d2773dSJanosch Frank 			     cmd->rc, cmd->rrc);
2639e0d2773dSJanosch Frank 		break;
2640e0d2773dSJanosch Frank 	}
264135d02493SJanosch Frank 	case KVM_PV_INFO: {
264235d02493SJanosch Frank 		struct kvm_s390_pv_info info = {};
264335d02493SJanosch Frank 		ssize_t data_len;
264435d02493SJanosch Frank 
264535d02493SJanosch Frank 		/*
264635d02493SJanosch Frank 		 * No need to check the VM protection here.
264735d02493SJanosch Frank 		 *
264835d02493SJanosch Frank 		 * Maybe user space wants to query some of the data
264935d02493SJanosch Frank 		 * when the VM is still unprotected. If we see the
265035d02493SJanosch Frank 		 * need to fence a new data command we can still
265135d02493SJanosch Frank 		 * return an error in the info handler.
265235d02493SJanosch Frank 		 */
265335d02493SJanosch Frank 
265435d02493SJanosch Frank 		r = -EFAULT;
265535d02493SJanosch Frank 		if (copy_from_user(&info, argp, sizeof(info.header)))
265635d02493SJanosch Frank 			break;
265735d02493SJanosch Frank 
265835d02493SJanosch Frank 		r = -EINVAL;
265935d02493SJanosch Frank 		if (info.header.len_max < sizeof(info.header))
266035d02493SJanosch Frank 			break;
266135d02493SJanosch Frank 
266235d02493SJanosch Frank 		data_len = kvm_s390_handle_pv_info(&info);
266335d02493SJanosch Frank 		if (data_len < 0) {
266435d02493SJanosch Frank 			r = data_len;
266535d02493SJanosch Frank 			break;
266635d02493SJanosch Frank 		}
266735d02493SJanosch Frank 		/*
266835d02493SJanosch Frank 		 * If a data command struct is extended (multiple
266935d02493SJanosch Frank 		 * times) this can be used to determine how much of it
267035d02493SJanosch Frank 		 * is valid.
267135d02493SJanosch Frank 		 */
267235d02493SJanosch Frank 		info.header.len_written = data_len;
267335d02493SJanosch Frank 
267435d02493SJanosch Frank 		r = -EFAULT;
267535d02493SJanosch Frank 		if (copy_to_user(argp, &info, data_len))
267635d02493SJanosch Frank 			break;
267735d02493SJanosch Frank 
267835d02493SJanosch Frank 		r = 0;
267935d02493SJanosch Frank 		break;
268035d02493SJanosch Frank 	}
26810460eb35SJanosch Frank 	case KVM_PV_DUMP: {
26820460eb35SJanosch Frank 		struct kvm_s390_pv_dmp dmp;
26830460eb35SJanosch Frank 
26840460eb35SJanosch Frank 		r = -EINVAL;
26850460eb35SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
26860460eb35SJanosch Frank 			break;
26870460eb35SJanosch Frank 
26880460eb35SJanosch Frank 		r = -EFAULT;
26890460eb35SJanosch Frank 		if (copy_from_user(&dmp, argp, sizeof(dmp)))
26900460eb35SJanosch Frank 			break;
26910460eb35SJanosch Frank 
26920460eb35SJanosch Frank 		r = kvm_s390_pv_dmp(kvm, cmd, dmp);
26930460eb35SJanosch Frank 		if (r)
26940460eb35SJanosch Frank 			break;
26950460eb35SJanosch Frank 
26960460eb35SJanosch Frank 		if (copy_to_user(argp, &dmp, sizeof(dmp))) {
26970460eb35SJanosch Frank 			r = -EFAULT;
26980460eb35SJanosch Frank 			break;
26990460eb35SJanosch Frank 		}
27000460eb35SJanosch Frank 
27010460eb35SJanosch Frank 		break;
27020460eb35SJanosch Frank 	}
270329b40f10SJanosch Frank 	default:
270429b40f10SJanosch Frank 		r = -ENOTTY;
270529b40f10SJanosch Frank 	}
270629b40f10SJanosch Frank 	return r;
270729b40f10SJanosch Frank }
270829b40f10SJanosch Frank 
2709e9e9feebSJanis Schoetterl-Glausch static bool access_key_invalid(u8 access_key)
2710e9e9feebSJanis Schoetterl-Glausch {
2711e9e9feebSJanis Schoetterl-Glausch 	return access_key > 0xf;
2712e9e9feebSJanis Schoetterl-Glausch }
2713e9e9feebSJanis Schoetterl-Glausch 
2714ef11c946SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
2715ef11c946SJanis Schoetterl-Glausch {
2716ef11c946SJanis Schoetterl-Glausch 	void __user *uaddr = (void __user *)mop->buf;
2717ef11c946SJanis Schoetterl-Glausch 	u64 supported_flags;
2718ef11c946SJanis Schoetterl-Glausch 	void *tmpbuf = NULL;
2719ef11c946SJanis Schoetterl-Glausch 	int r, srcu_idx;
2720ef11c946SJanis Schoetterl-Glausch 
2721ef11c946SJanis Schoetterl-Glausch 	supported_flags = KVM_S390_MEMOP_F_SKEY_PROTECTION
2722ef11c946SJanis Schoetterl-Glausch 			  | KVM_S390_MEMOP_F_CHECK_ONLY;
27233d9042f8SJanis Schoetterl-Glausch 	if (mop->flags & ~supported_flags || !mop->size)
2724ef11c946SJanis Schoetterl-Glausch 		return -EINVAL;
2725ef11c946SJanis Schoetterl-Glausch 	if (mop->size > MEM_OP_MAX_SIZE)
2726ef11c946SJanis Schoetterl-Glausch 		return -E2BIG;
2727b5d12744SJanis Schoetterl-Glausch 	/*
2728b5d12744SJanis Schoetterl-Glausch 	 * This is technically a heuristic only, if the kvm->lock is not
2729b5d12744SJanis Schoetterl-Glausch 	 * taken, it is not guaranteed that the vm is/remains non-protected.
2730b5d12744SJanis Schoetterl-Glausch 	 * This is ok from a kernel perspective, wrongdoing is detected
2731b5d12744SJanis Schoetterl-Glausch 	 * on the access, -EFAULT is returned and the vm may crash the
2732b5d12744SJanis Schoetterl-Glausch 	 * next time it accesses the memory in question.
2733b5d12744SJanis Schoetterl-Glausch 	 * There is no sane usecase to do switching and a memop on two
2734b5d12744SJanis Schoetterl-Glausch 	 * different CPUs at the same time.
2735b5d12744SJanis Schoetterl-Glausch 	 */
2736b5d12744SJanis Schoetterl-Glausch 	if (kvm_s390_pv_get_handle(kvm))
2737ef11c946SJanis Schoetterl-Glausch 		return -EINVAL;
2738ef11c946SJanis Schoetterl-Glausch 	if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
2739ef11c946SJanis Schoetterl-Glausch 		if (access_key_invalid(mop->key))
2740ef11c946SJanis Schoetterl-Glausch 			return -EINVAL;
2741ef11c946SJanis Schoetterl-Glausch 	} else {
2742ef11c946SJanis Schoetterl-Glausch 		mop->key = 0;
2743ef11c946SJanis Schoetterl-Glausch 	}
2744ef11c946SJanis Schoetterl-Glausch 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
2745ef11c946SJanis Schoetterl-Glausch 		tmpbuf = vmalloc(mop->size);
2746ef11c946SJanis Schoetterl-Glausch 		if (!tmpbuf)
2747ef11c946SJanis Schoetterl-Glausch 			return -ENOMEM;
2748ef11c946SJanis Schoetterl-Glausch 	}
2749ef11c946SJanis Schoetterl-Glausch 
2750ef11c946SJanis Schoetterl-Glausch 	srcu_idx = srcu_read_lock(&kvm->srcu);
2751ef11c946SJanis Schoetterl-Glausch 
2752ef11c946SJanis Schoetterl-Glausch 	if (kvm_is_error_gpa(kvm, mop->gaddr)) {
2753ef11c946SJanis Schoetterl-Glausch 		r = PGM_ADDRESSING;
2754ef11c946SJanis Schoetterl-Glausch 		goto out_unlock;
2755ef11c946SJanis Schoetterl-Glausch 	}
2756ef11c946SJanis Schoetterl-Glausch 
2757ef11c946SJanis Schoetterl-Glausch 	switch (mop->op) {
2758ef11c946SJanis Schoetterl-Glausch 	case KVM_S390_MEMOP_ABSOLUTE_READ: {
2759ef11c946SJanis Schoetterl-Glausch 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
2760ef11c946SJanis Schoetterl-Glausch 			r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_FETCH, mop->key);
2761ef11c946SJanis Schoetterl-Glausch 		} else {
2762ef11c946SJanis Schoetterl-Glausch 			r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
2763ef11c946SJanis Schoetterl-Glausch 						      mop->size, GACC_FETCH, mop->key);
2764ef11c946SJanis Schoetterl-Glausch 			if (r == 0) {
2765ef11c946SJanis Schoetterl-Glausch 				if (copy_to_user(uaddr, tmpbuf, mop->size))
2766ef11c946SJanis Schoetterl-Glausch 					r = -EFAULT;
2767ef11c946SJanis Schoetterl-Glausch 			}
2768ef11c946SJanis Schoetterl-Glausch 		}
2769ef11c946SJanis Schoetterl-Glausch 		break;
2770ef11c946SJanis Schoetterl-Glausch 	}
2771ef11c946SJanis Schoetterl-Glausch 	case KVM_S390_MEMOP_ABSOLUTE_WRITE: {
2772ef11c946SJanis Schoetterl-Glausch 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
2773ef11c946SJanis Schoetterl-Glausch 			r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_STORE, mop->key);
2774ef11c946SJanis Schoetterl-Glausch 		} else {
2775ef11c946SJanis Schoetterl-Glausch 			if (copy_from_user(tmpbuf, uaddr, mop->size)) {
2776ef11c946SJanis Schoetterl-Glausch 				r = -EFAULT;
2777ef11c946SJanis Schoetterl-Glausch 				break;
2778ef11c946SJanis Schoetterl-Glausch 			}
2779ef11c946SJanis Schoetterl-Glausch 			r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
2780ef11c946SJanis Schoetterl-Glausch 						      mop->size, GACC_STORE, mop->key);
2781ef11c946SJanis Schoetterl-Glausch 		}
2782ef11c946SJanis Schoetterl-Glausch 		break;
2783ef11c946SJanis Schoetterl-Glausch 	}
2784ef11c946SJanis Schoetterl-Glausch 	default:
2785ef11c946SJanis Schoetterl-Glausch 		r = -EINVAL;
2786ef11c946SJanis Schoetterl-Glausch 	}
2787ef11c946SJanis Schoetterl-Glausch 
2788ef11c946SJanis Schoetterl-Glausch out_unlock:
2789ef11c946SJanis Schoetterl-Glausch 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2790ef11c946SJanis Schoetterl-Glausch 
2791ef11c946SJanis Schoetterl-Glausch 	vfree(tmpbuf);
2792ef11c946SJanis Schoetterl-Glausch 	return r;
2793ef11c946SJanis Schoetterl-Glausch }
2794ef11c946SJanis Schoetterl-Glausch 
2795b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
2796b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
2797b0c632dbSHeiko Carstens {
2798b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
2799b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
2800f2061656SDominik Dingel 	struct kvm_device_attr attr;
2801b0c632dbSHeiko Carstens 	int r;
2802b0c632dbSHeiko Carstens 
2803b0c632dbSHeiko Carstens 	switch (ioctl) {
2804ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
2805ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
2806ba5c1e9bSCarsten Otte 
2807ba5c1e9bSCarsten Otte 		r = -EFAULT;
2808ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
2809ba5c1e9bSCarsten Otte 			break;
2810ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
2811ba5c1e9bSCarsten Otte 		break;
2812ba5c1e9bSCarsten Otte 	}
281384223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
281484223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
281584223598SCornelia Huck 
281684223598SCornelia Huck 		r = -EINVAL;
281784223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
281884223598SCornelia Huck 			/* Set up dummy routing. */
281984223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
2820152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
282184223598SCornelia Huck 		}
282284223598SCornelia Huck 		break;
282384223598SCornelia Huck 	}
2824f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
2825f2061656SDominik Dingel 		r = -EFAULT;
2826f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2827f2061656SDominik Dingel 			break;
2828f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
2829f2061656SDominik Dingel 		break;
2830f2061656SDominik Dingel 	}
2831f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
2832f2061656SDominik Dingel 		r = -EFAULT;
2833f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2834f2061656SDominik Dingel 			break;
2835f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
2836f2061656SDominik Dingel 		break;
2837f2061656SDominik Dingel 	}
2838f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
2839f2061656SDominik Dingel 		r = -EFAULT;
2840f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2841f2061656SDominik Dingel 			break;
2842f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
2843f2061656SDominik Dingel 		break;
2844f2061656SDominik Dingel 	}
284530ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
284630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
284730ee2a98SJason J. Herne 
284830ee2a98SJason J. Herne 		r = -EFAULT;
284930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
285030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
285130ee2a98SJason J. Herne 			break;
285230ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
285330ee2a98SJason J. Herne 		break;
285430ee2a98SJason J. Herne 	}
285530ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
285630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
285730ee2a98SJason J. Herne 
285830ee2a98SJason J. Herne 		r = -EFAULT;
285930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
286030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
286130ee2a98SJason J. Herne 			break;
286230ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
286330ee2a98SJason J. Herne 		break;
286430ee2a98SJason J. Herne 	}
28654036e387SClaudio Imbrenda 	case KVM_S390_GET_CMMA_BITS: {
28664036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
28674036e387SClaudio Imbrenda 
28684036e387SClaudio Imbrenda 		r = -EFAULT;
28694036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
28704036e387SClaudio Imbrenda 			break;
28711de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
28724036e387SClaudio Imbrenda 		r = kvm_s390_get_cmma_bits(kvm, &args);
28731de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
28744036e387SClaudio Imbrenda 		if (!r) {
28754036e387SClaudio Imbrenda 			r = copy_to_user(argp, &args, sizeof(args));
28764036e387SClaudio Imbrenda 			if (r)
28774036e387SClaudio Imbrenda 				r = -EFAULT;
28784036e387SClaudio Imbrenda 		}
28794036e387SClaudio Imbrenda 		break;
28804036e387SClaudio Imbrenda 	}
28814036e387SClaudio Imbrenda 	case KVM_S390_SET_CMMA_BITS: {
28824036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
28834036e387SClaudio Imbrenda 
28844036e387SClaudio Imbrenda 		r = -EFAULT;
28854036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
28864036e387SClaudio Imbrenda 			break;
28871de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
28884036e387SClaudio Imbrenda 		r = kvm_s390_set_cmma_bits(kvm, &args);
28891de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
28904036e387SClaudio Imbrenda 		break;
28914036e387SClaudio Imbrenda 	}
289229b40f10SJanosch Frank 	case KVM_S390_PV_COMMAND: {
289329b40f10SJanosch Frank 		struct kvm_pv_cmd args;
289429b40f10SJanosch Frank 
289567cf68b6SEric Farman 		/* protvirt means user cpu state */
289667cf68b6SEric Farman 		kvm_s390_set_user_cpu_state_ctrl(kvm);
289729b40f10SJanosch Frank 		r = 0;
289829b40f10SJanosch Frank 		if (!is_prot_virt_host()) {
289929b40f10SJanosch Frank 			r = -EINVAL;
290029b40f10SJanosch Frank 			break;
290129b40f10SJanosch Frank 		}
290229b40f10SJanosch Frank 		if (copy_from_user(&args, argp, sizeof(args))) {
290329b40f10SJanosch Frank 			r = -EFAULT;
290429b40f10SJanosch Frank 			break;
290529b40f10SJanosch Frank 		}
290629b40f10SJanosch Frank 		if (args.flags) {
290729b40f10SJanosch Frank 			r = -EINVAL;
290829b40f10SJanosch Frank 			break;
290929b40f10SJanosch Frank 		}
291029b40f10SJanosch Frank 		mutex_lock(&kvm->lock);
291129b40f10SJanosch Frank 		r = kvm_s390_handle_pv(kvm, &args);
291229b40f10SJanosch Frank 		mutex_unlock(&kvm->lock);
291329b40f10SJanosch Frank 		if (copy_to_user(argp, &args, sizeof(args))) {
291429b40f10SJanosch Frank 			r = -EFAULT;
291529b40f10SJanosch Frank 			break;
291629b40f10SJanosch Frank 		}
291729b40f10SJanosch Frank 		break;
291829b40f10SJanosch Frank 	}
2919ef11c946SJanis Schoetterl-Glausch 	case KVM_S390_MEM_OP: {
2920ef11c946SJanis Schoetterl-Glausch 		struct kvm_s390_mem_op mem_op;
2921ef11c946SJanis Schoetterl-Glausch 
2922ef11c946SJanis Schoetterl-Glausch 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
2923ef11c946SJanis Schoetterl-Glausch 			r = kvm_s390_vm_mem_op(kvm, &mem_op);
2924ef11c946SJanis Schoetterl-Glausch 		else
2925ef11c946SJanis Schoetterl-Glausch 			r = -EFAULT;
2926ef11c946SJanis Schoetterl-Glausch 		break;
2927ef11c946SJanis Schoetterl-Glausch 	}
2928db1c875eSMatthew Rosato 	case KVM_S390_ZPCI_OP: {
2929db1c875eSMatthew Rosato 		struct kvm_s390_zpci_op args;
2930db1c875eSMatthew Rosato 
2931db1c875eSMatthew Rosato 		r = -EINVAL;
2932db1c875eSMatthew Rosato 		if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
2933db1c875eSMatthew Rosato 			break;
2934db1c875eSMatthew Rosato 		if (copy_from_user(&args, argp, sizeof(args))) {
2935db1c875eSMatthew Rosato 			r = -EFAULT;
2936db1c875eSMatthew Rosato 			break;
2937db1c875eSMatthew Rosato 		}
2938db1c875eSMatthew Rosato 		r = kvm_s390_pci_zpci_op(kvm, &args);
2939db1c875eSMatthew Rosato 		break;
2940db1c875eSMatthew Rosato 	}
2941b0c632dbSHeiko Carstens 	default:
2942367e1319SAvi Kivity 		r = -ENOTTY;
2943b0c632dbSHeiko Carstens 	}
2944b0c632dbSHeiko Carstens 
2945b0c632dbSHeiko Carstens 	return r;
2946b0c632dbSHeiko Carstens }
2947b0c632dbSHeiko Carstens 
294845c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
294945c9b47cSTony Krowiak {
2950e585b24aSTony Krowiak 	struct ap_config_info info;
295145c9b47cSTony Krowiak 
2952e585b24aSTony Krowiak 	if (ap_instructions_available()) {
2953e585b24aSTony Krowiak 		if (ap_qci(&info) == 0)
2954e585b24aSTony Krowiak 			return info.apxa;
295545c9b47cSTony Krowiak 	}
295645c9b47cSTony Krowiak 
295745c9b47cSTony Krowiak 	return 0;
295845c9b47cSTony Krowiak }
295945c9b47cSTony Krowiak 
2960e585b24aSTony Krowiak /*
2961e585b24aSTony Krowiak  * The format of the crypto control block (CRYCB) is specified in the 3 low
2962e585b24aSTony Krowiak  * order bits of the CRYCB designation (CRYCBD) field as follows:
2963e585b24aSTony Krowiak  * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
2964e585b24aSTony Krowiak  *	     AP extended addressing (APXA) facility are installed.
2965e585b24aSTony Krowiak  * Format 1: The APXA facility is not installed but the MSAX3 facility is.
2966e585b24aSTony Krowiak  * Format 2: Both the APXA and MSAX3 facilities are installed
2967e585b24aSTony Krowiak  */
296845c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
296945c9b47cSTony Krowiak {
297045c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
297145c9b47cSTony Krowiak 
2972e585b24aSTony Krowiak 	/* Clear the CRYCB format bits - i.e., set format 0 by default */
2973e585b24aSTony Krowiak 	kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
2974e585b24aSTony Krowiak 
2975e585b24aSTony Krowiak 	/* Check whether MSAX3 is installed */
2976e585b24aSTony Krowiak 	if (!test_kvm_facility(kvm, 76))
2977e585b24aSTony Krowiak 		return;
2978e585b24aSTony Krowiak 
297945c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
298045c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
298145c9b47cSTony Krowiak 	else
298245c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
298345c9b47cSTony Krowiak }
298445c9b47cSTony Krowiak 
298586956e70STony Krowiak /*
298686956e70STony Krowiak  * kvm_arch_crypto_set_masks
298786956e70STony Krowiak  *
298886956e70STony Krowiak  * @kvm: pointer to the target guest's KVM struct containing the crypto masks
298986956e70STony Krowiak  *	 to be set.
299086956e70STony Krowiak  * @apm: the mask identifying the accessible AP adapters
299186956e70STony Krowiak  * @aqm: the mask identifying the accessible AP domains
299286956e70STony Krowiak  * @adm: the mask identifying the accessible AP control domains
299386956e70STony Krowiak  *
299486956e70STony Krowiak  * Set the masks that identify the adapters, domains and control domains to
299586956e70STony Krowiak  * which the KVM guest is granted access.
299686956e70STony Krowiak  *
299786956e70STony Krowiak  * Note: The kvm->lock mutex must be locked by the caller before invoking this
299886956e70STony Krowiak  *	 function.
299986956e70STony Krowiak  */
30000e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
30010e237e44SPierre Morel 			       unsigned long *aqm, unsigned long *adm)
30020e237e44SPierre Morel {
30030e237e44SPierre Morel 	struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
30040e237e44SPierre Morel 
30050e237e44SPierre Morel 	kvm_s390_vcpu_block_all(kvm);
30060e237e44SPierre Morel 
30070e237e44SPierre Morel 	switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
30080e237e44SPierre Morel 	case CRYCB_FORMAT2: /* APCB1 use 256 bits */
30090e237e44SPierre Morel 		memcpy(crycb->apcb1.apm, apm, 32);
30100e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
30110e237e44SPierre Morel 			 apm[0], apm[1], apm[2], apm[3]);
30120e237e44SPierre Morel 		memcpy(crycb->apcb1.aqm, aqm, 32);
30130e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
30140e237e44SPierre Morel 			 aqm[0], aqm[1], aqm[2], aqm[3]);
30150e237e44SPierre Morel 		memcpy(crycb->apcb1.adm, adm, 32);
30160e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
30170e237e44SPierre Morel 			 adm[0], adm[1], adm[2], adm[3]);
30180e237e44SPierre Morel 		break;
30190e237e44SPierre Morel 	case CRYCB_FORMAT1:
30200e237e44SPierre Morel 	case CRYCB_FORMAT0: /* Fall through both use APCB0 */
30210e237e44SPierre Morel 		memcpy(crycb->apcb0.apm, apm, 8);
30220e237e44SPierre Morel 		memcpy(crycb->apcb0.aqm, aqm, 2);
30230e237e44SPierre Morel 		memcpy(crycb->apcb0.adm, adm, 2);
30240e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
30250e237e44SPierre Morel 			 apm[0], *((unsigned short *)aqm),
30260e237e44SPierre Morel 			 *((unsigned short *)adm));
30270e237e44SPierre Morel 		break;
30280e237e44SPierre Morel 	default:	/* Can not happen */
30290e237e44SPierre Morel 		break;
30300e237e44SPierre Morel 	}
30310e237e44SPierre Morel 
30320e237e44SPierre Morel 	/* recreate the shadow crycb for each vcpu */
30330e237e44SPierre Morel 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
30340e237e44SPierre Morel 	kvm_s390_vcpu_unblock_all(kvm);
30350e237e44SPierre Morel }
30360e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
30370e237e44SPierre Morel 
303886956e70STony Krowiak /*
303986956e70STony Krowiak  * kvm_arch_crypto_clear_masks
304086956e70STony Krowiak  *
304186956e70STony Krowiak  * @kvm: pointer to the target guest's KVM struct containing the crypto masks
304286956e70STony Krowiak  *	 to be cleared.
304386956e70STony Krowiak  *
304486956e70STony Krowiak  * Clear the masks that identify the adapters, domains and control domains to
304586956e70STony Krowiak  * which the KVM guest is granted access.
304686956e70STony Krowiak  *
304786956e70STony Krowiak  * Note: The kvm->lock mutex must be locked by the caller before invoking this
304886956e70STony Krowiak  *	 function.
304986956e70STony Krowiak  */
305042104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm)
305142104598STony Krowiak {
305242104598STony Krowiak 	kvm_s390_vcpu_block_all(kvm);
305342104598STony Krowiak 
305442104598STony Krowiak 	memset(&kvm->arch.crypto.crycb->apcb0, 0,
305542104598STony Krowiak 	       sizeof(kvm->arch.crypto.crycb->apcb0));
305642104598STony Krowiak 	memset(&kvm->arch.crypto.crycb->apcb1, 0,
305742104598STony Krowiak 	       sizeof(kvm->arch.crypto.crycb->apcb1));
305842104598STony Krowiak 
30590e237e44SPierre Morel 	VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
30606cc571b1SPierre Morel 	/* recreate the shadow crycb for each vcpu */
30616cc571b1SPierre Morel 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
306242104598STony Krowiak 	kvm_s390_vcpu_unblock_all(kvm);
306342104598STony Krowiak }
306442104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
306542104598STony Krowiak 
30669bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
30679d8d5786SMichael Mueller {
30689bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
30699bb0ec09SDavid Hildenbrand 
30709bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
30719bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
30729bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
30739d8d5786SMichael Mueller }
30749d8d5786SMichael Mueller 
3075c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
30765102ee87STony Krowiak {
3077c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
307845c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
30791e753732STony Krowiak 	init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
30805102ee87STony Krowiak 
3081e585b24aSTony Krowiak 	if (!test_kvm_facility(kvm, 76))
3082e585b24aSTony Krowiak 		return;
3083e585b24aSTony Krowiak 
3084ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
3085ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
3086ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
3087ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
3088ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
3089ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
3090ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
30915102ee87STony Krowiak }
30925102ee87STony Krowiak 
30937d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
30947d43bafcSEugene (jno) Dvurechenski {
30957d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
30965e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
30977d43bafcSEugene (jno) Dvurechenski 	else
30987d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
30997d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
31007d43bafcSEugene (jno) Dvurechenski }
31017d43bafcSEugene (jno) Dvurechenski 
310209340b2fSMatthew Rosato void kvm_arch_free_vm(struct kvm *kvm)
310309340b2fSMatthew Rosato {
310409340b2fSMatthew Rosato 	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
310509340b2fSMatthew Rosato 		kvm_s390_pci_clear_list(kvm);
310609340b2fSMatthew Rosato 
310709340b2fSMatthew Rosato 	__kvm_arch_free_vm(kvm);
310809340b2fSMatthew Rosato }
310909340b2fSMatthew Rosato 
3110e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
3111b0c632dbSHeiko Carstens {
3112c4196218SChristian Borntraeger 	gfp_t alloc_flags = GFP_KERNEL_ACCOUNT;
31139d8d5786SMichael Mueller 	int i, rc;
3114b0c632dbSHeiko Carstens 	char debug_name[16];
3115f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
3116b0c632dbSHeiko Carstens 
3117e08b9637SCarsten Otte 	rc = -EINVAL;
3118e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
3119e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
3120e08b9637SCarsten Otte 		goto out_err;
3121e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
3122e08b9637SCarsten Otte 		goto out_err;
3123e08b9637SCarsten Otte #else
3124e08b9637SCarsten Otte 	if (type)
3125e08b9637SCarsten Otte 		goto out_err;
3126e08b9637SCarsten Otte #endif
3127e08b9637SCarsten Otte 
3128b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
3129b0c632dbSHeiko Carstens 	if (rc)
3130d89f5effSJan Kiszka 		goto out_err;
3131b0c632dbSHeiko Carstens 
3132b290411aSCarsten Otte 	rc = -ENOMEM;
3133b290411aSCarsten Otte 
313476a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
313576a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
31365e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
31379ac96d75SDavid Hildenbrand 	/* start with basic SCA */
313876a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
3139b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
3140d89f5effSJan Kiszka 		goto out_err;
31410d9ce162SJunaid Shahid 	mutex_lock(&kvm_lock);
3142c5c2c393SDavid Hildenbrand 	sca_offset += 16;
3143bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
3144c5c2c393SDavid Hildenbrand 		sca_offset = 0;
3145bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
3146bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
31470d9ce162SJunaid Shahid 	mutex_unlock(&kvm_lock);
3148b0c632dbSHeiko Carstens 
3149b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
3150b0c632dbSHeiko Carstens 
31511cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
3152b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
315340f5b735SDominik Dingel 		goto out_err;
3154b0c632dbSHeiko Carstens 
315519114bebSMichael Mueller 	BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
3156c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
3157c4196218SChristian Borntraeger 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA);
3158c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
315940f5b735SDominik Dingel 		goto out_err;
31609d8d5786SMichael Mueller 
316125c84dbaSMichael Mueller 	kvm->arch.sie_page2->kvm = kvm;
3162c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
3163c3b9e3e1SChristian Borntraeger 
3164c3b9e3e1SChristian Borntraeger 	for (i = 0; i < kvm_s390_fac_size(); i++) {
316517e89e13SSven Schnelle 		kvm->arch.model.fac_mask[i] = stfle_fac_list[i] &
3166c3b9e3e1SChristian Borntraeger 					      (kvm_s390_fac_base[i] |
3167c3b9e3e1SChristian Borntraeger 					       kvm_s390_fac_ext[i]);
316817e89e13SSven Schnelle 		kvm->arch.model.fac_list[i] = stfle_fac_list[i] &
3169c3b9e3e1SChristian Borntraeger 					      kvm_s390_fac_base[i];
3170c3b9e3e1SChristian Borntraeger 	}
3171346fa2f8SChristian Borntraeger 	kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
3172981467c9SMichael Mueller 
31731935222dSDavid Hildenbrand 	/* we are always in czam mode - even on pre z14 machines */
31741935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_mask, 138);
31751935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_list, 138);
31761935222dSDavid Hildenbrand 	/* we emulate STHYI in kvm */
317795ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
317895ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
31791bab1c02SClaudio Imbrenda 	if (MACHINE_HAS_TLB_GUEST) {
31801bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_mask, 147);
31811bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_list, 147);
31821bab1c02SClaudio Imbrenda 	}
318395ca2cb5SJanosch Frank 
318405f31e3bSPierre Morel 	if (css_general_characteristics.aiv && test_facility(65))
318505f31e3bSPierre Morel 		set_kvm_facility(kvm->arch.model.fac_mask, 65);
318605f31e3bSPierre Morel 
31879bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
318837c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
31899d8d5786SMichael Mueller 
3190c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
31915102ee87STony Krowiak 
319209340b2fSMatthew Rosato 	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
319309340b2fSMatthew Rosato 		mutex_lock(&kvm->lock);
319409340b2fSMatthew Rosato 		kvm_s390_pci_init_list(kvm);
319509340b2fSMatthew Rosato 		kvm_s390_vcpu_pci_enable_interp(kvm);
319609340b2fSMatthew Rosato 		mutex_unlock(&kvm->lock);
319709340b2fSMatthew Rosato 	}
319809340b2fSMatthew Rosato 
319951978393SFei Li 	mutex_init(&kvm->arch.float_int.ais_lock);
3200ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
32016d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
32026d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
32038a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
3204a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
3205ba5c1e9bSCarsten Otte 
3206b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
320778f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
3208b0c632dbSHeiko Carstens 
3209e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
3210e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
3211a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
3212e08b9637SCarsten Otte 	} else {
321332e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
3214ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = TASK_SIZE_MAX;
321532e6b236SGuenther Hutzl 		else
3216ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
321732e6b236SGuenther Hutzl 						    sclp.hamax + 1);
32186ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
3219598841caSCarsten Otte 		if (!kvm->arch.gmap)
322040f5b735SDominik Dingel 			goto out_err;
32212c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
322224eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
3223e08b9637SCarsten Otte 	}
3224fa6b7fe9SCornelia Huck 
3225c9f0a2b8SJanosch Frank 	kvm->arch.use_pfmfi = sclp.has_pfmfi;
322655531b74SJanosch Frank 	kvm->arch.use_skf = sclp.has_skey;
32278ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
3228a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
3229cc674ef2SMichael Mueller 	if (use_gisa)
3230d7c5cb01SMichael Mueller 		kvm_s390_gisa_init(kvm);
32318335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
32328ad35755SDavid Hildenbrand 
3233d89f5effSJan Kiszka 	return 0;
3234d89f5effSJan Kiszka out_err:
3235c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
323640f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
32377d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
323878f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
3239d89f5effSJan Kiszka 	return rc;
3240b0c632dbSHeiko Carstens }
3241b0c632dbSHeiko Carstens 
3242d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3243d329c035SChristian Borntraeger {
324429b40f10SJanosch Frank 	u16 rc, rrc;
324529b40f10SJanosch Frank 
3246d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
3247ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
324867335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
32493c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
3250bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
3251a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
325224fe0195SPierre Morel 	kvm_s390_update_topology_change_report(vcpu->kvm, 1);
325327e0393fSCarsten Otte 
325427e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
32556ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
325627e0393fSCarsten Otte 
3257e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
3258b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
325929b40f10SJanosch Frank 	/* We can not hold the vcpu mutex here, we are already dying */
326029b40f10SJanosch Frank 	if (kvm_s390_pv_cpu_get_handle(vcpu))
326129b40f10SJanosch Frank 		kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
3262d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
3263d329c035SChristian Borntraeger }
3264d329c035SChristian Borntraeger 
3265b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
3266b0c632dbSHeiko Carstens {
326729b40f10SJanosch Frank 	u16 rc, rrc;
326829b40f10SJanosch Frank 
326927592ae8SMarc Zyngier 	kvm_destroy_vcpus(kvm);
32707d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
3271d7c5cb01SMichael Mueller 	kvm_s390_gisa_destroy(kvm);
327229b40f10SJanosch Frank 	/*
327329b40f10SJanosch Frank 	 * We are already at the end of life and kvm->lock is not taken.
327429b40f10SJanosch Frank 	 * This is ok as the file descriptor is closed by now and nobody
327529b40f10SJanosch Frank 	 * can mess with the pv state. To avoid lockdep_assert_held from
327629b40f10SJanosch Frank 	 * complaining we do not use kvm_s390_pv_is_protected.
327729b40f10SJanosch Frank 	 */
327829b40f10SJanosch Frank 	if (kvm_s390_pv_get_handle(kvm))
327929b40f10SJanosch Frank 		kvm_s390_pv_deinit_vm(kvm, &rc, &rrc);
3280ca2fd060SClaudio Imbrenda 	/*
3281ca2fd060SClaudio Imbrenda 	 * Remove the mmu notifier only when the whole KVM VM is torn down,
3282ca2fd060SClaudio Imbrenda 	 * and only if one was registered to begin with. If the VM is
3283ca2fd060SClaudio Imbrenda 	 * currently not protected, but has been previously been protected,
3284ca2fd060SClaudio Imbrenda 	 * then it's possible that the notifier is still registered.
3285ca2fd060SClaudio Imbrenda 	 */
3286ca2fd060SClaudio Imbrenda 	if (kvm->arch.pv.mmu_notifier.ops)
3287ca2fd060SClaudio Imbrenda 		mmu_notifier_unregister(&kvm->arch.pv.mmu_notifier, kvm->mm);
3288ca2fd060SClaudio Imbrenda 
328929b40f10SJanosch Frank 	debug_unregister(kvm->arch.dbf);
3290c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
329127e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
32926ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
3293841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
329467335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
3295a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
32968335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
3297b0c632dbSHeiko Carstens }
3298b0c632dbSHeiko Carstens 
3299b0c632dbSHeiko Carstens /* Section: vcpu related */
3300dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
3301b0c632dbSHeiko Carstens {
33026ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
330327e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
330427e0393fSCarsten Otte 		return -ENOMEM;
33052c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
3306dafd032aSDominik Dingel 
330727e0393fSCarsten Otte 	return 0;
330827e0393fSCarsten Otte }
330927e0393fSCarsten Otte 
3310a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
3311a6e2f683SEugene (jno) Dvurechenski {
3312a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
3313a6940674SDavid Hildenbrand 		return;
33145e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
33157d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
33167d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
33177d43bafcSEugene (jno) Dvurechenski 
33187d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
33197d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
33207d43bafcSEugene (jno) Dvurechenski 	} else {
3321bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
3322a6e2f683SEugene (jno) Dvurechenski 
3323a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
3324a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
3325a6e2f683SEugene (jno) Dvurechenski 	}
33265e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
33277d43bafcSEugene (jno) Dvurechenski }
3328a6e2f683SEugene (jno) Dvurechenski 
3329eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
3330a6e2f683SEugene (jno) Dvurechenski {
3331a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
3332*fe0ef003SNico Boehr 		phys_addr_t sca_phys = virt_to_phys(vcpu->kvm->arch.sca);
3333a6940674SDavid Hildenbrand 
3334a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
3335*fe0ef003SNico Boehr 		vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3336*fe0ef003SNico Boehr 		vcpu->arch.sie_block->scaol = sca_phys;
3337f07afa04SDavid Hildenbrand 		return;
3338a6940674SDavid Hildenbrand 	}
3339eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
3340eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
3341eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
3342*fe0ef003SNico Boehr 		phys_addr_t sca_phys = virt_to_phys(sca);
33437d43bafcSEugene (jno) Dvurechenski 
3344*fe0ef003SNico Boehr 		sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block);
3345*fe0ef003SNico Boehr 		vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3346*fe0ef003SNico Boehr 		vcpu->arch.sie_block->scaol = sca_phys & ESCA_SCAOL_MASK;
33470c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
3348eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
33497d43bafcSEugene (jno) Dvurechenski 	} else {
3350eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
3351*fe0ef003SNico Boehr 		phys_addr_t sca_phys = virt_to_phys(sca);
3352a6e2f683SEugene (jno) Dvurechenski 
3353*fe0ef003SNico Boehr 		sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block);
3354*fe0ef003SNico Boehr 		vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3355*fe0ef003SNico Boehr 		vcpu->arch.sie_block->scaol = sca_phys;
3356eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
3357a6e2f683SEugene (jno) Dvurechenski 	}
3358eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
33595e044315SEugene (jno) Dvurechenski }
33605e044315SEugene (jno) Dvurechenski 
33615e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
33625e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
33635e044315SEugene (jno) Dvurechenski {
33645e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
33655e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
33665e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
33675e044315SEugene (jno) Dvurechenski }
33685e044315SEugene (jno) Dvurechenski 
33695e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
33705e044315SEugene (jno) Dvurechenski {
33715e044315SEugene (jno) Dvurechenski 	int i;
33725e044315SEugene (jno) Dvurechenski 
33735e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
33745e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
33755e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
33765e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
33775e044315SEugene (jno) Dvurechenski }
33785e044315SEugene (jno) Dvurechenski 
33795e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
33805e044315SEugene (jno) Dvurechenski {
33815e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
33825e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
33835e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
338446808a4cSMarc Zyngier 	unsigned long vcpu_idx;
33855e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
3386*fe0ef003SNico Boehr 	phys_addr_t new_sca_phys;
33875e044315SEugene (jno) Dvurechenski 
338829b40f10SJanosch Frank 	if (kvm->arch.use_esca)
338929b40f10SJanosch Frank 		return 0;
339029b40f10SJanosch Frank 
3391c4196218SChristian Borntraeger 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);
33925e044315SEugene (jno) Dvurechenski 	if (!new_sca)
33935e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
33945e044315SEugene (jno) Dvurechenski 
3395*fe0ef003SNico Boehr 	new_sca_phys = virt_to_phys(new_sca);
3396*fe0ef003SNico Boehr 	scaoh = new_sca_phys >> 32;
3397*fe0ef003SNico Boehr 	scaol = new_sca_phys & ESCA_SCAOL_MASK;
33985e044315SEugene (jno) Dvurechenski 
33995e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
34005e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
34015e044315SEugene (jno) Dvurechenski 
34025e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
34035e044315SEugene (jno) Dvurechenski 
34045e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
34055e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
34065e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
34070c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
34085e044315SEugene (jno) Dvurechenski 	}
34095e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
34105e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
34115e044315SEugene (jno) Dvurechenski 
34125e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
34135e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
34145e044315SEugene (jno) Dvurechenski 
34155e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
34165e044315SEugene (jno) Dvurechenski 
34178335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
34188335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
34195e044315SEugene (jno) Dvurechenski 	return 0;
34207d43bafcSEugene (jno) Dvurechenski }
3421a6e2f683SEugene (jno) Dvurechenski 
3422a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
3423a6e2f683SEugene (jno) Dvurechenski {
34245e044315SEugene (jno) Dvurechenski 	int rc;
34255e044315SEugene (jno) Dvurechenski 
3426a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
3427a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
3428a6940674SDavid Hildenbrand 			return true;
3429a6940674SDavid Hildenbrand 		return false;
3430a6940674SDavid Hildenbrand 	}
34315e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
34325e044315SEugene (jno) Dvurechenski 		return true;
343376a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
34345e044315SEugene (jno) Dvurechenski 		return false;
34355e044315SEugene (jno) Dvurechenski 
34365e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
34375e044315SEugene (jno) Dvurechenski 
34385e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
3439a6e2f683SEugene (jno) Dvurechenski }
3440a6e2f683SEugene (jno) Dvurechenski 
3441db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3442db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3443db0758b2SDavid Hildenbrand {
3444db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
34459c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3446db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
34479c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3448db0758b2SDavid Hildenbrand }
3449db0758b2SDavid Hildenbrand 
3450db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3451db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3452db0758b2SDavid Hildenbrand {
3453db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
34549c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3455db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
3456db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
34579c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3458db0758b2SDavid Hildenbrand }
3459db0758b2SDavid Hildenbrand 
3460db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3461db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3462db0758b2SDavid Hildenbrand {
3463db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
3464db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
3465db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
3466db0758b2SDavid Hildenbrand }
3467db0758b2SDavid Hildenbrand 
3468db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3469db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3470db0758b2SDavid Hildenbrand {
3471db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
3472db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
3473db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
3474db0758b2SDavid Hildenbrand }
3475db0758b2SDavid Hildenbrand 
3476db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3477db0758b2SDavid Hildenbrand {
3478db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3479db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
3480db0758b2SDavid Hildenbrand 	preempt_enable();
3481db0758b2SDavid Hildenbrand }
3482db0758b2SDavid Hildenbrand 
3483db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3484db0758b2SDavid Hildenbrand {
3485db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3486db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
3487db0758b2SDavid Hildenbrand 	preempt_enable();
3488db0758b2SDavid Hildenbrand }
3489db0758b2SDavid Hildenbrand 
34904287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
34914287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
34924287f247SDavid Hildenbrand {
3493db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
34949c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3495db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
3496db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
34974287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
34989c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3499db0758b2SDavid Hildenbrand 	preempt_enable();
35004287f247SDavid Hildenbrand }
35014287f247SDavid Hildenbrand 
3502db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
35034287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
35044287f247SDavid Hildenbrand {
35059c23a131SDavid Hildenbrand 	unsigned int seq;
3506db0758b2SDavid Hildenbrand 	__u64 value;
3507db0758b2SDavid Hildenbrand 
3508db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
35094287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
3510db0758b2SDavid Hildenbrand 
35119c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
35129c23a131SDavid Hildenbrand 	do {
35139c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
35149c23a131SDavid Hildenbrand 		/*
35159c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
35169c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
35179c23a131SDavid Hildenbrand 		 */
35189c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
3519db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
35209c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
35219c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
3522db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
35239c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
35249c23a131SDavid Hildenbrand 	preempt_enable();
3525db0758b2SDavid Hildenbrand 	return value;
35264287f247SDavid Hildenbrand }
35274287f247SDavid Hildenbrand 
3528b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3529b0c632dbSHeiko Carstens {
35309977e886SHendrik Brueckner 
353137d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
3532ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
35335ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3534db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
353501a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
3536b0c632dbSHeiko Carstens }
3537b0c632dbSHeiko Carstens 
3538b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3539b0c632dbSHeiko Carstens {
354001a745acSDavid Hildenbrand 	vcpu->cpu = -1;
35415ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3542db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
35439daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
354437d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
354537d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
35469977e886SHendrik Brueckner 
3547b0c632dbSHeiko Carstens }
3548b0c632dbSHeiko Carstens 
354931928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
355042897d86SMarcelo Tosatti {
355172f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
3552fdf03650SFan Zhang 	preempt_disable();
355372f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
3554d16b52cbSDavid Hildenbrand 	vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
3555fdf03650SFan Zhang 	preempt_enable();
355672f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
355725508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
3558dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
3559eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
356025508824SDavid Hildenbrand 	}
35616502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
35626502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
356337d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
356437d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
356542897d86SMarcelo Tosatti }
356642897d86SMarcelo Tosatti 
35678ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
35688ec2fa52SChristian Borntraeger {
35698ec2fa52SChristian Borntraeger 	if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
35708ec2fa52SChristian Borntraeger 	    test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
35718ec2fa52SChristian Borntraeger 		return true;
35728ec2fa52SChristian Borntraeger 	return false;
35738ec2fa52SChristian Borntraeger }
35748ec2fa52SChristian Borntraeger 
35758ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm)
35768ec2fa52SChristian Borntraeger {
35778ec2fa52SChristian Borntraeger 	/* At least one ECC subfunction must be present */
35788ec2fa52SChristian Borntraeger 	return kvm_has_pckmo_subfunc(kvm, 32) ||
35798ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 33) ||
35808ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 34) ||
35818ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 40) ||
35828ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 41);
35838ec2fa52SChristian Borntraeger 
35848ec2fa52SChristian Borntraeger }
35858ec2fa52SChristian Borntraeger 
35865102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
35875102ee87STony Krowiak {
3588e585b24aSTony Krowiak 	/*
3589e585b24aSTony Krowiak 	 * If the AP instructions are not being interpreted and the MSAX3
3590e585b24aSTony Krowiak 	 * facility is not configured for the guest, there is nothing to set up.
3591e585b24aSTony Krowiak 	 */
3592e585b24aSTony Krowiak 	if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
35935102ee87STony Krowiak 		return;
35945102ee87STony Krowiak 
3595e585b24aSTony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
3596a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
359737940fb0STony Krowiak 	vcpu->arch.sie_block->eca &= ~ECA_APIE;
35988ec2fa52SChristian Borntraeger 	vcpu->arch.sie_block->ecd &= ~ECD_ECC;
3599a374e892STony Krowiak 
3600e585b24aSTony Krowiak 	if (vcpu->kvm->arch.crypto.apie)
3601e585b24aSTony Krowiak 		vcpu->arch.sie_block->eca |= ECA_APIE;
3602e585b24aSTony Krowiak 
3603e585b24aSTony Krowiak 	/* Set up protected key support */
36048ec2fa52SChristian Borntraeger 	if (vcpu->kvm->arch.crypto.aes_kw) {
3605a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
36068ec2fa52SChristian Borntraeger 		/* ecc is also wrapped with AES key */
36078ec2fa52SChristian Borntraeger 		if (kvm_has_pckmo_ecc(vcpu->kvm))
36088ec2fa52SChristian Borntraeger 			vcpu->arch.sie_block->ecd |= ECD_ECC;
36098ec2fa52SChristian Borntraeger 	}
36108ec2fa52SChristian Borntraeger 
3611a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
3612a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
36135102ee87STony Krowiak }
36145102ee87STony Krowiak 
3615b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
3616b31605c1SDominik Dingel {
3617*fe0ef003SNico Boehr 	free_page((unsigned long)phys_to_virt(vcpu->arch.sie_block->cbrlo));
3618b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
3619b31605c1SDominik Dingel }
3620b31605c1SDominik Dingel 
3621b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
3622b31605c1SDominik Dingel {
3623*fe0ef003SNico Boehr 	void *cbrlo_page = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3624*fe0ef003SNico Boehr 
3625*fe0ef003SNico Boehr 	if (!cbrlo_page)
3626b31605c1SDominik Dingel 		return -ENOMEM;
3627*fe0ef003SNico Boehr 
3628*fe0ef003SNico Boehr 	vcpu->arch.sie_block->cbrlo = virt_to_phys(cbrlo_page);
3629b31605c1SDominik Dingel 	return 0;
3630b31605c1SDominik Dingel }
3631b31605c1SDominik Dingel 
363291520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
363391520f1aSMichael Mueller {
363491520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
363591520f1aSMichael Mueller 
363691520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
363780bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
3638*fe0ef003SNico Boehr 		vcpu->arch.sie_block->fac = virt_to_phys(model->fac_list);
363991520f1aSMichael Mueller }
364091520f1aSMichael Mueller 
3641ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
3642ff72bb55SSean Christopherson {
3643b31605c1SDominik Dingel 	int rc = 0;
364429b40f10SJanosch Frank 	u16 uvrc, uvrrc;
3645b31288faSKonstantin Weitz 
36469e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
36479e6dabefSCornelia Huck 						    CPUSTAT_SM |
3648a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
3649a4a4f191SGuenther Hutzl 
365053df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
3651ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
365253df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
3653ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
3654a4a4f191SGuenther Hutzl 
365591520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
365691520f1aSMichael Mueller 
3657bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
3658bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
36590c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
3660bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
36610c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_SRSI;
366224fe0195SPierre Morel 	if (test_kvm_facility(vcpu->kvm, 11))
366324fe0195SPierre Morel 		vcpu->arch.sie_block->ecb |= ECB_PTF;
3664f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
36650c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_TE;
36667119decfSJanis Schoetterl-Glausch 	if (!kvm_is_ucontrol(vcpu->kvm))
36677119decfSJanis Schoetterl-Glausch 		vcpu->arch.sie_block->ecb |= ECB_SPECI;
36687feb6bb8SMichael Mueller 
3669c9f0a2b8SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
36700c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
3671cd1836f5SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 130))
36720c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
36730c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
367448ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
36750c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_CEI;
367611ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
36770c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_IB;
367837c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
36790c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SII;
368037c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
36810c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SIGPI;
368218280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
36830c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_VX;
36840c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
368513211ea7SEric Farman 	}
36868fa1696eSCollin L. Walling 	if (test_kvm_facility(vcpu->kvm, 139))
36878fa1696eSCollin L. Walling 		vcpu->arch.sie_block->ecd |= ECD_MEF;
3688a3da7b4aSChristian Borntraeger 	if (test_kvm_facility(vcpu->kvm, 156))
3689a3da7b4aSChristian Borntraeger 		vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
3690d7c5cb01SMichael Mueller 	if (vcpu->arch.sie_block->gd) {
3691d7c5cb01SMichael Mueller 		vcpu->arch.sie_block->eca |= ECA_AIV;
3692d7c5cb01SMichael Mueller 		VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3693d7c5cb01SMichael Mueller 			   vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3694d7c5cb01SMichael Mueller 	}
3695*fe0ef003SNico Boehr 	vcpu->arch.sie_block->sdnxo = virt_to_phys(&vcpu->run->s.regs.sdnx) | SDNXC;
3696*fe0ef003SNico Boehr 	vcpu->arch.sie_block->riccbd = virt_to_phys(&vcpu->run->s.regs.riccb);
3697730cd632SFarhan Ali 
3698730cd632SFarhan Ali 	if (sclp.has_kss)
3699ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
3700730cd632SFarhan Ali 	else
3701492d8642SThomas Huth 		vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
37025a5e6536SMatthew Rosato 
3703e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
3704b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
3705b31605c1SDominik Dingel 		if (rc)
3706b31605c1SDominik Dingel 			return rc;
3707b31288faSKonstantin Weitz 	}
37080ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3709ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
37109d8d5786SMichael Mueller 
371167d49d52SCollin Walling 	vcpu->arch.sie_block->hpid = HPID_KVM;
371267d49d52SCollin Walling 
37135102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
37145102ee87STony Krowiak 
37153f4bbb43SMatthew Rosato 	kvm_s390_vcpu_pci_setup(vcpu);
37163f4bbb43SMatthew Rosato 
371729b40f10SJanosch Frank 	mutex_lock(&vcpu->kvm->lock);
371829b40f10SJanosch Frank 	if (kvm_s390_pv_is_protected(vcpu->kvm)) {
371929b40f10SJanosch Frank 		rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
372029b40f10SJanosch Frank 		if (rc)
372129b40f10SJanosch Frank 			kvm_s390_vcpu_unsetup_cmma(vcpu);
372229b40f10SJanosch Frank 	}
372329b40f10SJanosch Frank 	mutex_unlock(&vcpu->kvm->lock);
372429b40f10SJanosch Frank 
3725b31605c1SDominik Dingel 	return rc;
3726b0c632dbSHeiko Carstens }
3727b0c632dbSHeiko Carstens 
3728897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3729897cc38eSSean Christopherson {
3730897cc38eSSean Christopherson 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
3731897cc38eSSean Christopherson 		return -EINVAL;
3732897cc38eSSean Christopherson 	return 0;
3733897cc38eSSean Christopherson }
3734897cc38eSSean Christopherson 
3735e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3736b0c632dbSHeiko Carstens {
37377feb6bb8SMichael Mueller 	struct sie_page *sie_page;
3738897cc38eSSean Christopherson 	int rc;
37394d47555aSCarsten Otte 
3740da72ca4dSQingFeng Hao 	BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
3741c4196218SChristian Borntraeger 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT);
37427feb6bb8SMichael Mueller 	if (!sie_page)
3743e529ef66SSean Christopherson 		return -ENOMEM;
3744b0c632dbSHeiko Carstens 
37457feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
3746*fe0ef003SNico Boehr 	vcpu->arch.sie_block->itdba = virt_to_phys(&sie_page->itdb);
37477feb6bb8SMichael Mueller 
3748efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
3749efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
3750efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
3751efed1104SDavid Hildenbrand 
3752e529ef66SSean Christopherson 	vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
3753ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
3754ee6a569dSMichael Mueller 	vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm);
37559c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
3756ba5c1e9bSCarsten Otte 
3757321f8ee5SSean Christopherson 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3758321f8ee5SSean Christopherson 	kvm_clear_async_pf_completion_queue(vcpu);
3759321f8ee5SSean Christopherson 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
3760321f8ee5SSean Christopherson 				    KVM_SYNC_GPRS |
3761321f8ee5SSean Christopherson 				    KVM_SYNC_ACRS |
3762321f8ee5SSean Christopherson 				    KVM_SYNC_CRS |
3763321f8ee5SSean Christopherson 				    KVM_SYNC_ARCH0 |
376423a60f83SCollin Walling 				    KVM_SYNC_PFAULT |
376523a60f83SCollin Walling 				    KVM_SYNC_DIAG318;
3766321f8ee5SSean Christopherson 	kvm_s390_set_prefix(vcpu, 0);
3767321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 64))
3768321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
3769321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 82))
3770321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
3771321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 133))
3772321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
3773321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 156))
3774321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
3775321f8ee5SSean Christopherson 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
3776321f8ee5SSean Christopherson 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
3777321f8ee5SSean Christopherson 	 */
3778321f8ee5SSean Christopherson 	if (MACHINE_HAS_VX)
3779321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
3780321f8ee5SSean Christopherson 	else
3781321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
3782321f8ee5SSean Christopherson 
3783321f8ee5SSean Christopherson 	if (kvm_is_ucontrol(vcpu->kvm)) {
3784321f8ee5SSean Christopherson 		rc = __kvm_ucontrol_vcpu_init(vcpu);
3785321f8ee5SSean Christopherson 		if (rc)
3786a2017f17SSean Christopherson 			goto out_free_sie_block;
3787321f8ee5SSean Christopherson 	}
3788321f8ee5SSean Christopherson 
3789e529ef66SSean Christopherson 	VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
3790e529ef66SSean Christopherson 		 vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3791e529ef66SSean Christopherson 	trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3792b0c632dbSHeiko Carstens 
3793ff72bb55SSean Christopherson 	rc = kvm_s390_vcpu_setup(vcpu);
3794ff72bb55SSean Christopherson 	if (rc)
3795ff72bb55SSean Christopherson 		goto out_ucontrol_uninit;
379624fe0195SPierre Morel 
379724fe0195SPierre Morel 	kvm_s390_update_topology_change_report(vcpu->kvm, 1);
3798e529ef66SSean Christopherson 	return 0;
3799e529ef66SSean Christopherson 
3800ff72bb55SSean Christopherson out_ucontrol_uninit:
3801ff72bb55SSean Christopherson 	if (kvm_is_ucontrol(vcpu->kvm))
3802ff72bb55SSean Christopherson 		gmap_remove(vcpu->arch.gmap);
38037b06bf2fSWei Yongjun out_free_sie_block:
38047b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
3805e529ef66SSean Christopherson 	return rc;
3806b0c632dbSHeiko Carstens }
3807b0c632dbSHeiko Carstens 
3808b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3809b0c632dbSHeiko Carstens {
38109b57e9d5SHalil Pasic 	clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
38119a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
3812b0c632dbSHeiko Carstens }
3813b0c632dbSHeiko Carstens 
3814199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
3815199b5763SLongpeng(Mike) {
38160546c63dSLongpeng(Mike) 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
3817199b5763SLongpeng(Mike) }
3818199b5763SLongpeng(Mike) 
381927406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
382049b99e1eSChristian Borntraeger {
3821805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
382261a6df54SDavid Hildenbrand 	exit_sie(vcpu);
382349b99e1eSChristian Borntraeger }
382449b99e1eSChristian Borntraeger 
382527406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
382649b99e1eSChristian Borntraeger {
3827805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
382849b99e1eSChristian Borntraeger }
382949b99e1eSChristian Borntraeger 
38308e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
38318e236546SChristian Borntraeger {
3832805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
383361a6df54SDavid Hildenbrand 	exit_sie(vcpu);
38348e236546SChristian Borntraeger }
38358e236546SChristian Borntraeger 
38369ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
38379ea59728SDavid Hildenbrand {
38389ea59728SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->prog20) &
38399ea59728SDavid Hildenbrand 	       (PROG_BLOCK_SIE | PROG_REQUEST);
38409ea59728SDavid Hildenbrand }
38419ea59728SDavid Hildenbrand 
38428e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
38438e236546SChristian Borntraeger {
38449bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
38458e236546SChristian Borntraeger }
38468e236546SChristian Borntraeger 
384749b99e1eSChristian Borntraeger /*
38489ea59728SDavid Hildenbrand  * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
384949b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
385049b99e1eSChristian Borntraeger  * return immediately. */
385149b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
385249b99e1eSChristian Borntraeger {
3853ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
38549ea59728SDavid Hildenbrand 	kvm_s390_vsie_kick(vcpu);
385549b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
385649b99e1eSChristian Borntraeger 		cpu_relax();
385749b99e1eSChristian Borntraeger }
385849b99e1eSChristian Borntraeger 
38598e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
38608e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
386149b99e1eSChristian Borntraeger {
3862df06dae3SSean Christopherson 	__kvm_make_request(req, vcpu);
38638e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
386449b99e1eSChristian Borntraeger }
386549b99e1eSChristian Borntraeger 
3866414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
3867414d3b07SMartin Schwidefsky 			      unsigned long end)
38682c70fe44SChristian Borntraeger {
38692c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
38702c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
3871414d3b07SMartin Schwidefsky 	unsigned long prefix;
387246808a4cSMarc Zyngier 	unsigned long i;
38732c70fe44SChristian Borntraeger 
387465d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
387565d0b0d4SDavid Hildenbrand 		return;
3876414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
3877414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
3878414d3b07SMartin Schwidefsky 		return;
38792c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
38802c70fe44SChristian Borntraeger 		/* match against both prefix pages */
3881414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
3882414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
3883414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
3884414d3b07SMartin Schwidefsky 				   start, end);
3885cc65c3a1SSean Christopherson 			kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
38862c70fe44SChristian Borntraeger 		}
38872c70fe44SChristian Borntraeger 	}
38882c70fe44SChristian Borntraeger }
38892c70fe44SChristian Borntraeger 
38908b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
38918b905d28SChristian Borntraeger {
38928b905d28SChristian Borntraeger 	/* do not poll with more than halt_poll_max_steal percent of steal time */
38938b905d28SChristian Borntraeger 	if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >=
38946f390916SSean Christopherson 	    READ_ONCE(halt_poll_max_steal)) {
38958b905d28SChristian Borntraeger 		vcpu->stat.halt_no_poll_steal++;
38968b905d28SChristian Borntraeger 		return true;
38978b905d28SChristian Borntraeger 	}
38988b905d28SChristian Borntraeger 	return false;
38998b905d28SChristian Borntraeger }
39008b905d28SChristian Borntraeger 
3901b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
3902b6d33834SChristoffer Dall {
3903b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
3904b6d33834SChristoffer Dall 	BUG();
3905b6d33834SChristoffer Dall 	return 0;
3906b6d33834SChristoffer Dall }
3907b6d33834SChristoffer Dall 
390814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
390914eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
391014eebd91SCarsten Otte {
391114eebd91SCarsten Otte 	int r = -EINVAL;
391214eebd91SCarsten Otte 
391314eebd91SCarsten Otte 	switch (reg->id) {
391429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
391529b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
391629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
391729b7c71bSCarsten Otte 		break;
391829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
391929b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
392029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
392129b7c71bSCarsten Otte 		break;
392246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
39234287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
392446a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
392546a6dd1cSJason J. herne 		break;
392646a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
392746a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
392846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
392946a6dd1cSJason J. herne 		break;
3930536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
3931536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
3932536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3933536336c2SDominik Dingel 		break;
3934536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
3935536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
3936536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3937536336c2SDominik Dingel 		break;
3938536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
3939536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
3940536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3941536336c2SDominik Dingel 		break;
3942672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
3943672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
3944672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
3945672550fbSChristian Borntraeger 		break;
3946afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
3947afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
3948afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
3949afa45ff5SChristian Borntraeger 		break;
395014eebd91SCarsten Otte 	default:
395114eebd91SCarsten Otte 		break;
395214eebd91SCarsten Otte 	}
395314eebd91SCarsten Otte 
395414eebd91SCarsten Otte 	return r;
395514eebd91SCarsten Otte }
395614eebd91SCarsten Otte 
395714eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
395814eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
395914eebd91SCarsten Otte {
396014eebd91SCarsten Otte 	int r = -EINVAL;
39614287f247SDavid Hildenbrand 	__u64 val;
396214eebd91SCarsten Otte 
396314eebd91SCarsten Otte 	switch (reg->id) {
396429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
396529b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
396629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
396729b7c71bSCarsten Otte 		break;
396829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
396929b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
397029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
397129b7c71bSCarsten Otte 		break;
397246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
39734287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
39744287f247SDavid Hildenbrand 		if (!r)
39754287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
397646a6dd1cSJason J. herne 		break;
397746a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
397846a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
397946a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
398046a6dd1cSJason J. herne 		break;
3981536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
3982536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
3983536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
39849fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
39859fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
3986536336c2SDominik Dingel 		break;
3987536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
3988536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
3989536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3990536336c2SDominik Dingel 		break;
3991536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
3992536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
3993536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3994536336c2SDominik Dingel 		break;
3995672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
3996672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
3997672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
3998672550fbSChristian Borntraeger 		break;
3999afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
4000afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
4001afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
4002afa45ff5SChristian Borntraeger 		break;
400314eebd91SCarsten Otte 	default:
400414eebd91SCarsten Otte 		break;
400514eebd91SCarsten Otte 	}
400614eebd91SCarsten Otte 
400714eebd91SCarsten Otte 	return r;
400814eebd91SCarsten Otte }
4009b6d33834SChristoffer Dall 
40107de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
4011b0c632dbSHeiko Carstens {
40127de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
40137de3f142SJanosch Frank 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
40147de3f142SJanosch Frank 	memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
40157de3f142SJanosch Frank 
40167de3f142SJanosch Frank 	kvm_clear_async_pf_completion_queue(vcpu);
40177de3f142SJanosch Frank 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
40187de3f142SJanosch Frank 		kvm_s390_vcpu_stop(vcpu);
40197de3f142SJanosch Frank 	kvm_s390_clear_local_irqs(vcpu);
40207de3f142SJanosch Frank }
40217de3f142SJanosch Frank 
40227de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
40237de3f142SJanosch Frank {
40247de3f142SJanosch Frank 	/* Initial reset is a superset of the normal reset */
40257de3f142SJanosch Frank 	kvm_arch_vcpu_ioctl_normal_reset(vcpu);
40267de3f142SJanosch Frank 
4027e93fc7b4SChristian Borntraeger 	/*
4028e93fc7b4SChristian Borntraeger 	 * This equals initial cpu reset in pop, but we don't switch to ESA.
4029e93fc7b4SChristian Borntraeger 	 * We do not only reset the internal data, but also ...
4030e93fc7b4SChristian Borntraeger 	 */
40317de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.mask = 0;
40327de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.addr = 0;
40337de3f142SJanosch Frank 	kvm_s390_set_prefix(vcpu, 0);
40347de3f142SJanosch Frank 	kvm_s390_set_cpu_timer(vcpu, 0);
40357de3f142SJanosch Frank 	vcpu->arch.sie_block->ckc = 0;
40367de3f142SJanosch Frank 	memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
40377de3f142SJanosch Frank 	vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
40387de3f142SJanosch Frank 	vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
4039e93fc7b4SChristian Borntraeger 
4040e93fc7b4SChristian Borntraeger 	/* ... the data in sync regs */
4041e93fc7b4SChristian Borntraeger 	memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs));
4042e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.ckc = 0;
4043e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK;
4044e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK;
4045e93fc7b4SChristian Borntraeger 	vcpu->run->psw_addr = 0;
4046e93fc7b4SChristian Borntraeger 	vcpu->run->psw_mask = 0;
4047e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.todpr = 0;
4048e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.cputm = 0;
4049e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.ckc = 0;
4050e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.pp = 0;
4051e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.gbea = 1;
40527de3f142SJanosch Frank 	vcpu->run->s.regs.fpc = 0;
40530f303504SJanosch Frank 	/*
40540f303504SJanosch Frank 	 * Do not reset these registers in the protected case, as some of
40550f303504SJanosch Frank 	 * them are overlayed and they are not accessible in this case
40560f303504SJanosch Frank 	 * anyway.
40570f303504SJanosch Frank 	 */
40580f303504SJanosch Frank 	if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
40597de3f142SJanosch Frank 		vcpu->arch.sie_block->gbea = 1;
40607de3f142SJanosch Frank 		vcpu->arch.sie_block->pp = 0;
40617de3f142SJanosch Frank 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
40620f303504SJanosch Frank 		vcpu->arch.sie_block->todpr = 0;
40630f303504SJanosch Frank 	}
40647de3f142SJanosch Frank }
40657de3f142SJanosch Frank 
40667de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
40677de3f142SJanosch Frank {
40687de3f142SJanosch Frank 	struct kvm_sync_regs *regs = &vcpu->run->s.regs;
40697de3f142SJanosch Frank 
40707de3f142SJanosch Frank 	/* Clear reset is a superset of the initial reset */
40717de3f142SJanosch Frank 	kvm_arch_vcpu_ioctl_initial_reset(vcpu);
40727de3f142SJanosch Frank 
40737de3f142SJanosch Frank 	memset(&regs->gprs, 0, sizeof(regs->gprs));
40747de3f142SJanosch Frank 	memset(&regs->vrs, 0, sizeof(regs->vrs));
40757de3f142SJanosch Frank 	memset(&regs->acrs, 0, sizeof(regs->acrs));
40767de3f142SJanosch Frank 	memset(&regs->gscb, 0, sizeof(regs->gscb));
40777de3f142SJanosch Frank 
40787de3f142SJanosch Frank 	regs->etoken = 0;
40797de3f142SJanosch Frank 	regs->etoken_extension = 0;
4080b0c632dbSHeiko Carstens }
4081b0c632dbSHeiko Carstens 
4082b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4083b0c632dbSHeiko Carstens {
4084875656feSChristoffer Dall 	vcpu_load(vcpu);
40855a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
4086875656feSChristoffer Dall 	vcpu_put(vcpu);
4087b0c632dbSHeiko Carstens 	return 0;
4088b0c632dbSHeiko Carstens }
4089b0c632dbSHeiko Carstens 
4090b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4091b0c632dbSHeiko Carstens {
40921fc9b76bSChristoffer Dall 	vcpu_load(vcpu);
40935a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
40941fc9b76bSChristoffer Dall 	vcpu_put(vcpu);
4095b0c632dbSHeiko Carstens 	return 0;
4096b0c632dbSHeiko Carstens }
4097b0c632dbSHeiko Carstens 
4098b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4099b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
4100b0c632dbSHeiko Carstens {
4101b4ef9d4eSChristoffer Dall 	vcpu_load(vcpu);
4102b4ef9d4eSChristoffer Dall 
410359674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
4104b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
4105b4ef9d4eSChristoffer Dall 
4106b4ef9d4eSChristoffer Dall 	vcpu_put(vcpu);
4107b0c632dbSHeiko Carstens 	return 0;
4108b0c632dbSHeiko Carstens }
4109b0c632dbSHeiko Carstens 
4110b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4111b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
4112b0c632dbSHeiko Carstens {
4113bcdec41cSChristoffer Dall 	vcpu_load(vcpu);
4114bcdec41cSChristoffer Dall 
411559674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
4116b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
4117bcdec41cSChristoffer Dall 
4118bcdec41cSChristoffer Dall 	vcpu_put(vcpu);
4119b0c632dbSHeiko Carstens 	return 0;
4120b0c632dbSHeiko Carstens }
4121b0c632dbSHeiko Carstens 
4122b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4123b0c632dbSHeiko Carstens {
41246a96bc7fSChristoffer Dall 	int ret = 0;
41256a96bc7fSChristoffer Dall 
41266a96bc7fSChristoffer Dall 	vcpu_load(vcpu);
41276a96bc7fSChristoffer Dall 
41286a96bc7fSChristoffer Dall 	if (test_fp_ctl(fpu->fpc)) {
41296a96bc7fSChristoffer Dall 		ret = -EINVAL;
41306a96bc7fSChristoffer Dall 		goto out;
41316a96bc7fSChristoffer Dall 	}
4132e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
41339abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
4134a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
4135a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
41369abc2a08SDavid Hildenbrand 	else
4137a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
41386a96bc7fSChristoffer Dall 
41396a96bc7fSChristoffer Dall out:
41406a96bc7fSChristoffer Dall 	vcpu_put(vcpu);
41416a96bc7fSChristoffer Dall 	return ret;
4142b0c632dbSHeiko Carstens }
4143b0c632dbSHeiko Carstens 
4144b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4145b0c632dbSHeiko Carstens {
41461393123eSChristoffer Dall 	vcpu_load(vcpu);
41471393123eSChristoffer Dall 
41489abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
41499abc2a08SDavid Hildenbrand 	save_fpu_regs();
41509abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
4151a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
4152a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
41539abc2a08SDavid Hildenbrand 	else
4154a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
4155e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
41561393123eSChristoffer Dall 
41571393123eSChristoffer Dall 	vcpu_put(vcpu);
4158b0c632dbSHeiko Carstens 	return 0;
4159b0c632dbSHeiko Carstens }
4160b0c632dbSHeiko Carstens 
4161b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
4162b0c632dbSHeiko Carstens {
4163b0c632dbSHeiko Carstens 	int rc = 0;
4164b0c632dbSHeiko Carstens 
41657a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
4166b0c632dbSHeiko Carstens 		rc = -EBUSY;
4167d7b0b5ebSCarsten Otte 	else {
4168d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
4169d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
4170d7b0b5ebSCarsten Otte 	}
4171b0c632dbSHeiko Carstens 	return rc;
4172b0c632dbSHeiko Carstens }
4173b0c632dbSHeiko Carstens 
4174b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4175b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
4176b0c632dbSHeiko Carstens {
4177b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
4178b0c632dbSHeiko Carstens }
4179b0c632dbSHeiko Carstens 
418027291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
418127291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
418227291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
418327291e21SDavid Hildenbrand 
4184d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4185d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
4186b0c632dbSHeiko Carstens {
418727291e21SDavid Hildenbrand 	int rc = 0;
418827291e21SDavid Hildenbrand 
418966b56562SChristoffer Dall 	vcpu_load(vcpu);
419066b56562SChristoffer Dall 
419127291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
419227291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
419327291e21SDavid Hildenbrand 
419466b56562SChristoffer Dall 	if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
419566b56562SChristoffer Dall 		rc = -EINVAL;
419666b56562SChristoffer Dall 		goto out;
419766b56562SChristoffer Dall 	}
419866b56562SChristoffer Dall 	if (!sclp.has_gpere) {
419966b56562SChristoffer Dall 		rc = -EINVAL;
420066b56562SChristoffer Dall 		goto out;
420166b56562SChristoffer Dall 	}
420227291e21SDavid Hildenbrand 
420327291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
420427291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
420527291e21SDavid Hildenbrand 		/* enforce guest PER */
4206ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
420727291e21SDavid Hildenbrand 
420827291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
420927291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
421027291e21SDavid Hildenbrand 	} else {
42119daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
421227291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
421327291e21SDavid Hildenbrand 	}
421427291e21SDavid Hildenbrand 
421527291e21SDavid Hildenbrand 	if (rc) {
421627291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
421727291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
42189daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
421927291e21SDavid Hildenbrand 	}
422027291e21SDavid Hildenbrand 
422166b56562SChristoffer Dall out:
422266b56562SChristoffer Dall 	vcpu_put(vcpu);
422327291e21SDavid Hildenbrand 	return rc;
4224b0c632dbSHeiko Carstens }
4225b0c632dbSHeiko Carstens 
422662d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
422762d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
422862d9f0dbSMarcelo Tosatti {
4229fd232561SChristoffer Dall 	int ret;
4230fd232561SChristoffer Dall 
4231fd232561SChristoffer Dall 	vcpu_load(vcpu);
4232fd232561SChristoffer Dall 
42336352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
4234fd232561SChristoffer Dall 	ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
42356352e4d2SDavid Hildenbrand 				      KVM_MP_STATE_OPERATING;
4236fd232561SChristoffer Dall 
4237fd232561SChristoffer Dall 	vcpu_put(vcpu);
4238fd232561SChristoffer Dall 	return ret;
423962d9f0dbSMarcelo Tosatti }
424062d9f0dbSMarcelo Tosatti 
424162d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
424262d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
424362d9f0dbSMarcelo Tosatti {
42446352e4d2SDavid Hildenbrand 	int rc = 0;
42456352e4d2SDavid Hildenbrand 
4246e83dff5eSChristoffer Dall 	vcpu_load(vcpu);
4247e83dff5eSChristoffer Dall 
42486352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
424967cf68b6SEric Farman 	kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm);
42506352e4d2SDavid Hildenbrand 
42516352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
42526352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
4253fe28c786SJanosch Frank 		rc = kvm_s390_vcpu_stop(vcpu);
42546352e4d2SDavid Hildenbrand 		break;
42556352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
4256fe28c786SJanosch Frank 		rc = kvm_s390_vcpu_start(vcpu);
42576352e4d2SDavid Hildenbrand 		break;
42586352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
42597c36a3fcSJanosch Frank 		if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
42607c36a3fcSJanosch Frank 			rc = -ENXIO;
42617c36a3fcSJanosch Frank 			break;
42627c36a3fcSJanosch Frank 		}
42637c36a3fcSJanosch Frank 		rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD);
42647c36a3fcSJanosch Frank 		break;
42656352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
42663b684a42SJoe Perches 		fallthrough;	/* CHECK_STOP and LOAD are not supported yet */
42676352e4d2SDavid Hildenbrand 	default:
42686352e4d2SDavid Hildenbrand 		rc = -ENXIO;
42696352e4d2SDavid Hildenbrand 	}
42706352e4d2SDavid Hildenbrand 
4271e83dff5eSChristoffer Dall 	vcpu_put(vcpu);
42726352e4d2SDavid Hildenbrand 	return rc;
427362d9f0dbSMarcelo Tosatti }
427462d9f0dbSMarcelo Tosatti 
42758ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
42768ad35755SDavid Hildenbrand {
42778d5fb0dcSDavid Hildenbrand 	return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
42788ad35755SDavid Hildenbrand }
42798ad35755SDavid Hildenbrand 
42802c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
42812c70fe44SChristian Borntraeger {
42828ad35755SDavid Hildenbrand retry:
42838e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
42842fa6e1e1SRadim Krčmář 	if (!kvm_request_pending(vcpu))
4285586b7ccdSChristian Borntraeger 		return 0;
42862c70fe44SChristian Borntraeger 	/*
4287cc65c3a1SSean Christopherson 	 * If the guest prefix changed, re-arm the ipte notifier for the
4288b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
42892c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
42902c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
42912c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
42922c70fe44SChristian Borntraeger 	 */
4293cc65c3a1SSean Christopherson 	if (kvm_check_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu)) {
42942c70fe44SChristian Borntraeger 		int rc;
4295b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
4296fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
4297b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
4298aca411a4SJulius Niedworok 		if (rc) {
4299cc65c3a1SSean Christopherson 			kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
43002c70fe44SChristian Borntraeger 			return rc;
4301aca411a4SJulius Niedworok 		}
43028ad35755SDavid Hildenbrand 		goto retry;
43032c70fe44SChristian Borntraeger 	}
43048ad35755SDavid Hildenbrand 
4305d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
4306d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
4307d3d692c8SDavid Hildenbrand 		goto retry;
4308d3d692c8SDavid Hildenbrand 	}
4309d3d692c8SDavid Hildenbrand 
43108ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
43118ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
43128ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
4313ef8f4f49SDavid Hildenbrand 			kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
43148ad35755SDavid Hildenbrand 		}
43158ad35755SDavid Hildenbrand 		goto retry;
43168ad35755SDavid Hildenbrand 	}
43178ad35755SDavid Hildenbrand 
43188ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
43198ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
43208ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
43219daecfc6SDavid Hildenbrand 			kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
43228ad35755SDavid Hildenbrand 		}
43238ad35755SDavid Hildenbrand 		goto retry;
43248ad35755SDavid Hildenbrand 	}
43258ad35755SDavid Hildenbrand 
43266502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
43276502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
43286502a34cSDavid Hildenbrand 		goto retry;
43296502a34cSDavid Hildenbrand 	}
43306502a34cSDavid Hildenbrand 
4331190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
4332190df4a2SClaudio Imbrenda 		/*
4333c9f0a2b8SJanosch Frank 		 * Disable CMM virtualization; we will emulate the ESSA
4334190df4a2SClaudio Imbrenda 		 * instruction manually, in order to provide additional
4335190df4a2SClaudio Imbrenda 		 * functionalities needed for live migration.
4336190df4a2SClaudio Imbrenda 		 */
4337190df4a2SClaudio Imbrenda 		vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
4338190df4a2SClaudio Imbrenda 		goto retry;
4339190df4a2SClaudio Imbrenda 	}
4340190df4a2SClaudio Imbrenda 
4341190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
4342190df4a2SClaudio Imbrenda 		/*
4343c9f0a2b8SJanosch Frank 		 * Re-enable CMM virtualization if CMMA is available and
4344c9f0a2b8SJanosch Frank 		 * CMM has been used.
4345190df4a2SClaudio Imbrenda 		 */
4346190df4a2SClaudio Imbrenda 		if ((vcpu->kvm->arch.use_cmma) &&
4347c9f0a2b8SJanosch Frank 		    (vcpu->kvm->mm->context.uses_cmm))
4348190df4a2SClaudio Imbrenda 			vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
4349190df4a2SClaudio Imbrenda 		goto retry;
4350190df4a2SClaudio Imbrenda 	}
4351190df4a2SClaudio Imbrenda 
43523194cdb7SDavid Hildenbrand 	/* we left the vsie handler, nothing to do, just clear the request */
43533194cdb7SDavid Hildenbrand 	kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
43540759d068SDavid Hildenbrand 
43552c70fe44SChristian Borntraeger 	return 0;
43562c70fe44SChristian Borntraeger }
43572c70fe44SChristian Borntraeger 
4358c0573ba5SClaudio Imbrenda static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
43598fa1696eSCollin L. Walling {
43608fa1696eSCollin L. Walling 	struct kvm_vcpu *vcpu;
43612cfd7b73SHeiko Carstens 	union tod_clock clk;
436246808a4cSMarc Zyngier 	unsigned long i;
43638fa1696eSCollin L. Walling 
43648fa1696eSCollin L. Walling 	preempt_disable();
43658fa1696eSCollin L. Walling 
43662cfd7b73SHeiko Carstens 	store_tod_clock_ext(&clk);
43678fa1696eSCollin L. Walling 
43682cfd7b73SHeiko Carstens 	kvm->arch.epoch = gtod->tod - clk.tod;
43690e7def5fSDavid Hildenbrand 	kvm->arch.epdx = 0;
43700e7def5fSDavid Hildenbrand 	if (test_kvm_facility(kvm, 139)) {
43712cfd7b73SHeiko Carstens 		kvm->arch.epdx = gtod->epoch_idx - clk.ei;
43728fa1696eSCollin L. Walling 		if (kvm->arch.epoch > gtod->tod)
43738fa1696eSCollin L. Walling 			kvm->arch.epdx -= 1;
43740e7def5fSDavid Hildenbrand 	}
43758fa1696eSCollin L. Walling 
43768fa1696eSCollin L. Walling 	kvm_s390_vcpu_block_all(kvm);
43778fa1696eSCollin L. Walling 	kvm_for_each_vcpu(i, vcpu, kvm) {
43788fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
43798fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epdx  = kvm->arch.epdx;
43808fa1696eSCollin L. Walling 	}
43818fa1696eSCollin L. Walling 
43828fa1696eSCollin L. Walling 	kvm_s390_vcpu_unblock_all(kvm);
43838fa1696eSCollin L. Walling 	preempt_enable();
4384c0573ba5SClaudio Imbrenda }
4385c0573ba5SClaudio Imbrenda 
4386c0573ba5SClaudio Imbrenda void kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
4387c0573ba5SClaudio Imbrenda {
4388c0573ba5SClaudio Imbrenda 	mutex_lock(&kvm->lock);
4389c0573ba5SClaudio Imbrenda 	__kvm_s390_set_tod_clock(kvm, gtod);
43908fa1696eSCollin L. Walling 	mutex_unlock(&kvm->lock);
43918fa1696eSCollin L. Walling }
43928fa1696eSCollin L. Walling 
4393c0573ba5SClaudio Imbrenda int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
4394c0573ba5SClaudio Imbrenda {
4395c0573ba5SClaudio Imbrenda 	if (!mutex_trylock(&kvm->lock))
4396c0573ba5SClaudio Imbrenda 		return 0;
4397c0573ba5SClaudio Imbrenda 	__kvm_s390_set_tod_clock(kvm, gtod);
4398c0573ba5SClaudio Imbrenda 	mutex_unlock(&kvm->lock);
4399c0573ba5SClaudio Imbrenda 	return 1;
4400c0573ba5SClaudio Imbrenda }
4401c0573ba5SClaudio Imbrenda 
4402fa576c58SThomas Huth /**
4403fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
4404fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
4405fa576c58SThomas Huth  * @gpa: Guest physical address
4406fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
4407fa576c58SThomas Huth  *
4408fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
4409fa576c58SThomas Huth  *
4410fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
4411fa576c58SThomas Huth  */
4412fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
441324eb3a82SDominik Dingel {
4414527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
4415527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
441624eb3a82SDominik Dingel }
441724eb3a82SDominik Dingel 
44183c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
44193c038e6bSDominik Dingel 				      unsigned long token)
44203c038e6bSDominik Dingel {
44213c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
4422383d0b05SJens Freimann 	struct kvm_s390_irq irq;
44233c038e6bSDominik Dingel 
44243c038e6bSDominik Dingel 	if (start_token) {
4425383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
4426383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
4427383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
44283c038e6bSDominik Dingel 	} else {
44293c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
4430383d0b05SJens Freimann 		inti.parm64 = token;
44313c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
44323c038e6bSDominik Dingel 	}
44333c038e6bSDominik Dingel }
44343c038e6bSDominik Dingel 
44352a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
44363c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
44373c038e6bSDominik Dingel {
44383c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
44393c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
44402a18b7e7SVitaly Kuznetsov 
44412a18b7e7SVitaly Kuznetsov 	return true;
44423c038e6bSDominik Dingel }
44433c038e6bSDominik Dingel 
44443c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
44453c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
44463c038e6bSDominik Dingel {
44473c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
44483c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
44493c038e6bSDominik Dingel }
44503c038e6bSDominik Dingel 
44513c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
44523c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
44533c038e6bSDominik Dingel {
44543c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
44553c038e6bSDominik Dingel }
44563c038e6bSDominik Dingel 
44577c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
44583c038e6bSDominik Dingel {
44593c038e6bSDominik Dingel 	/*
44603c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
44613c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
44623c038e6bSDominik Dingel 	 */
44633c038e6bSDominik Dingel 	return true;
44643c038e6bSDominik Dingel }
44653c038e6bSDominik Dingel 
4466e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
44673c038e6bSDominik Dingel {
44683c038e6bSDominik Dingel 	hva_t hva;
44693c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
44703c038e6bSDominik Dingel 
44713c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4472e8c22266SVitaly Kuznetsov 		return false;
44733c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
44743c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
4475e8c22266SVitaly Kuznetsov 		return false;
44763c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
4477e8c22266SVitaly Kuznetsov 		return false;
44789a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
4479e8c22266SVitaly Kuznetsov 		return false;
4480b9224cd7SDavid Hildenbrand 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
4481e8c22266SVitaly Kuznetsov 		return false;
44823c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
4483e8c22266SVitaly Kuznetsov 		return false;
44843c038e6bSDominik Dingel 
448581480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
448681480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
448781480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
4488e8c22266SVitaly Kuznetsov 		return false;
44893c038e6bSDominik Dingel 
4490e8c22266SVitaly Kuznetsov 	return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
44913c038e6bSDominik Dingel }
44923c038e6bSDominik Dingel 
44933fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
4494b0c632dbSHeiko Carstens {
44953fb4c40fSThomas Huth 	int rc, cpuflags;
4496e168bf8dSCarsten Otte 
44973c038e6bSDominik Dingel 	/*
44983c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
44993c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
45003c038e6bSDominik Dingel 	 * handled outside the worker.
45013c038e6bSDominik Dingel 	 */
45023c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
45033c038e6bSDominik Dingel 
45047ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
45057ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
4506b0c632dbSHeiko Carstens 
4507b0c632dbSHeiko Carstens 	if (need_resched())
4508b0c632dbSHeiko Carstens 		schedule();
4509b0c632dbSHeiko Carstens 
451079395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
451179395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
451279395031SJens Freimann 		if (rc)
451379395031SJens Freimann 			return rc;
451479395031SJens Freimann 	}
45150ff31867SCarsten Otte 
45162c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
45172c70fe44SChristian Borntraeger 	if (rc)
45182c70fe44SChristian Borntraeger 		return rc;
45192c70fe44SChristian Borntraeger 
452027291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
452127291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
452227291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
452327291e21SDavid Hildenbrand 	}
452427291e21SDavid Hildenbrand 
45254eeef242SSean Christopherson 	clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
45269f30f621SMichael Mueller 
4527b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
45283fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
45293fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
45303fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
45312b29a9fdSDominik Dingel 
45323fb4c40fSThomas Huth 	return 0;
45333fb4c40fSThomas Huth }
45343fb4c40fSThomas Huth 
4535492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
4536492d8642SThomas Huth {
453756317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
453856317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
453956317920SDavid Hildenbrand 	};
454056317920SDavid Hildenbrand 	u8 opcode, ilen;
4541492d8642SThomas Huth 	int rc;
4542492d8642SThomas Huth 
4543492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4544492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
4545492d8642SThomas Huth 
4546492d8642SThomas Huth 	/*
4547492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
4548492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
4549492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
4550492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
4551492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
4552492d8642SThomas Huth 	 * to be able to forward the PSW.
4553492d8642SThomas Huth 	 */
45543fa8cad7SDavid Hildenbrand 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
455556317920SDavid Hildenbrand 	ilen = insn_length(opcode);
45569b0d721aSDavid Hildenbrand 	if (rc < 0) {
45579b0d721aSDavid Hildenbrand 		return rc;
45589b0d721aSDavid Hildenbrand 	} else if (rc) {
45599b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
45609b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
45619b0d721aSDavid Hildenbrand 		 * nullification if necessary.
45629b0d721aSDavid Hildenbrand 		 */
45639b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
45649b0d721aSDavid Hildenbrand 		ilen = 4;
45659b0d721aSDavid Hildenbrand 	}
456656317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
456756317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
456856317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
4569492d8642SThomas Huth }
4570492d8642SThomas Huth 
45713fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
45723fb4c40fSThomas Huth {
45734d62fcc0SQingFeng Hao 	struct mcck_volatile_info *mcck_info;
45744d62fcc0SQingFeng Hao 	struct sie_page *sie_page;
45754d62fcc0SQingFeng Hao 
45762b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
45772b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
45782b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
45792b29a9fdSDominik Dingel 
458027291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
458127291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
458227291e21SDavid Hildenbrand 
45837ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
45847ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
458571f116bfSDavid Hildenbrand 
45864d62fcc0SQingFeng Hao 	if (exit_reason == -EINTR) {
45874d62fcc0SQingFeng Hao 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
45884d62fcc0SQingFeng Hao 		sie_page = container_of(vcpu->arch.sie_block,
45894d62fcc0SQingFeng Hao 					struct sie_page, sie_block);
45904d62fcc0SQingFeng Hao 		mcck_info = &sie_page->mcck_info;
45914d62fcc0SQingFeng Hao 		kvm_s390_reinject_machine_check(vcpu, mcck_info);
45924d62fcc0SQingFeng Hao 		return 0;
45934d62fcc0SQingFeng Hao 	}
45944d62fcc0SQingFeng Hao 
459571f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
459671f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
459771f116bfSDavid Hildenbrand 
459871f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
459971f116bfSDavid Hildenbrand 			return rc;
460071f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
460171f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
460271f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
460371f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
460471f116bfSDavid Hildenbrand 		return -EREMOTE;
460571f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
460671f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
460771f116bfSDavid Hildenbrand 		return 0;
4608210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
4609210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
4610210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
4611210b1607SThomas Huth 						current->thread.gmap_addr;
4612210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
461371f116bfSDavid Hildenbrand 		return -EREMOTE;
461424eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
46153c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
461624eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
461771f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
461871f116bfSDavid Hildenbrand 			return 0;
461950a05be4SChristian Borntraeger 		vcpu->stat.pfault_sync++;
462071f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
4621fa576c58SThomas Huth 	}
462271f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
46233fb4c40fSThomas Huth }
46243fb4c40fSThomas Huth 
46253adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK)
46263fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
46273fb4c40fSThomas Huth {
46283fb4c40fSThomas Huth 	int rc, exit_reason;
4629c8aac234SJanosch Frank 	struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block;
46303fb4c40fSThomas Huth 
4631800c1065SThomas Huth 	/*
4632800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
4633800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
4634800c1065SThomas Huth 	 */
46352031f287SSean Christopherson 	kvm_vcpu_srcu_read_lock(vcpu);
4636800c1065SThomas Huth 
4637a76ccff6SThomas Huth 	do {
46383fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
46393fb4c40fSThomas Huth 		if (rc)
4640a76ccff6SThomas Huth 			break;
46413fb4c40fSThomas Huth 
46422031f287SSean Christopherson 		kvm_vcpu_srcu_read_unlock(vcpu);
46433fb4c40fSThomas Huth 		/*
4644a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
4645a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
46463fb4c40fSThomas Huth 		 */
46470097d12eSChristian Borntraeger 		local_irq_disable();
46486edaa530SPaolo Bonzini 		guest_enter_irqoff();
4649db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
46500097d12eSChristian Borntraeger 		local_irq_enable();
4651c8aac234SJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4652c8aac234SJanosch Frank 			memcpy(sie_page->pv_grregs,
4653c8aac234SJanosch Frank 			       vcpu->run->s.regs.gprs,
4654c8aac234SJanosch Frank 			       sizeof(sie_page->pv_grregs));
4655c8aac234SJanosch Frank 		}
465656e62a73SSven Schnelle 		if (test_cpu_flag(CIF_FPU))
465756e62a73SSven Schnelle 			load_fpu_regs();
4658a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
4659a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
4660c8aac234SJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4661c8aac234SJanosch Frank 			memcpy(vcpu->run->s.regs.gprs,
4662c8aac234SJanosch Frank 			       sie_page->pv_grregs,
4663c8aac234SJanosch Frank 			       sizeof(sie_page->pv_grregs));
46643adae0b4SJanosch Frank 			/*
46653adae0b4SJanosch Frank 			 * We're not allowed to inject interrupts on intercepts
46663adae0b4SJanosch Frank 			 * that leave the guest state in an "in-between" state
46673adae0b4SJanosch Frank 			 * where the next SIE entry will do a continuation.
46683adae0b4SJanosch Frank 			 * Fence interrupts in our "internal" PSW.
46693adae0b4SJanosch Frank 			 */
46703adae0b4SJanosch Frank 			if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR ||
46713adae0b4SJanosch Frank 			    vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) {
46723adae0b4SJanosch Frank 				vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
46733adae0b4SJanosch Frank 			}
4674c8aac234SJanosch Frank 		}
46750097d12eSChristian Borntraeger 		local_irq_disable();
4676db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
46776edaa530SPaolo Bonzini 		guest_exit_irqoff();
46780097d12eSChristian Borntraeger 		local_irq_enable();
46792031f287SSean Christopherson 		kvm_vcpu_srcu_read_lock(vcpu);
46803fb4c40fSThomas Huth 
46813fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
468227291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
46833fb4c40fSThomas Huth 
46842031f287SSean Christopherson 	kvm_vcpu_srcu_read_unlock(vcpu);
4685e168bf8dSCarsten Otte 	return rc;
4686b0c632dbSHeiko Carstens }
4687b0c632dbSHeiko Carstens 
46882f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu)
4689b028ee3eSDavid Hildenbrand {
46902f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
46914d5f2c04SChristian Borntraeger 	struct runtime_instr_cb *riccb;
46924e0b1ab7SFan Zhang 	struct gs_cb *gscb;
46934d5f2c04SChristian Borntraeger 
46944d5f2c04SChristian Borntraeger 	riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
46954e0b1ab7SFan Zhang 	gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
4696b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
4697b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
4698b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4699b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
4700b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
4701b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
4702b028ee3eSDavid Hildenbrand 	}
4703b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
4704b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
4705b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
4706b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
47079fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
47089fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
4709b028ee3eSDavid Hildenbrand 	}
471023a60f83SCollin Walling 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) {
471123a60f83SCollin Walling 		vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318;
471223a60f83SCollin Walling 		vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc;
47133fd8417fSCollin Walling 		VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc);
471423a60f83SCollin Walling 	}
471580cd8763SFan Zhang 	/*
471680cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
471780cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
471880cd8763SFan Zhang 	 */
471980cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
47204d5f2c04SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 64) &&
4721bb59c2daSAlice Frosi 	    riccb->v &&
47220c9d8683SDavid Hildenbrand 	    !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
47234d5f2c04SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
47240c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
472580cd8763SFan Zhang 	}
47264e0b1ab7SFan Zhang 	/*
47274e0b1ab7SFan Zhang 	 * If userspace sets the gscb (e.g. after migration) to non-zero,
47284e0b1ab7SFan Zhang 	 * we should enable GS here instead of doing the lazy enablement.
47294e0b1ab7SFan Zhang 	 */
47304e0b1ab7SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
47314e0b1ab7SFan Zhang 	    test_kvm_facility(vcpu->kvm, 133) &&
47324e0b1ab7SFan Zhang 	    gscb->gssm &&
47334e0b1ab7SFan Zhang 	    !vcpu->arch.gs_enabled) {
47344e0b1ab7SFan Zhang 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
47354e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecb |= ECB_GS;
47364e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
47374e0b1ab7SFan Zhang 		vcpu->arch.gs_enabled = 1;
473880cd8763SFan Zhang 	}
473935b3fde6SChristian Borntraeger 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
474035b3fde6SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 82)) {
474135b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
474235b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
474335b3fde6SChristian Borntraeger 	}
47444e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
47454e0b1ab7SFan Zhang 		preempt_disable();
47464e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
47474e0b1ab7SFan Zhang 		if (current->thread.gs_cb) {
47484e0b1ab7SFan Zhang 			vcpu->arch.host_gscb = current->thread.gs_cb;
47494e0b1ab7SFan Zhang 			save_gs_cb(vcpu->arch.host_gscb);
47504e0b1ab7SFan Zhang 		}
47514e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled) {
47524e0b1ab7SFan Zhang 			current->thread.gs_cb = (struct gs_cb *)
47534e0b1ab7SFan Zhang 						&vcpu->run->s.regs.gscb;
47544e0b1ab7SFan Zhang 			restore_gs_cb(current->thread.gs_cb);
47554e0b1ab7SFan Zhang 		}
47564e0b1ab7SFan Zhang 		preempt_enable();
47574e0b1ab7SFan Zhang 	}
4758a3da7b4aSChristian Borntraeger 	/* SIE will load etoken directly from SDNX and therefore kvm_run */
4759811ea797SJanosch Frank }
4760811ea797SJanosch Frank 
47612f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu)
4762811ea797SJanosch Frank {
47632f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
47642f0a83beSTianjia Zhang 
4765811ea797SJanosch Frank 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
4766811ea797SJanosch Frank 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
4767811ea797SJanosch Frank 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
4768811ea797SJanosch Frank 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
4769811ea797SJanosch Frank 		/* some control register changes require a tlb flush */
4770811ea797SJanosch Frank 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4771811ea797SJanosch Frank 	}
4772811ea797SJanosch Frank 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4773811ea797SJanosch Frank 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
4774811ea797SJanosch Frank 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
4775811ea797SJanosch Frank 	}
4776811ea797SJanosch Frank 	save_access_regs(vcpu->arch.host_acrs);
4777811ea797SJanosch Frank 	restore_access_regs(vcpu->run->s.regs.acrs);
4778811ea797SJanosch Frank 	/* save host (userspace) fprs/vrs */
4779811ea797SJanosch Frank 	save_fpu_regs();
4780811ea797SJanosch Frank 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
4781811ea797SJanosch Frank 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
4782811ea797SJanosch Frank 	if (MACHINE_HAS_VX)
4783811ea797SJanosch Frank 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
4784811ea797SJanosch Frank 	else
4785811ea797SJanosch Frank 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
4786811ea797SJanosch Frank 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
4787811ea797SJanosch Frank 	if (test_fp_ctl(current->thread.fpu.fpc))
4788811ea797SJanosch Frank 		/* User space provided an invalid FPC, let's clear it */
4789811ea797SJanosch Frank 		current->thread.fpu.fpc = 0;
4790811ea797SJanosch Frank 
4791811ea797SJanosch Frank 	/* Sync fmt2 only data */
4792811ea797SJanosch Frank 	if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) {
47932f0a83beSTianjia Zhang 		sync_regs_fmt2(vcpu);
4794811ea797SJanosch Frank 	} else {
4795811ea797SJanosch Frank 		/*
4796811ea797SJanosch Frank 		 * In several places we have to modify our internal view to
4797811ea797SJanosch Frank 		 * not do things that are disallowed by the ultravisor. For
4798811ea797SJanosch Frank 		 * example we must not inject interrupts after specific exits
4799811ea797SJanosch Frank 		 * (e.g. 112 prefix page not secure). We do this by turning
4800811ea797SJanosch Frank 		 * off the machine check, external and I/O interrupt bits
4801811ea797SJanosch Frank 		 * of our PSW copy. To avoid getting validity intercepts, we
4802811ea797SJanosch Frank 		 * do only accept the condition code from userspace.
4803811ea797SJanosch Frank 		 */
4804811ea797SJanosch Frank 		vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC;
4805811ea797SJanosch Frank 		vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask &
4806811ea797SJanosch Frank 						   PSW_MASK_CC;
4807811ea797SJanosch Frank 	}
480880cd8763SFan Zhang 
4809b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
4810b028ee3eSDavid Hildenbrand }
4811b028ee3eSDavid Hildenbrand 
48122f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu)
4813b028ee3eSDavid Hildenbrand {
48142f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
48152f0a83beSTianjia Zhang 
4816b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
4817b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
4818b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
481935b3fde6SChristian Borntraeger 	kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
482023a60f83SCollin Walling 	kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val;
48214e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
482244bada28SHeiko Carstens 		preempt_disable();
48234e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
48244e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled)
48254e0b1ab7SFan Zhang 			save_gs_cb(current->thread.gs_cb);
48264e0b1ab7SFan Zhang 		current->thread.gs_cb = vcpu->arch.host_gscb;
48274e0b1ab7SFan Zhang 		restore_gs_cb(vcpu->arch.host_gscb);
48284e0b1ab7SFan Zhang 		if (!vcpu->arch.host_gscb)
48294e0b1ab7SFan Zhang 			__ctl_clear_bit(2, 4);
48304e0b1ab7SFan Zhang 		vcpu->arch.host_gscb = NULL;
483144bada28SHeiko Carstens 		preempt_enable();
48324e0b1ab7SFan Zhang 	}
4833a3da7b4aSChristian Borntraeger 	/* SIE will save etoken directly into SDNX and therefore kvm_run */
4834b028ee3eSDavid Hildenbrand }
4835b028ee3eSDavid Hildenbrand 
48362f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu)
4837811ea797SJanosch Frank {
48382f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
48392f0a83beSTianjia Zhang 
4840811ea797SJanosch Frank 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
4841811ea797SJanosch Frank 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
4842811ea797SJanosch Frank 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
4843811ea797SJanosch Frank 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
4844811ea797SJanosch Frank 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
4845811ea797SJanosch Frank 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
4846811ea797SJanosch Frank 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
4847811ea797SJanosch Frank 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
4848811ea797SJanosch Frank 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
4849811ea797SJanosch Frank 	save_access_regs(vcpu->run->s.regs.acrs);
4850811ea797SJanosch Frank 	restore_access_regs(vcpu->arch.host_acrs);
4851811ea797SJanosch Frank 	/* Save guest register state */
4852811ea797SJanosch Frank 	save_fpu_regs();
4853811ea797SJanosch Frank 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4854811ea797SJanosch Frank 	/* Restore will be done lazily at return */
4855811ea797SJanosch Frank 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
4856811ea797SJanosch Frank 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
4857811ea797SJanosch Frank 	if (likely(!kvm_s390_pv_cpu_is_protected(vcpu)))
48582f0a83beSTianjia Zhang 		store_regs_fmt2(vcpu);
4859811ea797SJanosch Frank }
4860811ea797SJanosch Frank 
48611b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
4862b0c632dbSHeiko Carstens {
48631b94f6f8STianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
48648f2abe6aSChristian Borntraeger 	int rc;
4865b0c632dbSHeiko Carstens 
48660460eb35SJanosch Frank 	/*
48670460eb35SJanosch Frank 	 * Running a VM while dumping always has the potential to
48680460eb35SJanosch Frank 	 * produce inconsistent dump data. But for PV vcpus a SIE
48690460eb35SJanosch Frank 	 * entry while dumping could also lead to a fatal validity
48700460eb35SJanosch Frank 	 * intercept which we absolutely want to avoid.
48710460eb35SJanosch Frank 	 */
48720460eb35SJanosch Frank 	if (vcpu->kvm->arch.pv.dumping)
48730460eb35SJanosch Frank 		return -EINVAL;
48740460eb35SJanosch Frank 
4875460df4c1SPaolo Bonzini 	if (kvm_run->immediate_exit)
4876460df4c1SPaolo Bonzini 		return -EINTR;
4877460df4c1SPaolo Bonzini 
4878200824f5SThomas Huth 	if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
4879200824f5SThomas Huth 	    kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
4880200824f5SThomas Huth 		return -EINVAL;
4881200824f5SThomas Huth 
4882accb757dSChristoffer Dall 	vcpu_load(vcpu);
4883accb757dSChristoffer Dall 
488427291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
488527291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
4886accb757dSChristoffer Dall 		rc = 0;
4887accb757dSChristoffer Dall 		goto out;
488827291e21SDavid Hildenbrand 	}
488927291e21SDavid Hildenbrand 
489020b7035cSJan H. Schönherr 	kvm_sigset_activate(vcpu);
4891b0c632dbSHeiko Carstens 
4892fe28c786SJanosch Frank 	/*
4893fe28c786SJanosch Frank 	 * no need to check the return value of vcpu_start as it can only have
4894fe28c786SJanosch Frank 	 * an error for protvirt, but protvirt means user cpu state
4895fe28c786SJanosch Frank 	 */
48966352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
48976852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
48986352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
4899ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
49006352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
4901accb757dSChristoffer Dall 		rc = -EINVAL;
4902accb757dSChristoffer Dall 		goto out;
49036352e4d2SDavid Hildenbrand 	}
4904b0c632dbSHeiko Carstens 
49052f0a83beSTianjia Zhang 	sync_regs(vcpu);
4906db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
4907d7b0b5ebSCarsten Otte 
4908dab4079dSHeiko Carstens 	might_fault();
4909e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
49109ace903dSChristian Ehrhardt 
4911b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
4912b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
49138f2abe6aSChristian Borntraeger 		rc = -EINTR;
4914b1d16c49SChristian Ehrhardt 	}
49158f2abe6aSChristian Borntraeger 
491627291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
491727291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
491827291e21SDavid Hildenbrand 		rc = 0;
491927291e21SDavid Hildenbrand 	}
492027291e21SDavid Hildenbrand 
49218f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
492271f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
49238f2abe6aSChristian Borntraeger 		rc = 0;
49248f2abe6aSChristian Borntraeger 	}
49258f2abe6aSChristian Borntraeger 
4926db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
49272f0a83beSTianjia Zhang 	store_regs(vcpu);
4928d7b0b5ebSCarsten Otte 
492920b7035cSJan H. Schönherr 	kvm_sigset_deactivate(vcpu);
4930b0c632dbSHeiko Carstens 
4931b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
4932accb757dSChristoffer Dall out:
4933accb757dSChristoffer Dall 	vcpu_put(vcpu);
49347e8e6ab4SHeiko Carstens 	return rc;
4935b0c632dbSHeiko Carstens }
4936b0c632dbSHeiko Carstens 
4937b0c632dbSHeiko Carstens /*
4938b0c632dbSHeiko Carstens  * store status at address
4939b0c632dbSHeiko Carstens  * we use have two special cases:
4940b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
4941b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
4942b0c632dbSHeiko Carstens  */
4943d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
4944b0c632dbSHeiko Carstens {
4945092670cdSCarsten Otte 	unsigned char archmode = 1;
49469abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
4947fda902cbSMichael Mueller 	unsigned int px;
49484287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
4949d0bce605SHeiko Carstens 	int rc;
4950b0c632dbSHeiko Carstens 
4951d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
4952d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
4953d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
4954b0c632dbSHeiko Carstens 			return -EFAULT;
4955d9a3a09aSMartin Schwidefsky 		gpa = 0;
4956d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
4957d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
4958b0c632dbSHeiko Carstens 			return -EFAULT;
4959d9a3a09aSMartin Schwidefsky 		gpa = px;
4960d9a3a09aSMartin Schwidefsky 	} else
4961d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
49629abc2a08SDavid Hildenbrand 
49639abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
49649abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
49659522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
4966d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
49679abc2a08SDavid Hildenbrand 				     fprs, 128);
49689abc2a08SDavid Hildenbrand 	} else {
49699abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
49706fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
49719abc2a08SDavid Hildenbrand 	}
4972d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
4973d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
4974d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
4975d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
4976d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
4977fda902cbSMichael Mueller 			      &px, 4);
4978d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
49799abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
4980d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
4981d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
49824287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
4983d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
49844287f247SDavid Hildenbrand 			      &cputm, 8);
4985178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
4986d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
4987d0bce605SHeiko Carstens 			      &clkcomp, 8);
4988d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
4989d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
4990d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
4991d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
4992d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
4993b0c632dbSHeiko Carstens }
4994b0c632dbSHeiko Carstens 
4995e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
4996e879892cSThomas Huth {
4997e879892cSThomas Huth 	/*
4998e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
499931d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
5000e879892cSThomas Huth 	 * it into the save area
5001e879892cSThomas Huth 	 */
5002d0164ee2SHendrik Brueckner 	save_fpu_regs();
50039abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
5004e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
5005e879892cSThomas Huth 
5006e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
5007e879892cSThomas Huth }
5008e879892cSThomas Huth 
50098ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
50108ad35755SDavid Hildenbrand {
50118ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
50128e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
50138ad35755SDavid Hildenbrand }
50148ad35755SDavid Hildenbrand 
50158ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
50168ad35755SDavid Hildenbrand {
501746808a4cSMarc Zyngier 	unsigned long i;
50188ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
50198ad35755SDavid Hildenbrand 
50208ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
50218ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
50228ad35755SDavid Hildenbrand 	}
50238ad35755SDavid Hildenbrand }
50248ad35755SDavid Hildenbrand 
50258ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
50268ad35755SDavid Hildenbrand {
502709a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
502809a400e7SDavid Hildenbrand 		return;
50298ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
50308e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
50318ad35755SDavid Hildenbrand }
50328ad35755SDavid Hildenbrand 
5033fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
50346852d7b6SDavid Hildenbrand {
5035fe28c786SJanosch Frank 	int i, online_vcpus, r = 0, started_vcpus = 0;
50368ad35755SDavid Hildenbrand 
50378ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
5038fe28c786SJanosch Frank 		return 0;
50398ad35755SDavid Hildenbrand 
50406852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
50418ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
5042433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
50438ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
50448ad35755SDavid Hildenbrand 
5045fe28c786SJanosch Frank 	/* Let's tell the UV that we want to change into the operating state */
5046fe28c786SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5047fe28c786SJanosch Frank 		r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR);
5048fe28c786SJanosch Frank 		if (r) {
5049fe28c786SJanosch Frank 			spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5050fe28c786SJanosch Frank 			return r;
5051fe28c786SJanosch Frank 		}
5052fe28c786SJanosch Frank 	}
5053fe28c786SJanosch Frank 
50548ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
5055113d10bcSMarc Zyngier 		if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i)))
50568ad35755SDavid Hildenbrand 			started_vcpus++;
50578ad35755SDavid Hildenbrand 	}
50588ad35755SDavid Hildenbrand 
50598ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
50608ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
50618ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
50628ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
50638ad35755SDavid Hildenbrand 		/*
50648ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
50658ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
506638860756SBhaskar Chowdhury 		 * outstanding ENABLE requests.
50678ad35755SDavid Hildenbrand 		 */
50688ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
50698ad35755SDavid Hildenbrand 	}
50708ad35755SDavid Hildenbrand 
50719daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
50728ad35755SDavid Hildenbrand 	/*
507372f21820SChristian Borntraeger 	 * The real PSW might have changed due to a RESTART interpreted by the
507472f21820SChristian Borntraeger 	 * ultravisor. We block all interrupts and let the next sie exit
507572f21820SChristian Borntraeger 	 * refresh our view.
507672f21820SChristian Borntraeger 	 */
507772f21820SChristian Borntraeger 	if (kvm_s390_pv_cpu_is_protected(vcpu))
507872f21820SChristian Borntraeger 		vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
507972f21820SChristian Borntraeger 	/*
50808ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
50818ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
50828ad35755SDavid Hildenbrand 	 */
5083d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
5084433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5085fe28c786SJanosch Frank 	return 0;
50866852d7b6SDavid Hildenbrand }
50876852d7b6SDavid Hildenbrand 
5088fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
50896852d7b6SDavid Hildenbrand {
5090fe28c786SJanosch Frank 	int i, online_vcpus, r = 0, started_vcpus = 0;
50918ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
50928ad35755SDavid Hildenbrand 
50938ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
5094fe28c786SJanosch Frank 		return 0;
50958ad35755SDavid Hildenbrand 
50966852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
50978ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
5098433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
50998ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
51008ad35755SDavid Hildenbrand 
5101fe28c786SJanosch Frank 	/* Let's tell the UV that we want to change into the stopped state */
5102fe28c786SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5103fe28c786SJanosch Frank 		r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP);
5104fe28c786SJanosch Frank 		if (r) {
5105fe28c786SJanosch Frank 			spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5106fe28c786SJanosch Frank 			return r;
5107fe28c786SJanosch Frank 		}
5108fe28c786SJanosch Frank 	}
5109fe28c786SJanosch Frank 
5110812de046SEric Farman 	/*
5111812de046SEric Farman 	 * Set the VCPU to STOPPED and THEN clear the interrupt flag,
5112812de046SEric Farman 	 * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders
5113812de046SEric Farman 	 * have been fully processed. This will ensure that the VCPU
5114812de046SEric Farman 	 * is kept BUSY if another VCPU is inquiring with SIGP SENSE.
5115812de046SEric Farman 	 */
5116812de046SEric Farman 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
51176cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
511832f5ff63SDavid Hildenbrand 
51198ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
51208ad35755SDavid Hildenbrand 
51218ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
5122113d10bcSMarc Zyngier 		struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i);
5123113d10bcSMarc Zyngier 
5124113d10bcSMarc Zyngier 		if (!is_vcpu_stopped(tmp)) {
51258ad35755SDavid Hildenbrand 			started_vcpus++;
5126113d10bcSMarc Zyngier 			started_vcpu = tmp;
51278ad35755SDavid Hildenbrand 		}
51288ad35755SDavid Hildenbrand 	}
51298ad35755SDavid Hildenbrand 
51308ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
51318ad35755SDavid Hildenbrand 		/*
51328ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
51338ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
51348ad35755SDavid Hildenbrand 		 */
51358ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
51368ad35755SDavid Hildenbrand 	}
51378ad35755SDavid Hildenbrand 
5138433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5139fe28c786SJanosch Frank 	return 0;
51406852d7b6SDavid Hildenbrand }
51416852d7b6SDavid Hildenbrand 
5142d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
5143d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
5144d6712df9SCornelia Huck {
5145d6712df9SCornelia Huck 	int r;
5146d6712df9SCornelia Huck 
5147d6712df9SCornelia Huck 	if (cap->flags)
5148d6712df9SCornelia Huck 		return -EINVAL;
5149d6712df9SCornelia Huck 
5150d6712df9SCornelia Huck 	switch (cap->cap) {
5151fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
5152fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
5153fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
5154c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
5155fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
5156fa6b7fe9SCornelia Huck 		}
5157fa6b7fe9SCornelia Huck 		r = 0;
5158fa6b7fe9SCornelia Huck 		break;
5159d6712df9SCornelia Huck 	default:
5160d6712df9SCornelia Huck 		r = -EINVAL;
5161d6712df9SCornelia Huck 		break;
5162d6712df9SCornelia Huck 	}
5163d6712df9SCornelia Huck 	return r;
5164d6712df9SCornelia Huck }
5165d6712df9SCornelia Huck 
51660e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu,
516719e12277SJanosch Frank 				  struct kvm_s390_mem_op *mop)
516819e12277SJanosch Frank {
516919e12277SJanosch Frank 	void __user *uaddr = (void __user *)mop->buf;
517019e12277SJanosch Frank 	int r = 0;
517119e12277SJanosch Frank 
517219e12277SJanosch Frank 	if (mop->flags || !mop->size)
517319e12277SJanosch Frank 		return -EINVAL;
517419e12277SJanosch Frank 	if (mop->size + mop->sida_offset < mop->size)
517519e12277SJanosch Frank 		return -EINVAL;
517619e12277SJanosch Frank 	if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block))
517719e12277SJanosch Frank 		return -E2BIG;
51782c212e1bSJanis Schoetterl-Glausch 	if (!kvm_s390_pv_cpu_is_protected(vcpu))
51792c212e1bSJanis Schoetterl-Glausch 		return -EINVAL;
518019e12277SJanosch Frank 
518119e12277SJanosch Frank 	switch (mop->op) {
518219e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_READ:
518319e12277SJanosch Frank 		if (copy_to_user(uaddr, (void *)(sida_origin(vcpu->arch.sie_block) +
518419e12277SJanosch Frank 				 mop->sida_offset), mop->size))
518519e12277SJanosch Frank 			r = -EFAULT;
518619e12277SJanosch Frank 
518719e12277SJanosch Frank 		break;
518819e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_WRITE:
518919e12277SJanosch Frank 		if (copy_from_user((void *)(sida_origin(vcpu->arch.sie_block) +
519019e12277SJanosch Frank 				   mop->sida_offset), uaddr, mop->size))
519119e12277SJanosch Frank 			r = -EFAULT;
519219e12277SJanosch Frank 		break;
519319e12277SJanosch Frank 	}
519419e12277SJanosch Frank 	return r;
519519e12277SJanosch Frank }
51960e1234c0SJanis Schoetterl-Glausch 
51970e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu,
519841408c28SThomas Huth 				 struct kvm_s390_mem_op *mop)
519941408c28SThomas Huth {
520041408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
520141408c28SThomas Huth 	void *tmpbuf = NULL;
520219e12277SJanosch Frank 	int r = 0;
520341408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
5204e9e9feebSJanis Schoetterl-Glausch 				    | KVM_S390_MEMOP_F_CHECK_ONLY
5205e9e9feebSJanis Schoetterl-Glausch 				    | KVM_S390_MEMOP_F_SKEY_PROTECTION;
520641408c28SThomas Huth 
5207a13b03bbSThomas Huth 	if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size)
520841408c28SThomas Huth 		return -EINVAL;
520941408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
521041408c28SThomas Huth 		return -E2BIG;
521119e12277SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu))
521219e12277SJanosch Frank 		return -EINVAL;
5213e9e9feebSJanis Schoetterl-Glausch 	if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
5214e9e9feebSJanis Schoetterl-Glausch 		if (access_key_invalid(mop->key))
5215e9e9feebSJanis Schoetterl-Glausch 			return -EINVAL;
5216e9e9feebSJanis Schoetterl-Glausch 	} else {
5217e9e9feebSJanis Schoetterl-Glausch 		mop->key = 0;
5218e9e9feebSJanis Schoetterl-Glausch 	}
521941408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
522041408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
522141408c28SThomas Huth 		if (!tmpbuf)
522241408c28SThomas Huth 			return -ENOMEM;
522341408c28SThomas Huth 	}
522441408c28SThomas Huth 
522541408c28SThomas Huth 	switch (mop->op) {
522641408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
522741408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
5228e9e9feebSJanis Schoetterl-Glausch 			r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size,
5229e9e9feebSJanis Schoetterl-Glausch 					    GACC_FETCH, mop->key);
523041408c28SThomas Huth 			break;
523141408c28SThomas Huth 		}
5232e9e9feebSJanis Schoetterl-Glausch 		r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
5233e9e9feebSJanis Schoetterl-Glausch 					mop->size, mop->key);
523441408c28SThomas Huth 		if (r == 0) {
523541408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
523641408c28SThomas Huth 				r = -EFAULT;
523741408c28SThomas Huth 		}
523841408c28SThomas Huth 		break;
523941408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
524041408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
5241e9e9feebSJanis Schoetterl-Glausch 			r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size,
5242e9e9feebSJanis Schoetterl-Glausch 					    GACC_STORE, mop->key);
524341408c28SThomas Huth 			break;
524441408c28SThomas Huth 		}
524541408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
524641408c28SThomas Huth 			r = -EFAULT;
524741408c28SThomas Huth 			break;
524841408c28SThomas Huth 		}
5249e9e9feebSJanis Schoetterl-Glausch 		r = write_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
5250e9e9feebSJanis Schoetterl-Glausch 					 mop->size, mop->key);
525141408c28SThomas Huth 		break;
525241408c28SThomas Huth 	}
525341408c28SThomas Huth 
525441408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
525541408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
525641408c28SThomas Huth 
525741408c28SThomas Huth 	vfree(tmpbuf);
525841408c28SThomas Huth 	return r;
525941408c28SThomas Huth }
526041408c28SThomas Huth 
52610e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu,
526219e12277SJanosch Frank 				     struct kvm_s390_mem_op *mop)
526319e12277SJanosch Frank {
526419e12277SJanosch Frank 	int r, srcu_idx;
526519e12277SJanosch Frank 
526619e12277SJanosch Frank 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
526719e12277SJanosch Frank 
526819e12277SJanosch Frank 	switch (mop->op) {
526919e12277SJanosch Frank 	case KVM_S390_MEMOP_LOGICAL_READ:
527019e12277SJanosch Frank 	case KVM_S390_MEMOP_LOGICAL_WRITE:
52710e1234c0SJanis Schoetterl-Glausch 		r = kvm_s390_vcpu_mem_op(vcpu, mop);
527219e12277SJanosch Frank 		break;
527319e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_READ:
527419e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_WRITE:
527519e12277SJanosch Frank 		/* we are locked against sida going away by the vcpu->mutex */
52760e1234c0SJanis Schoetterl-Glausch 		r = kvm_s390_vcpu_sida_op(vcpu, mop);
527719e12277SJanosch Frank 		break;
527819e12277SJanosch Frank 	default:
527919e12277SJanosch Frank 		r = -EINVAL;
528019e12277SJanosch Frank 	}
528119e12277SJanosch Frank 
528219e12277SJanosch Frank 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
528319e12277SJanosch Frank 	return r;
528419e12277SJanosch Frank }
528519e12277SJanosch Frank 
52865cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp,
5287b0c632dbSHeiko Carstens 			       unsigned int ioctl, unsigned long arg)
5288b0c632dbSHeiko Carstens {
5289b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
5290b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
5291b0c632dbSHeiko Carstens 
529293736624SAvi Kivity 	switch (ioctl) {
529347b43c52SJens Freimann 	case KVM_S390_IRQ: {
529447b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
529547b43c52SJens Freimann 
529647b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
52979b062471SChristoffer Dall 			return -EFAULT;
52989b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
529947b43c52SJens Freimann 	}
530093736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
5301ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
530253936b5bSThomas Huth 		struct kvm_s390_irq s390irq = {};
5303ba5c1e9bSCarsten Otte 
5304ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
53059b062471SChristoffer Dall 			return -EFAULT;
5306383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
5307383d0b05SJens Freimann 			return -EINVAL;
53089b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
5309ba5c1e9bSCarsten Otte 	}
53109b062471SChristoffer Dall 	}
53115cb0944cSPaolo Bonzini 	return -ENOIOCTLCMD;
53125cb0944cSPaolo Bonzini }
53135cb0944cSPaolo Bonzini 
53148aba0958SJanosch Frank static int kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu *vcpu,
53158aba0958SJanosch Frank 					struct kvm_pv_cmd *cmd)
53168aba0958SJanosch Frank {
53178aba0958SJanosch Frank 	struct kvm_s390_pv_dmp dmp;
53188aba0958SJanosch Frank 	void *data;
53198aba0958SJanosch Frank 	int ret;
53208aba0958SJanosch Frank 
53218aba0958SJanosch Frank 	/* Dump initialization is a prerequisite */
53228aba0958SJanosch Frank 	if (!vcpu->kvm->arch.pv.dumping)
53238aba0958SJanosch Frank 		return -EINVAL;
53248aba0958SJanosch Frank 
53258aba0958SJanosch Frank 	if (copy_from_user(&dmp, (__u8 __user *)cmd->data, sizeof(dmp)))
53268aba0958SJanosch Frank 		return -EFAULT;
53278aba0958SJanosch Frank 
53288aba0958SJanosch Frank 	/* We only handle this subcmd right now */
53298aba0958SJanosch Frank 	if (dmp.subcmd != KVM_PV_DUMP_CPU)
53308aba0958SJanosch Frank 		return -EINVAL;
53318aba0958SJanosch Frank 
53328aba0958SJanosch Frank 	/* CPU dump length is the same as create cpu storage donation. */
53338aba0958SJanosch Frank 	if (dmp.buff_len != uv_info.guest_cpu_stor_len)
53348aba0958SJanosch Frank 		return -EINVAL;
53358aba0958SJanosch Frank 
53368aba0958SJanosch Frank 	data = kvzalloc(uv_info.guest_cpu_stor_len, GFP_KERNEL);
53378aba0958SJanosch Frank 	if (!data)
53388aba0958SJanosch Frank 		return -ENOMEM;
53398aba0958SJanosch Frank 
53408aba0958SJanosch Frank 	ret = kvm_s390_pv_dump_cpu(vcpu, data, &cmd->rc, &cmd->rrc);
53418aba0958SJanosch Frank 
53428aba0958SJanosch Frank 	VCPU_EVENT(vcpu, 3, "PROTVIRT DUMP CPU %d rc %x rrc %x",
53438aba0958SJanosch Frank 		   vcpu->vcpu_id, cmd->rc, cmd->rrc);
53448aba0958SJanosch Frank 
53458aba0958SJanosch Frank 	if (ret)
53468aba0958SJanosch Frank 		ret = -EINVAL;
53478aba0958SJanosch Frank 
53488aba0958SJanosch Frank 	/* On success copy over the dump data */
53498aba0958SJanosch Frank 	if (!ret && copy_to_user((__u8 __user *)dmp.buff_addr, data, uv_info.guest_cpu_stor_len))
53508aba0958SJanosch Frank 		ret = -EFAULT;
53518aba0958SJanosch Frank 
53528aba0958SJanosch Frank 	kvfree(data);
53538aba0958SJanosch Frank 	return ret;
53548aba0958SJanosch Frank }
53558aba0958SJanosch Frank 
53565cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp,
53575cb0944cSPaolo Bonzini 			 unsigned int ioctl, unsigned long arg)
53585cb0944cSPaolo Bonzini {
53595cb0944cSPaolo Bonzini 	struct kvm_vcpu *vcpu = filp->private_data;
53605cb0944cSPaolo Bonzini 	void __user *argp = (void __user *)arg;
53615cb0944cSPaolo Bonzini 	int idx;
53625cb0944cSPaolo Bonzini 	long r;
53638a8378faSJanosch Frank 	u16 rc, rrc;
53649b062471SChristoffer Dall 
53659b062471SChristoffer Dall 	vcpu_load(vcpu);
53669b062471SChristoffer Dall 
53679b062471SChristoffer Dall 	switch (ioctl) {
5368b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
5369800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
537055680890SChristian Borntraeger 		r = kvm_s390_store_status_unloaded(vcpu, arg);
5371800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
5372bc923cc9SAvi Kivity 		break;
5373b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
5374b0c632dbSHeiko Carstens 		psw_t psw;
5375b0c632dbSHeiko Carstens 
5376bc923cc9SAvi Kivity 		r = -EFAULT;
5377b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
5378bc923cc9SAvi Kivity 			break;
5379bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
5380bc923cc9SAvi Kivity 		break;
5381b0c632dbSHeiko Carstens 	}
53827de3f142SJanosch Frank 	case KVM_S390_CLEAR_RESET:
53837de3f142SJanosch Frank 		r = 0;
53847de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_clear_reset(vcpu);
53858a8378faSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
53868a8378faSJanosch Frank 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
53878a8378faSJanosch Frank 					  UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc);
53888a8378faSJanosch Frank 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x",
53898a8378faSJanosch Frank 				   rc, rrc);
53908a8378faSJanosch Frank 		}
53917de3f142SJanosch Frank 		break;
5392b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
53937de3f142SJanosch Frank 		r = 0;
53947de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_initial_reset(vcpu);
53958a8378faSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
53968a8378faSJanosch Frank 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
53978a8378faSJanosch Frank 					  UVC_CMD_CPU_RESET_INITIAL,
53988a8378faSJanosch Frank 					  &rc, &rrc);
53998a8378faSJanosch Frank 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x",
54008a8378faSJanosch Frank 				   rc, rrc);
54018a8378faSJanosch Frank 		}
54027de3f142SJanosch Frank 		break;
54037de3f142SJanosch Frank 	case KVM_S390_NORMAL_RESET:
54047de3f142SJanosch Frank 		r = 0;
54057de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_normal_reset(vcpu);
54068a8378faSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
54078a8378faSJanosch Frank 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
54088a8378faSJanosch Frank 					  UVC_CMD_CPU_RESET, &rc, &rrc);
54098a8378faSJanosch Frank 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x",
54108a8378faSJanosch Frank 				   rc, rrc);
54118a8378faSJanosch Frank 		}
5412bc923cc9SAvi Kivity 		break;
541314eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
541414eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
541514eebd91SCarsten Otte 		struct kvm_one_reg reg;
541668cf7b1fSJanosch Frank 		r = -EINVAL;
541768cf7b1fSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu))
541868cf7b1fSJanosch Frank 			break;
541914eebd91SCarsten Otte 		r = -EFAULT;
542014eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
542114eebd91SCarsten Otte 			break;
542214eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
542314eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
542414eebd91SCarsten Otte 		else
542514eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
542614eebd91SCarsten Otte 		break;
542714eebd91SCarsten Otte 	}
542827e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
542927e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
543027e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
543127e0393fSCarsten Otte 
543227e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
543327e0393fSCarsten Otte 			r = -EFAULT;
543427e0393fSCarsten Otte 			break;
543527e0393fSCarsten Otte 		}
543627e0393fSCarsten Otte 
543727e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
543827e0393fSCarsten Otte 			r = -EINVAL;
543927e0393fSCarsten Otte 			break;
544027e0393fSCarsten Otte 		}
544127e0393fSCarsten Otte 
544227e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
544327e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
544427e0393fSCarsten Otte 		break;
544527e0393fSCarsten Otte 	}
544627e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
544727e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
544827e0393fSCarsten Otte 
544927e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
545027e0393fSCarsten Otte 			r = -EFAULT;
545127e0393fSCarsten Otte 			break;
545227e0393fSCarsten Otte 		}
545327e0393fSCarsten Otte 
545427e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
545527e0393fSCarsten Otte 			r = -EINVAL;
545627e0393fSCarsten Otte 			break;
545727e0393fSCarsten Otte 		}
545827e0393fSCarsten Otte 
545927e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
546027e0393fSCarsten Otte 			ucasmap.length);
546127e0393fSCarsten Otte 		break;
546227e0393fSCarsten Otte 	}
546327e0393fSCarsten Otte #endif
5464ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
5465527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
5466ccc7910fSCarsten Otte 		break;
5467ccc7910fSCarsten Otte 	}
5468d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
5469d6712df9SCornelia Huck 	{
5470d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
5471d6712df9SCornelia Huck 		r = -EFAULT;
5472d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
5473d6712df9SCornelia Huck 			break;
5474d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
5475d6712df9SCornelia Huck 		break;
5476d6712df9SCornelia Huck 	}
547741408c28SThomas Huth 	case KVM_S390_MEM_OP: {
547841408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
547941408c28SThomas Huth 
548041408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
54810e1234c0SJanis Schoetterl-Glausch 			r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op);
548241408c28SThomas Huth 		else
548341408c28SThomas Huth 			r = -EFAULT;
548441408c28SThomas Huth 		break;
548541408c28SThomas Huth 	}
5486816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
5487816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
5488816c7667SJens Freimann 
5489816c7667SJens Freimann 		r = -EFAULT;
5490816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
5491816c7667SJens Freimann 			break;
5492816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
5493816c7667SJens Freimann 		    irq_state.len == 0 ||
5494816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
5495816c7667SJens Freimann 			r = -EINVAL;
5496816c7667SJens Freimann 			break;
5497816c7667SJens Freimann 		}
5498bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
5499816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
5500816c7667SJens Freimann 					   (void __user *) irq_state.buf,
5501816c7667SJens Freimann 					   irq_state.len);
5502816c7667SJens Freimann 		break;
5503816c7667SJens Freimann 	}
5504816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
5505816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
5506816c7667SJens Freimann 
5507816c7667SJens Freimann 		r = -EFAULT;
5508816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
5509816c7667SJens Freimann 			break;
5510816c7667SJens Freimann 		if (irq_state.len == 0) {
5511816c7667SJens Freimann 			r = -EINVAL;
5512816c7667SJens Freimann 			break;
5513816c7667SJens Freimann 		}
5514bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
5515816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
5516816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
5517816c7667SJens Freimann 					   irq_state.len);
5518816c7667SJens Freimann 		break;
5519816c7667SJens Freimann 	}
55208aba0958SJanosch Frank 	case KVM_S390_PV_CPU_COMMAND: {
55218aba0958SJanosch Frank 		struct kvm_pv_cmd cmd;
55228aba0958SJanosch Frank 
55238aba0958SJanosch Frank 		r = -EINVAL;
55248aba0958SJanosch Frank 		if (!is_prot_virt_host())
55258aba0958SJanosch Frank 			break;
55268aba0958SJanosch Frank 
55278aba0958SJanosch Frank 		r = -EFAULT;
55288aba0958SJanosch Frank 		if (copy_from_user(&cmd, argp, sizeof(cmd)))
55298aba0958SJanosch Frank 			break;
55308aba0958SJanosch Frank 
55318aba0958SJanosch Frank 		r = -EINVAL;
55328aba0958SJanosch Frank 		if (cmd.flags)
55338aba0958SJanosch Frank 			break;
55348aba0958SJanosch Frank 
55358aba0958SJanosch Frank 		/* We only handle this cmd right now */
55368aba0958SJanosch Frank 		if (cmd.cmd != KVM_PV_DUMP)
55378aba0958SJanosch Frank 			break;
55388aba0958SJanosch Frank 
55398aba0958SJanosch Frank 		r = kvm_s390_handle_pv_vcpu_dump(vcpu, &cmd);
55408aba0958SJanosch Frank 
55418aba0958SJanosch Frank 		/* Always copy over UV rc / rrc data */
55428aba0958SJanosch Frank 		if (copy_to_user((__u8 __user *)argp, &cmd.rc,
55438aba0958SJanosch Frank 				 sizeof(cmd.rc) + sizeof(cmd.rrc)))
55448aba0958SJanosch Frank 			r = -EFAULT;
55458aba0958SJanosch Frank 		break;
55468aba0958SJanosch Frank 	}
5547b0c632dbSHeiko Carstens 	default:
55483e6afcf1SCarsten Otte 		r = -ENOTTY;
5549b0c632dbSHeiko Carstens 	}
55509b062471SChristoffer Dall 
55519b062471SChristoffer Dall 	vcpu_put(vcpu);
5552bc923cc9SAvi Kivity 	return r;
5553b0c632dbSHeiko Carstens }
5554b0c632dbSHeiko Carstens 
55551499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
55565b1c1493SCarsten Otte {
55575b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
55585b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
55595b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
55605b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
55615b1c1493SCarsten Otte 		get_page(vmf->page);
55625b1c1493SCarsten Otte 		return 0;
55635b1c1493SCarsten Otte 	}
55645b1c1493SCarsten Otte #endif
55655b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
55665b1c1493SCarsten Otte }
55675b1c1493SCarsten Otte 
5568b0c632dbSHeiko Carstens /* Section: memory related */
5569f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
5570537a17b3SSean Christopherson 				   const struct kvm_memory_slot *old,
5571537a17b3SSean Christopherson 				   struct kvm_memory_slot *new,
55727b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
5573b0c632dbSHeiko Carstens {
5574ec5c8697SSean Christopherson 	gpa_t size;
5575ec5c8697SSean Christopherson 
5576ec5c8697SSean Christopherson 	/* When we are protected, we should not change the memory slots */
5577ec5c8697SSean Christopherson 	if (kvm_s390_pv_get_handle(kvm))
5578ec5c8697SSean Christopherson 		return -EINVAL;
5579ec5c8697SSean Christopherson 
5580ec5c8697SSean Christopherson 	if (change == KVM_MR_DELETE || change == KVM_MR_FLAGS_ONLY)
5581ec5c8697SSean Christopherson 		return 0;
5582cf5b4869SSean Christopherson 
5583dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
5584dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
5585dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
5586dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
5587b0c632dbSHeiko Carstens 
5588cf5b4869SSean Christopherson 	if (new->userspace_addr & 0xffffful)
5589b0c632dbSHeiko Carstens 		return -EINVAL;
5590b0c632dbSHeiko Carstens 
5591ec5c8697SSean Christopherson 	size = new->npages * PAGE_SIZE;
5592cf5b4869SSean Christopherson 	if (size & 0xffffful)
5593b0c632dbSHeiko Carstens 		return -EINVAL;
5594b0c632dbSHeiko Carstens 
5595cf5b4869SSean Christopherson 	if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit)
5596a3a92c31SDominik Dingel 		return -EINVAL;
5597a3a92c31SDominik Dingel 
5598f7784b8eSMarcelo Tosatti 	return 0;
5599f7784b8eSMarcelo Tosatti }
5600f7784b8eSMarcelo Tosatti 
5601f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
56029d4c197cSSean Christopherson 				struct kvm_memory_slot *old,
5603f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
56048482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
5605f7784b8eSMarcelo Tosatti {
560619ec166cSChristian Borntraeger 	int rc = 0;
5607f7784b8eSMarcelo Tosatti 
560819ec166cSChristian Borntraeger 	switch (change) {
560919ec166cSChristian Borntraeger 	case KVM_MR_DELETE:
561019ec166cSChristian Borntraeger 		rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
561119ec166cSChristian Borntraeger 					old->npages * PAGE_SIZE);
561219ec166cSChristian Borntraeger 		break;
561319ec166cSChristian Borntraeger 	case KVM_MR_MOVE:
561419ec166cSChristian Borntraeger 		rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
561519ec166cSChristian Borntraeger 					old->npages * PAGE_SIZE);
561619ec166cSChristian Borntraeger 		if (rc)
561719ec166cSChristian Borntraeger 			break;
56183b684a42SJoe Perches 		fallthrough;
561919ec166cSChristian Borntraeger 	case KVM_MR_CREATE:
5620cf5b4869SSean Christopherson 		rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr,
5621cf5b4869SSean Christopherson 				      new->base_gfn * PAGE_SIZE,
5622cf5b4869SSean Christopherson 				      new->npages * PAGE_SIZE);
562319ec166cSChristian Borntraeger 		break;
562419ec166cSChristian Borntraeger 	case KVM_MR_FLAGS_ONLY:
562519ec166cSChristian Borntraeger 		break;
562619ec166cSChristian Borntraeger 	default:
562719ec166cSChristian Borntraeger 		WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
562819ec166cSChristian Borntraeger 	}
5629598841caSCarsten Otte 	if (rc)
5630ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
5631598841caSCarsten Otte 	return;
5632b0c632dbSHeiko Carstens }
5633b0c632dbSHeiko Carstens 
563460a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
563560a37709SAlexander Yarygin {
563660a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
563760a37709SAlexander Yarygin 
563860a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
563960a37709SAlexander Yarygin }
564060a37709SAlexander Yarygin 
5641b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
5642b0c632dbSHeiko Carstens {
564360a37709SAlexander Yarygin 	int i;
564460a37709SAlexander Yarygin 
564507197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
56468d43d570SMichael Mueller 		pr_info("SIE is not available\n");
564707197fd0SDavid Hildenbrand 		return -ENODEV;
564807197fd0SDavid Hildenbrand 	}
564907197fd0SDavid Hildenbrand 
5650a4499382SJanosch Frank 	if (nested && hpage) {
56518d43d570SMichael Mueller 		pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
5652a4499382SJanosch Frank 		return -EINVAL;
5653a4499382SJanosch Frank 	}
5654a4499382SJanosch Frank 
565560a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
5656c3b9e3e1SChristian Borntraeger 		kvm_s390_fac_base[i] |=
565717e89e13SSven Schnelle 			stfle_fac_list[i] & nonhyp_mask(i);
565860a37709SAlexander Yarygin 
56599d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
5660b0c632dbSHeiko Carstens }
5661b0c632dbSHeiko Carstens 
5662b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
5663b0c632dbSHeiko Carstens {
5664b0c632dbSHeiko Carstens 	kvm_exit();
5665b0c632dbSHeiko Carstens }
5666b0c632dbSHeiko Carstens 
5667b0c632dbSHeiko Carstens module_init(kvm_s390_init);
5668b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
5669566af940SCornelia Huck 
5670566af940SCornelia Huck /*
5671566af940SCornelia Huck  * Enable autoloading of the kvm module.
5672566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
5673566af940SCornelia Huck  * since x86 takes a different approach.
5674566af940SCornelia Huck  */
5675566af940SCornelia Huck #include <linux/miscdevice.h>
5676566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
5677566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
5678