xref: /linux/arch/s390/kvm/kvm-s390.c (revision 29b40f105ec8d555984c1f72dc9133b122e51903)
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>
9b0c632dbSHeiko Carstens  *               Heiko Carstens <heiko.carstens@de.ibm.com>
10628eb9b8SChristian Ehrhardt  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
1115f36ebdSJason J. Herne  *               Jason J. Herne <jjherne@us.ibm.com>
12b0c632dbSHeiko Carstens  */
13b0c632dbSHeiko Carstens 
147aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390"
157aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
167aedd9d4SMichael Mueller 
17b0c632dbSHeiko Carstens #include <linux/compiler.h>
18b0c632dbSHeiko Carstens #include <linux/err.h>
19b0c632dbSHeiko Carstens #include <linux/fs.h>
20ca872302SChristian Borntraeger #include <linux/hrtimer.h>
21b0c632dbSHeiko Carstens #include <linux/init.h>
22b0c632dbSHeiko Carstens #include <linux/kvm.h>
23b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
24b2d73b2aSMartin Schwidefsky #include <linux/mman.h>
25b0c632dbSHeiko Carstens #include <linux/module.h>
26d3217967SPaul Gortmaker #include <linux/moduleparam.h>
27a374e892STony Krowiak #include <linux/random.h>
28b0c632dbSHeiko Carstens #include <linux/slab.h>
29ba5c1e9bSCarsten Otte #include <linux/timer.h>
3041408c28SThomas Huth #include <linux/vmalloc.h>
3115c9705fSDavid Hildenbrand #include <linux/bitmap.h>
32174cd4b1SIngo Molnar #include <linux/sched/signal.h>
33190df4a2SClaudio Imbrenda #include <linux/string.h>
34174cd4b1SIngo Molnar 
35cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
36b0c632dbSHeiko Carstens #include <asm/lowcore.h>
37fd5ada04SMartin Schwidefsky #include <asm/stp.h>
38b0c632dbSHeiko Carstens #include <asm/pgtable.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>
47*29b40f10SJanosch Frank #include <asm/uv.h>
488f2abe6aSChristian Borntraeger #include "kvm-s390.h"
49b0c632dbSHeiko Carstens #include "gaccess.h"
50b0c632dbSHeiko Carstens 
515786fffaSCornelia Huck #define CREATE_TRACE_POINTS
525786fffaSCornelia Huck #include "trace.h"
53ade38c31SCornelia Huck #include "trace-s390.h"
545786fffaSCornelia Huck 
5541408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
56816c7667SJens Freimann #define LOCAL_IRQS 32
57816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
58816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
5941408c28SThomas Huth 
60b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
61ccc40c53SChristian Borntraeger #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
62b0c632dbSHeiko Carstens 
63b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
64b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
650eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
668f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
678f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
688f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
69a5e0aceaSChristian Borntraeger 	{ "exit_io_request", VCPU_STAT(exit_io_request) },
708f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
71ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
729ec6de19SAlexander Yarygin 	{ "exit_pei", VCPU_STAT(exit_pei) },
73ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
74ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
75a011eeb2SJanosch Frank 	{ "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
76f7819512SPaolo Bonzini 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
7762bea5bfSPaolo Bonzini 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
783491caf2SChristian Borntraeger 	{ "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
798b905d28SChristian Borntraeger 	{ "halt_no_poll_steal", VCPU_STAT(halt_no_poll_steal) },
80ce2e4f0bSDavid Hildenbrand 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
81f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
82ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
83aba07508SDavid Hildenbrand 	{ "instruction_stctl", VCPU_STAT(instruction_stctl) },
84aba07508SDavid Hildenbrand 	{ "instruction_stctg", VCPU_STAT(instruction_stctg) },
85ccc40c53SChristian Borntraeger 	{ "deliver_ckc", VCPU_STAT(deliver_ckc) },
86ccc40c53SChristian Borntraeger 	{ "deliver_cputm", VCPU_STAT(deliver_cputm) },
87ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
887697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
89ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
90ccc40c53SChristian Borntraeger 	{ "deliver_virtio", VCPU_STAT(deliver_virtio) },
91ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
92ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
93ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
94ccc40c53SChristian Borntraeger 	{ "deliver_program", VCPU_STAT(deliver_program) },
95ccc40c53SChristian Borntraeger 	{ "deliver_io", VCPU_STAT(deliver_io) },
9632de0749SQingFeng Hao 	{ "deliver_machine_check", VCPU_STAT(deliver_machine_check) },
97ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
98ccc40c53SChristian Borntraeger 	{ "inject_ckc", VCPU_STAT(inject_ckc) },
99ccc40c53SChristian Borntraeger 	{ "inject_cputm", VCPU_STAT(inject_cputm) },
100ccc40c53SChristian Borntraeger 	{ "inject_external_call", VCPU_STAT(inject_external_call) },
101ccc40c53SChristian Borntraeger 	{ "inject_float_mchk", VM_STAT(inject_float_mchk) },
102ccc40c53SChristian Borntraeger 	{ "inject_emergency_signal", VCPU_STAT(inject_emergency_signal) },
103ccc40c53SChristian Borntraeger 	{ "inject_io", VM_STAT(inject_io) },
104ccc40c53SChristian Borntraeger 	{ "inject_mchk", VCPU_STAT(inject_mchk) },
105ccc40c53SChristian Borntraeger 	{ "inject_pfault_done", VM_STAT(inject_pfault_done) },
106ccc40c53SChristian Borntraeger 	{ "inject_program", VCPU_STAT(inject_program) },
107ccc40c53SChristian Borntraeger 	{ "inject_restart", VCPU_STAT(inject_restart) },
108ccc40c53SChristian Borntraeger 	{ "inject_service_signal", VM_STAT(inject_service_signal) },
109ccc40c53SChristian Borntraeger 	{ "inject_set_prefix", VCPU_STAT(inject_set_prefix) },
110ccc40c53SChristian Borntraeger 	{ "inject_stop_signal", VCPU_STAT(inject_stop_signal) },
111ccc40c53SChristian Borntraeger 	{ "inject_pfault_init", VCPU_STAT(inject_pfault_init) },
112ccc40c53SChristian Borntraeger 	{ "inject_virtio", VM_STAT(inject_virtio) },
113a37cb07aSChristian Borntraeger 	{ "instruction_epsw", VCPU_STAT(instruction_epsw) },
114a37cb07aSChristian Borntraeger 	{ "instruction_gs", VCPU_STAT(instruction_gs) },
115a37cb07aSChristian Borntraeger 	{ "instruction_io_other", VCPU_STAT(instruction_io_other) },
116a37cb07aSChristian Borntraeger 	{ "instruction_lpsw", VCPU_STAT(instruction_lpsw) },
117a37cb07aSChristian Borntraeger 	{ "instruction_lpswe", VCPU_STAT(instruction_lpswe) },
11869d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
119a37cb07aSChristian Borntraeger 	{ "instruction_ptff", VCPU_STAT(instruction_ptff) },
120453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
121a37cb07aSChristian Borntraeger 	{ "instruction_sck", VCPU_STAT(instruction_sck) },
122a37cb07aSChristian Borntraeger 	{ "instruction_sckpf", VCPU_STAT(instruction_sckpf) },
123453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
124453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
125453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
126a37cb07aSChristian Borntraeger 	{ "instruction_iske", VCPU_STAT(instruction_iske) },
127a37cb07aSChristian Borntraeger 	{ "instruction_ri", VCPU_STAT(instruction_ri) },
128a37cb07aSChristian Borntraeger 	{ "instruction_rrbe", VCPU_STAT(instruction_rrbe) },
129a37cb07aSChristian Borntraeger 	{ "instruction_sske", VCPU_STAT(instruction_sske) },
1308a242234SHeiko Carstens 	{ "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
131b31288faSKonstantin Weitz 	{ "instruction_essa", VCPU_STAT(instruction_essa) },
132453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
133453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
134a37cb07aSChristian Borntraeger 	{ "instruction_tb", VCPU_STAT(instruction_tb) },
135a37cb07aSChristian Borntraeger 	{ "instruction_tpi", VCPU_STAT(instruction_tpi) },
136bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
137a37cb07aSChristian Borntraeger 	{ "instruction_tsch", VCPU_STAT(instruction_tsch) },
13895ca2cb5SJanosch Frank 	{ "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
139a3508fbeSDavid Hildenbrand 	{ "instruction_sie", VCPU_STAT(instruction_sie) },
1405288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
141bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
1427697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
1435288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
14442cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
14542cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
1465288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
14742cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
14842cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
149cd7b4b61SEric Farman 	{ "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
1505288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
1515288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
1525288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
15342cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
15442cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
15542cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
156866c138cSChristian Borntraeger 	{ "instruction_diag_10", VCPU_STAT(diagnose_10) },
157866c138cSChristian Borntraeger 	{ "instruction_diag_44", VCPU_STAT(diagnose_44) },
158866c138cSChristian Borntraeger 	{ "instruction_diag_9c", VCPU_STAT(diagnose_9c) },
1598474e5caSChristian Borntraeger 	{ "diag_9c_ignored", VCPU_STAT(diagnose_9c_ignored) },
160866c138cSChristian Borntraeger 	{ "instruction_diag_258", VCPU_STAT(diagnose_258) },
161866c138cSChristian Borntraeger 	{ "instruction_diag_308", VCPU_STAT(diagnose_308) },
162866c138cSChristian Borntraeger 	{ "instruction_diag_500", VCPU_STAT(diagnose_500) },
163a37cb07aSChristian Borntraeger 	{ "instruction_diag_other", VCPU_STAT(diagnose_other) },
164b0c632dbSHeiko Carstens 	{ NULL }
165b0c632dbSHeiko Carstens };
166b0c632dbSHeiko Carstens 
1678fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext {
1688fa1696eSCollin L. Walling 	__u8 epoch_idx;
1698fa1696eSCollin L. Walling 	__u64 tod;
1708fa1696eSCollin L. Walling 	__u8 reserved[7];
1718fa1696eSCollin L. Walling } __packed;
1728fa1696eSCollin L. Walling 
173a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
174a411edf1SDavid Hildenbrand static int nested;
175a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
176a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
177a411edf1SDavid Hildenbrand 
178a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */
179a4499382SJanosch Frank static int hpage;
180a4499382SJanosch Frank module_param(hpage, int, 0444);
181a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support");
182b0c632dbSHeiko Carstens 
1838b905d28SChristian Borntraeger /* maximum percentage of steal time for polling.  >100 is treated like 100 */
1848b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10;
1858b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644);
186b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
1878b905d28SChristian Borntraeger 
188c3b9e3e1SChristian Borntraeger /*
189c3b9e3e1SChristian Borntraeger  * For now we handle at most 16 double words as this is what the s390 base
190c3b9e3e1SChristian Borntraeger  * kernel handles and stores in the prefix page. If we ever need to go beyond
191c3b9e3e1SChristian Borntraeger  * this, this requires changes to code, but the external uapi can stay.
192c3b9e3e1SChristian Borntraeger  */
193c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16
194c3b9e3e1SChristian Borntraeger 
195c3b9e3e1SChristian Borntraeger /*
196c3b9e3e1SChristian Borntraeger  * Base feature mask that defines default mask for facilities. Consists of the
197c3b9e3e1SChristian Borntraeger  * defines in FACILITIES_KVM and the non-hypervisor managed bits.
198c3b9e3e1SChristian Borntraeger  */
199c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
200c3b9e3e1SChristian Borntraeger /*
201c3b9e3e1SChristian Borntraeger  * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
202c3b9e3e1SChristian Borntraeger  * and defines the facilities that can be enabled via a cpu model.
203c3b9e3e1SChristian Borntraeger  */
204c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
205c3b9e3e1SChristian Borntraeger 
206c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void)
20778c4b59fSMichael Mueller {
208c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
209c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
210c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
211c3b9e3e1SChristian Borntraeger 		sizeof(S390_lowcore.stfle_fac_list));
212c3b9e3e1SChristian Borntraeger 
213c3b9e3e1SChristian Borntraeger 	return SIZE_INTERNAL;
21478c4b59fSMichael Mueller }
21578c4b59fSMichael Mueller 
21615c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
21715c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
2180a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
2190a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
22015c9705fSDavid Hildenbrand 
2219d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
222a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
22378f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
2243e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv;
2259d8d5786SMichael Mueller 
226b0c632dbSHeiko Carstens /* Section: not file related */
22713a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
228b0c632dbSHeiko Carstens {
229b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
23010474ae8SAlexander Graf 	return 0;
231b0c632dbSHeiko Carstens }
232b0c632dbSHeiko Carstens 
233f257d6dcSSean Christopherson int kvm_arch_check_processor_compat(void)
234f257d6dcSSean Christopherson {
235f257d6dcSSean Christopherson 	return 0;
236f257d6dcSSean Christopherson }
237f257d6dcSSean Christopherson 
238*29b40f10SJanosch Frank /* forward declarations */
239414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
240414d3b07SMartin Schwidefsky 			      unsigned long end);
241*29b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm);
2422c70fe44SChristian Borntraeger 
2431575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
2441575767eSDavid Hildenbrand {
2451575767eSDavid Hildenbrand 	u8 delta_idx = 0;
2461575767eSDavid Hildenbrand 
2471575767eSDavid Hildenbrand 	/*
2481575767eSDavid Hildenbrand 	 * The TOD jumps by delta, we have to compensate this by adding
2491575767eSDavid Hildenbrand 	 * -delta to the epoch.
2501575767eSDavid Hildenbrand 	 */
2511575767eSDavid Hildenbrand 	delta = -delta;
2521575767eSDavid Hildenbrand 
2531575767eSDavid Hildenbrand 	/* sign-extension - we're adding to signed values below */
2541575767eSDavid Hildenbrand 	if ((s64)delta < 0)
2551575767eSDavid Hildenbrand 		delta_idx = -1;
2561575767eSDavid Hildenbrand 
2571575767eSDavid Hildenbrand 	scb->epoch += delta;
2581575767eSDavid Hildenbrand 	if (scb->ecd & ECD_MEF) {
2591575767eSDavid Hildenbrand 		scb->epdx += delta_idx;
2601575767eSDavid Hildenbrand 		if (scb->epoch < delta)
2611575767eSDavid Hildenbrand 			scb->epdx += 1;
2621575767eSDavid Hildenbrand 	}
2631575767eSDavid Hildenbrand }
2641575767eSDavid Hildenbrand 
265fdf03650SFan Zhang /*
266fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
267fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
268fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
269fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
270fdf03650SFan Zhang  */
271fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
272fdf03650SFan Zhang 			  void *v)
273fdf03650SFan Zhang {
274fdf03650SFan Zhang 	struct kvm *kvm;
275fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
276fdf03650SFan Zhang 	int i;
277fdf03650SFan Zhang 	unsigned long long *delta = v;
278fdf03650SFan Zhang 
279fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
280fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
2811575767eSDavid Hildenbrand 			kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
2821575767eSDavid Hildenbrand 			if (i == 0) {
2831575767eSDavid Hildenbrand 				kvm->arch.epoch = vcpu->arch.sie_block->epoch;
2841575767eSDavid Hildenbrand 				kvm->arch.epdx = vcpu->arch.sie_block->epdx;
2851575767eSDavid Hildenbrand 			}
286db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
287db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
28891473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
2891575767eSDavid Hildenbrand 				kvm_clock_sync_scb(vcpu->arch.vsie_block,
2901575767eSDavid Hildenbrand 						   *delta);
291fdf03650SFan Zhang 		}
292fdf03650SFan Zhang 	}
293fdf03650SFan Zhang 	return NOTIFY_OK;
294fdf03650SFan Zhang }
295fdf03650SFan Zhang 
296fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
297fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
298fdf03650SFan Zhang };
299fdf03650SFan Zhang 
300b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
301b0c632dbSHeiko Carstens {
3022c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
303b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
304a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
305a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
306fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
307fdf03650SFan Zhang 				       &kvm_clock_notifier);
308b0c632dbSHeiko Carstens 	return 0;
309b0c632dbSHeiko Carstens }
310b0c632dbSHeiko Carstens 
311b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
312b0c632dbSHeiko Carstens {
313b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
314a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
315fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
316fdf03650SFan Zhang 					 &kvm_clock_notifier);
317b0c632dbSHeiko Carstens }
318b0c632dbSHeiko Carstens 
31922be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
32022be5a13SDavid Hildenbrand {
32122be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
32222be5a13SDavid Hildenbrand }
32322be5a13SDavid Hildenbrand 
3240a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
3250a763c78SDavid Hildenbrand {
3260a763c78SDavid Hildenbrand 	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
327d051ae53SHeiko Carstens 	int cc;
3280a763c78SDavid Hildenbrand 
3290a763c78SDavid Hildenbrand 	asm volatile(
3300a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
3310a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
3320a763c78SDavid Hildenbrand 		"	ipm	%0\n"
3330a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
3340a763c78SDavid Hildenbrand 		: "=d" (cc)
3350a763c78SDavid Hildenbrand 		: "d" (r0)
3360a763c78SDavid Hildenbrand 		: "cc");
3370a763c78SDavid Hildenbrand 	return cc == 0;
3380a763c78SDavid Hildenbrand }
3390a763c78SDavid Hildenbrand 
340d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
341d6681397SChristian Borntraeger {
342d6681397SChristian Borntraeger 	register unsigned long r0 asm("0") = 0;	/* query function */
343d6681397SChristian Borntraeger 	register unsigned long r1 asm("1") = (unsigned long) query;
344d6681397SChristian Borntraeger 
345d6681397SChristian Borntraeger 	asm volatile(
346d6681397SChristian Borntraeger 		/* Parameter regs are ignored */
347d6681397SChristian Borntraeger 		"	.insn	rrf,%[opc] << 16,2,4,6,0\n"
348b1c41ac3SHeiko Carstens 		:
349d6681397SChristian Borntraeger 		: "d" (r0), "a" (r1), [opc] "i" (opcode)
350b1c41ac3SHeiko Carstens 		: "cc", "memory");
351d6681397SChristian Borntraeger }
352d6681397SChristian Borntraeger 
353173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938
3544f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939
355173aec2dSChristian Borntraeger 
35622be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
35722be5a13SDavid Hildenbrand {
3580a763c78SDavid Hildenbrand 	int i;
3590a763c78SDavid Hildenbrand 
3600a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
3610a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
3620a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
3630a763c78SDavid Hildenbrand 	}
3640a763c78SDavid Hildenbrand 
3650a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
366221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
367221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
368221bb8a4SLinus Torvalds 		     PTFF_QAF);
3690a763c78SDavid Hildenbrand 
3700a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
37169c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
37269c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
37369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
37469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
37569c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
37669c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
37769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
37869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
37969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
38069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
3810a763c78SDavid Hildenbrand 	}
3820a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
38369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
38469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
3850a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
38669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
38769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
38869c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
38969c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
39069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
39169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
39269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
39369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
3940a763c78SDavid Hildenbrand 	}
3950a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
396985a9d20SHarald Freudenberger 		__cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
39769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
3980a763c78SDavid Hildenbrand 
399e000b8e0SJason J. Herne 	if (test_facility(146)) /* MSA8 */
400e000b8e0SJason J. Herne 		__cpacf_query(CPACF_KMA, (cpacf_mask_t *)
401e000b8e0SJason J. Herne 			      kvm_s390_available_subfunc.kma);
402e000b8e0SJason J. Herne 
40313209ad0SChristian Borntraeger 	if (test_facility(155)) /* MSA9 */
40413209ad0SChristian Borntraeger 		__cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
40513209ad0SChristian Borntraeger 			      kvm_s390_available_subfunc.kdsa);
40613209ad0SChristian Borntraeger 
407173aec2dSChristian Borntraeger 	if (test_facility(150)) /* SORTL */
408173aec2dSChristian Borntraeger 		__insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl);
409173aec2dSChristian Borntraeger 
4104f45b90eSChristian Borntraeger 	if (test_facility(151)) /* DFLTCC */
4114f45b90eSChristian Borntraeger 		__insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
4124f45b90eSChristian Borntraeger 
41322be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
41422be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
415a3508fbeSDavid Hildenbrand 	/*
416a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
417a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
418a3508fbeSDavid Hildenbrand 	 */
419a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
420a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
421a3508fbeSDavid Hildenbrand 		return;
422a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
42319c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
42419c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
4250615a326SDavid Hildenbrand 	if (sclp.has_siif)
4260615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
42777d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
42877d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
429a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
430a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
4315630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
4325630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
43313ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
43413ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
4357fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
4367fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
437730cd632SFarhan Ali 	if (sclp.has_kss)
438730cd632SFarhan Ali 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
4395d3876a8SDavid Hildenbrand 	/*
4405d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
4415d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
4425d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
4435d3876a8SDavid Hildenbrand 	 *
4445d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
4455d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
4465d3876a8SDavid Hildenbrand 	 *
4475d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
4485d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
4495d3876a8SDavid Hildenbrand 	 *
4505d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
4515d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
4525d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
4535d3876a8SDavid Hildenbrand 	 *
4545d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
4555d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
4565d3876a8SDavid Hildenbrand 	 */
45722be5a13SDavid Hildenbrand }
45822be5a13SDavid Hildenbrand 
459b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
460b0c632dbSHeiko Carstens {
461f76f6371SJanosch Frank 	int rc = -ENOMEM;
462308c3e66SMichael Mueller 
46378f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
46478f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
46578f26131SChristian Borntraeger 		return -ENOMEM;
46678f26131SChristian Borntraeger 
4673e6c5568SJanosch Frank 	kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
4683e6c5568SJanosch Frank 	if (!kvm_s390_dbf_uv)
4693e6c5568SJanosch Frank 		goto out;
4703e6c5568SJanosch Frank 
4713e6c5568SJanosch Frank 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
4723e6c5568SJanosch Frank 	    debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
473f76f6371SJanosch Frank 		goto out;
47478f26131SChristian Borntraeger 
47522be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
47622be5a13SDavid Hildenbrand 
47784877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
478308c3e66SMichael Mueller 	rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
479308c3e66SMichael Mueller 	if (rc) {
4808d43d570SMichael Mueller 		pr_err("A FLIC registration call failed with rc=%d\n", rc);
481f76f6371SJanosch Frank 		goto out;
482308c3e66SMichael Mueller 	}
483b1d1e76eSMichael Mueller 
484b1d1e76eSMichael Mueller 	rc = kvm_s390_gib_init(GAL_ISC);
485b1d1e76eSMichael Mueller 	if (rc)
486f76f6371SJanosch Frank 		goto out;
487b1d1e76eSMichael Mueller 
488308c3e66SMichael Mueller 	return 0;
489308c3e66SMichael Mueller 
490f76f6371SJanosch Frank out:
491f76f6371SJanosch Frank 	kvm_arch_exit();
492308c3e66SMichael Mueller 	return rc;
493b0c632dbSHeiko Carstens }
494b0c632dbSHeiko Carstens 
49578f26131SChristian Borntraeger void kvm_arch_exit(void)
49678f26131SChristian Borntraeger {
4971282c21eSMichael Mueller 	kvm_s390_gib_destroy();
49878f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
4993e6c5568SJanosch Frank 	debug_unregister(kvm_s390_dbf_uv);
50078f26131SChristian Borntraeger }
50178f26131SChristian Borntraeger 
502b0c632dbSHeiko Carstens /* Section: device related */
503b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
504b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
505b0c632dbSHeiko Carstens {
506b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
507b0c632dbSHeiko Carstens 		return s390_enable_sie();
508b0c632dbSHeiko Carstens 	return -EINVAL;
509b0c632dbSHeiko Carstens }
510b0c632dbSHeiko Carstens 
511784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
512b0c632dbSHeiko Carstens {
513d7b0b5ebSCarsten Otte 	int r;
514d7b0b5ebSCarsten Otte 
5152bd0ac4eSCarsten Otte 	switch (ext) {
516d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
517b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
51852e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
5191efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
5201efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
5211efd0f59SCarsten Otte #endif
5223c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
52360b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
52414eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
525d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
526fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
52710ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
528c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
52978599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
530f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
5316352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
532460df4c1SPaolo Bonzini 	case KVM_CAP_IMMEDIATE_EXIT:
53347b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
5342444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
535e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
53630ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
537816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
5386502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
5394036e387SClaudio Imbrenda 	case KVM_CAP_S390_CMMA_MIGRATION:
54047a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
541da9a1446SChristian Borntraeger 	case KVM_CAP_S390_AIS_MIGRATION:
5427de3f142SJanosch Frank 	case KVM_CAP_S390_VCPU_RESETS:
543d7b0b5ebSCarsten Otte 		r = 1;
544d7b0b5ebSCarsten Otte 		break;
545a4499382SJanosch Frank 	case KVM_CAP_S390_HPAGE_1M:
546a4499382SJanosch Frank 		r = 0;
54740ebdb8eSJanosch Frank 		if (hpage && !kvm_is_ucontrol(kvm))
548a4499382SJanosch Frank 			r = 1;
549a4499382SJanosch Frank 		break;
55041408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
55141408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
55241408c28SThomas Huth 		break;
553e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
554e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
555a86cb413SThomas Huth 	case KVM_CAP_MAX_VCPU_ID:
55676a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
557a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
558a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
559a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
56076a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
561e726b1bdSChristian Borntraeger 		break;
5621526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
563abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
5641526bf9cSChristian Borntraeger 		break;
56568c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
56668c55750SEric Farman 		r = MACHINE_HAS_VX;
56768c55750SEric Farman 		break;
568c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
569c6e5f166SFan Zhang 		r = test_facility(64);
570c6e5f166SFan Zhang 		break;
5714e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
5724e0b1ab7SFan Zhang 		r = test_facility(133);
5734e0b1ab7SFan Zhang 		break;
57435b3fde6SChristian Borntraeger 	case KVM_CAP_S390_BPB:
57535b3fde6SChristian Borntraeger 		r = test_facility(82);
57635b3fde6SChristian Borntraeger 		break;
5772bd0ac4eSCarsten Otte 	default:
578d7b0b5ebSCarsten Otte 		r = 0;
579b0c632dbSHeiko Carstens 	}
580d7b0b5ebSCarsten Otte 	return r;
5812bd0ac4eSCarsten Otte }
582b0c632dbSHeiko Carstens 
58315f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm,
58415f36ebdSJason J. Herne 				    struct kvm_memory_slot *memslot)
58515f36ebdSJason J. Herne {
5860959e168SJanosch Frank 	int i;
58715f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
5880959e168SJanosch Frank 	unsigned long gaddr, vmaddr;
58915f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
5900959e168SJanosch Frank 	DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
59115f36ebdSJason J. Herne 
5920959e168SJanosch Frank 	/* Loop over all guest segments */
5930959e168SJanosch Frank 	cur_gfn = memslot->base_gfn;
59415f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
5950959e168SJanosch Frank 	for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
5960959e168SJanosch Frank 		gaddr = gfn_to_gpa(cur_gfn);
5970959e168SJanosch Frank 		vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
5980959e168SJanosch Frank 		if (kvm_is_error_hva(vmaddr))
5990959e168SJanosch Frank 			continue;
60015f36ebdSJason J. Herne 
6010959e168SJanosch Frank 		bitmap_zero(bitmap, _PAGE_ENTRIES);
6020959e168SJanosch Frank 		gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
6030959e168SJanosch Frank 		for (i = 0; i < _PAGE_ENTRIES; i++) {
6040959e168SJanosch Frank 			if (test_bit(i, bitmap))
6050959e168SJanosch Frank 				mark_page_dirty(kvm, cur_gfn + i);
6060959e168SJanosch Frank 		}
6070959e168SJanosch Frank 
6081763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
6091763f8d0SChristian Borntraeger 			return;
61070c88a00SChristian Borntraeger 		cond_resched();
61115f36ebdSJason J. Herne 	}
61215f36ebdSJason J. Herne }
61315f36ebdSJason J. Herne 
614b0c632dbSHeiko Carstens /* Section: vm related */
615a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
616a6e2f683SEugene (jno) Dvurechenski 
617b0c632dbSHeiko Carstens /*
618b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
619b0c632dbSHeiko Carstens  */
620b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
621b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
622b0c632dbSHeiko Carstens {
62315f36ebdSJason J. Herne 	int r;
62415f36ebdSJason J. Herne 	unsigned long n;
6259f6b8029SPaolo Bonzini 	struct kvm_memslots *slots;
62615f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
62715f36ebdSJason J. Herne 	int is_dirty = 0;
62815f36ebdSJason J. Herne 
629e1e8a962SJanosch Frank 	if (kvm_is_ucontrol(kvm))
630e1e8a962SJanosch Frank 		return -EINVAL;
631e1e8a962SJanosch Frank 
63215f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
63315f36ebdSJason J. Herne 
63415f36ebdSJason J. Herne 	r = -EINVAL;
63515f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
63615f36ebdSJason J. Herne 		goto out;
63715f36ebdSJason J. Herne 
6389f6b8029SPaolo Bonzini 	slots = kvm_memslots(kvm);
6399f6b8029SPaolo Bonzini 	memslot = id_to_memslot(slots, log->slot);
64015f36ebdSJason J. Herne 	r = -ENOENT;
64115f36ebdSJason J. Herne 	if (!memslot->dirty_bitmap)
64215f36ebdSJason J. Herne 		goto out;
64315f36ebdSJason J. Herne 
64415f36ebdSJason J. Herne 	kvm_s390_sync_dirty_log(kvm, memslot);
64515f36ebdSJason J. Herne 	r = kvm_get_dirty_log(kvm, log, &is_dirty);
64615f36ebdSJason J. Herne 	if (r)
64715f36ebdSJason J. Herne 		goto out;
64815f36ebdSJason J. Herne 
64915f36ebdSJason J. Herne 	/* Clear the dirty log */
65015f36ebdSJason J. Herne 	if (is_dirty) {
65115f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
65215f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
65315f36ebdSJason J. Herne 	}
65415f36ebdSJason J. Herne 	r = 0;
65515f36ebdSJason J. Herne out:
65615f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
65715f36ebdSJason J. Herne 	return r;
658b0c632dbSHeiko Carstens }
659b0c632dbSHeiko Carstens 
6606502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
6616502a34cSDavid Hildenbrand {
6626502a34cSDavid Hildenbrand 	unsigned int i;
6636502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
6646502a34cSDavid Hildenbrand 
6656502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
6666502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
6676502a34cSDavid Hildenbrand 	}
6686502a34cSDavid Hildenbrand }
6696502a34cSDavid Hildenbrand 
670e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
671d938dc55SCornelia Huck {
672d938dc55SCornelia Huck 	int r;
673d938dc55SCornelia Huck 
674d938dc55SCornelia Huck 	if (cap->flags)
675d938dc55SCornelia Huck 		return -EINVAL;
676d938dc55SCornelia Huck 
677d938dc55SCornelia Huck 	switch (cap->cap) {
67884223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
679c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
68084223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
68184223598SCornelia Huck 		r = 0;
68284223598SCornelia Huck 		break;
6832444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
684c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
6852444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
6862444b352SDavid Hildenbrand 		r = 0;
6872444b352SDavid Hildenbrand 		break;
68868c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
6895967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
690a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
6915967c17bSDavid Hildenbrand 			r = -EBUSY;
6925967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
693c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
694c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
6952f87d942SGuenther Hutzl 			if (test_facility(134)) {
6962f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
6972f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_list, 134);
6982f87d942SGuenther Hutzl 			}
69953743aa7SMaxim Samoylov 			if (test_facility(135)) {
70053743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
70153743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_list, 135);
70253743aa7SMaxim Samoylov 			}
7037832e91cSChristian Borntraeger 			if (test_facility(148)) {
7047832e91cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 148);
7057832e91cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 148);
7067832e91cSChristian Borntraeger 			}
707d5cb6ab1SChristian Borntraeger 			if (test_facility(152)) {
708d5cb6ab1SChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 152);
709d5cb6ab1SChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 152);
710d5cb6ab1SChristian Borntraeger 			}
71118280d8bSMichael Mueller 			r = 0;
71218280d8bSMichael Mueller 		} else
71318280d8bSMichael Mueller 			r = -EINVAL;
7145967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
715c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
716c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
71768c55750SEric Farman 		break;
718c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
719c6e5f166SFan Zhang 		r = -EINVAL;
720c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
721a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
722c6e5f166SFan Zhang 			r = -EBUSY;
723c6e5f166SFan Zhang 		} else if (test_facility(64)) {
724c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
725c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
726c6e5f166SFan Zhang 			r = 0;
727c6e5f166SFan Zhang 		}
728c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
729c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
730c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
731c6e5f166SFan Zhang 		break;
73247a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
73347a4693eSYi Min Zhao 		mutex_lock(&kvm->lock);
73447a4693eSYi Min Zhao 		if (kvm->created_vcpus) {
73547a4693eSYi Min Zhao 			r = -EBUSY;
73647a4693eSYi Min Zhao 		} else {
73747a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_mask, 72);
73847a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_list, 72);
73947a4693eSYi Min Zhao 			r = 0;
74047a4693eSYi Min Zhao 		}
74147a4693eSYi Min Zhao 		mutex_unlock(&kvm->lock);
74247a4693eSYi Min Zhao 		VM_EVENT(kvm, 3, "ENABLE: AIS %s",
74347a4693eSYi Min Zhao 			 r ? "(not available)" : "(success)");
74447a4693eSYi Min Zhao 		break;
7454e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
7464e0b1ab7SFan Zhang 		r = -EINVAL;
7474e0b1ab7SFan Zhang 		mutex_lock(&kvm->lock);
748241e3ec0SChristian Borntraeger 		if (kvm->created_vcpus) {
7494e0b1ab7SFan Zhang 			r = -EBUSY;
7504e0b1ab7SFan Zhang 		} else if (test_facility(133)) {
7514e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_mask, 133);
7524e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_list, 133);
7534e0b1ab7SFan Zhang 			r = 0;
7544e0b1ab7SFan Zhang 		}
7554e0b1ab7SFan Zhang 		mutex_unlock(&kvm->lock);
7564e0b1ab7SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
7574e0b1ab7SFan Zhang 			 r ? "(not available)" : "(success)");
7584e0b1ab7SFan Zhang 		break;
759a4499382SJanosch Frank 	case KVM_CAP_S390_HPAGE_1M:
760a4499382SJanosch Frank 		mutex_lock(&kvm->lock);
761a4499382SJanosch Frank 		if (kvm->created_vcpus)
762a4499382SJanosch Frank 			r = -EBUSY;
76340ebdb8eSJanosch Frank 		else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
764a4499382SJanosch Frank 			r = -EINVAL;
765a4499382SJanosch Frank 		else {
766a4499382SJanosch Frank 			r = 0;
767df88f318SJanosch Frank 			down_write(&kvm->mm->mmap_sem);
768a4499382SJanosch Frank 			kvm->mm->context.allow_gmap_hpage_1m = 1;
769df88f318SJanosch Frank 			up_write(&kvm->mm->mmap_sem);
770a4499382SJanosch Frank 			/*
771a4499382SJanosch Frank 			 * We might have to create fake 4k page
772a4499382SJanosch Frank 			 * tables. To avoid that the hardware works on
773a4499382SJanosch Frank 			 * stale PGSTEs, we emulate these instructions.
774a4499382SJanosch Frank 			 */
775a4499382SJanosch Frank 			kvm->arch.use_skf = 0;
776a4499382SJanosch Frank 			kvm->arch.use_pfmfi = 0;
777a4499382SJanosch Frank 		}
778a4499382SJanosch Frank 		mutex_unlock(&kvm->lock);
779a4499382SJanosch Frank 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
780a4499382SJanosch Frank 			 r ? "(not available)" : "(success)");
781a4499382SJanosch Frank 		break;
782e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
783c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
784e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
785e44fc8c9SEkaterina Tumanova 		r = 0;
786e44fc8c9SEkaterina Tumanova 		break;
7876502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
7886502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
7896502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
7906502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
7916502a34cSDavid Hildenbrand 		r = 0;
7926502a34cSDavid Hildenbrand 		break;
793d938dc55SCornelia Huck 	default:
794d938dc55SCornelia Huck 		r = -EINVAL;
795d938dc55SCornelia Huck 		break;
796d938dc55SCornelia Huck 	}
797d938dc55SCornelia Huck 	return r;
798d938dc55SCornelia Huck }
799d938dc55SCornelia Huck 
8008c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
8018c0a7ce6SDominik Dingel {
8028c0a7ce6SDominik Dingel 	int ret;
8038c0a7ce6SDominik Dingel 
8048c0a7ce6SDominik Dingel 	switch (attr->attr) {
8058c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
8068c0a7ce6SDominik Dingel 		ret = 0;
807c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
808a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
809a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
8108c0a7ce6SDominik Dingel 			ret = -EFAULT;
8118c0a7ce6SDominik Dingel 		break;
8128c0a7ce6SDominik Dingel 	default:
8138c0a7ce6SDominik Dingel 		ret = -ENXIO;
8148c0a7ce6SDominik Dingel 		break;
8158c0a7ce6SDominik Dingel 	}
8168c0a7ce6SDominik Dingel 	return ret;
8178c0a7ce6SDominik Dingel }
8188c0a7ce6SDominik Dingel 
8198c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
8204f718eabSDominik Dingel {
8214f718eabSDominik Dingel 	int ret;
8224f718eabSDominik Dingel 	unsigned int idx;
8234f718eabSDominik Dingel 	switch (attr->attr) {
8244f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
825f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
826c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
827e6db1d61SDominik Dingel 			break;
828e6db1d61SDominik Dingel 
829c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
8304f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
831a4499382SJanosch Frank 		if (kvm->created_vcpus)
832a4499382SJanosch Frank 			ret = -EBUSY;
833a4499382SJanosch Frank 		else if (kvm->mm->context.allow_gmap_hpage_1m)
834a4499382SJanosch Frank 			ret = -EINVAL;
835a4499382SJanosch Frank 		else {
8364f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
837c9f0a2b8SJanosch Frank 			/* Not compatible with cmma. */
838c9f0a2b8SJanosch Frank 			kvm->arch.use_pfmfi = 0;
8394f718eabSDominik Dingel 			ret = 0;
8404f718eabSDominik Dingel 		}
8414f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
8424f718eabSDominik Dingel 		break;
8434f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
844f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
845f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
846f9cbd9b0SDavid Hildenbrand 			break;
847c3489155SDominik Dingel 		ret = -EINVAL;
848c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
849c3489155SDominik Dingel 			break;
850c3489155SDominik Dingel 
851c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
8524f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
8534f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
854a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
8554f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
8564f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
8574f718eabSDominik Dingel 		ret = 0;
8584f718eabSDominik Dingel 		break;
8598c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
8608c0a7ce6SDominik Dingel 		unsigned long new_limit;
8618c0a7ce6SDominik Dingel 
8628c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
8638c0a7ce6SDominik Dingel 			return -EINVAL;
8648c0a7ce6SDominik Dingel 
8658c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
8668c0a7ce6SDominik Dingel 			return -EFAULT;
8678c0a7ce6SDominik Dingel 
868a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
869a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
8708c0a7ce6SDominik Dingel 			return -E2BIG;
8718c0a7ce6SDominik Dingel 
872a3a92c31SDominik Dingel 		if (!new_limit)
873a3a92c31SDominik Dingel 			return -EINVAL;
874a3a92c31SDominik Dingel 
8756ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
876a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
877a3a92c31SDominik Dingel 			new_limit -= 1;
878a3a92c31SDominik Dingel 
8798c0a7ce6SDominik Dingel 		ret = -EBUSY;
8808c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
881a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
8826ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
8836ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
8848c0a7ce6SDominik Dingel 
8858c0a7ce6SDominik Dingel 			if (!new) {
8868c0a7ce6SDominik Dingel 				ret = -ENOMEM;
8878c0a7ce6SDominik Dingel 			} else {
8886ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
8898c0a7ce6SDominik Dingel 				new->private = kvm;
8908c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
8918c0a7ce6SDominik Dingel 				ret = 0;
8928c0a7ce6SDominik Dingel 			}
8938c0a7ce6SDominik Dingel 		}
8948c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
895a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
896a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
897a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
8988c0a7ce6SDominik Dingel 		break;
8998c0a7ce6SDominik Dingel 	}
9004f718eabSDominik Dingel 	default:
9014f718eabSDominik Dingel 		ret = -ENXIO;
9024f718eabSDominik Dingel 		break;
9034f718eabSDominik Dingel 	}
9044f718eabSDominik Dingel 	return ret;
9054f718eabSDominik Dingel }
9064f718eabSDominik Dingel 
907a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
908a374e892STony Krowiak 
90920c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
910a374e892STony Krowiak {
911a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
912a374e892STony Krowiak 	int i;
913a374e892STony Krowiak 
91420c922f0STony Krowiak 	kvm_s390_vcpu_block_all(kvm);
91520c922f0STony Krowiak 
9163194cdb7SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
91720c922f0STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
9183194cdb7SDavid Hildenbrand 		/* recreate the shadow crycb by leaving the VSIE handler */
9193194cdb7SDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
9203194cdb7SDavid Hildenbrand 	}
92120c922f0STony Krowiak 
92220c922f0STony Krowiak 	kvm_s390_vcpu_unblock_all(kvm);
92320c922f0STony Krowiak }
92420c922f0STony Krowiak 
92520c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
92620c922f0STony Krowiak {
927a374e892STony Krowiak 	mutex_lock(&kvm->lock);
928a374e892STony Krowiak 	switch (attr->attr) {
929a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
9308e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9318e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
93237940fb0STony Krowiak 			return -EINVAL;
9338e41bd54SChristian Borntraeger 		}
934a374e892STony Krowiak 		get_random_bytes(
935a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
936a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
937a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
938c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
939a374e892STony Krowiak 		break;
940a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
9418e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9428e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
94337940fb0STony Krowiak 			return -EINVAL;
9448e41bd54SChristian Borntraeger 		}
945a374e892STony Krowiak 		get_random_bytes(
946a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
947a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
948a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
949c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
950a374e892STony Krowiak 		break;
951a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
9528e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9538e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
95437940fb0STony Krowiak 			return -EINVAL;
9558e41bd54SChristian Borntraeger 		}
956a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
957a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
958a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
959c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
960a374e892STony Krowiak 		break;
961a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
9628e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9638e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
96437940fb0STony Krowiak 			return -EINVAL;
9658e41bd54SChristian Borntraeger 		}
966a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
967a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
968a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
969c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
970a374e892STony Krowiak 		break;
97137940fb0STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_APIE:
97237940fb0STony Krowiak 		if (!ap_instructions_available()) {
97337940fb0STony Krowiak 			mutex_unlock(&kvm->lock);
97437940fb0STony Krowiak 			return -EOPNOTSUPP;
97537940fb0STony Krowiak 		}
97637940fb0STony Krowiak 		kvm->arch.crypto.apie = 1;
97737940fb0STony Krowiak 		break;
97837940fb0STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_APIE:
97937940fb0STony Krowiak 		if (!ap_instructions_available()) {
98037940fb0STony Krowiak 			mutex_unlock(&kvm->lock);
98137940fb0STony Krowiak 			return -EOPNOTSUPP;
98237940fb0STony Krowiak 		}
98337940fb0STony Krowiak 		kvm->arch.crypto.apie = 0;
98437940fb0STony Krowiak 		break;
985a374e892STony Krowiak 	default:
986a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
987a374e892STony Krowiak 		return -ENXIO;
988a374e892STony Krowiak 	}
989a374e892STony Krowiak 
99020c922f0STony Krowiak 	kvm_s390_vcpu_crypto_reset_all(kvm);
991a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
992a374e892STony Krowiak 	return 0;
993a374e892STony Krowiak }
994a374e892STony Krowiak 
995190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
996190df4a2SClaudio Imbrenda {
997190df4a2SClaudio Imbrenda 	int cx;
998190df4a2SClaudio Imbrenda 	struct kvm_vcpu *vcpu;
999190df4a2SClaudio Imbrenda 
1000190df4a2SClaudio Imbrenda 	kvm_for_each_vcpu(cx, vcpu, kvm)
1001190df4a2SClaudio Imbrenda 		kvm_s390_sync_request(req, vcpu);
1002190df4a2SClaudio Imbrenda }
1003190df4a2SClaudio Imbrenda 
1004190df4a2SClaudio Imbrenda /*
1005190df4a2SClaudio Imbrenda  * Must be called with kvm->srcu held to avoid races on memslots, and with
10061de1ea7eSChristian Borntraeger  * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
1007190df4a2SClaudio Imbrenda  */
1008190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm)
1009190df4a2SClaudio Imbrenda {
1010190df4a2SClaudio Imbrenda 	struct kvm_memory_slot *ms;
1011190df4a2SClaudio Imbrenda 	struct kvm_memslots *slots;
1012afdad616SClaudio Imbrenda 	unsigned long ram_pages = 0;
1013190df4a2SClaudio Imbrenda 	int slotnr;
1014190df4a2SClaudio Imbrenda 
1015190df4a2SClaudio Imbrenda 	/* migration mode already enabled */
1016afdad616SClaudio Imbrenda 	if (kvm->arch.migration_mode)
1017190df4a2SClaudio Imbrenda 		return 0;
1018190df4a2SClaudio Imbrenda 	slots = kvm_memslots(kvm);
1019190df4a2SClaudio Imbrenda 	if (!slots || !slots->used_slots)
1020190df4a2SClaudio Imbrenda 		return -EINVAL;
1021190df4a2SClaudio Imbrenda 
1022afdad616SClaudio Imbrenda 	if (!kvm->arch.use_cmma) {
1023afdad616SClaudio Imbrenda 		kvm->arch.migration_mode = 1;
1024afdad616SClaudio Imbrenda 		return 0;
1025190df4a2SClaudio Imbrenda 	}
1026190df4a2SClaudio Imbrenda 	/* mark all the pages in active slots as dirty */
1027190df4a2SClaudio Imbrenda 	for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
1028190df4a2SClaudio Imbrenda 		ms = slots->memslots + slotnr;
102913a17cc0SIgor Mammedov 		if (!ms->dirty_bitmap)
103013a17cc0SIgor Mammedov 			return -EINVAL;
1031afdad616SClaudio Imbrenda 		/*
1032afdad616SClaudio Imbrenda 		 * The second half of the bitmap is only used on x86,
1033afdad616SClaudio Imbrenda 		 * and would be wasted otherwise, so we put it to good
1034afdad616SClaudio Imbrenda 		 * use here to keep track of the state of the storage
1035afdad616SClaudio Imbrenda 		 * attributes.
1036afdad616SClaudio Imbrenda 		 */
1037afdad616SClaudio Imbrenda 		memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
1038afdad616SClaudio Imbrenda 		ram_pages += ms->npages;
1039190df4a2SClaudio Imbrenda 	}
1040afdad616SClaudio Imbrenda 	atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
1041afdad616SClaudio Imbrenda 	kvm->arch.migration_mode = 1;
1042190df4a2SClaudio Imbrenda 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
1043190df4a2SClaudio Imbrenda 	return 0;
1044190df4a2SClaudio Imbrenda }
1045190df4a2SClaudio Imbrenda 
1046190df4a2SClaudio Imbrenda /*
10471de1ea7eSChristian Borntraeger  * Must be called with kvm->slots_lock to avoid races with ourselves and
1048190df4a2SClaudio Imbrenda  * kvm_s390_vm_start_migration.
1049190df4a2SClaudio Imbrenda  */
1050190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm)
1051190df4a2SClaudio Imbrenda {
1052190df4a2SClaudio Imbrenda 	/* migration mode already disabled */
1053afdad616SClaudio Imbrenda 	if (!kvm->arch.migration_mode)
1054190df4a2SClaudio Imbrenda 		return 0;
1055afdad616SClaudio Imbrenda 	kvm->arch.migration_mode = 0;
1056afdad616SClaudio Imbrenda 	if (kvm->arch.use_cmma)
1057190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
1058190df4a2SClaudio Imbrenda 	return 0;
1059190df4a2SClaudio Imbrenda }
1060190df4a2SClaudio Imbrenda 
1061190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm,
1062190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
1063190df4a2SClaudio Imbrenda {
10641de1ea7eSChristian Borntraeger 	int res = -ENXIO;
1065190df4a2SClaudio Imbrenda 
10661de1ea7eSChristian Borntraeger 	mutex_lock(&kvm->slots_lock);
1067190df4a2SClaudio Imbrenda 	switch (attr->attr) {
1068190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_START:
1069190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_start_migration(kvm);
1070190df4a2SClaudio Imbrenda 		break;
1071190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_STOP:
1072190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_stop_migration(kvm);
1073190df4a2SClaudio Imbrenda 		break;
1074190df4a2SClaudio Imbrenda 	default:
1075190df4a2SClaudio Imbrenda 		break;
1076190df4a2SClaudio Imbrenda 	}
10771de1ea7eSChristian Borntraeger 	mutex_unlock(&kvm->slots_lock);
1078190df4a2SClaudio Imbrenda 
1079190df4a2SClaudio Imbrenda 	return res;
1080190df4a2SClaudio Imbrenda }
1081190df4a2SClaudio Imbrenda 
1082190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm,
1083190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
1084190df4a2SClaudio Imbrenda {
1085afdad616SClaudio Imbrenda 	u64 mig = kvm->arch.migration_mode;
1086190df4a2SClaudio Imbrenda 
1087190df4a2SClaudio Imbrenda 	if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1088190df4a2SClaudio Imbrenda 		return -ENXIO;
1089190df4a2SClaudio Imbrenda 
1090190df4a2SClaudio Imbrenda 	if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1091190df4a2SClaudio Imbrenda 		return -EFAULT;
1092190df4a2SClaudio Imbrenda 	return 0;
1093190df4a2SClaudio Imbrenda }
1094190df4a2SClaudio Imbrenda 
10958fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
10968fa1696eSCollin L. Walling {
10978fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
10988fa1696eSCollin L. Walling 
10998fa1696eSCollin L. Walling 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
11008fa1696eSCollin L. Walling 		return -EFAULT;
11018fa1696eSCollin L. Walling 
11020e7def5fSDavid Hildenbrand 	if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
11038fa1696eSCollin L. Walling 		return -EINVAL;
11040e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
11058fa1696eSCollin L. Walling 
11068fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
11078fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
11088fa1696eSCollin L. Walling 
11098fa1696eSCollin L. Walling 	return 0;
11108fa1696eSCollin L. Walling }
11118fa1696eSCollin L. Walling 
111272f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
111372f25020SJason J. Herne {
111472f25020SJason J. Herne 	u8 gtod_high;
111572f25020SJason J. Herne 
111672f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
111772f25020SJason J. Herne 					   sizeof(gtod_high)))
111872f25020SJason J. Herne 		return -EFAULT;
111972f25020SJason J. Herne 
112072f25020SJason J. Herne 	if (gtod_high != 0)
112172f25020SJason J. Herne 		return -EINVAL;
112258c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
112372f25020SJason J. Herne 
112472f25020SJason J. Herne 	return 0;
112572f25020SJason J. Herne }
112672f25020SJason J. Herne 
112772f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
112872f25020SJason J. Herne {
11290e7def5fSDavid Hildenbrand 	struct kvm_s390_vm_tod_clock gtod = { 0 };
113072f25020SJason J. Herne 
11310e7def5fSDavid Hildenbrand 	if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
11320e7def5fSDavid Hildenbrand 			   sizeof(gtod.tod)))
113372f25020SJason J. Herne 		return -EFAULT;
113472f25020SJason J. Herne 
11350e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
11360e7def5fSDavid Hildenbrand 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
113772f25020SJason J. Herne 	return 0;
113872f25020SJason J. Herne }
113972f25020SJason J. Herne 
114072f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
114172f25020SJason J. Herne {
114272f25020SJason J. Herne 	int ret;
114372f25020SJason J. Herne 
114472f25020SJason J. Herne 	if (attr->flags)
114572f25020SJason J. Herne 		return -EINVAL;
114672f25020SJason J. Herne 
114772f25020SJason J. Herne 	switch (attr->attr) {
11488fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
11498fa1696eSCollin L. Walling 		ret = kvm_s390_set_tod_ext(kvm, attr);
11508fa1696eSCollin L. Walling 		break;
115172f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
115272f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
115372f25020SJason J. Herne 		break;
115472f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
115572f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
115672f25020SJason J. Herne 		break;
115772f25020SJason J. Herne 	default:
115872f25020SJason J. Herne 		ret = -ENXIO;
115972f25020SJason J. Herne 		break;
116072f25020SJason J. Herne 	}
116172f25020SJason J. Herne 	return ret;
116272f25020SJason J. Herne }
116372f25020SJason J. Herne 
116433d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm,
11658fa1696eSCollin L. Walling 				   struct kvm_s390_vm_tod_clock *gtod)
11668fa1696eSCollin L. Walling {
11678fa1696eSCollin L. Walling 	struct kvm_s390_tod_clock_ext htod;
11688fa1696eSCollin L. Walling 
11698fa1696eSCollin L. Walling 	preempt_disable();
11708fa1696eSCollin L. Walling 
11718fa1696eSCollin L. Walling 	get_tod_clock_ext((char *)&htod);
11728fa1696eSCollin L. Walling 
11738fa1696eSCollin L. Walling 	gtod->tod = htod.tod + kvm->arch.epoch;
117433d1b272SDavid Hildenbrand 	gtod->epoch_idx = 0;
117533d1b272SDavid Hildenbrand 	if (test_kvm_facility(kvm, 139)) {
11768fa1696eSCollin L. Walling 		gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx;
11778fa1696eSCollin L. Walling 		if (gtod->tod < htod.tod)
11788fa1696eSCollin L. Walling 			gtod->epoch_idx += 1;
117933d1b272SDavid Hildenbrand 	}
11808fa1696eSCollin L. Walling 
11818fa1696eSCollin L. Walling 	preempt_enable();
11828fa1696eSCollin L. Walling }
11838fa1696eSCollin L. Walling 
11848fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
11858fa1696eSCollin L. Walling {
11868fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
11878fa1696eSCollin L. Walling 
11888fa1696eSCollin L. Walling 	memset(&gtod, 0, sizeof(gtod));
118933d1b272SDavid Hildenbrand 	kvm_s390_get_tod_clock(kvm, &gtod);
11908fa1696eSCollin L. Walling 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
11918fa1696eSCollin L. Walling 		return -EFAULT;
11928fa1696eSCollin L. Walling 
11938fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
11948fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
11958fa1696eSCollin L. Walling 	return 0;
11968fa1696eSCollin L. Walling }
11978fa1696eSCollin L. Walling 
119872f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
119972f25020SJason J. Herne {
120072f25020SJason J. Herne 	u8 gtod_high = 0;
120172f25020SJason J. Herne 
120272f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
120372f25020SJason J. Herne 					 sizeof(gtod_high)))
120472f25020SJason J. Herne 		return -EFAULT;
120558c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
120672f25020SJason J. Herne 
120772f25020SJason J. Herne 	return 0;
120872f25020SJason J. Herne }
120972f25020SJason J. Herne 
121072f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
121172f25020SJason J. Herne {
12125a3d883aSDavid Hildenbrand 	u64 gtod;
121372f25020SJason J. Herne 
121460417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
121572f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
121672f25020SJason J. Herne 		return -EFAULT;
121758c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
121872f25020SJason J. Herne 
121972f25020SJason J. Herne 	return 0;
122072f25020SJason J. Herne }
122172f25020SJason J. Herne 
122272f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
122372f25020SJason J. Herne {
122472f25020SJason J. Herne 	int ret;
122572f25020SJason J. Herne 
122672f25020SJason J. Herne 	if (attr->flags)
122772f25020SJason J. Herne 		return -EINVAL;
122872f25020SJason J. Herne 
122972f25020SJason J. Herne 	switch (attr->attr) {
12308fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
12318fa1696eSCollin L. Walling 		ret = kvm_s390_get_tod_ext(kvm, attr);
12328fa1696eSCollin L. Walling 		break;
123372f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
123472f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
123572f25020SJason J. Herne 		break;
123672f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
123772f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
123872f25020SJason J. Herne 		break;
123972f25020SJason J. Herne 	default:
124072f25020SJason J. Herne 		ret = -ENXIO;
124172f25020SJason J. Herne 		break;
124272f25020SJason J. Herne 	}
124372f25020SJason J. Herne 	return ret;
124472f25020SJason J. Herne }
124572f25020SJason J. Herne 
1246658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1247658b6edaSMichael Mueller {
1248658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1249053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
1250658b6edaSMichael Mueller 	int ret = 0;
1251658b6edaSMichael Mueller 
1252658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
1253a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
1254658b6edaSMichael Mueller 		ret = -EBUSY;
1255658b6edaSMichael Mueller 		goto out;
1256658b6edaSMichael Mueller 	}
1257658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1258658b6edaSMichael Mueller 	if (!proc) {
1259658b6edaSMichael Mueller 		ret = -ENOMEM;
1260658b6edaSMichael Mueller 		goto out;
1261658b6edaSMichael Mueller 	}
1262658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
1263658b6edaSMichael Mueller 			    sizeof(*proc))) {
12649bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
1265053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
1266053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
12670487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
1268053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
1269053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
1270053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
1271053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
1272053dd230SDavid Hildenbrand 			else
1273658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
1274053dd230SDavid Hildenbrand 		}
1275c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
1276658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
1277a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1278a8c39dd7SChristian Borntraeger 			 kvm->arch.model.ibc,
1279a8c39dd7SChristian Borntraeger 			 kvm->arch.model.cpuid);
1280a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1281a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[0],
1282a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[1],
1283a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[2]);
1284658b6edaSMichael Mueller 	} else
1285658b6edaSMichael Mueller 		ret = -EFAULT;
1286658b6edaSMichael Mueller 	kfree(proc);
1287658b6edaSMichael Mueller out:
1288658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
1289658b6edaSMichael Mueller 	return ret;
1290658b6edaSMichael Mueller }
1291658b6edaSMichael Mueller 
129215c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
129315c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
129415c9705fSDavid Hildenbrand {
129515c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
129615c9705fSDavid Hildenbrand 
129715c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
129815c9705fSDavid Hildenbrand 		return -EFAULT;
129915c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
130015c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
130115c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
130215c9705fSDavid Hildenbrand 		return -EINVAL;
130315c9705fSDavid Hildenbrand 
130415c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
13052f8311c9SChristian Borntraeger 	if (kvm->created_vcpus) {
13062f8311c9SChristian Borntraeger 		mutex_unlock(&kvm->lock);
13072f8311c9SChristian Borntraeger 		return -EBUSY;
13082f8311c9SChristian Borntraeger 	}
130915c9705fSDavid Hildenbrand 	bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
131015c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
131115c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
13122f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
13132f8311c9SChristian Borntraeger 			 data.feat[0],
13142f8311c9SChristian Borntraeger 			 data.feat[1],
13152f8311c9SChristian Borntraeger 			 data.feat[2]);
13162f8311c9SChristian Borntraeger 	return 0;
131715c9705fSDavid Hildenbrand }
131815c9705fSDavid Hildenbrand 
13190a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
13200a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
13210a763c78SDavid Hildenbrand {
1322346fa2f8SChristian Borntraeger 	mutex_lock(&kvm->lock);
1323346fa2f8SChristian Borntraeger 	if (kvm->created_vcpus) {
1324346fa2f8SChristian Borntraeger 		mutex_unlock(&kvm->lock);
1325346fa2f8SChristian Borntraeger 		return -EBUSY;
1326346fa2f8SChristian Borntraeger 	}
1327346fa2f8SChristian Borntraeger 
1328346fa2f8SChristian Borntraeger 	if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1329346fa2f8SChristian Borntraeger 			   sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1330346fa2f8SChristian Borntraeger 		mutex_unlock(&kvm->lock);
1331346fa2f8SChristian Borntraeger 		return -EFAULT;
1332346fa2f8SChristian Borntraeger 	}
1333346fa2f8SChristian Borntraeger 	mutex_unlock(&kvm->lock);
1334346fa2f8SChristian Borntraeger 
133511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
133611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
133711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
133811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
133911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
134011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
134111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
134211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
134311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
134411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
134511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
134611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
134711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
134811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
134911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KM     subfunc 0x%16.16lx.%16.16lx",
135011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
135111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
135211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
135311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
135411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
135511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
135611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
135711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
135811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
135911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
136011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
136111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
136211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
136311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
136411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
136511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
136611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
136711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
136811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
136911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
137011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
137111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
137211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
137311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
137411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
137511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
137611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
137711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
137811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
137913209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
138013209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
138113209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1382173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1383173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1384173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1385173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1386173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
13874f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
13884f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
13894f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
13904f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
13914f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
139211ba5961SChristian Borntraeger 
1393346fa2f8SChristian Borntraeger 	return 0;
13940a763c78SDavid Hildenbrand }
13950a763c78SDavid Hildenbrand 
1396658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1397658b6edaSMichael Mueller {
1398658b6edaSMichael Mueller 	int ret = -ENXIO;
1399658b6edaSMichael Mueller 
1400658b6edaSMichael Mueller 	switch (attr->attr) {
1401658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1402658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
1403658b6edaSMichael Mueller 		break;
140415c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
140515c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
140615c9705fSDavid Hildenbrand 		break;
14070a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
14080a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
14090a763c78SDavid Hildenbrand 		break;
1410658b6edaSMichael Mueller 	}
1411658b6edaSMichael Mueller 	return ret;
1412658b6edaSMichael Mueller }
1413658b6edaSMichael Mueller 
1414658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1415658b6edaSMichael Mueller {
1416658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1417658b6edaSMichael Mueller 	int ret = 0;
1418658b6edaSMichael Mueller 
1419658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1420658b6edaSMichael Mueller 	if (!proc) {
1421658b6edaSMichael Mueller 		ret = -ENOMEM;
1422658b6edaSMichael Mueller 		goto out;
1423658b6edaSMichael Mueller 	}
14249bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
1425658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
1426c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1427c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1428a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1429a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1430a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1431a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1432a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[0],
1433a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[1],
1434a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[2]);
1435658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1436658b6edaSMichael Mueller 		ret = -EFAULT;
1437658b6edaSMichael Mueller 	kfree(proc);
1438658b6edaSMichael Mueller out:
1439658b6edaSMichael Mueller 	return ret;
1440658b6edaSMichael Mueller }
1441658b6edaSMichael Mueller 
1442658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1443658b6edaSMichael Mueller {
1444658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
1445658b6edaSMichael Mueller 	int ret = 0;
1446658b6edaSMichael Mueller 
1447658b6edaSMichael Mueller 	mach = kzalloc(sizeof(*mach), GFP_KERNEL);
1448658b6edaSMichael Mueller 	if (!mach) {
1449658b6edaSMichael Mueller 		ret = -ENOMEM;
1450658b6edaSMichael Mueller 		goto out;
1451658b6edaSMichael Mueller 	}
1452658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
145337c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
1454c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1455981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1456658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
145704478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
1458a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
1459a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1460a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1461a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
1462a8c39dd7SChristian Borntraeger 		 mach->fac_mask[0],
1463a8c39dd7SChristian Borntraeger 		 mach->fac_mask[1],
1464a8c39dd7SChristian Borntraeger 		 mach->fac_mask[2]);
1465a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
1466a8c39dd7SChristian Borntraeger 		 mach->fac_list[0],
1467a8c39dd7SChristian Borntraeger 		 mach->fac_list[1],
1468a8c39dd7SChristian Borntraeger 		 mach->fac_list[2]);
1469658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1470658b6edaSMichael Mueller 		ret = -EFAULT;
1471658b6edaSMichael Mueller 	kfree(mach);
1472658b6edaSMichael Mueller out:
1473658b6edaSMichael Mueller 	return ret;
1474658b6edaSMichael Mueller }
1475658b6edaSMichael Mueller 
147615c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
147715c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
147815c9705fSDavid Hildenbrand {
147915c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
148015c9705fSDavid Hildenbrand 
148115c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
148215c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
148315c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
148415c9705fSDavid Hildenbrand 		return -EFAULT;
14852f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
14862f8311c9SChristian Borntraeger 			 data.feat[0],
14872f8311c9SChristian Borntraeger 			 data.feat[1],
14882f8311c9SChristian Borntraeger 			 data.feat[2]);
148915c9705fSDavid Hildenbrand 	return 0;
149015c9705fSDavid Hildenbrand }
149115c9705fSDavid Hildenbrand 
149215c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
149315c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
149415c9705fSDavid Hildenbrand {
149515c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
149615c9705fSDavid Hildenbrand 
149715c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat,
149815c9705fSDavid Hildenbrand 		    kvm_s390_available_cpu_feat,
149915c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
150015c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
150115c9705fSDavid Hildenbrand 		return -EFAULT;
15022f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host feat:  0x%16.16llx.0x%16.16llx.0x%16.16llx",
15032f8311c9SChristian Borntraeger 			 data.feat[0],
15042f8311c9SChristian Borntraeger 			 data.feat[1],
15052f8311c9SChristian Borntraeger 			 data.feat[2]);
150615c9705fSDavid Hildenbrand 	return 0;
150715c9705fSDavid Hildenbrand }
150815c9705fSDavid Hildenbrand 
15090a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
15100a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
15110a763c78SDavid Hildenbrand {
1512346fa2f8SChristian Borntraeger 	if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1513346fa2f8SChristian Borntraeger 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
1514346fa2f8SChristian Borntraeger 		return -EFAULT;
1515346fa2f8SChristian Borntraeger 
151611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
151711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
151811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
151911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
152011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
152111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
152211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
152311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
152411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
152511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
152611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
152711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
152811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
152911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
153011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KM     subfunc 0x%16.16lx.%16.16lx",
153111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
153211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
153311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
153411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
153511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
153611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
153711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
153811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
153911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
154011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
154111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
154211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
154311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
154411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
154511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
154611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
154711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
154811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
154911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
155011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
155111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
155211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
155311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
155411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
155511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
155611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
155711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
155811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
155911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
156013209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
156113209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
156213209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1563173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1564173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1565173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1566173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1567173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
15684f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
15694f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
15704f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
15714f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
15724f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
157311ba5961SChristian Borntraeger 
1574346fa2f8SChristian Borntraeger 	return 0;
15750a763c78SDavid Hildenbrand }
15760a763c78SDavid Hildenbrand 
15770a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
15780a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
15790a763c78SDavid Hildenbrand {
15800a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
15810a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
15820a763c78SDavid Hildenbrand 		return -EFAULT;
158311ba5961SChristian Borntraeger 
158411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
158511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
158611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
158711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
158811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
158911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PTFF   subfunc 0x%16.16lx.%16.16lx",
159011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
159111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
159211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMAC   subfunc 0x%16.16lx.%16.16lx",
159311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
159411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
159511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMC    subfunc 0x%16.16lx.%16.16lx",
159611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
159711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
159811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KM     subfunc 0x%16.16lx.%16.16lx",
159911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
160011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
160111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KIMD   subfunc 0x%16.16lx.%16.16lx",
160211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
160311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
160411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KLMD   subfunc 0x%16.16lx.%16.16lx",
160511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
160611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
160711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PCKMO  subfunc 0x%16.16lx.%16.16lx",
160811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
160911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
161011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMCTR  subfunc 0x%16.16lx.%16.16lx",
161111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
161211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
161311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMF    subfunc 0x%16.16lx.%16.16lx",
161411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
161511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
161611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMO    subfunc 0x%16.16lx.%16.16lx",
161711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
161811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
161911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PCC    subfunc 0x%16.16lx.%16.16lx",
162011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
162111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
162211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PPNO   subfunc 0x%16.16lx.%16.16lx",
162311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
162411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
162511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMA    subfunc 0x%16.16lx.%16.16lx",
162611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
162711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
162813209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KDSA   subfunc 0x%16.16lx.%16.16lx",
162913209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
163013209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
1631173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1632173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1633173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1634173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1635173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
16364f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
16374f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
16384f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
16394f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
16404f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
164111ba5961SChristian Borntraeger 
16420a763c78SDavid Hildenbrand 	return 0;
16430a763c78SDavid Hildenbrand }
1644346fa2f8SChristian Borntraeger 
1645658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1646658b6edaSMichael Mueller {
1647658b6edaSMichael Mueller 	int ret = -ENXIO;
1648658b6edaSMichael Mueller 
1649658b6edaSMichael Mueller 	switch (attr->attr) {
1650658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1651658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
1652658b6edaSMichael Mueller 		break;
1653658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
1654658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
1655658b6edaSMichael Mueller 		break;
165615c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
165715c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
165815c9705fSDavid Hildenbrand 		break;
165915c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
166015c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
166115c9705fSDavid Hildenbrand 		break;
16620a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
16630a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
16640a763c78SDavid Hildenbrand 		break;
16650a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
16660a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
16670a763c78SDavid Hildenbrand 		break;
1668658b6edaSMichael Mueller 	}
1669658b6edaSMichael Mueller 	return ret;
1670658b6edaSMichael Mueller }
1671658b6edaSMichael Mueller 
1672f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1673f2061656SDominik Dingel {
1674f2061656SDominik Dingel 	int ret;
1675f2061656SDominik Dingel 
1676f2061656SDominik Dingel 	switch (attr->group) {
16774f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
16788c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
16794f718eabSDominik Dingel 		break;
168072f25020SJason J. Herne 	case KVM_S390_VM_TOD:
168172f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
168272f25020SJason J. Herne 		break;
1683658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1684658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1685658b6edaSMichael Mueller 		break;
1686a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1687a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1688a374e892STony Krowiak 		break;
1689190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1690190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_set_migration(kvm, attr);
1691190df4a2SClaudio Imbrenda 		break;
1692f2061656SDominik Dingel 	default:
1693f2061656SDominik Dingel 		ret = -ENXIO;
1694f2061656SDominik Dingel 		break;
1695f2061656SDominik Dingel 	}
1696f2061656SDominik Dingel 
1697f2061656SDominik Dingel 	return ret;
1698f2061656SDominik Dingel }
1699f2061656SDominik Dingel 
1700f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1701f2061656SDominik Dingel {
17028c0a7ce6SDominik Dingel 	int ret;
17038c0a7ce6SDominik Dingel 
17048c0a7ce6SDominik Dingel 	switch (attr->group) {
17058c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
17068c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
17078c0a7ce6SDominik Dingel 		break;
170872f25020SJason J. Herne 	case KVM_S390_VM_TOD:
170972f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
171072f25020SJason J. Herne 		break;
1711658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1712658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1713658b6edaSMichael Mueller 		break;
1714190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1715190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_get_migration(kvm, attr);
1716190df4a2SClaudio Imbrenda 		break;
17178c0a7ce6SDominik Dingel 	default:
17188c0a7ce6SDominik Dingel 		ret = -ENXIO;
17198c0a7ce6SDominik Dingel 		break;
17208c0a7ce6SDominik Dingel 	}
17218c0a7ce6SDominik Dingel 
17228c0a7ce6SDominik Dingel 	return ret;
1723f2061656SDominik Dingel }
1724f2061656SDominik Dingel 
1725f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1726f2061656SDominik Dingel {
1727f2061656SDominik Dingel 	int ret;
1728f2061656SDominik Dingel 
1729f2061656SDominik Dingel 	switch (attr->group) {
17304f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
17314f718eabSDominik Dingel 		switch (attr->attr) {
17324f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
17334f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1734f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1735f9cbd9b0SDavid Hildenbrand 			break;
17368c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
17374f718eabSDominik Dingel 			ret = 0;
17384f718eabSDominik Dingel 			break;
17394f718eabSDominik Dingel 		default:
17404f718eabSDominik Dingel 			ret = -ENXIO;
17414f718eabSDominik Dingel 			break;
17424f718eabSDominik Dingel 		}
17434f718eabSDominik Dingel 		break;
174472f25020SJason J. Herne 	case KVM_S390_VM_TOD:
174572f25020SJason J. Herne 		switch (attr->attr) {
174672f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
174772f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
174872f25020SJason J. Herne 			ret = 0;
174972f25020SJason J. Herne 			break;
175072f25020SJason J. Herne 		default:
175172f25020SJason J. Herne 			ret = -ENXIO;
175272f25020SJason J. Herne 			break;
175372f25020SJason J. Herne 		}
175472f25020SJason J. Herne 		break;
1755658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1756658b6edaSMichael Mueller 		switch (attr->attr) {
1757658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1758658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
175915c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
176015c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
17610a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1762346fa2f8SChristian Borntraeger 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1763658b6edaSMichael Mueller 			ret = 0;
1764658b6edaSMichael Mueller 			break;
1765658b6edaSMichael Mueller 		default:
1766658b6edaSMichael Mueller 			ret = -ENXIO;
1767658b6edaSMichael Mueller 			break;
1768658b6edaSMichael Mueller 		}
1769658b6edaSMichael Mueller 		break;
1770a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1771a374e892STony Krowiak 		switch (attr->attr) {
1772a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1773a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1774a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1775a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1776a374e892STony Krowiak 			ret = 0;
1777a374e892STony Krowiak 			break;
177837940fb0STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_APIE:
177937940fb0STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_APIE:
178037940fb0STony Krowiak 			ret = ap_instructions_available() ? 0 : -ENXIO;
178137940fb0STony Krowiak 			break;
1782a374e892STony Krowiak 		default:
1783a374e892STony Krowiak 			ret = -ENXIO;
1784a374e892STony Krowiak 			break;
1785a374e892STony Krowiak 		}
1786a374e892STony Krowiak 		break;
1787190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1788190df4a2SClaudio Imbrenda 		ret = 0;
1789190df4a2SClaudio Imbrenda 		break;
1790f2061656SDominik Dingel 	default:
1791f2061656SDominik Dingel 		ret = -ENXIO;
1792f2061656SDominik Dingel 		break;
1793f2061656SDominik Dingel 	}
1794f2061656SDominik Dingel 
1795f2061656SDominik Dingel 	return ret;
1796f2061656SDominik Dingel }
1797f2061656SDominik Dingel 
179830ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
179930ee2a98SJason J. Herne {
180030ee2a98SJason J. Herne 	uint8_t *keys;
180130ee2a98SJason J. Herne 	uint64_t hva;
18024f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
180330ee2a98SJason J. Herne 
180430ee2a98SJason J. Herne 	if (args->flags != 0)
180530ee2a98SJason J. Herne 		return -EINVAL;
180630ee2a98SJason J. Herne 
180730ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
180855531b74SJanosch Frank 	if (!mm_uses_skeys(current->mm))
180930ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
181030ee2a98SJason J. Herne 
181130ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
181230ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
181330ee2a98SJason J. Herne 		return -EINVAL;
181430ee2a98SJason J. Herne 
1815752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
181630ee2a98SJason J. Herne 	if (!keys)
181730ee2a98SJason J. Herne 		return -ENOMEM;
181830ee2a98SJason J. Herne 
1819d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
18204f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
182130ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
182230ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
182330ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
182430ee2a98SJason J. Herne 			r = -EFAULT;
1825d3ed1ceeSMartin Schwidefsky 			break;
182630ee2a98SJason J. Herne 		}
182730ee2a98SJason J. Herne 
1828154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
1829154c8c19SDavid Hildenbrand 		if (r)
1830d3ed1ceeSMartin Schwidefsky 			break;
183130ee2a98SJason J. Herne 	}
18324f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1833d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
183430ee2a98SJason J. Herne 
1835d3ed1ceeSMartin Schwidefsky 	if (!r) {
183630ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
183730ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
183830ee2a98SJason J. Herne 		if (r)
183930ee2a98SJason J. Herne 			r = -EFAULT;
1840d3ed1ceeSMartin Schwidefsky 	}
1841d3ed1ceeSMartin Schwidefsky 
184230ee2a98SJason J. Herne 	kvfree(keys);
184330ee2a98SJason J. Herne 	return r;
184430ee2a98SJason J. Herne }
184530ee2a98SJason J. Herne 
184630ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
184730ee2a98SJason J. Herne {
184830ee2a98SJason J. Herne 	uint8_t *keys;
184930ee2a98SJason J. Herne 	uint64_t hva;
18504f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
1851bd096f64SJanosch Frank 	bool unlocked;
185230ee2a98SJason J. Herne 
185330ee2a98SJason J. Herne 	if (args->flags != 0)
185430ee2a98SJason J. Herne 		return -EINVAL;
185530ee2a98SJason J. Herne 
185630ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
185730ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
185830ee2a98SJason J. Herne 		return -EINVAL;
185930ee2a98SJason J. Herne 
1860752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
186130ee2a98SJason J. Herne 	if (!keys)
186230ee2a98SJason J. Herne 		return -ENOMEM;
186330ee2a98SJason J. Herne 
186430ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
186530ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
186630ee2a98SJason J. Herne 	if (r) {
186730ee2a98SJason J. Herne 		r = -EFAULT;
186830ee2a98SJason J. Herne 		goto out;
186930ee2a98SJason J. Herne 	}
187030ee2a98SJason J. Herne 
187130ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
187214d4a425SDominik Dingel 	r = s390_enable_skey();
187314d4a425SDominik Dingel 	if (r)
187414d4a425SDominik Dingel 		goto out;
187530ee2a98SJason J. Herne 
1876bd096f64SJanosch Frank 	i = 0;
1877d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
18784f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
1879bd096f64SJanosch Frank         while (i < args->count) {
1880bd096f64SJanosch Frank 		unlocked = false;
188130ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
188230ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
188330ee2a98SJason J. Herne 			r = -EFAULT;
1884d3ed1ceeSMartin Schwidefsky 			break;
188530ee2a98SJason J. Herne 		}
188630ee2a98SJason J. Herne 
188730ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
188830ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
188930ee2a98SJason J. Herne 			r = -EINVAL;
1890d3ed1ceeSMartin Schwidefsky 			break;
189130ee2a98SJason J. Herne 		}
189230ee2a98SJason J. Herne 
1893fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
1894bd096f64SJanosch Frank 		if (r) {
1895bd096f64SJanosch Frank 			r = fixup_user_fault(current, current->mm, hva,
1896bd096f64SJanosch Frank 					     FAULT_FLAG_WRITE, &unlocked);
189730ee2a98SJason J. Herne 			if (r)
1898d3ed1ceeSMartin Schwidefsky 				break;
189930ee2a98SJason J. Herne 		}
1900bd096f64SJanosch Frank 		if (!r)
1901bd096f64SJanosch Frank 			i++;
1902bd096f64SJanosch Frank 	}
19034f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1904d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
190530ee2a98SJason J. Herne out:
190630ee2a98SJason J. Herne 	kvfree(keys);
190730ee2a98SJason J. Herne 	return r;
190830ee2a98SJason J. Herne }
190930ee2a98SJason J. Herne 
19104036e387SClaudio Imbrenda /*
19114036e387SClaudio Imbrenda  * Base address and length must be sent at the start of each block, therefore
19124036e387SClaudio Imbrenda  * it's cheaper to send some clean data, as long as it's less than the size of
19134036e387SClaudio Imbrenda  * two longs.
19144036e387SClaudio Imbrenda  */
19154036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
19164036e387SClaudio Imbrenda /* for consistency */
19174036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
19184036e387SClaudio Imbrenda 
19194036e387SClaudio Imbrenda /*
1920afdad616SClaudio Imbrenda  * Similar to gfn_to_memslot, but returns the index of a memslot also when the
1921afdad616SClaudio Imbrenda  * address falls in a hole. In that case the index of one of the memslots
1922afdad616SClaudio Imbrenda  * bordering the hole is returned.
1923afdad616SClaudio Imbrenda  */
1924afdad616SClaudio Imbrenda static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn)
1925afdad616SClaudio Imbrenda {
1926afdad616SClaudio Imbrenda 	int start = 0, end = slots->used_slots;
1927afdad616SClaudio Imbrenda 	int slot = atomic_read(&slots->lru_slot);
1928afdad616SClaudio Imbrenda 	struct kvm_memory_slot *memslots = slots->memslots;
1929afdad616SClaudio Imbrenda 
1930afdad616SClaudio Imbrenda 	if (gfn >= memslots[slot].base_gfn &&
1931afdad616SClaudio Imbrenda 	    gfn < memslots[slot].base_gfn + memslots[slot].npages)
1932afdad616SClaudio Imbrenda 		return slot;
1933afdad616SClaudio Imbrenda 
1934afdad616SClaudio Imbrenda 	while (start < end) {
1935afdad616SClaudio Imbrenda 		slot = start + (end - start) / 2;
1936afdad616SClaudio Imbrenda 
1937afdad616SClaudio Imbrenda 		if (gfn >= memslots[slot].base_gfn)
1938afdad616SClaudio Imbrenda 			end = slot;
1939afdad616SClaudio Imbrenda 		else
1940afdad616SClaudio Imbrenda 			start = slot + 1;
1941afdad616SClaudio Imbrenda 	}
1942afdad616SClaudio Imbrenda 
1943afdad616SClaudio Imbrenda 	if (gfn >= memslots[start].base_gfn &&
1944afdad616SClaudio Imbrenda 	    gfn < memslots[start].base_gfn + memslots[start].npages) {
1945afdad616SClaudio Imbrenda 		atomic_set(&slots->lru_slot, start);
1946afdad616SClaudio Imbrenda 	}
1947afdad616SClaudio Imbrenda 
1948afdad616SClaudio Imbrenda 	return start;
1949afdad616SClaudio Imbrenda }
1950afdad616SClaudio Imbrenda 
1951afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
1952afdad616SClaudio Imbrenda 			      u8 *res, unsigned long bufsize)
1953afdad616SClaudio Imbrenda {
1954afdad616SClaudio Imbrenda 	unsigned long pgstev, hva, cur_gfn = args->start_gfn;
1955afdad616SClaudio Imbrenda 
1956afdad616SClaudio Imbrenda 	args->count = 0;
1957afdad616SClaudio Imbrenda 	while (args->count < bufsize) {
1958afdad616SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur_gfn);
1959afdad616SClaudio Imbrenda 		/*
1960afdad616SClaudio Imbrenda 		 * We return an error if the first value was invalid, but we
1961afdad616SClaudio Imbrenda 		 * return successfully if at least one value was copied.
1962afdad616SClaudio Imbrenda 		 */
1963afdad616SClaudio Imbrenda 		if (kvm_is_error_hva(hva))
1964afdad616SClaudio Imbrenda 			return args->count ? 0 : -EFAULT;
1965afdad616SClaudio Imbrenda 		if (get_pgste(kvm->mm, hva, &pgstev) < 0)
1966afdad616SClaudio Imbrenda 			pgstev = 0;
1967afdad616SClaudio Imbrenda 		res[args->count++] = (pgstev >> 24) & 0x43;
1968afdad616SClaudio Imbrenda 		cur_gfn++;
1969afdad616SClaudio Imbrenda 	}
1970afdad616SClaudio Imbrenda 
1971afdad616SClaudio Imbrenda 	return 0;
1972afdad616SClaudio Imbrenda }
1973afdad616SClaudio Imbrenda 
1974afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
1975afdad616SClaudio Imbrenda 					      unsigned long cur_gfn)
1976afdad616SClaudio Imbrenda {
1977afdad616SClaudio Imbrenda 	int slotidx = gfn_to_memslot_approx(slots, cur_gfn);
1978afdad616SClaudio Imbrenda 	struct kvm_memory_slot *ms = slots->memslots + slotidx;
1979afdad616SClaudio Imbrenda 	unsigned long ofs = cur_gfn - ms->base_gfn;
1980afdad616SClaudio Imbrenda 
1981afdad616SClaudio Imbrenda 	if (ms->base_gfn + ms->npages <= cur_gfn) {
1982afdad616SClaudio Imbrenda 		slotidx--;
1983afdad616SClaudio Imbrenda 		/* If we are above the highest slot, wrap around */
1984afdad616SClaudio Imbrenda 		if (slotidx < 0)
1985afdad616SClaudio Imbrenda 			slotidx = slots->used_slots - 1;
1986afdad616SClaudio Imbrenda 
1987afdad616SClaudio Imbrenda 		ms = slots->memslots + slotidx;
1988afdad616SClaudio Imbrenda 		ofs = 0;
1989afdad616SClaudio Imbrenda 	}
1990afdad616SClaudio Imbrenda 	ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
1991afdad616SClaudio Imbrenda 	while ((slotidx > 0) && (ofs >= ms->npages)) {
1992afdad616SClaudio Imbrenda 		slotidx--;
1993afdad616SClaudio Imbrenda 		ms = slots->memslots + slotidx;
1994afdad616SClaudio Imbrenda 		ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0);
1995afdad616SClaudio Imbrenda 	}
1996afdad616SClaudio Imbrenda 	return ms->base_gfn + ofs;
1997afdad616SClaudio Imbrenda }
1998afdad616SClaudio Imbrenda 
1999afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2000afdad616SClaudio Imbrenda 			     u8 *res, unsigned long bufsize)
2001afdad616SClaudio Imbrenda {
2002afdad616SClaudio Imbrenda 	unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
2003afdad616SClaudio Imbrenda 	struct kvm_memslots *slots = kvm_memslots(kvm);
2004afdad616SClaudio Imbrenda 	struct kvm_memory_slot *ms;
2005afdad616SClaudio Imbrenda 
2006afdad616SClaudio Imbrenda 	cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
2007afdad616SClaudio Imbrenda 	ms = gfn_to_memslot(kvm, cur_gfn);
2008afdad616SClaudio Imbrenda 	args->count = 0;
2009afdad616SClaudio Imbrenda 	args->start_gfn = cur_gfn;
2010afdad616SClaudio Imbrenda 	if (!ms)
2011afdad616SClaudio Imbrenda 		return 0;
2012afdad616SClaudio Imbrenda 	next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2013afdad616SClaudio Imbrenda 	mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages;
2014afdad616SClaudio Imbrenda 
2015afdad616SClaudio Imbrenda 	while (args->count < bufsize) {
2016afdad616SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur_gfn);
2017afdad616SClaudio Imbrenda 		if (kvm_is_error_hva(hva))
2018afdad616SClaudio Imbrenda 			return 0;
2019afdad616SClaudio Imbrenda 		/* Decrement only if we actually flipped the bit to 0 */
2020afdad616SClaudio Imbrenda 		if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
2021afdad616SClaudio Imbrenda 			atomic64_dec(&kvm->arch.cmma_dirty_pages);
2022afdad616SClaudio Imbrenda 		if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2023afdad616SClaudio Imbrenda 			pgstev = 0;
2024afdad616SClaudio Imbrenda 		/* Save the value */
2025afdad616SClaudio Imbrenda 		res[args->count++] = (pgstev >> 24) & 0x43;
2026afdad616SClaudio Imbrenda 		/* If the next bit is too far away, stop. */
2027afdad616SClaudio Imbrenda 		if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
2028afdad616SClaudio Imbrenda 			return 0;
2029afdad616SClaudio Imbrenda 		/* If we reached the previous "next", find the next one */
2030afdad616SClaudio Imbrenda 		if (cur_gfn == next_gfn)
2031afdad616SClaudio Imbrenda 			next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2032afdad616SClaudio Imbrenda 		/* Reached the end of memory or of the buffer, stop */
2033afdad616SClaudio Imbrenda 		if ((next_gfn >= mem_end) ||
2034afdad616SClaudio Imbrenda 		    (next_gfn - args->start_gfn >= bufsize))
2035afdad616SClaudio Imbrenda 			return 0;
2036afdad616SClaudio Imbrenda 		cur_gfn++;
2037afdad616SClaudio Imbrenda 		/* Reached the end of the current memslot, take the next one. */
2038afdad616SClaudio Imbrenda 		if (cur_gfn - ms->base_gfn >= ms->npages) {
2039afdad616SClaudio Imbrenda 			ms = gfn_to_memslot(kvm, cur_gfn);
2040afdad616SClaudio Imbrenda 			if (!ms)
2041afdad616SClaudio Imbrenda 				return 0;
2042afdad616SClaudio Imbrenda 		}
2043afdad616SClaudio Imbrenda 	}
2044afdad616SClaudio Imbrenda 	return 0;
2045afdad616SClaudio Imbrenda }
2046afdad616SClaudio Imbrenda 
2047afdad616SClaudio Imbrenda /*
20484036e387SClaudio Imbrenda  * This function searches for the next page with dirty CMMA attributes, and
20494036e387SClaudio Imbrenda  * saves the attributes in the buffer up to either the end of the buffer or
20504036e387SClaudio Imbrenda  * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
20514036e387SClaudio Imbrenda  * no trailing clean bytes are saved.
20524036e387SClaudio Imbrenda  * In case no dirty bits were found, or if CMMA was not enabled or used, the
20534036e387SClaudio Imbrenda  * output buffer will indicate 0 as length.
20544036e387SClaudio Imbrenda  */
20554036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm,
20564036e387SClaudio Imbrenda 				  struct kvm_s390_cmma_log *args)
20574036e387SClaudio Imbrenda {
2058afdad616SClaudio Imbrenda 	unsigned long bufsize;
2059afdad616SClaudio Imbrenda 	int srcu_idx, peek, ret;
2060afdad616SClaudio Imbrenda 	u8 *values;
20614036e387SClaudio Imbrenda 
2062afdad616SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
20634036e387SClaudio Imbrenda 		return -ENXIO;
20644036e387SClaudio Imbrenda 	/* Invalid/unsupported flags were specified */
20654036e387SClaudio Imbrenda 	if (args->flags & ~KVM_S390_CMMA_PEEK)
20664036e387SClaudio Imbrenda 		return -EINVAL;
20674036e387SClaudio Imbrenda 	/* Migration mode query, and we are not doing a migration */
20684036e387SClaudio Imbrenda 	peek = !!(args->flags & KVM_S390_CMMA_PEEK);
2069afdad616SClaudio Imbrenda 	if (!peek && !kvm->arch.migration_mode)
20704036e387SClaudio Imbrenda 		return -EINVAL;
20714036e387SClaudio Imbrenda 	/* CMMA is disabled or was not used, or the buffer has length zero */
20724036e387SClaudio Imbrenda 	bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
2073c9f0a2b8SJanosch Frank 	if (!bufsize || !kvm->mm->context.uses_cmm) {
20744036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
20754036e387SClaudio Imbrenda 		return 0;
20764036e387SClaudio Imbrenda 	}
20774036e387SClaudio Imbrenda 	/* We are not peeking, and there are no dirty pages */
2078afdad616SClaudio Imbrenda 	if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
20794036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
20804036e387SClaudio Imbrenda 		return 0;
20814036e387SClaudio Imbrenda 	}
20824036e387SClaudio Imbrenda 
2083afdad616SClaudio Imbrenda 	values = vmalloc(bufsize);
2084afdad616SClaudio Imbrenda 	if (!values)
20854036e387SClaudio Imbrenda 		return -ENOMEM;
20864036e387SClaudio Imbrenda 
20874036e387SClaudio Imbrenda 	down_read(&kvm->mm->mmap_sem);
20884036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
2089afdad616SClaudio Imbrenda 	if (peek)
2090afdad616SClaudio Imbrenda 		ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
2091afdad616SClaudio Imbrenda 	else
2092afdad616SClaudio Imbrenda 		ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
20934036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
20944036e387SClaudio Imbrenda 	up_read(&kvm->mm->mmap_sem);
20954036e387SClaudio Imbrenda 
2096afdad616SClaudio Imbrenda 	if (kvm->arch.migration_mode)
2097afdad616SClaudio Imbrenda 		args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2098afdad616SClaudio Imbrenda 	else
2099afdad616SClaudio Imbrenda 		args->remaining = 0;
21004036e387SClaudio Imbrenda 
2101afdad616SClaudio Imbrenda 	if (copy_to_user((void __user *)args->values, values, args->count))
2102afdad616SClaudio Imbrenda 		ret = -EFAULT;
2103afdad616SClaudio Imbrenda 
2104afdad616SClaudio Imbrenda 	vfree(values);
2105afdad616SClaudio Imbrenda 	return ret;
21064036e387SClaudio Imbrenda }
21074036e387SClaudio Imbrenda 
21084036e387SClaudio Imbrenda /*
21094036e387SClaudio Imbrenda  * This function sets the CMMA attributes for the given pages. If the input
21104036e387SClaudio Imbrenda  * buffer has zero length, no action is taken, otherwise the attributes are
2111c9f0a2b8SJanosch Frank  * set and the mm->context.uses_cmm flag is set.
21124036e387SClaudio Imbrenda  */
21134036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm,
21144036e387SClaudio Imbrenda 				  const struct kvm_s390_cmma_log *args)
21154036e387SClaudio Imbrenda {
21164036e387SClaudio Imbrenda 	unsigned long hva, mask, pgstev, i;
21174036e387SClaudio Imbrenda 	uint8_t *bits;
21184036e387SClaudio Imbrenda 	int srcu_idx, r = 0;
21194036e387SClaudio Imbrenda 
21204036e387SClaudio Imbrenda 	mask = args->mask;
21214036e387SClaudio Imbrenda 
21224036e387SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
21234036e387SClaudio Imbrenda 		return -ENXIO;
21244036e387SClaudio Imbrenda 	/* invalid/unsupported flags */
21254036e387SClaudio Imbrenda 	if (args->flags != 0)
21264036e387SClaudio Imbrenda 		return -EINVAL;
21274036e387SClaudio Imbrenda 	/* Enforce sane limit on memory allocation */
21284036e387SClaudio Imbrenda 	if (args->count > KVM_S390_CMMA_SIZE_MAX)
21294036e387SClaudio Imbrenda 		return -EINVAL;
21304036e387SClaudio Imbrenda 	/* Nothing to do */
21314036e387SClaudio Imbrenda 	if (args->count == 0)
21324036e387SClaudio Imbrenda 		return 0;
21334036e387SClaudio Imbrenda 
213442bc47b3SKees Cook 	bits = vmalloc(array_size(sizeof(*bits), args->count));
21354036e387SClaudio Imbrenda 	if (!bits)
21364036e387SClaudio Imbrenda 		return -ENOMEM;
21374036e387SClaudio Imbrenda 
21384036e387SClaudio Imbrenda 	r = copy_from_user(bits, (void __user *)args->values, args->count);
21394036e387SClaudio Imbrenda 	if (r) {
21404036e387SClaudio Imbrenda 		r = -EFAULT;
21414036e387SClaudio Imbrenda 		goto out;
21424036e387SClaudio Imbrenda 	}
21434036e387SClaudio Imbrenda 
21444036e387SClaudio Imbrenda 	down_read(&kvm->mm->mmap_sem);
21454036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
21464036e387SClaudio Imbrenda 	for (i = 0; i < args->count; i++) {
21474036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, args->start_gfn + i);
21484036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
21494036e387SClaudio Imbrenda 			r = -EFAULT;
21504036e387SClaudio Imbrenda 			break;
21514036e387SClaudio Imbrenda 		}
21524036e387SClaudio Imbrenda 
21534036e387SClaudio Imbrenda 		pgstev = bits[i];
21544036e387SClaudio Imbrenda 		pgstev = pgstev << 24;
21551bab1c02SClaudio Imbrenda 		mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
21564036e387SClaudio Imbrenda 		set_pgste_bits(kvm->mm, hva, mask, pgstev);
21574036e387SClaudio Imbrenda 	}
21584036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
21594036e387SClaudio Imbrenda 	up_read(&kvm->mm->mmap_sem);
21604036e387SClaudio Imbrenda 
2161c9f0a2b8SJanosch Frank 	if (!kvm->mm->context.uses_cmm) {
21624036e387SClaudio Imbrenda 		down_write(&kvm->mm->mmap_sem);
2163c9f0a2b8SJanosch Frank 		kvm->mm->context.uses_cmm = 1;
21644036e387SClaudio Imbrenda 		up_write(&kvm->mm->mmap_sem);
21654036e387SClaudio Imbrenda 	}
21664036e387SClaudio Imbrenda out:
21674036e387SClaudio Imbrenda 	vfree(bits);
21684036e387SClaudio Imbrenda 	return r;
21694036e387SClaudio Imbrenda }
21704036e387SClaudio Imbrenda 
2171*29b40f10SJanosch Frank static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp)
2172*29b40f10SJanosch Frank {
2173*29b40f10SJanosch Frank 	struct kvm_vcpu *vcpu;
2174*29b40f10SJanosch Frank 	u16 rc, rrc;
2175*29b40f10SJanosch Frank 	int ret = 0;
2176*29b40f10SJanosch Frank 	int i;
2177*29b40f10SJanosch Frank 
2178*29b40f10SJanosch Frank 	/*
2179*29b40f10SJanosch Frank 	 * We ignore failures and try to destroy as many CPUs as possible.
2180*29b40f10SJanosch Frank 	 * At the same time we must not free the assigned resources when
2181*29b40f10SJanosch Frank 	 * this fails, as the ultravisor has still access to that memory.
2182*29b40f10SJanosch Frank 	 * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
2183*29b40f10SJanosch Frank 	 * behind.
2184*29b40f10SJanosch Frank 	 * We want to return the first failure rc and rrc, though.
2185*29b40f10SJanosch Frank 	 */
2186*29b40f10SJanosch Frank 	kvm_for_each_vcpu(i, vcpu, kvm) {
2187*29b40f10SJanosch Frank 		mutex_lock(&vcpu->mutex);
2188*29b40f10SJanosch Frank 		if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) {
2189*29b40f10SJanosch Frank 			*rcp = rc;
2190*29b40f10SJanosch Frank 			*rrcp = rrc;
2191*29b40f10SJanosch Frank 			ret = -EIO;
2192*29b40f10SJanosch Frank 		}
2193*29b40f10SJanosch Frank 		mutex_unlock(&vcpu->mutex);
2194*29b40f10SJanosch Frank 	}
2195*29b40f10SJanosch Frank 	return ret;
2196*29b40f10SJanosch Frank }
2197*29b40f10SJanosch Frank 
2198*29b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
2199*29b40f10SJanosch Frank {
2200*29b40f10SJanosch Frank 	int i, r = 0;
2201*29b40f10SJanosch Frank 	u16 dummy;
2202*29b40f10SJanosch Frank 
2203*29b40f10SJanosch Frank 	struct kvm_vcpu *vcpu;
2204*29b40f10SJanosch Frank 
2205*29b40f10SJanosch Frank 	kvm_for_each_vcpu(i, vcpu, kvm) {
2206*29b40f10SJanosch Frank 		mutex_lock(&vcpu->mutex);
2207*29b40f10SJanosch Frank 		r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
2208*29b40f10SJanosch Frank 		mutex_unlock(&vcpu->mutex);
2209*29b40f10SJanosch Frank 		if (r)
2210*29b40f10SJanosch Frank 			break;
2211*29b40f10SJanosch Frank 	}
2212*29b40f10SJanosch Frank 	if (r)
2213*29b40f10SJanosch Frank 		kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
2214*29b40f10SJanosch Frank 	return r;
2215*29b40f10SJanosch Frank }
2216*29b40f10SJanosch Frank 
2217*29b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
2218*29b40f10SJanosch Frank {
2219*29b40f10SJanosch Frank 	int r = 0;
2220*29b40f10SJanosch Frank 	u16 dummy;
2221*29b40f10SJanosch Frank 	void __user *argp = (void __user *)cmd->data;
2222*29b40f10SJanosch Frank 
2223*29b40f10SJanosch Frank 	switch (cmd->cmd) {
2224*29b40f10SJanosch Frank 	case KVM_PV_ENABLE: {
2225*29b40f10SJanosch Frank 		r = -EINVAL;
2226*29b40f10SJanosch Frank 		if (kvm_s390_pv_is_protected(kvm))
2227*29b40f10SJanosch Frank 			break;
2228*29b40f10SJanosch Frank 
2229*29b40f10SJanosch Frank 		/*
2230*29b40f10SJanosch Frank 		 *  FMT 4 SIE needs esca. As we never switch back to bsca from
2231*29b40f10SJanosch Frank 		 *  esca, we need no cleanup in the error cases below
2232*29b40f10SJanosch Frank 		 */
2233*29b40f10SJanosch Frank 		r = sca_switch_to_extended(kvm);
2234*29b40f10SJanosch Frank 		if (r)
2235*29b40f10SJanosch Frank 			break;
2236*29b40f10SJanosch Frank 
2237*29b40f10SJanosch Frank 		r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
2238*29b40f10SJanosch Frank 		if (r)
2239*29b40f10SJanosch Frank 			break;
2240*29b40f10SJanosch Frank 
2241*29b40f10SJanosch Frank 		r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
2242*29b40f10SJanosch Frank 		if (r)
2243*29b40f10SJanosch Frank 			kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
2244*29b40f10SJanosch Frank 		break;
2245*29b40f10SJanosch Frank 	}
2246*29b40f10SJanosch Frank 	case KVM_PV_DISABLE: {
2247*29b40f10SJanosch Frank 		r = -EINVAL;
2248*29b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2249*29b40f10SJanosch Frank 			break;
2250*29b40f10SJanosch Frank 
2251*29b40f10SJanosch Frank 		r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
2252*29b40f10SJanosch Frank 		/*
2253*29b40f10SJanosch Frank 		 * If a CPU could not be destroyed, destroy VM will also fail.
2254*29b40f10SJanosch Frank 		 * There is no point in trying to destroy it. Instead return
2255*29b40f10SJanosch Frank 		 * the rc and rrc from the first CPU that failed destroying.
2256*29b40f10SJanosch Frank 		 */
2257*29b40f10SJanosch Frank 		if (r)
2258*29b40f10SJanosch Frank 			break;
2259*29b40f10SJanosch Frank 		r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc);
2260*29b40f10SJanosch Frank 		break;
2261*29b40f10SJanosch Frank 	}
2262*29b40f10SJanosch Frank 	case KVM_PV_SET_SEC_PARMS: {
2263*29b40f10SJanosch Frank 		struct kvm_s390_pv_sec_parm parms = {};
2264*29b40f10SJanosch Frank 		void *hdr;
2265*29b40f10SJanosch Frank 
2266*29b40f10SJanosch Frank 		r = -EINVAL;
2267*29b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2268*29b40f10SJanosch Frank 			break;
2269*29b40f10SJanosch Frank 
2270*29b40f10SJanosch Frank 		r = -EFAULT;
2271*29b40f10SJanosch Frank 		if (copy_from_user(&parms, argp, sizeof(parms)))
2272*29b40f10SJanosch Frank 			break;
2273*29b40f10SJanosch Frank 
2274*29b40f10SJanosch Frank 		/* Currently restricted to 8KB */
2275*29b40f10SJanosch Frank 		r = -EINVAL;
2276*29b40f10SJanosch Frank 		if (parms.length > PAGE_SIZE * 2)
2277*29b40f10SJanosch Frank 			break;
2278*29b40f10SJanosch Frank 
2279*29b40f10SJanosch Frank 		r = -ENOMEM;
2280*29b40f10SJanosch Frank 		hdr = vmalloc(parms.length);
2281*29b40f10SJanosch Frank 		if (!hdr)
2282*29b40f10SJanosch Frank 			break;
2283*29b40f10SJanosch Frank 
2284*29b40f10SJanosch Frank 		r = -EFAULT;
2285*29b40f10SJanosch Frank 		if (!copy_from_user(hdr, (void __user *)parms.origin,
2286*29b40f10SJanosch Frank 				    parms.length))
2287*29b40f10SJanosch Frank 			r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
2288*29b40f10SJanosch Frank 						      &cmd->rc, &cmd->rrc);
2289*29b40f10SJanosch Frank 
2290*29b40f10SJanosch Frank 		vfree(hdr);
2291*29b40f10SJanosch Frank 		break;
2292*29b40f10SJanosch Frank 	}
2293*29b40f10SJanosch Frank 	case KVM_PV_UNPACK: {
2294*29b40f10SJanosch Frank 		struct kvm_s390_pv_unp unp = {};
2295*29b40f10SJanosch Frank 
2296*29b40f10SJanosch Frank 		r = -EINVAL;
2297*29b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2298*29b40f10SJanosch Frank 			break;
2299*29b40f10SJanosch Frank 
2300*29b40f10SJanosch Frank 		r = -EFAULT;
2301*29b40f10SJanosch Frank 		if (copy_from_user(&unp, argp, sizeof(unp)))
2302*29b40f10SJanosch Frank 			break;
2303*29b40f10SJanosch Frank 
2304*29b40f10SJanosch Frank 		r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
2305*29b40f10SJanosch Frank 				       &cmd->rc, &cmd->rrc);
2306*29b40f10SJanosch Frank 		break;
2307*29b40f10SJanosch Frank 	}
2308*29b40f10SJanosch Frank 	case KVM_PV_VERIFY: {
2309*29b40f10SJanosch Frank 		r = -EINVAL;
2310*29b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2311*29b40f10SJanosch Frank 			break;
2312*29b40f10SJanosch Frank 
2313*29b40f10SJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2314*29b40f10SJanosch Frank 				  UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
2315*29b40f10SJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
2316*29b40f10SJanosch Frank 			     cmd->rrc);
2317*29b40f10SJanosch Frank 		break;
2318*29b40f10SJanosch Frank 	}
2319*29b40f10SJanosch Frank 	default:
2320*29b40f10SJanosch Frank 		r = -ENOTTY;
2321*29b40f10SJanosch Frank 	}
2322*29b40f10SJanosch Frank 	return r;
2323*29b40f10SJanosch Frank }
2324*29b40f10SJanosch Frank 
2325b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
2326b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
2327b0c632dbSHeiko Carstens {
2328b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
2329b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
2330f2061656SDominik Dingel 	struct kvm_device_attr attr;
2331b0c632dbSHeiko Carstens 	int r;
2332b0c632dbSHeiko Carstens 
2333b0c632dbSHeiko Carstens 	switch (ioctl) {
2334ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
2335ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
2336ba5c1e9bSCarsten Otte 
2337ba5c1e9bSCarsten Otte 		r = -EFAULT;
2338ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
2339ba5c1e9bSCarsten Otte 			break;
2340ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
2341ba5c1e9bSCarsten Otte 		break;
2342ba5c1e9bSCarsten Otte 	}
234384223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
234484223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
234584223598SCornelia Huck 
234684223598SCornelia Huck 		r = -EINVAL;
234784223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
234884223598SCornelia Huck 			/* Set up dummy routing. */
234984223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
2350152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
235184223598SCornelia Huck 		}
235284223598SCornelia Huck 		break;
235384223598SCornelia Huck 	}
2354f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
2355f2061656SDominik Dingel 		r = -EFAULT;
2356f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2357f2061656SDominik Dingel 			break;
2358f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
2359f2061656SDominik Dingel 		break;
2360f2061656SDominik Dingel 	}
2361f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
2362f2061656SDominik Dingel 		r = -EFAULT;
2363f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2364f2061656SDominik Dingel 			break;
2365f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
2366f2061656SDominik Dingel 		break;
2367f2061656SDominik Dingel 	}
2368f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
2369f2061656SDominik Dingel 		r = -EFAULT;
2370f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2371f2061656SDominik Dingel 			break;
2372f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
2373f2061656SDominik Dingel 		break;
2374f2061656SDominik Dingel 	}
237530ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
237630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
237730ee2a98SJason J. Herne 
237830ee2a98SJason J. Herne 		r = -EFAULT;
237930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
238030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
238130ee2a98SJason J. Herne 			break;
238230ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
238330ee2a98SJason J. Herne 		break;
238430ee2a98SJason J. Herne 	}
238530ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
238630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
238730ee2a98SJason J. Herne 
238830ee2a98SJason J. Herne 		r = -EFAULT;
238930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
239030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
239130ee2a98SJason J. Herne 			break;
239230ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
239330ee2a98SJason J. Herne 		break;
239430ee2a98SJason J. Herne 	}
23954036e387SClaudio Imbrenda 	case KVM_S390_GET_CMMA_BITS: {
23964036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
23974036e387SClaudio Imbrenda 
23984036e387SClaudio Imbrenda 		r = -EFAULT;
23994036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
24004036e387SClaudio Imbrenda 			break;
24011de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
24024036e387SClaudio Imbrenda 		r = kvm_s390_get_cmma_bits(kvm, &args);
24031de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
24044036e387SClaudio Imbrenda 		if (!r) {
24054036e387SClaudio Imbrenda 			r = copy_to_user(argp, &args, sizeof(args));
24064036e387SClaudio Imbrenda 			if (r)
24074036e387SClaudio Imbrenda 				r = -EFAULT;
24084036e387SClaudio Imbrenda 		}
24094036e387SClaudio Imbrenda 		break;
24104036e387SClaudio Imbrenda 	}
24114036e387SClaudio Imbrenda 	case KVM_S390_SET_CMMA_BITS: {
24124036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
24134036e387SClaudio Imbrenda 
24144036e387SClaudio Imbrenda 		r = -EFAULT;
24154036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
24164036e387SClaudio Imbrenda 			break;
24171de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
24184036e387SClaudio Imbrenda 		r = kvm_s390_set_cmma_bits(kvm, &args);
24191de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
24204036e387SClaudio Imbrenda 		break;
24214036e387SClaudio Imbrenda 	}
2422*29b40f10SJanosch Frank 	case KVM_S390_PV_COMMAND: {
2423*29b40f10SJanosch Frank 		struct kvm_pv_cmd args;
2424*29b40f10SJanosch Frank 
2425*29b40f10SJanosch Frank 		r = 0;
2426*29b40f10SJanosch Frank 		if (!is_prot_virt_host()) {
2427*29b40f10SJanosch Frank 			r = -EINVAL;
2428*29b40f10SJanosch Frank 			break;
2429*29b40f10SJanosch Frank 		}
2430*29b40f10SJanosch Frank 		if (copy_from_user(&args, argp, sizeof(args))) {
2431*29b40f10SJanosch Frank 			r = -EFAULT;
2432*29b40f10SJanosch Frank 			break;
2433*29b40f10SJanosch Frank 		}
2434*29b40f10SJanosch Frank 		if (args.flags) {
2435*29b40f10SJanosch Frank 			r = -EINVAL;
2436*29b40f10SJanosch Frank 			break;
2437*29b40f10SJanosch Frank 		}
2438*29b40f10SJanosch Frank 		mutex_lock(&kvm->lock);
2439*29b40f10SJanosch Frank 		r = kvm_s390_handle_pv(kvm, &args);
2440*29b40f10SJanosch Frank 		mutex_unlock(&kvm->lock);
2441*29b40f10SJanosch Frank 		if (copy_to_user(argp, &args, sizeof(args))) {
2442*29b40f10SJanosch Frank 			r = -EFAULT;
2443*29b40f10SJanosch Frank 			break;
2444*29b40f10SJanosch Frank 		}
2445*29b40f10SJanosch Frank 		break;
2446*29b40f10SJanosch Frank 	}
2447b0c632dbSHeiko Carstens 	default:
2448367e1319SAvi Kivity 		r = -ENOTTY;
2449b0c632dbSHeiko Carstens 	}
2450b0c632dbSHeiko Carstens 
2451b0c632dbSHeiko Carstens 	return r;
2452b0c632dbSHeiko Carstens }
2453b0c632dbSHeiko Carstens 
245445c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
245545c9b47cSTony Krowiak {
2456e585b24aSTony Krowiak 	struct ap_config_info info;
245745c9b47cSTony Krowiak 
2458e585b24aSTony Krowiak 	if (ap_instructions_available()) {
2459e585b24aSTony Krowiak 		if (ap_qci(&info) == 0)
2460e585b24aSTony Krowiak 			return info.apxa;
246145c9b47cSTony Krowiak 	}
246245c9b47cSTony Krowiak 
246345c9b47cSTony Krowiak 	return 0;
246445c9b47cSTony Krowiak }
246545c9b47cSTony Krowiak 
2466e585b24aSTony Krowiak /*
2467e585b24aSTony Krowiak  * The format of the crypto control block (CRYCB) is specified in the 3 low
2468e585b24aSTony Krowiak  * order bits of the CRYCB designation (CRYCBD) field as follows:
2469e585b24aSTony Krowiak  * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
2470e585b24aSTony Krowiak  *	     AP extended addressing (APXA) facility are installed.
2471e585b24aSTony Krowiak  * Format 1: The APXA facility is not installed but the MSAX3 facility is.
2472e585b24aSTony Krowiak  * Format 2: Both the APXA and MSAX3 facilities are installed
2473e585b24aSTony Krowiak  */
247445c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
247545c9b47cSTony Krowiak {
247645c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
247745c9b47cSTony Krowiak 
2478e585b24aSTony Krowiak 	/* Clear the CRYCB format bits - i.e., set format 0 by default */
2479e585b24aSTony Krowiak 	kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
2480e585b24aSTony Krowiak 
2481e585b24aSTony Krowiak 	/* Check whether MSAX3 is installed */
2482e585b24aSTony Krowiak 	if (!test_kvm_facility(kvm, 76))
2483e585b24aSTony Krowiak 		return;
2484e585b24aSTony Krowiak 
248545c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
248645c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
248745c9b47cSTony Krowiak 	else
248845c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
248945c9b47cSTony Krowiak }
249045c9b47cSTony Krowiak 
24910e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
24920e237e44SPierre Morel 			       unsigned long *aqm, unsigned long *adm)
24930e237e44SPierre Morel {
24940e237e44SPierre Morel 	struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
24950e237e44SPierre Morel 
24960e237e44SPierre Morel 	mutex_lock(&kvm->lock);
24970e237e44SPierre Morel 	kvm_s390_vcpu_block_all(kvm);
24980e237e44SPierre Morel 
24990e237e44SPierre Morel 	switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
25000e237e44SPierre Morel 	case CRYCB_FORMAT2: /* APCB1 use 256 bits */
25010e237e44SPierre Morel 		memcpy(crycb->apcb1.apm, apm, 32);
25020e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
25030e237e44SPierre Morel 			 apm[0], apm[1], apm[2], apm[3]);
25040e237e44SPierre Morel 		memcpy(crycb->apcb1.aqm, aqm, 32);
25050e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
25060e237e44SPierre Morel 			 aqm[0], aqm[1], aqm[2], aqm[3]);
25070e237e44SPierre Morel 		memcpy(crycb->apcb1.adm, adm, 32);
25080e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
25090e237e44SPierre Morel 			 adm[0], adm[1], adm[2], adm[3]);
25100e237e44SPierre Morel 		break;
25110e237e44SPierre Morel 	case CRYCB_FORMAT1:
25120e237e44SPierre Morel 	case CRYCB_FORMAT0: /* Fall through both use APCB0 */
25130e237e44SPierre Morel 		memcpy(crycb->apcb0.apm, apm, 8);
25140e237e44SPierre Morel 		memcpy(crycb->apcb0.aqm, aqm, 2);
25150e237e44SPierre Morel 		memcpy(crycb->apcb0.adm, adm, 2);
25160e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
25170e237e44SPierre Morel 			 apm[0], *((unsigned short *)aqm),
25180e237e44SPierre Morel 			 *((unsigned short *)adm));
25190e237e44SPierre Morel 		break;
25200e237e44SPierre Morel 	default:	/* Can not happen */
25210e237e44SPierre Morel 		break;
25220e237e44SPierre Morel 	}
25230e237e44SPierre Morel 
25240e237e44SPierre Morel 	/* recreate the shadow crycb for each vcpu */
25250e237e44SPierre Morel 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
25260e237e44SPierre Morel 	kvm_s390_vcpu_unblock_all(kvm);
25270e237e44SPierre Morel 	mutex_unlock(&kvm->lock);
25280e237e44SPierre Morel }
25290e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
25300e237e44SPierre Morel 
253142104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm)
253242104598STony Krowiak {
253342104598STony Krowiak 	mutex_lock(&kvm->lock);
253442104598STony Krowiak 	kvm_s390_vcpu_block_all(kvm);
253542104598STony Krowiak 
253642104598STony Krowiak 	memset(&kvm->arch.crypto.crycb->apcb0, 0,
253742104598STony Krowiak 	       sizeof(kvm->arch.crypto.crycb->apcb0));
253842104598STony Krowiak 	memset(&kvm->arch.crypto.crycb->apcb1, 0,
253942104598STony Krowiak 	       sizeof(kvm->arch.crypto.crycb->apcb1));
254042104598STony Krowiak 
25410e237e44SPierre Morel 	VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
25426cc571b1SPierre Morel 	/* recreate the shadow crycb for each vcpu */
25436cc571b1SPierre Morel 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
254442104598STony Krowiak 	kvm_s390_vcpu_unblock_all(kvm);
254542104598STony Krowiak 	mutex_unlock(&kvm->lock);
254642104598STony Krowiak }
254742104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
254842104598STony Krowiak 
25499bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
25509d8d5786SMichael Mueller {
25519bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
25529bb0ec09SDavid Hildenbrand 
25539bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
25549bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
25559bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
25569d8d5786SMichael Mueller }
25579d8d5786SMichael Mueller 
2558c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
25595102ee87STony Krowiak {
2560c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
256145c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
25625102ee87STony Krowiak 
2563e585b24aSTony Krowiak 	if (!test_kvm_facility(kvm, 76))
2564e585b24aSTony Krowiak 		return;
2565e585b24aSTony Krowiak 
2566ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
2567ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
2568ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
2569ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
2570ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
2571ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
2572ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
25735102ee87STony Krowiak }
25745102ee87STony Krowiak 
25757d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
25767d43bafcSEugene (jno) Dvurechenski {
25777d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
25785e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
25797d43bafcSEugene (jno) Dvurechenski 	else
25807d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
25817d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
25827d43bafcSEugene (jno) Dvurechenski }
25837d43bafcSEugene (jno) Dvurechenski 
2584e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
2585b0c632dbSHeiko Carstens {
258676a6dd72SDavid Hildenbrand 	gfp_t alloc_flags = GFP_KERNEL;
25879d8d5786SMichael Mueller 	int i, rc;
2588b0c632dbSHeiko Carstens 	char debug_name[16];
2589f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
2590b0c632dbSHeiko Carstens 
2591e08b9637SCarsten Otte 	rc = -EINVAL;
2592e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
2593e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
2594e08b9637SCarsten Otte 		goto out_err;
2595e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
2596e08b9637SCarsten Otte 		goto out_err;
2597e08b9637SCarsten Otte #else
2598e08b9637SCarsten Otte 	if (type)
2599e08b9637SCarsten Otte 		goto out_err;
2600e08b9637SCarsten Otte #endif
2601e08b9637SCarsten Otte 
2602b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
2603b0c632dbSHeiko Carstens 	if (rc)
2604d89f5effSJan Kiszka 		goto out_err;
2605b0c632dbSHeiko Carstens 
2606b290411aSCarsten Otte 	rc = -ENOMEM;
2607b290411aSCarsten Otte 
260876a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
260976a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
26105e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
26119ac96d75SDavid Hildenbrand 	/* start with basic SCA */
261276a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
2613b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
2614d89f5effSJan Kiszka 		goto out_err;
26150d9ce162SJunaid Shahid 	mutex_lock(&kvm_lock);
2616c5c2c393SDavid Hildenbrand 	sca_offset += 16;
2617bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
2618c5c2c393SDavid Hildenbrand 		sca_offset = 0;
2619bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
2620bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
26210d9ce162SJunaid Shahid 	mutex_unlock(&kvm_lock);
2622b0c632dbSHeiko Carstens 
2623b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
2624b0c632dbSHeiko Carstens 
26251cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
2626b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
262740f5b735SDominik Dingel 		goto out_err;
2628b0c632dbSHeiko Carstens 
262919114bebSMichael Mueller 	BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
2630c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
2631c54f0d6aSDavid Hildenbrand 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
2632c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
263340f5b735SDominik Dingel 		goto out_err;
26349d8d5786SMichael Mueller 
263525c84dbaSMichael Mueller 	kvm->arch.sie_page2->kvm = kvm;
2636c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
2637c3b9e3e1SChristian Borntraeger 
2638c3b9e3e1SChristian Borntraeger 	for (i = 0; i < kvm_s390_fac_size(); i++) {
2639c3b9e3e1SChristian Borntraeger 		kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] &
2640c3b9e3e1SChristian Borntraeger 					      (kvm_s390_fac_base[i] |
2641c3b9e3e1SChristian Borntraeger 					       kvm_s390_fac_ext[i]);
2642c3b9e3e1SChristian Borntraeger 		kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] &
2643c3b9e3e1SChristian Borntraeger 					      kvm_s390_fac_base[i];
2644c3b9e3e1SChristian Borntraeger 	}
2645346fa2f8SChristian Borntraeger 	kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
2646981467c9SMichael Mueller 
26471935222dSDavid Hildenbrand 	/* we are always in czam mode - even on pre z14 machines */
26481935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_mask, 138);
26491935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_list, 138);
26501935222dSDavid Hildenbrand 	/* we emulate STHYI in kvm */
265195ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
265295ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
26531bab1c02SClaudio Imbrenda 	if (MACHINE_HAS_TLB_GUEST) {
26541bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_mask, 147);
26551bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_list, 147);
26561bab1c02SClaudio Imbrenda 	}
265795ca2cb5SJanosch Frank 
265805f31e3bSPierre Morel 	if (css_general_characteristics.aiv && test_facility(65))
265905f31e3bSPierre Morel 		set_kvm_facility(kvm->arch.model.fac_mask, 65);
266005f31e3bSPierre Morel 
26619bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
266237c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
26639d8d5786SMichael Mueller 
2664c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
26655102ee87STony Krowiak 
266651978393SFei Li 	mutex_init(&kvm->arch.float_int.ais_lock);
2667ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
26686d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
26696d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
26708a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
2671a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
2672ba5c1e9bSCarsten Otte 
2673b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
267478f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
2675b0c632dbSHeiko Carstens 
2676e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
2677e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
2678a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
2679e08b9637SCarsten Otte 	} else {
268032e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
2681ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = TASK_SIZE_MAX;
268232e6b236SGuenther Hutzl 		else
2683ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
268432e6b236SGuenther Hutzl 						    sclp.hamax + 1);
26856ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
2686598841caSCarsten Otte 		if (!kvm->arch.gmap)
268740f5b735SDominik Dingel 			goto out_err;
26882c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
268924eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
2690e08b9637SCarsten Otte 	}
2691fa6b7fe9SCornelia Huck 
2692c9f0a2b8SJanosch Frank 	kvm->arch.use_pfmfi = sclp.has_pfmfi;
269355531b74SJanosch Frank 	kvm->arch.use_skf = sclp.has_skey;
26948ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
2695a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
2696d7c5cb01SMichael Mueller 	kvm_s390_gisa_init(kvm);
26978335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
26988ad35755SDavid Hildenbrand 
2699d89f5effSJan Kiszka 	return 0;
2700d89f5effSJan Kiszka out_err:
2701c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
270240f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
27037d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
270478f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
2705d89f5effSJan Kiszka 	return rc;
2706b0c632dbSHeiko Carstens }
2707b0c632dbSHeiko Carstens 
2708d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
2709d329c035SChristian Borntraeger {
2710*29b40f10SJanosch Frank 	u16 rc, rrc;
2711*29b40f10SJanosch Frank 
2712d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2713ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
271467335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
27153c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
2716bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
2717a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
271827e0393fSCarsten Otte 
271927e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
27206ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
272127e0393fSCarsten Otte 
2722e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
2723b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
2724*29b40f10SJanosch Frank 	/* We can not hold the vcpu mutex here, we are already dying */
2725*29b40f10SJanosch Frank 	if (kvm_s390_pv_cpu_get_handle(vcpu))
2726*29b40f10SJanosch Frank 		kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
2727d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2728d329c035SChristian Borntraeger }
2729d329c035SChristian Borntraeger 
2730d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
2731d329c035SChristian Borntraeger {
2732d329c035SChristian Borntraeger 	unsigned int i;
2733988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
2734d329c035SChristian Borntraeger 
2735988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
27364543bdc0SSean Christopherson 		kvm_vcpu_destroy(vcpu);
2737988a2caeSGleb Natapov 
2738988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
2739988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2740d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
2741988a2caeSGleb Natapov 
2742988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
2743988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
2744d329c035SChristian Borntraeger }
2745d329c035SChristian Borntraeger 
2746b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
2747b0c632dbSHeiko Carstens {
2748*29b40f10SJanosch Frank 	u16 rc, rrc;
2749*29b40f10SJanosch Frank 
2750d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
27517d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
2752d7c5cb01SMichael Mueller 	kvm_s390_gisa_destroy(kvm);
2753*29b40f10SJanosch Frank 	/*
2754*29b40f10SJanosch Frank 	 * We are already at the end of life and kvm->lock is not taken.
2755*29b40f10SJanosch Frank 	 * This is ok as the file descriptor is closed by now and nobody
2756*29b40f10SJanosch Frank 	 * can mess with the pv state. To avoid lockdep_assert_held from
2757*29b40f10SJanosch Frank 	 * complaining we do not use kvm_s390_pv_is_protected.
2758*29b40f10SJanosch Frank 	 */
2759*29b40f10SJanosch Frank 	if (kvm_s390_pv_get_handle(kvm))
2760*29b40f10SJanosch Frank 		kvm_s390_pv_deinit_vm(kvm, &rc, &rrc);
2761*29b40f10SJanosch Frank 	debug_unregister(kvm->arch.dbf);
2762c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
276327e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
27646ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
2765841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
276667335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
2767a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
27688335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2769b0c632dbSHeiko Carstens }
2770b0c632dbSHeiko Carstens 
2771b0c632dbSHeiko Carstens /* Section: vcpu related */
2772dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2773b0c632dbSHeiko Carstens {
27746ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
277527e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
277627e0393fSCarsten Otte 		return -ENOMEM;
27772c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
2778dafd032aSDominik Dingel 
277927e0393fSCarsten Otte 	return 0;
278027e0393fSCarsten Otte }
278127e0393fSCarsten Otte 
2782a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2783a6e2f683SEugene (jno) Dvurechenski {
2784a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
2785a6940674SDavid Hildenbrand 		return;
27865e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
27877d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
27887d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
27897d43bafcSEugene (jno) Dvurechenski 
27907d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
27917d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
27927d43bafcSEugene (jno) Dvurechenski 	} else {
2793bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2794a6e2f683SEugene (jno) Dvurechenski 
2795a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2796a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
2797a6e2f683SEugene (jno) Dvurechenski 	}
27985e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
27997d43bafcSEugene (jno) Dvurechenski }
2800a6e2f683SEugene (jno) Dvurechenski 
2801eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2802a6e2f683SEugene (jno) Dvurechenski {
2803a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2804a6940674SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2805a6940674SDavid Hildenbrand 
2806a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
2807a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2808a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2809f07afa04SDavid Hildenbrand 		return;
2810a6940674SDavid Hildenbrand 	}
2811eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
2812eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
2813eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
28147d43bafcSEugene (jno) Dvurechenski 
2815eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
28167d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
28177d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
28180c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2819eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
28207d43bafcSEugene (jno) Dvurechenski 	} else {
2821eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2822a6e2f683SEugene (jno) Dvurechenski 
2823eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2824a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2825a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2826eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2827a6e2f683SEugene (jno) Dvurechenski 	}
2828eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
28295e044315SEugene (jno) Dvurechenski }
28305e044315SEugene (jno) Dvurechenski 
28315e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
28325e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
28335e044315SEugene (jno) Dvurechenski {
28345e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
28355e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
28365e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
28375e044315SEugene (jno) Dvurechenski }
28385e044315SEugene (jno) Dvurechenski 
28395e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
28405e044315SEugene (jno) Dvurechenski {
28415e044315SEugene (jno) Dvurechenski 	int i;
28425e044315SEugene (jno) Dvurechenski 
28435e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
28445e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
28455e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
28465e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
28475e044315SEugene (jno) Dvurechenski }
28485e044315SEugene (jno) Dvurechenski 
28495e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
28505e044315SEugene (jno) Dvurechenski {
28515e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
28525e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
28535e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
28545e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
28555e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
28565e044315SEugene (jno) Dvurechenski 
2857*29b40f10SJanosch Frank 	if (kvm->arch.use_esca)
2858*29b40f10SJanosch Frank 		return 0;
2859*29b40f10SJanosch Frank 
28605e044315SEugene (jno) Dvurechenski 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
28615e044315SEugene (jno) Dvurechenski 	if (!new_sca)
28625e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
28635e044315SEugene (jno) Dvurechenski 
28645e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
28655e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
28665e044315SEugene (jno) Dvurechenski 
28675e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
28685e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
28695e044315SEugene (jno) Dvurechenski 
28705e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
28715e044315SEugene (jno) Dvurechenski 
28725e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
28735e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
28745e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
28750c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
28765e044315SEugene (jno) Dvurechenski 	}
28775e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
28785e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
28795e044315SEugene (jno) Dvurechenski 
28805e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
28815e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
28825e044315SEugene (jno) Dvurechenski 
28835e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
28845e044315SEugene (jno) Dvurechenski 
28858335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
28868335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
28875e044315SEugene (jno) Dvurechenski 	return 0;
28887d43bafcSEugene (jno) Dvurechenski }
2889a6e2f683SEugene (jno) Dvurechenski 
2890a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2891a6e2f683SEugene (jno) Dvurechenski {
28925e044315SEugene (jno) Dvurechenski 	int rc;
28935e044315SEugene (jno) Dvurechenski 
2894a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2895a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
2896a6940674SDavid Hildenbrand 			return true;
2897a6940674SDavid Hildenbrand 		return false;
2898a6940674SDavid Hildenbrand 	}
28995e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
29005e044315SEugene (jno) Dvurechenski 		return true;
290176a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
29025e044315SEugene (jno) Dvurechenski 		return false;
29035e044315SEugene (jno) Dvurechenski 
29045e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
29055e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
29065e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
29075e044315SEugene (jno) Dvurechenski 
29085e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
2909a6e2f683SEugene (jno) Dvurechenski }
2910a6e2f683SEugene (jno) Dvurechenski 
2911db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2912db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2913db0758b2SDavid Hildenbrand {
2914db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
29159c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2916db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
29179c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2918db0758b2SDavid Hildenbrand }
2919db0758b2SDavid Hildenbrand 
2920db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2921db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2922db0758b2SDavid Hildenbrand {
2923db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
29249c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2925db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2926db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
29279c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2928db0758b2SDavid Hildenbrand }
2929db0758b2SDavid Hildenbrand 
2930db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2931db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2932db0758b2SDavid Hildenbrand {
2933db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
2934db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
2935db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
2936db0758b2SDavid Hildenbrand }
2937db0758b2SDavid Hildenbrand 
2938db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2939db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2940db0758b2SDavid Hildenbrand {
2941db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
2942db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
2943db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
2944db0758b2SDavid Hildenbrand }
2945db0758b2SDavid Hildenbrand 
2946db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2947db0758b2SDavid Hildenbrand {
2948db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2949db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
2950db0758b2SDavid Hildenbrand 	preempt_enable();
2951db0758b2SDavid Hildenbrand }
2952db0758b2SDavid Hildenbrand 
2953db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2954db0758b2SDavid Hildenbrand {
2955db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2956db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
2957db0758b2SDavid Hildenbrand 	preempt_enable();
2958db0758b2SDavid Hildenbrand }
2959db0758b2SDavid Hildenbrand 
29604287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
29614287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
29624287f247SDavid Hildenbrand {
2963db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
29649c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2965db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
2966db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
29674287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
29689c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2969db0758b2SDavid Hildenbrand 	preempt_enable();
29704287f247SDavid Hildenbrand }
29714287f247SDavid Hildenbrand 
2972db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
29734287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
29744287f247SDavid Hildenbrand {
29759c23a131SDavid Hildenbrand 	unsigned int seq;
2976db0758b2SDavid Hildenbrand 	__u64 value;
2977db0758b2SDavid Hildenbrand 
2978db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
29794287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
2980db0758b2SDavid Hildenbrand 
29819c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
29829c23a131SDavid Hildenbrand 	do {
29839c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
29849c23a131SDavid Hildenbrand 		/*
29859c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
29869c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
29879c23a131SDavid Hildenbrand 		 */
29889c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
2989db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
29909c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
29919c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
2992db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
29939c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
29949c23a131SDavid Hildenbrand 	preempt_enable();
2995db0758b2SDavid Hildenbrand 	return value;
29964287f247SDavid Hildenbrand }
29974287f247SDavid Hildenbrand 
2998b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2999b0c632dbSHeiko Carstens {
30009977e886SHendrik Brueckner 
300137d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
3002ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
30035ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3004db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
300501a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
3006b0c632dbSHeiko Carstens }
3007b0c632dbSHeiko Carstens 
3008b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3009b0c632dbSHeiko Carstens {
301001a745acSDavid Hildenbrand 	vcpu->cpu = -1;
30115ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3012db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
30139daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
301437d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
301537d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
30169977e886SHendrik Brueckner 
3017b0c632dbSHeiko Carstens }
3018b0c632dbSHeiko Carstens 
301931928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
302042897d86SMarcelo Tosatti {
302172f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
3022fdf03650SFan Zhang 	preempt_disable();
302372f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
3024d16b52cbSDavid Hildenbrand 	vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
3025fdf03650SFan Zhang 	preempt_enable();
302672f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
302725508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
3028dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
3029eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
303025508824SDavid Hildenbrand 	}
30316502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
30326502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
303337d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
303437d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
303542897d86SMarcelo Tosatti }
303642897d86SMarcelo Tosatti 
30378ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
30388ec2fa52SChristian Borntraeger {
30398ec2fa52SChristian Borntraeger 	if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
30408ec2fa52SChristian Borntraeger 	    test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
30418ec2fa52SChristian Borntraeger 		return true;
30428ec2fa52SChristian Borntraeger 	return false;
30438ec2fa52SChristian Borntraeger }
30448ec2fa52SChristian Borntraeger 
30458ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm)
30468ec2fa52SChristian Borntraeger {
30478ec2fa52SChristian Borntraeger 	/* At least one ECC subfunction must be present */
30488ec2fa52SChristian Borntraeger 	return kvm_has_pckmo_subfunc(kvm, 32) ||
30498ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 33) ||
30508ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 34) ||
30518ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 40) ||
30528ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 41);
30538ec2fa52SChristian Borntraeger 
30548ec2fa52SChristian Borntraeger }
30558ec2fa52SChristian Borntraeger 
30565102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
30575102ee87STony Krowiak {
3058e585b24aSTony Krowiak 	/*
3059e585b24aSTony Krowiak 	 * If the AP instructions are not being interpreted and the MSAX3
3060e585b24aSTony Krowiak 	 * facility is not configured for the guest, there is nothing to set up.
3061e585b24aSTony Krowiak 	 */
3062e585b24aSTony Krowiak 	if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
30635102ee87STony Krowiak 		return;
30645102ee87STony Krowiak 
3065e585b24aSTony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
3066a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
306737940fb0STony Krowiak 	vcpu->arch.sie_block->eca &= ~ECA_APIE;
30688ec2fa52SChristian Borntraeger 	vcpu->arch.sie_block->ecd &= ~ECD_ECC;
3069a374e892STony Krowiak 
3070e585b24aSTony Krowiak 	if (vcpu->kvm->arch.crypto.apie)
3071e585b24aSTony Krowiak 		vcpu->arch.sie_block->eca |= ECA_APIE;
3072e585b24aSTony Krowiak 
3073e585b24aSTony Krowiak 	/* Set up protected key support */
30748ec2fa52SChristian Borntraeger 	if (vcpu->kvm->arch.crypto.aes_kw) {
3075a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
30768ec2fa52SChristian Borntraeger 		/* ecc is also wrapped with AES key */
30778ec2fa52SChristian Borntraeger 		if (kvm_has_pckmo_ecc(vcpu->kvm))
30788ec2fa52SChristian Borntraeger 			vcpu->arch.sie_block->ecd |= ECD_ECC;
30798ec2fa52SChristian Borntraeger 	}
30808ec2fa52SChristian Borntraeger 
3081a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
3082a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
30835102ee87STony Krowiak }
30845102ee87STony Krowiak 
3085b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
3086b31605c1SDominik Dingel {
3087b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
3088b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
3089b31605c1SDominik Dingel }
3090b31605c1SDominik Dingel 
3091b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
3092b31605c1SDominik Dingel {
3093b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
3094b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
3095b31605c1SDominik Dingel 		return -ENOMEM;
3096b31605c1SDominik Dingel 	return 0;
3097b31605c1SDominik Dingel }
3098b31605c1SDominik Dingel 
309991520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
310091520f1aSMichael Mueller {
310191520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
310291520f1aSMichael Mueller 
310391520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
310480bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
3105c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
310691520f1aSMichael Mueller }
310791520f1aSMichael Mueller 
3108ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
3109ff72bb55SSean Christopherson {
3110b31605c1SDominik Dingel 	int rc = 0;
3111*29b40f10SJanosch Frank 	u16 uvrc, uvrrc;
3112b31288faSKonstantin Weitz 
31139e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
31149e6dabefSCornelia Huck 						    CPUSTAT_SM |
3115a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
3116a4a4f191SGuenther Hutzl 
311753df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
3118ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
311953df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
3120ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
3121a4a4f191SGuenther Hutzl 
312291520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
312391520f1aSMichael Mueller 
3124bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
3125bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
31260c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
3127bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
31280c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_SRSI;
3129f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
31300c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_TE;
31317feb6bb8SMichael Mueller 
3132c9f0a2b8SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
31330c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
3134cd1836f5SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 130))
31350c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
31360c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
313748ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
31380c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_CEI;
313911ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
31400c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_IB;
314137c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
31420c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SII;
314337c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
31440c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SIGPI;
314518280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
31460c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_VX;
31470c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
314813211ea7SEric Farman 	}
31498fa1696eSCollin L. Walling 	if (test_kvm_facility(vcpu->kvm, 139))
31508fa1696eSCollin L. Walling 		vcpu->arch.sie_block->ecd |= ECD_MEF;
3151a3da7b4aSChristian Borntraeger 	if (test_kvm_facility(vcpu->kvm, 156))
3152a3da7b4aSChristian Borntraeger 		vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
3153d7c5cb01SMichael Mueller 	if (vcpu->arch.sie_block->gd) {
3154d7c5cb01SMichael Mueller 		vcpu->arch.sie_block->eca |= ECA_AIV;
3155d7c5cb01SMichael Mueller 		VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3156d7c5cb01SMichael Mueller 			   vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3157d7c5cb01SMichael Mueller 	}
31584e0b1ab7SFan Zhang 	vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
31594e0b1ab7SFan Zhang 					| SDNXC;
3160c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
3161730cd632SFarhan Ali 
3162730cd632SFarhan Ali 	if (sclp.has_kss)
3163ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
3164730cd632SFarhan Ali 	else
3165492d8642SThomas Huth 		vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
31665a5e6536SMatthew Rosato 
3167e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
3168b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
3169b31605c1SDominik Dingel 		if (rc)
3170b31605c1SDominik Dingel 			return rc;
3171b31288faSKonstantin Weitz 	}
31720ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3173ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
31749d8d5786SMichael Mueller 
317567d49d52SCollin Walling 	vcpu->arch.sie_block->hpid = HPID_KVM;
317667d49d52SCollin Walling 
31775102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
31785102ee87STony Krowiak 
3179*29b40f10SJanosch Frank 	mutex_lock(&vcpu->kvm->lock);
3180*29b40f10SJanosch Frank 	if (kvm_s390_pv_is_protected(vcpu->kvm)) {
3181*29b40f10SJanosch Frank 		rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
3182*29b40f10SJanosch Frank 		if (rc)
3183*29b40f10SJanosch Frank 			kvm_s390_vcpu_unsetup_cmma(vcpu);
3184*29b40f10SJanosch Frank 	}
3185*29b40f10SJanosch Frank 	mutex_unlock(&vcpu->kvm->lock);
3186*29b40f10SJanosch Frank 
3187b31605c1SDominik Dingel 	return rc;
3188b0c632dbSHeiko Carstens }
3189b0c632dbSHeiko Carstens 
3190897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3191897cc38eSSean Christopherson {
3192897cc38eSSean Christopherson 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
3193897cc38eSSean Christopherson 		return -EINVAL;
3194897cc38eSSean Christopherson 	return 0;
3195897cc38eSSean Christopherson }
3196897cc38eSSean Christopherson 
3197e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3198b0c632dbSHeiko Carstens {
31997feb6bb8SMichael Mueller 	struct sie_page *sie_page;
3200897cc38eSSean Christopherson 	int rc;
32014d47555aSCarsten Otte 
3202da72ca4dSQingFeng Hao 	BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
32037feb6bb8SMichael Mueller 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
32047feb6bb8SMichael Mueller 	if (!sie_page)
3205e529ef66SSean Christopherson 		return -ENOMEM;
3206b0c632dbSHeiko Carstens 
32077feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
32087feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
32097feb6bb8SMichael Mueller 
3210efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
3211efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
3212efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
3213efed1104SDavid Hildenbrand 
3214e529ef66SSean Christopherson 	vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
3215ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
3216e529ef66SSean Christopherson 	vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin;
32174b9f9525SMichael Mueller 	if (vcpu->arch.sie_block->gd && sclp.has_gisaf)
32184b9f9525SMichael Mueller 		vcpu->arch.sie_block->gd |= GISA_FORMAT1;
32199c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
3220ba5c1e9bSCarsten Otte 
3221321f8ee5SSean Christopherson 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3222321f8ee5SSean Christopherson 	kvm_clear_async_pf_completion_queue(vcpu);
3223321f8ee5SSean Christopherson 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
3224321f8ee5SSean Christopherson 				    KVM_SYNC_GPRS |
3225321f8ee5SSean Christopherson 				    KVM_SYNC_ACRS |
3226321f8ee5SSean Christopherson 				    KVM_SYNC_CRS |
3227321f8ee5SSean Christopherson 				    KVM_SYNC_ARCH0 |
3228321f8ee5SSean Christopherson 				    KVM_SYNC_PFAULT;
3229321f8ee5SSean Christopherson 	kvm_s390_set_prefix(vcpu, 0);
3230321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 64))
3231321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
3232321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 82))
3233321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
3234321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 133))
3235321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
3236321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 156))
3237321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
3238321f8ee5SSean Christopherson 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
3239321f8ee5SSean Christopherson 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
3240321f8ee5SSean Christopherson 	 */
3241321f8ee5SSean Christopherson 	if (MACHINE_HAS_VX)
3242321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
3243321f8ee5SSean Christopherson 	else
3244321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
3245321f8ee5SSean Christopherson 
3246321f8ee5SSean Christopherson 	if (kvm_is_ucontrol(vcpu->kvm)) {
3247321f8ee5SSean Christopherson 		rc = __kvm_ucontrol_vcpu_init(vcpu);
3248321f8ee5SSean Christopherson 		if (rc)
3249a2017f17SSean Christopherson 			goto out_free_sie_block;
3250321f8ee5SSean Christopherson 	}
3251321f8ee5SSean Christopherson 
3252e529ef66SSean Christopherson 	VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
3253e529ef66SSean Christopherson 		 vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3254e529ef66SSean Christopherson 	trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3255b0c632dbSHeiko Carstens 
3256ff72bb55SSean Christopherson 	rc = kvm_s390_vcpu_setup(vcpu);
3257ff72bb55SSean Christopherson 	if (rc)
3258ff72bb55SSean Christopherson 		goto out_ucontrol_uninit;
3259e529ef66SSean Christopherson 	return 0;
3260e529ef66SSean Christopherson 
3261ff72bb55SSean Christopherson out_ucontrol_uninit:
3262ff72bb55SSean Christopherson 	if (kvm_is_ucontrol(vcpu->kvm))
3263ff72bb55SSean Christopherson 		gmap_remove(vcpu->arch.gmap);
32647b06bf2fSWei Yongjun out_free_sie_block:
32657b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
3266e529ef66SSean Christopherson 	return rc;
3267b0c632dbSHeiko Carstens }
3268b0c632dbSHeiko Carstens 
3269b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3270b0c632dbSHeiko Carstens {
32719a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
3272b0c632dbSHeiko Carstens }
3273b0c632dbSHeiko Carstens 
3274199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
3275199b5763SLongpeng(Mike) {
32760546c63dSLongpeng(Mike) 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
3277199b5763SLongpeng(Mike) }
3278199b5763SLongpeng(Mike) 
327927406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
328049b99e1eSChristian Borntraeger {
3281805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
328261a6df54SDavid Hildenbrand 	exit_sie(vcpu);
328349b99e1eSChristian Borntraeger }
328449b99e1eSChristian Borntraeger 
328527406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
328649b99e1eSChristian Borntraeger {
3287805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
328849b99e1eSChristian Borntraeger }
328949b99e1eSChristian Borntraeger 
32908e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
32918e236546SChristian Borntraeger {
3292805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
329361a6df54SDavid Hildenbrand 	exit_sie(vcpu);
32948e236546SChristian Borntraeger }
32958e236546SChristian Borntraeger 
32969ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
32979ea59728SDavid Hildenbrand {
32989ea59728SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->prog20) &
32999ea59728SDavid Hildenbrand 	       (PROG_BLOCK_SIE | PROG_REQUEST);
33009ea59728SDavid Hildenbrand }
33019ea59728SDavid Hildenbrand 
33028e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
33038e236546SChristian Borntraeger {
33049bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
33058e236546SChristian Borntraeger }
33068e236546SChristian Borntraeger 
330749b99e1eSChristian Borntraeger /*
33089ea59728SDavid Hildenbrand  * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
330949b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
331049b99e1eSChristian Borntraeger  * return immediately. */
331149b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
331249b99e1eSChristian Borntraeger {
3313ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
33149ea59728SDavid Hildenbrand 	kvm_s390_vsie_kick(vcpu);
331549b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
331649b99e1eSChristian Borntraeger 		cpu_relax();
331749b99e1eSChristian Borntraeger }
331849b99e1eSChristian Borntraeger 
33198e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
33208e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
332149b99e1eSChristian Borntraeger {
33228e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
33238e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
332449b99e1eSChristian Borntraeger }
332549b99e1eSChristian Borntraeger 
3326414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
3327414d3b07SMartin Schwidefsky 			      unsigned long end)
33282c70fe44SChristian Borntraeger {
33292c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
33302c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
3331414d3b07SMartin Schwidefsky 	unsigned long prefix;
3332414d3b07SMartin Schwidefsky 	int i;
33332c70fe44SChristian Borntraeger 
333465d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
333565d0b0d4SDavid Hildenbrand 		return;
3336414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
3337414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
3338414d3b07SMartin Schwidefsky 		return;
33392c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
33402c70fe44SChristian Borntraeger 		/* match against both prefix pages */
3341414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
3342414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
3343414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
3344414d3b07SMartin Schwidefsky 				   start, end);
33458e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
33462c70fe44SChristian Borntraeger 		}
33472c70fe44SChristian Borntraeger 	}
33482c70fe44SChristian Borntraeger }
33492c70fe44SChristian Borntraeger 
33508b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
33518b905d28SChristian Borntraeger {
33528b905d28SChristian Borntraeger 	/* do not poll with more than halt_poll_max_steal percent of steal time */
33538b905d28SChristian Borntraeger 	if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >=
33548b905d28SChristian Borntraeger 	    halt_poll_max_steal) {
33558b905d28SChristian Borntraeger 		vcpu->stat.halt_no_poll_steal++;
33568b905d28SChristian Borntraeger 		return true;
33578b905d28SChristian Borntraeger 	}
33588b905d28SChristian Borntraeger 	return false;
33598b905d28SChristian Borntraeger }
33608b905d28SChristian Borntraeger 
3361b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
3362b6d33834SChristoffer Dall {
3363b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
3364b6d33834SChristoffer Dall 	BUG();
3365b6d33834SChristoffer Dall 	return 0;
3366b6d33834SChristoffer Dall }
3367b6d33834SChristoffer Dall 
336814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
336914eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
337014eebd91SCarsten Otte {
337114eebd91SCarsten Otte 	int r = -EINVAL;
337214eebd91SCarsten Otte 
337314eebd91SCarsten Otte 	switch (reg->id) {
337429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
337529b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
337629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
337729b7c71bSCarsten Otte 		break;
337829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
337929b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
338029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
338129b7c71bSCarsten Otte 		break;
338246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
33834287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
338446a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
338546a6dd1cSJason J. herne 		break;
338646a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
338746a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
338846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
338946a6dd1cSJason J. herne 		break;
3390536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
3391536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
3392536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3393536336c2SDominik Dingel 		break;
3394536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
3395536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
3396536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3397536336c2SDominik Dingel 		break;
3398536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
3399536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
3400536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3401536336c2SDominik Dingel 		break;
3402672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
3403672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
3404672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
3405672550fbSChristian Borntraeger 		break;
3406afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
3407afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
3408afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
3409afa45ff5SChristian Borntraeger 		break;
341014eebd91SCarsten Otte 	default:
341114eebd91SCarsten Otte 		break;
341214eebd91SCarsten Otte 	}
341314eebd91SCarsten Otte 
341414eebd91SCarsten Otte 	return r;
341514eebd91SCarsten Otte }
341614eebd91SCarsten Otte 
341714eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
341814eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
341914eebd91SCarsten Otte {
342014eebd91SCarsten Otte 	int r = -EINVAL;
34214287f247SDavid Hildenbrand 	__u64 val;
342214eebd91SCarsten Otte 
342314eebd91SCarsten Otte 	switch (reg->id) {
342429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
342529b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
342629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
342729b7c71bSCarsten Otte 		break;
342829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
342929b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
343029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
343129b7c71bSCarsten Otte 		break;
343246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
34334287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
34344287f247SDavid Hildenbrand 		if (!r)
34354287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
343646a6dd1cSJason J. herne 		break;
343746a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
343846a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
343946a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
344046a6dd1cSJason J. herne 		break;
3441536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
3442536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
3443536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
34449fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
34459fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
3446536336c2SDominik Dingel 		break;
3447536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
3448536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
3449536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3450536336c2SDominik Dingel 		break;
3451536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
3452536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
3453536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3454536336c2SDominik Dingel 		break;
3455672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
3456672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
3457672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
3458672550fbSChristian Borntraeger 		break;
3459afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
3460afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
3461afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
3462afa45ff5SChristian Borntraeger 		break;
346314eebd91SCarsten Otte 	default:
346414eebd91SCarsten Otte 		break;
346514eebd91SCarsten Otte 	}
346614eebd91SCarsten Otte 
346714eebd91SCarsten Otte 	return r;
346814eebd91SCarsten Otte }
3469b6d33834SChristoffer Dall 
34707de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
3471b0c632dbSHeiko Carstens {
34727de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
34737de3f142SJanosch Frank 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
34747de3f142SJanosch Frank 	memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
34757de3f142SJanosch Frank 
34767de3f142SJanosch Frank 	kvm_clear_async_pf_completion_queue(vcpu);
34777de3f142SJanosch Frank 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
34787de3f142SJanosch Frank 		kvm_s390_vcpu_stop(vcpu);
34797de3f142SJanosch Frank 	kvm_s390_clear_local_irqs(vcpu);
34807de3f142SJanosch Frank }
34817de3f142SJanosch Frank 
34827de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
34837de3f142SJanosch Frank {
34847de3f142SJanosch Frank 	/* Initial reset is a superset of the normal reset */
34857de3f142SJanosch Frank 	kvm_arch_vcpu_ioctl_normal_reset(vcpu);
34867de3f142SJanosch Frank 
34877de3f142SJanosch Frank 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
34887de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.mask = 0;
34897de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.addr = 0;
34907de3f142SJanosch Frank 	kvm_s390_set_prefix(vcpu, 0);
34917de3f142SJanosch Frank 	kvm_s390_set_cpu_timer(vcpu, 0);
34927de3f142SJanosch Frank 	vcpu->arch.sie_block->ckc = 0;
34937de3f142SJanosch Frank 	vcpu->arch.sie_block->todpr = 0;
34947de3f142SJanosch Frank 	memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
34957de3f142SJanosch Frank 	vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
34967de3f142SJanosch Frank 	vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
34977de3f142SJanosch Frank 	vcpu->run->s.regs.fpc = 0;
34987de3f142SJanosch Frank 	vcpu->arch.sie_block->gbea = 1;
34997de3f142SJanosch Frank 	vcpu->arch.sie_block->pp = 0;
35007de3f142SJanosch Frank 	vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
35017de3f142SJanosch Frank }
35027de3f142SJanosch Frank 
35037de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
35047de3f142SJanosch Frank {
35057de3f142SJanosch Frank 	struct kvm_sync_regs *regs = &vcpu->run->s.regs;
35067de3f142SJanosch Frank 
35077de3f142SJanosch Frank 	/* Clear reset is a superset of the initial reset */
35087de3f142SJanosch Frank 	kvm_arch_vcpu_ioctl_initial_reset(vcpu);
35097de3f142SJanosch Frank 
35107de3f142SJanosch Frank 	memset(&regs->gprs, 0, sizeof(regs->gprs));
35117de3f142SJanosch Frank 	memset(&regs->vrs, 0, sizeof(regs->vrs));
35127de3f142SJanosch Frank 	memset(&regs->acrs, 0, sizeof(regs->acrs));
35137de3f142SJanosch Frank 	memset(&regs->gscb, 0, sizeof(regs->gscb));
35147de3f142SJanosch Frank 
35157de3f142SJanosch Frank 	regs->etoken = 0;
35167de3f142SJanosch Frank 	regs->etoken_extension = 0;
3517b0c632dbSHeiko Carstens }
3518b0c632dbSHeiko Carstens 
3519b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3520b0c632dbSHeiko Carstens {
3521875656feSChristoffer Dall 	vcpu_load(vcpu);
35225a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
3523875656feSChristoffer Dall 	vcpu_put(vcpu);
3524b0c632dbSHeiko Carstens 	return 0;
3525b0c632dbSHeiko Carstens }
3526b0c632dbSHeiko Carstens 
3527b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3528b0c632dbSHeiko Carstens {
35291fc9b76bSChristoffer Dall 	vcpu_load(vcpu);
35305a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
35311fc9b76bSChristoffer Dall 	vcpu_put(vcpu);
3532b0c632dbSHeiko Carstens 	return 0;
3533b0c632dbSHeiko Carstens }
3534b0c632dbSHeiko Carstens 
3535b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3536b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
3537b0c632dbSHeiko Carstens {
3538b4ef9d4eSChristoffer Dall 	vcpu_load(vcpu);
3539b4ef9d4eSChristoffer Dall 
354059674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
3541b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
3542b4ef9d4eSChristoffer Dall 
3543b4ef9d4eSChristoffer Dall 	vcpu_put(vcpu);
3544b0c632dbSHeiko Carstens 	return 0;
3545b0c632dbSHeiko Carstens }
3546b0c632dbSHeiko Carstens 
3547b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3548b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
3549b0c632dbSHeiko Carstens {
3550bcdec41cSChristoffer Dall 	vcpu_load(vcpu);
3551bcdec41cSChristoffer Dall 
355259674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
3553b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
3554bcdec41cSChristoffer Dall 
3555bcdec41cSChristoffer Dall 	vcpu_put(vcpu);
3556b0c632dbSHeiko Carstens 	return 0;
3557b0c632dbSHeiko Carstens }
3558b0c632dbSHeiko Carstens 
3559b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3560b0c632dbSHeiko Carstens {
35616a96bc7fSChristoffer Dall 	int ret = 0;
35626a96bc7fSChristoffer Dall 
35636a96bc7fSChristoffer Dall 	vcpu_load(vcpu);
35646a96bc7fSChristoffer Dall 
35656a96bc7fSChristoffer Dall 	if (test_fp_ctl(fpu->fpc)) {
35666a96bc7fSChristoffer Dall 		ret = -EINVAL;
35676a96bc7fSChristoffer Dall 		goto out;
35686a96bc7fSChristoffer Dall 	}
3569e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
35709abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
3571a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
3572a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
35739abc2a08SDavid Hildenbrand 	else
3574a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
35756a96bc7fSChristoffer Dall 
35766a96bc7fSChristoffer Dall out:
35776a96bc7fSChristoffer Dall 	vcpu_put(vcpu);
35786a96bc7fSChristoffer Dall 	return ret;
3579b0c632dbSHeiko Carstens }
3580b0c632dbSHeiko Carstens 
3581b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3582b0c632dbSHeiko Carstens {
35831393123eSChristoffer Dall 	vcpu_load(vcpu);
35841393123eSChristoffer Dall 
35859abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
35869abc2a08SDavid Hildenbrand 	save_fpu_regs();
35879abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
3588a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
3589a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
35909abc2a08SDavid Hildenbrand 	else
3591a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
3592e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
35931393123eSChristoffer Dall 
35941393123eSChristoffer Dall 	vcpu_put(vcpu);
3595b0c632dbSHeiko Carstens 	return 0;
3596b0c632dbSHeiko Carstens }
3597b0c632dbSHeiko Carstens 
3598b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
3599b0c632dbSHeiko Carstens {
3600b0c632dbSHeiko Carstens 	int rc = 0;
3601b0c632dbSHeiko Carstens 
36027a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
3603b0c632dbSHeiko Carstens 		rc = -EBUSY;
3604d7b0b5ebSCarsten Otte 	else {
3605d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
3606d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
3607d7b0b5ebSCarsten Otte 	}
3608b0c632dbSHeiko Carstens 	return rc;
3609b0c632dbSHeiko Carstens }
3610b0c632dbSHeiko Carstens 
3611b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3612b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
3613b0c632dbSHeiko Carstens {
3614b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
3615b0c632dbSHeiko Carstens }
3616b0c632dbSHeiko Carstens 
361727291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
361827291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
361927291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
362027291e21SDavid Hildenbrand 
3621d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
3622d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
3623b0c632dbSHeiko Carstens {
362427291e21SDavid Hildenbrand 	int rc = 0;
362527291e21SDavid Hildenbrand 
362666b56562SChristoffer Dall 	vcpu_load(vcpu);
362766b56562SChristoffer Dall 
362827291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
362927291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
363027291e21SDavid Hildenbrand 
363166b56562SChristoffer Dall 	if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
363266b56562SChristoffer Dall 		rc = -EINVAL;
363366b56562SChristoffer Dall 		goto out;
363466b56562SChristoffer Dall 	}
363566b56562SChristoffer Dall 	if (!sclp.has_gpere) {
363666b56562SChristoffer Dall 		rc = -EINVAL;
363766b56562SChristoffer Dall 		goto out;
363866b56562SChristoffer Dall 	}
363927291e21SDavid Hildenbrand 
364027291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
364127291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
364227291e21SDavid Hildenbrand 		/* enforce guest PER */
3643ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
364427291e21SDavid Hildenbrand 
364527291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
364627291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
364727291e21SDavid Hildenbrand 	} else {
36489daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
364927291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
365027291e21SDavid Hildenbrand 	}
365127291e21SDavid Hildenbrand 
365227291e21SDavid Hildenbrand 	if (rc) {
365327291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
365427291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
36559daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
365627291e21SDavid Hildenbrand 	}
365727291e21SDavid Hildenbrand 
365866b56562SChristoffer Dall out:
365966b56562SChristoffer Dall 	vcpu_put(vcpu);
366027291e21SDavid Hildenbrand 	return rc;
3661b0c632dbSHeiko Carstens }
3662b0c632dbSHeiko Carstens 
366362d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
366462d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
366562d9f0dbSMarcelo Tosatti {
3666fd232561SChristoffer Dall 	int ret;
3667fd232561SChristoffer Dall 
3668fd232561SChristoffer Dall 	vcpu_load(vcpu);
3669fd232561SChristoffer Dall 
36706352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
3671fd232561SChristoffer Dall 	ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
36726352e4d2SDavid Hildenbrand 				      KVM_MP_STATE_OPERATING;
3673fd232561SChristoffer Dall 
3674fd232561SChristoffer Dall 	vcpu_put(vcpu);
3675fd232561SChristoffer Dall 	return ret;
367662d9f0dbSMarcelo Tosatti }
367762d9f0dbSMarcelo Tosatti 
367862d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
367962d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
368062d9f0dbSMarcelo Tosatti {
36816352e4d2SDavid Hildenbrand 	int rc = 0;
36826352e4d2SDavid Hildenbrand 
3683e83dff5eSChristoffer Dall 	vcpu_load(vcpu);
3684e83dff5eSChristoffer Dall 
36856352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
36866352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
36876352e4d2SDavid Hildenbrand 
36886352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
36896352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
36906352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
36916352e4d2SDavid Hildenbrand 		break;
36926352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
36936352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
36946352e4d2SDavid Hildenbrand 		break;
36956352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
36966352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
36976352e4d2SDavid Hildenbrand 		/* fall through - CHECK_STOP and LOAD are not supported yet */
36986352e4d2SDavid Hildenbrand 	default:
36996352e4d2SDavid Hildenbrand 		rc = -ENXIO;
37006352e4d2SDavid Hildenbrand 	}
37016352e4d2SDavid Hildenbrand 
3702e83dff5eSChristoffer Dall 	vcpu_put(vcpu);
37036352e4d2SDavid Hildenbrand 	return rc;
370462d9f0dbSMarcelo Tosatti }
370562d9f0dbSMarcelo Tosatti 
37068ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
37078ad35755SDavid Hildenbrand {
37088d5fb0dcSDavid Hildenbrand 	return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
37098ad35755SDavid Hildenbrand }
37108ad35755SDavid Hildenbrand 
37112c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
37122c70fe44SChristian Borntraeger {
37138ad35755SDavid Hildenbrand retry:
37148e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
37152fa6e1e1SRadim Krčmář 	if (!kvm_request_pending(vcpu))
3716586b7ccdSChristian Borntraeger 		return 0;
37172c70fe44SChristian Borntraeger 	/*
37182c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
3719b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
37202c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
37212c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
37222c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
37232c70fe44SChristian Borntraeger 	 */
37248ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
37252c70fe44SChristian Borntraeger 		int rc;
3726b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
3727fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
3728b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
3729aca411a4SJulius Niedworok 		if (rc) {
3730aca411a4SJulius Niedworok 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
37312c70fe44SChristian Borntraeger 			return rc;
3732aca411a4SJulius Niedworok 		}
37338ad35755SDavid Hildenbrand 		goto retry;
37342c70fe44SChristian Borntraeger 	}
37358ad35755SDavid Hildenbrand 
3736d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
3737d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
3738d3d692c8SDavid Hildenbrand 		goto retry;
3739d3d692c8SDavid Hildenbrand 	}
3740d3d692c8SDavid Hildenbrand 
37418ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
37428ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
37438ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
3744ef8f4f49SDavid Hildenbrand 			kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
37458ad35755SDavid Hildenbrand 		}
37468ad35755SDavid Hildenbrand 		goto retry;
37478ad35755SDavid Hildenbrand 	}
37488ad35755SDavid Hildenbrand 
37498ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
37508ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
37518ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
37529daecfc6SDavid Hildenbrand 			kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
37538ad35755SDavid Hildenbrand 		}
37548ad35755SDavid Hildenbrand 		goto retry;
37558ad35755SDavid Hildenbrand 	}
37568ad35755SDavid Hildenbrand 
37576502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
37586502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
37596502a34cSDavid Hildenbrand 		goto retry;
37606502a34cSDavid Hildenbrand 	}
37616502a34cSDavid Hildenbrand 
3762190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
3763190df4a2SClaudio Imbrenda 		/*
3764c9f0a2b8SJanosch Frank 		 * Disable CMM virtualization; we will emulate the ESSA
3765190df4a2SClaudio Imbrenda 		 * instruction manually, in order to provide additional
3766190df4a2SClaudio Imbrenda 		 * functionalities needed for live migration.
3767190df4a2SClaudio Imbrenda 		 */
3768190df4a2SClaudio Imbrenda 		vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
3769190df4a2SClaudio Imbrenda 		goto retry;
3770190df4a2SClaudio Imbrenda 	}
3771190df4a2SClaudio Imbrenda 
3772190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
3773190df4a2SClaudio Imbrenda 		/*
3774c9f0a2b8SJanosch Frank 		 * Re-enable CMM virtualization if CMMA is available and
3775c9f0a2b8SJanosch Frank 		 * CMM has been used.
3776190df4a2SClaudio Imbrenda 		 */
3777190df4a2SClaudio Imbrenda 		if ((vcpu->kvm->arch.use_cmma) &&
3778c9f0a2b8SJanosch Frank 		    (vcpu->kvm->mm->context.uses_cmm))
3779190df4a2SClaudio Imbrenda 			vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
3780190df4a2SClaudio Imbrenda 		goto retry;
3781190df4a2SClaudio Imbrenda 	}
3782190df4a2SClaudio Imbrenda 
37830759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
378472875d8aSRadim Krčmář 	kvm_clear_request(KVM_REQ_UNHALT, vcpu);
37853194cdb7SDavid Hildenbrand 	/* we left the vsie handler, nothing to do, just clear the request */
37863194cdb7SDavid Hildenbrand 	kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
37870759d068SDavid Hildenbrand 
37882c70fe44SChristian Borntraeger 	return 0;
37892c70fe44SChristian Borntraeger }
37902c70fe44SChristian Borntraeger 
37910e7def5fSDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm,
37928fa1696eSCollin L. Walling 			    const struct kvm_s390_vm_tod_clock *gtod)
37938fa1696eSCollin L. Walling {
37948fa1696eSCollin L. Walling 	struct kvm_vcpu *vcpu;
37958fa1696eSCollin L. Walling 	struct kvm_s390_tod_clock_ext htod;
37968fa1696eSCollin L. Walling 	int i;
37978fa1696eSCollin L. Walling 
37988fa1696eSCollin L. Walling 	mutex_lock(&kvm->lock);
37998fa1696eSCollin L. Walling 	preempt_disable();
38008fa1696eSCollin L. Walling 
38018fa1696eSCollin L. Walling 	get_tod_clock_ext((char *)&htod);
38028fa1696eSCollin L. Walling 
38038fa1696eSCollin L. Walling 	kvm->arch.epoch = gtod->tod - htod.tod;
38040e7def5fSDavid Hildenbrand 	kvm->arch.epdx = 0;
38050e7def5fSDavid Hildenbrand 	if (test_kvm_facility(kvm, 139)) {
38068fa1696eSCollin L. Walling 		kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx;
38078fa1696eSCollin L. Walling 		if (kvm->arch.epoch > gtod->tod)
38088fa1696eSCollin L. Walling 			kvm->arch.epdx -= 1;
38090e7def5fSDavid Hildenbrand 	}
38108fa1696eSCollin L. Walling 
38118fa1696eSCollin L. Walling 	kvm_s390_vcpu_block_all(kvm);
38128fa1696eSCollin L. Walling 	kvm_for_each_vcpu(i, vcpu, kvm) {
38138fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
38148fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epdx  = kvm->arch.epdx;
38158fa1696eSCollin L. Walling 	}
38168fa1696eSCollin L. Walling 
38178fa1696eSCollin L. Walling 	kvm_s390_vcpu_unblock_all(kvm);
38188fa1696eSCollin L. Walling 	preempt_enable();
38198fa1696eSCollin L. Walling 	mutex_unlock(&kvm->lock);
38208fa1696eSCollin L. Walling }
38218fa1696eSCollin L. Walling 
3822fa576c58SThomas Huth /**
3823fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
3824fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
3825fa576c58SThomas Huth  * @gpa: Guest physical address
3826fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
3827fa576c58SThomas Huth  *
3828fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
3829fa576c58SThomas Huth  *
3830fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
3831fa576c58SThomas Huth  */
3832fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
383324eb3a82SDominik Dingel {
3834527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
3835527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
383624eb3a82SDominik Dingel }
383724eb3a82SDominik Dingel 
38383c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
38393c038e6bSDominik Dingel 				      unsigned long token)
38403c038e6bSDominik Dingel {
38413c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
3842383d0b05SJens Freimann 	struct kvm_s390_irq irq;
38433c038e6bSDominik Dingel 
38443c038e6bSDominik Dingel 	if (start_token) {
3845383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
3846383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
3847383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
38483c038e6bSDominik Dingel 	} else {
38493c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
3850383d0b05SJens Freimann 		inti.parm64 = token;
38513c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
38523c038e6bSDominik Dingel 	}
38533c038e6bSDominik Dingel }
38543c038e6bSDominik Dingel 
38553c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
38563c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
38573c038e6bSDominik Dingel {
38583c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
38593c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
38603c038e6bSDominik Dingel }
38613c038e6bSDominik Dingel 
38623c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
38633c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
38643c038e6bSDominik Dingel {
38653c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
38663c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
38673c038e6bSDominik Dingel }
38683c038e6bSDominik Dingel 
38693c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
38703c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
38713c038e6bSDominik Dingel {
38723c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
38733c038e6bSDominik Dingel }
38743c038e6bSDominik Dingel 
38753c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
38763c038e6bSDominik Dingel {
38773c038e6bSDominik Dingel 	/*
38783c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
38793c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
38803c038e6bSDominik Dingel 	 */
38813c038e6bSDominik Dingel 	return true;
38823c038e6bSDominik Dingel }
38833c038e6bSDominik Dingel 
38843c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
38853c038e6bSDominik Dingel {
38863c038e6bSDominik Dingel 	hva_t hva;
38873c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
38883c038e6bSDominik Dingel 	int rc;
38893c038e6bSDominik Dingel 
38903c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
38913c038e6bSDominik Dingel 		return 0;
38923c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
38933c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
38943c038e6bSDominik Dingel 		return 0;
38953c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
38963c038e6bSDominik Dingel 		return 0;
38979a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
38983c038e6bSDominik Dingel 		return 0;
3899b9224cd7SDavid Hildenbrand 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
39003c038e6bSDominik Dingel 		return 0;
39013c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
39023c038e6bSDominik Dingel 		return 0;
39033c038e6bSDominik Dingel 
390481480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
390581480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
390681480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
39073c038e6bSDominik Dingel 		return 0;
39083c038e6bSDominik Dingel 
39093c038e6bSDominik Dingel 	rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
39103c038e6bSDominik Dingel 	return rc;
39113c038e6bSDominik Dingel }
39123c038e6bSDominik Dingel 
39133fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
3914b0c632dbSHeiko Carstens {
39153fb4c40fSThomas Huth 	int rc, cpuflags;
3916e168bf8dSCarsten Otte 
39173c038e6bSDominik Dingel 	/*
39183c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
39193c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
39203c038e6bSDominik Dingel 	 * handled outside the worker.
39213c038e6bSDominik Dingel 	 */
39223c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
39233c038e6bSDominik Dingel 
39247ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
39257ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
3926b0c632dbSHeiko Carstens 
3927b0c632dbSHeiko Carstens 	if (need_resched())
3928b0c632dbSHeiko Carstens 		schedule();
3929b0c632dbSHeiko Carstens 
3930d3a73acbSMartin Schwidefsky 	if (test_cpu_flag(CIF_MCCK_PENDING))
393171cde587SChristian Borntraeger 		s390_handle_mcck();
393271cde587SChristian Borntraeger 
393379395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
393479395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
393579395031SJens Freimann 		if (rc)
393679395031SJens Freimann 			return rc;
393779395031SJens Freimann 	}
39380ff31867SCarsten Otte 
39392c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
39402c70fe44SChristian Borntraeger 	if (rc)
39412c70fe44SChristian Borntraeger 		return rc;
39422c70fe44SChristian Borntraeger 
394327291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
394427291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
394527291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
394627291e21SDavid Hildenbrand 	}
394727291e21SDavid Hildenbrand 
39489f30f621SMichael Mueller 	clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask);
39499f30f621SMichael Mueller 
3950b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
39513fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
39523fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
39533fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
39542b29a9fdSDominik Dingel 
39553fb4c40fSThomas Huth 	return 0;
39563fb4c40fSThomas Huth }
39573fb4c40fSThomas Huth 
3958492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
3959492d8642SThomas Huth {
396056317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
396156317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
396256317920SDavid Hildenbrand 	};
396356317920SDavid Hildenbrand 	u8 opcode, ilen;
3964492d8642SThomas Huth 	int rc;
3965492d8642SThomas Huth 
3966492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
3967492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
3968492d8642SThomas Huth 
3969492d8642SThomas Huth 	/*
3970492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
3971492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
3972492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
3973492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
3974492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
3975492d8642SThomas Huth 	 * to be able to forward the PSW.
3976492d8642SThomas Huth 	 */
39773fa8cad7SDavid Hildenbrand 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
397856317920SDavid Hildenbrand 	ilen = insn_length(opcode);
39799b0d721aSDavid Hildenbrand 	if (rc < 0) {
39809b0d721aSDavid Hildenbrand 		return rc;
39819b0d721aSDavid Hildenbrand 	} else if (rc) {
39829b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
39839b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
39849b0d721aSDavid Hildenbrand 		 * nullification if necessary.
39859b0d721aSDavid Hildenbrand 		 */
39869b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
39879b0d721aSDavid Hildenbrand 		ilen = 4;
39889b0d721aSDavid Hildenbrand 	}
398956317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
399056317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
399156317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
3992492d8642SThomas Huth }
3993492d8642SThomas Huth 
39943fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
39953fb4c40fSThomas Huth {
39964d62fcc0SQingFeng Hao 	struct mcck_volatile_info *mcck_info;
39974d62fcc0SQingFeng Hao 	struct sie_page *sie_page;
39984d62fcc0SQingFeng Hao 
39992b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
40002b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
40012b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
40022b29a9fdSDominik Dingel 
400327291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
400427291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
400527291e21SDavid Hildenbrand 
40067ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
40077ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
400871f116bfSDavid Hildenbrand 
40094d62fcc0SQingFeng Hao 	if (exit_reason == -EINTR) {
40104d62fcc0SQingFeng Hao 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
40114d62fcc0SQingFeng Hao 		sie_page = container_of(vcpu->arch.sie_block,
40124d62fcc0SQingFeng Hao 					struct sie_page, sie_block);
40134d62fcc0SQingFeng Hao 		mcck_info = &sie_page->mcck_info;
40144d62fcc0SQingFeng Hao 		kvm_s390_reinject_machine_check(vcpu, mcck_info);
40154d62fcc0SQingFeng Hao 		return 0;
40164d62fcc0SQingFeng Hao 	}
40174d62fcc0SQingFeng Hao 
401871f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
401971f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
402071f116bfSDavid Hildenbrand 
402171f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
402271f116bfSDavid Hildenbrand 			return rc;
402371f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
402471f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
402571f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
402671f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
402771f116bfSDavid Hildenbrand 		return -EREMOTE;
402871f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
402971f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
403071f116bfSDavid Hildenbrand 		return 0;
4031210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
4032210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
4033210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
4034210b1607SThomas Huth 						current->thread.gmap_addr;
4035210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
403671f116bfSDavid Hildenbrand 		return -EREMOTE;
403724eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
40383c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
403924eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
404071f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
404171f116bfSDavid Hildenbrand 			return 0;
404271f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
4043fa576c58SThomas Huth 	}
404471f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
40453fb4c40fSThomas Huth }
40463fb4c40fSThomas Huth 
40473fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
40483fb4c40fSThomas Huth {
40493fb4c40fSThomas Huth 	int rc, exit_reason;
40503fb4c40fSThomas Huth 
4051800c1065SThomas Huth 	/*
4052800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
4053800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
4054800c1065SThomas Huth 	 */
4055800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4056800c1065SThomas Huth 
4057a76ccff6SThomas Huth 	do {
40583fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
40593fb4c40fSThomas Huth 		if (rc)
4060a76ccff6SThomas Huth 			break;
40613fb4c40fSThomas Huth 
4062800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
40633fb4c40fSThomas Huth 		/*
4064a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
4065a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
40663fb4c40fSThomas Huth 		 */
40670097d12eSChristian Borntraeger 		local_irq_disable();
40686edaa530SPaolo Bonzini 		guest_enter_irqoff();
4069db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
40700097d12eSChristian Borntraeger 		local_irq_enable();
4071a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
4072a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
40730097d12eSChristian Borntraeger 		local_irq_disable();
4074db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
40756edaa530SPaolo Bonzini 		guest_exit_irqoff();
40760097d12eSChristian Borntraeger 		local_irq_enable();
4077800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
40783fb4c40fSThomas Huth 
40793fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
408027291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
40813fb4c40fSThomas Huth 
4082800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4083e168bf8dSCarsten Otte 	return rc;
4084b0c632dbSHeiko Carstens }
4085b0c632dbSHeiko Carstens 
4086b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4087b028ee3eSDavid Hildenbrand {
40884d5f2c04SChristian Borntraeger 	struct runtime_instr_cb *riccb;
40894e0b1ab7SFan Zhang 	struct gs_cb *gscb;
40904d5f2c04SChristian Borntraeger 
40914d5f2c04SChristian Borntraeger 	riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
40924e0b1ab7SFan Zhang 	gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
4093b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
4094b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
4095b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
4096b028ee3eSDavid Hildenbrand 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
4097b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
4098b028ee3eSDavid Hildenbrand 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
4099d3d692c8SDavid Hildenbrand 		/* some control register changes require a tlb flush */
4100d3d692c8SDavid Hildenbrand 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4101b028ee3eSDavid Hildenbrand 	}
4102b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
41034287f247SDavid Hildenbrand 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
4104b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
4105b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
4106b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
4107b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
4108b028ee3eSDavid Hildenbrand 	}
4109b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
4110b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
4111b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
4112b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
41139fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
41149fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
4115b028ee3eSDavid Hildenbrand 	}
411680cd8763SFan Zhang 	/*
411780cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
411880cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
411980cd8763SFan Zhang 	 */
412080cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
41214d5f2c04SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 64) &&
4122bb59c2daSAlice Frosi 	    riccb->v &&
41230c9d8683SDavid Hildenbrand 	    !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
41244d5f2c04SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
41250c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
412680cd8763SFan Zhang 	}
41274e0b1ab7SFan Zhang 	/*
41284e0b1ab7SFan Zhang 	 * If userspace sets the gscb (e.g. after migration) to non-zero,
41294e0b1ab7SFan Zhang 	 * we should enable GS here instead of doing the lazy enablement.
41304e0b1ab7SFan Zhang 	 */
41314e0b1ab7SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
41324e0b1ab7SFan Zhang 	    test_kvm_facility(vcpu->kvm, 133) &&
41334e0b1ab7SFan Zhang 	    gscb->gssm &&
41344e0b1ab7SFan Zhang 	    !vcpu->arch.gs_enabled) {
41354e0b1ab7SFan Zhang 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
41364e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecb |= ECB_GS;
41374e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
41384e0b1ab7SFan Zhang 		vcpu->arch.gs_enabled = 1;
413980cd8763SFan Zhang 	}
414035b3fde6SChristian Borntraeger 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
414135b3fde6SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 82)) {
414235b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
414335b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
414435b3fde6SChristian Borntraeger 	}
414531d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->arch.host_acrs);
414631d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
4147e1788bb9SChristian Borntraeger 	/* save host (userspace) fprs/vrs */
4148e1788bb9SChristian Borntraeger 	save_fpu_regs();
4149e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
4150e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
4151e1788bb9SChristian Borntraeger 	if (MACHINE_HAS_VX)
4152e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
4153e1788bb9SChristian Borntraeger 	else
4154e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
4155e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
4156e1788bb9SChristian Borntraeger 	if (test_fp_ctl(current->thread.fpu.fpc))
4157e1788bb9SChristian Borntraeger 		/* User space provided an invalid FPC, let's clear it */
4158e1788bb9SChristian Borntraeger 		current->thread.fpu.fpc = 0;
41594e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
41604e0b1ab7SFan Zhang 		preempt_disable();
41614e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
41624e0b1ab7SFan Zhang 		if (current->thread.gs_cb) {
41634e0b1ab7SFan Zhang 			vcpu->arch.host_gscb = current->thread.gs_cb;
41644e0b1ab7SFan Zhang 			save_gs_cb(vcpu->arch.host_gscb);
41654e0b1ab7SFan Zhang 		}
41664e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled) {
41674e0b1ab7SFan Zhang 			current->thread.gs_cb = (struct gs_cb *)
41684e0b1ab7SFan Zhang 						&vcpu->run->s.regs.gscb;
41694e0b1ab7SFan Zhang 			restore_gs_cb(current->thread.gs_cb);
41704e0b1ab7SFan Zhang 		}
41714e0b1ab7SFan Zhang 		preempt_enable();
41724e0b1ab7SFan Zhang 	}
4173a3da7b4aSChristian Borntraeger 	/* SIE will load etoken directly from SDNX and therefore kvm_run */
417480cd8763SFan Zhang 
4175b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
4176b028ee3eSDavid Hildenbrand }
4177b028ee3eSDavid Hildenbrand 
4178b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4179b028ee3eSDavid Hildenbrand {
4180b028ee3eSDavid Hildenbrand 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
4181b028ee3eSDavid Hildenbrand 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
4182b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
4183b028ee3eSDavid Hildenbrand 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
41844287f247SDavid Hildenbrand 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
4185b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
4186b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
4187b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
4188b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
4189b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
4190b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
4191b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
419235b3fde6SChristian Borntraeger 	kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
419331d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
419431d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->arch.host_acrs);
4195e1788bb9SChristian Borntraeger 	/* Save guest register state */
4196e1788bb9SChristian Borntraeger 	save_fpu_regs();
4197e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4198e1788bb9SChristian Borntraeger 	/* Restore will be done lazily at return */
4199e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
4200e1788bb9SChristian Borntraeger 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
42014e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
42024e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
42034e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled)
42044e0b1ab7SFan Zhang 			save_gs_cb(current->thread.gs_cb);
42054e0b1ab7SFan Zhang 		preempt_disable();
42064e0b1ab7SFan Zhang 		current->thread.gs_cb = vcpu->arch.host_gscb;
42074e0b1ab7SFan Zhang 		restore_gs_cb(vcpu->arch.host_gscb);
42084e0b1ab7SFan Zhang 		preempt_enable();
42094e0b1ab7SFan Zhang 		if (!vcpu->arch.host_gscb)
42104e0b1ab7SFan Zhang 			__ctl_clear_bit(2, 4);
42114e0b1ab7SFan Zhang 		vcpu->arch.host_gscb = NULL;
42124e0b1ab7SFan Zhang 	}
4213a3da7b4aSChristian Borntraeger 	/* SIE will save etoken directly into SDNX and therefore kvm_run */
4214b028ee3eSDavid Hildenbrand }
4215b028ee3eSDavid Hildenbrand 
4216b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4217b0c632dbSHeiko Carstens {
42188f2abe6aSChristian Borntraeger 	int rc;
4219b0c632dbSHeiko Carstens 
4220460df4c1SPaolo Bonzini 	if (kvm_run->immediate_exit)
4221460df4c1SPaolo Bonzini 		return -EINTR;
4222460df4c1SPaolo Bonzini 
4223200824f5SThomas Huth 	if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
4224200824f5SThomas Huth 	    kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
4225200824f5SThomas Huth 		return -EINVAL;
4226200824f5SThomas Huth 
4227accb757dSChristoffer Dall 	vcpu_load(vcpu);
4228accb757dSChristoffer Dall 
422927291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
423027291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
4231accb757dSChristoffer Dall 		rc = 0;
4232accb757dSChristoffer Dall 		goto out;
423327291e21SDavid Hildenbrand 	}
423427291e21SDavid Hildenbrand 
423520b7035cSJan H. Schönherr 	kvm_sigset_activate(vcpu);
4236b0c632dbSHeiko Carstens 
42376352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
42386852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
42396352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
4240ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
42416352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
4242accb757dSChristoffer Dall 		rc = -EINVAL;
4243accb757dSChristoffer Dall 		goto out;
42446352e4d2SDavid Hildenbrand 	}
4245b0c632dbSHeiko Carstens 
4246b028ee3eSDavid Hildenbrand 	sync_regs(vcpu, kvm_run);
4247db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
4248d7b0b5ebSCarsten Otte 
4249dab4079dSHeiko Carstens 	might_fault();
4250e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
42519ace903dSChristian Ehrhardt 
4252b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
4253b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
42548f2abe6aSChristian Borntraeger 		rc = -EINTR;
4255b1d16c49SChristian Ehrhardt 	}
42568f2abe6aSChristian Borntraeger 
425727291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
425827291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
425927291e21SDavid Hildenbrand 		rc = 0;
426027291e21SDavid Hildenbrand 	}
426127291e21SDavid Hildenbrand 
42628f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
426371f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
42648f2abe6aSChristian Borntraeger 		rc = 0;
42658f2abe6aSChristian Borntraeger 	}
42668f2abe6aSChristian Borntraeger 
4267db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
4268b028ee3eSDavid Hildenbrand 	store_regs(vcpu, kvm_run);
4269d7b0b5ebSCarsten Otte 
427020b7035cSJan H. Schönherr 	kvm_sigset_deactivate(vcpu);
4271b0c632dbSHeiko Carstens 
4272b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
4273accb757dSChristoffer Dall out:
4274accb757dSChristoffer Dall 	vcpu_put(vcpu);
42757e8e6ab4SHeiko Carstens 	return rc;
4276b0c632dbSHeiko Carstens }
4277b0c632dbSHeiko Carstens 
4278b0c632dbSHeiko Carstens /*
4279b0c632dbSHeiko Carstens  * store status at address
4280b0c632dbSHeiko Carstens  * we use have two special cases:
4281b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
4282b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
4283b0c632dbSHeiko Carstens  */
4284d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
4285b0c632dbSHeiko Carstens {
4286092670cdSCarsten Otte 	unsigned char archmode = 1;
42879abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
4288fda902cbSMichael Mueller 	unsigned int px;
42894287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
4290d0bce605SHeiko Carstens 	int rc;
4291b0c632dbSHeiko Carstens 
4292d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
4293d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
4294d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
4295b0c632dbSHeiko Carstens 			return -EFAULT;
4296d9a3a09aSMartin Schwidefsky 		gpa = 0;
4297d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
4298d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
4299b0c632dbSHeiko Carstens 			return -EFAULT;
4300d9a3a09aSMartin Schwidefsky 		gpa = px;
4301d9a3a09aSMartin Schwidefsky 	} else
4302d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
43039abc2a08SDavid Hildenbrand 
43049abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
43059abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
43069522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
4307d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
43089abc2a08SDavid Hildenbrand 				     fprs, 128);
43099abc2a08SDavid Hildenbrand 	} else {
43109abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
43116fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
43129abc2a08SDavid Hildenbrand 	}
4313d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
4314d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
4315d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
4316d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
4317d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
4318fda902cbSMichael Mueller 			      &px, 4);
4319d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
43209abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
4321d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
4322d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
43234287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
4324d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
43254287f247SDavid Hildenbrand 			      &cputm, 8);
4326178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
4327d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
4328d0bce605SHeiko Carstens 			      &clkcomp, 8);
4329d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
4330d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
4331d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
4332d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
4333d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
4334b0c632dbSHeiko Carstens }
4335b0c632dbSHeiko Carstens 
4336e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
4337e879892cSThomas Huth {
4338e879892cSThomas Huth 	/*
4339e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
434031d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
4341e879892cSThomas Huth 	 * it into the save area
4342e879892cSThomas Huth 	 */
4343d0164ee2SHendrik Brueckner 	save_fpu_regs();
43449abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4345e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
4346e879892cSThomas Huth 
4347e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
4348e879892cSThomas Huth }
4349e879892cSThomas Huth 
43508ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
43518ad35755SDavid Hildenbrand {
43528ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
43538e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
43548ad35755SDavid Hildenbrand }
43558ad35755SDavid Hildenbrand 
43568ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
43578ad35755SDavid Hildenbrand {
43588ad35755SDavid Hildenbrand 	unsigned int i;
43598ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
43608ad35755SDavid Hildenbrand 
43618ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
43628ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
43638ad35755SDavid Hildenbrand 	}
43648ad35755SDavid Hildenbrand }
43658ad35755SDavid Hildenbrand 
43668ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
43678ad35755SDavid Hildenbrand {
436809a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
436909a400e7SDavid Hildenbrand 		return;
43708ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
43718e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
43728ad35755SDavid Hildenbrand }
43738ad35755SDavid Hildenbrand 
43746852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
43756852d7b6SDavid Hildenbrand {
43768ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
43778ad35755SDavid Hildenbrand 
43788ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
43798ad35755SDavid Hildenbrand 		return;
43808ad35755SDavid Hildenbrand 
43816852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
43828ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
4383433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
43848ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
43858ad35755SDavid Hildenbrand 
43868ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
43878ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
43888ad35755SDavid Hildenbrand 			started_vcpus++;
43898ad35755SDavid Hildenbrand 	}
43908ad35755SDavid Hildenbrand 
43918ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
43928ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
43938ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
43948ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
43958ad35755SDavid Hildenbrand 		/*
43968ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
43978ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
43988ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
43998ad35755SDavid Hildenbrand 		 */
44008ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
44018ad35755SDavid Hildenbrand 	}
44028ad35755SDavid Hildenbrand 
44039daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
44048ad35755SDavid Hildenbrand 	/*
44058ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
44068ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
44078ad35755SDavid Hildenbrand 	 */
4408d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4409433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
44108ad35755SDavid Hildenbrand 	return;
44116852d7b6SDavid Hildenbrand }
44126852d7b6SDavid Hildenbrand 
44136852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
44146852d7b6SDavid Hildenbrand {
44158ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
44168ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
44178ad35755SDavid Hildenbrand 
44188ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
44198ad35755SDavid Hildenbrand 		return;
44208ad35755SDavid Hildenbrand 
44216852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
44228ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
4423433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
44248ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
44258ad35755SDavid Hildenbrand 
442632f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
44276cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
442832f5ff63SDavid Hildenbrand 
4429ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
44308ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
44318ad35755SDavid Hildenbrand 
44328ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
44338ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
44348ad35755SDavid Hildenbrand 			started_vcpus++;
44358ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
44368ad35755SDavid Hildenbrand 		}
44378ad35755SDavid Hildenbrand 	}
44388ad35755SDavid Hildenbrand 
44398ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
44408ad35755SDavid Hildenbrand 		/*
44418ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
44428ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
44438ad35755SDavid Hildenbrand 		 */
44448ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
44458ad35755SDavid Hildenbrand 	}
44468ad35755SDavid Hildenbrand 
4447433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
44488ad35755SDavid Hildenbrand 	return;
44496852d7b6SDavid Hildenbrand }
44506852d7b6SDavid Hildenbrand 
4451d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4452d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
4453d6712df9SCornelia Huck {
4454d6712df9SCornelia Huck 	int r;
4455d6712df9SCornelia Huck 
4456d6712df9SCornelia Huck 	if (cap->flags)
4457d6712df9SCornelia Huck 		return -EINVAL;
4458d6712df9SCornelia Huck 
4459d6712df9SCornelia Huck 	switch (cap->cap) {
4460fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
4461fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
4462fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
4463c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
4464fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
4465fa6b7fe9SCornelia Huck 		}
4466fa6b7fe9SCornelia Huck 		r = 0;
4467fa6b7fe9SCornelia Huck 		break;
4468d6712df9SCornelia Huck 	default:
4469d6712df9SCornelia Huck 		r = -EINVAL;
4470d6712df9SCornelia Huck 		break;
4471d6712df9SCornelia Huck 	}
4472d6712df9SCornelia Huck 	return r;
4473d6712df9SCornelia Huck }
4474d6712df9SCornelia Huck 
447541408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
447641408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
447741408c28SThomas Huth {
447841408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
447941408c28SThomas Huth 	void *tmpbuf = NULL;
448041408c28SThomas Huth 	int r, srcu_idx;
448141408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
448241408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
448341408c28SThomas Huth 
4484a13b03bbSThomas Huth 	if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size)
448541408c28SThomas Huth 		return -EINVAL;
448641408c28SThomas Huth 
448741408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
448841408c28SThomas Huth 		return -E2BIG;
448941408c28SThomas Huth 
449041408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
449141408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
449241408c28SThomas Huth 		if (!tmpbuf)
449341408c28SThomas Huth 			return -ENOMEM;
449441408c28SThomas Huth 	}
449541408c28SThomas Huth 
449641408c28SThomas Huth 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
449741408c28SThomas Huth 
449841408c28SThomas Huth 	switch (mop->op) {
449941408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
450041408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
450192c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
450292c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
450341408c28SThomas Huth 			break;
450441408c28SThomas Huth 		}
450541408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
450641408c28SThomas Huth 		if (r == 0) {
450741408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
450841408c28SThomas Huth 				r = -EFAULT;
450941408c28SThomas Huth 		}
451041408c28SThomas Huth 		break;
451141408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
451241408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
451392c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
451492c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
451541408c28SThomas Huth 			break;
451641408c28SThomas Huth 		}
451741408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
451841408c28SThomas Huth 			r = -EFAULT;
451941408c28SThomas Huth 			break;
452041408c28SThomas Huth 		}
452141408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
452241408c28SThomas Huth 		break;
452341408c28SThomas Huth 	default:
452441408c28SThomas Huth 		r = -EINVAL;
452541408c28SThomas Huth 	}
452641408c28SThomas Huth 
452741408c28SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
452841408c28SThomas Huth 
452941408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
453041408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
453141408c28SThomas Huth 
453241408c28SThomas Huth 	vfree(tmpbuf);
453341408c28SThomas Huth 	return r;
453441408c28SThomas Huth }
453541408c28SThomas Huth 
45365cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp,
4537b0c632dbSHeiko Carstens 			       unsigned int ioctl, unsigned long arg)
4538b0c632dbSHeiko Carstens {
4539b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
4540b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
4541b0c632dbSHeiko Carstens 
454293736624SAvi Kivity 	switch (ioctl) {
454347b43c52SJens Freimann 	case KVM_S390_IRQ: {
454447b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
454547b43c52SJens Freimann 
454647b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
45479b062471SChristoffer Dall 			return -EFAULT;
45489b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
454947b43c52SJens Freimann 	}
455093736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
4551ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
455253936b5bSThomas Huth 		struct kvm_s390_irq s390irq = {};
4553ba5c1e9bSCarsten Otte 
4554ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
45559b062471SChristoffer Dall 			return -EFAULT;
4556383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
4557383d0b05SJens Freimann 			return -EINVAL;
45589b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
4559ba5c1e9bSCarsten Otte 	}
45609b062471SChristoffer Dall 	}
45615cb0944cSPaolo Bonzini 	return -ENOIOCTLCMD;
45625cb0944cSPaolo Bonzini }
45635cb0944cSPaolo Bonzini 
45645cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp,
45655cb0944cSPaolo Bonzini 			 unsigned int ioctl, unsigned long arg)
45665cb0944cSPaolo Bonzini {
45675cb0944cSPaolo Bonzini 	struct kvm_vcpu *vcpu = filp->private_data;
45685cb0944cSPaolo Bonzini 	void __user *argp = (void __user *)arg;
45695cb0944cSPaolo Bonzini 	int idx;
45705cb0944cSPaolo Bonzini 	long r;
45719b062471SChristoffer Dall 
45729b062471SChristoffer Dall 	vcpu_load(vcpu);
45739b062471SChristoffer Dall 
45749b062471SChristoffer Dall 	switch (ioctl) {
4575b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
4576800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
457755680890SChristian Borntraeger 		r = kvm_s390_store_status_unloaded(vcpu, arg);
4578800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
4579bc923cc9SAvi Kivity 		break;
4580b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
4581b0c632dbSHeiko Carstens 		psw_t psw;
4582b0c632dbSHeiko Carstens 
4583bc923cc9SAvi Kivity 		r = -EFAULT;
4584b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
4585bc923cc9SAvi Kivity 			break;
4586bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
4587bc923cc9SAvi Kivity 		break;
4588b0c632dbSHeiko Carstens 	}
45897de3f142SJanosch Frank 	case KVM_S390_CLEAR_RESET:
45907de3f142SJanosch Frank 		r = 0;
45917de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_clear_reset(vcpu);
45927de3f142SJanosch Frank 		break;
4593b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
45947de3f142SJanosch Frank 		r = 0;
45957de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_initial_reset(vcpu);
45967de3f142SJanosch Frank 		break;
45977de3f142SJanosch Frank 	case KVM_S390_NORMAL_RESET:
45987de3f142SJanosch Frank 		r = 0;
45997de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_normal_reset(vcpu);
4600bc923cc9SAvi Kivity 		break;
460114eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
460214eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
460314eebd91SCarsten Otte 		struct kvm_one_reg reg;
460414eebd91SCarsten Otte 		r = -EFAULT;
460514eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
460614eebd91SCarsten Otte 			break;
460714eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
460814eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
460914eebd91SCarsten Otte 		else
461014eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
461114eebd91SCarsten Otte 		break;
461214eebd91SCarsten Otte 	}
461327e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
461427e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
461527e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
461627e0393fSCarsten Otte 
461727e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
461827e0393fSCarsten Otte 			r = -EFAULT;
461927e0393fSCarsten Otte 			break;
462027e0393fSCarsten Otte 		}
462127e0393fSCarsten Otte 
462227e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
462327e0393fSCarsten Otte 			r = -EINVAL;
462427e0393fSCarsten Otte 			break;
462527e0393fSCarsten Otte 		}
462627e0393fSCarsten Otte 
462727e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
462827e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
462927e0393fSCarsten Otte 		break;
463027e0393fSCarsten Otte 	}
463127e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
463227e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
463327e0393fSCarsten Otte 
463427e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
463527e0393fSCarsten Otte 			r = -EFAULT;
463627e0393fSCarsten Otte 			break;
463727e0393fSCarsten Otte 		}
463827e0393fSCarsten Otte 
463927e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
464027e0393fSCarsten Otte 			r = -EINVAL;
464127e0393fSCarsten Otte 			break;
464227e0393fSCarsten Otte 		}
464327e0393fSCarsten Otte 
464427e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
464527e0393fSCarsten Otte 			ucasmap.length);
464627e0393fSCarsten Otte 		break;
464727e0393fSCarsten Otte 	}
464827e0393fSCarsten Otte #endif
4649ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
4650527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
4651ccc7910fSCarsten Otte 		break;
4652ccc7910fSCarsten Otte 	}
4653d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
4654d6712df9SCornelia Huck 	{
4655d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
4656d6712df9SCornelia Huck 		r = -EFAULT;
4657d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
4658d6712df9SCornelia Huck 			break;
4659d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4660d6712df9SCornelia Huck 		break;
4661d6712df9SCornelia Huck 	}
466241408c28SThomas Huth 	case KVM_S390_MEM_OP: {
466341408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
466441408c28SThomas Huth 
466541408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
466641408c28SThomas Huth 			r = kvm_s390_guest_mem_op(vcpu, &mem_op);
466741408c28SThomas Huth 		else
466841408c28SThomas Huth 			r = -EFAULT;
466941408c28SThomas Huth 		break;
467041408c28SThomas Huth 	}
4671816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
4672816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
4673816c7667SJens Freimann 
4674816c7667SJens Freimann 		r = -EFAULT;
4675816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4676816c7667SJens Freimann 			break;
4677816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
4678816c7667SJens Freimann 		    irq_state.len == 0 ||
4679816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
4680816c7667SJens Freimann 			r = -EINVAL;
4681816c7667SJens Freimann 			break;
4682816c7667SJens Freimann 		}
4683bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
4684816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
4685816c7667SJens Freimann 					   (void __user *) irq_state.buf,
4686816c7667SJens Freimann 					   irq_state.len);
4687816c7667SJens Freimann 		break;
4688816c7667SJens Freimann 	}
4689816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
4690816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
4691816c7667SJens Freimann 
4692816c7667SJens Freimann 		r = -EFAULT;
4693816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4694816c7667SJens Freimann 			break;
4695816c7667SJens Freimann 		if (irq_state.len == 0) {
4696816c7667SJens Freimann 			r = -EINVAL;
4697816c7667SJens Freimann 			break;
4698816c7667SJens Freimann 		}
4699bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
4700816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
4701816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
4702816c7667SJens Freimann 					   irq_state.len);
4703816c7667SJens Freimann 		break;
4704816c7667SJens Freimann 	}
4705b0c632dbSHeiko Carstens 	default:
47063e6afcf1SCarsten Otte 		r = -ENOTTY;
4707b0c632dbSHeiko Carstens 	}
47089b062471SChristoffer Dall 
47099b062471SChristoffer Dall 	vcpu_put(vcpu);
4710bc923cc9SAvi Kivity 	return r;
4711b0c632dbSHeiko Carstens }
4712b0c632dbSHeiko Carstens 
47131499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
47145b1c1493SCarsten Otte {
47155b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
47165b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
47175b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
47185b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
47195b1c1493SCarsten Otte 		get_page(vmf->page);
47205b1c1493SCarsten Otte 		return 0;
47215b1c1493SCarsten Otte 	}
47225b1c1493SCarsten Otte #endif
47235b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
47245b1c1493SCarsten Otte }
47255b1c1493SCarsten Otte 
47265587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
47275587027cSAneesh Kumar K.V 			    unsigned long npages)
4728db3fe4ebSTakuya Yoshikawa {
4729db3fe4ebSTakuya Yoshikawa 	return 0;
4730db3fe4ebSTakuya Yoshikawa }
4731db3fe4ebSTakuya Yoshikawa 
4732b0c632dbSHeiko Carstens /* Section: memory related */
4733f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
4734f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
473509170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
47367b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
4737b0c632dbSHeiko Carstens {
4738dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
4739dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
4740dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
4741dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
4742b0c632dbSHeiko Carstens 
4743598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
4744b0c632dbSHeiko Carstens 		return -EINVAL;
4745b0c632dbSHeiko Carstens 
4746598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
4747b0c632dbSHeiko Carstens 		return -EINVAL;
4748b0c632dbSHeiko Carstens 
4749a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
4750a3a92c31SDominik Dingel 		return -EINVAL;
4751a3a92c31SDominik Dingel 
4752*29b40f10SJanosch Frank 	/* When we are protected, we should not change the memory slots */
4753*29b40f10SJanosch Frank 	if (kvm_s390_pv_get_handle(kvm))
4754*29b40f10SJanosch Frank 		return -EINVAL;
4755f7784b8eSMarcelo Tosatti 	return 0;
4756f7784b8eSMarcelo Tosatti }
4757f7784b8eSMarcelo Tosatti 
4758f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
475909170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
47608482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
4761f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
47628482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
4763f7784b8eSMarcelo Tosatti {
476419ec166cSChristian Borntraeger 	int rc = 0;
4765f7784b8eSMarcelo Tosatti 
476619ec166cSChristian Borntraeger 	switch (change) {
476719ec166cSChristian Borntraeger 	case KVM_MR_DELETE:
476819ec166cSChristian Borntraeger 		rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
476919ec166cSChristian Borntraeger 					old->npages * PAGE_SIZE);
477019ec166cSChristian Borntraeger 		break;
477119ec166cSChristian Borntraeger 	case KVM_MR_MOVE:
477219ec166cSChristian Borntraeger 		rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
477319ec166cSChristian Borntraeger 					old->npages * PAGE_SIZE);
477419ec166cSChristian Borntraeger 		if (rc)
477519ec166cSChristian Borntraeger 			break;
477619ec166cSChristian Borntraeger 		/* FALLTHROUGH */
477719ec166cSChristian Borntraeger 	case KVM_MR_CREATE:
4778598841caSCarsten Otte 		rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
4779598841caSCarsten Otte 				      mem->guest_phys_addr, mem->memory_size);
478019ec166cSChristian Borntraeger 		break;
478119ec166cSChristian Borntraeger 	case KVM_MR_FLAGS_ONLY:
478219ec166cSChristian Borntraeger 		break;
478319ec166cSChristian Borntraeger 	default:
478419ec166cSChristian Borntraeger 		WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
478519ec166cSChristian Borntraeger 	}
4786598841caSCarsten Otte 	if (rc)
4787ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
4788598841caSCarsten Otte 	return;
4789b0c632dbSHeiko Carstens }
4790b0c632dbSHeiko Carstens 
479160a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
479260a37709SAlexander Yarygin {
479360a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
479460a37709SAlexander Yarygin 
479560a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
479660a37709SAlexander Yarygin }
479760a37709SAlexander Yarygin 
47983491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
47993491caf2SChristian Borntraeger {
48003491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
48013491caf2SChristian Borntraeger }
48023491caf2SChristian Borntraeger 
4803b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
4804b0c632dbSHeiko Carstens {
480560a37709SAlexander Yarygin 	int i;
480660a37709SAlexander Yarygin 
480707197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
48088d43d570SMichael Mueller 		pr_info("SIE is not available\n");
480907197fd0SDavid Hildenbrand 		return -ENODEV;
481007197fd0SDavid Hildenbrand 	}
481107197fd0SDavid Hildenbrand 
4812a4499382SJanosch Frank 	if (nested && hpage) {
48138d43d570SMichael Mueller 		pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
4814a4499382SJanosch Frank 		return -EINVAL;
4815a4499382SJanosch Frank 	}
4816a4499382SJanosch Frank 
481760a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
4818c3b9e3e1SChristian Borntraeger 		kvm_s390_fac_base[i] |=
481960a37709SAlexander Yarygin 			S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
482060a37709SAlexander Yarygin 
48219d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
4822b0c632dbSHeiko Carstens }
4823b0c632dbSHeiko Carstens 
4824b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
4825b0c632dbSHeiko Carstens {
4826b0c632dbSHeiko Carstens 	kvm_exit();
4827b0c632dbSHeiko Carstens }
4828b0c632dbSHeiko Carstens 
4829b0c632dbSHeiko Carstens module_init(kvm_s390_init);
4830b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
4831566af940SCornelia Huck 
4832566af940SCornelia Huck /*
4833566af940SCornelia Huck  * Enable autoloading of the kvm module.
4834566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
4835566af940SCornelia Huck  * since x86 takes a different approach.
4836566af940SCornelia Huck  */
4837566af940SCornelia Huck #include <linux/miscdevice.h>
4838566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
4839566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
4840