1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b0c632dbSHeiko Carstens /*
3bb64da9aSChristian Borntraeger * hosting IBM Z kernel virtual machines (s390x)
4b0c632dbSHeiko Carstens *
53e6c5568SJanosch Frank * Copyright IBM Corp. 2008, 2020
6b0c632dbSHeiko Carstens *
7b0c632dbSHeiko Carstens * Author(s): Carsten Otte <cotte@de.ibm.com>
8b0c632dbSHeiko Carstens * Christian Borntraeger <borntraeger@de.ibm.com>
9628eb9b8SChristian Ehrhardt * Christian Ehrhardt <ehrhardt@de.ibm.com>
1015f36ebdSJason J. Herne * Jason J. Herne <jjherne@us.ibm.com>
11b0c632dbSHeiko Carstens */
12b0c632dbSHeiko Carstens
137aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390"
147aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
157aedd9d4SMichael Mueller
16b0c632dbSHeiko Carstens #include <linux/compiler.h>
17b0c632dbSHeiko Carstens #include <linux/err.h>
18b0c632dbSHeiko Carstens #include <linux/fs.h>
19ca872302SChristian Borntraeger #include <linux/hrtimer.h>
20b0c632dbSHeiko Carstens #include <linux/init.h>
21b0c632dbSHeiko Carstens #include <linux/kvm.h>
22b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
23b2d73b2aSMartin Schwidefsky #include <linux/mman.h>
24b0c632dbSHeiko Carstens #include <linux/module.h>
25d3217967SPaul Gortmaker #include <linux/moduleparam.h>
26a374e892STony Krowiak #include <linux/random.h>
27b0c632dbSHeiko Carstens #include <linux/slab.h>
28ba5c1e9bSCarsten Otte #include <linux/timer.h>
2941408c28SThomas Huth #include <linux/vmalloc.h>
3015c9705fSDavid Hildenbrand #include <linux/bitmap.h>
31174cd4b1SIngo Molnar #include <linux/sched/signal.h>
32190df4a2SClaudio Imbrenda #include <linux/string.h>
3365fddcfcSMike Rapoport #include <linux/pgtable.h>
34ca2fd060SClaudio Imbrenda #include <linux/mmu_notifier.h>
35174cd4b1SIngo Molnar
36cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
37b0c632dbSHeiko Carstens #include <asm/lowcore.h>
38fd5ada04SMartin Schwidefsky #include <asm/stp.h>
391e133ab2SMartin Schwidefsky #include <asm/gmap.h>
40f5daba1dSHeiko Carstens #include <asm/nmi.h>
41a0616cdeSDavid Howells #include <asm/switch_to.h>
426d3da241SJens Freimann #include <asm/isc.h>
431526bf9cSChristian Borntraeger #include <asm/sclp.h>
440a763c78SDavid Hildenbrand #include <asm/cpacf.h>
45221bb8a4SLinus Torvalds #include <asm/timex.h>
46e585b24aSTony Krowiak #include <asm/ap.h>
4729b40f10SJanosch Frank #include <asm/uv.h>
4856e62a73SSven Schnelle #include <asm/fpu/api.h>
498f2abe6aSChristian Borntraeger #include "kvm-s390.h"
50b0c632dbSHeiko Carstens #include "gaccess.h"
5198b1d33dSMatthew Rosato #include "pci.h"
52b0c632dbSHeiko Carstens
535786fffaSCornelia Huck #define CREATE_TRACE_POINTS
545786fffaSCornelia Huck #include "trace.h"
55ade38c31SCornelia Huck #include "trace-s390.h"
565786fffaSCornelia Huck
5741408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
58816c7667SJens Freimann #define LOCAL_IRQS 32
59816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
60816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS))
6141408c28SThomas Huth
62fcfe1baeSJing Zhang const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
63fcfe1baeSJing Zhang KVM_GENERIC_VM_STATS(),
64fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_io),
65fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_float_mchk),
66fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_pfault_done),
67fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_service_signal),
6873f91b00SMatthew Rosato STATS_DESC_COUNTER(VM, inject_virtio),
6999b86c9bSNico Boehr STATS_DESC_COUNTER(VM, aen_forward),
7099b86c9bSNico Boehr STATS_DESC_COUNTER(VM, gmap_shadow_reuse),
7199b86c9bSNico Boehr STATS_DESC_COUNTER(VM, gmap_shadow_create),
7299b86c9bSNico Boehr STATS_DESC_COUNTER(VM, gmap_shadow_r1_entry),
7399b86c9bSNico Boehr STATS_DESC_COUNTER(VM, gmap_shadow_r2_entry),
7499b86c9bSNico Boehr STATS_DESC_COUNTER(VM, gmap_shadow_r3_entry),
7599b86c9bSNico Boehr STATS_DESC_COUNTER(VM, gmap_shadow_sg_entry),
7699b86c9bSNico Boehr STATS_DESC_COUNTER(VM, gmap_shadow_pg_entry),
77fcfe1baeSJing Zhang };
78fcfe1baeSJing Zhang
79fcfe1baeSJing Zhang const struct kvm_stats_header kvm_vm_stats_header = {
80fcfe1baeSJing Zhang .name_size = KVM_STATS_NAME_SIZE,
81fcfe1baeSJing Zhang .num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
82fcfe1baeSJing Zhang .id_offset = sizeof(struct kvm_stats_header),
83fcfe1baeSJing Zhang .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
84fcfe1baeSJing Zhang .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
85fcfe1baeSJing Zhang sizeof(kvm_vm_stats_desc),
86fcfe1baeSJing Zhang };
87fcfe1baeSJing Zhang
88ce55c049SJing Zhang const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
89ce55c049SJing Zhang KVM_GENERIC_VCPU_STATS(),
90ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_userspace),
91ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_null),
92ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_external_request),
93ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_io_request),
94ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_external_interrupt),
95ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_stop_request),
96ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_validity),
97ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_instruction),
98ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_pei),
99ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, halt_no_poll_steal),
100ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lctl),
101ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lctlg),
102ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stctl),
103ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stctg),
104ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_program_interruption),
105ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_instr_and_program),
106ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_operation_exception),
107ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_ckc),
108ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_cputm),
109ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_external_call),
110ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_emergency_signal),
111ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_service_signal),
112ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_virtio),
113ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_stop_signal),
114ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_prefix_signal),
115ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_restart_signal),
116ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_program),
117ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_io),
118ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_machine_check),
119ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_wait_state),
120ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_ckc),
121ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_cputm),
122ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_external_call),
123ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_emergency_signal),
124ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_mchk),
125ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_pfault_init),
126ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_program),
127ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_restart),
128ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_set_prefix),
129ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_stop_signal),
130ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_epsw),
131ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_gs),
132ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_io_other),
133ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lpsw),
134ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lpswe),
135*f4513867SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_lpswey),
136ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_pfmf),
137ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ptff),
138ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sck),
139ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sckpf),
140ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stidp),
141ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_spx),
142ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stpx),
143ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stap),
144ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_iske),
145ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ri),
146ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_rrbe),
147ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sske),
148ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock),
149ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stsi),
150ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stfl),
151ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tb),
152ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tpi),
153ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tprot),
154ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tsch),
155ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sie),
156ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_essa),
157ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sthyi),
158ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_sense),
159ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running),
160ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call),
161ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency),
162ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency),
163ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_start),
164ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_stop),
165ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status),
166ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status),
167ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status),
168ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_arch),
169ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix),
170ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_restart),
171ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset),
172ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset),
173ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown),
174bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_10),
175bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_44),
176bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c),
177bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, diag_9c_ignored),
178bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, diag_9c_forward),
179bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_258),
180bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_308),
181bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_500),
182bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_other),
183ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, pfault_sync)
184ce55c049SJing Zhang };
185ce55c049SJing Zhang
186ce55c049SJing Zhang const struct kvm_stats_header kvm_vcpu_stats_header = {
187ce55c049SJing Zhang .name_size = KVM_STATS_NAME_SIZE,
188ce55c049SJing Zhang .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
189ce55c049SJing Zhang .id_offset = sizeof(struct kvm_stats_header),
190ce55c049SJing Zhang .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
191ce55c049SJing Zhang .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
192ce55c049SJing Zhang sizeof(kvm_vcpu_stats_desc),
193ce55c049SJing Zhang };
194ce55c049SJing Zhang
195a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
196a411edf1SDavid Hildenbrand static int nested;
197a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
198a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
199a411edf1SDavid Hildenbrand
200a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */
201a4499382SJanosch Frank static int hpage;
202a4499382SJanosch Frank module_param(hpage, int, 0444);
203a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support");
204b0c632dbSHeiko Carstens
2058b905d28SChristian Borntraeger /* maximum percentage of steal time for polling. >100 is treated like 100 */
2068b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10;
2078b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644);
208b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
2098b905d28SChristian Borntraeger
210cc674ef2SMichael Mueller /* if set to true, the GISA will be initialized and used if available */
211cc674ef2SMichael Mueller static bool use_gisa = true;
212cc674ef2SMichael Mueller module_param(use_gisa, bool, 0644);
213cc674ef2SMichael Mueller MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it.");
214cc674ef2SMichael Mueller
21587e28a15SPierre Morel /* maximum diag9c forwarding per second */
21687e28a15SPierre Morel unsigned int diag9c_forwarding_hz;
21787e28a15SPierre Morel module_param(diag9c_forwarding_hz, uint, 0644);
21887e28a15SPierre Morel MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off");
21987e28a15SPierre Morel
220c3b9e3e1SChristian Borntraeger /*
221cc726886SClaudio Imbrenda * allow asynchronous deinit for protected guests; enable by default since
222cc726886SClaudio Imbrenda * the feature is opt-in anyway
223cc726886SClaudio Imbrenda */
224cc726886SClaudio Imbrenda static int async_destroy = 1;
225cc726886SClaudio Imbrenda module_param(async_destroy, int, 0444);
226cc726886SClaudio Imbrenda MODULE_PARM_DESC(async_destroy, "Asynchronous destroy for protected guests");
227fb491d55SClaudio Imbrenda
228c3b9e3e1SChristian Borntraeger /*
229c3b9e3e1SChristian Borntraeger * For now we handle at most 16 double words as this is what the s390 base
230c3b9e3e1SChristian Borntraeger * kernel handles and stores in the prefix page. If we ever need to go beyond
231c3b9e3e1SChristian Borntraeger * this, this requires changes to code, but the external uapi can stay.
232c3b9e3e1SChristian Borntraeger */
233c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16
234c3b9e3e1SChristian Borntraeger
235c3b9e3e1SChristian Borntraeger /*
236c3b9e3e1SChristian Borntraeger * Base feature mask that defines default mask for facilities. Consists of the
237c3b9e3e1SChristian Borntraeger * defines in FACILITIES_KVM and the non-hypervisor managed bits.
238c3b9e3e1SChristian Borntraeger */
239c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
240c3b9e3e1SChristian Borntraeger /*
241c3b9e3e1SChristian Borntraeger * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
242c3b9e3e1SChristian Borntraeger * and defines the facilities that can be enabled via a cpu model.
243c3b9e3e1SChristian Borntraeger */
244c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
245c3b9e3e1SChristian Borntraeger
kvm_s390_fac_size(void)246c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void)
24778c4b59fSMichael Mueller {
248c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
249c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
250c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
25117e89e13SSven Schnelle sizeof(stfle_fac_list));
252c3b9e3e1SChristian Borntraeger
253c3b9e3e1SChristian Borntraeger return SIZE_INTERNAL;
25478c4b59fSMichael Mueller }
25578c4b59fSMichael Mueller
25615c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
25715c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
2580a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
2590a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
26015c9705fSDavid Hildenbrand
2619d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
262a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
26378f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
2643e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv;
2659d8d5786SMichael Mueller
266b0c632dbSHeiko Carstens /* Section: not file related */
26729b40f10SJanosch Frank /* forward declarations */
268414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
269414d3b07SMartin Schwidefsky unsigned long end);
27029b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm);
2712c70fe44SChristian Borntraeger
kvm_clock_sync_scb(struct kvm_s390_sie_block * scb,u64 delta)2721575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
2731575767eSDavid Hildenbrand {
2741575767eSDavid Hildenbrand u8 delta_idx = 0;
2751575767eSDavid Hildenbrand
2761575767eSDavid Hildenbrand /*
2771575767eSDavid Hildenbrand * The TOD jumps by delta, we have to compensate this by adding
2781575767eSDavid Hildenbrand * -delta to the epoch.
2791575767eSDavid Hildenbrand */
2801575767eSDavid Hildenbrand delta = -delta;
2811575767eSDavid Hildenbrand
2821575767eSDavid Hildenbrand /* sign-extension - we're adding to signed values below */
2831575767eSDavid Hildenbrand if ((s64)delta < 0)
2841575767eSDavid Hildenbrand delta_idx = -1;
2851575767eSDavid Hildenbrand
2861575767eSDavid Hildenbrand scb->epoch += delta;
2871575767eSDavid Hildenbrand if (scb->ecd & ECD_MEF) {
2881575767eSDavid Hildenbrand scb->epdx += delta_idx;
2891575767eSDavid Hildenbrand if (scb->epoch < delta)
2901575767eSDavid Hildenbrand scb->epdx += 1;
2911575767eSDavid Hildenbrand }
2921575767eSDavid Hildenbrand }
2931575767eSDavid Hildenbrand
294fdf03650SFan Zhang /*
295fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore
296fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to
297fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs,
298fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch.
299fdf03650SFan Zhang */
kvm_clock_sync(struct notifier_block * notifier,unsigned long val,void * v)300fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
301fdf03650SFan Zhang void *v)
302fdf03650SFan Zhang {
303fdf03650SFan Zhang struct kvm *kvm;
304fdf03650SFan Zhang struct kvm_vcpu *vcpu;
30546808a4cSMarc Zyngier unsigned long i;
306fdf03650SFan Zhang unsigned long long *delta = v;
307fdf03650SFan Zhang
308fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) {
309fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) {
3101575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
3111575767eSDavid Hildenbrand if (i == 0) {
3121575767eSDavid Hildenbrand kvm->arch.epoch = vcpu->arch.sie_block->epoch;
3131575767eSDavid Hildenbrand kvm->arch.epdx = vcpu->arch.sie_block->epdx;
3141575767eSDavid Hildenbrand }
315db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled)
316db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta;
31791473b48SDavid Hildenbrand if (vcpu->arch.vsie_block)
3181575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.vsie_block,
3191575767eSDavid Hildenbrand *delta);
320fdf03650SFan Zhang }
321fdf03650SFan Zhang }
322fdf03650SFan Zhang return NOTIFY_OK;
323fdf03650SFan Zhang }
324fdf03650SFan Zhang
325fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
326fdf03650SFan Zhang .notifier_call = kvm_clock_sync,
327fdf03650SFan Zhang };
328fdf03650SFan Zhang
allow_cpu_feat(unsigned long nr)32922be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
33022be5a13SDavid Hildenbrand {
33122be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat);
33222be5a13SDavid Hildenbrand }
33322be5a13SDavid Hildenbrand
plo_test_bit(unsigned char nr)3340a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
3350a763c78SDavid Hildenbrand {
3364fa3b91bSHeiko Carstens unsigned long function = (unsigned long)nr | 0x100;
337d051ae53SHeiko Carstens int cc;
3380a763c78SDavid Hildenbrand
3390a763c78SDavid Hildenbrand asm volatile(
3404fa3b91bSHeiko Carstens " lgr 0,%[function]\n"
3410a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */
3420a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n"
3430a763c78SDavid Hildenbrand " ipm %0\n"
3440a763c78SDavid Hildenbrand " srl %0,28\n"
3450a763c78SDavid Hildenbrand : "=d" (cc)
3464fa3b91bSHeiko Carstens : [function] "d" (function)
3474fa3b91bSHeiko Carstens : "cc", "0");
3480a763c78SDavid Hildenbrand return cc == 0;
3490a763c78SDavid Hildenbrand }
3500a763c78SDavid Hildenbrand
__insn32_query(unsigned int opcode,u8 * query)351d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
352d6681397SChristian Borntraeger {
353d6681397SChristian Borntraeger asm volatile(
3544fa3b91bSHeiko Carstens " lghi 0,0\n"
3554fa3b91bSHeiko Carstens " lgr 1,%[query]\n"
3564fa3b91bSHeiko Carstens /* Parameter registers are ignored */
357d6681397SChristian Borntraeger " .insn rrf,%[opc] << 16,2,4,6,0\n"
358b1c41ac3SHeiko Carstens :
3594fa3b91bSHeiko Carstens : [query] "d" ((unsigned long)query), [opc] "i" (opcode)
3604fa3b91bSHeiko Carstens : "cc", "memory", "0", "1");
361d6681397SChristian Borntraeger }
362d6681397SChristian Borntraeger
363173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938
3644f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939
365173aec2dSChristian Borntraeger
kvm_s390_cpu_feat_init(void)3666c30cd2eSSean Christopherson static void __init kvm_s390_cpu_feat_init(void)
36722be5a13SDavid Hildenbrand {
3680a763c78SDavid Hildenbrand int i;
3690a763c78SDavid Hildenbrand
3700a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) {
3710a763c78SDavid Hildenbrand if (plo_test_bit(i))
3720a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
3730a763c78SDavid Hildenbrand }
3740a763c78SDavid Hildenbrand
3750a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */
376221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff,
377221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff),
378221bb8a4SLinus Torvalds PTFF_QAF);
3790a763c78SDavid Hildenbrand
3800a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */
38169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
38269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac);
38369c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *)
38469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc);
38569c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *)
38669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km);
38769c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
38869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd);
38969c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
39069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd);
3910a763c78SDavid Hildenbrand }
3920a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */
39369c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
39469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo);
3950a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */
39669c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
39769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr);
39869c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *)
39969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf);
40069c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *)
40169c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo);
40269c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *)
40369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc);
4040a763c78SDavid Hildenbrand }
4050a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */
406985a9d20SHarald Freudenberger __cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
40769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno);
4080a763c78SDavid Hildenbrand
409e000b8e0SJason J. Herne if (test_facility(146)) /* MSA8 */
410e000b8e0SJason J. Herne __cpacf_query(CPACF_KMA, (cpacf_mask_t *)
411e000b8e0SJason J. Herne kvm_s390_available_subfunc.kma);
412e000b8e0SJason J. Herne
41313209ad0SChristian Borntraeger if (test_facility(155)) /* MSA9 */
41413209ad0SChristian Borntraeger __cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
41513209ad0SChristian Borntraeger kvm_s390_available_subfunc.kdsa);
41613209ad0SChristian Borntraeger
417173aec2dSChristian Borntraeger if (test_facility(150)) /* SORTL */
418173aec2dSChristian Borntraeger __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl);
419173aec2dSChristian Borntraeger
4204f45b90eSChristian Borntraeger if (test_facility(151)) /* DFLTCC */
4214f45b90eSChristian Borntraeger __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
4224f45b90eSChristian Borntraeger
42322be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP)
42422be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
425a3508fbeSDavid Hildenbrand /*
426a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
427a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
428a3508fbeSDavid Hildenbrand */
429a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
430a411edf1SDavid Hildenbrand !test_facility(3) || !nested)
431a3508fbeSDavid Hildenbrand return;
432a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
43319c439b5SDavid Hildenbrand if (sclp.has_64bscao)
43419c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
4350615a326SDavid Hildenbrand if (sclp.has_siif)
4360615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
43777d18f6dSDavid Hildenbrand if (sclp.has_gpere)
43877d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
439a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls)
440a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
4415630a8e8SDavid Hildenbrand if (sclp.has_ib)
4425630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
44313ee3f67SDavid Hildenbrand if (sclp.has_cei)
44413ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
4457fd7f39dSDavid Hildenbrand if (sclp.has_ibs)
4467fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
447730cd632SFarhan Ali if (sclp.has_kss)
448730cd632SFarhan Ali allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
4495d3876a8SDavid Hildenbrand /*
4505d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
4515d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE
4525d3876a8SDavid Hildenbrand * instead of the real storage key.
4535d3876a8SDavid Hildenbrand *
4545d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
4555d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident.
4565d3876a8SDavid Hildenbrand *
4575d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
4585d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
4595d3876a8SDavid Hildenbrand *
4605d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
4615d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
4625d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
4635d3876a8SDavid Hildenbrand *
4645d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
4655d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock.
4665d3876a8SDavid Hildenbrand */
46722be5a13SDavid Hildenbrand }
46822be5a13SDavid Hildenbrand
__kvm_s390_init(void)4696c30cd2eSSean Christopherson static int __init __kvm_s390_init(void)
470b0c632dbSHeiko Carstens {
471f76f6371SJanosch Frank int rc = -ENOMEM;
472308c3e66SMichael Mueller
47378f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
47478f26131SChristian Borntraeger if (!kvm_s390_dbf)
47578f26131SChristian Borntraeger return -ENOMEM;
47678f26131SChristian Borntraeger
4773e6c5568SJanosch Frank kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
4783e6c5568SJanosch Frank if (!kvm_s390_dbf_uv)
479b801ef42SSean Christopherson goto err_kvm_uv;
4803e6c5568SJanosch Frank
4813e6c5568SJanosch Frank if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
4823e6c5568SJanosch Frank debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
483b801ef42SSean Christopherson goto err_debug_view;
48478f26131SChristian Borntraeger
48522be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init();
48622be5a13SDavid Hildenbrand
48784877d93SCornelia Huck /* Register floating interrupt controller interface. */
488308c3e66SMichael Mueller rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
489308c3e66SMichael Mueller if (rc) {
4908d43d570SMichael Mueller pr_err("A FLIC registration call failed with rc=%d\n", rc);
491b801ef42SSean Christopherson goto err_flic;
492308c3e66SMichael Mueller }
493b1d1e76eSMichael Mueller
494189e7d87SMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
49598b1d33dSMatthew Rosato rc = kvm_s390_pci_init();
49698b1d33dSMatthew Rosato if (rc) {
49798b1d33dSMatthew Rosato pr_err("Unable to allocate AIFT for PCI\n");
498b801ef42SSean Christopherson goto err_pci;
49998b1d33dSMatthew Rosato }
50098b1d33dSMatthew Rosato }
50198b1d33dSMatthew Rosato
502b1d1e76eSMichael Mueller rc = kvm_s390_gib_init(GAL_ISC);
503b1d1e76eSMichael Mueller if (rc)
504b801ef42SSean Christopherson goto err_gib;
505b1d1e76eSMichael Mueller
506e43f5762SSean Christopherson gmap_notifier.notifier_call = kvm_gmap_notifier;
507e43f5762SSean Christopherson gmap_register_pte_notifier(&gmap_notifier);
508e43f5762SSean Christopherson vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
509e43f5762SSean Christopherson gmap_register_pte_notifier(&vsie_gmap_notifier);
510e43f5762SSean Christopherson atomic_notifier_chain_register(&s390_epoch_delta_notifier,
511e43f5762SSean Christopherson &kvm_clock_notifier);
512e43f5762SSean Christopherson
513308c3e66SMichael Mueller return 0;
514308c3e66SMichael Mueller
515b801ef42SSean Christopherson err_gib:
516b801ef42SSean Christopherson if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
517b801ef42SSean Christopherson kvm_s390_pci_exit();
518b801ef42SSean Christopherson err_pci:
519b801ef42SSean Christopherson err_flic:
520b801ef42SSean Christopherson err_debug_view:
521b801ef42SSean Christopherson debug_unregister(kvm_s390_dbf_uv);
522b801ef42SSean Christopherson err_kvm_uv:
523b801ef42SSean Christopherson debug_unregister(kvm_s390_dbf);
524308c3e66SMichael Mueller return rc;
525b0c632dbSHeiko Carstens }
526b0c632dbSHeiko Carstens
__kvm_s390_exit(void)527b8449265SSean Christopherson static void __kvm_s390_exit(void)
52878f26131SChristian Borntraeger {
529e43f5762SSean Christopherson gmap_unregister_pte_notifier(&gmap_notifier);
530e43f5762SSean Christopherson gmap_unregister_pte_notifier(&vsie_gmap_notifier);
531e43f5762SSean Christopherson atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
532e43f5762SSean Christopherson &kvm_clock_notifier);
533e43f5762SSean Christopherson
5341282c21eSMichael Mueller kvm_s390_gib_destroy();
535189e7d87SMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
53698b1d33dSMatthew Rosato kvm_s390_pci_exit();
53778f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf);
5383e6c5568SJanosch Frank debug_unregister(kvm_s390_dbf_uv);
53978f26131SChristian Borntraeger }
54078f26131SChristian Borntraeger
541b0c632dbSHeiko Carstens /* Section: device related */
kvm_arch_dev_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)542b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
543b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg)
544b0c632dbSHeiko Carstens {
545b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE)
546b0c632dbSHeiko Carstens return s390_enable_sie();
547b0c632dbSHeiko Carstens return -EINVAL;
548b0c632dbSHeiko Carstens }
549b0c632dbSHeiko Carstens
kvm_vm_ioctl_check_extension(struct kvm * kvm,long ext)550784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
551b0c632dbSHeiko Carstens {
552d7b0b5ebSCarsten Otte int r;
553d7b0b5ebSCarsten Otte
5542bd0ac4eSCarsten Otte switch (ext) {
555d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW:
556b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP:
55752e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU:
5581efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
5591efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL:
5601efd0f59SCarsten Otte #endif
5613c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF:
56260b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS:
56314eebd91SCarsten Otte case KVM_CAP_ONE_REG:
564d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP:
565fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT:
56610ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD:
567c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL:
56878599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP:
569f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES:
5706352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE:
571460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT:
57247b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ:
5732444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP:
574e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI:
57530ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS:
576816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE:
5776502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0:
5784036e387SClaudio Imbrenda case KVM_CAP_S390_CMMA_MIGRATION:
57947a4693eSYi Min Zhao case KVM_CAP_S390_AIS:
580da9a1446SChristian Borntraeger case KVM_CAP_S390_AIS_MIGRATION:
5817de3f142SJanosch Frank case KVM_CAP_S390_VCPU_RESETS:
582b9b2782cSPeter Xu case KVM_CAP_SET_GUEST_DEBUG:
58323a60f83SCollin Walling case KVM_CAP_S390_DIAG318:
58452882b9cSAlexey Kardashevskiy case KVM_CAP_IRQFD_RESAMPLE:
585d7b0b5ebSCarsten Otte r = 1;
586d7b0b5ebSCarsten Otte break;
587a43b80b7SMaxim Levitsky case KVM_CAP_SET_GUEST_DEBUG2:
588a43b80b7SMaxim Levitsky r = KVM_GUESTDBG_VALID_MASK;
589a43b80b7SMaxim Levitsky break;
590a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M:
591a4499382SJanosch Frank r = 0;
59240ebdb8eSJanosch Frank if (hpage && !kvm_is_ucontrol(kvm))
593a4499382SJanosch Frank r = 1;
594a4499382SJanosch Frank break;
59541408c28SThomas Huth case KVM_CAP_S390_MEM_OP:
59641408c28SThomas Huth r = MEM_OP_MAX_SIZE;
59741408c28SThomas Huth break;
5983fd49805SJanis Schoetterl-Glausch case KVM_CAP_S390_MEM_OP_EXTENSION:
5993fd49805SJanis Schoetterl-Glausch /*
6003fd49805SJanis Schoetterl-Glausch * Flag bits indicating which extensions are supported.
6013fd49805SJanis Schoetterl-Glausch * If r > 0, the base extension must also be supported/indicated,
6023fd49805SJanis Schoetterl-Glausch * in order to maintain backwards compatibility.
6033fd49805SJanis Schoetterl-Glausch */
6043fd49805SJanis Schoetterl-Glausch r = KVM_S390_MEMOP_EXTENSION_CAP_BASE |
6053fd49805SJanis Schoetterl-Glausch KVM_S390_MEMOP_EXTENSION_CAP_CMPXCHG;
6063fd49805SJanis Schoetterl-Glausch break;
607e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS:
608e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS:
609a86cb413SThomas Huth case KVM_CAP_MAX_VCPU_ID:
61076a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS;
611a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries())
612a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS;
613a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao)
61476a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS;
61582cc27efSVitaly Kuznetsov if (ext == KVM_CAP_NR_VCPUS)
61682cc27efSVitaly Kuznetsov r = min_t(unsigned int, num_online_cpus(), r);
617e726b1bdSChristian Borntraeger break;
6181526bf9cSChristian Borntraeger case KVM_CAP_S390_COW:
619abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP;
6201526bf9cSChristian Borntraeger break;
62168c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS:
62268c55750SEric Farman r = MACHINE_HAS_VX;
62368c55750SEric Farman break;
624c6e5f166SFan Zhang case KVM_CAP_S390_RI:
625c6e5f166SFan Zhang r = test_facility(64);
626c6e5f166SFan Zhang break;
6274e0b1ab7SFan Zhang case KVM_CAP_S390_GS:
6284e0b1ab7SFan Zhang r = test_facility(133);
6294e0b1ab7SFan Zhang break;
63035b3fde6SChristian Borntraeger case KVM_CAP_S390_BPB:
63135b3fde6SChristian Borntraeger r = test_facility(82);
63235b3fde6SChristian Borntraeger break;
6338c516b25SClaudio Imbrenda case KVM_CAP_S390_PROTECTED_ASYNC_DISABLE:
6348c516b25SClaudio Imbrenda r = async_destroy && is_prot_virt_host();
6358c516b25SClaudio Imbrenda break;
63613da9ae1SChristian Borntraeger case KVM_CAP_S390_PROTECTED:
63713da9ae1SChristian Borntraeger r = is_prot_virt_host();
63813da9ae1SChristian Borntraeger break;
639e9bf3acbSJanosch Frank case KVM_CAP_S390_PROTECTED_DUMP: {
640e9bf3acbSJanosch Frank u64 pv_cmds_dump[] = {
641e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_INIT,
642e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE,
643e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_CPU,
644e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_COMPLETE,
645e9bf3acbSJanosch Frank };
646e9bf3acbSJanosch Frank int i;
647e9bf3acbSJanosch Frank
648e9bf3acbSJanosch Frank r = is_prot_virt_host();
649e9bf3acbSJanosch Frank
650e9bf3acbSJanosch Frank for (i = 0; i < ARRAY_SIZE(pv_cmds_dump); i++) {
651e9bf3acbSJanosch Frank if (!test_bit_inv(pv_cmds_dump[i],
652e9bf3acbSJanosch Frank (unsigned long *)&uv_info.inst_calls_list)) {
653e9bf3acbSJanosch Frank r = 0;
654e9bf3acbSJanosch Frank break;
655e9bf3acbSJanosch Frank }
656e9bf3acbSJanosch Frank }
657e9bf3acbSJanosch Frank break;
658e9bf3acbSJanosch Frank }
659db1c875eSMatthew Rosato case KVM_CAP_S390_ZPCI_OP:
660db1c875eSMatthew Rosato r = kvm_s390_pci_interp_allowed();
661db1c875eSMatthew Rosato break;
662f5ecfee9SPierre Morel case KVM_CAP_S390_CPU_TOPOLOGY:
663f5ecfee9SPierre Morel r = test_facility(11);
664f5ecfee9SPierre Morel break;
6652bd0ac4eSCarsten Otte default:
666d7b0b5ebSCarsten Otte r = 0;
667b0c632dbSHeiko Carstens }
668d7b0b5ebSCarsten Otte return r;
6692bd0ac4eSCarsten Otte }
670b0c632dbSHeiko Carstens
kvm_arch_sync_dirty_log(struct kvm * kvm,struct kvm_memory_slot * memslot)6710dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
67215f36ebdSJason J. Herne {
6730959e168SJanosch Frank int i;
67415f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn;
6750959e168SJanosch Frank unsigned long gaddr, vmaddr;
67615f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap;
6770959e168SJanosch Frank DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
67815f36ebdSJason J. Herne
6790959e168SJanosch Frank /* Loop over all guest segments */
6800959e168SJanosch Frank cur_gfn = memslot->base_gfn;
68115f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages;
6820959e168SJanosch Frank for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
6830959e168SJanosch Frank gaddr = gfn_to_gpa(cur_gfn);
6840959e168SJanosch Frank vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
6850959e168SJanosch Frank if (kvm_is_error_hva(vmaddr))
6860959e168SJanosch Frank continue;
68715f36ebdSJason J. Herne
6880959e168SJanosch Frank bitmap_zero(bitmap, _PAGE_ENTRIES);
6890959e168SJanosch Frank gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
6900959e168SJanosch Frank for (i = 0; i < _PAGE_ENTRIES; i++) {
6910959e168SJanosch Frank if (test_bit(i, bitmap))
6920959e168SJanosch Frank mark_page_dirty(kvm, cur_gfn + i);
6930959e168SJanosch Frank }
6940959e168SJanosch Frank
6951763f8d0SChristian Borntraeger if (fatal_signal_pending(current))
6961763f8d0SChristian Borntraeger return;
69770c88a00SChristian Borntraeger cond_resched();
69815f36ebdSJason J. Herne }
69915f36ebdSJason J. Herne }
70015f36ebdSJason J. Herne
701b0c632dbSHeiko Carstens /* Section: vm related */
702a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
703a6e2f683SEugene (jno) Dvurechenski
704b0c632dbSHeiko Carstens /*
705b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot.
706b0c632dbSHeiko Carstens */
kvm_vm_ioctl_get_dirty_log(struct kvm * kvm,struct kvm_dirty_log * log)707b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
708b0c632dbSHeiko Carstens struct kvm_dirty_log *log)
709b0c632dbSHeiko Carstens {
71015f36ebdSJason J. Herne int r;
71115f36ebdSJason J. Herne unsigned long n;
71215f36ebdSJason J. Herne struct kvm_memory_slot *memslot;
7132a49f61dSSean Christopherson int is_dirty;
71415f36ebdSJason J. Herne
715e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm))
716e1e8a962SJanosch Frank return -EINVAL;
717e1e8a962SJanosch Frank
71815f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock);
71915f36ebdSJason J. Herne
72015f36ebdSJason J. Herne r = -EINVAL;
72115f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS)
72215f36ebdSJason J. Herne goto out;
72315f36ebdSJason J. Herne
7242a49f61dSSean Christopherson r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
72515f36ebdSJason J. Herne if (r)
72615f36ebdSJason J. Herne goto out;
72715f36ebdSJason J. Herne
72815f36ebdSJason J. Herne /* Clear the dirty log */
72915f36ebdSJason J. Herne if (is_dirty) {
73015f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot);
73115f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n);
73215f36ebdSJason J. Herne }
73315f36ebdSJason J. Herne r = 0;
73415f36ebdSJason J. Herne out:
73515f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock);
73615f36ebdSJason J. Herne return r;
737b0c632dbSHeiko Carstens }
738b0c632dbSHeiko Carstens
icpt_operexc_on_all_vcpus(struct kvm * kvm)7396502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
7406502a34cSDavid Hildenbrand {
74146808a4cSMarc Zyngier unsigned long i;
7426502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu;
7436502a34cSDavid Hildenbrand
7446502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) {
7456502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
7466502a34cSDavid Hildenbrand }
7476502a34cSDavid Hildenbrand }
7486502a34cSDavid Hildenbrand
kvm_vm_ioctl_enable_cap(struct kvm * kvm,struct kvm_enable_cap * cap)749e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
750d938dc55SCornelia Huck {
751d938dc55SCornelia Huck int r;
752d938dc55SCornelia Huck
753d938dc55SCornelia Huck if (cap->flags)
754d938dc55SCornelia Huck return -EINVAL;
755d938dc55SCornelia Huck
756d938dc55SCornelia Huck switch (cap->cap) {
75784223598SCornelia Huck case KVM_CAP_S390_IRQCHIP:
758c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
75984223598SCornelia Huck kvm->arch.use_irqchip = 1;
76084223598SCornelia Huck r = 0;
76184223598SCornelia Huck break;
7622444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP:
763c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
7642444b352SDavid Hildenbrand kvm->arch.user_sigp = 1;
7652444b352SDavid Hildenbrand r = 0;
7662444b352SDavid Hildenbrand break;
76768c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS:
7685967c17bSDavid Hildenbrand mutex_lock(&kvm->lock);
769a03825bbSPaolo Bonzini if (kvm->created_vcpus) {
7705967c17bSDavid Hildenbrand r = -EBUSY;
7715967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) {
772c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129);
773c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129);
7742f87d942SGuenther Hutzl if (test_facility(134)) {
7752f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134);
7762f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134);
7772f87d942SGuenther Hutzl }
77853743aa7SMaxim Samoylov if (test_facility(135)) {
77953743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135);
78053743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135);
78153743aa7SMaxim Samoylov }
7827832e91cSChristian Borntraeger if (test_facility(148)) {
7837832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 148);
7847832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 148);
7857832e91cSChristian Borntraeger }
786d5cb6ab1SChristian Borntraeger if (test_facility(152)) {
787d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 152);
788d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 152);
789d5cb6ab1SChristian Borntraeger }
7901f703d2cSChristian Borntraeger if (test_facility(192)) {
7911f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 192);
7921f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 192);
7931f703d2cSChristian Borntraeger }
79418280d8bSMichael Mueller r = 0;
79518280d8bSMichael Mueller } else
79618280d8bSMichael Mueller r = -EINVAL;
7975967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock);
798c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
799c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)");
80068c55750SEric Farman break;
801c6e5f166SFan Zhang case KVM_CAP_S390_RI:
802c6e5f166SFan Zhang r = -EINVAL;
803c6e5f166SFan Zhang mutex_lock(&kvm->lock);
804a03825bbSPaolo Bonzini if (kvm->created_vcpus) {
805c6e5f166SFan Zhang r = -EBUSY;
806c6e5f166SFan Zhang } else if (test_facility(64)) {
807c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64);
808c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64);
809c6e5f166SFan Zhang r = 0;
810c6e5f166SFan Zhang }
811c6e5f166SFan Zhang mutex_unlock(&kvm->lock);
812c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
813c6e5f166SFan Zhang r ? "(not available)" : "(success)");
814c6e5f166SFan Zhang break;
81547a4693eSYi Min Zhao case KVM_CAP_S390_AIS:
81647a4693eSYi Min Zhao mutex_lock(&kvm->lock);
81747a4693eSYi Min Zhao if (kvm->created_vcpus) {
81847a4693eSYi Min Zhao r = -EBUSY;
81947a4693eSYi Min Zhao } else {
82047a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72);
82147a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72);
82247a4693eSYi Min Zhao r = 0;
82347a4693eSYi Min Zhao }
82447a4693eSYi Min Zhao mutex_unlock(&kvm->lock);
82547a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s",
82647a4693eSYi Min Zhao r ? "(not available)" : "(success)");
82747a4693eSYi Min Zhao break;
8284e0b1ab7SFan Zhang case KVM_CAP_S390_GS:
8294e0b1ab7SFan Zhang r = -EINVAL;
8304e0b1ab7SFan Zhang mutex_lock(&kvm->lock);
831241e3ec0SChristian Borntraeger if (kvm->created_vcpus) {
8324e0b1ab7SFan Zhang r = -EBUSY;
8334e0b1ab7SFan Zhang } else if (test_facility(133)) {
8344e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133);
8354e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133);
8364e0b1ab7SFan Zhang r = 0;
8374e0b1ab7SFan Zhang }
8384e0b1ab7SFan Zhang mutex_unlock(&kvm->lock);
8394e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
8404e0b1ab7SFan Zhang r ? "(not available)" : "(success)");
8414e0b1ab7SFan Zhang break;
842a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M:
843a4499382SJanosch Frank mutex_lock(&kvm->lock);
844a4499382SJanosch Frank if (kvm->created_vcpus)
845a4499382SJanosch Frank r = -EBUSY;
84640ebdb8eSJanosch Frank else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
847a4499382SJanosch Frank r = -EINVAL;
848a4499382SJanosch Frank else {
849a4499382SJanosch Frank r = 0;
850d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm);
851a4499382SJanosch Frank kvm->mm->context.allow_gmap_hpage_1m = 1;
852d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm);
853a4499382SJanosch Frank /*
854a4499382SJanosch Frank * We might have to create fake 4k page
855a4499382SJanosch Frank * tables. To avoid that the hardware works on
856a4499382SJanosch Frank * stale PGSTEs, we emulate these instructions.
857a4499382SJanosch Frank */
858a4499382SJanosch Frank kvm->arch.use_skf = 0;
859a4499382SJanosch Frank kvm->arch.use_pfmfi = 0;
860a4499382SJanosch Frank }
861a4499382SJanosch Frank mutex_unlock(&kvm->lock);
862a4499382SJanosch Frank VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
863a4499382SJanosch Frank r ? "(not available)" : "(success)");
864a4499382SJanosch Frank break;
865e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI:
866c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
867e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1;
868e44fc8c9SEkaterina Tumanova r = 0;
869e44fc8c9SEkaterina Tumanova break;
8706502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0:
8716502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
8726502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1;
8736502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm);
8746502a34cSDavid Hildenbrand r = 0;
8756502a34cSDavid Hildenbrand break;
876f5ecfee9SPierre Morel case KVM_CAP_S390_CPU_TOPOLOGY:
877f5ecfee9SPierre Morel r = -EINVAL;
878f5ecfee9SPierre Morel mutex_lock(&kvm->lock);
879f5ecfee9SPierre Morel if (kvm->created_vcpus) {
880f5ecfee9SPierre Morel r = -EBUSY;
881f5ecfee9SPierre Morel } else if (test_facility(11)) {
882f5ecfee9SPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 11);
883f5ecfee9SPierre Morel set_kvm_facility(kvm->arch.model.fac_list, 11);
884f5ecfee9SPierre Morel r = 0;
885f5ecfee9SPierre Morel }
886f5ecfee9SPierre Morel mutex_unlock(&kvm->lock);
887f5ecfee9SPierre Morel VM_EVENT(kvm, 3, "ENABLE: CAP_S390_CPU_TOPOLOGY %s",
888f5ecfee9SPierre Morel r ? "(not available)" : "(success)");
889f5ecfee9SPierre Morel break;
890d938dc55SCornelia Huck default:
891d938dc55SCornelia Huck r = -EINVAL;
892d938dc55SCornelia Huck break;
893d938dc55SCornelia Huck }
894d938dc55SCornelia Huck return r;
895d938dc55SCornelia Huck }
896d938dc55SCornelia Huck
kvm_s390_get_mem_control(struct kvm * kvm,struct kvm_device_attr * attr)8978c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
8988c0a7ce6SDominik Dingel {
8998c0a7ce6SDominik Dingel int ret;
9008c0a7ce6SDominik Dingel
9018c0a7ce6SDominik Dingel switch (attr->attr) {
9028c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE:
9038c0a7ce6SDominik Dingel ret = 0;
904c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
905a3a92c31SDominik Dingel kvm->arch.mem_limit);
906a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
9078c0a7ce6SDominik Dingel ret = -EFAULT;
9088c0a7ce6SDominik Dingel break;
9098c0a7ce6SDominik Dingel default:
9108c0a7ce6SDominik Dingel ret = -ENXIO;
9118c0a7ce6SDominik Dingel break;
9128c0a7ce6SDominik Dingel }
9138c0a7ce6SDominik Dingel return ret;
9148c0a7ce6SDominik Dingel }
9158c0a7ce6SDominik Dingel
kvm_s390_set_mem_control(struct kvm * kvm,struct kvm_device_attr * attr)9168c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
9174f718eabSDominik Dingel {
9184f718eabSDominik Dingel int ret;
9194f718eabSDominik Dingel unsigned int idx;
9204f718eabSDominik Dingel switch (attr->attr) {
9214f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA:
922f9cbd9b0SDavid Hildenbrand ret = -ENXIO;
923c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma)
924e6db1d61SDominik Dingel break;
925e6db1d61SDominik Dingel
926c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
9274f718eabSDominik Dingel mutex_lock(&kvm->lock);
928a4499382SJanosch Frank if (kvm->created_vcpus)
929a4499382SJanosch Frank ret = -EBUSY;
930a4499382SJanosch Frank else if (kvm->mm->context.allow_gmap_hpage_1m)
931a4499382SJanosch Frank ret = -EINVAL;
932a4499382SJanosch Frank else {
9334f718eabSDominik Dingel kvm->arch.use_cmma = 1;
934c9f0a2b8SJanosch Frank /* Not compatible with cmma. */
935c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = 0;
9364f718eabSDominik Dingel ret = 0;
9374f718eabSDominik Dingel }
9384f718eabSDominik Dingel mutex_unlock(&kvm->lock);
9394f718eabSDominik Dingel break;
9404f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA:
941f9cbd9b0SDavid Hildenbrand ret = -ENXIO;
942f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma)
943f9cbd9b0SDavid Hildenbrand break;
944c3489155SDominik Dingel ret = -EINVAL;
945c3489155SDominik Dingel if (!kvm->arch.use_cmma)
946c3489155SDominik Dingel break;
947c3489155SDominik Dingel
948c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
9494f718eabSDominik Dingel mutex_lock(&kvm->lock);
9504f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu);
951a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm);
9524f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx);
9534f718eabSDominik Dingel mutex_unlock(&kvm->lock);
9544f718eabSDominik Dingel ret = 0;
9554f718eabSDominik Dingel break;
9568c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: {
9578c0a7ce6SDominik Dingel unsigned long new_limit;
9588c0a7ce6SDominik Dingel
9598c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm))
9608c0a7ce6SDominik Dingel return -EINVAL;
9618c0a7ce6SDominik Dingel
9628c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr))
9638c0a7ce6SDominik Dingel return -EFAULT;
9648c0a7ce6SDominik Dingel
965a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
966a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit)
9678c0a7ce6SDominik Dingel return -E2BIG;
9688c0a7ce6SDominik Dingel
969a3a92c31SDominik Dingel if (!new_limit)
970a3a92c31SDominik Dingel return -EINVAL;
971a3a92c31SDominik Dingel
9726ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */
973a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT)
974a3a92c31SDominik Dingel new_limit -= 1;
975a3a92c31SDominik Dingel
9768c0a7ce6SDominik Dingel ret = -EBUSY;
9778c0a7ce6SDominik Dingel mutex_lock(&kvm->lock);
978a03825bbSPaolo Bonzini if (!kvm->created_vcpus) {
9796ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */
9806ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit);
9818c0a7ce6SDominik Dingel
9828c0a7ce6SDominik Dingel if (!new) {
9838c0a7ce6SDominik Dingel ret = -ENOMEM;
9848c0a7ce6SDominik Dingel } else {
9856ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap);
9868c0a7ce6SDominik Dingel new->private = kvm;
9878c0a7ce6SDominik Dingel kvm->arch.gmap = new;
9888c0a7ce6SDominik Dingel ret = 0;
9898c0a7ce6SDominik Dingel }
9908c0a7ce6SDominik Dingel }
9918c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock);
992a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
993a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
994a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce);
9958c0a7ce6SDominik Dingel break;
9968c0a7ce6SDominik Dingel }
9974f718eabSDominik Dingel default:
9984f718eabSDominik Dingel ret = -ENXIO;
9994f718eabSDominik Dingel break;
10004f718eabSDominik Dingel }
10014f718eabSDominik Dingel return ret;
10024f718eabSDominik Dingel }
10034f718eabSDominik Dingel
1004a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
1005a374e892STony Krowiak
kvm_s390_vcpu_crypto_reset_all(struct kvm * kvm)100620c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
1007a374e892STony Krowiak {
1008a374e892STony Krowiak struct kvm_vcpu *vcpu;
100946808a4cSMarc Zyngier unsigned long i;
1010a374e892STony Krowiak
101120c922f0STony Krowiak kvm_s390_vcpu_block_all(kvm);
101220c922f0STony Krowiak
10133194cdb7SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) {
101420c922f0STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu);
10153194cdb7SDavid Hildenbrand /* recreate the shadow crycb by leaving the VSIE handler */
10163194cdb7SDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
10173194cdb7SDavid Hildenbrand }
101820c922f0STony Krowiak
101920c922f0STony Krowiak kvm_s390_vcpu_unblock_all(kvm);
102020c922f0STony Krowiak }
102120c922f0STony Krowiak
kvm_s390_vm_set_crypto(struct kvm * kvm,struct kvm_device_attr * attr)102220c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
102320c922f0STony Krowiak {
1024a374e892STony Krowiak mutex_lock(&kvm->lock);
1025a374e892STony Krowiak switch (attr->attr) {
1026a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
10278e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) {
10288e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock);
102937940fb0STony Krowiak return -EINVAL;
10308e41bd54SChristian Borntraeger }
1031a374e892STony Krowiak get_random_bytes(
1032a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1033a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1034a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1;
1035c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
1036a374e892STony Krowiak break;
1037a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
10388e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) {
10398e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock);
104037940fb0STony Krowiak return -EINVAL;
10418e41bd54SChristian Borntraeger }
1042a374e892STony Krowiak get_random_bytes(
1043a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1044a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
1045a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1;
1046c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
1047a374e892STony Krowiak break;
1048a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
10498e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) {
10508e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock);
105137940fb0STony Krowiak return -EINVAL;
10528e41bd54SChristian Borntraeger }
1053a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0;
1054a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
1055a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1056c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
1057a374e892STony Krowiak break;
1058a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
10598e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) {
10608e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock);
106137940fb0STony Krowiak return -EINVAL;
10628e41bd54SChristian Borntraeger }
1063a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0;
1064a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
1065a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
1066c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
1067a374e892STony Krowiak break;
106837940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE:
106937940fb0STony Krowiak if (!ap_instructions_available()) {
107037940fb0STony Krowiak mutex_unlock(&kvm->lock);
107137940fb0STony Krowiak return -EOPNOTSUPP;
107237940fb0STony Krowiak }
107337940fb0STony Krowiak kvm->arch.crypto.apie = 1;
107437940fb0STony Krowiak break;
107537940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE:
107637940fb0STony Krowiak if (!ap_instructions_available()) {
107737940fb0STony Krowiak mutex_unlock(&kvm->lock);
107837940fb0STony Krowiak return -EOPNOTSUPP;
107937940fb0STony Krowiak }
108037940fb0STony Krowiak kvm->arch.crypto.apie = 0;
108137940fb0STony Krowiak break;
1082a374e892STony Krowiak default:
1083a374e892STony Krowiak mutex_unlock(&kvm->lock);
1084a374e892STony Krowiak return -ENXIO;
1085a374e892STony Krowiak }
1086a374e892STony Krowiak
108720c922f0STony Krowiak kvm_s390_vcpu_crypto_reset_all(kvm);
1088a374e892STony Krowiak mutex_unlock(&kvm->lock);
1089a374e892STony Krowiak return 0;
1090a374e892STony Krowiak }
1091a374e892STony Krowiak
kvm_s390_vcpu_pci_setup(struct kvm_vcpu * vcpu)10923f4bbb43SMatthew Rosato static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu)
10933f4bbb43SMatthew Rosato {
10943f4bbb43SMatthew Rosato /* Only set the ECB bits after guest requests zPCI interpretation */
10953f4bbb43SMatthew Rosato if (!vcpu->kvm->arch.use_zpci_interp)
10963f4bbb43SMatthew Rosato return;
10973f4bbb43SMatthew Rosato
10983f4bbb43SMatthew Rosato vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI;
10993f4bbb43SMatthew Rosato vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI;
11003f4bbb43SMatthew Rosato }
11013f4bbb43SMatthew Rosato
kvm_s390_vcpu_pci_enable_interp(struct kvm * kvm)11023f4bbb43SMatthew Rosato void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm)
11033f4bbb43SMatthew Rosato {
11043f4bbb43SMatthew Rosato struct kvm_vcpu *vcpu;
11053f4bbb43SMatthew Rosato unsigned long i;
11063f4bbb43SMatthew Rosato
11073f4bbb43SMatthew Rosato lockdep_assert_held(&kvm->lock);
11083f4bbb43SMatthew Rosato
11093f4bbb43SMatthew Rosato if (!kvm_s390_pci_interp_allowed())
11103f4bbb43SMatthew Rosato return;
11113f4bbb43SMatthew Rosato
11123f4bbb43SMatthew Rosato /*
11133f4bbb43SMatthew Rosato * If host is configured for PCI and the necessary facilities are
11143f4bbb43SMatthew Rosato * available, turn on interpretation for the life of this guest
11153f4bbb43SMatthew Rosato */
11163f4bbb43SMatthew Rosato kvm->arch.use_zpci_interp = 1;
11173f4bbb43SMatthew Rosato
11183f4bbb43SMatthew Rosato kvm_s390_vcpu_block_all(kvm);
11193f4bbb43SMatthew Rosato
11203f4bbb43SMatthew Rosato kvm_for_each_vcpu(i, vcpu, kvm) {
11213f4bbb43SMatthew Rosato kvm_s390_vcpu_pci_setup(vcpu);
11223f4bbb43SMatthew Rosato kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
11233f4bbb43SMatthew Rosato }
11243f4bbb43SMatthew Rosato
11253f4bbb43SMatthew Rosato kvm_s390_vcpu_unblock_all(kvm);
11263f4bbb43SMatthew Rosato }
11273f4bbb43SMatthew Rosato
kvm_s390_sync_request_broadcast(struct kvm * kvm,int req)1128190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
1129190df4a2SClaudio Imbrenda {
113046808a4cSMarc Zyngier unsigned long cx;
1131190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu;
1132190df4a2SClaudio Imbrenda
1133190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm)
1134190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu);
1135190df4a2SClaudio Imbrenda }
1136190df4a2SClaudio Imbrenda
1137190df4a2SClaudio Imbrenda /*
1138190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with
11391de1ea7eSChristian Borntraeger * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
1140190df4a2SClaudio Imbrenda */
kvm_s390_vm_start_migration(struct kvm * kvm)1141190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm)
1142190df4a2SClaudio Imbrenda {
1143190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms;
1144190df4a2SClaudio Imbrenda struct kvm_memslots *slots;
1145afdad616SClaudio Imbrenda unsigned long ram_pages = 0;
1146a54d8066SMaciej S. Szmigiero int bkt;
1147190df4a2SClaudio Imbrenda
1148190df4a2SClaudio Imbrenda /* migration mode already enabled */
1149afdad616SClaudio Imbrenda if (kvm->arch.migration_mode)
1150190df4a2SClaudio Imbrenda return 0;
1151190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm);
1152a54d8066SMaciej S. Szmigiero if (!slots || kvm_memslots_empty(slots))
1153190df4a2SClaudio Imbrenda return -EINVAL;
1154190df4a2SClaudio Imbrenda
1155afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) {
1156afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1;
1157afdad616SClaudio Imbrenda return 0;
1158190df4a2SClaudio Imbrenda }
1159190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */
1160a54d8066SMaciej S. Szmigiero kvm_for_each_memslot(ms, bkt, slots) {
116113a17cc0SIgor Mammedov if (!ms->dirty_bitmap)
116213a17cc0SIgor Mammedov return -EINVAL;
1163afdad616SClaudio Imbrenda /*
1164afdad616SClaudio Imbrenda * The second half of the bitmap is only used on x86,
1165afdad616SClaudio Imbrenda * and would be wasted otherwise, so we put it to good
1166afdad616SClaudio Imbrenda * use here to keep track of the state of the storage
1167afdad616SClaudio Imbrenda * attributes.
1168afdad616SClaudio Imbrenda */
1169afdad616SClaudio Imbrenda memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
1170afdad616SClaudio Imbrenda ram_pages += ms->npages;
1171190df4a2SClaudio Imbrenda }
1172afdad616SClaudio Imbrenda atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
1173afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1;
1174190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
1175190df4a2SClaudio Imbrenda return 0;
1176190df4a2SClaudio Imbrenda }
1177190df4a2SClaudio Imbrenda
1178190df4a2SClaudio Imbrenda /*
11791de1ea7eSChristian Borntraeger * Must be called with kvm->slots_lock to avoid races with ourselves and
1180190df4a2SClaudio Imbrenda * kvm_s390_vm_start_migration.
1181190df4a2SClaudio Imbrenda */
kvm_s390_vm_stop_migration(struct kvm * kvm)1182190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm)
1183190df4a2SClaudio Imbrenda {
1184190df4a2SClaudio Imbrenda /* migration mode already disabled */
1185afdad616SClaudio Imbrenda if (!kvm->arch.migration_mode)
1186190df4a2SClaudio Imbrenda return 0;
1187afdad616SClaudio Imbrenda kvm->arch.migration_mode = 0;
1188afdad616SClaudio Imbrenda if (kvm->arch.use_cmma)
1189190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
1190190df4a2SClaudio Imbrenda return 0;
1191190df4a2SClaudio Imbrenda }
1192190df4a2SClaudio Imbrenda
kvm_s390_vm_set_migration(struct kvm * kvm,struct kvm_device_attr * attr)1193190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm,
1194190df4a2SClaudio Imbrenda struct kvm_device_attr *attr)
1195190df4a2SClaudio Imbrenda {
11961de1ea7eSChristian Borntraeger int res = -ENXIO;
1197190df4a2SClaudio Imbrenda
11981de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock);
1199190df4a2SClaudio Imbrenda switch (attr->attr) {
1200190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_START:
1201190df4a2SClaudio Imbrenda res = kvm_s390_vm_start_migration(kvm);
1202190df4a2SClaudio Imbrenda break;
1203190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_STOP:
1204190df4a2SClaudio Imbrenda res = kvm_s390_vm_stop_migration(kvm);
1205190df4a2SClaudio Imbrenda break;
1206190df4a2SClaudio Imbrenda default:
1207190df4a2SClaudio Imbrenda break;
1208190df4a2SClaudio Imbrenda }
12091de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock);
1210190df4a2SClaudio Imbrenda
1211190df4a2SClaudio Imbrenda return res;
1212190df4a2SClaudio Imbrenda }
1213190df4a2SClaudio Imbrenda
kvm_s390_vm_get_migration(struct kvm * kvm,struct kvm_device_attr * attr)1214190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm,
1215190df4a2SClaudio Imbrenda struct kvm_device_attr *attr)
1216190df4a2SClaudio Imbrenda {
1217afdad616SClaudio Imbrenda u64 mig = kvm->arch.migration_mode;
1218190df4a2SClaudio Imbrenda
1219190df4a2SClaudio Imbrenda if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1220190df4a2SClaudio Imbrenda return -ENXIO;
1221190df4a2SClaudio Imbrenda
1222190df4a2SClaudio Imbrenda if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1223190df4a2SClaudio Imbrenda return -EFAULT;
1224190df4a2SClaudio Imbrenda return 0;
1225190df4a2SClaudio Imbrenda }
1226190df4a2SClaudio Imbrenda
12276973091dSNico Boehr static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod);
12286973091dSNico Boehr
kvm_s390_set_tod_ext(struct kvm * kvm,struct kvm_device_attr * attr)12298fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
12308fa1696eSCollin L. Walling {
12318fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod;
12328fa1696eSCollin L. Walling
12338fa1696eSCollin L. Walling if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod)))
12348fa1696eSCollin L. Walling return -EFAULT;
12358fa1696eSCollin L. Walling
12360e7def5fSDavid Hildenbrand if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
12378fa1696eSCollin L. Walling return -EINVAL;
12386973091dSNico Boehr __kvm_s390_set_tod_clock(kvm, >od);
12398fa1696eSCollin L. Walling
12408fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
12418fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod);
12428fa1696eSCollin L. Walling
12438fa1696eSCollin L. Walling return 0;
12448fa1696eSCollin L. Walling }
12458fa1696eSCollin L. Walling
kvm_s390_set_tod_high(struct kvm * kvm,struct kvm_device_attr * attr)124672f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
124772f25020SJason J. Herne {
124872f25020SJason J. Herne u8 gtod_high;
124972f25020SJason J. Herne
125072f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr,
125172f25020SJason J. Herne sizeof(gtod_high)))
125272f25020SJason J. Herne return -EFAULT;
125372f25020SJason J. Herne
125472f25020SJason J. Herne if (gtod_high != 0)
125572f25020SJason J. Herne return -EINVAL;
125658c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
125772f25020SJason J. Herne
125872f25020SJason J. Herne return 0;
125972f25020SJason J. Herne }
126072f25020SJason J. Herne
kvm_s390_set_tod_low(struct kvm * kvm,struct kvm_device_attr * attr)126172f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
126272f25020SJason J. Herne {
12630e7def5fSDavid Hildenbrand struct kvm_s390_vm_tod_clock gtod = { 0 };
126472f25020SJason J. Herne
12650e7def5fSDavid Hildenbrand if (copy_from_user(>od.tod, (void __user *)attr->addr,
12660e7def5fSDavid Hildenbrand sizeof(gtod.tod)))
126772f25020SJason J. Herne return -EFAULT;
126872f25020SJason J. Herne
12696973091dSNico Boehr __kvm_s390_set_tod_clock(kvm, >od);
12700e7def5fSDavid Hildenbrand VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
127172f25020SJason J. Herne return 0;
127272f25020SJason J. Herne }
127372f25020SJason J. Herne
kvm_s390_set_tod(struct kvm * kvm,struct kvm_device_attr * attr)127472f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
127572f25020SJason J. Herne {
127672f25020SJason J. Herne int ret;
127772f25020SJason J. Herne
127872f25020SJason J. Herne if (attr->flags)
127972f25020SJason J. Herne return -EINVAL;
128072f25020SJason J. Herne
12816973091dSNico Boehr mutex_lock(&kvm->lock);
12826973091dSNico Boehr /*
12836973091dSNico Boehr * For protected guests, the TOD is managed by the ultravisor, so trying
12846973091dSNico Boehr * to change it will never bring the expected results.
12856973091dSNico Boehr */
12866973091dSNico Boehr if (kvm_s390_pv_is_protected(kvm)) {
12876973091dSNico Boehr ret = -EOPNOTSUPP;
12886973091dSNico Boehr goto out_unlock;
12896973091dSNico Boehr }
12906973091dSNico Boehr
129172f25020SJason J. Herne switch (attr->attr) {
12928fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT:
12938fa1696eSCollin L. Walling ret = kvm_s390_set_tod_ext(kvm, attr);
12948fa1696eSCollin L. Walling break;
129572f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH:
129672f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr);
129772f25020SJason J. Herne break;
129872f25020SJason J. Herne case KVM_S390_VM_TOD_LOW:
129972f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr);
130072f25020SJason J. Herne break;
130172f25020SJason J. Herne default:
130272f25020SJason J. Herne ret = -ENXIO;
130372f25020SJason J. Herne break;
130472f25020SJason J. Herne }
13056973091dSNico Boehr
13066973091dSNico Boehr out_unlock:
13076973091dSNico Boehr mutex_unlock(&kvm->lock);
130872f25020SJason J. Herne return ret;
130972f25020SJason J. Herne }
131072f25020SJason J. Herne
kvm_s390_get_tod_clock(struct kvm * kvm,struct kvm_s390_vm_tod_clock * gtod)131133d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm,
13128fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock *gtod)
13138fa1696eSCollin L. Walling {
13142cfd7b73SHeiko Carstens union tod_clock clk;
13158fa1696eSCollin L. Walling
13168fa1696eSCollin L. Walling preempt_disable();
13178fa1696eSCollin L. Walling
13182cfd7b73SHeiko Carstens store_tod_clock_ext(&clk);
13198fa1696eSCollin L. Walling
13202cfd7b73SHeiko Carstens gtod->tod = clk.tod + kvm->arch.epoch;
132133d1b272SDavid Hildenbrand gtod->epoch_idx = 0;
132233d1b272SDavid Hildenbrand if (test_kvm_facility(kvm, 139)) {
13232cfd7b73SHeiko Carstens gtod->epoch_idx = clk.ei + kvm->arch.epdx;
13242cfd7b73SHeiko Carstens if (gtod->tod < clk.tod)
13258fa1696eSCollin L. Walling gtod->epoch_idx += 1;
132633d1b272SDavid Hildenbrand }
13278fa1696eSCollin L. Walling
13288fa1696eSCollin L. Walling preempt_enable();
13298fa1696eSCollin L. Walling }
13308fa1696eSCollin L. Walling
kvm_s390_get_tod_ext(struct kvm * kvm,struct kvm_device_attr * attr)13318fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
13328fa1696eSCollin L. Walling {
13338fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod;
13348fa1696eSCollin L. Walling
13358fa1696eSCollin L. Walling memset(>od, 0, sizeof(gtod));
133633d1b272SDavid Hildenbrand kvm_s390_get_tod_clock(kvm, >od);
13378fa1696eSCollin L. Walling if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod)))
13388fa1696eSCollin L. Walling return -EFAULT;
13398fa1696eSCollin L. Walling
13408fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
13418fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod);
13428fa1696eSCollin L. Walling return 0;
13438fa1696eSCollin L. Walling }
13448fa1696eSCollin L. Walling
kvm_s390_get_tod_high(struct kvm * kvm,struct kvm_device_attr * attr)134572f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
134672f25020SJason J. Herne {
134772f25020SJason J. Herne u8 gtod_high = 0;
134872f25020SJason J. Herne
134972f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high,
135072f25020SJason J. Herne sizeof(gtod_high)))
135172f25020SJason J. Herne return -EFAULT;
135258c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
135372f25020SJason J. Herne
135472f25020SJason J. Herne return 0;
135572f25020SJason J. Herne }
135672f25020SJason J. Herne
kvm_s390_get_tod_low(struct kvm * kvm,struct kvm_device_attr * attr)135772f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
135872f25020SJason J. Herne {
13595a3d883aSDavid Hildenbrand u64 gtod;
136072f25020SJason J. Herne
136160417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm);
136272f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod)))
136372f25020SJason J. Herne return -EFAULT;
136458c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
136572f25020SJason J. Herne
136672f25020SJason J. Herne return 0;
136772f25020SJason J. Herne }
136872f25020SJason J. Herne
kvm_s390_get_tod(struct kvm * kvm,struct kvm_device_attr * attr)136972f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
137072f25020SJason J. Herne {
137172f25020SJason J. Herne int ret;
137272f25020SJason J. Herne
137372f25020SJason J. Herne if (attr->flags)
137472f25020SJason J. Herne return -EINVAL;
137572f25020SJason J. Herne
137672f25020SJason J. Herne switch (attr->attr) {
13778fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT:
13788fa1696eSCollin L. Walling ret = kvm_s390_get_tod_ext(kvm, attr);
13798fa1696eSCollin L. Walling break;
138072f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH:
138172f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr);
138272f25020SJason J. Herne break;
138372f25020SJason J. Herne case KVM_S390_VM_TOD_LOW:
138472f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr);
138572f25020SJason J. Herne break;
138672f25020SJason J. Herne default:
138772f25020SJason J. Herne ret = -ENXIO;
138872f25020SJason J. Herne break;
138972f25020SJason J. Herne }
139072f25020SJason J. Herne return ret;
139172f25020SJason J. Herne }
139272f25020SJason J. Herne
kvm_s390_set_processor(struct kvm * kvm,struct kvm_device_attr * attr)1393658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1394658b6edaSMichael Mueller {
1395658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc;
1396053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc;
1397658b6edaSMichael Mueller int ret = 0;
1398658b6edaSMichael Mueller
1399658b6edaSMichael Mueller mutex_lock(&kvm->lock);
1400a03825bbSPaolo Bonzini if (kvm->created_vcpus) {
1401658b6edaSMichael Mueller ret = -EBUSY;
1402658b6edaSMichael Mueller goto out;
1403658b6edaSMichael Mueller }
1404c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1405658b6edaSMichael Mueller if (!proc) {
1406658b6edaSMichael Mueller ret = -ENOMEM;
1407658b6edaSMichael Mueller goto out;
1408658b6edaSMichael Mueller }
1409658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr,
1410658b6edaSMichael Mueller sizeof(*proc))) {
14119bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid;
1412053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff;
1413053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff;
14140487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) {
1415053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc)
1416053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc;
1417053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc)
1418053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc;
1419053dd230SDavid Hildenbrand else
1420658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc;
1421053dd230SDavid Hildenbrand }
1422c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list,
1423658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE);
1424a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1425a8c39dd7SChristian Borntraeger kvm->arch.model.ibc,
1426a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid);
1427a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1428a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0],
1429a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1],
1430a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]);
1431658b6edaSMichael Mueller } else
1432658b6edaSMichael Mueller ret = -EFAULT;
1433658b6edaSMichael Mueller kfree(proc);
1434658b6edaSMichael Mueller out:
1435658b6edaSMichael Mueller mutex_unlock(&kvm->lock);
1436658b6edaSMichael Mueller return ret;
1437658b6edaSMichael Mueller }
1438658b6edaSMichael Mueller
kvm_s390_set_processor_feat(struct kvm * kvm,struct kvm_device_attr * attr)143915c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
144015c9705fSDavid Hildenbrand struct kvm_device_attr *attr)
144115c9705fSDavid Hildenbrand {
144215c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data;
144315c9705fSDavid Hildenbrand
144415c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
144515c9705fSDavid Hildenbrand return -EFAULT;
144615c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat,
144715c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat,
144815c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS))
144915c9705fSDavid Hildenbrand return -EINVAL;
145015c9705fSDavid Hildenbrand
145115c9705fSDavid Hildenbrand mutex_lock(&kvm->lock);
14522f8311c9SChristian Borntraeger if (kvm->created_vcpus) {
14532f8311c9SChristian Borntraeger mutex_unlock(&kvm->lock);
14542f8311c9SChristian Borntraeger return -EBUSY;
14552f8311c9SChristian Borntraeger }
1456da0f8e95SYury Norov bitmap_from_arr64(kvm->arch.cpu_feat, data.feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
145715c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock);
14582f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
14592f8311c9SChristian Borntraeger data.feat[0],
14602f8311c9SChristian Borntraeger data.feat[1],
14612f8311c9SChristian Borntraeger data.feat[2]);
14622f8311c9SChristian Borntraeger return 0;
146315c9705fSDavid Hildenbrand }
146415c9705fSDavid Hildenbrand
kvm_s390_set_processor_subfunc(struct kvm * kvm,struct kvm_device_attr * attr)14650a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
14660a763c78SDavid Hildenbrand struct kvm_device_attr *attr)
14670a763c78SDavid Hildenbrand {
1468346fa2f8SChristian Borntraeger mutex_lock(&kvm->lock);
1469346fa2f8SChristian Borntraeger if (kvm->created_vcpus) {
1470346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock);
1471346fa2f8SChristian Borntraeger return -EBUSY;
1472346fa2f8SChristian Borntraeger }
1473346fa2f8SChristian Borntraeger
1474346fa2f8SChristian Borntraeger if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1475346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1476346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock);
1477346fa2f8SChristian Borntraeger return -EFAULT;
1478346fa2f8SChristian Borntraeger }
1479346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock);
1480346fa2f8SChristian Borntraeger
148111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
148211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
148311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
148411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
148511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
148611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
148711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
148811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
148911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
149011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
149111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
149211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx",
149311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
149411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
149511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx",
149611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
149711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
149811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
149911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
150011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
150111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
150211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
150311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
150411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
150511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
150611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
150711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
150811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
150911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
151011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx",
151111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
151211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
151311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx",
151411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
151511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
151611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx",
151711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
151811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
151911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
152011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
152111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
152211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx",
152311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
152411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
152513209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
152613209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
152713209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1528173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1529173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1530173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1531173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1532173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
15334f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
15344f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
15354f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
15364f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
15374f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
153811ba5961SChristian Borntraeger
1539346fa2f8SChristian Borntraeger return 0;
15400a763c78SDavid Hildenbrand }
15410a763c78SDavid Hildenbrand
154219c654bfSSteffen Eiden #define KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK \
154319c654bfSSteffen Eiden ( \
154419c654bfSSteffen Eiden ((struct kvm_s390_vm_cpu_uv_feat){ \
154519c654bfSSteffen Eiden .ap = 1, \
154619c654bfSSteffen Eiden .ap_intr = 1, \
154719c654bfSSteffen Eiden }) \
154819c654bfSSteffen Eiden .feat \
154919c654bfSSteffen Eiden )
155019c654bfSSteffen Eiden
kvm_s390_set_uv_feat(struct kvm * kvm,struct kvm_device_attr * attr)155119c654bfSSteffen Eiden static int kvm_s390_set_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
155219c654bfSSteffen Eiden {
155319c654bfSSteffen Eiden struct kvm_s390_vm_cpu_uv_feat __user *ptr = (void __user *)attr->addr;
155419c654bfSSteffen Eiden unsigned long data, filter;
155519c654bfSSteffen Eiden
155619c654bfSSteffen Eiden filter = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
155719c654bfSSteffen Eiden if (get_user(data, &ptr->feat))
155819c654bfSSteffen Eiden return -EFAULT;
155919c654bfSSteffen Eiden if (!bitmap_subset(&data, &filter, KVM_S390_VM_CPU_UV_FEAT_NR_BITS))
156019c654bfSSteffen Eiden return -EINVAL;
156119c654bfSSteffen Eiden
156219c654bfSSteffen Eiden mutex_lock(&kvm->lock);
156319c654bfSSteffen Eiden if (kvm->created_vcpus) {
156419c654bfSSteffen Eiden mutex_unlock(&kvm->lock);
156519c654bfSSteffen Eiden return -EBUSY;
156619c654bfSSteffen Eiden }
156719c654bfSSteffen Eiden kvm->arch.model.uv_feat_guest.feat = data;
156819c654bfSSteffen Eiden mutex_unlock(&kvm->lock);
156919c654bfSSteffen Eiden
157019c654bfSSteffen Eiden VM_EVENT(kvm, 3, "SET: guest UV-feat: 0x%16.16lx", data);
157119c654bfSSteffen Eiden
157219c654bfSSteffen Eiden return 0;
157319c654bfSSteffen Eiden }
157419c654bfSSteffen Eiden
kvm_s390_set_cpu_model(struct kvm * kvm,struct kvm_device_attr * attr)1575658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1576658b6edaSMichael Mueller {
1577658b6edaSMichael Mueller int ret = -ENXIO;
1578658b6edaSMichael Mueller
1579658b6edaSMichael Mueller switch (attr->attr) {
1580658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR:
1581658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr);
1582658b6edaSMichael Mueller break;
158315c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT:
158415c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr);
158515c9705fSDavid Hildenbrand break;
15860a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
15870a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr);
15880a763c78SDavid Hildenbrand break;
158919c654bfSSteffen Eiden case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
159019c654bfSSteffen Eiden ret = kvm_s390_set_uv_feat(kvm, attr);
159119c654bfSSteffen Eiden break;
1592658b6edaSMichael Mueller }
1593658b6edaSMichael Mueller return ret;
1594658b6edaSMichael Mueller }
1595658b6edaSMichael Mueller
kvm_s390_get_processor(struct kvm * kvm,struct kvm_device_attr * attr)1596658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1597658b6edaSMichael Mueller {
1598658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc;
1599658b6edaSMichael Mueller int ret = 0;
1600658b6edaSMichael Mueller
1601c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1602658b6edaSMichael Mueller if (!proc) {
1603658b6edaSMichael Mueller ret = -ENOMEM;
1604658b6edaSMichael Mueller goto out;
1605658b6edaSMichael Mueller }
16069bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid;
1607658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc;
1608c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1609c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE);
1610a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1611a8c39dd7SChristian Borntraeger kvm->arch.model.ibc,
1612a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid);
1613a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1614a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0],
1615a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1],
1616a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]);
1617658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1618658b6edaSMichael Mueller ret = -EFAULT;
1619658b6edaSMichael Mueller kfree(proc);
1620658b6edaSMichael Mueller out:
1621658b6edaSMichael Mueller return ret;
1622658b6edaSMichael Mueller }
1623658b6edaSMichael Mueller
kvm_s390_get_machine(struct kvm * kvm,struct kvm_device_attr * attr)1624658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1625658b6edaSMichael Mueller {
1626658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach;
1627658b6edaSMichael Mueller int ret = 0;
1628658b6edaSMichael Mueller
1629c4196218SChristian Borntraeger mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT);
1630658b6edaSMichael Mueller if (!mach) {
1631658b6edaSMichael Mueller ret = -ENOMEM;
1632658b6edaSMichael Mueller goto out;
1633658b6edaSMichael Mueller }
1634658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid);
163537c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc;
1636c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1637981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE);
163817e89e13SSven Schnelle memcpy((unsigned long *)&mach->fac_list, stfle_fac_list,
163917e89e13SSven Schnelle sizeof(stfle_fac_list));
1640a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
1641a8c39dd7SChristian Borntraeger kvm->arch.model.ibc,
1642a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid);
1643a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
1644a8c39dd7SChristian Borntraeger mach->fac_mask[0],
1645a8c39dd7SChristian Borntraeger mach->fac_mask[1],
1646a8c39dd7SChristian Borntraeger mach->fac_mask[2]);
1647a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1648a8c39dd7SChristian Borntraeger mach->fac_list[0],
1649a8c39dd7SChristian Borntraeger mach->fac_list[1],
1650a8c39dd7SChristian Borntraeger mach->fac_list[2]);
1651658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1652658b6edaSMichael Mueller ret = -EFAULT;
1653658b6edaSMichael Mueller kfree(mach);
1654658b6edaSMichael Mueller out:
1655658b6edaSMichael Mueller return ret;
1656658b6edaSMichael Mueller }
1657658b6edaSMichael Mueller
kvm_s390_get_processor_feat(struct kvm * kvm,struct kvm_device_attr * attr)165815c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
165915c9705fSDavid Hildenbrand struct kvm_device_attr *attr)
166015c9705fSDavid Hildenbrand {
166115c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data;
166215c9705fSDavid Hildenbrand
1663da0f8e95SYury Norov bitmap_to_arr64(data.feat, kvm->arch.cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
166415c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
166515c9705fSDavid Hildenbrand return -EFAULT;
16662f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
16672f8311c9SChristian Borntraeger data.feat[0],
16682f8311c9SChristian Borntraeger data.feat[1],
16692f8311c9SChristian Borntraeger data.feat[2]);
167015c9705fSDavid Hildenbrand return 0;
167115c9705fSDavid Hildenbrand }
167215c9705fSDavid Hildenbrand
kvm_s390_get_machine_feat(struct kvm * kvm,struct kvm_device_attr * attr)167315c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
167415c9705fSDavid Hildenbrand struct kvm_device_attr *attr)
167515c9705fSDavid Hildenbrand {
167615c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data;
167715c9705fSDavid Hildenbrand
1678da0f8e95SYury Norov bitmap_to_arr64(data.feat, kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
167915c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
168015c9705fSDavid Hildenbrand return -EFAULT;
16812f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
16822f8311c9SChristian Borntraeger data.feat[0],
16832f8311c9SChristian Borntraeger data.feat[1],
16842f8311c9SChristian Borntraeger data.feat[2]);
168515c9705fSDavid Hildenbrand return 0;
168615c9705fSDavid Hildenbrand }
168715c9705fSDavid Hildenbrand
kvm_s390_get_processor_subfunc(struct kvm * kvm,struct kvm_device_attr * attr)16880a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
16890a763c78SDavid Hildenbrand struct kvm_device_attr *attr)
16900a763c78SDavid Hildenbrand {
1691346fa2f8SChristian Borntraeger if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1692346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc)))
1693346fa2f8SChristian Borntraeger return -EFAULT;
1694346fa2f8SChristian Borntraeger
169511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
169611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
169711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
169811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
169911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
170011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
170111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
170211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
170311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
170411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
170511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
170611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx",
170711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
170811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
170911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx",
171011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
171111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
171211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
171311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
171411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
171511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
171611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
171711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
171811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
171911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
172011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
172111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
172211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
172311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
172411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx",
172511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
172611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
172711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx",
172811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
172911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
173011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx",
173111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
173211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
173311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
173411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
173511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
173611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx",
173711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
173811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
173913209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
174013209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
174113209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1742173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1743173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1744173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1745173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1746173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
17474f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
17484f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
17494f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
17504f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
17514f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
175211ba5961SChristian Borntraeger
1753346fa2f8SChristian Borntraeger return 0;
17540a763c78SDavid Hildenbrand }
17550a763c78SDavid Hildenbrand
kvm_s390_get_machine_subfunc(struct kvm * kvm,struct kvm_device_attr * attr)17560a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
17570a763c78SDavid Hildenbrand struct kvm_device_attr *attr)
17580a763c78SDavid Hildenbrand {
17590a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
17600a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc)))
17610a763c78SDavid Hildenbrand return -EFAULT;
176211ba5961SChristian Borntraeger
176311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
176411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
176511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
176611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
176711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
176811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx",
176911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
177011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
177111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx",
177211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
177311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
177411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx",
177511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
177611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
177711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx",
177811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
177911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
178011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx",
178111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
178211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
178311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx",
178411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
178511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
178611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx",
178711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
178811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
178911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx",
179011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
179111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
179211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx",
179311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
179411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
179511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx",
179611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
179711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
179811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx",
179911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
180011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
180111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx",
180211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
180311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
180411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx",
180511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
180611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
180713209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx",
180813209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
180913209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
1810173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1811173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1812173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1813173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1814173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
18154f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
18164f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
18174f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
18184f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
18194f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
182011ba5961SChristian Borntraeger
18210a763c78SDavid Hildenbrand return 0;
18220a763c78SDavid Hildenbrand }
1823346fa2f8SChristian Borntraeger
kvm_s390_get_processor_uv_feat(struct kvm * kvm,struct kvm_device_attr * attr)182419c654bfSSteffen Eiden static int kvm_s390_get_processor_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
182519c654bfSSteffen Eiden {
182619c654bfSSteffen Eiden struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
182719c654bfSSteffen Eiden unsigned long feat = kvm->arch.model.uv_feat_guest.feat;
182819c654bfSSteffen Eiden
182919c654bfSSteffen Eiden if (put_user(feat, &dst->feat))
183019c654bfSSteffen Eiden return -EFAULT;
183119c654bfSSteffen Eiden VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
183219c654bfSSteffen Eiden
183319c654bfSSteffen Eiden return 0;
183419c654bfSSteffen Eiden }
183519c654bfSSteffen Eiden
kvm_s390_get_machine_uv_feat(struct kvm * kvm,struct kvm_device_attr * attr)183619c654bfSSteffen Eiden static int kvm_s390_get_machine_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
183719c654bfSSteffen Eiden {
183819c654bfSSteffen Eiden struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
183919c654bfSSteffen Eiden unsigned long feat;
184019c654bfSSteffen Eiden
184119c654bfSSteffen Eiden BUILD_BUG_ON(sizeof(*dst) != sizeof(uv_info.uv_feature_indications));
184219c654bfSSteffen Eiden
184319c654bfSSteffen Eiden feat = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
184419c654bfSSteffen Eiden if (put_user(feat, &dst->feat))
184519c654bfSSteffen Eiden return -EFAULT;
184619c654bfSSteffen Eiden VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
184719c654bfSSteffen Eiden
184819c654bfSSteffen Eiden return 0;
184919c654bfSSteffen Eiden }
185019c654bfSSteffen Eiden
kvm_s390_get_cpu_model(struct kvm * kvm,struct kvm_device_attr * attr)1851658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1852658b6edaSMichael Mueller {
1853658b6edaSMichael Mueller int ret = -ENXIO;
1854658b6edaSMichael Mueller
1855658b6edaSMichael Mueller switch (attr->attr) {
1856658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR:
1857658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr);
1858658b6edaSMichael Mueller break;
1859658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE:
1860658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr);
1861658b6edaSMichael Mueller break;
186215c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT:
186315c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr);
186415c9705fSDavid Hildenbrand break;
186515c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT:
186615c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr);
186715c9705fSDavid Hildenbrand break;
18680a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
18690a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr);
18700a763c78SDavid Hildenbrand break;
18710a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
18720a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr);
18730a763c78SDavid Hildenbrand break;
187419c654bfSSteffen Eiden case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
187519c654bfSSteffen Eiden ret = kvm_s390_get_processor_uv_feat(kvm, attr);
187619c654bfSSteffen Eiden break;
187719c654bfSSteffen Eiden case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
187819c654bfSSteffen Eiden ret = kvm_s390_get_machine_uv_feat(kvm, attr);
187919c654bfSSteffen Eiden break;
1880658b6edaSMichael Mueller }
1881658b6edaSMichael Mueller return ret;
1882658b6edaSMichael Mueller }
1883658b6edaSMichael Mueller
188424fe0195SPierre Morel /**
188524fe0195SPierre Morel * kvm_s390_update_topology_change_report - update CPU topology change report
188624fe0195SPierre Morel * @kvm: guest KVM description
188724fe0195SPierre Morel * @val: set or clear the MTCR bit
188824fe0195SPierre Morel *
188924fe0195SPierre Morel * Updates the Multiprocessor Topology-Change-Report bit to signal
189024fe0195SPierre Morel * the guest with a topology change.
189124fe0195SPierre Morel * This is only relevant if the topology facility is present.
189224fe0195SPierre Morel *
189324fe0195SPierre Morel * The SCA version, bsca or esca, doesn't matter as offset is the same.
189424fe0195SPierre Morel */
kvm_s390_update_topology_change_report(struct kvm * kvm,bool val)189524fe0195SPierre Morel static void kvm_s390_update_topology_change_report(struct kvm *kvm, bool val)
189624fe0195SPierre Morel {
189724fe0195SPierre Morel union sca_utility new, old;
189824fe0195SPierre Morel struct bsca_block *sca;
189924fe0195SPierre Morel
190024fe0195SPierre Morel read_lock(&kvm->arch.sca_lock);
190124fe0195SPierre Morel sca = kvm->arch.sca;
190224fe0195SPierre Morel do {
190324fe0195SPierre Morel old = READ_ONCE(sca->utility);
190424fe0195SPierre Morel new = old;
190524fe0195SPierre Morel new.mtcr = val;
190624fe0195SPierre Morel } while (cmpxchg(&sca->utility.val, old.val, new.val) != old.val);
190724fe0195SPierre Morel read_unlock(&kvm->arch.sca_lock);
190824fe0195SPierre Morel }
190924fe0195SPierre Morel
kvm_s390_set_topo_change_indication(struct kvm * kvm,struct kvm_device_attr * attr)1910f5ecfee9SPierre Morel static int kvm_s390_set_topo_change_indication(struct kvm *kvm,
1911f5ecfee9SPierre Morel struct kvm_device_attr *attr)
1912f5ecfee9SPierre Morel {
1913f5ecfee9SPierre Morel if (!test_kvm_facility(kvm, 11))
1914f5ecfee9SPierre Morel return -ENXIO;
1915f5ecfee9SPierre Morel
1916f5ecfee9SPierre Morel kvm_s390_update_topology_change_report(kvm, !!attr->attr);
1917f5ecfee9SPierre Morel return 0;
1918f5ecfee9SPierre Morel }
1919f5ecfee9SPierre Morel
kvm_s390_get_topo_change_indication(struct kvm * kvm,struct kvm_device_attr * attr)1920f5ecfee9SPierre Morel static int kvm_s390_get_topo_change_indication(struct kvm *kvm,
1921f5ecfee9SPierre Morel struct kvm_device_attr *attr)
1922f5ecfee9SPierre Morel {
1923f5ecfee9SPierre Morel u8 topo;
1924f5ecfee9SPierre Morel
1925f5ecfee9SPierre Morel if (!test_kvm_facility(kvm, 11))
1926f5ecfee9SPierre Morel return -ENXIO;
1927f5ecfee9SPierre Morel
1928f5ecfee9SPierre Morel read_lock(&kvm->arch.sca_lock);
1929f5ecfee9SPierre Morel topo = ((struct bsca_block *)kvm->arch.sca)->utility.mtcr;
1930f5ecfee9SPierre Morel read_unlock(&kvm->arch.sca_lock);
1931f5ecfee9SPierre Morel
1932f5ecfee9SPierre Morel return put_user(topo, (u8 __user *)attr->addr);
1933f5ecfee9SPierre Morel }
1934f5ecfee9SPierre Morel
kvm_s390_vm_set_attr(struct kvm * kvm,struct kvm_device_attr * attr)1935f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1936f2061656SDominik Dingel {
1937f2061656SDominik Dingel int ret;
1938f2061656SDominik Dingel
1939f2061656SDominik Dingel switch (attr->group) {
19404f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL:
19418c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr);
19424f718eabSDominik Dingel break;
194372f25020SJason J. Herne case KVM_S390_VM_TOD:
194472f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr);
194572f25020SJason J. Herne break;
1946658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL:
1947658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr);
1948658b6edaSMichael Mueller break;
1949a374e892STony Krowiak case KVM_S390_VM_CRYPTO:
1950a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr);
1951a374e892STony Krowiak break;
1952190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION:
1953190df4a2SClaudio Imbrenda ret = kvm_s390_vm_set_migration(kvm, attr);
1954190df4a2SClaudio Imbrenda break;
1955f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY:
1956f5ecfee9SPierre Morel ret = kvm_s390_set_topo_change_indication(kvm, attr);
1957f5ecfee9SPierre Morel break;
1958f2061656SDominik Dingel default:
1959f2061656SDominik Dingel ret = -ENXIO;
1960f2061656SDominik Dingel break;
1961f2061656SDominik Dingel }
1962f2061656SDominik Dingel
1963f2061656SDominik Dingel return ret;
1964f2061656SDominik Dingel }
1965f2061656SDominik Dingel
kvm_s390_vm_get_attr(struct kvm * kvm,struct kvm_device_attr * attr)1966f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1967f2061656SDominik Dingel {
19688c0a7ce6SDominik Dingel int ret;
19698c0a7ce6SDominik Dingel
19708c0a7ce6SDominik Dingel switch (attr->group) {
19718c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL:
19728c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr);
19738c0a7ce6SDominik Dingel break;
197472f25020SJason J. Herne case KVM_S390_VM_TOD:
197572f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr);
197672f25020SJason J. Herne break;
1977658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL:
1978658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr);
1979658b6edaSMichael Mueller break;
1980190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION:
1981190df4a2SClaudio Imbrenda ret = kvm_s390_vm_get_migration(kvm, attr);
1982190df4a2SClaudio Imbrenda break;
1983f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY:
1984f5ecfee9SPierre Morel ret = kvm_s390_get_topo_change_indication(kvm, attr);
1985f5ecfee9SPierre Morel break;
19868c0a7ce6SDominik Dingel default:
19878c0a7ce6SDominik Dingel ret = -ENXIO;
19888c0a7ce6SDominik Dingel break;
19898c0a7ce6SDominik Dingel }
19908c0a7ce6SDominik Dingel
19918c0a7ce6SDominik Dingel return ret;
1992f2061656SDominik Dingel }
1993f2061656SDominik Dingel
kvm_s390_vm_has_attr(struct kvm * kvm,struct kvm_device_attr * attr)1994f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1995f2061656SDominik Dingel {
1996f2061656SDominik Dingel int ret;
1997f2061656SDominik Dingel
1998f2061656SDominik Dingel switch (attr->group) {
19994f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL:
20004f718eabSDominik Dingel switch (attr->attr) {
20014f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA:
20024f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA:
2003f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO;
2004f9cbd9b0SDavid Hildenbrand break;
20058c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE:
20064f718eabSDominik Dingel ret = 0;
20074f718eabSDominik Dingel break;
20084f718eabSDominik Dingel default:
20094f718eabSDominik Dingel ret = -ENXIO;
20104f718eabSDominik Dingel break;
20114f718eabSDominik Dingel }
20124f718eabSDominik Dingel break;
201372f25020SJason J. Herne case KVM_S390_VM_TOD:
201472f25020SJason J. Herne switch (attr->attr) {
201572f25020SJason J. Herne case KVM_S390_VM_TOD_LOW:
201672f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH:
201772f25020SJason J. Herne ret = 0;
201872f25020SJason J. Herne break;
201972f25020SJason J. Herne default:
202072f25020SJason J. Herne ret = -ENXIO;
202172f25020SJason J. Herne break;
202272f25020SJason J. Herne }
202372f25020SJason J. Herne break;
2024658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL:
2025658b6edaSMichael Mueller switch (attr->attr) {
2026658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR:
2027658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE:
202815c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT:
202915c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT:
20300a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
2031346fa2f8SChristian Borntraeger case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
203219c654bfSSteffen Eiden case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
203319c654bfSSteffen Eiden case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
2034658b6edaSMichael Mueller ret = 0;
2035658b6edaSMichael Mueller break;
2036658b6edaSMichael Mueller default:
2037658b6edaSMichael Mueller ret = -ENXIO;
2038658b6edaSMichael Mueller break;
2039658b6edaSMichael Mueller }
2040658b6edaSMichael Mueller break;
2041a374e892STony Krowiak case KVM_S390_VM_CRYPTO:
2042a374e892STony Krowiak switch (attr->attr) {
2043a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
2044a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
2045a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
2046a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
2047a374e892STony Krowiak ret = 0;
2048a374e892STony Krowiak break;
204937940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE:
205037940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE:
205137940fb0STony Krowiak ret = ap_instructions_available() ? 0 : -ENXIO;
205237940fb0STony Krowiak break;
2053a374e892STony Krowiak default:
2054a374e892STony Krowiak ret = -ENXIO;
2055a374e892STony Krowiak break;
2056a374e892STony Krowiak }
2057a374e892STony Krowiak break;
2058190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION:
2059190df4a2SClaudio Imbrenda ret = 0;
2060190df4a2SClaudio Imbrenda break;
2061f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY:
2062f5ecfee9SPierre Morel ret = test_kvm_facility(kvm, 11) ? 0 : -ENXIO;
2063f5ecfee9SPierre Morel break;
2064f2061656SDominik Dingel default:
2065f2061656SDominik Dingel ret = -ENXIO;
2066f2061656SDominik Dingel break;
2067f2061656SDominik Dingel }
2068f2061656SDominik Dingel
2069f2061656SDominik Dingel return ret;
2070f2061656SDominik Dingel }
2071f2061656SDominik Dingel
kvm_s390_get_skeys(struct kvm * kvm,struct kvm_s390_skeys * args)207271fb165eSThomas Huth static int kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
207330ee2a98SJason J. Herne {
207430ee2a98SJason J. Herne uint8_t *keys;
207530ee2a98SJason J. Herne uint64_t hva;
20764f899147SChristian Borntraeger int srcu_idx, i, r = 0;
207730ee2a98SJason J. Herne
207830ee2a98SJason J. Herne if (args->flags != 0)
207930ee2a98SJason J. Herne return -EINVAL;
208030ee2a98SJason J. Herne
208130ee2a98SJason J. Herne /* Is this guest using storage keys? */
208255531b74SJanosch Frank if (!mm_uses_skeys(current->mm))
208330ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE;
208430ee2a98SJason J. Herne
208530ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */
208630ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
208730ee2a98SJason J. Herne return -EINVAL;
208830ee2a98SJason J. Herne
2089c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
209030ee2a98SJason J. Herne if (!keys)
209130ee2a98SJason J. Herne return -ENOMEM;
209230ee2a98SJason J. Herne
2093d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm);
20944f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu);
209530ee2a98SJason J. Herne for (i = 0; i < args->count; i++) {
209630ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i);
209730ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) {
209830ee2a98SJason J. Herne r = -EFAULT;
2099d3ed1ceeSMartin Schwidefsky break;
210030ee2a98SJason J. Herne }
210130ee2a98SJason J. Herne
2102154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]);
2103154c8c19SDavid Hildenbrand if (r)
2104d3ed1ceeSMartin Schwidefsky break;
210530ee2a98SJason J. Herne }
21064f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx);
2107d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm);
210830ee2a98SJason J. Herne
2109d3ed1ceeSMartin Schwidefsky if (!r) {
211030ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
211130ee2a98SJason J. Herne sizeof(uint8_t) * args->count);
211230ee2a98SJason J. Herne if (r)
211330ee2a98SJason J. Herne r = -EFAULT;
2114d3ed1ceeSMartin Schwidefsky }
2115d3ed1ceeSMartin Schwidefsky
211630ee2a98SJason J. Herne kvfree(keys);
211730ee2a98SJason J. Herne return r;
211830ee2a98SJason J. Herne }
211930ee2a98SJason J. Herne
kvm_s390_set_skeys(struct kvm * kvm,struct kvm_s390_skeys * args)212071fb165eSThomas Huth static int kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
212130ee2a98SJason J. Herne {
212230ee2a98SJason J. Herne uint8_t *keys;
212330ee2a98SJason J. Herne uint64_t hva;
21244f899147SChristian Borntraeger int srcu_idx, i, r = 0;
2125bd096f64SJanosch Frank bool unlocked;
212630ee2a98SJason J. Herne
212730ee2a98SJason J. Herne if (args->flags != 0)
212830ee2a98SJason J. Herne return -EINVAL;
212930ee2a98SJason J. Herne
213030ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */
213130ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
213230ee2a98SJason J. Herne return -EINVAL;
213330ee2a98SJason J. Herne
2134c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
213530ee2a98SJason J. Herne if (!keys)
213630ee2a98SJason J. Herne return -ENOMEM;
213730ee2a98SJason J. Herne
213830ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
213930ee2a98SJason J. Herne sizeof(uint8_t) * args->count);
214030ee2a98SJason J. Herne if (r) {
214130ee2a98SJason J. Herne r = -EFAULT;
214230ee2a98SJason J. Herne goto out;
214330ee2a98SJason J. Herne }
214430ee2a98SJason J. Herne
214530ee2a98SJason J. Herne /* Enable storage key handling for the guest */
214614d4a425SDominik Dingel r = s390_enable_skey();
214714d4a425SDominik Dingel if (r)
214814d4a425SDominik Dingel goto out;
214930ee2a98SJason J. Herne
2150bd096f64SJanosch Frank i = 0;
2151d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm);
21524f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu);
2153bd096f64SJanosch Frank while (i < args->count) {
2154bd096f64SJanosch Frank unlocked = false;
215530ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i);
215630ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) {
215730ee2a98SJason J. Herne r = -EFAULT;
2158d3ed1ceeSMartin Schwidefsky break;
215930ee2a98SJason J. Herne }
216030ee2a98SJason J. Herne
216130ee2a98SJason J. Herne /* Lowest order bit is reserved */
216230ee2a98SJason J. Herne if (keys[i] & 0x01) {
216330ee2a98SJason J. Herne r = -EINVAL;
2164d3ed1ceeSMartin Schwidefsky break;
216530ee2a98SJason J. Herne }
216630ee2a98SJason J. Herne
2167fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0);
2168bd096f64SJanosch Frank if (r) {
216964019a2eSPeter Xu r = fixup_user_fault(current->mm, hva,
2170bd096f64SJanosch Frank FAULT_FLAG_WRITE, &unlocked);
217130ee2a98SJason J. Herne if (r)
2172d3ed1ceeSMartin Schwidefsky break;
217330ee2a98SJason J. Herne }
2174bd096f64SJanosch Frank if (!r)
2175bd096f64SJanosch Frank i++;
2176bd096f64SJanosch Frank }
21774f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx);
2178d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm);
217930ee2a98SJason J. Herne out:
218030ee2a98SJason J. Herne kvfree(keys);
218130ee2a98SJason J. Herne return r;
218230ee2a98SJason J. Herne }
218330ee2a98SJason J. Herne
21844036e387SClaudio Imbrenda /*
21854036e387SClaudio Imbrenda * Base address and length must be sent at the start of each block, therefore
21864036e387SClaudio Imbrenda * it's cheaper to send some clean data, as long as it's less than the size of
21874036e387SClaudio Imbrenda * two longs.
21884036e387SClaudio Imbrenda */
21894036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
21904036e387SClaudio Imbrenda /* for consistency */
21914036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
21924036e387SClaudio Imbrenda
kvm_s390_peek_cmma(struct kvm * kvm,struct kvm_s390_cmma_log * args,u8 * res,unsigned long bufsize)2193afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2194afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize)
2195afdad616SClaudio Imbrenda {
2196afdad616SClaudio Imbrenda unsigned long pgstev, hva, cur_gfn = args->start_gfn;
2197afdad616SClaudio Imbrenda
2198afdad616SClaudio Imbrenda args->count = 0;
2199afdad616SClaudio Imbrenda while (args->count < bufsize) {
2200afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn);
2201afdad616SClaudio Imbrenda /*
2202afdad616SClaudio Imbrenda * We return an error if the first value was invalid, but we
2203afdad616SClaudio Imbrenda * return successfully if at least one value was copied.
2204afdad616SClaudio Imbrenda */
2205afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva))
2206afdad616SClaudio Imbrenda return args->count ? 0 : -EFAULT;
2207afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2208afdad616SClaudio Imbrenda pgstev = 0;
2209afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43;
2210afdad616SClaudio Imbrenda cur_gfn++;
2211afdad616SClaudio Imbrenda }
2212afdad616SClaudio Imbrenda
2213afdad616SClaudio Imbrenda return 0;
2214afdad616SClaudio Imbrenda }
2215afdad616SClaudio Imbrenda
gfn_to_memslot_approx(struct kvm_memslots * slots,gfn_t gfn)2216c928bfc2SMaciej S. Szmigiero static struct kvm_memory_slot *gfn_to_memslot_approx(struct kvm_memslots *slots,
2217c928bfc2SMaciej S. Szmigiero gfn_t gfn)
2218c928bfc2SMaciej S. Szmigiero {
2219c928bfc2SMaciej S. Szmigiero return ____gfn_to_memslot(slots, gfn, true);
2220c928bfc2SMaciej S. Szmigiero }
2221c928bfc2SMaciej S. Szmigiero
kvm_s390_next_dirty_cmma(struct kvm_memslots * slots,unsigned long cur_gfn)2222afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
2223afdad616SClaudio Imbrenda unsigned long cur_gfn)
2224afdad616SClaudio Imbrenda {
2225c928bfc2SMaciej S. Szmigiero struct kvm_memory_slot *ms = gfn_to_memslot_approx(slots, cur_gfn);
2226afdad616SClaudio Imbrenda unsigned long ofs = cur_gfn - ms->base_gfn;
2227a54d8066SMaciej S. Szmigiero struct rb_node *mnode = &ms->gfn_node[slots->node_idx];
2228afdad616SClaudio Imbrenda
2229afdad616SClaudio Imbrenda if (ms->base_gfn + ms->npages <= cur_gfn) {
2230a54d8066SMaciej S. Szmigiero mnode = rb_next(mnode);
2231afdad616SClaudio Imbrenda /* If we are above the highest slot, wrap around */
2232a54d8066SMaciej S. Szmigiero if (!mnode)
2233a54d8066SMaciej S. Szmigiero mnode = rb_first(&slots->gfn_tree);
2234afdad616SClaudio Imbrenda
2235a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]);
2236afdad616SClaudio Imbrenda ofs = 0;
2237afdad616SClaudio Imbrenda }
2238285cff4cSNico Boehr
2239285cff4cSNico Boehr if (cur_gfn < ms->base_gfn)
2240285cff4cSNico Boehr ofs = 0;
2241285cff4cSNico Boehr
2242afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
2243a54d8066SMaciej S. Szmigiero while (ofs >= ms->npages && (mnode = rb_next(mnode))) {
2244a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]);
2245b5c7e7ecSYury Norov ofs = find_first_bit(kvm_second_dirty_bitmap(ms), ms->npages);
2246afdad616SClaudio Imbrenda }
2247afdad616SClaudio Imbrenda return ms->base_gfn + ofs;
2248afdad616SClaudio Imbrenda }
2249afdad616SClaudio Imbrenda
kvm_s390_get_cmma(struct kvm * kvm,struct kvm_s390_cmma_log * args,u8 * res,unsigned long bufsize)2250afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2251afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize)
2252afdad616SClaudio Imbrenda {
2253afdad616SClaudio Imbrenda unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
2254afdad616SClaudio Imbrenda struct kvm_memslots *slots = kvm_memslots(kvm);
2255afdad616SClaudio Imbrenda struct kvm_memory_slot *ms;
2256afdad616SClaudio Imbrenda
2257a54d8066SMaciej S. Szmigiero if (unlikely(kvm_memslots_empty(slots)))
22580774a964SSean Christopherson return 0;
22590774a964SSean Christopherson
2260afdad616SClaudio Imbrenda cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
2261afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn);
2262afdad616SClaudio Imbrenda args->count = 0;
2263afdad616SClaudio Imbrenda args->start_gfn = cur_gfn;
2264afdad616SClaudio Imbrenda if (!ms)
2265afdad616SClaudio Imbrenda return 0;
2266afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
22676a656832SMaciej S. Szmigiero mem_end = kvm_s390_get_gfn_end(slots);
2268afdad616SClaudio Imbrenda
2269afdad616SClaudio Imbrenda while (args->count < bufsize) {
2270afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn);
2271afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva))
2272afdad616SClaudio Imbrenda return 0;
2273afdad616SClaudio Imbrenda /* Decrement only if we actually flipped the bit to 0 */
2274afdad616SClaudio Imbrenda if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
2275afdad616SClaudio Imbrenda atomic64_dec(&kvm->arch.cmma_dirty_pages);
2276afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2277afdad616SClaudio Imbrenda pgstev = 0;
2278afdad616SClaudio Imbrenda /* Save the value */
2279afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43;
2280afdad616SClaudio Imbrenda /* If the next bit is too far away, stop. */
2281afdad616SClaudio Imbrenda if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
2282afdad616SClaudio Imbrenda return 0;
2283afdad616SClaudio Imbrenda /* If we reached the previous "next", find the next one */
2284afdad616SClaudio Imbrenda if (cur_gfn == next_gfn)
2285afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2286afdad616SClaudio Imbrenda /* Reached the end of memory or of the buffer, stop */
2287afdad616SClaudio Imbrenda if ((next_gfn >= mem_end) ||
2288afdad616SClaudio Imbrenda (next_gfn - args->start_gfn >= bufsize))
2289afdad616SClaudio Imbrenda return 0;
2290afdad616SClaudio Imbrenda cur_gfn++;
2291afdad616SClaudio Imbrenda /* Reached the end of the current memslot, take the next one. */
2292afdad616SClaudio Imbrenda if (cur_gfn - ms->base_gfn >= ms->npages) {
2293afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn);
2294afdad616SClaudio Imbrenda if (!ms)
2295afdad616SClaudio Imbrenda return 0;
2296afdad616SClaudio Imbrenda }
2297afdad616SClaudio Imbrenda }
2298afdad616SClaudio Imbrenda return 0;
2299afdad616SClaudio Imbrenda }
2300afdad616SClaudio Imbrenda
2301afdad616SClaudio Imbrenda /*
23024036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and
23034036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or
23044036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
23054036e387SClaudio Imbrenda * no trailing clean bytes are saved.
23064036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the
23074036e387SClaudio Imbrenda * output buffer will indicate 0 as length.
23084036e387SClaudio Imbrenda */
kvm_s390_get_cmma_bits(struct kvm * kvm,struct kvm_s390_cmma_log * args)23094036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm,
23104036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args)
23114036e387SClaudio Imbrenda {
2312afdad616SClaudio Imbrenda unsigned long bufsize;
2313afdad616SClaudio Imbrenda int srcu_idx, peek, ret;
2314afdad616SClaudio Imbrenda u8 *values;
23154036e387SClaudio Imbrenda
2316afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma)
23174036e387SClaudio Imbrenda return -ENXIO;
23184036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */
23194036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK)
23204036e387SClaudio Imbrenda return -EINVAL;
23214036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */
23224036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK);
2323afdad616SClaudio Imbrenda if (!peek && !kvm->arch.migration_mode)
23244036e387SClaudio Imbrenda return -EINVAL;
23254036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */
23264036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
2327c9f0a2b8SJanosch Frank if (!bufsize || !kvm->mm->context.uses_cmm) {
23284036e387SClaudio Imbrenda memset(args, 0, sizeof(*args));
23294036e387SClaudio Imbrenda return 0;
23304036e387SClaudio Imbrenda }
23314036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */
2332afdad616SClaudio Imbrenda if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
23334036e387SClaudio Imbrenda memset(args, 0, sizeof(*args));
23344036e387SClaudio Imbrenda return 0;
23354036e387SClaudio Imbrenda }
23364036e387SClaudio Imbrenda
2337afdad616SClaudio Imbrenda values = vmalloc(bufsize);
2338afdad616SClaudio Imbrenda if (!values)
23394036e387SClaudio Imbrenda return -ENOMEM;
23404036e387SClaudio Imbrenda
2341d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm);
23424036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu);
2343afdad616SClaudio Imbrenda if (peek)
2344afdad616SClaudio Imbrenda ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
2345afdad616SClaudio Imbrenda else
2346afdad616SClaudio Imbrenda ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
23474036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx);
2348d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm);
23494036e387SClaudio Imbrenda
2350afdad616SClaudio Imbrenda if (kvm->arch.migration_mode)
2351afdad616SClaudio Imbrenda args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2352afdad616SClaudio Imbrenda else
2353afdad616SClaudio Imbrenda args->remaining = 0;
23544036e387SClaudio Imbrenda
2355afdad616SClaudio Imbrenda if (copy_to_user((void __user *)args->values, values, args->count))
2356afdad616SClaudio Imbrenda ret = -EFAULT;
2357afdad616SClaudio Imbrenda
2358afdad616SClaudio Imbrenda vfree(values);
2359afdad616SClaudio Imbrenda return ret;
23604036e387SClaudio Imbrenda }
23614036e387SClaudio Imbrenda
23624036e387SClaudio Imbrenda /*
23634036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input
23644036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are
2365c9f0a2b8SJanosch Frank * set and the mm->context.uses_cmm flag is set.
23664036e387SClaudio Imbrenda */
kvm_s390_set_cmma_bits(struct kvm * kvm,const struct kvm_s390_cmma_log * args)23674036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm,
23684036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args)
23694036e387SClaudio Imbrenda {
23704036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i;
23714036e387SClaudio Imbrenda uint8_t *bits;
23724036e387SClaudio Imbrenda int srcu_idx, r = 0;
23734036e387SClaudio Imbrenda
23744036e387SClaudio Imbrenda mask = args->mask;
23754036e387SClaudio Imbrenda
23764036e387SClaudio Imbrenda if (!kvm->arch.use_cmma)
23774036e387SClaudio Imbrenda return -ENXIO;
23784036e387SClaudio Imbrenda /* invalid/unsupported flags */
23794036e387SClaudio Imbrenda if (args->flags != 0)
23804036e387SClaudio Imbrenda return -EINVAL;
23814036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */
23824036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX)
23834036e387SClaudio Imbrenda return -EINVAL;
23844036e387SClaudio Imbrenda /* Nothing to do */
23854036e387SClaudio Imbrenda if (args->count == 0)
23864036e387SClaudio Imbrenda return 0;
23874036e387SClaudio Imbrenda
238842bc47b3SKees Cook bits = vmalloc(array_size(sizeof(*bits), args->count));
23894036e387SClaudio Imbrenda if (!bits)
23904036e387SClaudio Imbrenda return -ENOMEM;
23914036e387SClaudio Imbrenda
23924036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count);
23934036e387SClaudio Imbrenda if (r) {
23944036e387SClaudio Imbrenda r = -EFAULT;
23954036e387SClaudio Imbrenda goto out;
23964036e387SClaudio Imbrenda }
23974036e387SClaudio Imbrenda
2398d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm);
23994036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu);
24004036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) {
24014036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i);
24024036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) {
24034036e387SClaudio Imbrenda r = -EFAULT;
24044036e387SClaudio Imbrenda break;
24054036e387SClaudio Imbrenda }
24064036e387SClaudio Imbrenda
24074036e387SClaudio Imbrenda pgstev = bits[i];
24084036e387SClaudio Imbrenda pgstev = pgstev << 24;
24091bab1c02SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
24104036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev);
24114036e387SClaudio Imbrenda }
24124036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx);
2413d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm);
24144036e387SClaudio Imbrenda
2415c9f0a2b8SJanosch Frank if (!kvm->mm->context.uses_cmm) {
2416d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm);
2417c9f0a2b8SJanosch Frank kvm->mm->context.uses_cmm = 1;
2418d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm);
24194036e387SClaudio Imbrenda }
24204036e387SClaudio Imbrenda out:
24214036e387SClaudio Imbrenda vfree(bits);
24224036e387SClaudio Imbrenda return r;
24234036e387SClaudio Imbrenda }
24244036e387SClaudio Imbrenda
2425be48d86fSClaudio Imbrenda /**
2426be48d86fSClaudio Imbrenda * kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to
2427be48d86fSClaudio Imbrenda * non protected.
2428be48d86fSClaudio Imbrenda * @kvm: the VM whose protected vCPUs are to be converted
2429be48d86fSClaudio Imbrenda * @rc: return value for the RC field of the UVC (in case of error)
2430be48d86fSClaudio Imbrenda * @rrc: return value for the RRC field of the UVC (in case of error)
2431be48d86fSClaudio Imbrenda *
2432be48d86fSClaudio Imbrenda * Does not stop in case of error, tries to convert as many
2433be48d86fSClaudio Imbrenda * CPUs as possible. In case of error, the RC and RRC of the last error are
2434be48d86fSClaudio Imbrenda * returned.
2435be48d86fSClaudio Imbrenda *
2436be48d86fSClaudio Imbrenda * Return: 0 in case of success, otherwise -EIO
2437be48d86fSClaudio Imbrenda */
kvm_s390_cpus_from_pv(struct kvm * kvm,u16 * rc,u16 * rrc)2438be48d86fSClaudio Imbrenda int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
243929b40f10SJanosch Frank {
244029b40f10SJanosch Frank struct kvm_vcpu *vcpu;
244146808a4cSMarc Zyngier unsigned long i;
2442be48d86fSClaudio Imbrenda u16 _rc, _rrc;
2443be48d86fSClaudio Imbrenda int ret = 0;
244429b40f10SJanosch Frank
244529b40f10SJanosch Frank /*
244629b40f10SJanosch Frank * We ignore failures and try to destroy as many CPUs as possible.
244729b40f10SJanosch Frank * At the same time we must not free the assigned resources when
244829b40f10SJanosch Frank * this fails, as the ultravisor has still access to that memory.
244929b40f10SJanosch Frank * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
245029b40f10SJanosch Frank * behind.
245129b40f10SJanosch Frank * We want to return the first failure rc and rrc, though.
245229b40f10SJanosch Frank */
245329b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) {
245429b40f10SJanosch Frank mutex_lock(&vcpu->mutex);
2455be48d86fSClaudio Imbrenda if (kvm_s390_pv_destroy_cpu(vcpu, &_rc, &_rrc) && !ret) {
2456be48d86fSClaudio Imbrenda *rc = _rc;
2457be48d86fSClaudio Imbrenda *rrc = _rrc;
245829b40f10SJanosch Frank ret = -EIO;
245929b40f10SJanosch Frank }
246029b40f10SJanosch Frank mutex_unlock(&vcpu->mutex);
246129b40f10SJanosch Frank }
2462ee6a569dSMichael Mueller /* Ensure that we re-enable gisa if the non-PV guest used it but the PV guest did not. */
2463ee6a569dSMichael Mueller if (use_gisa)
2464ee6a569dSMichael Mueller kvm_s390_gisa_enable(kvm);
246529b40f10SJanosch Frank return ret;
246629b40f10SJanosch Frank }
246729b40f10SJanosch Frank
2468be48d86fSClaudio Imbrenda /**
2469be48d86fSClaudio Imbrenda * kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM
2470be48d86fSClaudio Imbrenda * to protected.
2471be48d86fSClaudio Imbrenda * @kvm: the VM whose protected vCPUs are to be converted
2472be48d86fSClaudio Imbrenda * @rc: return value for the RC field of the UVC (in case of error)
2473be48d86fSClaudio Imbrenda * @rrc: return value for the RRC field of the UVC (in case of error)
2474be48d86fSClaudio Imbrenda *
2475be48d86fSClaudio Imbrenda * Tries to undo the conversion in case of error.
2476be48d86fSClaudio Imbrenda *
2477be48d86fSClaudio Imbrenda * Return: 0 in case of success, otherwise -EIO
2478be48d86fSClaudio Imbrenda */
kvm_s390_cpus_to_pv(struct kvm * kvm,u16 * rc,u16 * rrc)247929b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
248029b40f10SJanosch Frank {
248146808a4cSMarc Zyngier unsigned long i;
248246808a4cSMarc Zyngier int r = 0;
248329b40f10SJanosch Frank u16 dummy;
248429b40f10SJanosch Frank
248529b40f10SJanosch Frank struct kvm_vcpu *vcpu;
248629b40f10SJanosch Frank
2487ee6a569dSMichael Mueller /* Disable the GISA if the ultravisor does not support AIV. */
248859a88140SSteffen Eiden if (!uv_has_feature(BIT_UV_FEAT_AIV))
2489ee6a569dSMichael Mueller kvm_s390_gisa_disable(kvm);
2490ee6a569dSMichael Mueller
249129b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) {
249229b40f10SJanosch Frank mutex_lock(&vcpu->mutex);
249329b40f10SJanosch Frank r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
249429b40f10SJanosch Frank mutex_unlock(&vcpu->mutex);
249529b40f10SJanosch Frank if (r)
249629b40f10SJanosch Frank break;
249729b40f10SJanosch Frank }
249829b40f10SJanosch Frank if (r)
249929b40f10SJanosch Frank kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
250029b40f10SJanosch Frank return r;
250129b40f10SJanosch Frank }
250229b40f10SJanosch Frank
250335d02493SJanosch Frank /*
250435d02493SJanosch Frank * Here we provide user space with a direct interface to query UV
250535d02493SJanosch Frank * related data like UV maxima and available features as well as
250635d02493SJanosch Frank * feature specific data.
250735d02493SJanosch Frank *
250835d02493SJanosch Frank * To facilitate future extension of the data structures we'll try to
250935d02493SJanosch Frank * write data up to the maximum requested length.
251035d02493SJanosch Frank */
kvm_s390_handle_pv_info(struct kvm_s390_pv_info * info)251135d02493SJanosch Frank static ssize_t kvm_s390_handle_pv_info(struct kvm_s390_pv_info *info)
251235d02493SJanosch Frank {
251335d02493SJanosch Frank ssize_t len_min;
251435d02493SJanosch Frank
251535d02493SJanosch Frank switch (info->header.id) {
251635d02493SJanosch Frank case KVM_PV_INFO_VM: {
251735d02493SJanosch Frank len_min = sizeof(info->header) + sizeof(info->vm);
251835d02493SJanosch Frank
251935d02493SJanosch Frank if (info->header.len_max < len_min)
252035d02493SJanosch Frank return -EINVAL;
252135d02493SJanosch Frank
252235d02493SJanosch Frank memcpy(info->vm.inst_calls_list,
252335d02493SJanosch Frank uv_info.inst_calls_list,
252435d02493SJanosch Frank sizeof(uv_info.inst_calls_list));
252535d02493SJanosch Frank
252635d02493SJanosch Frank /* It's max cpuid not max cpus, so it's off by one */
252735d02493SJanosch Frank info->vm.max_cpus = uv_info.max_guest_cpu_id + 1;
252835d02493SJanosch Frank info->vm.max_guests = uv_info.max_num_sec_conf;
252935d02493SJanosch Frank info->vm.max_guest_addr = uv_info.max_sec_stor_addr;
253035d02493SJanosch Frank info->vm.feature_indication = uv_info.uv_feature_indications;
253135d02493SJanosch Frank
253235d02493SJanosch Frank return len_min;
253335d02493SJanosch Frank }
2534fe9a93e0SJanosch Frank case KVM_PV_INFO_DUMP: {
2535fe9a93e0SJanosch Frank len_min = sizeof(info->header) + sizeof(info->dump);
2536fe9a93e0SJanosch Frank
2537fe9a93e0SJanosch Frank if (info->header.len_max < len_min)
2538fe9a93e0SJanosch Frank return -EINVAL;
2539fe9a93e0SJanosch Frank
2540fe9a93e0SJanosch Frank info->dump.dump_cpu_buffer_len = uv_info.guest_cpu_stor_len;
2541fe9a93e0SJanosch Frank info->dump.dump_config_mem_buffer_per_1m = uv_info.conf_dump_storage_state_len;
2542fe9a93e0SJanosch Frank info->dump.dump_config_finalize_len = uv_info.conf_dump_finalize_len;
2543fe9a93e0SJanosch Frank return len_min;
2544fe9a93e0SJanosch Frank }
254535d02493SJanosch Frank default:
254635d02493SJanosch Frank return -EINVAL;
254735d02493SJanosch Frank }
254835d02493SJanosch Frank }
254935d02493SJanosch Frank
kvm_s390_pv_dmp(struct kvm * kvm,struct kvm_pv_cmd * cmd,struct kvm_s390_pv_dmp dmp)25500460eb35SJanosch Frank static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd,
25510460eb35SJanosch Frank struct kvm_s390_pv_dmp dmp)
25520460eb35SJanosch Frank {
25530460eb35SJanosch Frank int r = -EINVAL;
25540460eb35SJanosch Frank void __user *result_buff = (void __user *)dmp.buff_addr;
25550460eb35SJanosch Frank
25560460eb35SJanosch Frank switch (dmp.subcmd) {
25570460eb35SJanosch Frank case KVM_PV_DUMP_INIT: {
25580460eb35SJanosch Frank if (kvm->arch.pv.dumping)
25590460eb35SJanosch Frank break;
25600460eb35SJanosch Frank
25610460eb35SJanosch Frank /*
25620460eb35SJanosch Frank * Block SIE entry as concurrent dump UVCs could lead
25630460eb35SJanosch Frank * to validities.
25640460eb35SJanosch Frank */
25650460eb35SJanosch Frank kvm_s390_vcpu_block_all(kvm);
25660460eb35SJanosch Frank
25670460eb35SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
25680460eb35SJanosch Frank UVC_CMD_DUMP_INIT, &cmd->rc, &cmd->rrc);
25690460eb35SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP INIT: rc %x rrc %x",
25700460eb35SJanosch Frank cmd->rc, cmd->rrc);
25710460eb35SJanosch Frank if (!r) {
25720460eb35SJanosch Frank kvm->arch.pv.dumping = true;
25730460eb35SJanosch Frank } else {
25740460eb35SJanosch Frank kvm_s390_vcpu_unblock_all(kvm);
25750460eb35SJanosch Frank r = -EINVAL;
25760460eb35SJanosch Frank }
25770460eb35SJanosch Frank break;
25780460eb35SJanosch Frank }
25790460eb35SJanosch Frank case KVM_PV_DUMP_CONFIG_STOR_STATE: {
25800460eb35SJanosch Frank if (!kvm->arch.pv.dumping)
25810460eb35SJanosch Frank break;
25820460eb35SJanosch Frank
25830460eb35SJanosch Frank /*
25840460eb35SJanosch Frank * gaddr is an output parameter since we might stop
25850460eb35SJanosch Frank * early. As dmp will be copied back in our caller, we
25860460eb35SJanosch Frank * don't need to do it ourselves.
25870460eb35SJanosch Frank */
25880460eb35SJanosch Frank r = kvm_s390_pv_dump_stor_state(kvm, result_buff, &dmp.gaddr, dmp.buff_len,
25890460eb35SJanosch Frank &cmd->rc, &cmd->rrc);
25900460eb35SJanosch Frank break;
25910460eb35SJanosch Frank }
25920460eb35SJanosch Frank case KVM_PV_DUMP_COMPLETE: {
25930460eb35SJanosch Frank if (!kvm->arch.pv.dumping)
25940460eb35SJanosch Frank break;
25950460eb35SJanosch Frank
25960460eb35SJanosch Frank r = -EINVAL;
25970460eb35SJanosch Frank if (dmp.buff_len < uv_info.conf_dump_finalize_len)
25980460eb35SJanosch Frank break;
25990460eb35SJanosch Frank
26000460eb35SJanosch Frank r = kvm_s390_pv_dump_complete(kvm, result_buff,
26010460eb35SJanosch Frank &cmd->rc, &cmd->rrc);
26020460eb35SJanosch Frank break;
26030460eb35SJanosch Frank }
26040460eb35SJanosch Frank default:
26050460eb35SJanosch Frank r = -ENOTTY;
26060460eb35SJanosch Frank break;
26070460eb35SJanosch Frank }
26080460eb35SJanosch Frank
26090460eb35SJanosch Frank return r;
26100460eb35SJanosch Frank }
26110460eb35SJanosch Frank
kvm_s390_handle_pv(struct kvm * kvm,struct kvm_pv_cmd * cmd)261229b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
261329b40f10SJanosch Frank {
2614fb491d55SClaudio Imbrenda const bool need_lock = (cmd->cmd != KVM_PV_ASYNC_CLEANUP_PERFORM);
2615fb491d55SClaudio Imbrenda void __user *argp = (void __user *)cmd->data;
261629b40f10SJanosch Frank int r = 0;
261729b40f10SJanosch Frank u16 dummy;
2618fb491d55SClaudio Imbrenda
2619fb491d55SClaudio Imbrenda if (need_lock)
2620fb491d55SClaudio Imbrenda mutex_lock(&kvm->lock);
262129b40f10SJanosch Frank
262229b40f10SJanosch Frank switch (cmd->cmd) {
262329b40f10SJanosch Frank case KVM_PV_ENABLE: {
262429b40f10SJanosch Frank r = -EINVAL;
262529b40f10SJanosch Frank if (kvm_s390_pv_is_protected(kvm))
262629b40f10SJanosch Frank break;
262729b40f10SJanosch Frank
262829b40f10SJanosch Frank /*
262929b40f10SJanosch Frank * FMT 4 SIE needs esca. As we never switch back to bsca from
263029b40f10SJanosch Frank * esca, we need no cleanup in the error cases below
263129b40f10SJanosch Frank */
263229b40f10SJanosch Frank r = sca_switch_to_extended(kvm);
263329b40f10SJanosch Frank if (r)
263429b40f10SJanosch Frank break;
263529b40f10SJanosch Frank
2636840565b1SDavid Hildenbrand r = s390_disable_cow_sharing();
2637fa0c5eabSJanosch Frank if (r)
2638fa0c5eabSJanosch Frank break;
2639fa0c5eabSJanosch Frank
264029b40f10SJanosch Frank r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
264129b40f10SJanosch Frank if (r)
264229b40f10SJanosch Frank break;
264329b40f10SJanosch Frank
264429b40f10SJanosch Frank r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
264529b40f10SJanosch Frank if (r)
264629b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
26470890ddeaSChristian Borntraeger
26480890ddeaSChristian Borntraeger /* we need to block service interrupts from now on */
26490890ddeaSChristian Borntraeger set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
265029b40f10SJanosch Frank break;
265129b40f10SJanosch Frank }
2652fb491d55SClaudio Imbrenda case KVM_PV_ASYNC_CLEANUP_PREPARE:
2653fb491d55SClaudio Imbrenda r = -EINVAL;
2654fb491d55SClaudio Imbrenda if (!kvm_s390_pv_is_protected(kvm) || !async_destroy)
2655fb491d55SClaudio Imbrenda break;
2656fb491d55SClaudio Imbrenda
2657fb491d55SClaudio Imbrenda r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
2658fb491d55SClaudio Imbrenda /*
2659fb491d55SClaudio Imbrenda * If a CPU could not be destroyed, destroy VM will also fail.
2660fb491d55SClaudio Imbrenda * There is no point in trying to destroy it. Instead return
2661fb491d55SClaudio Imbrenda * the rc and rrc from the first CPU that failed destroying.
2662fb491d55SClaudio Imbrenda */
2663fb491d55SClaudio Imbrenda if (r)
2664fb491d55SClaudio Imbrenda break;
2665fb491d55SClaudio Imbrenda r = kvm_s390_pv_set_aside(kvm, &cmd->rc, &cmd->rrc);
2666fb491d55SClaudio Imbrenda
2667fb491d55SClaudio Imbrenda /* no need to block service interrupts any more */
2668fb491d55SClaudio Imbrenda clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
2669fb491d55SClaudio Imbrenda break;
2670fb491d55SClaudio Imbrenda case KVM_PV_ASYNC_CLEANUP_PERFORM:
2671fb491d55SClaudio Imbrenda r = -EINVAL;
2672fb491d55SClaudio Imbrenda if (!async_destroy)
2673fb491d55SClaudio Imbrenda break;
2674fb491d55SClaudio Imbrenda /* kvm->lock must not be held; this is asserted inside the function. */
2675fb491d55SClaudio Imbrenda r = kvm_s390_pv_deinit_aside_vm(kvm, &cmd->rc, &cmd->rrc);
2676fb491d55SClaudio Imbrenda break;
267729b40f10SJanosch Frank case KVM_PV_DISABLE: {
267829b40f10SJanosch Frank r = -EINVAL;
267929b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm))
268029b40f10SJanosch Frank break;
268129b40f10SJanosch Frank
268229b40f10SJanosch Frank r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
268329b40f10SJanosch Frank /*
268429b40f10SJanosch Frank * If a CPU could not be destroyed, destroy VM will also fail.
268529b40f10SJanosch Frank * There is no point in trying to destroy it. Instead return
268629b40f10SJanosch Frank * the rc and rrc from the first CPU that failed destroying.
268729b40f10SJanosch Frank */
268829b40f10SJanosch Frank if (r)
268929b40f10SJanosch Frank break;
2690fb491d55SClaudio Imbrenda r = kvm_s390_pv_deinit_cleanup_all(kvm, &cmd->rc, &cmd->rrc);
26910890ddeaSChristian Borntraeger
26920890ddeaSChristian Borntraeger /* no need to block service interrupts any more */
26930890ddeaSChristian Borntraeger clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
269429b40f10SJanosch Frank break;
269529b40f10SJanosch Frank }
269629b40f10SJanosch Frank case KVM_PV_SET_SEC_PARMS: {
269729b40f10SJanosch Frank struct kvm_s390_pv_sec_parm parms = {};
269829b40f10SJanosch Frank void *hdr;
269929b40f10SJanosch Frank
270029b40f10SJanosch Frank r = -EINVAL;
270129b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm))
270229b40f10SJanosch Frank break;
270329b40f10SJanosch Frank
270429b40f10SJanosch Frank r = -EFAULT;
270529b40f10SJanosch Frank if (copy_from_user(&parms, argp, sizeof(parms)))
270629b40f10SJanosch Frank break;
270729b40f10SJanosch Frank
270829b40f10SJanosch Frank /* Currently restricted to 8KB */
270929b40f10SJanosch Frank r = -EINVAL;
271029b40f10SJanosch Frank if (parms.length > PAGE_SIZE * 2)
271129b40f10SJanosch Frank break;
271229b40f10SJanosch Frank
271329b40f10SJanosch Frank r = -ENOMEM;
271429b40f10SJanosch Frank hdr = vmalloc(parms.length);
271529b40f10SJanosch Frank if (!hdr)
271629b40f10SJanosch Frank break;
271729b40f10SJanosch Frank
271829b40f10SJanosch Frank r = -EFAULT;
271929b40f10SJanosch Frank if (!copy_from_user(hdr, (void __user *)parms.origin,
272029b40f10SJanosch Frank parms.length))
272129b40f10SJanosch Frank r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
272229b40f10SJanosch Frank &cmd->rc, &cmd->rrc);
272329b40f10SJanosch Frank
272429b40f10SJanosch Frank vfree(hdr);
272529b40f10SJanosch Frank break;
272629b40f10SJanosch Frank }
272729b40f10SJanosch Frank case KVM_PV_UNPACK: {
272829b40f10SJanosch Frank struct kvm_s390_pv_unp unp = {};
272929b40f10SJanosch Frank
273029b40f10SJanosch Frank r = -EINVAL;
27311ed576a2SJanosch Frank if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm))
273229b40f10SJanosch Frank break;
273329b40f10SJanosch Frank
273429b40f10SJanosch Frank r = -EFAULT;
273529b40f10SJanosch Frank if (copy_from_user(&unp, argp, sizeof(unp)))
273629b40f10SJanosch Frank break;
273729b40f10SJanosch Frank
273829b40f10SJanosch Frank r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
273929b40f10SJanosch Frank &cmd->rc, &cmd->rrc);
274029b40f10SJanosch Frank break;
274129b40f10SJanosch Frank }
274229b40f10SJanosch Frank case KVM_PV_VERIFY: {
274329b40f10SJanosch Frank r = -EINVAL;
274429b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm))
274529b40f10SJanosch Frank break;
274629b40f10SJanosch Frank
274729b40f10SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
274829b40f10SJanosch Frank UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
274929b40f10SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
275029b40f10SJanosch Frank cmd->rrc);
275129b40f10SJanosch Frank break;
275229b40f10SJanosch Frank }
2753e0d2773dSJanosch Frank case KVM_PV_PREP_RESET: {
2754e0d2773dSJanosch Frank r = -EINVAL;
2755e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm))
2756e0d2773dSJanosch Frank break;
2757e0d2773dSJanosch Frank
2758e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2759e0d2773dSJanosch Frank UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc);
2760e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x",
2761e0d2773dSJanosch Frank cmd->rc, cmd->rrc);
2762e0d2773dSJanosch Frank break;
2763e0d2773dSJanosch Frank }
2764e0d2773dSJanosch Frank case KVM_PV_UNSHARE_ALL: {
2765e0d2773dSJanosch Frank r = -EINVAL;
2766e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm))
2767e0d2773dSJanosch Frank break;
2768e0d2773dSJanosch Frank
2769e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2770e0d2773dSJanosch Frank UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc);
2771e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x",
2772e0d2773dSJanosch Frank cmd->rc, cmd->rrc);
2773e0d2773dSJanosch Frank break;
2774e0d2773dSJanosch Frank }
277535d02493SJanosch Frank case KVM_PV_INFO: {
277635d02493SJanosch Frank struct kvm_s390_pv_info info = {};
277735d02493SJanosch Frank ssize_t data_len;
277835d02493SJanosch Frank
277935d02493SJanosch Frank /*
278035d02493SJanosch Frank * No need to check the VM protection here.
278135d02493SJanosch Frank *
278235d02493SJanosch Frank * Maybe user space wants to query some of the data
278335d02493SJanosch Frank * when the VM is still unprotected. If we see the
278435d02493SJanosch Frank * need to fence a new data command we can still
278535d02493SJanosch Frank * return an error in the info handler.
278635d02493SJanosch Frank */
278735d02493SJanosch Frank
278835d02493SJanosch Frank r = -EFAULT;
278935d02493SJanosch Frank if (copy_from_user(&info, argp, sizeof(info.header)))
279035d02493SJanosch Frank break;
279135d02493SJanosch Frank
279235d02493SJanosch Frank r = -EINVAL;
279335d02493SJanosch Frank if (info.header.len_max < sizeof(info.header))
279435d02493SJanosch Frank break;
279535d02493SJanosch Frank
279635d02493SJanosch Frank data_len = kvm_s390_handle_pv_info(&info);
279735d02493SJanosch Frank if (data_len < 0) {
279835d02493SJanosch Frank r = data_len;
279935d02493SJanosch Frank break;
280035d02493SJanosch Frank }
280135d02493SJanosch Frank /*
280235d02493SJanosch Frank * If a data command struct is extended (multiple
280335d02493SJanosch Frank * times) this can be used to determine how much of it
280435d02493SJanosch Frank * is valid.
280535d02493SJanosch Frank */
280635d02493SJanosch Frank info.header.len_written = data_len;
280735d02493SJanosch Frank
280835d02493SJanosch Frank r = -EFAULT;
280935d02493SJanosch Frank if (copy_to_user(argp, &info, data_len))
281035d02493SJanosch Frank break;
281135d02493SJanosch Frank
281235d02493SJanosch Frank r = 0;
281335d02493SJanosch Frank break;
281435d02493SJanosch Frank }
28150460eb35SJanosch Frank case KVM_PV_DUMP: {
28160460eb35SJanosch Frank struct kvm_s390_pv_dmp dmp;
28170460eb35SJanosch Frank
28180460eb35SJanosch Frank r = -EINVAL;
28190460eb35SJanosch Frank if (!kvm_s390_pv_is_protected(kvm))
28200460eb35SJanosch Frank break;
28210460eb35SJanosch Frank
28220460eb35SJanosch Frank r = -EFAULT;
28230460eb35SJanosch Frank if (copy_from_user(&dmp, argp, sizeof(dmp)))
28240460eb35SJanosch Frank break;
28250460eb35SJanosch Frank
28260460eb35SJanosch Frank r = kvm_s390_pv_dmp(kvm, cmd, dmp);
28270460eb35SJanosch Frank if (r)
28280460eb35SJanosch Frank break;
28290460eb35SJanosch Frank
28300460eb35SJanosch Frank if (copy_to_user(argp, &dmp, sizeof(dmp))) {
28310460eb35SJanosch Frank r = -EFAULT;
28320460eb35SJanosch Frank break;
28330460eb35SJanosch Frank }
28340460eb35SJanosch Frank
28350460eb35SJanosch Frank break;
28360460eb35SJanosch Frank }
283729b40f10SJanosch Frank default:
283829b40f10SJanosch Frank r = -ENOTTY;
283929b40f10SJanosch Frank }
2840fb491d55SClaudio Imbrenda if (need_lock)
2841fb491d55SClaudio Imbrenda mutex_unlock(&kvm->lock);
2842fb491d55SClaudio Imbrenda
284329b40f10SJanosch Frank return r;
284429b40f10SJanosch Frank }
284529b40f10SJanosch Frank
mem_op_validate_common(struct kvm_s390_mem_op * mop,u64 supported_flags)2846a41f505eSJanis Schoetterl-Glausch static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_flags)
2847e9e9feebSJanis Schoetterl-Glausch {
28483d9042f8SJanis Schoetterl-Glausch if (mop->flags & ~supported_flags || !mop->size)
2849ef11c946SJanis Schoetterl-Glausch return -EINVAL;
2850ef11c946SJanis Schoetterl-Glausch if (mop->size > MEM_OP_MAX_SIZE)
2851ef11c946SJanis Schoetterl-Glausch return -E2BIG;
2852ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
2853a41f505eSJanis Schoetterl-Glausch if (mop->key > 0xf)
2854ef11c946SJanis Schoetterl-Glausch return -EINVAL;
2855ef11c946SJanis Schoetterl-Glausch } else {
2856ef11c946SJanis Schoetterl-Glausch mop->key = 0;
2857ef11c946SJanis Schoetterl-Glausch }
2858a41f505eSJanis Schoetterl-Glausch return 0;
2859e9e9feebSJanis Schoetterl-Glausch }
2860e9e9feebSJanis Schoetterl-Glausch
kvm_s390_vm_mem_op_abs(struct kvm * kvm,struct kvm_s390_mem_op * mop)28618550bcb7SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop)
2862ef11c946SJanis Schoetterl-Glausch {
2863ef11c946SJanis Schoetterl-Glausch void __user *uaddr = (void __user *)mop->buf;
28640d6d4d23SJanis Schoetterl-Glausch enum gacc_mode acc_mode;
2865ef11c946SJanis Schoetterl-Glausch void *tmpbuf = NULL;
2866ef11c946SJanis Schoetterl-Glausch int r, srcu_idx;
2867ef11c946SJanis Schoetterl-Glausch
2868a41f505eSJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION |
2869a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_CHECK_ONLY);
2870a41f505eSJanis Schoetterl-Glausch if (r)
2871a41f505eSJanis Schoetterl-Glausch return r;
2872a41f505eSJanis Schoetterl-Glausch
2873ef11c946SJanis Schoetterl-Glausch if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
2874ef11c946SJanis Schoetterl-Glausch tmpbuf = vmalloc(mop->size);
2875ef11c946SJanis Schoetterl-Glausch if (!tmpbuf)
2876ef11c946SJanis Schoetterl-Glausch return -ENOMEM;
2877ef11c946SJanis Schoetterl-Glausch }
2878ef11c946SJanis Schoetterl-Glausch
2879ef11c946SJanis Schoetterl-Glausch srcu_idx = srcu_read_lock(&kvm->srcu);
2880ef11c946SJanis Schoetterl-Glausch
2881ef11c946SJanis Schoetterl-Glausch if (kvm_is_error_gpa(kvm, mop->gaddr)) {
2882ef11c946SJanis Schoetterl-Glausch r = PGM_ADDRESSING;
2883ef11c946SJanis Schoetterl-Glausch goto out_unlock;
2884ef11c946SJanis Schoetterl-Glausch }
2885ef11c946SJanis Schoetterl-Glausch
28860d6d4d23SJanis Schoetterl-Glausch acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE;
2887ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
28880d6d4d23SJanis Schoetterl-Glausch r = check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key);
28890d6d4d23SJanis Schoetterl-Glausch goto out_unlock;
28900d6d4d23SJanis Schoetterl-Glausch }
28910d6d4d23SJanis Schoetterl-Glausch if (acc_mode == GACC_FETCH) {
2892ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
2893ef11c946SJanis Schoetterl-Glausch mop->size, GACC_FETCH, mop->key);
28940d6d4d23SJanis Schoetterl-Glausch if (r)
28950d6d4d23SJanis Schoetterl-Glausch goto out_unlock;
2896ef11c946SJanis Schoetterl-Glausch if (copy_to_user(uaddr, tmpbuf, mop->size))
2897ef11c946SJanis Schoetterl-Glausch r = -EFAULT;
2898ef11c946SJanis Schoetterl-Glausch } else {
2899ef11c946SJanis Schoetterl-Glausch if (copy_from_user(tmpbuf, uaddr, mop->size)) {
2900ef11c946SJanis Schoetterl-Glausch r = -EFAULT;
29010d6d4d23SJanis Schoetterl-Glausch goto out_unlock;
2902ef11c946SJanis Schoetterl-Glausch }
2903ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
2904ef11c946SJanis Schoetterl-Glausch mop->size, GACC_STORE, mop->key);
2905ef11c946SJanis Schoetterl-Glausch }
2906ef11c946SJanis Schoetterl-Glausch
2907ef11c946SJanis Schoetterl-Glausch out_unlock:
2908ef11c946SJanis Schoetterl-Glausch srcu_read_unlock(&kvm->srcu, srcu_idx);
2909ef11c946SJanis Schoetterl-Glausch
2910ef11c946SJanis Schoetterl-Glausch vfree(tmpbuf);
2911ef11c946SJanis Schoetterl-Glausch return r;
2912ef11c946SJanis Schoetterl-Glausch }
2913ef11c946SJanis Schoetterl-Glausch
kvm_s390_vm_mem_op_cmpxchg(struct kvm * kvm,struct kvm_s390_mem_op * mop)29143fd49805SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op_cmpxchg(struct kvm *kvm, struct kvm_s390_mem_op *mop)
29153fd49805SJanis Schoetterl-Glausch {
29163fd49805SJanis Schoetterl-Glausch void __user *uaddr = (void __user *)mop->buf;
29173fd49805SJanis Schoetterl-Glausch void __user *old_addr = (void __user *)mop->old_addr;
29183fd49805SJanis Schoetterl-Glausch union {
29193fd49805SJanis Schoetterl-Glausch __uint128_t quad;
29203fd49805SJanis Schoetterl-Glausch char raw[sizeof(__uint128_t)];
29213fd49805SJanis Schoetterl-Glausch } old = { .quad = 0}, new = { .quad = 0 };
29223fd49805SJanis Schoetterl-Glausch unsigned int off_in_quad = sizeof(new) - mop->size;
29233fd49805SJanis Schoetterl-Glausch int r, srcu_idx;
29243fd49805SJanis Schoetterl-Glausch bool success;
29253fd49805SJanis Schoetterl-Glausch
29263fd49805SJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION);
29273fd49805SJanis Schoetterl-Glausch if (r)
29283fd49805SJanis Schoetterl-Glausch return r;
29293fd49805SJanis Schoetterl-Glausch /*
29303fd49805SJanis Schoetterl-Glausch * This validates off_in_quad. Checking that size is a power
29313fd49805SJanis Schoetterl-Glausch * of two is not necessary, as cmpxchg_guest_abs_with_key
29323fd49805SJanis Schoetterl-Glausch * takes care of that
29333fd49805SJanis Schoetterl-Glausch */
29343fd49805SJanis Schoetterl-Glausch if (mop->size > sizeof(new))
29353fd49805SJanis Schoetterl-Glausch return -EINVAL;
29363fd49805SJanis Schoetterl-Glausch if (copy_from_user(&new.raw[off_in_quad], uaddr, mop->size))
29373fd49805SJanis Schoetterl-Glausch return -EFAULT;
29383fd49805SJanis Schoetterl-Glausch if (copy_from_user(&old.raw[off_in_quad], old_addr, mop->size))
29393fd49805SJanis Schoetterl-Glausch return -EFAULT;
29403fd49805SJanis Schoetterl-Glausch
29413fd49805SJanis Schoetterl-Glausch srcu_idx = srcu_read_lock(&kvm->srcu);
29423fd49805SJanis Schoetterl-Glausch
29433fd49805SJanis Schoetterl-Glausch if (kvm_is_error_gpa(kvm, mop->gaddr)) {
29443fd49805SJanis Schoetterl-Glausch r = PGM_ADDRESSING;
29453fd49805SJanis Schoetterl-Glausch goto out_unlock;
29463fd49805SJanis Schoetterl-Glausch }
29473fd49805SJanis Schoetterl-Glausch
29483fd49805SJanis Schoetterl-Glausch r = cmpxchg_guest_abs_with_key(kvm, mop->gaddr, mop->size, &old.quad,
29493fd49805SJanis Schoetterl-Glausch new.quad, mop->key, &success);
29503fd49805SJanis Schoetterl-Glausch if (!success && copy_to_user(old_addr, &old.raw[off_in_quad], mop->size))
29513fd49805SJanis Schoetterl-Glausch r = -EFAULT;
29523fd49805SJanis Schoetterl-Glausch
29533fd49805SJanis Schoetterl-Glausch out_unlock:
29543fd49805SJanis Schoetterl-Glausch srcu_read_unlock(&kvm->srcu, srcu_idx);
29553fd49805SJanis Schoetterl-Glausch return r;
29563fd49805SJanis Schoetterl-Glausch }
29573fd49805SJanis Schoetterl-Glausch
kvm_s390_vm_mem_op(struct kvm * kvm,struct kvm_s390_mem_op * mop)29588550bcb7SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
29598550bcb7SJanis Schoetterl-Glausch {
29608550bcb7SJanis Schoetterl-Glausch /*
29618550bcb7SJanis Schoetterl-Glausch * This is technically a heuristic only, if the kvm->lock is not
29628550bcb7SJanis Schoetterl-Glausch * taken, it is not guaranteed that the vm is/remains non-protected.
29638550bcb7SJanis Schoetterl-Glausch * This is ok from a kernel perspective, wrongdoing is detected
29648550bcb7SJanis Schoetterl-Glausch * on the access, -EFAULT is returned and the vm may crash the
29658550bcb7SJanis Schoetterl-Glausch * next time it accesses the memory in question.
29668550bcb7SJanis Schoetterl-Glausch * There is no sane usecase to do switching and a memop on two
29678550bcb7SJanis Schoetterl-Glausch * different CPUs at the same time.
29688550bcb7SJanis Schoetterl-Glausch */
29698550bcb7SJanis Schoetterl-Glausch if (kvm_s390_pv_get_handle(kvm))
29708550bcb7SJanis Schoetterl-Glausch return -EINVAL;
29718550bcb7SJanis Schoetterl-Glausch
29728550bcb7SJanis Schoetterl-Glausch switch (mop->op) {
29738550bcb7SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_READ:
29748550bcb7SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_WRITE:
29758550bcb7SJanis Schoetterl-Glausch return kvm_s390_vm_mem_op_abs(kvm, mop);
29763fd49805SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_CMPXCHG:
29773fd49805SJanis Schoetterl-Glausch return kvm_s390_vm_mem_op_cmpxchg(kvm, mop);
29788550bcb7SJanis Schoetterl-Glausch default:
29798550bcb7SJanis Schoetterl-Glausch return -EINVAL;
29808550bcb7SJanis Schoetterl-Glausch }
29818550bcb7SJanis Schoetterl-Glausch }
29828550bcb7SJanis Schoetterl-Glausch
kvm_arch_vm_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)2983d8708b80SThomas Huth int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
2984b0c632dbSHeiko Carstens {
2985b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data;
2986b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg;
2987f2061656SDominik Dingel struct kvm_device_attr attr;
2988b0c632dbSHeiko Carstens int r;
2989b0c632dbSHeiko Carstens
2990b0c632dbSHeiko Carstens switch (ioctl) {
2991ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: {
2992ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int;
2993ba5c1e9bSCarsten Otte
2994ba5c1e9bSCarsten Otte r = -EFAULT;
2995ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int)))
2996ba5c1e9bSCarsten Otte break;
2997ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int);
2998ba5c1e9bSCarsten Otte break;
2999ba5c1e9bSCarsten Otte }
300084223598SCornelia Huck case KVM_CREATE_IRQCHIP: {
300184223598SCornelia Huck struct kvm_irq_routing_entry routing;
300284223598SCornelia Huck
300384223598SCornelia Huck r = -EINVAL;
300484223598SCornelia Huck if (kvm->arch.use_irqchip) {
300584223598SCornelia Huck /* Set up dummy routing. */
300684223598SCornelia Huck memset(&routing, 0, sizeof(routing));
3007152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0);
300884223598SCornelia Huck }
300984223598SCornelia Huck break;
301084223598SCornelia Huck }
3011f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: {
3012f2061656SDominik Dingel r = -EFAULT;
3013f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
3014f2061656SDominik Dingel break;
3015f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr);
3016f2061656SDominik Dingel break;
3017f2061656SDominik Dingel }
3018f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: {
3019f2061656SDominik Dingel r = -EFAULT;
3020f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
3021f2061656SDominik Dingel break;
3022f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr);
3023f2061656SDominik Dingel break;
3024f2061656SDominik Dingel }
3025f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: {
3026f2061656SDominik Dingel r = -EFAULT;
3027f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
3028f2061656SDominik Dingel break;
3029f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr);
3030f2061656SDominik Dingel break;
3031f2061656SDominik Dingel }
303230ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: {
303330ee2a98SJason J. Herne struct kvm_s390_skeys args;
303430ee2a98SJason J. Herne
303530ee2a98SJason J. Herne r = -EFAULT;
303630ee2a98SJason J. Herne if (copy_from_user(&args, argp,
303730ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys)))
303830ee2a98SJason J. Herne break;
303930ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args);
304030ee2a98SJason J. Herne break;
304130ee2a98SJason J. Herne }
304230ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: {
304330ee2a98SJason J. Herne struct kvm_s390_skeys args;
304430ee2a98SJason J. Herne
304530ee2a98SJason J. Herne r = -EFAULT;
304630ee2a98SJason J. Herne if (copy_from_user(&args, argp,
304730ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys)))
304830ee2a98SJason J. Herne break;
304930ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args);
305030ee2a98SJason J. Herne break;
305130ee2a98SJason J. Herne }
30524036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: {
30534036e387SClaudio Imbrenda struct kvm_s390_cmma_log args;
30544036e387SClaudio Imbrenda
30554036e387SClaudio Imbrenda r = -EFAULT;
30564036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args)))
30574036e387SClaudio Imbrenda break;
30581de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock);
30594036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args);
30601de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock);
30614036e387SClaudio Imbrenda if (!r) {
30624036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args));
30634036e387SClaudio Imbrenda if (r)
30644036e387SClaudio Imbrenda r = -EFAULT;
30654036e387SClaudio Imbrenda }
30664036e387SClaudio Imbrenda break;
30674036e387SClaudio Imbrenda }
30684036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: {
30694036e387SClaudio Imbrenda struct kvm_s390_cmma_log args;
30704036e387SClaudio Imbrenda
30714036e387SClaudio Imbrenda r = -EFAULT;
30724036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args)))
30734036e387SClaudio Imbrenda break;
30741de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock);
30754036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args);
30761de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock);
30774036e387SClaudio Imbrenda break;
30784036e387SClaudio Imbrenda }
307929b40f10SJanosch Frank case KVM_S390_PV_COMMAND: {
308029b40f10SJanosch Frank struct kvm_pv_cmd args;
308129b40f10SJanosch Frank
308267cf68b6SEric Farman /* protvirt means user cpu state */
308367cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(kvm);
308429b40f10SJanosch Frank r = 0;
308529b40f10SJanosch Frank if (!is_prot_virt_host()) {
308629b40f10SJanosch Frank r = -EINVAL;
308729b40f10SJanosch Frank break;
308829b40f10SJanosch Frank }
308929b40f10SJanosch Frank if (copy_from_user(&args, argp, sizeof(args))) {
309029b40f10SJanosch Frank r = -EFAULT;
309129b40f10SJanosch Frank break;
309229b40f10SJanosch Frank }
309329b40f10SJanosch Frank if (args.flags) {
309429b40f10SJanosch Frank r = -EINVAL;
309529b40f10SJanosch Frank break;
309629b40f10SJanosch Frank }
3097fb491d55SClaudio Imbrenda /* must be called without kvm->lock */
309829b40f10SJanosch Frank r = kvm_s390_handle_pv(kvm, &args);
309929b40f10SJanosch Frank if (copy_to_user(argp, &args, sizeof(args))) {
310029b40f10SJanosch Frank r = -EFAULT;
310129b40f10SJanosch Frank break;
310229b40f10SJanosch Frank }
310329b40f10SJanosch Frank break;
310429b40f10SJanosch Frank }
3105ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEM_OP: {
3106ef11c946SJanis Schoetterl-Glausch struct kvm_s390_mem_op mem_op;
3107ef11c946SJanis Schoetterl-Glausch
3108ef11c946SJanis Schoetterl-Glausch if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
3109ef11c946SJanis Schoetterl-Glausch r = kvm_s390_vm_mem_op(kvm, &mem_op);
3110ef11c946SJanis Schoetterl-Glausch else
3111ef11c946SJanis Schoetterl-Glausch r = -EFAULT;
3112ef11c946SJanis Schoetterl-Glausch break;
3113ef11c946SJanis Schoetterl-Glausch }
3114db1c875eSMatthew Rosato case KVM_S390_ZPCI_OP: {
3115db1c875eSMatthew Rosato struct kvm_s390_zpci_op args;
3116db1c875eSMatthew Rosato
3117db1c875eSMatthew Rosato r = -EINVAL;
3118db1c875eSMatthew Rosato if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
3119db1c875eSMatthew Rosato break;
3120db1c875eSMatthew Rosato if (copy_from_user(&args, argp, sizeof(args))) {
3121db1c875eSMatthew Rosato r = -EFAULT;
3122db1c875eSMatthew Rosato break;
3123db1c875eSMatthew Rosato }
3124db1c875eSMatthew Rosato r = kvm_s390_pci_zpci_op(kvm, &args);
3125db1c875eSMatthew Rosato break;
3126db1c875eSMatthew Rosato }
3127b0c632dbSHeiko Carstens default:
3128367e1319SAvi Kivity r = -ENOTTY;
3129b0c632dbSHeiko Carstens }
3130b0c632dbSHeiko Carstens
3131b0c632dbSHeiko Carstens return r;
3132b0c632dbSHeiko Carstens }
3133b0c632dbSHeiko Carstens
kvm_s390_apxa_installed(void)313445c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
313545c9b47cSTony Krowiak {
3136e585b24aSTony Krowiak struct ap_config_info info;
313745c9b47cSTony Krowiak
3138e585b24aSTony Krowiak if (ap_instructions_available()) {
3139e585b24aSTony Krowiak if (ap_qci(&info) == 0)
3140e585b24aSTony Krowiak return info.apxa;
314145c9b47cSTony Krowiak }
314245c9b47cSTony Krowiak
314345c9b47cSTony Krowiak return 0;
314445c9b47cSTony Krowiak }
314545c9b47cSTony Krowiak
3146e585b24aSTony Krowiak /*
3147e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low
3148e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows:
3149e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
3150e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed.
3151e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is.
3152e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed
3153e585b24aSTony Krowiak */
kvm_s390_set_crycb_format(struct kvm * kvm)315445c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
315545c9b47cSTony Krowiak {
315645c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
315745c9b47cSTony Krowiak
3158e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */
3159e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
3160e585b24aSTony Krowiak
3161e585b24aSTony Krowiak /* Check whether MSAX3 is installed */
3162e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76))
3163e585b24aSTony Krowiak return;
3164e585b24aSTony Krowiak
316545c9b47cSTony Krowiak if (kvm_s390_apxa_installed())
316645c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
316745c9b47cSTony Krowiak else
316845c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
316945c9b47cSTony Krowiak }
317045c9b47cSTony Krowiak
317186956e70STony Krowiak /*
317286956e70STony Krowiak * kvm_arch_crypto_set_masks
317386956e70STony Krowiak *
317486956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks
317586956e70STony Krowiak * to be set.
317686956e70STony Krowiak * @apm: the mask identifying the accessible AP adapters
317786956e70STony Krowiak * @aqm: the mask identifying the accessible AP domains
317886956e70STony Krowiak * @adm: the mask identifying the accessible AP control domains
317986956e70STony Krowiak *
318086956e70STony Krowiak * Set the masks that identify the adapters, domains and control domains to
318186956e70STony Krowiak * which the KVM guest is granted access.
318286956e70STony Krowiak *
318386956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this
318486956e70STony Krowiak * function.
318586956e70STony Krowiak */
kvm_arch_crypto_set_masks(struct kvm * kvm,unsigned long * apm,unsigned long * aqm,unsigned long * adm)31860e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
31870e237e44SPierre Morel unsigned long *aqm, unsigned long *adm)
31880e237e44SPierre Morel {
31890e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
31900e237e44SPierre Morel
31910e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm);
31920e237e44SPierre Morel
31930e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
31940e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */
31950e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32);
31960e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
31970e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]);
31980e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32);
31990e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
32000e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]);
32010e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32);
32020e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
32030e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]);
32040e237e44SPierre Morel break;
32050e237e44SPierre Morel case CRYCB_FORMAT1:
32060e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */
32070e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8);
32080e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2);
32090e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2);
32100e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
32110e237e44SPierre Morel apm[0], *((unsigned short *)aqm),
32120e237e44SPierre Morel *((unsigned short *)adm));
32130e237e44SPierre Morel break;
32140e237e44SPierre Morel default: /* Can not happen */
32150e237e44SPierre Morel break;
32160e237e44SPierre Morel }
32170e237e44SPierre Morel
32180e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */
32190e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
32200e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm);
32210e237e44SPierre Morel }
32220e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
32230e237e44SPierre Morel
322486956e70STony Krowiak /*
322586956e70STony Krowiak * kvm_arch_crypto_clear_masks
322686956e70STony Krowiak *
322786956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks
322886956e70STony Krowiak * to be cleared.
322986956e70STony Krowiak *
323086956e70STony Krowiak * Clear the masks that identify the adapters, domains and control domains to
323186956e70STony Krowiak * which the KVM guest is granted access.
323286956e70STony Krowiak *
323386956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this
323486956e70STony Krowiak * function.
323586956e70STony Krowiak */
kvm_arch_crypto_clear_masks(struct kvm * kvm)323642104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm)
323742104598STony Krowiak {
323842104598STony Krowiak kvm_s390_vcpu_block_all(kvm);
323942104598STony Krowiak
324042104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0,
324142104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0));
324242104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0,
324342104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1));
324442104598STony Krowiak
32450e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
32466cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */
32476cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
324842104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm);
324942104598STony Krowiak }
325042104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
325142104598STony Krowiak
kvm_s390_get_initial_cpuid(void)32529bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
32539d8d5786SMichael Mueller {
32549bb0ec09SDavid Hildenbrand struct cpuid cpuid;
32559bb0ec09SDavid Hildenbrand
32569bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid);
32579bb0ec09SDavid Hildenbrand cpuid.version = 0xff;
32589bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid);
32599d8d5786SMichael Mueller }
32609d8d5786SMichael Mueller
kvm_s390_crypto_init(struct kvm * kvm)3261c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
32625102ee87STony Krowiak {
3263c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
326445c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm);
32651e753732STony Krowiak init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
32665102ee87STony Krowiak
3267e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76))
3268e585b24aSTony Krowiak return;
3269e585b24aSTony Krowiak
3270ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */
3271ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1;
3272ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1;
3273ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
3274ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
3275ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
3276ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
32775102ee87STony Krowiak }
32785102ee87STony Krowiak
sca_dispose(struct kvm * kvm)32797d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
32807d43bafcSEugene (jno) Dvurechenski {
32817d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca)
32825e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
32837d43bafcSEugene (jno) Dvurechenski else
32847d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca));
32857d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL;
32867d43bafcSEugene (jno) Dvurechenski }
32877d43bafcSEugene (jno) Dvurechenski
kvm_arch_free_vm(struct kvm * kvm)328809340b2fSMatthew Rosato void kvm_arch_free_vm(struct kvm *kvm)
328909340b2fSMatthew Rosato {
329009340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
329109340b2fSMatthew Rosato kvm_s390_pci_clear_list(kvm);
329209340b2fSMatthew Rosato
329309340b2fSMatthew Rosato __kvm_arch_free_vm(kvm);
329409340b2fSMatthew Rosato }
329509340b2fSMatthew Rosato
kvm_arch_init_vm(struct kvm * kvm,unsigned long type)3296e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
3297b0c632dbSHeiko Carstens {
3298c4196218SChristian Borntraeger gfp_t alloc_flags = GFP_KERNEL_ACCOUNT;
32999d8d5786SMichael Mueller int i, rc;
3300b0c632dbSHeiko Carstens char debug_name[16];
3301f6c137ffSChristian Borntraeger static unsigned long sca_offset;
3302b0c632dbSHeiko Carstens
3303e08b9637SCarsten Otte rc = -EINVAL;
3304e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
3305e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL)
3306e08b9637SCarsten Otte goto out_err;
3307e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
3308e08b9637SCarsten Otte goto out_err;
3309e08b9637SCarsten Otte #else
3310e08b9637SCarsten Otte if (type)
3311e08b9637SCarsten Otte goto out_err;
3312e08b9637SCarsten Otte #endif
3313e08b9637SCarsten Otte
3314b0c632dbSHeiko Carstens rc = s390_enable_sie();
3315b0c632dbSHeiko Carstens if (rc)
3316d89f5effSJan Kiszka goto out_err;
3317b0c632dbSHeiko Carstens
3318b290411aSCarsten Otte rc = -ENOMEM;
3319b290411aSCarsten Otte
332076a6dd72SDavid Hildenbrand if (!sclp.has_64bscao)
332176a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA;
33225e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock);
33239ac96d75SDavid Hildenbrand /* start with basic SCA */
332476a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
3325b0c632dbSHeiko Carstens if (!kvm->arch.sca)
3326d89f5effSJan Kiszka goto out_err;
33270d9ce162SJunaid Shahid mutex_lock(&kvm_lock);
3328c5c2c393SDavid Hildenbrand sca_offset += 16;
3329bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
3330c5c2c393SDavid Hildenbrand sca_offset = 0;
3331bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *)
3332bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset);
33330d9ce162SJunaid Shahid mutex_unlock(&kvm_lock);
3334b0c632dbSHeiko Carstens
3335b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid);
3336b0c632dbSHeiko Carstens
33371cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
3338b0c632dbSHeiko Carstens if (!kvm->arch.dbf)
333940f5b735SDominik Dingel goto out_err;
3340b0c632dbSHeiko Carstens
334119114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
3342c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 =
3343c4196218SChristian Borntraeger (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA);
3344c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2)
334540f5b735SDominik Dingel goto out_err;
33469d8d5786SMichael Mueller
334725c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm;
3348c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
3349c3b9e3e1SChristian Borntraeger
3350c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) {
335117e89e13SSven Schnelle kvm->arch.model.fac_mask[i] = stfle_fac_list[i] &
3352c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] |
3353c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]);
335417e89e13SSven Schnelle kvm->arch.model.fac_list[i] = stfle_fac_list[i] &
3355c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i];
3356c3b9e3e1SChristian Borntraeger }
3357346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
3358981467c9SMichael Mueller
33591935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */
33601935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138);
33611935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138);
33621935222dSDavid Hildenbrand /* we emulate STHYI in kvm */
336395ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74);
336495ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74);
33651bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) {
33661bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147);
33671bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147);
33681bab1c02SClaudio Imbrenda }
336995ca2cb5SJanosch Frank
337005f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65))
337105f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65);
337205f31e3bSPierre Morel
33739bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
337437c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff;
33759d8d5786SMichael Mueller
337619c654bfSSteffen Eiden kvm->arch.model.uv_feat_guest.feat = 0;
337719c654bfSSteffen Eiden
3378c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm);
33795102ee87STony Krowiak
338009340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
338109340b2fSMatthew Rosato mutex_lock(&kvm->lock);
338209340b2fSMatthew Rosato kvm_s390_pci_init_list(kvm);
338309340b2fSMatthew Rosato kvm_s390_vcpu_pci_enable_interp(kvm);
338409340b2fSMatthew Rosato mutex_unlock(&kvm->lock);
338509340b2fSMatthew Rosato }
338609340b2fSMatthew Rosato
338751978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock);
3388ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock);
33896d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++)
33906d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
33918a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq);
3392a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex);
3393ba5c1e9bSCarsten Otte
3394b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
339578f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type);
3396b0c632dbSHeiko Carstens
3397e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) {
3398e08b9637SCarsten Otte kvm->arch.gmap = NULL;
3399a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
3400e08b9637SCarsten Otte } else {
340132e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX)
3402ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX;
340332e6b236SGuenther Hutzl else
3404ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
340532e6b236SGuenther Hutzl sclp.hamax + 1);
34066ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
3407598841caSCarsten Otte if (!kvm->arch.gmap)
340840f5b735SDominik Dingel goto out_err;
34092c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm;
341024eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0;
3411e08b9637SCarsten Otte }
3412fa6b7fe9SCornelia Huck
3413c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi;
341455531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey;
34158ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock);
3416a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm);
3417cc674ef2SMichael Mueller if (use_gisa)
3418d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm);
3419fb491d55SClaudio Imbrenda INIT_LIST_HEAD(&kvm->arch.pv.need_cleanup);
3420fb491d55SClaudio Imbrenda kvm->arch.pv.set_aside = NULL;
34218335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
34228ad35755SDavid Hildenbrand
3423d89f5effSJan Kiszka return 0;
3424d89f5effSJan Kiszka out_err:
3425c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2);
342640f5b735SDominik Dingel debug_unregister(kvm->arch.dbf);
34277d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm);
342878f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc);
3429d89f5effSJan Kiszka return rc;
3430b0c632dbSHeiko Carstens }
3431b0c632dbSHeiko Carstens
kvm_arch_vcpu_destroy(struct kvm_vcpu * vcpu)3432d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3433d329c035SChristian Borntraeger {
343429b40f10SJanosch Frank u16 rc, rrc;
343529b40f10SJanosch Frank
3436d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu");
3437ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
343867335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu);
34393c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu);
3440bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm))
3441a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu);
344224fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1);
344327e0393fSCarsten Otte
344427e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm))
34456ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap);
344627e0393fSCarsten Otte
3447e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma)
3448b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu);
344929b40f10SJanosch Frank /* We can not hold the vcpu mutex here, we are already dying */
345029b40f10SJanosch Frank if (kvm_s390_pv_cpu_get_handle(vcpu))
345129b40f10SJanosch Frank kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
3452d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block));
3453d329c035SChristian Borntraeger }
3454d329c035SChristian Borntraeger
kvm_arch_destroy_vm(struct kvm * kvm)3455b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
3456b0c632dbSHeiko Carstens {
345729b40f10SJanosch Frank u16 rc, rrc;
345829b40f10SJanosch Frank
345927592ae8SMarc Zyngier kvm_destroy_vcpus(kvm);
34607d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm);
3461d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm);
346229b40f10SJanosch Frank /*
346329b40f10SJanosch Frank * We are already at the end of life and kvm->lock is not taken.
346429b40f10SJanosch Frank * This is ok as the file descriptor is closed by now and nobody
3465fb491d55SClaudio Imbrenda * can mess with the pv state.
346629b40f10SJanosch Frank */
3467fb491d55SClaudio Imbrenda kvm_s390_pv_deinit_cleanup_all(kvm, &rc, &rrc);
3468ca2fd060SClaudio Imbrenda /*
3469ca2fd060SClaudio Imbrenda * Remove the mmu notifier only when the whole KVM VM is torn down,
3470ca2fd060SClaudio Imbrenda * and only if one was registered to begin with. If the VM is
3471ca2fd060SClaudio Imbrenda * currently not protected, but has been previously been protected,
3472ca2fd060SClaudio Imbrenda * then it's possible that the notifier is still registered.
3473ca2fd060SClaudio Imbrenda */
3474ca2fd060SClaudio Imbrenda if (kvm->arch.pv.mmu_notifier.ops)
3475ca2fd060SClaudio Imbrenda mmu_notifier_unregister(&kvm->arch.pv.mmu_notifier, kvm->mm);
3476ca2fd060SClaudio Imbrenda
347729b40f10SJanosch Frank debug_unregister(kvm->arch.dbf);
3478c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2);
347927e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm))
34806ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap);
3481841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm);
348267335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm);
3483a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm);
34848335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
3485b0c632dbSHeiko Carstens }
3486b0c632dbSHeiko Carstens
3487b0c632dbSHeiko Carstens /* Section: vcpu related */
__kvm_ucontrol_vcpu_init(struct kvm_vcpu * vcpu)3488dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
3489b0c632dbSHeiko Carstens {
34906ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL);
349127e0393fSCarsten Otte if (!vcpu->arch.gmap)
349227e0393fSCarsten Otte return -ENOMEM;
34932c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm;
3494dafd032aSDominik Dingel
349527e0393fSCarsten Otte return 0;
349627e0393fSCarsten Otte }
349727e0393fSCarsten Otte
sca_del_vcpu(struct kvm_vcpu * vcpu)3498a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
3499a6e2f683SEugene (jno) Dvurechenski {
3500a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries())
3501a6940674SDavid Hildenbrand return;
35025e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock);
35037d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) {
35047d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca;
35057d43bafcSEugene (jno) Dvurechenski
35067d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
35077d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0;
35087d43bafcSEugene (jno) Dvurechenski } else {
3509bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca;
3510a6e2f683SEugene (jno) Dvurechenski
3511a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
3512a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0;
3513a6e2f683SEugene (jno) Dvurechenski }
35145e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock);
35157d43bafcSEugene (jno) Dvurechenski }
3516a6e2f683SEugene (jno) Dvurechenski
sca_add_vcpu(struct kvm_vcpu * vcpu)3517eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
3518a6e2f683SEugene (jno) Dvurechenski {
3519a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) {
3520fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(vcpu->kvm->arch.sca);
3521a6940674SDavid Hildenbrand
3522a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */
3523fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3524fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys;
3525f07afa04SDavid Hildenbrand return;
3526a6940674SDavid Hildenbrand }
3527eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock);
3528eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) {
3529eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca;
3530fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca);
35317d43bafcSEugene (jno) Dvurechenski
3532fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block);
3533fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3534fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys & ESCA_SCAOL_MASK;
35350c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
3536eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
35377d43bafcSEugene (jno) Dvurechenski } else {
3538eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca;
3539fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca);
3540a6e2f683SEugene (jno) Dvurechenski
3541fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block);
3542fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3543fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys;
3544eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
3545a6e2f683SEugene (jno) Dvurechenski }
3546eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock);
35475e044315SEugene (jno) Dvurechenski }
35485e044315SEugene (jno) Dvurechenski
35495e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
sca_copy_entry(struct esca_entry * d,struct bsca_entry * s)35505e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
35515e044315SEugene (jno) Dvurechenski {
35525e044315SEugene (jno) Dvurechenski d->sda = s->sda;
35535e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c;
35545e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn;
35555e044315SEugene (jno) Dvurechenski }
35565e044315SEugene (jno) Dvurechenski
sca_copy_b_to_e(struct esca_block * d,struct bsca_block * s)35575e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
35585e044315SEugene (jno) Dvurechenski {
35595e044315SEugene (jno) Dvurechenski int i;
35605e044315SEugene (jno) Dvurechenski
35615e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control;
35625e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn;
35635e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
35645e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]);
35655e044315SEugene (jno) Dvurechenski }
35665e044315SEugene (jno) Dvurechenski
sca_switch_to_extended(struct kvm * kvm)35675e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
35685e044315SEugene (jno) Dvurechenski {
35695e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca;
35705e044315SEugene (jno) Dvurechenski struct esca_block *new_sca;
35715e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu;
357246808a4cSMarc Zyngier unsigned long vcpu_idx;
35735e044315SEugene (jno) Dvurechenski u32 scaol, scaoh;
3574fe0ef003SNico Boehr phys_addr_t new_sca_phys;
35755e044315SEugene (jno) Dvurechenski
357629b40f10SJanosch Frank if (kvm->arch.use_esca)
357729b40f10SJanosch Frank return 0;
357829b40f10SJanosch Frank
3579c4196218SChristian Borntraeger new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);
35805e044315SEugene (jno) Dvurechenski if (!new_sca)
35815e044315SEugene (jno) Dvurechenski return -ENOMEM;
35825e044315SEugene (jno) Dvurechenski
3583fe0ef003SNico Boehr new_sca_phys = virt_to_phys(new_sca);
3584fe0ef003SNico Boehr scaoh = new_sca_phys >> 32;
3585fe0ef003SNico Boehr scaol = new_sca_phys & ESCA_SCAOL_MASK;
35865e044315SEugene (jno) Dvurechenski
35875e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm);
35885e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock);
35895e044315SEugene (jno) Dvurechenski
35905e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca);
35915e044315SEugene (jno) Dvurechenski
35925e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
35935e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh;
35945e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol;
35950c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
35965e044315SEugene (jno) Dvurechenski }
35975e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca;
35985e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1;
35995e044315SEugene (jno) Dvurechenski
36005e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock);
36015e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm);
36025e044315SEugene (jno) Dvurechenski
36035e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca);
36045e044315SEugene (jno) Dvurechenski
36058335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
36068335713aSChristian Borntraeger old_sca, kvm->arch.sca);
36075e044315SEugene (jno) Dvurechenski return 0;
36087d43bafcSEugene (jno) Dvurechenski }
3609a6e2f683SEugene (jno) Dvurechenski
sca_can_add_vcpu(struct kvm * kvm,unsigned int id)3610a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
3611a6e2f683SEugene (jno) Dvurechenski {
36125e044315SEugene (jno) Dvurechenski int rc;
36135e044315SEugene (jno) Dvurechenski
3614a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) {
3615a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS)
3616a6940674SDavid Hildenbrand return true;
3617a6940674SDavid Hildenbrand return false;
3618a6940674SDavid Hildenbrand }
36195e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS)
36205e044315SEugene (jno) Dvurechenski return true;
362176a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao)
36225e044315SEugene (jno) Dvurechenski return false;
36235e044315SEugene (jno) Dvurechenski
36245e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
36255e044315SEugene (jno) Dvurechenski
36265e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
3627a6e2f683SEugene (jno) Dvurechenski }
3628a6e2f683SEugene (jno) Dvurechenski
3629db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
__start_cpu_timer_accounting(struct kvm_vcpu * vcpu)3630db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3631db0758b2SDavid Hildenbrand {
3632db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
36339c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3634db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast();
36359c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3636db0758b2SDavid Hildenbrand }
3637db0758b2SDavid Hildenbrand
3638db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
__stop_cpu_timer_accounting(struct kvm_vcpu * vcpu)3639db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3640db0758b2SDavid Hildenbrand {
3641db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
36429c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3643db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
3644db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0;
36459c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3646db0758b2SDavid Hildenbrand }
3647db0758b2SDavid Hildenbrand
3648db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
__enable_cpu_timer_accounting(struct kvm_vcpu * vcpu)3649db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3650db0758b2SDavid Hildenbrand {
3651db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled);
3652db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true;
3653db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu);
3654db0758b2SDavid Hildenbrand }
3655db0758b2SDavid Hildenbrand
3656db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
__disable_cpu_timer_accounting(struct kvm_vcpu * vcpu)3657db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3658db0758b2SDavid Hildenbrand {
3659db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
3660db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu);
3661db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false;
3662db0758b2SDavid Hildenbrand }
3663db0758b2SDavid Hildenbrand
enable_cpu_timer_accounting(struct kvm_vcpu * vcpu)3664db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3665db0758b2SDavid Hildenbrand {
3666db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3667db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu);
3668db0758b2SDavid Hildenbrand preempt_enable();
3669db0758b2SDavid Hildenbrand }
3670db0758b2SDavid Hildenbrand
disable_cpu_timer_accounting(struct kvm_vcpu * vcpu)3671db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3672db0758b2SDavid Hildenbrand {
3673db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3674db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu);
3675db0758b2SDavid Hildenbrand preempt_enable();
3676db0758b2SDavid Hildenbrand }
3677db0758b2SDavid Hildenbrand
36784287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
kvm_s390_set_cpu_timer(struct kvm_vcpu * vcpu,__u64 cputm)36794287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
36804287f247SDavid Hildenbrand {
3681db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */
36829c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3683db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled)
3684db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast();
36854287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm;
36869c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3687db0758b2SDavid Hildenbrand preempt_enable();
36884287f247SDavid Hildenbrand }
36894287f247SDavid Hildenbrand
3690db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
kvm_s390_get_cpu_timer(struct kvm_vcpu * vcpu)36914287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
36924287f247SDavid Hildenbrand {
36939c23a131SDavid Hildenbrand unsigned int seq;
3694db0758b2SDavid Hildenbrand __u64 value;
3695db0758b2SDavid Hildenbrand
3696db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled))
36974287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm;
3698db0758b2SDavid Hildenbrand
36999c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */
37009c23a131SDavid Hildenbrand do {
37019c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
37029c23a131SDavid Hildenbrand /*
37039c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical
37049c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock.
37059c23a131SDavid Hildenbrand */
37069c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
3707db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm;
37089c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */
37099c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start))
3710db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
37119c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
37129c23a131SDavid Hildenbrand preempt_enable();
3713db0758b2SDavid Hildenbrand return value;
37144287f247SDavid Hildenbrand }
37154287f247SDavid Hildenbrand
kvm_arch_vcpu_load(struct kvm_vcpu * vcpu,int cpu)3716b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3717b0c632dbSHeiko Carstens {
37189977e886SHendrik Brueckner
371937d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap);
3720ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
37215ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3722db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu);
372301a745acSDavid Hildenbrand vcpu->cpu = cpu;
3724b0c632dbSHeiko Carstens }
3725b0c632dbSHeiko Carstens
kvm_arch_vcpu_put(struct kvm_vcpu * vcpu)3726b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3727b0c632dbSHeiko Carstens {
372801a745acSDavid Hildenbrand vcpu->cpu = -1;
37295ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3730db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu);
37319daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
373237d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled();
373337d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap);
37349977e886SHendrik Brueckner
3735b0c632dbSHeiko Carstens }
3736b0c632dbSHeiko Carstens
kvm_arch_vcpu_postcreate(struct kvm_vcpu * vcpu)373731928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
373842897d86SMarcelo Tosatti {
373972f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock);
3740fdf03650SFan Zhang preempt_disable();
374172f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
3742d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
3743fdf03650SFan Zhang preempt_enable();
374472f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock);
374525508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) {
3746dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap;
3747eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu);
374825508824SDavid Hildenbrand }
37496502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
37506502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
375137d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */
375237d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap;
375342897d86SMarcelo Tosatti }
375442897d86SMarcelo Tosatti
kvm_has_pckmo_subfunc(struct kvm * kvm,unsigned long nr)37558ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
37568ec2fa52SChristian Borntraeger {
37578ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
37588ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
37598ec2fa52SChristian Borntraeger return true;
37608ec2fa52SChristian Borntraeger return false;
37618ec2fa52SChristian Borntraeger }
37628ec2fa52SChristian Borntraeger
kvm_has_pckmo_ecc(struct kvm * kvm)37638ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm)
37648ec2fa52SChristian Borntraeger {
37658ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */
37668ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) ||
37678ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) ||
37688ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) ||
37698ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) ||
37708ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41);
37718ec2fa52SChristian Borntraeger
37728ec2fa52SChristian Borntraeger }
37738ec2fa52SChristian Borntraeger
kvm_s390_vcpu_crypto_setup(struct kvm_vcpu * vcpu)37745102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
37755102ee87STony Krowiak {
3776e585b24aSTony Krowiak /*
3777e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3
3778e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up.
3779e585b24aSTony Krowiak */
3780e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
37815102ee87STony Krowiak return;
37825102ee87STony Krowiak
3783e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
3784a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
378537940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE;
37868ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC;
3787a374e892STony Krowiak
3788e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie)
3789e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE;
3790e585b24aSTony Krowiak
3791e585b24aSTony Krowiak /* Set up protected key support */
37928ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) {
3793a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES;
37948ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */
37958ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm))
37968ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC;
37978ec2fa52SChristian Borntraeger }
37988ec2fa52SChristian Borntraeger
3799a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw)
3800a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
38015102ee87STony Krowiak }
38025102ee87STony Krowiak
kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu * vcpu)3803b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
3804b31605c1SDominik Dingel {
3805fe0ef003SNico Boehr free_page((unsigned long)phys_to_virt(vcpu->arch.sie_block->cbrlo));
3806b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0;
3807b31605c1SDominik Dingel }
3808b31605c1SDominik Dingel
kvm_s390_vcpu_setup_cmma(struct kvm_vcpu * vcpu)3809b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
3810b31605c1SDominik Dingel {
3811fe0ef003SNico Boehr void *cbrlo_page = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3812fe0ef003SNico Boehr
3813fe0ef003SNico Boehr if (!cbrlo_page)
3814b31605c1SDominik Dingel return -ENOMEM;
3815fe0ef003SNico Boehr
3816fe0ef003SNico Boehr vcpu->arch.sie_block->cbrlo = virt_to_phys(cbrlo_page);
3817b31605c1SDominik Dingel return 0;
3818b31605c1SDominik Dingel }
3819b31605c1SDominik Dingel
kvm_s390_vcpu_setup_model(struct kvm_vcpu * vcpu)382091520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
382191520f1aSMichael Mueller {
382291520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
382391520f1aSMichael Mueller
382491520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc;
382580bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7))
3826fe0ef003SNico Boehr vcpu->arch.sie_block->fac = virt_to_phys(model->fac_list);
382791520f1aSMichael Mueller }
382891520f1aSMichael Mueller
kvm_s390_vcpu_setup(struct kvm_vcpu * vcpu)3829ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
3830ff72bb55SSean Christopherson {
3831b31605c1SDominik Dingel int rc = 0;
383229b40f10SJanosch Frank u16 uvrc, uvrrc;
3833b31288faSKonstantin Weitz
38349e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
38359e6dabefSCornelia Huck CPUSTAT_SM |
3836a4a4f191SGuenther Hutzl CPUSTAT_STOPPED);
3837a4a4f191SGuenther Hutzl
383853df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78))
3839ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
384053df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8))
3841ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
3842a4a4f191SGuenther Hutzl
384391520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu);
384491520f1aSMichael Mueller
3845bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
3846bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP)
38470c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
3848bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9))
38490c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI;
385024fe0195SPierre Morel if (test_kvm_facility(vcpu->kvm, 11))
385124fe0195SPierre Morel vcpu->arch.sie_block->ecb |= ECB_PTF;
3852f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73))
38530c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE;
38547119decfSJanis Schoetterl-Glausch if (!kvm_is_ucontrol(vcpu->kvm))
38557119decfSJanis Schoetterl-Glausch vcpu->arch.sie_block->ecb |= ECB_SPECI;
38567feb6bb8SMichael Mueller
3857c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
38580c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
3859cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130))
38600c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
38610c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
386248ee7d3aSDavid Hildenbrand if (sclp.has_cei)
38630c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI;
386411ad65b7SDavid Hildenbrand if (sclp.has_ib)
38650c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB;
386637c5f6c8SDavid Hildenbrand if (sclp.has_siif)
38670c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII;
386837c5f6c8SDavid Hildenbrand if (sclp.has_sigpif)
38690c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI;
387018280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) {
38710c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX;
38720c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
387313211ea7SEric Farman }
38748fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139))
38758fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF;
3876a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156))
3877a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
3878d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) {
3879d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV;
3880d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3881d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3882d7c5cb01SMichael Mueller }
3883fe0ef003SNico Boehr vcpu->arch.sie_block->sdnxo = virt_to_phys(&vcpu->run->s.regs.sdnx) | SDNXC;
3884fe0ef003SNico Boehr vcpu->arch.sie_block->riccbd = virt_to_phys(&vcpu->run->s.regs.riccb);
3885730cd632SFarhan Ali
3886730cd632SFarhan Ali if (sclp.has_kss)
3887ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
3888730cd632SFarhan Ali else
3889492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
38905a5e6536SMatthew Rosato
3891e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) {
3892b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu);
3893b31605c1SDominik Dingel if (rc)
3894b31605c1SDominik Dingel return rc;
3895b31288faSKonstantin Weitz }
38960ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3897ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
38989d8d5786SMichael Mueller
389967d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM;
390067d49d52SCollin Walling
39015102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu);
39025102ee87STony Krowiak
39033f4bbb43SMatthew Rosato kvm_s390_vcpu_pci_setup(vcpu);
39043f4bbb43SMatthew Rosato
390529b40f10SJanosch Frank mutex_lock(&vcpu->kvm->lock);
390629b40f10SJanosch Frank if (kvm_s390_pv_is_protected(vcpu->kvm)) {
390729b40f10SJanosch Frank rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
390829b40f10SJanosch Frank if (rc)
390929b40f10SJanosch Frank kvm_s390_vcpu_unsetup_cmma(vcpu);
391029b40f10SJanosch Frank }
391129b40f10SJanosch Frank mutex_unlock(&vcpu->kvm->lock);
391229b40f10SJanosch Frank
3913b31605c1SDominik Dingel return rc;
3914b0c632dbSHeiko Carstens }
3915b0c632dbSHeiko Carstens
kvm_arch_vcpu_precreate(struct kvm * kvm,unsigned int id)3916897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3917897cc38eSSean Christopherson {
3918897cc38eSSean Christopherson if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
3919897cc38eSSean Christopherson return -EINVAL;
3920897cc38eSSean Christopherson return 0;
3921897cc38eSSean Christopherson }
3922897cc38eSSean Christopherson
kvm_arch_vcpu_create(struct kvm_vcpu * vcpu)3923e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3924b0c632dbSHeiko Carstens {
39257feb6bb8SMichael Mueller struct sie_page *sie_page;
3926897cc38eSSean Christopherson int rc;
39274d47555aSCarsten Otte
3928da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
3929c4196218SChristian Borntraeger sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT);
39307feb6bb8SMichael Mueller if (!sie_page)
3931e529ef66SSean Christopherson return -ENOMEM;
3932b0c632dbSHeiko Carstens
39337feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block;
3934fe0ef003SNico Boehr vcpu->arch.sie_block->itdba = virt_to_phys(&sie_page->itdb);
39357feb6bb8SMichael Mueller
3936efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */
3937efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0;
3938efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax;
3939efed1104SDavid Hildenbrand
3940e529ef66SSean Christopherson vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
3941ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock);
3942ee6a569dSMichael Mueller vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm);
39439c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount);
3944ba5c1e9bSCarsten Otte
3945321f8ee5SSean Christopherson vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3946321f8ee5SSean Christopherson kvm_clear_async_pf_completion_queue(vcpu);
3947321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
3948321f8ee5SSean Christopherson KVM_SYNC_GPRS |
3949321f8ee5SSean Christopherson KVM_SYNC_ACRS |
3950321f8ee5SSean Christopherson KVM_SYNC_CRS |
3951321f8ee5SSean Christopherson KVM_SYNC_ARCH0 |
395223a60f83SCollin Walling KVM_SYNC_PFAULT |
395323a60f83SCollin Walling KVM_SYNC_DIAG318;
3954321f8ee5SSean Christopherson kvm_s390_set_prefix(vcpu, 0);
3955321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 64))
3956321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
3957321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 82))
3958321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
3959321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 133))
3960321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
3961321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 156))
3962321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
3963321f8ee5SSean Christopherson /* fprs can be synchronized via vrs, even if the guest has no vx. With
3964321f8ee5SSean Christopherson * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
3965321f8ee5SSean Christopherson */
3966321f8ee5SSean Christopherson if (MACHINE_HAS_VX)
3967321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
3968321f8ee5SSean Christopherson else
3969321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
3970321f8ee5SSean Christopherson
3971321f8ee5SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) {
3972321f8ee5SSean Christopherson rc = __kvm_ucontrol_vcpu_init(vcpu);
3973321f8ee5SSean Christopherson if (rc)
3974a2017f17SSean Christopherson goto out_free_sie_block;
3975321f8ee5SSean Christopherson }
3976321f8ee5SSean Christopherson
3977e529ef66SSean Christopherson VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
3978e529ef66SSean Christopherson vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3979e529ef66SSean Christopherson trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3980b0c632dbSHeiko Carstens
3981ff72bb55SSean Christopherson rc = kvm_s390_vcpu_setup(vcpu);
3982ff72bb55SSean Christopherson if (rc)
3983ff72bb55SSean Christopherson goto out_ucontrol_uninit;
398424fe0195SPierre Morel
398524fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1);
3986e529ef66SSean Christopherson return 0;
3987e529ef66SSean Christopherson
3988ff72bb55SSean Christopherson out_ucontrol_uninit:
3989ff72bb55SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm))
3990ff72bb55SSean Christopherson gmap_remove(vcpu->arch.gmap);
39917b06bf2fSWei Yongjun out_free_sie_block:
39927b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block));
3993e529ef66SSean Christopherson return rc;
3994b0c632dbSHeiko Carstens }
3995b0c632dbSHeiko Carstens
kvm_arch_vcpu_runnable(struct kvm_vcpu * vcpu)3996b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3997b0c632dbSHeiko Carstens {
39989b57e9d5SHalil Pasic clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
39999a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0);
4000b0c632dbSHeiko Carstens }
4001b0c632dbSHeiko Carstens
kvm_arch_vcpu_in_kernel(struct kvm_vcpu * vcpu)4002199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
4003199b5763SLongpeng(Mike) {
40040546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
4005199b5763SLongpeng(Mike) }
4006199b5763SLongpeng(Mike)
kvm_s390_vcpu_block(struct kvm_vcpu * vcpu)400727406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
400849b99e1eSChristian Borntraeger {
4009805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
401061a6df54SDavid Hildenbrand exit_sie(vcpu);
401149b99e1eSChristian Borntraeger }
401249b99e1eSChristian Borntraeger
kvm_s390_vcpu_unblock(struct kvm_vcpu * vcpu)401327406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
401449b99e1eSChristian Borntraeger {
4015805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
401649b99e1eSChristian Borntraeger }
401749b99e1eSChristian Borntraeger
kvm_s390_vcpu_request(struct kvm_vcpu * vcpu)40188e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
40198e236546SChristian Borntraeger {
4020805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
402161a6df54SDavid Hildenbrand exit_sie(vcpu);
40228e236546SChristian Borntraeger }
40238e236546SChristian Borntraeger
kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu * vcpu)40249ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
40259ea59728SDavid Hildenbrand {
40269ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) &
40279ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST);
40289ea59728SDavid Hildenbrand }
40299ea59728SDavid Hildenbrand
kvm_s390_vcpu_request_handled(struct kvm_vcpu * vcpu)40308e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
40318e236546SChristian Borntraeger {
40329bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
40338e236546SChristian Borntraeger }
40348e236546SChristian Borntraeger
403549b99e1eSChristian Borntraeger /*
40369ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
403749b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will
403849b99e1eSChristian Borntraeger * return immediately. */
exit_sie(struct kvm_vcpu * vcpu)403949b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
404049b99e1eSChristian Borntraeger {
4041ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
40429ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu);
404349b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
404449b99e1eSChristian Borntraeger cpu_relax();
404549b99e1eSChristian Borntraeger }
404649b99e1eSChristian Borntraeger
40478e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
kvm_s390_sync_request(int req,struct kvm_vcpu * vcpu)40488e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
404949b99e1eSChristian Borntraeger {
4050df06dae3SSean Christopherson __kvm_make_request(req, vcpu);
40518e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu);
405249b99e1eSChristian Borntraeger }
405349b99e1eSChristian Borntraeger
kvm_gmap_notifier(struct gmap * gmap,unsigned long start,unsigned long end)4054414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
4055414d3b07SMartin Schwidefsky unsigned long end)
40562c70fe44SChristian Borntraeger {
40572c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private;
40582c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu;
4059414d3b07SMartin Schwidefsky unsigned long prefix;
406046808a4cSMarc Zyngier unsigned long i;
40612c70fe44SChristian Borntraeger
406265d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap))
406365d0b0d4SDavid Hildenbrand return;
4064414d3b07SMartin Schwidefsky if (start >= 1UL << 31)
4065414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */
4066414d3b07SMartin Schwidefsky return;
40672c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) {
40682c70fe44SChristian Borntraeger /* match against both prefix pages */
4069414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu);
4070414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
4071414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
4072414d3b07SMartin Schwidefsky start, end);
4073cc65c3a1SSean Christopherson kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
40742c70fe44SChristian Borntraeger }
40752c70fe44SChristian Borntraeger }
40762c70fe44SChristian Borntraeger }
40772c70fe44SChristian Borntraeger
kvm_arch_no_poll(struct kvm_vcpu * vcpu)40788b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
40798b905d28SChristian Borntraeger {
40808b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */
40818b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >=
40826f390916SSean Christopherson READ_ONCE(halt_poll_max_steal)) {
40838b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++;
40848b905d28SChristian Borntraeger return true;
40858b905d28SChristian Borntraeger }
40868b905d28SChristian Borntraeger return false;
40878b905d28SChristian Borntraeger }
40888b905d28SChristian Borntraeger
kvm_arch_vcpu_should_kick(struct kvm_vcpu * vcpu)4089b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
4090b6d33834SChristoffer Dall {
4091b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */
4092b6d33834SChristoffer Dall BUG();
4093b6d33834SChristoffer Dall return 0;
4094b6d33834SChristoffer Dall }
4095b6d33834SChristoffer Dall
kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu * vcpu,struct kvm_one_reg * reg)409614eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
409714eebd91SCarsten Otte struct kvm_one_reg *reg)
409814eebd91SCarsten Otte {
409914eebd91SCarsten Otte int r = -EINVAL;
410014eebd91SCarsten Otte
410114eebd91SCarsten Otte switch (reg->id) {
410229b7c71bSCarsten Otte case KVM_REG_S390_TODPR:
410329b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr,
410429b7c71bSCarsten Otte (u32 __user *)reg->addr);
410529b7c71bSCarsten Otte break;
410629b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF:
410729b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch,
410829b7c71bSCarsten Otte (u64 __user *)reg->addr);
410929b7c71bSCarsten Otte break;
411046a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER:
41114287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu),
411246a6dd1cSJason J. herne (u64 __user *)reg->addr);
411346a6dd1cSJason J. herne break;
411446a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP:
411546a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc,
411646a6dd1cSJason J. herne (u64 __user *)reg->addr);
411746a6dd1cSJason J. herne break;
4118536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN:
4119536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token,
4120536336c2SDominik Dingel (u64 __user *)reg->addr);
4121536336c2SDominik Dingel break;
4122536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE:
4123536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare,
4124536336c2SDominik Dingel (u64 __user *)reg->addr);
4125536336c2SDominik Dingel break;
4126536336c2SDominik Dingel case KVM_REG_S390_PFSELECT:
4127536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select,
4128536336c2SDominik Dingel (u64 __user *)reg->addr);
4129536336c2SDominik Dingel break;
4130672550fbSChristian Borntraeger case KVM_REG_S390_PP:
4131672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp,
4132672550fbSChristian Borntraeger (u64 __user *)reg->addr);
4133672550fbSChristian Borntraeger break;
4134afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA:
4135afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea,
4136afa45ff5SChristian Borntraeger (u64 __user *)reg->addr);
4137afa45ff5SChristian Borntraeger break;
413814eebd91SCarsten Otte default:
413914eebd91SCarsten Otte break;
414014eebd91SCarsten Otte }
414114eebd91SCarsten Otte
414214eebd91SCarsten Otte return r;
414314eebd91SCarsten Otte }
414414eebd91SCarsten Otte
kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu * vcpu,struct kvm_one_reg * reg)414514eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
414614eebd91SCarsten Otte struct kvm_one_reg *reg)
414714eebd91SCarsten Otte {
414814eebd91SCarsten Otte int r = -EINVAL;
41494287f247SDavid Hildenbrand __u64 val;
415014eebd91SCarsten Otte
415114eebd91SCarsten Otte switch (reg->id) {
415229b7c71bSCarsten Otte case KVM_REG_S390_TODPR:
415329b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr,
415429b7c71bSCarsten Otte (u32 __user *)reg->addr);
415529b7c71bSCarsten Otte break;
415629b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF:
415729b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch,
415829b7c71bSCarsten Otte (u64 __user *)reg->addr);
415929b7c71bSCarsten Otte break;
416046a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER:
41614287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr);
41624287f247SDavid Hildenbrand if (!r)
41634287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val);
416446a6dd1cSJason J. herne break;
416546a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP:
416646a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc,
416746a6dd1cSJason J. herne (u64 __user *)reg->addr);
416846a6dd1cSJason J. herne break;
4169536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN:
4170536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token,
4171536336c2SDominik Dingel (u64 __user *)reg->addr);
41729fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
41739fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu);
4174536336c2SDominik Dingel break;
4175536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE:
4176536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare,
4177536336c2SDominik Dingel (u64 __user *)reg->addr);
4178536336c2SDominik Dingel break;
4179536336c2SDominik Dingel case KVM_REG_S390_PFSELECT:
4180536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select,
4181536336c2SDominik Dingel (u64 __user *)reg->addr);
4182536336c2SDominik Dingel break;
4183672550fbSChristian Borntraeger case KVM_REG_S390_PP:
4184672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp,
4185672550fbSChristian Borntraeger (u64 __user *)reg->addr);
4186672550fbSChristian Borntraeger break;
4187afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA:
4188afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea,
4189afa45ff5SChristian Borntraeger (u64 __user *)reg->addr);
4190afa45ff5SChristian Borntraeger break;
419114eebd91SCarsten Otte default:
419214eebd91SCarsten Otte break;
419314eebd91SCarsten Otte }
419414eebd91SCarsten Otte
419514eebd91SCarsten Otte return r;
419614eebd91SCarsten Otte }
4197b6d33834SChristoffer Dall
kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu * vcpu)41987de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
4199b0c632dbSHeiko Carstens {
42007de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
42017de3f142SJanosch Frank vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
42027de3f142SJanosch Frank memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
42037de3f142SJanosch Frank
42047de3f142SJanosch Frank kvm_clear_async_pf_completion_queue(vcpu);
42057de3f142SJanosch Frank if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
42067de3f142SJanosch Frank kvm_s390_vcpu_stop(vcpu);
42077de3f142SJanosch Frank kvm_s390_clear_local_irqs(vcpu);
42087de3f142SJanosch Frank }
42097de3f142SJanosch Frank
kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu * vcpu)42107de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
42117de3f142SJanosch Frank {
42127de3f142SJanosch Frank /* Initial reset is a superset of the normal reset */
42137de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu);
42147de3f142SJanosch Frank
4215e93fc7b4SChristian Borntraeger /*
4216e93fc7b4SChristian Borntraeger * This equals initial cpu reset in pop, but we don't switch to ESA.
4217e93fc7b4SChristian Borntraeger * We do not only reset the internal data, but also ...
4218e93fc7b4SChristian Borntraeger */
42197de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask = 0;
42207de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.addr = 0;
42217de3f142SJanosch Frank kvm_s390_set_prefix(vcpu, 0);
42227de3f142SJanosch Frank kvm_s390_set_cpu_timer(vcpu, 0);
42237de3f142SJanosch Frank vcpu->arch.sie_block->ckc = 0;
42247de3f142SJanosch Frank memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
42257de3f142SJanosch Frank vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
42267de3f142SJanosch Frank vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
4227e93fc7b4SChristian Borntraeger
4228e93fc7b4SChristian Borntraeger /* ... the data in sync regs */
4229e93fc7b4SChristian Borntraeger memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs));
4230e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0;
4231e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK;
4232e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK;
4233e93fc7b4SChristian Borntraeger vcpu->run->psw_addr = 0;
4234e93fc7b4SChristian Borntraeger vcpu->run->psw_mask = 0;
4235e93fc7b4SChristian Borntraeger vcpu->run->s.regs.todpr = 0;
4236e93fc7b4SChristian Borntraeger vcpu->run->s.regs.cputm = 0;
4237e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0;
4238e93fc7b4SChristian Borntraeger vcpu->run->s.regs.pp = 0;
4239e93fc7b4SChristian Borntraeger vcpu->run->s.regs.gbea = 1;
42407de3f142SJanosch Frank vcpu->run->s.regs.fpc = 0;
42410f303504SJanosch Frank /*
42420f303504SJanosch Frank * Do not reset these registers in the protected case, as some of
4243cada938aSHeiko Carstens * them are overlaid and they are not accessible in this case
42440f303504SJanosch Frank * anyway.
42450f303504SJanosch Frank */
42460f303504SJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
42477de3f142SJanosch Frank vcpu->arch.sie_block->gbea = 1;
42487de3f142SJanosch Frank vcpu->arch.sie_block->pp = 0;
42497de3f142SJanosch Frank vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
42500f303504SJanosch Frank vcpu->arch.sie_block->todpr = 0;
42510f303504SJanosch Frank }
42527de3f142SJanosch Frank }
42537de3f142SJanosch Frank
kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu * vcpu)42547de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
42557de3f142SJanosch Frank {
42567de3f142SJanosch Frank struct kvm_sync_regs *regs = &vcpu->run->s.regs;
42577de3f142SJanosch Frank
42587de3f142SJanosch Frank /* Clear reset is a superset of the initial reset */
42597de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu);
42607de3f142SJanosch Frank
42617de3f142SJanosch Frank memset(®s->gprs, 0, sizeof(regs->gprs));
42627de3f142SJanosch Frank memset(®s->vrs, 0, sizeof(regs->vrs));
42637de3f142SJanosch Frank memset(®s->acrs, 0, sizeof(regs->acrs));
42647de3f142SJanosch Frank memset(®s->gscb, 0, sizeof(regs->gscb));
42657de3f142SJanosch Frank
42667de3f142SJanosch Frank regs->etoken = 0;
42677de3f142SJanosch Frank regs->etoken_extension = 0;
4268b0c632dbSHeiko Carstens }
4269b0c632dbSHeiko Carstens
kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu * vcpu,struct kvm_regs * regs)4270b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4271b0c632dbSHeiko Carstens {
4272875656feSChristoffer Dall vcpu_load(vcpu);
42735a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs));
4274875656feSChristoffer Dall vcpu_put(vcpu);
4275b0c632dbSHeiko Carstens return 0;
4276b0c632dbSHeiko Carstens }
4277b0c632dbSHeiko Carstens
kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu * vcpu,struct kvm_regs * regs)4278b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4279b0c632dbSHeiko Carstens {
42801fc9b76bSChristoffer Dall vcpu_load(vcpu);
42815a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
42821fc9b76bSChristoffer Dall vcpu_put(vcpu);
4283b0c632dbSHeiko Carstens return 0;
4284b0c632dbSHeiko Carstens }
4285b0c632dbSHeiko Carstens
kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)4286b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4287b0c632dbSHeiko Carstens struct kvm_sregs *sregs)
4288b0c632dbSHeiko Carstens {
4289b4ef9d4eSChristoffer Dall vcpu_load(vcpu);
4290b4ef9d4eSChristoffer Dall
429159674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
4292b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
4293b4ef9d4eSChristoffer Dall
4294b4ef9d4eSChristoffer Dall vcpu_put(vcpu);
4295b0c632dbSHeiko Carstens return 0;
4296b0c632dbSHeiko Carstens }
4297b0c632dbSHeiko Carstens
kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)4298b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4299b0c632dbSHeiko Carstens struct kvm_sregs *sregs)
4300b0c632dbSHeiko Carstens {
4301bcdec41cSChristoffer Dall vcpu_load(vcpu);
4302bcdec41cSChristoffer Dall
430359674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
4304b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
4305bcdec41cSChristoffer Dall
4306bcdec41cSChristoffer Dall vcpu_put(vcpu);
4307b0c632dbSHeiko Carstens return 0;
4308b0c632dbSHeiko Carstens }
4309b0c632dbSHeiko Carstens
kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)4310b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4311b0c632dbSHeiko Carstens {
43126a96bc7fSChristoffer Dall int ret = 0;
43136a96bc7fSChristoffer Dall
43146a96bc7fSChristoffer Dall vcpu_load(vcpu);
43156a96bc7fSChristoffer Dall
4316e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc;
43179abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX)
4318a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
4319a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs);
43209abc2a08SDavid Hildenbrand else
4321a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
43226a96bc7fSChristoffer Dall
43236a96bc7fSChristoffer Dall vcpu_put(vcpu);
43246a96bc7fSChristoffer Dall return ret;
4325b0c632dbSHeiko Carstens }
4326b0c632dbSHeiko Carstens
kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)4327b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4328b0c632dbSHeiko Carstens {
43291393123eSChristoffer Dall vcpu_load(vcpu);
43301393123eSChristoffer Dall
43319abc2a08SDavid Hildenbrand /* make sure we have the latest values */
43329abc2a08SDavid Hildenbrand save_fpu_regs();
43339abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX)
4334a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs,
4335a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs);
43369abc2a08SDavid Hildenbrand else
4337a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
4338e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc;
43391393123eSChristoffer Dall
43401393123eSChristoffer Dall vcpu_put(vcpu);
4341b0c632dbSHeiko Carstens return 0;
4342b0c632dbSHeiko Carstens }
4343b0c632dbSHeiko Carstens
kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu * vcpu,psw_t psw)4344b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
4345b0c632dbSHeiko Carstens {
4346b0c632dbSHeiko Carstens int rc = 0;
4347b0c632dbSHeiko Carstens
43487a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu))
4349b0c632dbSHeiko Carstens rc = -EBUSY;
4350d7b0b5ebSCarsten Otte else {
4351d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask;
4352d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr;
4353d7b0b5ebSCarsten Otte }
4354b0c632dbSHeiko Carstens return rc;
4355b0c632dbSHeiko Carstens }
4356b0c632dbSHeiko Carstens
kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu * vcpu,struct kvm_translation * tr)4357b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4358b0c632dbSHeiko Carstens struct kvm_translation *tr)
4359b0c632dbSHeiko Carstens {
4360b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */
4361b0c632dbSHeiko Carstens }
4362b0c632dbSHeiko Carstens
436327291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
436427291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \
436527291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE)
436627291e21SDavid Hildenbrand
kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu * vcpu,struct kvm_guest_debug * dbg)4367d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4368d0bfb940SJan Kiszka struct kvm_guest_debug *dbg)
4369b0c632dbSHeiko Carstens {
437027291e21SDavid Hildenbrand int rc = 0;
437127291e21SDavid Hildenbrand
437266b56562SChristoffer Dall vcpu_load(vcpu);
437366b56562SChristoffer Dall
437427291e21SDavid Hildenbrand vcpu->guest_debug = 0;
437527291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu);
437627291e21SDavid Hildenbrand
437766b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
437866b56562SChristoffer Dall rc = -EINVAL;
437966b56562SChristoffer Dall goto out;
438066b56562SChristoffer Dall }
438166b56562SChristoffer Dall if (!sclp.has_gpere) {
438266b56562SChristoffer Dall rc = -EINVAL;
438366b56562SChristoffer Dall goto out;
438466b56562SChristoffer Dall }
438527291e21SDavid Hildenbrand
438627291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) {
438727291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control;
438827291e21SDavid Hildenbrand /* enforce guest PER */
4389ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
439027291e21SDavid Hildenbrand
439127291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
439227291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg);
439327291e21SDavid Hildenbrand } else {
43949daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
439527291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0;
439627291e21SDavid Hildenbrand }
439727291e21SDavid Hildenbrand
439827291e21SDavid Hildenbrand if (rc) {
439927291e21SDavid Hildenbrand vcpu->guest_debug = 0;
440027291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu);
44019daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
440227291e21SDavid Hildenbrand }
440327291e21SDavid Hildenbrand
440466b56562SChristoffer Dall out:
440566b56562SChristoffer Dall vcpu_put(vcpu);
440627291e21SDavid Hildenbrand return rc;
4407b0c632dbSHeiko Carstens }
4408b0c632dbSHeiko Carstens
kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)440962d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
441062d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state)
441162d9f0dbSMarcelo Tosatti {
4412fd232561SChristoffer Dall int ret;
4413fd232561SChristoffer Dall
4414fd232561SChristoffer Dall vcpu_load(vcpu);
4415fd232561SChristoffer Dall
44166352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */
4417fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
44186352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING;
4419fd232561SChristoffer Dall
4420fd232561SChristoffer Dall vcpu_put(vcpu);
4421fd232561SChristoffer Dall return ret;
442262d9f0dbSMarcelo Tosatti }
442362d9f0dbSMarcelo Tosatti
kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu * vcpu,struct kvm_mp_state * mp_state)442462d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
442562d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state)
442662d9f0dbSMarcelo Tosatti {
44276352e4d2SDavid Hildenbrand int rc = 0;
44286352e4d2SDavid Hildenbrand
4429e83dff5eSChristoffer Dall vcpu_load(vcpu);
4430e83dff5eSChristoffer Dall
44316352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */
443267cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm);
44336352e4d2SDavid Hildenbrand
44346352e4d2SDavid Hildenbrand switch (mp_state->mp_state) {
44356352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED:
4436fe28c786SJanosch Frank rc = kvm_s390_vcpu_stop(vcpu);
44376352e4d2SDavid Hildenbrand break;
44386352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING:
4439fe28c786SJanosch Frank rc = kvm_s390_vcpu_start(vcpu);
44406352e4d2SDavid Hildenbrand break;
44416352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD:
44427c36a3fcSJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
44437c36a3fcSJanosch Frank rc = -ENXIO;
44447c36a3fcSJanosch Frank break;
44457c36a3fcSJanosch Frank }
44467c36a3fcSJanosch Frank rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD);
44477c36a3fcSJanosch Frank break;
44486352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP:
44493b684a42SJoe Perches fallthrough; /* CHECK_STOP and LOAD are not supported yet */
44506352e4d2SDavid Hildenbrand default:
44516352e4d2SDavid Hildenbrand rc = -ENXIO;
44526352e4d2SDavid Hildenbrand }
44536352e4d2SDavid Hildenbrand
4454e83dff5eSChristoffer Dall vcpu_put(vcpu);
44556352e4d2SDavid Hildenbrand return rc;
445662d9f0dbSMarcelo Tosatti }
445762d9f0dbSMarcelo Tosatti
ibs_enabled(struct kvm_vcpu * vcpu)44588ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
44598ad35755SDavid Hildenbrand {
44608d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
44618ad35755SDavid Hildenbrand }
44628ad35755SDavid Hildenbrand
kvm_s390_handle_requests(struct kvm_vcpu * vcpu)44632c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
44642c70fe44SChristian Borntraeger {
44658ad35755SDavid Hildenbrand retry:
44668e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu);
44672fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu))
4468586b7ccdSChristian Borntraeger return 0;
44692c70fe44SChristian Borntraeger /*
4470cc65c3a1SSean Christopherson * If the guest prefix changed, re-arm the ipte notifier for the
4471b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
44722c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has
44732c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that
44742c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop.
44752c70fe44SChristian Borntraeger */
4476cc65c3a1SSean Christopherson if (kvm_check_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu)) {
44772c70fe44SChristian Borntraeger int rc;
4478b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap,
4479fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu),
4480b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE);
4481aca411a4SJulius Niedworok if (rc) {
4482cc65c3a1SSean Christopherson kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
44832c70fe44SChristian Borntraeger return rc;
4484aca411a4SJulius Niedworok }
44858ad35755SDavid Hildenbrand goto retry;
44862c70fe44SChristian Borntraeger }
44878ad35755SDavid Hildenbrand
4488d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
4489d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff;
4490d3d692c8SDavid Hildenbrand goto retry;
4491d3d692c8SDavid Hildenbrand }
4492d3d692c8SDavid Hildenbrand
44938ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
44948ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) {
44958ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
4496ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
44978ad35755SDavid Hildenbrand }
44988ad35755SDavid Hildenbrand goto retry;
44998ad35755SDavid Hildenbrand }
45008ad35755SDavid Hildenbrand
45018ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
45028ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) {
45038ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
45049daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
45058ad35755SDavid Hildenbrand }
45068ad35755SDavid Hildenbrand goto retry;
45078ad35755SDavid Hildenbrand }
45088ad35755SDavid Hildenbrand
45096502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
45106502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
45116502a34cSDavid Hildenbrand goto retry;
45126502a34cSDavid Hildenbrand }
45136502a34cSDavid Hildenbrand
4514190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
4515190df4a2SClaudio Imbrenda /*
4516c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA
4517190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional
4518190df4a2SClaudio Imbrenda * functionalities needed for live migration.
4519190df4a2SClaudio Imbrenda */
4520190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
4521190df4a2SClaudio Imbrenda goto retry;
4522190df4a2SClaudio Imbrenda }
4523190df4a2SClaudio Imbrenda
4524190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
4525190df4a2SClaudio Imbrenda /*
4526c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and
4527c9f0a2b8SJanosch Frank * CMM has been used.
4528190df4a2SClaudio Imbrenda */
4529190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) &&
4530c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm))
4531190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
4532190df4a2SClaudio Imbrenda goto retry;
4533190df4a2SClaudio Imbrenda }
4534190df4a2SClaudio Imbrenda
45353194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */
45363194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
45370759d068SDavid Hildenbrand
45382c70fe44SChristian Borntraeger return 0;
45392c70fe44SChristian Borntraeger }
45402c70fe44SChristian Borntraeger
__kvm_s390_set_tod_clock(struct kvm * kvm,const struct kvm_s390_vm_tod_clock * gtod)4541c0573ba5SClaudio Imbrenda static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
45428fa1696eSCollin L. Walling {
45438fa1696eSCollin L. Walling struct kvm_vcpu *vcpu;
45442cfd7b73SHeiko Carstens union tod_clock clk;
454546808a4cSMarc Zyngier unsigned long i;
45468fa1696eSCollin L. Walling
45478fa1696eSCollin L. Walling preempt_disable();
45488fa1696eSCollin L. Walling
45492cfd7b73SHeiko Carstens store_tod_clock_ext(&clk);
45508fa1696eSCollin L. Walling
45512cfd7b73SHeiko Carstens kvm->arch.epoch = gtod->tod - clk.tod;
45520e7def5fSDavid Hildenbrand kvm->arch.epdx = 0;
45530e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) {
45542cfd7b73SHeiko Carstens kvm->arch.epdx = gtod->epoch_idx - clk.ei;
45558fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod)
45568fa1696eSCollin L. Walling kvm->arch.epdx -= 1;
45570e7def5fSDavid Hildenbrand }
45588fa1696eSCollin L. Walling
45598fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm);
45608fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) {
45618fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch;
45628fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx;
45638fa1696eSCollin L. Walling }
45648fa1696eSCollin L. Walling
45658fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm);
45668fa1696eSCollin L. Walling preempt_enable();
4567c0573ba5SClaudio Imbrenda }
4568c0573ba5SClaudio Imbrenda
kvm_s390_try_set_tod_clock(struct kvm * kvm,const struct kvm_s390_vm_tod_clock * gtod)4569c0573ba5SClaudio Imbrenda int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
4570c0573ba5SClaudio Imbrenda {
4571c0573ba5SClaudio Imbrenda if (!mutex_trylock(&kvm->lock))
4572c0573ba5SClaudio Imbrenda return 0;
4573c0573ba5SClaudio Imbrenda __kvm_s390_set_tod_clock(kvm, gtod);
4574c0573ba5SClaudio Imbrenda mutex_unlock(&kvm->lock);
4575c0573ba5SClaudio Imbrenda return 1;
4576c0573ba5SClaudio Imbrenda }
4577c0573ba5SClaudio Imbrenda
4578fa576c58SThomas Huth /**
4579fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary
4580fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu
4581fa576c58SThomas Huth * @gpa: Guest physical address
4582fa576c58SThomas Huth * @writable: Whether the page should be writable or not
4583fa576c58SThomas Huth *
4584fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host.
4585fa576c58SThomas Huth *
4586fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise.
4587fa576c58SThomas Huth */
kvm_arch_fault_in_page(struct kvm_vcpu * vcpu,gpa_t gpa,int writable)4588fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
458924eb3a82SDominik Dingel {
4590527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa,
4591527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0);
459224eb3a82SDominik Dingel }
459324eb3a82SDominik Dingel
__kvm_inject_pfault_token(struct kvm_vcpu * vcpu,bool start_token,unsigned long token)45943c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
45953c038e6bSDominik Dingel unsigned long token)
45963c038e6bSDominik Dingel {
45973c038e6bSDominik Dingel struct kvm_s390_interrupt inti;
4598383d0b05SJens Freimann struct kvm_s390_irq irq;
45993c038e6bSDominik Dingel
46003c038e6bSDominik Dingel if (start_token) {
4601383d0b05SJens Freimann irq.u.ext.ext_params2 = token;
4602383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT;
4603383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
46043c038e6bSDominik Dingel } else {
46053c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE;
4606383d0b05SJens Freimann inti.parm64 = token;
46073c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
46083c038e6bSDominik Dingel }
46093c038e6bSDominik Dingel }
46103c038e6bSDominik Dingel
kvm_arch_async_page_not_present(struct kvm_vcpu * vcpu,struct kvm_async_pf * work)46112a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
46123c038e6bSDominik Dingel struct kvm_async_pf *work)
46133c038e6bSDominik Dingel {
46143c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
46153c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
46162a18b7e7SVitaly Kuznetsov
46172a18b7e7SVitaly Kuznetsov return true;
46183c038e6bSDominik Dingel }
46193c038e6bSDominik Dingel
kvm_arch_async_page_present(struct kvm_vcpu * vcpu,struct kvm_async_pf * work)46203c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
46213c038e6bSDominik Dingel struct kvm_async_pf *work)
46223c038e6bSDominik Dingel {
46233c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
46243c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
46253c038e6bSDominik Dingel }
46263c038e6bSDominik Dingel
kvm_arch_async_page_ready(struct kvm_vcpu * vcpu,struct kvm_async_pf * work)46273c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
46283c038e6bSDominik Dingel struct kvm_async_pf *work)
46293c038e6bSDominik Dingel {
46303c038e6bSDominik Dingel /* s390 will always inject the page directly */
46313c038e6bSDominik Dingel }
46323c038e6bSDominik Dingel
kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu * vcpu)46337c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
46343c038e6bSDominik Dingel {
46353c038e6bSDominik Dingel /*
46363c038e6bSDominik Dingel * s390 will always inject the page directly,
46373c038e6bSDominik Dingel * but we still want check_async_completion to cleanup
46383c038e6bSDominik Dingel */
46393c038e6bSDominik Dingel return true;
46403c038e6bSDominik Dingel }
46413c038e6bSDominik Dingel
kvm_arch_setup_async_pf(struct kvm_vcpu * vcpu)4642e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
46433c038e6bSDominik Dingel {
46443c038e6bSDominik Dingel hva_t hva;
46453c038e6bSDominik Dingel struct kvm_arch_async_pf arch;
46463c038e6bSDominik Dingel
46473c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4648e8c22266SVitaly Kuznetsov return false;
46493c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
46503c038e6bSDominik Dingel vcpu->arch.pfault_compare)
4651e8c22266SVitaly Kuznetsov return false;
46523c038e6bSDominik Dingel if (psw_extint_disabled(vcpu))
4653e8c22266SVitaly Kuznetsov return false;
46549a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0))
4655e8c22266SVitaly Kuznetsov return false;
4656b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
4657e8c22266SVitaly Kuznetsov return false;
46583c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled)
4659e8c22266SVitaly Kuznetsov return false;
46603c038e6bSDominik Dingel
466181480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
466281480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK;
466381480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
4664e8c22266SVitaly Kuznetsov return false;
46653c038e6bSDominik Dingel
4666e8c22266SVitaly Kuznetsov return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
46673c038e6bSDominik Dingel }
46683c038e6bSDominik Dingel
vcpu_pre_run(struct kvm_vcpu * vcpu)46693fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
4670b0c632dbSHeiko Carstens {
46713fb4c40fSThomas Huth int rc, cpuflags;
4672e168bf8dSCarsten Otte
46733c038e6bSDominik Dingel /*
46743c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly
46753c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is
46763c038e6bSDominik Dingel * handled outside the worker.
46773c038e6bSDominik Dingel */
46783c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu);
46793c038e6bSDominik Dingel
46807ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
46817ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
4682b0c632dbSHeiko Carstens
4683b0c632dbSHeiko Carstens if (need_resched())
4684b0c632dbSHeiko Carstens schedule();
4685b0c632dbSHeiko Carstens
468679395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) {
468779395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu);
468816631c42SIlya Leoshkevich if (rc || guestdbg_exit_pending(vcpu))
468979395031SJens Freimann return rc;
469079395031SJens Freimann }
46910ff31867SCarsten Otte
46922c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu);
46932c70fe44SChristian Borntraeger if (rc)
46942c70fe44SChristian Borntraeger return rc;
46952c70fe44SChristian Borntraeger
469627291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) {
469727291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu);
469827291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu);
469927291e21SDavid Hildenbrand }
470027291e21SDavid Hildenbrand
47014eeef242SSean Christopherson clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
47029f30f621SMichael Mueller
4703b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0;
47043fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
47053fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
47063fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags);
47072b29a9fdSDominik Dingel
47083fb4c40fSThomas Huth return 0;
47093fb4c40fSThomas Huth }
47103fb4c40fSThomas Huth
vcpu_post_run_fault_in_sie(struct kvm_vcpu * vcpu)4711492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
4712492d8642SThomas Huth {
471356317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = {
471456317920SDavid Hildenbrand .code = PGM_ADDRESSING,
471556317920SDavid Hildenbrand };
471656317920SDavid Hildenbrand u8 opcode, ilen;
4717492d8642SThomas Huth int rc;
4718492d8642SThomas Huth
4719492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4720492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu);
4721492d8642SThomas Huth
4722492d8642SThomas Huth /*
4723492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a
4724492d8642SThomas Huth * suppressing or terminating exception. However, since we came here
4725492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting
4726492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got
4727492d8642SThomas Huth * to look up the current opcode to get the length of the instruction
4728492d8642SThomas Huth * to be able to forward the PSW.
4729492d8642SThomas Huth */
47303fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
473156317920SDavid Hildenbrand ilen = insn_length(opcode);
47329b0d721aSDavid Hildenbrand if (rc < 0) {
47339b0d721aSDavid Hildenbrand return rc;
47349b0d721aSDavid Hildenbrand } else if (rc) {
47359b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen.
47369b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of
47379b0d721aSDavid Hildenbrand * nullification if necessary.
47389b0d721aSDavid Hildenbrand */
47399b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm;
47409b0d721aSDavid Hildenbrand ilen = 4;
47419b0d721aSDavid Hildenbrand }
474256317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
474356317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen);
474456317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
4745492d8642SThomas Huth }
4746492d8642SThomas Huth
vcpu_post_run(struct kvm_vcpu * vcpu,int exit_reason)47473fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
47483fb4c40fSThomas Huth {
47494d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info;
47504d62fcc0SQingFeng Hao struct sie_page *sie_page;
47514d62fcc0SQingFeng Hao
47522b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
47532b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode);
47542b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
47552b29a9fdSDominik Dingel
475627291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu))
475727291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu);
475827291e21SDavid Hildenbrand
47597ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
47607ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
476171f116bfSDavid Hildenbrand
47624d62fcc0SQingFeng Hao if (exit_reason == -EINTR) {
47634d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check");
47644d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block,
47654d62fcc0SQingFeng Hao struct sie_page, sie_block);
47664d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info;
47674d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info);
47684d62fcc0SQingFeng Hao return 0;
47694d62fcc0SQingFeng Hao }
47704d62fcc0SQingFeng Hao
477171f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) {
477271f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu);
477371f116bfSDavid Hildenbrand
477471f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP)
477571f116bfSDavid Hildenbrand return rc;
477671f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
477771f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
477871f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
477971f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
478071f116bfSDavid Hildenbrand return -EREMOTE;
478171f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) {
478271f116bfSDavid Hildenbrand vcpu->stat.exit_null++;
478371f116bfSDavid Hildenbrand return 0;
4784210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) {
4785210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
4786210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code =
4787210b1607SThomas Huth current->thread.gmap_addr;
4788210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10;
478971f116bfSDavid Hildenbrand return -EREMOTE;
479024eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) {
47913c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu);
479224eb3a82SDominik Dingel current->thread.gmap_pfault = 0;
479371f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu))
479471f116bfSDavid Hildenbrand return 0;
479550a05be4SChristian Borntraeger vcpu->stat.pfault_sync++;
479671f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
4797fa576c58SThomas Huth }
479871f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu);
47993fb4c40fSThomas Huth }
48003fb4c40fSThomas Huth
48013adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK)
__vcpu_run(struct kvm_vcpu * vcpu)48023fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
48033fb4c40fSThomas Huth {
48043fb4c40fSThomas Huth int rc, exit_reason;
4805c8aac234SJanosch Frank struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block;
48063fb4c40fSThomas Huth
4807800c1065SThomas Huth /*
4808800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run-
4809800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected
4810800c1065SThomas Huth */
48112031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu);
4812800c1065SThomas Huth
4813a76ccff6SThomas Huth do {
48143fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu);
481516631c42SIlya Leoshkevich if (rc || guestdbg_exit_pending(vcpu))
4816a76ccff6SThomas Huth break;
48173fb4c40fSThomas Huth
48182031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu);
48193fb4c40fSThomas Huth /*
4820a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between
4821a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess.
48223fb4c40fSThomas Huth */
48230097d12eSChristian Borntraeger local_irq_disable();
48246edaa530SPaolo Bonzini guest_enter_irqoff();
4825db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu);
48260097d12eSChristian Borntraeger local_irq_enable();
4827c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4828c8aac234SJanosch Frank memcpy(sie_page->pv_grregs,
4829c8aac234SJanosch Frank vcpu->run->s.regs.gprs,
4830c8aac234SJanosch Frank sizeof(sie_page->pv_grregs));
4831c8aac234SJanosch Frank }
483256e62a73SSven Schnelle if (test_cpu_flag(CIF_FPU))
483356e62a73SSven Schnelle load_fpu_regs();
4834a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block,
4835a76ccff6SThomas Huth vcpu->run->s.regs.gprs);
4836c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4837c8aac234SJanosch Frank memcpy(vcpu->run->s.regs.gprs,
4838c8aac234SJanosch Frank sie_page->pv_grregs,
4839c8aac234SJanosch Frank sizeof(sie_page->pv_grregs));
48403adae0b4SJanosch Frank /*
48413adae0b4SJanosch Frank * We're not allowed to inject interrupts on intercepts
48423adae0b4SJanosch Frank * that leave the guest state in an "in-between" state
48433adae0b4SJanosch Frank * where the next SIE entry will do a continuation.
48443adae0b4SJanosch Frank * Fence interrupts in our "internal" PSW.
48453adae0b4SJanosch Frank */
48463adae0b4SJanosch Frank if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR ||
48473adae0b4SJanosch Frank vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) {
48483adae0b4SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
48493adae0b4SJanosch Frank }
4850c8aac234SJanosch Frank }
48510097d12eSChristian Borntraeger local_irq_disable();
4852db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu);
48536edaa530SPaolo Bonzini guest_exit_irqoff();
48540097d12eSChristian Borntraeger local_irq_enable();
48552031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu);
48563fb4c40fSThomas Huth
48573fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason);
485827291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
48593fb4c40fSThomas Huth
48602031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu);
4861e168bf8dSCarsten Otte return rc;
4862b0c632dbSHeiko Carstens }
4863b0c632dbSHeiko Carstens
sync_regs_fmt2(struct kvm_vcpu * vcpu)48642f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu)
4865b028ee3eSDavid Hildenbrand {
48662f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run;
48674d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb;
48684e0b1ab7SFan Zhang struct gs_cb *gscb;
48694d5f2c04SChristian Borntraeger
48704d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
48714e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
4872b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
4873b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
4874b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4875b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
4876b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
4877b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
4878b028ee3eSDavid Hildenbrand }
4879b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
4880b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft;
4881b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
4882b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
48839fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
48849fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu);
4885b028ee3eSDavid Hildenbrand }
488623a60f83SCollin Walling if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) {
488723a60f83SCollin Walling vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318;
488823a60f83SCollin Walling vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc;
48893fd8417fSCollin Walling VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc);
489023a60f83SCollin Walling }
489180cd8763SFan Zhang /*
489280cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state,
489380cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement.
489480cd8763SFan Zhang */
489580cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
48964d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) &&
4897bb59c2daSAlice Frosi riccb->v &&
48980c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
48994d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
49000c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI;
490180cd8763SFan Zhang }
49024e0b1ab7SFan Zhang /*
49034e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero,
49044e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement.
49054e0b1ab7SFan Zhang */
49064e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
49074e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) &&
49084e0b1ab7SFan Zhang gscb->gssm &&
49094e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) {
49104e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
49114e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS;
49124e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
49134e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1;
491480cd8763SFan Zhang }
491535b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
491635b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) {
491735b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
491835b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
491935b3fde6SChristian Borntraeger }
49204e0b1ab7SFan Zhang if (MACHINE_HAS_GS) {
49214e0b1ab7SFan Zhang preempt_disable();
49224e0b1ab7SFan Zhang __ctl_set_bit(2, 4);
49234e0b1ab7SFan Zhang if (current->thread.gs_cb) {
49244e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb;
49254e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb);
49264e0b1ab7SFan Zhang }
49274e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) {
49284e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *)
49294e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb;
49304e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb);
49314e0b1ab7SFan Zhang }
49324e0b1ab7SFan Zhang preempt_enable();
49334e0b1ab7SFan Zhang }
4934a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */
4935811ea797SJanosch Frank }
4936811ea797SJanosch Frank
sync_regs(struct kvm_vcpu * vcpu)49372f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu)
4938811ea797SJanosch Frank {
49392f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run;
49402f0a83beSTianjia Zhang
4941811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
4942811ea797SJanosch Frank kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
4943811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
4944811ea797SJanosch Frank memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
4945811ea797SJanosch Frank /* some control register changes require a tlb flush */
4946811ea797SJanosch Frank kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4947811ea797SJanosch Frank }
4948811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4949811ea797SJanosch Frank kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
4950811ea797SJanosch Frank vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
4951811ea797SJanosch Frank }
4952811ea797SJanosch Frank save_access_regs(vcpu->arch.host_acrs);
4953811ea797SJanosch Frank restore_access_regs(vcpu->run->s.regs.acrs);
4954811ea797SJanosch Frank /* save host (userspace) fprs/vrs */
4955811ea797SJanosch Frank save_fpu_regs();
4956811ea797SJanosch Frank vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
4957811ea797SJanosch Frank vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
4958811ea797SJanosch Frank if (MACHINE_HAS_VX)
4959811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.vrs;
4960811ea797SJanosch Frank else
4961811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.fprs;
4962811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
4963811ea797SJanosch Frank if (test_fp_ctl(current->thread.fpu.fpc))
4964811ea797SJanosch Frank /* User space provided an invalid FPC, let's clear it */
4965811ea797SJanosch Frank current->thread.fpu.fpc = 0;
4966811ea797SJanosch Frank
4967811ea797SJanosch Frank /* Sync fmt2 only data */
4968811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) {
49692f0a83beSTianjia Zhang sync_regs_fmt2(vcpu);
4970811ea797SJanosch Frank } else {
4971811ea797SJanosch Frank /*
4972811ea797SJanosch Frank * In several places we have to modify our internal view to
4973811ea797SJanosch Frank * not do things that are disallowed by the ultravisor. For
4974811ea797SJanosch Frank * example we must not inject interrupts after specific exits
4975811ea797SJanosch Frank * (e.g. 112 prefix page not secure). We do this by turning
4976811ea797SJanosch Frank * off the machine check, external and I/O interrupt bits
4977811ea797SJanosch Frank * of our PSW copy. To avoid getting validity intercepts, we
4978811ea797SJanosch Frank * do only accept the condition code from userspace.
4979811ea797SJanosch Frank */
4980811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC;
4981811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask &
4982811ea797SJanosch Frank PSW_MASK_CC;
4983811ea797SJanosch Frank }
498480cd8763SFan Zhang
4985b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0;
4986b028ee3eSDavid Hildenbrand }
4987b028ee3eSDavid Hildenbrand
store_regs_fmt2(struct kvm_vcpu * vcpu)49882f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu)
4989b028ee3eSDavid Hildenbrand {
49902f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run;
49912f0a83beSTianjia Zhang
4992b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
4993b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
4994b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
499535b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
499623a60f83SCollin Walling kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val;
49974e0b1ab7SFan Zhang if (MACHINE_HAS_GS) {
499844bada28SHeiko Carstens preempt_disable();
49994e0b1ab7SFan Zhang __ctl_set_bit(2, 4);
50004e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled)
50014e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb);
50024e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb;
50034e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb);
50044e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb)
50054e0b1ab7SFan Zhang __ctl_clear_bit(2, 4);
50064e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL;
500744bada28SHeiko Carstens preempt_enable();
50084e0b1ab7SFan Zhang }
5009a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */
5010b028ee3eSDavid Hildenbrand }
5011b028ee3eSDavid Hildenbrand
store_regs(struct kvm_vcpu * vcpu)50122f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu)
5013811ea797SJanosch Frank {
50142f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run;
50152f0a83beSTianjia Zhang
5016811ea797SJanosch Frank kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
5017811ea797SJanosch Frank kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
5018811ea797SJanosch Frank kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
5019811ea797SJanosch Frank memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
5020811ea797SJanosch Frank kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
5021811ea797SJanosch Frank kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
5022811ea797SJanosch Frank kvm_run->s.regs.pft = vcpu->arch.pfault_token;
5023811ea797SJanosch Frank kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
5024811ea797SJanosch Frank kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
5025811ea797SJanosch Frank save_access_regs(vcpu->run->s.regs.acrs);
5026811ea797SJanosch Frank restore_access_regs(vcpu->arch.host_acrs);
5027811ea797SJanosch Frank /* Save guest register state */
5028811ea797SJanosch Frank save_fpu_regs();
5029811ea797SJanosch Frank vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
5030811ea797SJanosch Frank /* Restore will be done lazily at return */
5031811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
5032811ea797SJanosch Frank current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
5033811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu)))
50342f0a83beSTianjia Zhang store_regs_fmt2(vcpu);
5035811ea797SJanosch Frank }
5036811ea797SJanosch Frank
kvm_arch_vcpu_ioctl_run(struct kvm_vcpu * vcpu)50371b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
5038b0c632dbSHeiko Carstens {
50391b94f6f8STianjia Zhang struct kvm_run *kvm_run = vcpu->run;
50408f2abe6aSChristian Borntraeger int rc;
5041b0c632dbSHeiko Carstens
50420460eb35SJanosch Frank /*
50430460eb35SJanosch Frank * Running a VM while dumping always has the potential to
50440460eb35SJanosch Frank * produce inconsistent dump data. But for PV vcpus a SIE
50450460eb35SJanosch Frank * entry while dumping could also lead to a fatal validity
50460460eb35SJanosch Frank * intercept which we absolutely want to avoid.
50470460eb35SJanosch Frank */
50480460eb35SJanosch Frank if (vcpu->kvm->arch.pv.dumping)
50490460eb35SJanosch Frank return -EINVAL;
50500460eb35SJanosch Frank
5051460df4c1SPaolo Bonzini if (kvm_run->immediate_exit)
5052460df4c1SPaolo Bonzini return -EINTR;
5053460df4c1SPaolo Bonzini
5054200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
5055200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
5056200824f5SThomas Huth return -EINVAL;
5057200824f5SThomas Huth
5058accb757dSChristoffer Dall vcpu_load(vcpu);
5059accb757dSChristoffer Dall
506027291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) {
506127291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu);
5062accb757dSChristoffer Dall rc = 0;
5063accb757dSChristoffer Dall goto out;
506427291e21SDavid Hildenbrand }
506527291e21SDavid Hildenbrand
506620b7035cSJan H. Schönherr kvm_sigset_activate(vcpu);
5067b0c632dbSHeiko Carstens
5068fe28c786SJanosch Frank /*
5069fe28c786SJanosch Frank * no need to check the return value of vcpu_start as it can only have
5070fe28c786SJanosch Frank * an error for protvirt, but protvirt means user cpu state
5071fe28c786SJanosch Frank */
50726352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
50736852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu);
50746352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) {
5075ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n",
50766352e4d2SDavid Hildenbrand vcpu->vcpu_id);
5077accb757dSChristoffer Dall rc = -EINVAL;
5078accb757dSChristoffer Dall goto out;
50796352e4d2SDavid Hildenbrand }
5080b0c632dbSHeiko Carstens
50812f0a83beSTianjia Zhang sync_regs(vcpu);
5082db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu);
5083d7b0b5ebSCarsten Otte
5084dab4079dSHeiko Carstens might_fault();
5085e168bf8dSCarsten Otte rc = __vcpu_run(vcpu);
50869ace903dSChristian Ehrhardt
5087b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) {
5088b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR;
50898f2abe6aSChristian Borntraeger rc = -EINTR;
5090b1d16c49SChristian Ehrhardt }
50918f2abe6aSChristian Borntraeger
509227291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) {
509327291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu);
509427291e21SDavid Hildenbrand rc = 0;
509527291e21SDavid Hildenbrand }
509627291e21SDavid Hildenbrand
50978f2abe6aSChristian Borntraeger if (rc == -EREMOTE) {
509871f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */
50998f2abe6aSChristian Borntraeger rc = 0;
51008f2abe6aSChristian Borntraeger }
51018f2abe6aSChristian Borntraeger
5102db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu);
51032f0a83beSTianjia Zhang store_regs(vcpu);
5104d7b0b5ebSCarsten Otte
510520b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu);
5106b0c632dbSHeiko Carstens
5107b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++;
5108accb757dSChristoffer Dall out:
5109accb757dSChristoffer Dall vcpu_put(vcpu);
51107e8e6ab4SHeiko Carstens return rc;
5111b0c632dbSHeiko Carstens }
5112b0c632dbSHeiko Carstens
5113b0c632dbSHeiko Carstens /*
5114b0c632dbSHeiko Carstens * store status at address
5115b0c632dbSHeiko Carstens * we use have two special cases:
5116b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
5117b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
5118b0c632dbSHeiko Carstens */
kvm_s390_store_status_unloaded(struct kvm_vcpu * vcpu,unsigned long gpa)5119d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
5120b0c632dbSHeiko Carstens {
5121092670cdSCarsten Otte unsigned char archmode = 1;
51229abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS];
5123fda902cbSMichael Mueller unsigned int px;
51244287f247SDavid Hildenbrand u64 clkcomp, cputm;
5125d0bce605SHeiko Carstens int rc;
5126b0c632dbSHeiko Carstens
5127d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu);
5128d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
5129d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1))
5130b0c632dbSHeiko Carstens return -EFAULT;
5131d9a3a09aSMartin Schwidefsky gpa = 0;
5132d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
5133d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1))
5134b0c632dbSHeiko Carstens return -EFAULT;
5135d9a3a09aSMartin Schwidefsky gpa = px;
5136d9a3a09aSMartin Schwidefsky } else
5137d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA;
51389abc2a08SDavid Hildenbrand
51399abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */
51409abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) {
51419522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
5142d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
51439abc2a08SDavid Hildenbrand fprs, 128);
51449abc2a08SDavid Hildenbrand } else {
51459abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
51466fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128);
51479abc2a08SDavid Hildenbrand }
5148d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
5149d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128);
5150d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
5151d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16);
5152d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
5153fda902cbSMichael Mueller &px, 4);
5154d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
51559abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4);
5156d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
5157d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4);
51584287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu);
5159d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
51604287f247SDavid Hildenbrand &cputm, 8);
5161178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8;
5162d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
5163d0bce605SHeiko Carstens &clkcomp, 8);
5164d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
5165d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64);
5166d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
5167d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128);
5168d0bce605SHeiko Carstens return rc ? -EFAULT : 0;
5169b0c632dbSHeiko Carstens }
5170b0c632dbSHeiko Carstens
kvm_s390_vcpu_store_status(struct kvm_vcpu * vcpu,unsigned long addr)5171e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
5172e879892cSThomas Huth {
5173e879892cSThomas Huth /*
5174e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
517531d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save
5176e879892cSThomas Huth * it into the save area
5177e879892cSThomas Huth */
5178d0164ee2SHendrik Brueckner save_fpu_regs();
51799abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
5180e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs);
5181e879892cSThomas Huth
5182e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr);
5183e879892cSThomas Huth }
5184e879892cSThomas Huth
__disable_ibs_on_vcpu(struct kvm_vcpu * vcpu)51858ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
51868ad35755SDavid Hildenbrand {
51878ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
51888e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
51898ad35755SDavid Hildenbrand }
51908ad35755SDavid Hildenbrand
__disable_ibs_on_all_vcpus(struct kvm * kvm)51918ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
51928ad35755SDavid Hildenbrand {
519346808a4cSMarc Zyngier unsigned long i;
51948ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu;
51958ad35755SDavid Hildenbrand
51968ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) {
51978ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu);
51988ad35755SDavid Hildenbrand }
51998ad35755SDavid Hildenbrand }
52008ad35755SDavid Hildenbrand
__enable_ibs_on_vcpu(struct kvm_vcpu * vcpu)52018ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
52028ad35755SDavid Hildenbrand {
520309a400e7SDavid Hildenbrand if (!sclp.has_ibs)
520409a400e7SDavid Hildenbrand return;
52058ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
52068e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
52078ad35755SDavid Hildenbrand }
52088ad35755SDavid Hildenbrand
kvm_s390_vcpu_start(struct kvm_vcpu * vcpu)5209fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
52106852d7b6SDavid Hildenbrand {
5211fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0;
52128ad35755SDavid Hildenbrand
52138ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu))
5214fe28c786SJanosch Frank return 0;
52158ad35755SDavid Hildenbrand
52166852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
52178ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */
5218433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock);
52198ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
52208ad35755SDavid Hildenbrand
5221fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the operating state */
5222fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5223fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR);
5224fe28c786SJanosch Frank if (r) {
5225fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5226fe28c786SJanosch Frank return r;
5227fe28c786SJanosch Frank }
5228fe28c786SJanosch Frank }
5229fe28c786SJanosch Frank
52308ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) {
5231113d10bcSMarc Zyngier if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i)))
52328ad35755SDavid Hildenbrand started_vcpus++;
52338ad35755SDavid Hildenbrand }
52348ad35755SDavid Hildenbrand
52358ad35755SDavid Hildenbrand if (started_vcpus == 0) {
52368ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */
52378ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu);
52388ad35755SDavid Hildenbrand } else if (started_vcpus == 1) {
52398ad35755SDavid Hildenbrand /*
52408ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable
52418ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially
524238860756SBhaskar Chowdhury * outstanding ENABLE requests.
52438ad35755SDavid Hildenbrand */
52448ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm);
52458ad35755SDavid Hildenbrand }
52468ad35755SDavid Hildenbrand
52479daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
52488ad35755SDavid Hildenbrand /*
524972f21820SChristian Borntraeger * The real PSW might have changed due to a RESTART interpreted by the
525072f21820SChristian Borntraeger * ultravisor. We block all interrupts and let the next sie exit
525172f21820SChristian Borntraeger * refresh our view.
525272f21820SChristian Borntraeger */
525372f21820SChristian Borntraeger if (kvm_s390_pv_cpu_is_protected(vcpu))
525472f21820SChristian Borntraeger vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
525572f21820SChristian Borntraeger /*
52568ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline.
52578ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup.
52588ad35755SDavid Hildenbrand */
5259d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
5260433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5261fe28c786SJanosch Frank return 0;
52626852d7b6SDavid Hildenbrand }
52636852d7b6SDavid Hildenbrand
kvm_s390_vcpu_stop(struct kvm_vcpu * vcpu)5264fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
52656852d7b6SDavid Hildenbrand {
5266fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0;
52678ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL;
52688ad35755SDavid Hildenbrand
52698ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu))
5270fe28c786SJanosch Frank return 0;
52718ad35755SDavid Hildenbrand
52726852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
52738ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */
5274433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock);
52758ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
52768ad35755SDavid Hildenbrand
5277fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the stopped state */
5278fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5279fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP);
5280fe28c786SJanosch Frank if (r) {
5281fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5282fe28c786SJanosch Frank return r;
5283fe28c786SJanosch Frank }
5284fe28c786SJanosch Frank }
5285fe28c786SJanosch Frank
5286812de046SEric Farman /*
5287812de046SEric Farman * Set the VCPU to STOPPED and THEN clear the interrupt flag,
5288812de046SEric Farman * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders
5289812de046SEric Farman * have been fully processed. This will ensure that the VCPU
5290812de046SEric Farman * is kept BUSY if another VCPU is inquiring with SIGP SENSE.
5291812de046SEric Farman */
5292812de046SEric Farman kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
52936cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu);
529432f5ff63SDavid Hildenbrand
52958ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu);
52968ad35755SDavid Hildenbrand
52978ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) {
5298113d10bcSMarc Zyngier struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i);
5299113d10bcSMarc Zyngier
5300113d10bcSMarc Zyngier if (!is_vcpu_stopped(tmp)) {
53018ad35755SDavid Hildenbrand started_vcpus++;
5302113d10bcSMarc Zyngier started_vcpu = tmp;
53038ad35755SDavid Hildenbrand }
53048ad35755SDavid Hildenbrand }
53058ad35755SDavid Hildenbrand
53068ad35755SDavid Hildenbrand if (started_vcpus == 1) {
53078ad35755SDavid Hildenbrand /*
53088ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the
53098ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up.
53108ad35755SDavid Hildenbrand */
53118ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu);
53128ad35755SDavid Hildenbrand }
53138ad35755SDavid Hildenbrand
5314433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5315fe28c786SJanosch Frank return 0;
53166852d7b6SDavid Hildenbrand }
53176852d7b6SDavid Hildenbrand
kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu * vcpu,struct kvm_enable_cap * cap)5318d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
5319d6712df9SCornelia Huck struct kvm_enable_cap *cap)
5320d6712df9SCornelia Huck {
5321d6712df9SCornelia Huck int r;
5322d6712df9SCornelia Huck
5323d6712df9SCornelia Huck if (cap->flags)
5324d6712df9SCornelia Huck return -EINVAL;
5325d6712df9SCornelia Huck
5326d6712df9SCornelia Huck switch (cap->cap) {
5327fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT:
5328fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) {
5329fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1;
5330c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
5331fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm);
5332fa6b7fe9SCornelia Huck }
5333fa6b7fe9SCornelia Huck r = 0;
5334fa6b7fe9SCornelia Huck break;
5335d6712df9SCornelia Huck default:
5336d6712df9SCornelia Huck r = -EINVAL;
5337d6712df9SCornelia Huck break;
5338d6712df9SCornelia Huck }
5339d6712df9SCornelia Huck return r;
5340d6712df9SCornelia Huck }
5341d6712df9SCornelia Huck
kvm_s390_vcpu_sida_op(struct kvm_vcpu * vcpu,struct kvm_s390_mem_op * mop)53420e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu,
534319e12277SJanosch Frank struct kvm_s390_mem_op *mop)
534419e12277SJanosch Frank {
534519e12277SJanosch Frank void __user *uaddr = (void __user *)mop->buf;
5346b99f4512SNico Boehr void *sida_addr;
534719e12277SJanosch Frank int r = 0;
534819e12277SJanosch Frank
534919e12277SJanosch Frank if (mop->flags || !mop->size)
535019e12277SJanosch Frank return -EINVAL;
535119e12277SJanosch Frank if (mop->size + mop->sida_offset < mop->size)
535219e12277SJanosch Frank return -EINVAL;
535319e12277SJanosch Frank if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block))
535419e12277SJanosch Frank return -E2BIG;
53552c212e1bSJanis Schoetterl-Glausch if (!kvm_s390_pv_cpu_is_protected(vcpu))
53562c212e1bSJanis Schoetterl-Glausch return -EINVAL;
535719e12277SJanosch Frank
5358b99f4512SNico Boehr sida_addr = (char *)sida_addr(vcpu->arch.sie_block) + mop->sida_offset;
5359b99f4512SNico Boehr
536019e12277SJanosch Frank switch (mop->op) {
536119e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ:
5362b99f4512SNico Boehr if (copy_to_user(uaddr, sida_addr, mop->size))
536319e12277SJanosch Frank r = -EFAULT;
536419e12277SJanosch Frank
536519e12277SJanosch Frank break;
536619e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE:
5367b99f4512SNico Boehr if (copy_from_user(sida_addr, uaddr, mop->size))
536819e12277SJanosch Frank r = -EFAULT;
536919e12277SJanosch Frank break;
537019e12277SJanosch Frank }
537119e12277SJanosch Frank return r;
537219e12277SJanosch Frank }
53730e1234c0SJanis Schoetterl-Glausch
kvm_s390_vcpu_mem_op(struct kvm_vcpu * vcpu,struct kvm_s390_mem_op * mop)53740e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu,
537541408c28SThomas Huth struct kvm_s390_mem_op *mop)
537641408c28SThomas Huth {
537741408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf;
5378701422b3SJanis Schoetterl-Glausch enum gacc_mode acc_mode;
537941408c28SThomas Huth void *tmpbuf = NULL;
5380a41f505eSJanis Schoetterl-Glausch int r;
538141408c28SThomas Huth
5382a41f505eSJanis Schoetterl-Glausch r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_INJECT_EXCEPTION |
5383a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_CHECK_ONLY |
5384a41f505eSJanis Schoetterl-Glausch KVM_S390_MEMOP_F_SKEY_PROTECTION);
5385a41f505eSJanis Schoetterl-Glausch if (r)
5386a41f505eSJanis Schoetterl-Glausch return r;
5387a41f505eSJanis Schoetterl-Glausch if (mop->ar >= NUM_ACRS)
538841408c28SThomas Huth return -EINVAL;
538919e12277SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu))
539019e12277SJanosch Frank return -EINVAL;
539141408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
539241408c28SThomas Huth tmpbuf = vmalloc(mop->size);
539341408c28SThomas Huth if (!tmpbuf)
539441408c28SThomas Huth return -ENOMEM;
539541408c28SThomas Huth }
539641408c28SThomas Huth
5397701422b3SJanis Schoetterl-Glausch acc_mode = mop->op == KVM_S390_MEMOP_LOGICAL_READ ? GACC_FETCH : GACC_STORE;
539841408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
5399e9e9feebSJanis Schoetterl-Glausch r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size,
5400701422b3SJanis Schoetterl-Glausch acc_mode, mop->key);
5401701422b3SJanis Schoetterl-Glausch goto out_inject;
540241408c28SThomas Huth }
5403701422b3SJanis Schoetterl-Glausch if (acc_mode == GACC_FETCH) {
5404e9e9feebSJanis Schoetterl-Glausch r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
5405e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key);
5406701422b3SJanis Schoetterl-Glausch if (r)
5407701422b3SJanis Schoetterl-Glausch goto out_inject;
5408701422b3SJanis Schoetterl-Glausch if (copy_to_user(uaddr, tmpbuf, mop->size)) {
540941408c28SThomas Huth r = -EFAULT;
5410701422b3SJanis Schoetterl-Glausch goto out_free;
541141408c28SThomas Huth }
5412701422b3SJanis Schoetterl-Glausch } else {
541341408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) {
541441408c28SThomas Huth r = -EFAULT;
5415701422b3SJanis Schoetterl-Glausch goto out_free;
541641408c28SThomas Huth }
5417e9e9feebSJanis Schoetterl-Glausch r = write_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
5418e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key);
541941408c28SThomas Huth }
542041408c28SThomas Huth
5421701422b3SJanis Schoetterl-Glausch out_inject:
542241408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
542341408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
542441408c28SThomas Huth
5425701422b3SJanis Schoetterl-Glausch out_free:
542641408c28SThomas Huth vfree(tmpbuf);
542741408c28SThomas Huth return r;
542841408c28SThomas Huth }
542941408c28SThomas Huth
kvm_s390_vcpu_memsida_op(struct kvm_vcpu * vcpu,struct kvm_s390_mem_op * mop)54300e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu,
543119e12277SJanosch Frank struct kvm_s390_mem_op *mop)
543219e12277SJanosch Frank {
543319e12277SJanosch Frank int r, srcu_idx;
543419e12277SJanosch Frank
543519e12277SJanosch Frank srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
543619e12277SJanosch Frank
543719e12277SJanosch Frank switch (mop->op) {
543819e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_READ:
543919e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_WRITE:
54400e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_mem_op(vcpu, mop);
544119e12277SJanosch Frank break;
544219e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ:
544319e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE:
544419e12277SJanosch Frank /* we are locked against sida going away by the vcpu->mutex */
54450e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_sida_op(vcpu, mop);
544619e12277SJanosch Frank break;
544719e12277SJanosch Frank default:
544819e12277SJanosch Frank r = -EINVAL;
544919e12277SJanosch Frank }
545019e12277SJanosch Frank
545119e12277SJanosch Frank srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
545219e12277SJanosch Frank return r;
545319e12277SJanosch Frank }
545419e12277SJanosch Frank
kvm_arch_vcpu_async_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)54555cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp,
5456b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg)
5457b0c632dbSHeiko Carstens {
5458b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data;
5459b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg;
54601ad1fa82SIlya Leoshkevich int rc;
5461b0c632dbSHeiko Carstens
546293736624SAvi Kivity switch (ioctl) {
546347b43c52SJens Freimann case KVM_S390_IRQ: {
546447b43c52SJens Freimann struct kvm_s390_irq s390irq;
546547b43c52SJens Freimann
546647b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
54679b062471SChristoffer Dall return -EFAULT;
54681ad1fa82SIlya Leoshkevich rc = kvm_s390_inject_vcpu(vcpu, &s390irq);
54691ad1fa82SIlya Leoshkevich break;
547047b43c52SJens Freimann }
547193736624SAvi Kivity case KVM_S390_INTERRUPT: {
5472ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int;
547353936b5bSThomas Huth struct kvm_s390_irq s390irq = {};
5474ba5c1e9bSCarsten Otte
5475ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int)))
54769b062471SChristoffer Dall return -EFAULT;
5477383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq))
5478383d0b05SJens Freimann return -EINVAL;
54791ad1fa82SIlya Leoshkevich rc = kvm_s390_inject_vcpu(vcpu, &s390irq);
54801ad1fa82SIlya Leoshkevich break;
5481ba5c1e9bSCarsten Otte }
54821ad1fa82SIlya Leoshkevich default:
54831ad1fa82SIlya Leoshkevich rc = -ENOIOCTLCMD;
54841ad1fa82SIlya Leoshkevich break;
54859b062471SChristoffer Dall }
54861ad1fa82SIlya Leoshkevich
54871ad1fa82SIlya Leoshkevich /*
54881ad1fa82SIlya Leoshkevich * To simplify single stepping of userspace-emulated instructions,
54891ad1fa82SIlya Leoshkevich * KVM_EXIT_S390_SIEIC exit sets KVM_GUESTDBG_EXIT_PENDING (see
54901ad1fa82SIlya Leoshkevich * should_handle_per_ifetch()). However, if userspace emulation injects
54911ad1fa82SIlya Leoshkevich * an interrupt, it needs to be cleared, so that KVM_EXIT_DEBUG happens
54921ad1fa82SIlya Leoshkevich * after (and not before) the interrupt delivery.
54931ad1fa82SIlya Leoshkevich */
54941ad1fa82SIlya Leoshkevich if (!rc)
54951ad1fa82SIlya Leoshkevich vcpu->guest_debug &= ~KVM_GUESTDBG_EXIT_PENDING;
54961ad1fa82SIlya Leoshkevich
54971ad1fa82SIlya Leoshkevich return rc;
54985cb0944cSPaolo Bonzini }
54995cb0944cSPaolo Bonzini
kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu * vcpu,struct kvm_pv_cmd * cmd)55008aba0958SJanosch Frank static int kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu *vcpu,
55018aba0958SJanosch Frank struct kvm_pv_cmd *cmd)
55028aba0958SJanosch Frank {
55038aba0958SJanosch Frank struct kvm_s390_pv_dmp dmp;
55048aba0958SJanosch Frank void *data;
55058aba0958SJanosch Frank int ret;
55068aba0958SJanosch Frank
55078aba0958SJanosch Frank /* Dump initialization is a prerequisite */
55088aba0958SJanosch Frank if (!vcpu->kvm->arch.pv.dumping)
55098aba0958SJanosch Frank return -EINVAL;
55108aba0958SJanosch Frank
55118aba0958SJanosch Frank if (copy_from_user(&dmp, (__u8 __user *)cmd->data, sizeof(dmp)))
55128aba0958SJanosch Frank return -EFAULT;
55138aba0958SJanosch Frank
55148aba0958SJanosch Frank /* We only handle this subcmd right now */
55158aba0958SJanosch Frank if (dmp.subcmd != KVM_PV_DUMP_CPU)
55168aba0958SJanosch Frank return -EINVAL;
55178aba0958SJanosch Frank
55188aba0958SJanosch Frank /* CPU dump length is the same as create cpu storage donation. */
55198aba0958SJanosch Frank if (dmp.buff_len != uv_info.guest_cpu_stor_len)
55208aba0958SJanosch Frank return -EINVAL;
55218aba0958SJanosch Frank
55228aba0958SJanosch Frank data = kvzalloc(uv_info.guest_cpu_stor_len, GFP_KERNEL);
55238aba0958SJanosch Frank if (!data)
55248aba0958SJanosch Frank return -ENOMEM;
55258aba0958SJanosch Frank
55268aba0958SJanosch Frank ret = kvm_s390_pv_dump_cpu(vcpu, data, &cmd->rc, &cmd->rrc);
55278aba0958SJanosch Frank
55288aba0958SJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT DUMP CPU %d rc %x rrc %x",
55298aba0958SJanosch Frank vcpu->vcpu_id, cmd->rc, cmd->rrc);
55308aba0958SJanosch Frank
55318aba0958SJanosch Frank if (ret)
55328aba0958SJanosch Frank ret = -EINVAL;
55338aba0958SJanosch Frank
55348aba0958SJanosch Frank /* On success copy over the dump data */
55358aba0958SJanosch Frank if (!ret && copy_to_user((__u8 __user *)dmp.buff_addr, data, uv_info.guest_cpu_stor_len))
55368aba0958SJanosch Frank ret = -EFAULT;
55378aba0958SJanosch Frank
55388aba0958SJanosch Frank kvfree(data);
55398aba0958SJanosch Frank return ret;
55408aba0958SJanosch Frank }
55418aba0958SJanosch Frank
kvm_arch_vcpu_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)55425cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp,
55435cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg)
55445cb0944cSPaolo Bonzini {
55455cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data;
55465cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg;
55475cb0944cSPaolo Bonzini int idx;
55485cb0944cSPaolo Bonzini long r;
55498a8378faSJanosch Frank u16 rc, rrc;
55509b062471SChristoffer Dall
55519b062471SChristoffer Dall vcpu_load(vcpu);
55529b062471SChristoffer Dall
55539b062471SChristoffer Dall switch (ioctl) {
5554b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS:
5555800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu);
555655680890SChristian Borntraeger r = kvm_s390_store_status_unloaded(vcpu, arg);
5557800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx);
5558bc923cc9SAvi Kivity break;
5559b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: {
5560b0c632dbSHeiko Carstens psw_t psw;
5561b0c632dbSHeiko Carstens
5562bc923cc9SAvi Kivity r = -EFAULT;
5563b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw)))
5564bc923cc9SAvi Kivity break;
5565bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
5566bc923cc9SAvi Kivity break;
5567b0c632dbSHeiko Carstens }
55687de3f142SJanosch Frank case KVM_S390_CLEAR_RESET:
55697de3f142SJanosch Frank r = 0;
55707de3f142SJanosch Frank kvm_arch_vcpu_ioctl_clear_reset(vcpu);
55718a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) {
55728a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
55738a8378faSJanosch Frank UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc);
55748a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x",
55758a8378faSJanosch Frank rc, rrc);
55768a8378faSJanosch Frank }
55777de3f142SJanosch Frank break;
5578b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET:
55797de3f142SJanosch Frank r = 0;
55807de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu);
55818a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) {
55828a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
55838a8378faSJanosch Frank UVC_CMD_CPU_RESET_INITIAL,
55848a8378faSJanosch Frank &rc, &rrc);
55858a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x",
55868a8378faSJanosch Frank rc, rrc);
55878a8378faSJanosch Frank }
55887de3f142SJanosch Frank break;
55897de3f142SJanosch Frank case KVM_S390_NORMAL_RESET:
55907de3f142SJanosch Frank r = 0;
55917de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu);
55928a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) {
55938a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
55948a8378faSJanosch Frank UVC_CMD_CPU_RESET, &rc, &rrc);
55958a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x",
55968a8378faSJanosch Frank rc, rrc);
55978a8378faSJanosch Frank }
5598bc923cc9SAvi Kivity break;
559914eebd91SCarsten Otte case KVM_SET_ONE_REG:
560014eebd91SCarsten Otte case KVM_GET_ONE_REG: {
560114eebd91SCarsten Otte struct kvm_one_reg reg;
560268cf7b1fSJanosch Frank r = -EINVAL;
560368cf7b1fSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu))
560468cf7b1fSJanosch Frank break;
560514eebd91SCarsten Otte r = -EFAULT;
560614eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg)))
560714eebd91SCarsten Otte break;
560814eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG)
560914eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®);
561014eebd91SCarsten Otte else
561114eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®);
561214eebd91SCarsten Otte break;
561314eebd91SCarsten Otte }
561427e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
561527e0393fSCarsten Otte case KVM_S390_UCAS_MAP: {
561627e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap;
561727e0393fSCarsten Otte
561827e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
561927e0393fSCarsten Otte r = -EFAULT;
562027e0393fSCarsten Otte break;
562127e0393fSCarsten Otte }
562227e0393fSCarsten Otte
562327e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) {
562427e0393fSCarsten Otte r = -EINVAL;
562527e0393fSCarsten Otte break;
562627e0393fSCarsten Otte }
562727e0393fSCarsten Otte
562827e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
562927e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length);
563027e0393fSCarsten Otte break;
563127e0393fSCarsten Otte }
563227e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: {
563327e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap;
563427e0393fSCarsten Otte
563527e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
563627e0393fSCarsten Otte r = -EFAULT;
563727e0393fSCarsten Otte break;
563827e0393fSCarsten Otte }
563927e0393fSCarsten Otte
564027e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) {
564127e0393fSCarsten Otte r = -EINVAL;
564227e0393fSCarsten Otte break;
564327e0393fSCarsten Otte }
564427e0393fSCarsten Otte
564527e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
564627e0393fSCarsten Otte ucasmap.length);
564727e0393fSCarsten Otte break;
564827e0393fSCarsten Otte }
564927e0393fSCarsten Otte #endif
5650ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: {
5651527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0);
5652ccc7910fSCarsten Otte break;
5653ccc7910fSCarsten Otte }
5654d6712df9SCornelia Huck case KVM_ENABLE_CAP:
5655d6712df9SCornelia Huck {
5656d6712df9SCornelia Huck struct kvm_enable_cap cap;
5657d6712df9SCornelia Huck r = -EFAULT;
5658d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap)))
5659d6712df9SCornelia Huck break;
5660d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
5661d6712df9SCornelia Huck break;
5662d6712df9SCornelia Huck }
566341408c28SThomas Huth case KVM_S390_MEM_OP: {
566441408c28SThomas Huth struct kvm_s390_mem_op mem_op;
566541408c28SThomas Huth
566641408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
56670e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op);
566841408c28SThomas Huth else
566941408c28SThomas Huth r = -EFAULT;
567041408c28SThomas Huth break;
567141408c28SThomas Huth }
5672816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: {
5673816c7667SJens Freimann struct kvm_s390_irq_state irq_state;
5674816c7667SJens Freimann
5675816c7667SJens Freimann r = -EFAULT;
5676816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
5677816c7667SJens Freimann break;
5678816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF ||
5679816c7667SJens Freimann irq_state.len == 0 ||
5680816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
5681816c7667SJens Freimann r = -EINVAL;
5682816c7667SJens Freimann break;
5683816c7667SJens Freimann }
5684bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */
5685816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu,
5686816c7667SJens Freimann (void __user *) irq_state.buf,
5687816c7667SJens Freimann irq_state.len);
5688816c7667SJens Freimann break;
5689816c7667SJens Freimann }
5690816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: {
5691816c7667SJens Freimann struct kvm_s390_irq_state irq_state;
5692816c7667SJens Freimann
5693816c7667SJens Freimann r = -EFAULT;
5694816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
5695816c7667SJens Freimann break;
5696816c7667SJens Freimann if (irq_state.len == 0) {
5697816c7667SJens Freimann r = -EINVAL;
5698816c7667SJens Freimann break;
5699816c7667SJens Freimann }
5700bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */
5701816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu,
5702816c7667SJens Freimann (__u8 __user *) irq_state.buf,
5703816c7667SJens Freimann irq_state.len);
5704816c7667SJens Freimann break;
5705816c7667SJens Freimann }
57068aba0958SJanosch Frank case KVM_S390_PV_CPU_COMMAND: {
57078aba0958SJanosch Frank struct kvm_pv_cmd cmd;
57088aba0958SJanosch Frank
57098aba0958SJanosch Frank r = -EINVAL;
57108aba0958SJanosch Frank if (!is_prot_virt_host())
57118aba0958SJanosch Frank break;
57128aba0958SJanosch Frank
57138aba0958SJanosch Frank r = -EFAULT;
57148aba0958SJanosch Frank if (copy_from_user(&cmd, argp, sizeof(cmd)))
57158aba0958SJanosch Frank break;
57168aba0958SJanosch Frank
57178aba0958SJanosch Frank r = -EINVAL;
57188aba0958SJanosch Frank if (cmd.flags)
57198aba0958SJanosch Frank break;
57208aba0958SJanosch Frank
57218aba0958SJanosch Frank /* We only handle this cmd right now */
57228aba0958SJanosch Frank if (cmd.cmd != KVM_PV_DUMP)
57238aba0958SJanosch Frank break;
57248aba0958SJanosch Frank
57258aba0958SJanosch Frank r = kvm_s390_handle_pv_vcpu_dump(vcpu, &cmd);
57268aba0958SJanosch Frank
57278aba0958SJanosch Frank /* Always copy over UV rc / rrc data */
57288aba0958SJanosch Frank if (copy_to_user((__u8 __user *)argp, &cmd.rc,
57298aba0958SJanosch Frank sizeof(cmd.rc) + sizeof(cmd.rrc)))
57308aba0958SJanosch Frank r = -EFAULT;
57318aba0958SJanosch Frank break;
57328aba0958SJanosch Frank }
5733b0c632dbSHeiko Carstens default:
57343e6afcf1SCarsten Otte r = -ENOTTY;
5735b0c632dbSHeiko Carstens }
57369b062471SChristoffer Dall
57379b062471SChristoffer Dall vcpu_put(vcpu);
5738bc923cc9SAvi Kivity return r;
5739b0c632dbSHeiko Carstens }
5740b0c632dbSHeiko Carstens
kvm_arch_vcpu_fault(struct kvm_vcpu * vcpu,struct vm_fault * vmf)57411499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
57425b1c1493SCarsten Otte {
57435b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
57445b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
57455b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) {
57465b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block);
57475b1c1493SCarsten Otte get_page(vmf->page);
57485b1c1493SCarsten Otte return 0;
57495b1c1493SCarsten Otte }
57505b1c1493SCarsten Otte #endif
57515b1c1493SCarsten Otte return VM_FAULT_SIGBUS;
57525b1c1493SCarsten Otte }
57535b1c1493SCarsten Otte
kvm_arch_irqchip_in_kernel(struct kvm * kvm)5754d663b8a2SPaolo Bonzini bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
5755d663b8a2SPaolo Bonzini {
5756d663b8a2SPaolo Bonzini return true;
5757d663b8a2SPaolo Bonzini }
5758d663b8a2SPaolo Bonzini
5759b0c632dbSHeiko Carstens /* Section: memory related */
kvm_arch_prepare_memory_region(struct kvm * kvm,const struct kvm_memory_slot * old,struct kvm_memory_slot * new,enum kvm_mr_change change)5760f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
5761537a17b3SSean Christopherson const struct kvm_memory_slot *old,
5762537a17b3SSean Christopherson struct kvm_memory_slot *new,
57637b6195a9STakuya Yoshikawa enum kvm_mr_change change)
5764b0c632dbSHeiko Carstens {
5765ec5c8697SSean Christopherson gpa_t size;
5766ec5c8697SSean Christopherson
5767ec5c8697SSean Christopherson /* When we are protected, we should not change the memory slots */
5768ec5c8697SSean Christopherson if (kvm_s390_pv_get_handle(kvm))
5769ec5c8697SSean Christopherson return -EINVAL;
5770ec5c8697SSean Christopherson
5771f2d3155eSNico Boehr if (change != KVM_MR_DELETE && change != KVM_MR_FLAGS_ONLY) {
5772f2d3155eSNico Boehr /*
5773f2d3155eSNico Boehr * A few sanity checks. We can have memory slots which have to be
5774f2d3155eSNico Boehr * located/ended at a segment boundary (1MB). The memory in userland is
5775f2d3155eSNico Boehr * ok to be fragmented into various different vmas. It is okay to mmap()
5776f2d3155eSNico Boehr * and munmap() stuff in this slot after doing this call at any time
5777f2d3155eSNico Boehr */
5778b0c632dbSHeiko Carstens
5779cf5b4869SSean Christopherson if (new->userspace_addr & 0xffffful)
5780b0c632dbSHeiko Carstens return -EINVAL;
5781b0c632dbSHeiko Carstens
5782ec5c8697SSean Christopherson size = new->npages * PAGE_SIZE;
5783cf5b4869SSean Christopherson if (size & 0xffffful)
5784b0c632dbSHeiko Carstens return -EINVAL;
5785b0c632dbSHeiko Carstens
5786cf5b4869SSean Christopherson if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit)
5787a3a92c31SDominik Dingel return -EINVAL;
5788f2d3155eSNico Boehr }
5789f2d3155eSNico Boehr
5790f2d3155eSNico Boehr if (!kvm->arch.migration_mode)
5791f2d3155eSNico Boehr return 0;
5792f2d3155eSNico Boehr
5793f2d3155eSNico Boehr /*
5794f2d3155eSNico Boehr * Turn off migration mode when:
5795f2d3155eSNico Boehr * - userspace creates a new memslot with dirty logging off,
5796f2d3155eSNico Boehr * - userspace modifies an existing memslot (MOVE or FLAGS_ONLY) and
5797f2d3155eSNico Boehr * dirty logging is turned off.
5798f2d3155eSNico Boehr * Migration mode expects dirty page logging being enabled to store
5799f2d3155eSNico Boehr * its dirty bitmap.
5800f2d3155eSNico Boehr */
5801f2d3155eSNico Boehr if (change != KVM_MR_DELETE &&
5802f2d3155eSNico Boehr !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
5803f2d3155eSNico Boehr WARN(kvm_s390_vm_stop_migration(kvm),
5804f2d3155eSNico Boehr "Failed to stop migration mode");
5805a3a92c31SDominik Dingel
5806f7784b8eSMarcelo Tosatti return 0;
5807f7784b8eSMarcelo Tosatti }
5808f7784b8eSMarcelo Tosatti
kvm_arch_commit_memory_region(struct kvm * kvm,struct kvm_memory_slot * old,const struct kvm_memory_slot * new,enum kvm_mr_change change)5809f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
58109d4c197cSSean Christopherson struct kvm_memory_slot *old,
5811f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new,
58128482644aSTakuya Yoshikawa enum kvm_mr_change change)
5813f7784b8eSMarcelo Tosatti {
581419ec166cSChristian Borntraeger int rc = 0;
5815f7784b8eSMarcelo Tosatti
581619ec166cSChristian Borntraeger switch (change) {
581719ec166cSChristian Borntraeger case KVM_MR_DELETE:
581819ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
581919ec166cSChristian Borntraeger old->npages * PAGE_SIZE);
582019ec166cSChristian Borntraeger break;
582119ec166cSChristian Borntraeger case KVM_MR_MOVE:
582219ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
582319ec166cSChristian Borntraeger old->npages * PAGE_SIZE);
582419ec166cSChristian Borntraeger if (rc)
582519ec166cSChristian Borntraeger break;
58263b684a42SJoe Perches fallthrough;
582719ec166cSChristian Borntraeger case KVM_MR_CREATE:
5828cf5b4869SSean Christopherson rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr,
5829cf5b4869SSean Christopherson new->base_gfn * PAGE_SIZE,
5830cf5b4869SSean Christopherson new->npages * PAGE_SIZE);
583119ec166cSChristian Borntraeger break;
583219ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY:
583319ec166cSChristian Borntraeger break;
583419ec166cSChristian Borntraeger default:
583519ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
583619ec166cSChristian Borntraeger }
5837598841caSCarsten Otte if (rc)
5838ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n");
5839598841caSCarsten Otte return;
5840b0c632dbSHeiko Carstens }
5841b0c632dbSHeiko Carstens
nonhyp_mask(int i)584260a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
584360a37709SAlexander Yarygin {
584460a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
584560a37709SAlexander Yarygin
584660a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
584760a37709SAlexander Yarygin }
584860a37709SAlexander Yarygin
kvm_s390_init(void)5849b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
5850b0c632dbSHeiko Carstens {
5851b8449265SSean Christopherson int i, r;
585260a37709SAlexander Yarygin
585307197fd0SDavid Hildenbrand if (!sclp.has_sief2) {
58548d43d570SMichael Mueller pr_info("SIE is not available\n");
585507197fd0SDavid Hildenbrand return -ENODEV;
585607197fd0SDavid Hildenbrand }
585707197fd0SDavid Hildenbrand
5858a4499382SJanosch Frank if (nested && hpage) {
58598d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
5860a4499382SJanosch Frank return -EINVAL;
5861a4499382SJanosch Frank }
5862a4499382SJanosch Frank
586360a37709SAlexander Yarygin for (i = 0; i < 16; i++)
5864c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |=
586517e89e13SSven Schnelle stfle_fac_list[i] & nonhyp_mask(i);
586660a37709SAlexander Yarygin
5867b8449265SSean Christopherson r = __kvm_s390_init();
5868b8449265SSean Christopherson if (r)
5869b8449265SSean Christopherson return r;
5870b8449265SSean Christopherson
587181a1cf9fSSean Christopherson r = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
5872b8449265SSean Christopherson if (r) {
5873b8449265SSean Christopherson __kvm_s390_exit();
5874b8449265SSean Christopherson return r;
5875b8449265SSean Christopherson }
5876b8449265SSean Christopherson return 0;
5877b0c632dbSHeiko Carstens }
5878b0c632dbSHeiko Carstens
kvm_s390_exit(void)5879b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
5880b0c632dbSHeiko Carstens {
5881b0c632dbSHeiko Carstens kvm_exit();
5882b8449265SSean Christopherson
5883b8449265SSean Christopherson __kvm_s390_exit();
5884b0c632dbSHeiko Carstens }
5885b0c632dbSHeiko Carstens
5886b0c632dbSHeiko Carstens module_init(kvm_s390_init);
5887b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
5888566af940SCornelia Huck
5889566af940SCornelia Huck /*
5890566af940SCornelia Huck * Enable autoloading of the kvm module.
5891566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c
5892566af940SCornelia Huck * since x86 takes a different approach.
5893566af940SCornelia Huck */
5894566af940SCornelia Huck #include <linux/miscdevice.h>
5895566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
5896566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
5897