Merge tag 'drm-next-2023-11-10' of git://anongit.freedesktop.org/drm/drm
[linux-block.git] / arch / s390 / kvm / kvm-s390.c
CommitLineData
d809aa23 1// SPDX-License-Identifier: GPL-2.0
b0c632db 2/*
bb64da9a 3 * hosting IBM Z kernel virtual machines (s390x)
b0c632db 4 *
3e6c5568 5 * Copyright IBM Corp. 2008, 2020
b0c632db
HC
6 *
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
628eb9b8 9 * Christian Ehrhardt <ehrhardt@de.ibm.com>
15f36ebd 10 * Jason J. Herne <jjherne@us.ibm.com>
b0c632db
HC
11 */
12
7aedd9d4
MM
13#define KMSG_COMPONENT "kvm-s390"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
b0c632db
HC
16#include <linux/compiler.h>
17#include <linux/err.h>
18#include <linux/fs.h>
ca872302 19#include <linux/hrtimer.h>
b0c632db
HC
20#include <linux/init.h>
21#include <linux/kvm.h>
22#include <linux/kvm_host.h>
b2d73b2a 23#include <linux/mman.h>
b0c632db 24#include <linux/module.h>
d3217967 25#include <linux/moduleparam.h>
a374e892 26#include <linux/random.h>
b0c632db 27#include <linux/slab.h>
ba5c1e9b 28#include <linux/timer.h>
41408c28 29#include <linux/vmalloc.h>
15c9705f 30#include <linux/bitmap.h>
174cd4b1 31#include <linux/sched/signal.h>
190df4a2 32#include <linux/string.h>
65fddcfc 33#include <linux/pgtable.h>
ca2fd060 34#include <linux/mmu_notifier.h>
174cd4b1 35
cbb870c8 36#include <asm/asm-offsets.h>
b0c632db 37#include <asm/lowcore.h>
fd5ada04 38#include <asm/stp.h>
1e133ab2 39#include <asm/gmap.h>
f5daba1d 40#include <asm/nmi.h>
a0616cde 41#include <asm/switch_to.h>
6d3da241 42#include <asm/isc.h>
1526bf9c 43#include <asm/sclp.h>
0a763c78 44#include <asm/cpacf.h>
221bb8a4 45#include <asm/timex.h>
e585b24a 46#include <asm/ap.h>
29b40f10 47#include <asm/uv.h>
56e62a73 48#include <asm/fpu/api.h>
8f2abe6a 49#include "kvm-s390.h"
b0c632db 50#include "gaccess.h"
98b1d33d 51#include "pci.h"
b0c632db 52
5786fffa
CH
53#define CREATE_TRACE_POINTS
54#include "trace.h"
ade38c31 55#include "trace-s390.h"
5786fffa 56
41408c28 57#define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
816c7667
JF
58#define LOCAL_IRQS 32
59#define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
60 (KVM_MAX_VCPUS + LOCAL_IRQS))
41408c28 61
fcfe1bae
JZ
62const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
63 KVM_GENERIC_VM_STATS(),
64 STATS_DESC_COUNTER(VM, inject_io),
65 STATS_DESC_COUNTER(VM, inject_float_mchk),
66 STATS_DESC_COUNTER(VM, inject_pfault_done),
67 STATS_DESC_COUNTER(VM, inject_service_signal),
73f91b00 68 STATS_DESC_COUNTER(VM, inject_virtio),
c3235e2d
NB
69 STATS_DESC_COUNTER(VM, aen_forward),
70 STATS_DESC_COUNTER(VM, gmap_shadow_reuse),
71 STATS_DESC_COUNTER(VM, gmap_shadow_create),
72 STATS_DESC_COUNTER(VM, gmap_shadow_r1_entry),
73 STATS_DESC_COUNTER(VM, gmap_shadow_r2_entry),
74 STATS_DESC_COUNTER(VM, gmap_shadow_r3_entry),
75 STATS_DESC_COUNTER(VM, gmap_shadow_sg_entry),
76 STATS_DESC_COUNTER(VM, gmap_shadow_pg_entry),
fcfe1bae 77};
fcfe1bae
JZ
78
79const struct kvm_stats_header kvm_vm_stats_header = {
80 .name_size = KVM_STATS_NAME_SIZE,
81 .num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
82 .id_offset = sizeof(struct kvm_stats_header),
83 .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
84 .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
85 sizeof(kvm_vm_stats_desc),
86};
87
ce55c049
JZ
88const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
89 KVM_GENERIC_VCPU_STATS(),
90 STATS_DESC_COUNTER(VCPU, exit_userspace),
91 STATS_DESC_COUNTER(VCPU, exit_null),
92 STATS_DESC_COUNTER(VCPU, exit_external_request),
93 STATS_DESC_COUNTER(VCPU, exit_io_request),
94 STATS_DESC_COUNTER(VCPU, exit_external_interrupt),
95 STATS_DESC_COUNTER(VCPU, exit_stop_request),
96 STATS_DESC_COUNTER(VCPU, exit_validity),
97 STATS_DESC_COUNTER(VCPU, exit_instruction),
98 STATS_DESC_COUNTER(VCPU, exit_pei),
99 STATS_DESC_COUNTER(VCPU, halt_no_poll_steal),
100 STATS_DESC_COUNTER(VCPU, instruction_lctl),
101 STATS_DESC_COUNTER(VCPU, instruction_lctlg),
102 STATS_DESC_COUNTER(VCPU, instruction_stctl),
103 STATS_DESC_COUNTER(VCPU, instruction_stctg),
104 STATS_DESC_COUNTER(VCPU, exit_program_interruption),
105 STATS_DESC_COUNTER(VCPU, exit_instr_and_program),
106 STATS_DESC_COUNTER(VCPU, exit_operation_exception),
107 STATS_DESC_COUNTER(VCPU, deliver_ckc),
108 STATS_DESC_COUNTER(VCPU, deliver_cputm),
109 STATS_DESC_COUNTER(VCPU, deliver_external_call),
110 STATS_DESC_COUNTER(VCPU, deliver_emergency_signal),
111 STATS_DESC_COUNTER(VCPU, deliver_service_signal),
112 STATS_DESC_COUNTER(VCPU, deliver_virtio),
113 STATS_DESC_COUNTER(VCPU, deliver_stop_signal),
114 STATS_DESC_COUNTER(VCPU, deliver_prefix_signal),
115 STATS_DESC_COUNTER(VCPU, deliver_restart_signal),
116 STATS_DESC_COUNTER(VCPU, deliver_program),
117 STATS_DESC_COUNTER(VCPU, deliver_io),
118 STATS_DESC_COUNTER(VCPU, deliver_machine_check),
119 STATS_DESC_COUNTER(VCPU, exit_wait_state),
120 STATS_DESC_COUNTER(VCPU, inject_ckc),
121 STATS_DESC_COUNTER(VCPU, inject_cputm),
122 STATS_DESC_COUNTER(VCPU, inject_external_call),
123 STATS_DESC_COUNTER(VCPU, inject_emergency_signal),
124 STATS_DESC_COUNTER(VCPU, inject_mchk),
125 STATS_DESC_COUNTER(VCPU, inject_pfault_init),
126 STATS_DESC_COUNTER(VCPU, inject_program),
127 STATS_DESC_COUNTER(VCPU, inject_restart),
128 STATS_DESC_COUNTER(VCPU, inject_set_prefix),
129 STATS_DESC_COUNTER(VCPU, inject_stop_signal),
130 STATS_DESC_COUNTER(VCPU, instruction_epsw),
131 STATS_DESC_COUNTER(VCPU, instruction_gs),
132 STATS_DESC_COUNTER(VCPU, instruction_io_other),
133 STATS_DESC_COUNTER(VCPU, instruction_lpsw),
134 STATS_DESC_COUNTER(VCPU, instruction_lpswe),
135 STATS_DESC_COUNTER(VCPU, instruction_pfmf),
136 STATS_DESC_COUNTER(VCPU, instruction_ptff),
137 STATS_DESC_COUNTER(VCPU, instruction_sck),
138 STATS_DESC_COUNTER(VCPU, instruction_sckpf),
139 STATS_DESC_COUNTER(VCPU, instruction_stidp),
140 STATS_DESC_COUNTER(VCPU, instruction_spx),
141 STATS_DESC_COUNTER(VCPU, instruction_stpx),
142 STATS_DESC_COUNTER(VCPU, instruction_stap),
143 STATS_DESC_COUNTER(VCPU, instruction_iske),
144 STATS_DESC_COUNTER(VCPU, instruction_ri),
145 STATS_DESC_COUNTER(VCPU, instruction_rrbe),
146 STATS_DESC_COUNTER(VCPU, instruction_sske),
147 STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock),
148 STATS_DESC_COUNTER(VCPU, instruction_stsi),
149 STATS_DESC_COUNTER(VCPU, instruction_stfl),
150 STATS_DESC_COUNTER(VCPU, instruction_tb),
151 STATS_DESC_COUNTER(VCPU, instruction_tpi),
152 STATS_DESC_COUNTER(VCPU, instruction_tprot),
153 STATS_DESC_COUNTER(VCPU, instruction_tsch),
154 STATS_DESC_COUNTER(VCPU, instruction_sie),
155 STATS_DESC_COUNTER(VCPU, instruction_essa),
156 STATS_DESC_COUNTER(VCPU, instruction_sthyi),
157 STATS_DESC_COUNTER(VCPU, instruction_sigp_sense),
158 STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running),
159 STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call),
160 STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency),
161 STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency),
162 STATS_DESC_COUNTER(VCPU, instruction_sigp_start),
163 STATS_DESC_COUNTER(VCPU, instruction_sigp_stop),
164 STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status),
165 STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status),
166 STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status),
167 STATS_DESC_COUNTER(VCPU, instruction_sigp_arch),
168 STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix),
169 STATS_DESC_COUNTER(VCPU, instruction_sigp_restart),
170 STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset),
171 STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset),
172 STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown),
bb000f64
CB
173 STATS_DESC_COUNTER(VCPU, instruction_diagnose_10),
174 STATS_DESC_COUNTER(VCPU, instruction_diagnose_44),
175 STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c),
176 STATS_DESC_COUNTER(VCPU, diag_9c_ignored),
177 STATS_DESC_COUNTER(VCPU, diag_9c_forward),
178 STATS_DESC_COUNTER(VCPU, instruction_diagnose_258),
179 STATS_DESC_COUNTER(VCPU, instruction_diagnose_308),
180 STATS_DESC_COUNTER(VCPU, instruction_diagnose_500),
181 STATS_DESC_COUNTER(VCPU, instruction_diagnose_other),
ce55c049
JZ
182 STATS_DESC_COUNTER(VCPU, pfault_sync)
183};
ce55c049
JZ
184
185const struct kvm_stats_header kvm_vcpu_stats_header = {
186 .name_size = KVM_STATS_NAME_SIZE,
187 .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
188 .id_offset = sizeof(struct kvm_stats_header),
189 .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
190 .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
191 sizeof(kvm_vcpu_stats_desc),
192};
193
a411edf1
DH
194/* allow nested virtualization in KVM (if enabled by user space) */
195static int nested;
196module_param(nested, int, S_IRUGO);
197MODULE_PARM_DESC(nested, "Nested virtualization support");
198
a4499382
JF
199/* allow 1m huge page guest backing, if !nested */
200static int hpage;
201module_param(hpage, int, 0444);
202MODULE_PARM_DESC(hpage, "1m huge page backing support");
b0c632db 203
8b905d28
CB
204/* maximum percentage of steal time for polling. >100 is treated like 100 */
205static u8 halt_poll_max_steal = 10;
206module_param(halt_poll_max_steal, byte, 0644);
b41fb528 207MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
8b905d28 208
cc674ef2
MM
209/* if set to true, the GISA will be initialized and used if available */
210static bool use_gisa = true;
211module_param(use_gisa, bool, 0644);
212MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it.");
213
87e28a15
PM
214/* maximum diag9c forwarding per second */
215unsigned int diag9c_forwarding_hz;
216module_param(diag9c_forwarding_hz, uint, 0644);
217MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off");
218
cc726886
CI
219/*
220 * allow asynchronous deinit for protected guests; enable by default since
221 * the feature is opt-in anyway
222 */
223static int async_destroy = 1;
224module_param(async_destroy, int, 0444);
225MODULE_PARM_DESC(async_destroy, "Asynchronous destroy for protected guests");
fb491d55 226
c3b9e3e1
CB
227/*
228 * For now we handle at most 16 double words as this is what the s390 base
229 * kernel handles and stores in the prefix page. If we ever need to go beyond
230 * this, this requires changes to code, but the external uapi can stay.
231 */
232#define SIZE_INTERNAL 16
233
234/*
235 * Base feature mask that defines default mask for facilities. Consists of the
236 * defines in FACILITIES_KVM and the non-hypervisor managed bits.
237 */
238static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
239/*
240 * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
241 * and defines the facilities that can be enabled via a cpu model.
242 */
243static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
244
245static unsigned long kvm_s390_fac_size(void)
78c4b59f 246{
c3b9e3e1
CB
247 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
248 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
249 BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
17e89e13 250 sizeof(stfle_fac_list));
c3b9e3e1
CB
251
252 return SIZE_INTERNAL;
78c4b59f
MM
253}
254
15c9705f
DH
255/* available cpu features supported by kvm */
256static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
0a763c78
DH
257/* available subfunctions indicated via query / "test bit" */
258static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
15c9705f 259
9d8d5786 260static struct gmap_notifier gmap_notifier;
a3508fbe 261static struct gmap_notifier vsie_gmap_notifier;
78f26131 262debug_info_t *kvm_s390_dbf;
3e6c5568 263debug_info_t *kvm_s390_dbf_uv;
9d8d5786 264
b0c632db 265/* Section: not file related */
29b40f10 266/* forward declarations */
414d3b07
MS
267static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
268 unsigned long end);
29b40f10 269static int sca_switch_to_extended(struct kvm *kvm);
2c70fe44 270
1575767e
DH
271static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
272{
273 u8 delta_idx = 0;
274
275 /*
276 * The TOD jumps by delta, we have to compensate this by adding
277 * -delta to the epoch.
278 */
279 delta = -delta;
280
281 /* sign-extension - we're adding to signed values below */
282 if ((s64)delta < 0)
283 delta_idx = -1;
284
285 scb->epoch += delta;
286 if (scb->ecd & ECD_MEF) {
287 scb->epdx += delta_idx;
288 if (scb->epoch < delta)
289 scb->epdx += 1;
290 }
291}
292
fdf03650
FZ
293/*
294 * This callback is executed during stop_machine(). All CPUs are therefore
295 * temporarily stopped. In order not to change guest behavior, we have to
296 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
297 * so a CPU won't be stopped while calculating with the epoch.
298 */
299static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
300 void *v)
301{
302 struct kvm *kvm;
303 struct kvm_vcpu *vcpu;
46808a4c 304 unsigned long i;
fdf03650
FZ
305 unsigned long long *delta = v;
306
307 list_for_each_entry(kvm, &vm_list, vm_list) {
fdf03650 308 kvm_for_each_vcpu(i, vcpu, kvm) {
1575767e
DH
309 kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
310 if (i == 0) {
311 kvm->arch.epoch = vcpu->arch.sie_block->epoch;
312 kvm->arch.epdx = vcpu->arch.sie_block->epdx;
313 }
db0758b2
DH
314 if (vcpu->arch.cputm_enabled)
315 vcpu->arch.cputm_start += *delta;
91473b48 316 if (vcpu->arch.vsie_block)
1575767e
DH
317 kvm_clock_sync_scb(vcpu->arch.vsie_block,
318 *delta);
fdf03650
FZ
319 }
320 }
321 return NOTIFY_OK;
322}
323
324static struct notifier_block kvm_clock_notifier = {
325 .notifier_call = kvm_clock_sync,
326};
327
22be5a13
DH
328static void allow_cpu_feat(unsigned long nr)
329{
330 set_bit_inv(nr, kvm_s390_available_cpu_feat);
331}
332
0a763c78
DH
333static inline int plo_test_bit(unsigned char nr)
334{
4fa3b91b 335 unsigned long function = (unsigned long)nr | 0x100;
d051ae53 336 int cc;
0a763c78
DH
337
338 asm volatile(
4fa3b91b 339 " lgr 0,%[function]\n"
0a763c78
DH
340 /* Parameter registers are ignored for "test bit" */
341 " plo 0,0,0,0(0)\n"
342 " ipm %0\n"
343 " srl %0,28\n"
344 : "=d" (cc)
4fa3b91b
HC
345 : [function] "d" (function)
346 : "cc", "0");
0a763c78
DH
347 return cc == 0;
348}
349
d0dea733 350static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
d6681397 351{
d6681397 352 asm volatile(
4fa3b91b
HC
353 " lghi 0,0\n"
354 " lgr 1,%[query]\n"
355 /* Parameter registers are ignored */
d6681397 356 " .insn rrf,%[opc] << 16,2,4,6,0\n"
b1c41ac3 357 :
4fa3b91b
HC
358 : [query] "d" ((unsigned long)query), [opc] "i" (opcode)
359 : "cc", "memory", "0", "1");
d6681397
CB
360}
361
173aec2d 362#define INSN_SORTL 0xb938
4f45b90e 363#define INSN_DFLTCC 0xb939
173aec2d 364
6c30cd2e 365static void __init kvm_s390_cpu_feat_init(void)
22be5a13 366{
0a763c78
DH
367 int i;
368
369 for (i = 0; i < 256; ++i) {
370 if (plo_test_bit(i))
371 kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
372 }
373
374 if (test_facility(28)) /* TOD-clock steering */
221bb8a4
LT
375 ptff(kvm_s390_available_subfunc.ptff,
376 sizeof(kvm_s390_available_subfunc.ptff),
377 PTFF_QAF);
0a763c78
DH
378
379 if (test_facility(17)) { /* MSA */
69c0e360
MS
380 __cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
381 kvm_s390_available_subfunc.kmac);
382 __cpacf_query(CPACF_KMC, (cpacf_mask_t *)
383 kvm_s390_available_subfunc.kmc);
384 __cpacf_query(CPACF_KM, (cpacf_mask_t *)
385 kvm_s390_available_subfunc.km);
386 __cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
387 kvm_s390_available_subfunc.kimd);
388 __cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
389 kvm_s390_available_subfunc.klmd);
0a763c78
DH
390 }
391 if (test_facility(76)) /* MSA3 */
69c0e360
MS
392 __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
393 kvm_s390_available_subfunc.pckmo);
0a763c78 394 if (test_facility(77)) { /* MSA4 */
69c0e360
MS
395 __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
396 kvm_s390_available_subfunc.kmctr);
397 __cpacf_query(CPACF_KMF, (cpacf_mask_t *)
398 kvm_s390_available_subfunc.kmf);
399 __cpacf_query(CPACF_KMO, (cpacf_mask_t *)
400 kvm_s390_available_subfunc.kmo);
401 __cpacf_query(CPACF_PCC, (cpacf_mask_t *)
402 kvm_s390_available_subfunc.pcc);
0a763c78
DH
403 }
404 if (test_facility(57)) /* MSA5 */
985a9d20 405 __cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
69c0e360 406 kvm_s390_available_subfunc.ppno);
0a763c78 407
e000b8e0
JH
408 if (test_facility(146)) /* MSA8 */
409 __cpacf_query(CPACF_KMA, (cpacf_mask_t *)
410 kvm_s390_available_subfunc.kma);
411
13209ad0
CB
412 if (test_facility(155)) /* MSA9 */
413 __cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
414 kvm_s390_available_subfunc.kdsa);
415
173aec2d
CB
416 if (test_facility(150)) /* SORTL */
417 __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl);
418
4f45b90e
CB
419 if (test_facility(151)) /* DFLTCC */
420 __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
421
22be5a13
DH
422 if (MACHINE_HAS_ESOP)
423 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
a3508fbe
DH
424 /*
425 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
426 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
427 */
428 if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
a411edf1 429 !test_facility(3) || !nested)
a3508fbe
DH
430 return;
431 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
19c439b5
DH
432 if (sclp.has_64bscao)
433 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
0615a326
DH
434 if (sclp.has_siif)
435 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
77d18f6d
DH
436 if (sclp.has_gpere)
437 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
a1b7b9b2
DH
438 if (sclp.has_gsls)
439 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
5630a8e8
DH
440 if (sclp.has_ib)
441 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
13ee3f67
DH
442 if (sclp.has_cei)
443 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
7fd7f39d
DH
444 if (sclp.has_ibs)
445 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
730cd632
FA
446 if (sclp.has_kss)
447 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
5d3876a8
DH
448 /*
449 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
450 * all skey handling functions read/set the skey from the PGSTE
451 * instead of the real storage key.
452 *
453 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
454 * pages being detected as preserved although they are resident.
455 *
456 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
457 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
458 *
459 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
460 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
461 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
462 *
463 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
464 * cannot easily shadow the SCA because of the ipte lock.
465 */
22be5a13
DH
466}
467
6c30cd2e 468static int __init __kvm_s390_init(void)
b0c632db 469{
f76f6371 470 int rc = -ENOMEM;
308c3e66 471
78f26131
CB
472 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
473 if (!kvm_s390_dbf)
474 return -ENOMEM;
475
3e6c5568
JF
476 kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
477 if (!kvm_s390_dbf_uv)
b801ef42 478 goto err_kvm_uv;
3e6c5568
JF
479
480 if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
481 debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
b801ef42 482 goto err_debug_view;
78f26131 483
22be5a13
DH
484 kvm_s390_cpu_feat_init();
485
84877d93 486 /* Register floating interrupt controller interface. */
308c3e66
MM
487 rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
488 if (rc) {
8d43d570 489 pr_err("A FLIC registration call failed with rc=%d\n", rc);
b801ef42 490 goto err_flic;
308c3e66 491 }
b1d1e76e 492
189e7d87 493 if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
98b1d33d
MR
494 rc = kvm_s390_pci_init();
495 if (rc) {
496 pr_err("Unable to allocate AIFT for PCI\n");
b801ef42 497 goto err_pci;
98b1d33d
MR
498 }
499 }
500
b1d1e76e
MM
501 rc = kvm_s390_gib_init(GAL_ISC);
502 if (rc)
b801ef42 503 goto err_gib;
b1d1e76e 504
e43f5762
SC
505 gmap_notifier.notifier_call = kvm_gmap_notifier;
506 gmap_register_pte_notifier(&gmap_notifier);
507 vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
508 gmap_register_pte_notifier(&vsie_gmap_notifier);
509 atomic_notifier_chain_register(&s390_epoch_delta_notifier,
510 &kvm_clock_notifier);
511
308c3e66
MM
512 return 0;
513
b801ef42
SC
514err_gib:
515 if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
516 kvm_s390_pci_exit();
517err_pci:
518err_flic:
519err_debug_view:
520 debug_unregister(kvm_s390_dbf_uv);
521err_kvm_uv:
522 debug_unregister(kvm_s390_dbf);
308c3e66 523 return rc;
b0c632db
HC
524}
525
b8449265 526static void __kvm_s390_exit(void)
78f26131 527{
e43f5762
SC
528 gmap_unregister_pte_notifier(&gmap_notifier);
529 gmap_unregister_pte_notifier(&vsie_gmap_notifier);
530 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
531 &kvm_clock_notifier);
532
1282c21e 533 kvm_s390_gib_destroy();
189e7d87 534 if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
98b1d33d 535 kvm_s390_pci_exit();
78f26131 536 debug_unregister(kvm_s390_dbf);
3e6c5568 537 debug_unregister(kvm_s390_dbf_uv);
78f26131
CB
538}
539
b0c632db
HC
540/* Section: device related */
541long kvm_arch_dev_ioctl(struct file *filp,
542 unsigned int ioctl, unsigned long arg)
543{
544 if (ioctl == KVM_S390_ENABLE_SIE)
545 return s390_enable_sie();
546 return -EINVAL;
547}
548
784aa3d7 549int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
b0c632db 550{
d7b0b5eb
CO
551 int r;
552
2bd0ac4e 553 switch (ext) {
d7b0b5eb 554 case KVM_CAP_S390_PSW:
b6cf8788 555 case KVM_CAP_S390_GMAP:
52e16b18 556 case KVM_CAP_SYNC_MMU:
1efd0f59
CO
557#ifdef CONFIG_KVM_S390_UCONTROL
558 case KVM_CAP_S390_UCONTROL:
559#endif
3c038e6b 560 case KVM_CAP_ASYNC_PF:
60b413c9 561 case KVM_CAP_SYNC_REGS:
14eebd91 562 case KVM_CAP_ONE_REG:
d6712df9 563 case KVM_CAP_ENABLE_CAP:
fa6b7fe9 564 case KVM_CAP_S390_CSS_SUPPORT:
10ccaa1e 565 case KVM_CAP_IOEVENTFD:
c05c4186 566 case KVM_CAP_DEVICE_CTRL:
78599d90 567 case KVM_CAP_S390_IRQCHIP:
f2061656 568 case KVM_CAP_VM_ATTRIBUTES:
6352e4d2 569 case KVM_CAP_MP_STATE:
460df4c1 570 case KVM_CAP_IMMEDIATE_EXIT:
47b43c52 571 case KVM_CAP_S390_INJECT_IRQ:
2444b352 572 case KVM_CAP_S390_USER_SIGP:
e44fc8c9 573 case KVM_CAP_S390_USER_STSI:
30ee2a98 574 case KVM_CAP_S390_SKEYS:
816c7667 575 case KVM_CAP_S390_IRQ_STATE:
6502a34c 576 case KVM_CAP_S390_USER_INSTR0:
4036e387 577 case KVM_CAP_S390_CMMA_MIGRATION:
47a4693e 578 case KVM_CAP_S390_AIS:
da9a1446 579 case KVM_CAP_S390_AIS_MIGRATION:
7de3f142 580 case KVM_CAP_S390_VCPU_RESETS:
b9b2782c 581 case KVM_CAP_SET_GUEST_DEBUG:
23a60f83 582 case KVM_CAP_S390_DIAG318:
52882b9c 583 case KVM_CAP_IRQFD_RESAMPLE:
d7b0b5eb
CO
584 r = 1;
585 break;
a43b80b7
ML
586 case KVM_CAP_SET_GUEST_DEBUG2:
587 r = KVM_GUESTDBG_VALID_MASK;
588 break;
a4499382
JF
589 case KVM_CAP_S390_HPAGE_1M:
590 r = 0;
40ebdb8e 591 if (hpage && !kvm_is_ucontrol(kvm))
a4499382
JF
592 r = 1;
593 break;
41408c28
TH
594 case KVM_CAP_S390_MEM_OP:
595 r = MEM_OP_MAX_SIZE;
596 break;
3fd49805
JSG
597 case KVM_CAP_S390_MEM_OP_EXTENSION:
598 /*
599 * Flag bits indicating which extensions are supported.
600 * If r > 0, the base extension must also be supported/indicated,
601 * in order to maintain backwards compatibility.
602 */
603 r = KVM_S390_MEMOP_EXTENSION_CAP_BASE |
604 KVM_S390_MEMOP_EXTENSION_CAP_CMPXCHG;
605 break;
e726b1bd
CB
606 case KVM_CAP_NR_VCPUS:
607 case KVM_CAP_MAX_VCPUS:
a86cb413 608 case KVM_CAP_MAX_VCPU_ID:
76a6dd72 609 r = KVM_S390_BSCA_CPU_SLOTS;
a6940674
DH
610 if (!kvm_s390_use_sca_entries())
611 r = KVM_MAX_VCPUS;
612 else if (sclp.has_esca && sclp.has_64bscao)
76a6dd72 613 r = KVM_S390_ESCA_CPU_SLOTS;
82cc27ef
VK
614 if (ext == KVM_CAP_NR_VCPUS)
615 r = min_t(unsigned int, num_online_cpus(), r);
e726b1bd 616 break;
1526bf9c 617 case KVM_CAP_S390_COW:
abf09bed 618 r = MACHINE_HAS_ESOP;
1526bf9c 619 break;
68c55750
EF
620 case KVM_CAP_S390_VECTOR_REGISTERS:
621 r = MACHINE_HAS_VX;
622 break;
c6e5f166
FZ
623 case KVM_CAP_S390_RI:
624 r = test_facility(64);
625 break;
4e0b1ab7
FZ
626 case KVM_CAP_S390_GS:
627 r = test_facility(133);
628 break;
35b3fde6
CB
629 case KVM_CAP_S390_BPB:
630 r = test_facility(82);
631 break;
8c516b25
CI
632 case KVM_CAP_S390_PROTECTED_ASYNC_DISABLE:
633 r = async_destroy && is_prot_virt_host();
634 break;
13da9ae1
CB
635 case KVM_CAP_S390_PROTECTED:
636 r = is_prot_virt_host();
637 break;
e9bf3acb
JF
638 case KVM_CAP_S390_PROTECTED_DUMP: {
639 u64 pv_cmds_dump[] = {
640 BIT_UVC_CMD_DUMP_INIT,
641 BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE,
642 BIT_UVC_CMD_DUMP_CPU,
643 BIT_UVC_CMD_DUMP_COMPLETE,
644 };
645 int i;
646
647 r = is_prot_virt_host();
648
649 for (i = 0; i < ARRAY_SIZE(pv_cmds_dump); i++) {
650 if (!test_bit_inv(pv_cmds_dump[i],
651 (unsigned long *)&uv_info.inst_calls_list)) {
652 r = 0;
653 break;
654 }
655 }
656 break;
657 }
db1c875e
MR
658 case KVM_CAP_S390_ZPCI_OP:
659 r = kvm_s390_pci_interp_allowed();
660 break;
f5ecfee9
PM
661 case KVM_CAP_S390_CPU_TOPOLOGY:
662 r = test_facility(11);
663 break;
2bd0ac4e 664 default:
d7b0b5eb 665 r = 0;
2bd0ac4e 666 }
d7b0b5eb 667 return r;
b0c632db
HC
668}
669
0dff0846 670void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
15f36ebd 671{
0959e168 672 int i;
15f36ebd 673 gfn_t cur_gfn, last_gfn;
0959e168 674 unsigned long gaddr, vmaddr;
15f36ebd 675 struct gmap *gmap = kvm->arch.gmap;
0959e168 676 DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
15f36ebd 677
0959e168
JF
678 /* Loop over all guest segments */
679 cur_gfn = memslot->base_gfn;
15f36ebd 680 last_gfn = memslot->base_gfn + memslot->npages;
0959e168
JF
681 for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
682 gaddr = gfn_to_gpa(cur_gfn);
683 vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
684 if (kvm_is_error_hva(vmaddr))
685 continue;
686
687 bitmap_zero(bitmap, _PAGE_ENTRIES);
688 gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
689 for (i = 0; i < _PAGE_ENTRIES; i++) {
690 if (test_bit(i, bitmap))
691 mark_page_dirty(kvm, cur_gfn + i);
692 }
15f36ebd 693
1763f8d0
CB
694 if (fatal_signal_pending(current))
695 return;
70c88a00 696 cond_resched();
15f36ebd 697 }
15f36ebd
JH
698}
699
b0c632db 700/* Section: vm related */
a6e2f683
ED
701static void sca_del_vcpu(struct kvm_vcpu *vcpu);
702
b0c632db
HC
703/*
704 * Get (and clear) the dirty memory log for a memory slot.
705 */
706int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
707 struct kvm_dirty_log *log)
708{
15f36ebd
JH
709 int r;
710 unsigned long n;
711 struct kvm_memory_slot *memslot;
2a49f61d 712 int is_dirty;
15f36ebd 713
e1e8a962
JF
714 if (kvm_is_ucontrol(kvm))
715 return -EINVAL;
716
15f36ebd
JH
717 mutex_lock(&kvm->slots_lock);
718
719 r = -EINVAL;
720 if (log->slot >= KVM_USER_MEM_SLOTS)
721 goto out;
722
2a49f61d 723 r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
15f36ebd
JH
724 if (r)
725 goto out;
726
727 /* Clear the dirty log */
728 if (is_dirty) {
729 n = kvm_dirty_bitmap_bytes(memslot);
730 memset(memslot->dirty_bitmap, 0, n);
731 }
732 r = 0;
733out:
734 mutex_unlock(&kvm->slots_lock);
735 return r;
b0c632db
HC
736}
737
6502a34c
DH
738static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
739{
46808a4c 740 unsigned long i;
6502a34c
DH
741 struct kvm_vcpu *vcpu;
742
743 kvm_for_each_vcpu(i, vcpu, kvm) {
744 kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
745 }
746}
747
e5d83c74 748int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
d938dc55
CH
749{
750 int r;
751
752 if (cap->flags)
753 return -EINVAL;
754
755 switch (cap->cap) {
84223598 756 case KVM_CAP_S390_IRQCHIP:
c92ea7b9 757 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
84223598
CH
758 kvm->arch.use_irqchip = 1;
759 r = 0;
760 break;
2444b352 761 case KVM_CAP_S390_USER_SIGP:
c92ea7b9 762 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
2444b352
DH
763 kvm->arch.user_sigp = 1;
764 r = 0;
765 break;
68c55750 766 case KVM_CAP_S390_VECTOR_REGISTERS:
5967c17b 767 mutex_lock(&kvm->lock);
a03825bb 768 if (kvm->created_vcpus) {
5967c17b
DH
769 r = -EBUSY;
770 } else if (MACHINE_HAS_VX) {
c54f0d6a
DH
771 set_kvm_facility(kvm->arch.model.fac_mask, 129);
772 set_kvm_facility(kvm->arch.model.fac_list, 129);
2f87d942
GH
773 if (test_facility(134)) {
774 set_kvm_facility(kvm->arch.model.fac_mask, 134);
775 set_kvm_facility(kvm->arch.model.fac_list, 134);
776 }
53743aa7
MS
777 if (test_facility(135)) {
778 set_kvm_facility(kvm->arch.model.fac_mask, 135);
779 set_kvm_facility(kvm->arch.model.fac_list, 135);
780 }
7832e91c
CB
781 if (test_facility(148)) {
782 set_kvm_facility(kvm->arch.model.fac_mask, 148);
783 set_kvm_facility(kvm->arch.model.fac_list, 148);
784 }
d5cb6ab1
CB
785 if (test_facility(152)) {
786 set_kvm_facility(kvm->arch.model.fac_mask, 152);
787 set_kvm_facility(kvm->arch.model.fac_list, 152);
788 }
1f703d2c
CB
789 if (test_facility(192)) {
790 set_kvm_facility(kvm->arch.model.fac_mask, 192);
791 set_kvm_facility(kvm->arch.model.fac_list, 192);
792 }
18280d8b
MM
793 r = 0;
794 } else
795 r = -EINVAL;
5967c17b 796 mutex_unlock(&kvm->lock);
c92ea7b9
CB
797 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
798 r ? "(not available)" : "(success)");
68c55750 799 break;
c6e5f166
FZ
800 case KVM_CAP_S390_RI:
801 r = -EINVAL;
802 mutex_lock(&kvm->lock);
a03825bb 803 if (kvm->created_vcpus) {
c6e5f166
FZ
804 r = -EBUSY;
805 } else if (test_facility(64)) {
c54f0d6a
DH
806 set_kvm_facility(kvm->arch.model.fac_mask, 64);
807 set_kvm_facility(kvm->arch.model.fac_list, 64);
c6e5f166
FZ
808 r = 0;
809 }
810 mutex_unlock(&kvm->lock);
811 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
812 r ? "(not available)" : "(success)");
813 break;
47a4693e
YMZ
814 case KVM_CAP_S390_AIS:
815 mutex_lock(&kvm->lock);
816 if (kvm->created_vcpus) {
817 r = -EBUSY;
818 } else {
819 set_kvm_facility(kvm->arch.model.fac_mask, 72);
820 set_kvm_facility(kvm->arch.model.fac_list, 72);
47a4693e
YMZ
821 r = 0;
822 }
823 mutex_unlock(&kvm->lock);
824 VM_EVENT(kvm, 3, "ENABLE: AIS %s",
825 r ? "(not available)" : "(success)");
826 break;
4e0b1ab7
FZ
827 case KVM_CAP_S390_GS:
828 r = -EINVAL;
829 mutex_lock(&kvm->lock);
241e3ec0 830 if (kvm->created_vcpus) {
4e0b1ab7
FZ
831 r = -EBUSY;
832 } else if (test_facility(133)) {
833 set_kvm_facility(kvm->arch.model.fac_mask, 133);
834 set_kvm_facility(kvm->arch.model.fac_list, 133);
835 r = 0;
836 }
837 mutex_unlock(&kvm->lock);
838 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
839 r ? "(not available)" : "(success)");
840 break;
a4499382
JF
841 case KVM_CAP_S390_HPAGE_1M:
842 mutex_lock(&kvm->lock);
843 if (kvm->created_vcpus)
844 r = -EBUSY;
40ebdb8e 845 else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
a4499382
JF
846 r = -EINVAL;
847 else {
848 r = 0;
d8ed45c5 849 mmap_write_lock(kvm->mm);
a4499382 850 kvm->mm->context.allow_gmap_hpage_1m = 1;
d8ed45c5 851 mmap_write_unlock(kvm->mm);
a4499382
JF
852 /*
853 * We might have to create fake 4k page
854 * tables. To avoid that the hardware works on
855 * stale PGSTEs, we emulate these instructions.
856 */
857 kvm->arch.use_skf = 0;
858 kvm->arch.use_pfmfi = 0;
859 }
860 mutex_unlock(&kvm->lock);
861 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
862 r ? "(not available)" : "(success)");
863 break;
e44fc8c9 864 case KVM_CAP_S390_USER_STSI:
c92ea7b9 865 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
e44fc8c9
ET
866 kvm->arch.user_stsi = 1;
867 r = 0;
868 break;
6502a34c
DH
869 case KVM_CAP_S390_USER_INSTR0:
870 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
871 kvm->arch.user_instr0 = 1;
872 icpt_operexc_on_all_vcpus(kvm);
873 r = 0;
874 break;
f5ecfee9
PM
875 case KVM_CAP_S390_CPU_TOPOLOGY:
876 r = -EINVAL;
877 mutex_lock(&kvm->lock);
878 if (kvm->created_vcpus) {
879 r = -EBUSY;
880 } else if (test_facility(11)) {
881 set_kvm_facility(kvm->arch.model.fac_mask, 11);
882 set_kvm_facility(kvm->arch.model.fac_list, 11);
883 r = 0;
884 }
885 mutex_unlock(&kvm->lock);
886 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_CPU_TOPOLOGY %s",
887 r ? "(not available)" : "(success)");
888 break;
d938dc55
CH
889 default:
890 r = -EINVAL;
891 break;
892 }
893 return r;
894}
895
8c0a7ce6
DD
896static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
897{
898 int ret;
899
900 switch (attr->attr) {
901 case KVM_S390_VM_MEM_LIMIT_SIZE:
902 ret = 0;
c92ea7b9 903 VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
a3a92c31
DD
904 kvm->arch.mem_limit);
905 if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
8c0a7ce6
DD
906 ret = -EFAULT;
907 break;
908 default:
909 ret = -ENXIO;
910 break;
911 }
912 return ret;
913}
914
915static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
4f718eab
DD
916{
917 int ret;
918 unsigned int idx;
919 switch (attr->attr) {
920 case KVM_S390_VM_MEM_ENABLE_CMMA:
f9cbd9b0 921 ret = -ENXIO;
c24cc9c8 922 if (!sclp.has_cmma)
e6db1d61
DD
923 break;
924
c92ea7b9 925 VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
4f718eab 926 mutex_lock(&kvm->lock);
a4499382
JF
927 if (kvm->created_vcpus)
928 ret = -EBUSY;
929 else if (kvm->mm->context.allow_gmap_hpage_1m)
930 ret = -EINVAL;
931 else {
4f718eab 932 kvm->arch.use_cmma = 1;
c9f0a2b8
JF
933 /* Not compatible with cmma. */
934 kvm->arch.use_pfmfi = 0;
4f718eab
DD
935 ret = 0;
936 }
937 mutex_unlock(&kvm->lock);
938 break;
939 case KVM_S390_VM_MEM_CLR_CMMA:
f9cbd9b0
DH
940 ret = -ENXIO;
941 if (!sclp.has_cmma)
942 break;
c3489155
DD
943 ret = -EINVAL;
944 if (!kvm->arch.use_cmma)
945 break;
946
c92ea7b9 947 VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
4f718eab
DD
948 mutex_lock(&kvm->lock);
949 idx = srcu_read_lock(&kvm->srcu);
a13cff31 950 s390_reset_cmma(kvm->arch.gmap->mm);
4f718eab
DD
951 srcu_read_unlock(&kvm->srcu, idx);
952 mutex_unlock(&kvm->lock);
953 ret = 0;
954 break;
8c0a7ce6
DD
955 case KVM_S390_VM_MEM_LIMIT_SIZE: {
956 unsigned long new_limit;
957
958 if (kvm_is_ucontrol(kvm))
959 return -EINVAL;
960
961 if (get_user(new_limit, (u64 __user *)attr->addr))
962 return -EFAULT;
963
a3a92c31
DD
964 if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
965 new_limit > kvm->arch.mem_limit)
8c0a7ce6
DD
966 return -E2BIG;
967
a3a92c31
DD
968 if (!new_limit)
969 return -EINVAL;
970
6ea427bb 971 /* gmap_create takes last usable address */
a3a92c31
DD
972 if (new_limit != KVM_S390_NO_MEM_LIMIT)
973 new_limit -= 1;
974
8c0a7ce6
DD
975 ret = -EBUSY;
976 mutex_lock(&kvm->lock);
a03825bb 977 if (!kvm->created_vcpus) {
6ea427bb
MS
978 /* gmap_create will round the limit up */
979 struct gmap *new = gmap_create(current->mm, new_limit);
8c0a7ce6
DD
980
981 if (!new) {
982 ret = -ENOMEM;
983 } else {
6ea427bb 984 gmap_remove(kvm->arch.gmap);
8c0a7ce6
DD
985 new->private = kvm;
986 kvm->arch.gmap = new;
987 ret = 0;
988 }
989 }
990 mutex_unlock(&kvm->lock);
a3a92c31
DD
991 VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
992 VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
993 (void *) kvm->arch.gmap->asce);
8c0a7ce6
DD
994 break;
995 }
4f718eab
DD
996 default:
997 ret = -ENXIO;
998 break;
999 }
1000 return ret;
1001}
1002
a374e892
TK
1003static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
1004
20c922f0 1005void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
a374e892
TK
1006{
1007 struct kvm_vcpu *vcpu;
46808a4c 1008 unsigned long i;
a374e892 1009
20c922f0
TK
1010 kvm_s390_vcpu_block_all(kvm);
1011
3194cdb7 1012 kvm_for_each_vcpu(i, vcpu, kvm) {
20c922f0 1013 kvm_s390_vcpu_crypto_setup(vcpu);
3194cdb7
DH
1014 /* recreate the shadow crycb by leaving the VSIE handler */
1015 kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
1016 }
20c922f0
TK
1017
1018 kvm_s390_vcpu_unblock_all(kvm);
1019}
1020
1021static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
1022{
a374e892
TK
1023 mutex_lock(&kvm->lock);
1024 switch (attr->attr) {
1025 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
8e41bd54
CB
1026 if (!test_kvm_facility(kvm, 76)) {
1027 mutex_unlock(&kvm->lock);
37940fb0 1028 return -EINVAL;
8e41bd54 1029 }
a374e892
TK
1030 get_random_bytes(
1031 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1032 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1033 kvm->arch.crypto.aes_kw = 1;
c92ea7b9 1034 VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
a374e892
TK
1035 break;
1036 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
8e41bd54
CB
1037 if (!test_kvm_facility(kvm, 76)) {
1038 mutex_unlock(&kvm->lock);
37940fb0 1039 return -EINVAL;
8e41bd54 1040 }
a374e892
TK
1041 get_random_bytes(
1042 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1043 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
1044 kvm->arch.crypto.dea_kw = 1;
c92ea7b9 1045 VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
a374e892
TK
1046 break;
1047 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
8e41bd54
CB
1048 if (!test_kvm_facility(kvm, 76)) {
1049 mutex_unlock(&kvm->lock);
37940fb0 1050 return -EINVAL;
8e41bd54 1051 }
a374e892
TK
1052 kvm->arch.crypto.aes_kw = 0;
1053 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
1054 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
c92ea7b9 1055 VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
a374e892
TK
1056 break;
1057 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
8e41bd54
CB
1058 if (!test_kvm_facility(kvm, 76)) {
1059 mutex_unlock(&kvm->lock);
37940fb0 1060 return -EINVAL;
8e41bd54 1061 }
a374e892
TK
1062 kvm->arch.crypto.dea_kw = 0;
1063 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
1064 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
c92ea7b9 1065 VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
a374e892 1066 break;
37940fb0
TK
1067 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
1068 if (!ap_instructions_available()) {
1069 mutex_unlock(&kvm->lock);
1070 return -EOPNOTSUPP;
1071 }
1072 kvm->arch.crypto.apie = 1;
1073 break;
1074 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
1075 if (!ap_instructions_available()) {
1076 mutex_unlock(&kvm->lock);
1077 return -EOPNOTSUPP;
1078 }
1079 kvm->arch.crypto.apie = 0;
1080 break;
a374e892
TK
1081 default:
1082 mutex_unlock(&kvm->lock);
1083 return -ENXIO;
1084 }
1085
20c922f0 1086 kvm_s390_vcpu_crypto_reset_all(kvm);
a374e892
TK
1087 mutex_unlock(&kvm->lock);
1088 return 0;
1089}
1090
3f4bbb43
MR
1091static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu)
1092{
1093 /* Only set the ECB bits after guest requests zPCI interpretation */
1094 if (!vcpu->kvm->arch.use_zpci_interp)
1095 return;
1096
1097 vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI;
1098 vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI;
1099}
1100
1101void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm)
1102{
1103 struct kvm_vcpu *vcpu;
1104 unsigned long i;
1105
1106 lockdep_assert_held(&kvm->lock);
1107
1108 if (!kvm_s390_pci_interp_allowed())
1109 return;
1110
1111 /*
1112 * If host is configured for PCI and the necessary facilities are
1113 * available, turn on interpretation for the life of this guest
1114 */
1115 kvm->arch.use_zpci_interp = 1;
1116
1117 kvm_s390_vcpu_block_all(kvm);
1118
1119 kvm_for_each_vcpu(i, vcpu, kvm) {
1120 kvm_s390_vcpu_pci_setup(vcpu);
1121 kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
1122 }
1123
1124 kvm_s390_vcpu_unblock_all(kvm);
1125}
1126
190df4a2
CI
1127static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
1128{
46808a4c 1129 unsigned long cx;
190df4a2
CI
1130 struct kvm_vcpu *vcpu;
1131
1132 kvm_for_each_vcpu(cx, vcpu, kvm)
1133 kvm_s390_sync_request(req, vcpu);
1134}
1135
1136/*
1137 * Must be called with kvm->srcu held to avoid races on memslots, and with
1de1ea7e 1138 * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
190df4a2
CI
1139 */
1140static int kvm_s390_vm_start_migration(struct kvm *kvm)
1141{
190df4a2 1142 struct kvm_memory_slot *ms;
190df4a2 1143 struct kvm_memslots *slots;
afdad616 1144 unsigned long ram_pages = 0;
a54d8066 1145 int bkt;
190df4a2
CI
1146
1147 /* migration mode already enabled */
afdad616 1148 if (kvm->arch.migration_mode)
190df4a2 1149 return 0;
190df4a2 1150 slots = kvm_memslots(kvm);
a54d8066 1151 if (!slots || kvm_memslots_empty(slots))
190df4a2
CI
1152 return -EINVAL;
1153
afdad616
CI
1154 if (!kvm->arch.use_cmma) {
1155 kvm->arch.migration_mode = 1;
1156 return 0;
1157 }
1158 /* mark all the pages in active slots as dirty */
a54d8066 1159 kvm_for_each_memslot(ms, bkt, slots) {
13a17cc0
IM
1160 if (!ms->dirty_bitmap)
1161 return -EINVAL;
190df4a2 1162 /*
afdad616
CI
1163 * The second half of the bitmap is only used on x86,
1164 * and would be wasted otherwise, so we put it to good
1165 * use here to keep track of the state of the storage
1166 * attributes.
190df4a2 1167 */
afdad616
CI
1168 memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
1169 ram_pages += ms->npages;
190df4a2 1170 }
afdad616
CI
1171 atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
1172 kvm->arch.migration_mode = 1;
1173 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
190df4a2
CI
1174 return 0;
1175}
1176
1177/*
1de1ea7e 1178 * Must be called with kvm->slots_lock to avoid races with ourselves and
190df4a2
CI
1179 * kvm_s390_vm_start_migration.
1180 */
1181static int kvm_s390_vm_stop_migration(struct kvm *kvm)
1182{
190df4a2 1183 /* migration mode already disabled */
afdad616 1184 if (!kvm->arch.migration_mode)
190df4a2 1185 return 0;
afdad616
CI
1186 kvm->arch.migration_mode = 0;
1187 if (kvm->arch.use_cmma)
190df4a2 1188 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
190df4a2
CI
1189 return 0;
1190}
1191
1192static int kvm_s390_vm_set_migration(struct kvm *kvm,
1193 struct kvm_device_attr *attr)
1194{
1de1ea7e 1195 int res = -ENXIO;
190df4a2 1196
1de1ea7e 1197 mutex_lock(&kvm->slots_lock);
190df4a2
CI
1198 switch (attr->attr) {
1199 case KVM_S390_VM_MIGRATION_START:
190df4a2 1200 res = kvm_s390_vm_start_migration(kvm);
190df4a2
CI
1201 break;
1202 case KVM_S390_VM_MIGRATION_STOP:
1203 res = kvm_s390_vm_stop_migration(kvm);
1204 break;
1205 default:
1206 break;
1207 }
1de1ea7e 1208 mutex_unlock(&kvm->slots_lock);
190df4a2
CI
1209
1210 return res;
1211}
1212
1213static int kvm_s390_vm_get_migration(struct kvm *kvm,
1214 struct kvm_device_attr *attr)
1215{
afdad616 1216 u64 mig = kvm->arch.migration_mode;
190df4a2
CI
1217
1218 if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1219 return -ENXIO;
1220
1221 if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1222 return -EFAULT;
1223 return 0;
1224}
1225
6973091d
NB
1226static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod);
1227
8fa1696e
CW
1228static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1229{
1230 struct kvm_s390_vm_tod_clock gtod;
1231
1232 if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
1233 return -EFAULT;
1234
0e7def5f 1235 if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
8fa1696e 1236 return -EINVAL;
6973091d 1237 __kvm_s390_set_tod_clock(kvm, &gtod);
8fa1696e
CW
1238
1239 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
1240 gtod.epoch_idx, gtod.tod);
1241
1242 return 0;
1243}
1244
72f25020
JH
1245static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1246{
1247 u8 gtod_high;
1248
1249 if (copy_from_user(&gtod_high, (void __user *)attr->addr,
1250 sizeof(gtod_high)))
1251 return -EFAULT;
1252
1253 if (gtod_high != 0)
1254 return -EINVAL;
58c383c6 1255 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
72f25020
JH
1256
1257 return 0;
1258}
1259
1260static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1261{
0e7def5f 1262 struct kvm_s390_vm_tod_clock gtod = { 0 };
72f25020 1263
0e7def5f
DH
1264 if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
1265 sizeof(gtod.tod)))
72f25020
JH
1266 return -EFAULT;
1267
6973091d 1268 __kvm_s390_set_tod_clock(kvm, &gtod);
0e7def5f 1269 VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
72f25020
JH
1270 return 0;
1271}
1272
1273static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1274{
1275 int ret;
1276
1277 if (attr->flags)
1278 return -EINVAL;
1279
6973091d
NB
1280 mutex_lock(&kvm->lock);
1281 /*
1282 * For protected guests, the TOD is managed by the ultravisor, so trying
1283 * to change it will never bring the expected results.
1284 */
1285 if (kvm_s390_pv_is_protected(kvm)) {
1286 ret = -EOPNOTSUPP;
1287 goto out_unlock;
1288 }
1289
72f25020 1290 switch (attr->attr) {
8fa1696e
CW
1291 case KVM_S390_VM_TOD_EXT:
1292 ret = kvm_s390_set_tod_ext(kvm, attr);
1293 break;
72f25020
JH
1294 case KVM_S390_VM_TOD_HIGH:
1295 ret = kvm_s390_set_tod_high(kvm, attr);
1296 break;
1297 case KVM_S390_VM_TOD_LOW:
1298 ret = kvm_s390_set_tod_low(kvm, attr);
1299 break;
1300 default:
1301 ret = -ENXIO;
1302 break;
1303 }
6973091d
NB
1304
1305out_unlock:
1306 mutex_unlock(&kvm->lock);
72f25020
JH
1307 return ret;
1308}
1309
33d1b272
DH
1310static void kvm_s390_get_tod_clock(struct kvm *kvm,
1311 struct kvm_s390_vm_tod_clock *gtod)
8fa1696e 1312{
2cfd7b73 1313 union tod_clock clk;
8fa1696e
CW
1314
1315 preempt_disable();
1316
2cfd7b73 1317 store_tod_clock_ext(&clk);
8fa1696e 1318
2cfd7b73 1319 gtod->tod = clk.tod + kvm->arch.epoch;
33d1b272
DH
1320 gtod->epoch_idx = 0;
1321 if (test_kvm_facility(kvm, 139)) {
2cfd7b73
HC
1322 gtod->epoch_idx = clk.ei + kvm->arch.epdx;
1323 if (gtod->tod < clk.tod)
33d1b272
DH
1324 gtod->epoch_idx += 1;
1325 }
8fa1696e
CW
1326
1327 preempt_enable();
1328}
1329
1330static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1331{
1332 struct kvm_s390_vm_tod_clock gtod;
1333
1334 memset(&gtod, 0, sizeof(gtod));
33d1b272 1335 kvm_s390_get_tod_clock(kvm, &gtod);
8fa1696e
CW
1336 if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
1337 return -EFAULT;
1338
1339 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
1340 gtod.epoch_idx, gtod.tod);
1341 return 0;
1342}
1343
72f25020
JH
1344static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1345{
1346 u8 gtod_high = 0;
1347
1348 if (copy_to_user((void __user *)attr->addr, &gtod_high,
1349 sizeof(gtod_high)))
1350 return -EFAULT;
58c383c6 1351 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
72f25020
JH
1352
1353 return 0;
1354}
1355
1356static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1357{
5a3d883a 1358 u64 gtod;
72f25020 1359
60417fcc 1360 gtod = kvm_s390_get_tod_clock_fast(kvm);
72f25020
JH
1361 if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
1362 return -EFAULT;
58c383c6 1363 VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
72f25020
JH
1364
1365 return 0;
1366}
1367
1368static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1369{
1370 int ret;
1371
1372 if (attr->flags)
1373 return -EINVAL;
1374
1375 switch (attr->attr) {
8fa1696e
CW
1376 case KVM_S390_VM_TOD_EXT:
1377 ret = kvm_s390_get_tod_ext(kvm, attr);
1378 break;
72f25020
JH
1379 case KVM_S390_VM_TOD_HIGH:
1380 ret = kvm_s390_get_tod_high(kvm, attr);
1381 break;
1382 case KVM_S390_VM_TOD_LOW:
1383 ret = kvm_s390_get_tod_low(kvm, attr);
1384 break;
1385 default:
1386 ret = -ENXIO;
1387 break;
1388 }
1389 return ret;
1390}
1391
658b6eda
MM
1392static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1393{
1394 struct kvm_s390_vm_cpu_processor *proc;
053dd230 1395 u16 lowest_ibc, unblocked_ibc;
658b6eda
MM
1396 int ret = 0;
1397
1398 mutex_lock(&kvm->lock);
a03825bb 1399 if (kvm->created_vcpus) {
658b6eda
MM
1400 ret = -EBUSY;
1401 goto out;
1402 }
c4196218 1403 proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
658b6eda
MM
1404 if (!proc) {
1405 ret = -ENOMEM;
1406 goto out;
1407 }
1408 if (!copy_from_user(proc, (void __user *)attr->addr,
1409 sizeof(*proc))) {
9bb0ec09 1410 kvm->arch.model.cpuid = proc->cpuid;
053dd230
DH
1411 lowest_ibc = sclp.ibc >> 16 & 0xfff;
1412 unblocked_ibc = sclp.ibc & 0xfff;
0487c44d 1413 if (lowest_ibc && proc->ibc) {
053dd230
DH
1414 if (proc->ibc > unblocked_ibc)
1415 kvm->arch.model.ibc = unblocked_ibc;
1416 else if (proc->ibc < lowest_ibc)
1417 kvm->arch.model.ibc = lowest_ibc;
1418 else
1419 kvm->arch.model.ibc = proc->ibc;
1420 }
c54f0d6a 1421 memcpy(kvm->arch.model.fac_list, proc->fac_list,
658b6eda 1422 S390_ARCH_FAC_LIST_SIZE_BYTE);
a8c39dd7
CB
1423 VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1424 kvm->arch.model.ibc,
1425 kvm->arch.model.cpuid);
1426 VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1427 kvm->arch.model.fac_list[0],
1428 kvm->arch.model.fac_list[1],
1429 kvm->arch.model.fac_list[2]);
658b6eda
MM
1430 } else
1431 ret = -EFAULT;
1432 kfree(proc);
1433out:
1434 mutex_unlock(&kvm->lock);
1435 return ret;
1436}
1437
15c9705f
DH
1438static int kvm_s390_set_processor_feat(struct kvm *kvm,
1439 struct kvm_device_attr *attr)
1440{
1441 struct kvm_s390_vm_cpu_feat data;
15c9705f
DH
1442
1443 if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
1444 return -EFAULT;
1445 if (!bitmap_subset((unsigned long *) data.feat,
1446 kvm_s390_available_cpu_feat,
1447 KVM_S390_VM_CPU_FEAT_NR_BITS))
1448 return -EINVAL;
1449
1450 mutex_lock(&kvm->lock);
2f8311c9
CB
1451 if (kvm->created_vcpus) {
1452 mutex_unlock(&kvm->lock);
1453 return -EBUSY;
15c9705f 1454 }
da0f8e95 1455 bitmap_from_arr64(kvm->arch.cpu_feat, data.feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
15c9705f 1456 mutex_unlock(&kvm->lock);
2f8311c9
CB
1457 VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1458 data.feat[0],
1459 data.feat[1],
1460 data.feat[2]);
1461 return 0;
15c9705f
DH
1462}
1463
0a763c78
DH
1464static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
1465 struct kvm_device_attr *attr)
1466{
346fa2f8
CB
1467 mutex_lock(&kvm->lock);
1468 if (kvm->created_vcpus) {
1469 mutex_unlock(&kvm->lock);
1470 return -EBUSY;
1471 }
1472
1473 if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1474 sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1475 mutex_unlock(&kvm->lock);
1476 return -EFAULT;
1477 }
1478 mutex_unlock(&kvm->lock);
1479
11ba5961
CB
1480 VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1481 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1482 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1483 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1484 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1485 VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1486 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1487 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1488 VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1489 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1490 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1491 VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1492 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1493 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1494 VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx",
1495 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1496 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1497 VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1498 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1499 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1500 VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1501 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1502 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1503 VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1504 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1505 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1506 VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1507 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1508 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1509 VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1510 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1511 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1512 VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1513 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1514 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1515 VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1516 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1517 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1518 VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1519 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1520 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1521 VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1522 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1523 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
13209ad0
CB
1524 VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1525 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1526 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
173aec2d
CB
1527 VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1528 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1529 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1530 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1531 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
4f45b90e
CB
1532 VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1533 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1534 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1535 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1536 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
11ba5961 1537
346fa2f8 1538 return 0;
0a763c78
DH
1539}
1540
19c654bf
SE
1541#define KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK \
1542( \
1543 ((struct kvm_s390_vm_cpu_uv_feat){ \
1544 .ap = 1, \
1545 .ap_intr = 1, \
1546 }) \
1547 .feat \
1548)
1549
1550static int kvm_s390_set_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
1551{
1552 struct kvm_s390_vm_cpu_uv_feat __user *ptr = (void __user *)attr->addr;
1553 unsigned long data, filter;
1554
1555 filter = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
1556 if (get_user(data, &ptr->feat))
1557 return -EFAULT;
1558 if (!bitmap_subset(&data, &filter, KVM_S390_VM_CPU_UV_FEAT_NR_BITS))
1559 return -EINVAL;
1560
1561 mutex_lock(&kvm->lock);
1562 if (kvm->created_vcpus) {
1563 mutex_unlock(&kvm->lock);
1564 return -EBUSY;
1565 }
1566 kvm->arch.model.uv_feat_guest.feat = data;
1567 mutex_unlock(&kvm->lock);
1568
1569 VM_EVENT(kvm, 3, "SET: guest UV-feat: 0x%16.16lx", data);
1570
1571 return 0;
1572}
1573
658b6eda
MM
1574static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1575{
1576 int ret = -ENXIO;
1577
1578 switch (attr->attr) {
1579 case KVM_S390_VM_CPU_PROCESSOR:
1580 ret = kvm_s390_set_processor(kvm, attr);
1581 break;
15c9705f
DH
1582 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1583 ret = kvm_s390_set_processor_feat(kvm, attr);
1584 break;
0a763c78
DH
1585 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1586 ret = kvm_s390_set_processor_subfunc(kvm, attr);
1587 break;
19c654bf
SE
1588 case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
1589 ret = kvm_s390_set_uv_feat(kvm, attr);
1590 break;
658b6eda
MM
1591 }
1592 return ret;
1593}
1594
1595static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1596{
1597 struct kvm_s390_vm_cpu_processor *proc;
1598 int ret = 0;
1599
c4196218 1600 proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
658b6eda
MM
1601 if (!proc) {
1602 ret = -ENOMEM;
1603 goto out;
1604 }
9bb0ec09 1605 proc->cpuid = kvm->arch.model.cpuid;
658b6eda 1606 proc->ibc = kvm->arch.model.ibc;
c54f0d6a
DH
1607 memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1608 S390_ARCH_FAC_LIST_SIZE_BYTE);
a8c39dd7
CB
1609 VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1610 kvm->arch.model.ibc,
1611 kvm->arch.model.cpuid);
1612 VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1613 kvm->arch.model.fac_list[0],
1614 kvm->arch.model.fac_list[1],
1615 kvm->arch.model.fac_list[2]);
658b6eda
MM
1616 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1617 ret = -EFAULT;
1618 kfree(proc);
1619out:
1620 return ret;
1621}
1622
1623static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1624{
1625 struct kvm_s390_vm_cpu_machine *mach;
1626 int ret = 0;
1627
c4196218 1628 mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT);
658b6eda
MM
1629 if (!mach) {
1630 ret = -ENOMEM;
1631 goto out;
1632 }
1633 get_cpu_id((struct cpuid *) &mach->cpuid);
37c5f6c8 1634 mach->ibc = sclp.ibc;
c54f0d6a 1635 memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
981467c9 1636 S390_ARCH_FAC_LIST_SIZE_BYTE);
17e89e13
SS
1637 memcpy((unsigned long *)&mach->fac_list, stfle_fac_list,
1638 sizeof(stfle_fac_list));
a8c39dd7
CB
1639 VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
1640 kvm->arch.model.ibc,
1641 kvm->arch.model.cpuid);
1642 VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
1643 mach->fac_mask[0],
1644 mach->fac_mask[1],
1645 mach->fac_mask[2]);
1646 VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1647 mach->fac_list[0],
1648 mach->fac_list[1],
1649 mach->fac_list[2]);
658b6eda
MM
1650 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1651 ret = -EFAULT;
1652 kfree(mach);
1653out:
1654 return ret;
1655}
1656
15c9705f
DH
1657static int kvm_s390_get_processor_feat(struct kvm *kvm,
1658 struct kvm_device_attr *attr)
1659{
1660 struct kvm_s390_vm_cpu_feat data;
1661
da0f8e95 1662 bitmap_to_arr64(data.feat, kvm->arch.cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
15c9705f
DH
1663 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1664 return -EFAULT;
2f8311c9
CB
1665 VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1666 data.feat[0],
1667 data.feat[1],
1668 data.feat[2]);
15c9705f
DH
1669 return 0;
1670}
1671
1672static int kvm_s390_get_machine_feat(struct kvm *kvm,
1673 struct kvm_device_attr *attr)
1674{
1675 struct kvm_s390_vm_cpu_feat data;
1676
da0f8e95 1677 bitmap_to_arr64(data.feat, kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
15c9705f
DH
1678 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1679 return -EFAULT;
2f8311c9
CB
1680 VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1681 data.feat[0],
1682 data.feat[1],
1683 data.feat[2]);
15c9705f
DH
1684 return 0;
1685}
1686
0a763c78
DH
1687static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
1688 struct kvm_device_attr *attr)
1689{
346fa2f8
CB
1690 if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1691 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1692 return -EFAULT;
1693
11ba5961
CB
1694 VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1695 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1696 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1697 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1698 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1699 VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1700 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1701 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1702 VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1703 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1704 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1705 VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1706 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1707 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1708 VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx",
1709 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1710 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1711 VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1712 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1713 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1714 VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1715 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1716 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1717 VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1718 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1719 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1720 VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1721 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1722 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1723 VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1724 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1725 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1726 VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1727 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1728 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1729 VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1730 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1731 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1732 VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1733 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1734 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1735 VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1736 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1737 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
13209ad0
CB
1738 VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1739 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1740 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
173aec2d
CB
1741 VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1742 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1743 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1744 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1745 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
4f45b90e
CB
1746 VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1747 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1748 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1749 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1750 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
11ba5961 1751
346fa2f8 1752 return 0;
0a763c78
DH
1753}
1754
1755static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
1756 struct kvm_device_attr *attr)
1757{
1758 if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
1759 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1760 return -EFAULT;
11ba5961
CB
1761
1762 VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1763 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
1764 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
1765 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
1766 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
1767 VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx",
1768 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
1769 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
1770 VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx",
1771 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
1772 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
1773 VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx",
1774 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
1775 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
1776 VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx",
1777 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
1778 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
1779 VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx",
1780 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
1781 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
1782 VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx",
1783 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
1784 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
1785 VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx",
1786 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
1787 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
1788 VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx",
1789 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
1790 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
1791 VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx",
1792 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
1793 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
1794 VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx",
1795 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
1796 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
1797 VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx",
1798 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
1799 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
1800 VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx",
1801 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
1802 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
1803 VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx",
1804 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
1805 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
13209ad0
CB
1806 VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx",
1807 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
1808 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
173aec2d
CB
1809 VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1810 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1811 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1812 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1813 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
4f45b90e
CB
1814 VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1815 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
1816 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
1817 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
1818 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
11ba5961 1819
0a763c78
DH
1820 return 0;
1821}
346fa2f8 1822
19c654bf
SE
1823static int kvm_s390_get_processor_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
1824{
1825 struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
1826 unsigned long feat = kvm->arch.model.uv_feat_guest.feat;
1827
1828 if (put_user(feat, &dst->feat))
1829 return -EFAULT;
1830 VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
1831
1832 return 0;
1833}
1834
1835static int kvm_s390_get_machine_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
1836{
1837 struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
1838 unsigned long feat;
1839
1840 BUILD_BUG_ON(sizeof(*dst) != sizeof(uv_info.uv_feature_indications));
1841
1842 feat = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
1843 if (put_user(feat, &dst->feat))
1844 return -EFAULT;
1845 VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
1846
1847 return 0;
1848}
1849
658b6eda
MM
1850static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1851{
1852 int ret = -ENXIO;
1853
1854 switch (attr->attr) {
1855 case KVM_S390_VM_CPU_PROCESSOR:
1856 ret = kvm_s390_get_processor(kvm, attr);
1857 break;
1858 case KVM_S390_VM_CPU_MACHINE:
1859 ret = kvm_s390_get_machine(kvm, attr);
1860 break;
15c9705f
DH
1861 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1862 ret = kvm_s390_get_processor_feat(kvm, attr);
1863 break;
1864 case KVM_S390_VM_CPU_MACHINE_FEAT:
1865 ret = kvm_s390_get_machine_feat(kvm, attr);
1866 break;
0a763c78
DH
1867 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1868 ret = kvm_s390_get_processor_subfunc(kvm, attr);
1869 break;
1870 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1871 ret = kvm_s390_get_machine_subfunc(kvm, attr);
1872 break;
19c654bf
SE
1873 case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
1874 ret = kvm_s390_get_processor_uv_feat(kvm, attr);
1875 break;
1876 case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
1877 ret = kvm_s390_get_machine_uv_feat(kvm, attr);
1878 break;
658b6eda
MM
1879 }
1880 return ret;
1881}
1882
24fe0195
PM
1883/**
1884 * kvm_s390_update_topology_change_report - update CPU topology change report
1885 * @kvm: guest KVM description
1886 * @val: set or clear the MTCR bit
1887 *
1888 * Updates the Multiprocessor Topology-Change-Report bit to signal
1889 * the guest with a topology change.
1890 * This is only relevant if the topology facility is present.
1891 *
1892 * The SCA version, bsca or esca, doesn't matter as offset is the same.
1893 */
1894static void kvm_s390_update_topology_change_report(struct kvm *kvm, bool val)
1895{
1896 union sca_utility new, old;
1897 struct bsca_block *sca;
1898
1899 read_lock(&kvm->arch.sca_lock);
1900 sca = kvm->arch.sca;
1901 do {
1902 old = READ_ONCE(sca->utility);
1903 new = old;
1904 new.mtcr = val;
1905 } while (cmpxchg(&sca->utility.val, old.val, new.val) != old.val);
1906 read_unlock(&kvm->arch.sca_lock);
1907}
1908
f5ecfee9
PM
1909static int kvm_s390_set_topo_change_indication(struct kvm *kvm,
1910 struct kvm_device_attr *attr)
1911{
1912 if (!test_kvm_facility(kvm, 11))
1913 return -ENXIO;
1914
1915 kvm_s390_update_topology_change_report(kvm, !!attr->attr);
1916 return 0;
1917}
1918
1919static int kvm_s390_get_topo_change_indication(struct kvm *kvm,
1920 struct kvm_device_attr *attr)
1921{
1922 u8 topo;
1923
1924 if (!test_kvm_facility(kvm, 11))
1925 return -ENXIO;
1926
1927 read_lock(&kvm->arch.sca_lock);
1928 topo = ((struct bsca_block *)kvm->arch.sca)->utility.mtcr;
1929 read_unlock(&kvm->arch.sca_lock);
1930
1931 return put_user(topo, (u8 __user *)attr->addr);
1932}
1933
f2061656
DD
1934static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1935{
1936 int ret;
1937
1938 switch (attr->group) {
4f718eab 1939 case KVM_S390_VM_MEM_CTRL:
8c0a7ce6 1940 ret = kvm_s390_set_mem_control(kvm, attr);
4f718eab 1941 break;
72f25020
JH
1942 case KVM_S390_VM_TOD:
1943 ret = kvm_s390_set_tod(kvm, attr);
1944 break;
658b6eda
MM
1945 case KVM_S390_VM_CPU_MODEL:
1946 ret = kvm_s390_set_cpu_model(kvm, attr);
1947 break;
a374e892
TK
1948 case KVM_S390_VM_CRYPTO:
1949 ret = kvm_s390_vm_set_crypto(kvm, attr);
1950 break;
190df4a2
CI
1951 case KVM_S390_VM_MIGRATION:
1952 ret = kvm_s390_vm_set_migration(kvm, attr);
1953 break;
f5ecfee9
PM
1954 case KVM_S390_VM_CPU_TOPOLOGY:
1955 ret = kvm_s390_set_topo_change_indication(kvm, attr);
1956 break;
f2061656
DD
1957 default:
1958 ret = -ENXIO;
1959 break;
1960 }
1961
1962 return ret;
1963}
1964
1965static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1966{
8c0a7ce6
DD
1967 int ret;
1968
1969 switch (attr->group) {
1970 case KVM_S390_VM_MEM_CTRL:
1971 ret = kvm_s390_get_mem_control(kvm, attr);
1972 break;
72f25020
JH
1973 case KVM_S390_VM_TOD:
1974 ret = kvm_s390_get_tod(kvm, attr);
1975 break;
658b6eda
MM
1976 case KVM_S390_VM_CPU_MODEL:
1977 ret = kvm_s390_get_cpu_model(kvm, attr);
1978 break;
190df4a2
CI
1979 case KVM_S390_VM_MIGRATION:
1980 ret = kvm_s390_vm_get_migration(kvm, attr);
1981 break;
f5ecfee9
PM
1982 case KVM_S390_VM_CPU_TOPOLOGY:
1983 ret = kvm_s390_get_topo_change_indication(kvm, attr);
1984 break;
8c0a7ce6
DD
1985 default:
1986 ret = -ENXIO;
1987 break;
1988 }
1989
1990 return ret;
f2061656
DD
1991}
1992
1993static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1994{
1995 int ret;
1996
1997 switch (attr->group) {
4f718eab
DD
1998 case KVM_S390_VM_MEM_CTRL:
1999 switch (attr->attr) {
2000 case KVM_S390_VM_MEM_ENABLE_CMMA:
2001 case KVM_S390_VM_MEM_CLR_CMMA:
f9cbd9b0
DH
2002 ret = sclp.has_cmma ? 0 : -ENXIO;
2003 break;
8c0a7ce6 2004 case KVM_S390_VM_MEM_LIMIT_SIZE:
4f718eab
DD
2005 ret = 0;
2006 break;
2007 default:
2008 ret = -ENXIO;
2009 break;
2010 }
2011 break;
72f25020
JH
2012 case KVM_S390_VM_TOD:
2013 switch (attr->attr) {
2014 case KVM_S390_VM_TOD_LOW:
2015 case KVM_S390_VM_TOD_HIGH:
2016 ret = 0;
2017 break;
2018 default:
2019 ret = -ENXIO;
2020 break;
2021 }
2022 break;
658b6eda
MM
2023 case KVM_S390_VM_CPU_MODEL:
2024 switch (attr->attr) {
2025 case KVM_S390_VM_CPU_PROCESSOR:
2026 case KVM_S390_VM_CPU_MACHINE:
15c9705f
DH
2027 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
2028 case KVM_S390_VM_CPU_MACHINE_FEAT:
0a763c78 2029 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
346fa2f8 2030 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
19c654bf
SE
2031 case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
2032 case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
658b6eda
MM
2033 ret = 0;
2034 break;
2035 default:
2036 ret = -ENXIO;
2037 break;
2038 }
2039 break;
a374e892
TK
2040 case KVM_S390_VM_CRYPTO:
2041 switch (attr->attr) {
2042 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
2043 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
2044 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
2045 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
2046 ret = 0;
2047 break;
37940fb0
TK
2048 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
2049 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
2050 ret = ap_instructions_available() ? 0 : -ENXIO;
2051 break;
a374e892
TK
2052 default:
2053 ret = -ENXIO;
2054 break;
2055 }
2056 break;
190df4a2
CI
2057 case KVM_S390_VM_MIGRATION:
2058 ret = 0;
2059 break;
f5ecfee9
PM
2060 case KVM_S390_VM_CPU_TOPOLOGY:
2061 ret = test_kvm_facility(kvm, 11) ? 0 : -ENXIO;
2062 break;
f2061656
DD
2063 default:
2064 ret = -ENXIO;
2065 break;
2066 }
2067
2068 return ret;
2069}
2070
71fb165e 2071static int kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
30ee2a98
JH
2072{
2073 uint8_t *keys;
2074 uint64_t hva;
4f899147 2075 int srcu_idx, i, r = 0;
30ee2a98
JH
2076
2077 if (args->flags != 0)
2078 return -EINVAL;
2079
2080 /* Is this guest using storage keys? */
55531b74 2081 if (!mm_uses_skeys(current->mm))
30ee2a98
JH
2082 return KVM_S390_GET_SKEYS_NONE;
2083
2084 /* Enforce sane limit on memory allocation */
2085 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
2086 return -EINVAL;
2087
c4196218 2088 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
30ee2a98
JH
2089 if (!keys)
2090 return -ENOMEM;
2091
d8ed45c5 2092 mmap_read_lock(current->mm);
4f899147 2093 srcu_idx = srcu_read_lock(&kvm->srcu);
30ee2a98
JH
2094 for (i = 0; i < args->count; i++) {
2095 hva = gfn_to_hva(kvm, args->start_gfn + i);
2096 if (kvm_is_error_hva(hva)) {
2097 r = -EFAULT;
d3ed1cee 2098 break;
30ee2a98
JH
2099 }
2100
154c8c19
DH
2101 r = get_guest_storage_key(current->mm, hva, &keys[i]);
2102 if (r)
d3ed1cee 2103 break;
30ee2a98 2104 }
4f899147 2105 srcu_read_unlock(&kvm->srcu, srcu_idx);
d8ed45c5 2106 mmap_read_unlock(current->mm);
d3ed1cee
MS
2107
2108 if (!r) {
2109 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
2110 sizeof(uint8_t) * args->count);
2111 if (r)
2112 r = -EFAULT;
30ee2a98
JH
2113 }
2114
30ee2a98
JH
2115 kvfree(keys);
2116 return r;
2117}
2118
71fb165e 2119static int kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
30ee2a98
JH
2120{
2121 uint8_t *keys;
2122 uint64_t hva;
4f899147 2123 int srcu_idx, i, r = 0;
bd096f64 2124 bool unlocked;
30ee2a98
JH
2125
2126 if (args->flags != 0)
2127 return -EINVAL;
2128
2129 /* Enforce sane limit on memory allocation */
2130 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
2131 return -EINVAL;
2132
c4196218 2133 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
30ee2a98
JH
2134 if (!keys)
2135 return -ENOMEM;
2136
2137 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
2138 sizeof(uint8_t) * args->count);
2139 if (r) {
2140 r = -EFAULT;
2141 goto out;
2142 }
2143
2144 /* Enable storage key handling for the guest */
14d4a425
DD
2145 r = s390_enable_skey();
2146 if (r)
2147 goto out;
30ee2a98 2148
bd096f64 2149 i = 0;
d8ed45c5 2150 mmap_read_lock(current->mm);
4f899147 2151 srcu_idx = srcu_read_lock(&kvm->srcu);
bd096f64
JF
2152 while (i < args->count) {
2153 unlocked = false;
30ee2a98
JH
2154 hva = gfn_to_hva(kvm, args->start_gfn + i);
2155 if (kvm_is_error_hva(hva)) {
2156 r = -EFAULT;
d3ed1cee 2157 break;
30ee2a98
JH
2158 }
2159
2160 /* Lowest order bit is reserved */
2161 if (keys[i] & 0x01) {
2162 r = -EINVAL;
d3ed1cee 2163 break;
30ee2a98
JH
2164 }
2165
fe69eabf 2166 r = set_guest_storage_key(current->mm, hva, keys[i], 0);
bd096f64 2167 if (r) {
64019a2e 2168 r = fixup_user_fault(current->mm, hva,
bd096f64
JF
2169 FAULT_FLAG_WRITE, &unlocked);
2170 if (r)
2171 break;
2172 }
2173 if (!r)
2174 i++;
30ee2a98 2175 }
4f899147 2176 srcu_read_unlock(&kvm->srcu, srcu_idx);
d8ed45c5 2177 mmap_read_unlock(current->mm);
30ee2a98
JH
2178out:
2179 kvfree(keys);
2180 return r;
2181}
2182
4036e387
CI
2183/*
2184 * Base address and length must be sent at the start of each block, therefore
2185 * it's cheaper to send some clean data, as long as it's less than the size of
2186 * two longs.
2187 */
2188#define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
2189/* for consistency */
2190#define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
2191
afdad616
CI
2192static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2193 u8 *res, unsigned long bufsize)
2194{
2195 unsigned long pgstev, hva, cur_gfn = args->start_gfn;
2196
2197 args->count = 0;
2198 while (args->count < bufsize) {
2199 hva = gfn_to_hva(kvm, cur_gfn);
2200 /*
2201 * We return an error if the first value was invalid, but we
2202 * return successfully if at least one value was copied.
2203 */
2204 if (kvm_is_error_hva(hva))
2205 return args->count ? 0 : -EFAULT;
2206 if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2207 pgstev = 0;
2208 res[args->count++] = (pgstev >> 24) & 0x43;
2209 cur_gfn++;
2210 }
2211
2212 return 0;
2213}
2214
c928bfc2
MS
2215static struct kvm_memory_slot *gfn_to_memslot_approx(struct kvm_memslots *slots,
2216 gfn_t gfn)
2217{
2218 return ____gfn_to_memslot(slots, gfn, true);
2219}
2220
afdad616
CI
2221static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
2222 unsigned long cur_gfn)
2223{
c928bfc2 2224 struct kvm_memory_slot *ms = gfn_to_memslot_approx(slots, cur_gfn);
afdad616 2225 unsigned long ofs = cur_gfn - ms->base_gfn;
a54d8066 2226 struct rb_node *mnode = &ms->gfn_node[slots->node_idx];
afdad616
CI
2227
2228 if (ms->base_gfn + ms->npages <= cur_gfn) {
a54d8066 2229 mnode = rb_next(mnode);
afdad616 2230 /* If we are above the highest slot, wrap around */
a54d8066
MS
2231 if (!mnode)
2232 mnode = rb_first(&slots->gfn_tree);
afdad616 2233
a54d8066 2234 ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]);
afdad616
CI
2235 ofs = 0;
2236 }
285cff4c
NB
2237
2238 if (cur_gfn < ms->base_gfn)
2239 ofs = 0;
2240
afdad616 2241 ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
a54d8066
MS
2242 while (ofs >= ms->npages && (mnode = rb_next(mnode))) {
2243 ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]);
b5c7e7ec 2244 ofs = find_first_bit(kvm_second_dirty_bitmap(ms), ms->npages);
afdad616
CI
2245 }
2246 return ms->base_gfn + ofs;
2247}
2248
2249static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2250 u8 *res, unsigned long bufsize)
2251{
2252 unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
2253 struct kvm_memslots *slots = kvm_memslots(kvm);
2254 struct kvm_memory_slot *ms;
2255
a54d8066 2256 if (unlikely(kvm_memslots_empty(slots)))
0774a964
SC
2257 return 0;
2258
afdad616
CI
2259 cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
2260 ms = gfn_to_memslot(kvm, cur_gfn);
2261 args->count = 0;
2262 args->start_gfn = cur_gfn;
2263 if (!ms)
2264 return 0;
2265 next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
6a656832 2266 mem_end = kvm_s390_get_gfn_end(slots);
afdad616
CI
2267
2268 while (args->count < bufsize) {
2269 hva = gfn_to_hva(kvm, cur_gfn);
2270 if (kvm_is_error_hva(hva))
2271 return 0;
2272 /* Decrement only if we actually flipped the bit to 0 */
2273 if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
2274 atomic64_dec(&kvm->arch.cmma_dirty_pages);
2275 if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2276 pgstev = 0;
2277 /* Save the value */
2278 res[args->count++] = (pgstev >> 24) & 0x43;
2279 /* If the next bit is too far away, stop. */
2280 if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
2281 return 0;
2282 /* If we reached the previous "next", find the next one */
2283 if (cur_gfn == next_gfn)
2284 next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2285 /* Reached the end of memory or of the buffer, stop */
2286 if ((next_gfn >= mem_end) ||
2287 (next_gfn - args->start_gfn >= bufsize))
2288 return 0;
2289 cur_gfn++;
2290 /* Reached the end of the current memslot, take the next one. */
2291 if (cur_gfn - ms->base_gfn >= ms->npages) {
2292 ms = gfn_to_memslot(kvm, cur_gfn);
2293 if (!ms)
2294 return 0;
2295 }
2296 }
2297 return 0;
2298}
2299
4036e387
CI
2300/*
2301 * This function searches for the next page with dirty CMMA attributes, and
2302 * saves the attributes in the buffer up to either the end of the buffer or
2303 * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
2304 * no trailing clean bytes are saved.
2305 * In case no dirty bits were found, or if CMMA was not enabled or used, the
2306 * output buffer will indicate 0 as length.
2307 */
2308static int kvm_s390_get_cmma_bits(struct kvm *kvm,
2309 struct kvm_s390_cmma_log *args)
2310{
afdad616
CI
2311 unsigned long bufsize;
2312 int srcu_idx, peek, ret;
2313 u8 *values;
4036e387 2314
afdad616 2315 if (!kvm->arch.use_cmma)
4036e387
CI
2316 return -ENXIO;
2317 /* Invalid/unsupported flags were specified */
2318 if (args->flags & ~KVM_S390_CMMA_PEEK)
2319 return -EINVAL;
2320 /* Migration mode query, and we are not doing a migration */
2321 peek = !!(args->flags & KVM_S390_CMMA_PEEK);
afdad616 2322 if (!peek && !kvm->arch.migration_mode)
4036e387
CI
2323 return -EINVAL;
2324 /* CMMA is disabled or was not used, or the buffer has length zero */
2325 bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
c9f0a2b8 2326 if (!bufsize || !kvm->mm->context.uses_cmm) {
4036e387
CI
2327 memset(args, 0, sizeof(*args));
2328 return 0;
2329 }
afdad616
CI
2330 /* We are not peeking, and there are no dirty pages */
2331 if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
2332 memset(args, 0, sizeof(*args));
2333 return 0;
4036e387
CI
2334 }
2335
afdad616
CI
2336 values = vmalloc(bufsize);
2337 if (!values)
4036e387
CI
2338 return -ENOMEM;
2339
d8ed45c5 2340 mmap_read_lock(kvm->mm);
4036e387 2341 srcu_idx = srcu_read_lock(&kvm->srcu);
afdad616
CI
2342 if (peek)
2343 ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
2344 else
2345 ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
4036e387 2346 srcu_read_unlock(&kvm->srcu, srcu_idx);
d8ed45c5 2347 mmap_read_unlock(kvm->mm);
4036e387 2348
afdad616
CI
2349 if (kvm->arch.migration_mode)
2350 args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2351 else
2352 args->remaining = 0;
4036e387 2353
afdad616
CI
2354 if (copy_to_user((void __user *)args->values, values, args->count))
2355 ret = -EFAULT;
2356
2357 vfree(values);
2358 return ret;
4036e387
CI
2359}
2360
2361/*
2362 * This function sets the CMMA attributes for the given pages. If the input
2363 * buffer has zero length, no action is taken, otherwise the attributes are
c9f0a2b8 2364 * set and the mm->context.uses_cmm flag is set.
4036e387
CI
2365 */
2366static int kvm_s390_set_cmma_bits(struct kvm *kvm,
2367 const struct kvm_s390_cmma_log *args)
2368{
2369 unsigned long hva, mask, pgstev, i;
2370 uint8_t *bits;
2371 int srcu_idx, r = 0;
2372
2373 mask = args->mask;
2374
2375 if (!kvm->arch.use_cmma)
2376 return -ENXIO;
2377 /* invalid/unsupported flags */
2378 if (args->flags != 0)
2379 return -EINVAL;
2380 /* Enforce sane limit on memory allocation */
2381 if (args->count > KVM_S390_CMMA_SIZE_MAX)
2382 return -EINVAL;
2383 /* Nothing to do */
2384 if (args->count == 0)
2385 return 0;
2386
42bc47b3 2387 bits = vmalloc(array_size(sizeof(*bits), args->count));
4036e387
CI
2388 if (!bits)
2389 return -ENOMEM;
2390
2391 r = copy_from_user(bits, (void __user *)args->values, args->count);
2392 if (r) {
2393 r = -EFAULT;
2394 goto out;
2395 }
2396
d8ed45c5 2397 mmap_read_lock(kvm->mm);
4036e387
CI
2398 srcu_idx = srcu_read_lock(&kvm->srcu);
2399 for (i = 0; i < args->count; i++) {
2400 hva = gfn_to_hva(kvm, args->start_gfn + i);
2401 if (kvm_is_error_hva(hva)) {
2402 r = -EFAULT;
2403 break;
2404 }
2405
2406 pgstev = bits[i];
2407 pgstev = pgstev << 24;
1bab1c02 2408 mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
4036e387
CI
2409 set_pgste_bits(kvm->mm, hva, mask, pgstev);
2410 }
2411 srcu_read_unlock(&kvm->srcu, srcu_idx);
d8ed45c5 2412 mmap_read_unlock(kvm->mm);
4036e387 2413
c9f0a2b8 2414 if (!kvm->mm->context.uses_cmm) {
d8ed45c5 2415 mmap_write_lock(kvm->mm);
c9f0a2b8 2416 kvm->mm->context.uses_cmm = 1;
d8ed45c5 2417 mmap_write_unlock(kvm->mm);
4036e387
CI
2418 }
2419out:
2420 vfree(bits);
2421 return r;
2422}
2423
be48d86f
CI
2424/**
2425 * kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to
2426 * non protected.
2427 * @kvm: the VM whose protected vCPUs are to be converted
2428 * @rc: return value for the RC field of the UVC (in case of error)
2429 * @rrc: return value for the RRC field of the UVC (in case of error)
2430 *
2431 * Does not stop in case of error, tries to convert as many
2432 * CPUs as possible. In case of error, the RC and RRC of the last error are
2433 * returned.
2434 *
2435 * Return: 0 in case of success, otherwise -EIO
2436 */
2437int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
29b40f10
JF
2438{
2439 struct kvm_vcpu *vcpu;
46808a4c 2440 unsigned long i;
be48d86f
CI
2441 u16 _rc, _rrc;
2442 int ret = 0;
29b40f10
JF
2443
2444 /*
2445 * We ignore failures and try to destroy as many CPUs as possible.
2446 * At the same time we must not free the assigned resources when
2447 * this fails, as the ultravisor has still access to that memory.
2448 * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
2449 * behind.
2450 * We want to return the first failure rc and rrc, though.
2451 */
2452 kvm_for_each_vcpu(i, vcpu, kvm) {
2453 mutex_lock(&vcpu->mutex);
be48d86f
CI
2454 if (kvm_s390_pv_destroy_cpu(vcpu, &_rc, &_rrc) && !ret) {
2455 *rc = _rc;
2456 *rrc = _rrc;
29b40f10
JF
2457 ret = -EIO;
2458 }
2459 mutex_unlock(&vcpu->mutex);
2460 }
ee6a569d
MM
2461 /* Ensure that we re-enable gisa if the non-PV guest used it but the PV guest did not. */
2462 if (use_gisa)
2463 kvm_s390_gisa_enable(kvm);
29b40f10
JF
2464 return ret;
2465}
2466
be48d86f
CI
2467/**
2468 * kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM
2469 * to protected.
2470 * @kvm: the VM whose protected vCPUs are to be converted
2471 * @rc: return value for the RC field of the UVC (in case of error)
2472 * @rrc: return value for the RRC field of the UVC (in case of error)
2473 *
2474 * Tries to undo the conversion in case of error.
2475 *
2476 * Return: 0 in case of success, otherwise -EIO
2477 */
29b40f10
JF
2478static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
2479{
46808a4c
MZ
2480 unsigned long i;
2481 int r = 0;
29b40f10
JF
2482 u16 dummy;
2483
2484 struct kvm_vcpu *vcpu;
2485
ee6a569d 2486 /* Disable the GISA if the ultravisor does not support AIV. */
59a88140 2487 if (!uv_has_feature(BIT_UV_FEAT_AIV))
ee6a569d
MM
2488 kvm_s390_gisa_disable(kvm);
2489
29b40f10
JF
2490 kvm_for_each_vcpu(i, vcpu, kvm) {
2491 mutex_lock(&vcpu->mutex);
2492 r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
2493 mutex_unlock(&vcpu->mutex);
2494 if (r)
2495 break;
2496 }
2497 if (r)
2498 kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
2499 return r;
2500}
2501
35d02493
JF
2502/*
2503 * Here we provide user space with a direct interface to query UV
2504 * related data like UV maxima and available features as well as
2505 * feature specific data.
2506 *
2507 * To facilitate future extension of the data structures we'll try to
2508 * write data up to the maximum requested length.
2509 */
2510static ssize_t kvm_s390_handle_pv_info(struct kvm_s390_pv_info *info)
2511{
2512 ssize_t len_min;
2513
2514 switch (info->header.id) {
2515 case KVM_PV_INFO_VM: {
2516 len_min = sizeof(info->header) + sizeof(info->vm);
2517
2518 if (info->header.len_max < len_min)
2519 return -EINVAL;
2520
2521 memcpy(info->vm.inst_calls_list,
2522 uv_info.inst_calls_list,
2523 sizeof(uv_info.inst_calls_list));
2524
2525 /* It's max cpuid not max cpus, so it's off by one */
2526 info->vm.max_cpus = uv_info.max_guest_cpu_id + 1;
2527 info->vm.max_guests = uv_info.max_num_sec_conf;
2528 info->vm.max_guest_addr = uv_info.max_sec_stor_addr;
2529 info->vm.feature_indication = uv_info.uv_feature_indications;
2530
2531 return len_min;
2532 }
fe9a93e0
JF
2533 case KVM_PV_INFO_DUMP: {
2534 len_min = sizeof(info->header) + sizeof(info->dump);
2535
2536 if (info->header.len_max < len_min)
2537 return -EINVAL;
2538
2539 info->dump.dump_cpu_buffer_len = uv_info.guest_cpu_stor_len;
2540 info->dump.dump_config_mem_buffer_per_1m = uv_info.conf_dump_storage_state_len;
2541 info->dump.dump_config_finalize_len = uv_info.conf_dump_finalize_len;
2542 return len_min;
2543 }
35d02493
JF
2544 default:
2545 return -EINVAL;
2546 }
2547}
2548
0460eb35
JF
2549static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd,
2550 struct kvm_s390_pv_dmp dmp)
2551{
2552 int r = -EINVAL;
2553 void __user *result_buff = (void __user *)dmp.buff_addr;
2554
2555 switch (dmp.subcmd) {
2556 case KVM_PV_DUMP_INIT: {
2557 if (kvm->arch.pv.dumping)
2558 break;
2559
2560 /*
2561 * Block SIE entry as concurrent dump UVCs could lead
2562 * to validities.
2563 */
2564 kvm_s390_vcpu_block_all(kvm);
2565
2566 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2567 UVC_CMD_DUMP_INIT, &cmd->rc, &cmd->rrc);
2568 KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP INIT: rc %x rrc %x",
2569 cmd->rc, cmd->rrc);
2570 if (!r) {
2571 kvm->arch.pv.dumping = true;
2572 } else {
2573 kvm_s390_vcpu_unblock_all(kvm);
2574 r = -EINVAL;
2575 }
2576 break;
2577 }
2578 case KVM_PV_DUMP_CONFIG_STOR_STATE: {
2579 if (!kvm->arch.pv.dumping)
2580 break;
2581
2582 /*
2583 * gaddr is an output parameter since we might stop
2584 * early. As dmp will be copied back in our caller, we
2585 * don't need to do it ourselves.
2586 */
2587 r = kvm_s390_pv_dump_stor_state(kvm, result_buff, &dmp.gaddr, dmp.buff_len,
2588 &cmd->rc, &cmd->rrc);
2589 break;
2590 }
2591 case KVM_PV_DUMP_COMPLETE: {
2592 if (!kvm->arch.pv.dumping)
2593 break;
2594
2595 r = -EINVAL;
2596 if (dmp.buff_len < uv_info.conf_dump_finalize_len)
2597 break;
2598
2599 r = kvm_s390_pv_dump_complete(kvm, result_buff,
2600 &cmd->rc, &cmd->rrc);
2601 break;
2602 }
2603 default:
2604 r = -ENOTTY;
2605 break;
2606 }
2607
2608 return r;
2609}
2610
29b40f10
JF
2611static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
2612{
fb491d55
CI
2613 const bool need_lock = (cmd->cmd != KVM_PV_ASYNC_CLEANUP_PERFORM);
2614 void __user *argp = (void __user *)cmd->data;
29b40f10
JF
2615 int r = 0;
2616 u16 dummy;
fb491d55
CI
2617
2618 if (need_lock)
2619 mutex_lock(&kvm->lock);
29b40f10
JF
2620
2621 switch (cmd->cmd) {
2622 case KVM_PV_ENABLE: {
2623 r = -EINVAL;
2624 if (kvm_s390_pv_is_protected(kvm))
2625 break;
2626
2627 /*
2628 * FMT 4 SIE needs esca. As we never switch back to bsca from
2629 * esca, we need no cleanup in the error cases below
2630 */
2631 r = sca_switch_to_extended(kvm);
2632 if (r)
2633 break;
2634
d8ed45c5 2635 mmap_write_lock(current->mm);
fa0c5eab 2636 r = gmap_mark_unmergeable();
d8ed45c5 2637 mmap_write_unlock(current->mm);
fa0c5eab
JF
2638 if (r)
2639 break;
2640
29b40f10
JF
2641 r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
2642 if (r)
2643 break;
2644
2645 r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
2646 if (r)
2647 kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
0890ddea
CB
2648
2649 /* we need to block service interrupts from now on */
2650 set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
29b40f10
JF
2651 break;
2652 }
fb491d55
CI
2653 case KVM_PV_ASYNC_CLEANUP_PREPARE:
2654 r = -EINVAL;
2655 if (!kvm_s390_pv_is_protected(kvm) || !async_destroy)
2656 break;
2657
2658 r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
2659 /*
2660 * If a CPU could not be destroyed, destroy VM will also fail.
2661 * There is no point in trying to destroy it. Instead return
2662 * the rc and rrc from the first CPU that failed destroying.
2663 */
2664 if (r)
2665 break;
2666 r = kvm_s390_pv_set_aside(kvm, &cmd->rc, &cmd->rrc);
2667
2668 /* no need to block service interrupts any more */
2669 clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
2670 break;
2671 case KVM_PV_ASYNC_CLEANUP_PERFORM:
2672 r = -EINVAL;
2673 if (!async_destroy)
2674 break;
2675 /* kvm->lock must not be held; this is asserted inside the function. */
2676 r = kvm_s390_pv_deinit_aside_vm(kvm, &cmd->rc, &cmd->rrc);
2677 break;
29b40f10
JF
2678 case KVM_PV_DISABLE: {
2679 r = -EINVAL;
2680 if (!kvm_s390_pv_is_protected(kvm))
2681 break;
2682
2683 r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
2684 /*
2685 * If a CPU could not be destroyed, destroy VM will also fail.
2686 * There is no point in trying to destroy it. Instead return
2687 * the rc and rrc from the first CPU that failed destroying.
2688 */
2689 if (r)
2690 break;
fb491d55 2691 r = kvm_s390_pv_deinit_cleanup_all(kvm, &cmd->rc, &cmd->rrc);
0890ddea
CB
2692
2693 /* no need to block service interrupts any more */
2694 clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
29b40f10
JF
2695 break;
2696 }
2697 case KVM_PV_SET_SEC_PARMS: {
2698 struct kvm_s390_pv_sec_parm parms = {};
2699 void *hdr;
2700
2701 r = -EINVAL;
2702 if (!kvm_s390_pv_is_protected(kvm))
2703 break;
2704
2705 r = -EFAULT;
2706 if (copy_from_user(&parms, argp, sizeof(parms)))
2707 break;
2708
2709 /* Currently restricted to 8KB */
2710 r = -EINVAL;
2711 if (parms.length > PAGE_SIZE * 2)
2712 break;
2713
2714 r = -ENOMEM;
2715 hdr = vmalloc(parms.length);
2716 if (!hdr)
2717 break;
2718
2719 r = -EFAULT;
2720 if (!copy_from_user(hdr, (void __user *)parms.origin,
2721 parms.length))
2722 r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
2723 &cmd->rc, &cmd->rrc);
2724
2725 vfree(hdr);
2726 break;
2727 }
2728 case KVM_PV_UNPACK: {
2729 struct kvm_s390_pv_unp unp = {};
2730
2731 r = -EINVAL;
1ed576a2 2732 if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm))
29b40f10
JF
2733 break;
2734
2735 r = -EFAULT;
2736 if (copy_from_user(&unp, argp, sizeof(unp)))
2737 break;
2738
2739 r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
2740 &cmd->rc, &cmd->rrc);
2741 break;
2742 }
2743 case KVM_PV_VERIFY: {
2744 r = -EINVAL;
2745 if (!kvm_s390_pv_is_protected(kvm))
2746 break;
2747
2748 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2749 UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
2750 KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
2751 cmd->rrc);
2752 break;
2753 }
e0d2773d
JF
2754 case KVM_PV_PREP_RESET: {
2755 r = -EINVAL;
2756 if (!kvm_s390_pv_is_protected(kvm))
2757 break;
2758
2759 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2760 UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc);
2761 KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x",
2762 cmd->rc, cmd->rrc);
2763 break;
2764 }
2765 case KVM_PV_UNSHARE_ALL: {
2766 r = -EINVAL;
2767 if (!kvm_s390_pv_is_protected(kvm))
2768 break;
2769
2770 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2771 UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc);
2772 KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x",
2773 cmd->rc, cmd->rrc);
2774 break;
2775 }
35d02493
JF
2776 case KVM_PV_INFO: {
2777 struct kvm_s390_pv_info info = {};
2778 ssize_t data_len;
2779
2780 /*
2781 * No need to check the VM protection here.
2782 *
2783 * Maybe user space wants to query some of the data
2784 * when the VM is still unprotected. If we see the
2785 * need to fence a new data command we can still
2786 * return an error in the info handler.
2787 */
2788
2789 r = -EFAULT;
2790 if (copy_from_user(&info, argp, sizeof(info.header)))
2791 break;
2792
2793 r = -EINVAL;
2794 if (info.header.len_max < sizeof(info.header))
2795 break;
2796
2797 data_len = kvm_s390_handle_pv_info(&info);
2798 if (data_len < 0) {
2799 r = data_len;
2800 break;
2801 }
2802 /*
2803 * If a data command struct is extended (multiple
2804 * times) this can be used to determine how much of it
2805 * is valid.
2806 */
2807 info.header.len_written = data_len;
2808
2809 r = -EFAULT;
2810 if (copy_to_user(argp, &info, data_len))
2811 break;
2812
2813 r = 0;
2814 break;
2815 }
0460eb35
JF
2816 case KVM_PV_DUMP: {
2817 struct kvm_s390_pv_dmp dmp;
2818
2819 r = -EINVAL;
2820 if (!kvm_s390_pv_is_protected(kvm))
2821 break;
2822
2823 r = -EFAULT;
2824 if (copy_from_user(&dmp, argp, sizeof(dmp)))
2825 break;
2826
2827 r = kvm_s390_pv_dmp(kvm, cmd, dmp);
2828 if (r)
2829 break;
2830
2831 if (copy_to_user(argp, &dmp, sizeof(dmp))) {
2832 r = -EFAULT;
2833 break;
2834 }
2835
2836 break;
2837 }
29b40f10
JF
2838 default:
2839 r = -ENOTTY;
2840 }
fb491d55
CI
2841 if (need_lock)
2842 mutex_unlock(&kvm->lock);
2843
29b40f10
JF
2844 return r;
2845}
2846
a41f505e 2847static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_flags)
ef11c946 2848{
3d9042f8 2849 if (mop->flags & ~supported_flags || !mop->size)
ef11c946
JSG
2850 return -EINVAL;
2851 if (mop->size > MEM_OP_MAX_SIZE)
2852 return -E2BIG;
ef11c946 2853 if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
a41f505e 2854 if (mop->key > 0xf)
ef11c946
JSG
2855 return -EINVAL;
2856 } else {
2857 mop->key = 0;
2858 }
a41f505e 2859 return 0;
e9e9feeb
JSG
2860}
2861
8550bcb7 2862static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop)
ef11c946
JSG
2863{
2864 void __user *uaddr = (void __user *)mop->buf;
0d6d4d23 2865 enum gacc_mode acc_mode;
ef11c946
JSG
2866 void *tmpbuf = NULL;
2867 int r, srcu_idx;
2868
a41f505e
JSG
2869 r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION |
2870 KVM_S390_MEMOP_F_CHECK_ONLY);
2871 if (r)
2872 return r;
2873
ef11c946
JSG
2874 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
2875 tmpbuf = vmalloc(mop->size);
2876 if (!tmpbuf)
2877 return -ENOMEM;
2878 }
2879
2880 srcu_idx = srcu_read_lock(&kvm->srcu);
2881
2882 if (kvm_is_error_gpa(kvm, mop->gaddr)) {
2883 r = PGM_ADDRESSING;
2884 goto out_unlock;
2885 }
2886
0d6d4d23
JSG
2887 acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE;
2888 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
2889 r = check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key);
2890 goto out_unlock;
ef11c946 2891 }
0d6d4d23
JSG
2892 if (acc_mode == GACC_FETCH) {
2893 r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
2894 mop->size, GACC_FETCH, mop->key);
2895 if (r)
2896 goto out_unlock;
2897 if (copy_to_user(uaddr, tmpbuf, mop->size))
2898 r = -EFAULT;
2899 } else {
2900 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
2901 r = -EFAULT;
2902 goto out_unlock;
ef11c946 2903 }
0d6d4d23
JSG
2904 r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
2905 mop->size, GACC_STORE, mop->key);
ef11c946
JSG
2906 }
2907
2908out_unlock:
2909 srcu_read_unlock(&kvm->srcu, srcu_idx);
2910
2911 vfree(tmpbuf);
2912 return r;
2913}
2914
3fd49805
JSG
2915static int kvm_s390_vm_mem_op_cmpxchg(struct kvm *kvm, struct kvm_s390_mem_op *mop)
2916{
2917 void __user *uaddr = (void __user *)mop->buf;
2918 void __user *old_addr = (void __user *)mop->old_addr;
2919 union {
2920 __uint128_t quad;
2921 char raw[sizeof(__uint128_t)];
2922 } old = { .quad = 0}, new = { .quad = 0 };
2923 unsigned int off_in_quad = sizeof(new) - mop->size;
2924 int r, srcu_idx;
2925 bool success;
2926
2927 r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION);
2928 if (r)
2929 return r;
2930 /*
2931 * This validates off_in_quad. Checking that size is a power
2932 * of two is not necessary, as cmpxchg_guest_abs_with_key
2933 * takes care of that
2934 */
2935 if (mop->size > sizeof(new))
2936 return -EINVAL;
2937 if (copy_from_user(&new.raw[off_in_quad], uaddr, mop->size))
2938 return -EFAULT;
2939 if (copy_from_user(&old.raw[off_in_quad], old_addr, mop->size))
2940 return -EFAULT;
2941
2942 srcu_idx = srcu_read_lock(&kvm->srcu);
2943
2944 if (kvm_is_error_gpa(kvm, mop->gaddr)) {
2945 r = PGM_ADDRESSING;
2946 goto out_unlock;
2947 }
2948
2949 r = cmpxchg_guest_abs_with_key(kvm, mop->gaddr, mop->size, &old.quad,
2950 new.quad, mop->key, &success);
2951 if (!success && copy_to_user(old_addr, &old.raw[off_in_quad], mop->size))
2952 r = -EFAULT;
2953
2954out_unlock:
2955 srcu_read_unlock(&kvm->srcu, srcu_idx);
2956 return r;
2957}
2958
8550bcb7
JSG
2959static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
2960{
2961 /*
2962 * This is technically a heuristic only, if the kvm->lock is not
2963 * taken, it is not guaranteed that the vm is/remains non-protected.
2964 * This is ok from a kernel perspective, wrongdoing is detected
2965 * on the access, -EFAULT is returned and the vm may crash the
2966 * next time it accesses the memory in question.
2967 * There is no sane usecase to do switching and a memop on two
2968 * different CPUs at the same time.
2969 */
2970 if (kvm_s390_pv_get_handle(kvm))
2971 return -EINVAL;
2972
2973 switch (mop->op) {
2974 case KVM_S390_MEMOP_ABSOLUTE_READ:
2975 case KVM_S390_MEMOP_ABSOLUTE_WRITE:
2976 return kvm_s390_vm_mem_op_abs(kvm, mop);
3fd49805
JSG
2977 case KVM_S390_MEMOP_ABSOLUTE_CMPXCHG:
2978 return kvm_s390_vm_mem_op_cmpxchg(kvm, mop);
8550bcb7
JSG
2979 default:
2980 return -EINVAL;
2981 }
2982}
2983
d8708b80 2984int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
b0c632db
HC
2985{
2986 struct kvm *kvm = filp->private_data;
2987 void __user *argp = (void __user *)arg;
f2061656 2988 struct kvm_device_attr attr;
b0c632db
HC
2989 int r;
2990
2991 switch (ioctl) {
ba5c1e9b
CO
2992 case KVM_S390_INTERRUPT: {
2993 struct kvm_s390_interrupt s390int;
2994
2995 r = -EFAULT;
2996 if (copy_from_user(&s390int, argp, sizeof(s390int)))
2997 break;
2998 r = kvm_s390_inject_vm(kvm, &s390int);
2999 break;
3000 }
84223598
CH
3001 case KVM_CREATE_IRQCHIP: {
3002 struct kvm_irq_routing_entry routing;
3003
3004 r = -EINVAL;
3005 if (kvm->arch.use_irqchip) {
3006 /* Set up dummy routing. */
3007 memset(&routing, 0, sizeof(routing));
152b2839 3008 r = kvm_set_irq_routing(kvm, &routing, 0, 0);
84223598
CH
3009 }
3010 break;
3011 }
f2061656
DD
3012 case KVM_SET_DEVICE_ATTR: {
3013 r = -EFAULT;
3014 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
3015 break;
3016 r = kvm_s390_vm_set_attr(kvm, &attr);
3017 break;
3018 }
3019 case KVM_GET_DEVICE_ATTR: {
3020 r = -EFAULT;
3021 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
3022 break;
3023 r = kvm_s390_vm_get_attr(kvm, &attr);
3024 break;
3025 }
3026 case KVM_HAS_DEVICE_ATTR: {
3027 r = -EFAULT;
3028 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
3029 break;
3030 r = kvm_s390_vm_has_attr(kvm, &attr);
3031 break;
3032 }
30ee2a98
JH
3033 case KVM_S390_GET_SKEYS: {
3034 struct kvm_s390_skeys args;
3035
3036 r = -EFAULT;
3037 if (copy_from_user(&args, argp,
3038 sizeof(struct kvm_s390_skeys)))
3039 break;
3040 r = kvm_s390_get_skeys(kvm, &args);
3041 break;
3042 }
3043 case KVM_S390_SET_SKEYS: {
3044 struct kvm_s390_skeys args;
3045
3046 r = -EFAULT;
3047 if (copy_from_user(&args, argp,
3048 sizeof(struct kvm_s390_skeys)))
3049 break;
3050 r = kvm_s390_set_skeys(kvm, &args);
3051 break;
3052 }
4036e387
CI
3053 case KVM_S390_GET_CMMA_BITS: {
3054 struct kvm_s390_cmma_log args;
3055
3056 r = -EFAULT;
3057 if (copy_from_user(&args, argp, sizeof(args)))
3058 break;
1de1ea7e 3059 mutex_lock(&kvm->slots_lock);
4036e387 3060 r = kvm_s390_get_cmma_bits(kvm, &args);
1de1ea7e 3061 mutex_unlock(&kvm->slots_lock);
4036e387
CI
3062 if (!r) {
3063 r = copy_to_user(argp, &args, sizeof(args));
3064 if (r)
3065 r = -EFAULT;
3066 }
3067 break;
3068 }
3069 case KVM_S390_SET_CMMA_BITS: {
3070 struct kvm_s390_cmma_log args;
3071
3072 r = -EFAULT;
3073 if (copy_from_user(&args, argp, sizeof(args)))
3074 break;
1de1ea7e 3075 mutex_lock(&kvm->slots_lock);
4036e387 3076 r = kvm_s390_set_cmma_bits(kvm, &args);
1de1ea7e 3077 mutex_unlock(&kvm->slots_lock);
4036e387
CI
3078 break;
3079 }
29b40f10
JF
3080 case KVM_S390_PV_COMMAND: {
3081 struct kvm_pv_cmd args;
3082
67cf68b6
EF
3083 /* protvirt means user cpu state */
3084 kvm_s390_set_user_cpu_state_ctrl(kvm);
29b40f10
JF
3085 r = 0;
3086 if (!is_prot_virt_host()) {
3087 r = -EINVAL;
3088 break;
3089 }
3090 if (copy_from_user(&args, argp, sizeof(args))) {
3091 r = -EFAULT;
3092 break;
3093 }
3094 if (args.flags) {
3095 r = -EINVAL;
3096 break;
3097 }
fb491d55 3098 /* must be called without kvm->lock */
29b40f10 3099 r = kvm_s390_handle_pv(kvm, &args);
29b40f10
JF
3100 if (copy_to_user(argp, &args, sizeof(args))) {
3101 r = -EFAULT;
3102 break;
3103 }
3104 break;
3105 }
ef11c946
JSG
3106 case KVM_S390_MEM_OP: {
3107 struct kvm_s390_mem_op mem_op;
3108
3109 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
3110 r = kvm_s390_vm_mem_op(kvm, &mem_op);
3111 else
3112 r = -EFAULT;
3113 break;
3114 }
db1c875e
MR
3115 case KVM_S390_ZPCI_OP: {
3116 struct kvm_s390_zpci_op args;
3117
3118 r = -EINVAL;
3119 if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
3120 break;
3121 if (copy_from_user(&args, argp, sizeof(args))) {
3122 r = -EFAULT;
3123 break;
3124 }
3125 r = kvm_s390_pci_zpci_op(kvm, &args);
3126 break;
3127 }
b0c632db 3128 default:
367e1319 3129 r = -ENOTTY;
b0c632db
HC
3130 }
3131
3132 return r;
3133}
3134
45c9b47c
TK
3135static int kvm_s390_apxa_installed(void)
3136{
e585b24a 3137 struct ap_config_info info;
45c9b47c 3138
e585b24a
TK
3139 if (ap_instructions_available()) {
3140 if (ap_qci(&info) == 0)
3141 return info.apxa;
45c9b47c
TK
3142 }
3143
3144 return 0;
3145}
3146
e585b24a
TK
3147/*
3148 * The format of the crypto control block (CRYCB) is specified in the 3 low
3149 * order bits of the CRYCB designation (CRYCBD) field as follows:
3150 * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
3151 * AP extended addressing (APXA) facility are installed.
3152 * Format 1: The APXA facility is not installed but the MSAX3 facility is.
3153 * Format 2: Both the APXA and MSAX3 facilities are installed
3154 */
45c9b47c
TK
3155static void kvm_s390_set_crycb_format(struct kvm *kvm)
3156{
3157 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
3158
e585b24a
TK
3159 /* Clear the CRYCB format bits - i.e., set format 0 by default */
3160 kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
3161
3162 /* Check whether MSAX3 is installed */
3163 if (!test_kvm_facility(kvm, 76))
3164 return;
3165
45c9b47c
TK
3166 if (kvm_s390_apxa_installed())
3167 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
3168 else
3169 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
3170}
3171
86956e70
TK
3172/*
3173 * kvm_arch_crypto_set_masks
3174 *
3175 * @kvm: pointer to the target guest's KVM struct containing the crypto masks
3176 * to be set.
3177 * @apm: the mask identifying the accessible AP adapters
3178 * @aqm: the mask identifying the accessible AP domains
3179 * @adm: the mask identifying the accessible AP control domains
3180 *
3181 * Set the masks that identify the adapters, domains and control domains to
3182 * which the KVM guest is granted access.
3183 *
3184 * Note: The kvm->lock mutex must be locked by the caller before invoking this
3185 * function.
3186 */
0e237e44
PM
3187void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
3188 unsigned long *aqm, unsigned long *adm)
3189{
3190 struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
3191
0e237e44
PM
3192 kvm_s390_vcpu_block_all(kvm);
3193
3194 switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
3195 case CRYCB_FORMAT2: /* APCB1 use 256 bits */
3196 memcpy(crycb->apcb1.apm, apm, 32);
3197 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
3198 apm[0], apm[1], apm[2], apm[3]);
3199 memcpy(crycb->apcb1.aqm, aqm, 32);
3200 VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
3201 aqm[0], aqm[1], aqm[2], aqm[3]);
3202 memcpy(crycb->apcb1.adm, adm, 32);
3203 VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
3204 adm[0], adm[1], adm[2], adm[3]);
3205 break;
3206 case CRYCB_FORMAT1:
3207 case CRYCB_FORMAT0: /* Fall through both use APCB0 */
3208 memcpy(crycb->apcb0.apm, apm, 8);
3209 memcpy(crycb->apcb0.aqm, aqm, 2);
3210 memcpy(crycb->apcb0.adm, adm, 2);
3211 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
3212 apm[0], *((unsigned short *)aqm),
3213 *((unsigned short *)adm));
3214 break;
3215 default: /* Can not happen */
3216 break;
3217 }
3218
3219 /* recreate the shadow crycb for each vcpu */
3220 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
3221 kvm_s390_vcpu_unblock_all(kvm);
0e237e44
PM
3222}
3223EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
3224
86956e70
TK
3225/*
3226 * kvm_arch_crypto_clear_masks
3227 *
3228 * @kvm: pointer to the target guest's KVM struct containing the crypto masks
3229 * to be cleared.
3230 *
3231 * Clear the masks that identify the adapters, domains and control domains to
3232 * which the KVM guest is granted access.
3233 *
3234 * Note: The kvm->lock mutex must be locked by the caller before invoking this
3235 * function.
3236 */
42104598
TK
3237void kvm_arch_crypto_clear_masks(struct kvm *kvm)
3238{
42104598
TK
3239 kvm_s390_vcpu_block_all(kvm);
3240
3241 memset(&kvm->arch.crypto.crycb->apcb0, 0,
3242 sizeof(kvm->arch.crypto.crycb->apcb0));
3243 memset(&kvm->arch.crypto.crycb->apcb1, 0,
3244 sizeof(kvm->arch.crypto.crycb->apcb1));
3245
0e237e44 3246 VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
6cc571b1
PM
3247 /* recreate the shadow crycb for each vcpu */
3248 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
42104598 3249 kvm_s390_vcpu_unblock_all(kvm);
42104598
TK
3250}
3251EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
3252
9bb0ec09 3253static u64 kvm_s390_get_initial_cpuid(void)
9d8d5786 3254{
9bb0ec09
DH
3255 struct cpuid cpuid;
3256
3257 get_cpu_id(&cpuid);
3258 cpuid.version = 0xff;
3259 return *((u64 *) &cpuid);
9d8d5786
MM
3260}
3261
c54f0d6a 3262static void kvm_s390_crypto_init(struct kvm *kvm)
5102ee87 3263{
c54f0d6a 3264 kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
45c9b47c 3265 kvm_s390_set_crycb_format(kvm);
1e753732 3266 init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
5102ee87 3267
e585b24a
TK
3268 if (!test_kvm_facility(kvm, 76))
3269 return;
3270
ed6f76b4
TK
3271 /* Enable AES/DEA protected key functions by default */
3272 kvm->arch.crypto.aes_kw = 1;
3273 kvm->arch.crypto.dea_kw = 1;
3274 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
3275 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
3276 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
3277 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
5102ee87
TK
3278}
3279
7d43bafc
ED
3280static void sca_dispose(struct kvm *kvm)
3281{
3282 if (kvm->arch.use_esca)
5e044315 3283 free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
7d43bafc
ED
3284 else
3285 free_page((unsigned long)(kvm->arch.sca));
3286 kvm->arch.sca = NULL;
3287}
3288
09340b2f
MR
3289void kvm_arch_free_vm(struct kvm *kvm)
3290{
3291 if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
3292 kvm_s390_pci_clear_list(kvm);
3293
3294 __kvm_arch_free_vm(kvm);
3295}
3296
e08b9637 3297int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
b0c632db 3298{
c4196218 3299 gfp_t alloc_flags = GFP_KERNEL_ACCOUNT;
9d8d5786 3300 int i, rc;
b0c632db 3301 char debug_name[16];
f6c137ff 3302 static unsigned long sca_offset;
b0c632db 3303
e08b9637
CO
3304 rc = -EINVAL;
3305#ifdef CONFIG_KVM_S390_UCONTROL
3306 if (type & ~KVM_VM_S390_UCONTROL)
3307 goto out_err;
3308 if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
3309 goto out_err;
3310#else
3311 if (type)
3312 goto out_err;
3313#endif
3314
b0c632db
HC
3315 rc = s390_enable_sie();
3316 if (rc)
d89f5eff 3317 goto out_err;
b0c632db 3318
b290411a
CO
3319 rc = -ENOMEM;
3320
76a6dd72
DH
3321 if (!sclp.has_64bscao)
3322 alloc_flags |= GFP_DMA;
5e044315 3323 rwlock_init(&kvm->arch.sca_lock);
9ac96d75 3324 /* start with basic SCA */
76a6dd72 3325 kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
b0c632db 3326 if (!kvm->arch.sca)
d89f5eff 3327 goto out_err;
0d9ce162 3328 mutex_lock(&kvm_lock);
c5c2c393 3329 sca_offset += 16;
bc784cce 3330 if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
c5c2c393 3331 sca_offset = 0;
bc784cce
ED
3332 kvm->arch.sca = (struct bsca_block *)
3333 ((char *) kvm->arch.sca + sca_offset);
0d9ce162 3334 mutex_unlock(&kvm_lock);
b0c632db
HC
3335
3336 sprintf(debug_name, "kvm-%u", current->pid);
3337
1cb9cf72 3338 kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
b0c632db 3339 if (!kvm->arch.dbf)
40f5b735 3340 goto out_err;
b0c632db 3341
19114beb 3342 BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
c54f0d6a 3343 kvm->arch.sie_page2 =
c4196218 3344 (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA);
c54f0d6a 3345 if (!kvm->arch.sie_page2)
40f5b735 3346 goto out_err;
9d8d5786 3347
25c84dba 3348 kvm->arch.sie_page2->kvm = kvm;
c54f0d6a 3349 kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
c3b9e3e1
CB
3350
3351 for (i = 0; i < kvm_s390_fac_size(); i++) {
17e89e13 3352 kvm->arch.model.fac_mask[i] = stfle_fac_list[i] &
c3b9e3e1
CB
3353 (kvm_s390_fac_base[i] |
3354 kvm_s390_fac_ext[i]);
17e89e13 3355 kvm->arch.model.fac_list[i] = stfle_fac_list[i] &
c3b9e3e1
CB
3356 kvm_s390_fac_base[i];
3357 }
346fa2f8 3358 kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
981467c9 3359
1935222d
DH
3360 /* we are always in czam mode - even on pre z14 machines */
3361 set_kvm_facility(kvm->arch.model.fac_mask, 138);
3362 set_kvm_facility(kvm->arch.model.fac_list, 138);
3363 /* we emulate STHYI in kvm */
95ca2cb5
JF
3364 set_kvm_facility(kvm->arch.model.fac_mask, 74);
3365 set_kvm_facility(kvm->arch.model.fac_list, 74);
1bab1c02
CI
3366 if (MACHINE_HAS_TLB_GUEST) {
3367 set_kvm_facility(kvm->arch.model.fac_mask, 147);
3368 set_kvm_facility(kvm->arch.model.fac_list, 147);
3369 }
95ca2cb5 3370
05f31e3b
PM
3371 if (css_general_characteristics.aiv && test_facility(65))
3372 set_kvm_facility(kvm->arch.model.fac_mask, 65);
3373
9bb0ec09 3374 kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
37c5f6c8 3375 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
9d8d5786 3376
19c654bf
SE
3377 kvm->arch.model.uv_feat_guest.feat = 0;
3378
c54f0d6a 3379 kvm_s390_crypto_init(kvm);
5102ee87 3380
09340b2f
MR
3381 if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
3382 mutex_lock(&kvm->lock);
3383 kvm_s390_pci_init_list(kvm);
3384 kvm_s390_vcpu_pci_enable_interp(kvm);
3385 mutex_unlock(&kvm->lock);
3386 }
3387
51978393 3388 mutex_init(&kvm->arch.float_int.ais_lock);
ba5c1e9b 3389 spin_lock_init(&kvm->arch.float_int.lock);
6d3da241
JF
3390 for (i = 0; i < FIRQ_LIST_COUNT; i++)
3391 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
8a242234 3392 init_waitqueue_head(&kvm->arch.ipte_wq);
a6b7e459 3393 mutex_init(&kvm->arch.ipte_mutex);
ba5c1e9b 3394
b0c632db 3395 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
78f26131 3396 VM_EVENT(kvm, 3, "vm created with type %lu", type);
b0c632db 3397
e08b9637
CO
3398 if (type & KVM_VM_S390_UCONTROL) {
3399 kvm->arch.gmap = NULL;
a3a92c31 3400 kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
e08b9637 3401 } else {
32e6b236 3402 if (sclp.hamax == U64_MAX)
ee71d16d 3403 kvm->arch.mem_limit = TASK_SIZE_MAX;
32e6b236 3404 else
ee71d16d 3405 kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
32e6b236 3406 sclp.hamax + 1);
6ea427bb 3407 kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
e08b9637 3408 if (!kvm->arch.gmap)
40f5b735 3409 goto out_err;
2c70fe44 3410 kvm->arch.gmap->private = kvm;
24eb3a82 3411 kvm->arch.gmap->pfault_enabled = 0;
e08b9637 3412 }
fa6b7fe9 3413
c9f0a2b8 3414 kvm->arch.use_pfmfi = sclp.has_pfmfi;
55531b74 3415 kvm->arch.use_skf = sclp.has_skey;
8ad35755 3416 spin_lock_init(&kvm->arch.start_stop_lock);
a3508fbe 3417 kvm_s390_vsie_init(kvm);
cc674ef2
MM
3418 if (use_gisa)
3419 kvm_s390_gisa_init(kvm);
fb491d55
CI
3420 INIT_LIST_HEAD(&kvm->arch.pv.need_cleanup);
3421 kvm->arch.pv.set_aside = NULL;
8335713a 3422 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
8ad35755 3423
d89f5eff 3424 return 0;
40f5b735 3425out_err:
c54f0d6a 3426 free_page((unsigned long)kvm->arch.sie_page2);
598841ca 3427 debug_unregister(kvm->arch.dbf);
7d43bafc 3428 sca_dispose(kvm);
78f26131 3429 KVM_EVENT(3, "creation of vm failed: %d", rc);
d89f5eff 3430 return rc;
b0c632db
HC
3431}
3432
d329c035
CB
3433void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3434{
29b40f10
JF
3435 u16 rc, rrc;
3436
d329c035 3437 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
ade38c31 3438 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
67335e63 3439 kvm_s390_clear_local_irqs(vcpu);
3c038e6b 3440 kvm_clear_async_pf_completion_queue(vcpu);
bc784cce 3441 if (!kvm_is_ucontrol(vcpu->kvm))
a6e2f683 3442 sca_del_vcpu(vcpu);
24fe0195 3443 kvm_s390_update_topology_change_report(vcpu->kvm, 1);
27e0393f
CO
3444
3445 if (kvm_is_ucontrol(vcpu->kvm))
6ea427bb 3446 gmap_remove(vcpu->arch.gmap);
27e0393f 3447
e6db1d61 3448 if (vcpu->kvm->arch.use_cmma)
b31605c1 3449 kvm_s390_vcpu_unsetup_cmma(vcpu);
29b40f10
JF
3450 /* We can not hold the vcpu mutex here, we are already dying */
3451 if (kvm_s390_pv_cpu_get_handle(vcpu))
3452 kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
d329c035 3453 free_page((unsigned long)(vcpu->arch.sie_block));
d329c035
CB
3454}
3455
b0c632db
HC
3456void kvm_arch_destroy_vm(struct kvm *kvm)
3457{
29b40f10
JF
3458 u16 rc, rrc;
3459
27592ae8 3460 kvm_destroy_vcpus(kvm);
7d43bafc 3461 sca_dispose(kvm);
d7c5cb01 3462 kvm_s390_gisa_destroy(kvm);
29b40f10
JF
3463 /*
3464 * We are already at the end of life and kvm->lock is not taken.
3465 * This is ok as the file descriptor is closed by now and nobody
fb491d55 3466 * can mess with the pv state.
29b40f10 3467 */
fb491d55 3468 kvm_s390_pv_deinit_cleanup_all(kvm, &rc, &rrc);
ca2fd060
CI
3469 /*
3470 * Remove the mmu notifier only when the whole KVM VM is torn down,
3471 * and only if one was registered to begin with. If the VM is
3472 * currently not protected, but has been previously been protected,
3473 * then it's possible that the notifier is still registered.
3474 */
3475 if (kvm->arch.pv.mmu_notifier.ops)
3476 mmu_notifier_unregister(&kvm->arch.pv.mmu_notifier, kvm->mm);
3477
29b40f10 3478 debug_unregister(kvm->arch.dbf);
c54f0d6a 3479 free_page((unsigned long)kvm->arch.sie_page2);
27e0393f 3480 if (!kvm_is_ucontrol(kvm))
6ea427bb 3481 gmap_remove(kvm->arch.gmap);
841b91c5 3482 kvm_s390_destroy_adapters(kvm);
67335e63 3483 kvm_s390_clear_float_irqs(kvm);
a3508fbe 3484 kvm_s390_vsie_destroy(kvm);
8335713a 3485 KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
b0c632db
HC
3486}
3487
3488/* Section: vcpu related */
dafd032a
DD
3489static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
3490{
6ea427bb 3491 vcpu->arch.gmap = gmap_create(current->mm, -1UL);
dafd032a
DD
3492 if (!vcpu->arch.gmap)
3493 return -ENOMEM;
3494 vcpu->arch.gmap->private = vcpu->kvm;
3495
3496 return 0;
3497}
3498
a6e2f683
ED
3499static void sca_del_vcpu(struct kvm_vcpu *vcpu)
3500{
a6940674
DH
3501 if (!kvm_s390_use_sca_entries())
3502 return;
5e044315 3503 read_lock(&vcpu->kvm->arch.sca_lock);
7d43bafc
ED
3504 if (vcpu->kvm->arch.use_esca) {
3505 struct esca_block *sca = vcpu->kvm->arch.sca;
a6e2f683 3506
7d43bafc 3507 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
10ce32d5 3508 sca->cpu[vcpu->vcpu_id].sda = 0;
7d43bafc
ED
3509 } else {
3510 struct bsca_block *sca = vcpu->kvm->arch.sca;
3511
3512 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
10ce32d5 3513 sca->cpu[vcpu->vcpu_id].sda = 0;
7d43bafc 3514 }
5e044315 3515 read_unlock(&vcpu->kvm->arch.sca_lock);
a6e2f683
ED
3516}
3517
eaa78f34 3518static void sca_add_vcpu(struct kvm_vcpu *vcpu)
a6e2f683 3519{
a6940674 3520 if (!kvm_s390_use_sca_entries()) {
fe0ef003 3521 phys_addr_t sca_phys = virt_to_phys(vcpu->kvm->arch.sca);
a6940674
DH
3522
3523 /* we still need the basic sca for the ipte control */
fe0ef003
NB
3524 vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3525 vcpu->arch.sie_block->scaol = sca_phys;
f07afa04 3526 return;
a6940674 3527 }
eaa78f34
DH
3528 read_lock(&vcpu->kvm->arch.sca_lock);
3529 if (vcpu->kvm->arch.use_esca) {
3530 struct esca_block *sca = vcpu->kvm->arch.sca;
fe0ef003 3531 phys_addr_t sca_phys = virt_to_phys(sca);
7d43bafc 3532
fe0ef003
NB
3533 sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block);
3534 vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3535 vcpu->arch.sie_block->scaol = sca_phys & ESCA_SCAOL_MASK;
0c9d8683 3536 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
eaa78f34 3537 set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
7d43bafc 3538 } else {
eaa78f34 3539 struct bsca_block *sca = vcpu->kvm->arch.sca;
fe0ef003 3540 phys_addr_t sca_phys = virt_to_phys(sca);
a6e2f683 3541
fe0ef003
NB
3542 sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block);
3543 vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3544 vcpu->arch.sie_block->scaol = sca_phys;
eaa78f34 3545 set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
7d43bafc 3546 }
eaa78f34 3547 read_unlock(&vcpu->kvm->arch.sca_lock);
5e044315
ED
3548}
3549
3550/* Basic SCA to Extended SCA data copy routines */
3551static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
3552{
3553 d->sda = s->sda;
3554 d->sigp_ctrl.c = s->sigp_ctrl.c;
3555 d->sigp_ctrl.scn = s->sigp_ctrl.scn;
3556}
3557
3558static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
3559{
3560 int i;
3561
3562 d->ipte_control = s->ipte_control;
3563 d->mcn[0] = s->mcn;
3564 for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
3565 sca_copy_entry(&d->cpu[i], &s->cpu[i]);
3566}
3567
3568static int sca_switch_to_extended(struct kvm *kvm)
3569{
3570 struct bsca_block *old_sca = kvm->arch.sca;
3571 struct esca_block *new_sca;
3572 struct kvm_vcpu *vcpu;
46808a4c 3573 unsigned long vcpu_idx;
5e044315 3574 u32 scaol, scaoh;
fe0ef003 3575 phys_addr_t new_sca_phys;
5e044315 3576
29b40f10
JF
3577 if (kvm->arch.use_esca)
3578 return 0;
3579
c4196218 3580 new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);
5e044315
ED
3581 if (!new_sca)
3582 return -ENOMEM;
3583
fe0ef003
NB
3584 new_sca_phys = virt_to_phys(new_sca);
3585 scaoh = new_sca_phys >> 32;
3586 scaol = new_sca_phys & ESCA_SCAOL_MASK;
5e044315
ED
3587
3588 kvm_s390_vcpu_block_all(kvm);
3589 write_lock(&kvm->arch.sca_lock);
3590
3591 sca_copy_b_to_e(new_sca, old_sca);
3592
3593 kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
3594 vcpu->arch.sie_block->scaoh = scaoh;
3595 vcpu->arch.sie_block->scaol = scaol;
0c9d8683 3596 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
5e044315
ED
3597 }
3598 kvm->arch.sca = new_sca;
3599 kvm->arch.use_esca = 1;
3600
3601 write_unlock(&kvm->arch.sca_lock);
3602 kvm_s390_vcpu_unblock_all(kvm);
3603
3604 free_page((unsigned long)old_sca);
3605
8335713a
CB
3606 VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
3607 old_sca, kvm->arch.sca);
5e044315 3608 return 0;
a6e2f683
ED
3609}
3610
3611static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
3612{
5e044315
ED
3613 int rc;
3614
a6940674
DH
3615 if (!kvm_s390_use_sca_entries()) {
3616 if (id < KVM_MAX_VCPUS)
3617 return true;
3618 return false;
3619 }
5e044315
ED
3620 if (id < KVM_S390_BSCA_CPU_SLOTS)
3621 return true;
76a6dd72 3622 if (!sclp.has_esca || !sclp.has_64bscao)
5e044315
ED
3623 return false;
3624
5e044315 3625 rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
5e044315
ED
3626
3627 return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
a6e2f683
ED
3628}
3629
db0758b2
DH
3630/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3631static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3632{
3633 WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
9c23a131 3634 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
db0758b2 3635 vcpu->arch.cputm_start = get_tod_clock_fast();
9c23a131 3636 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
db0758b2
DH
3637}
3638
3639/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3640static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3641{
3642 WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
9c23a131 3643 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
db0758b2
DH
3644 vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
3645 vcpu->arch.cputm_start = 0;
9c23a131 3646 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
db0758b2
DH
3647}
3648
3649/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3650static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3651{
3652 WARN_ON_ONCE(vcpu->arch.cputm_enabled);
3653 vcpu->arch.cputm_enabled = true;
3654 __start_cpu_timer_accounting(vcpu);
3655}
3656
3657/* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3658static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3659{
3660 WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
3661 __stop_cpu_timer_accounting(vcpu);
3662 vcpu->arch.cputm_enabled = false;
3663}
3664
3665static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3666{
3667 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3668 __enable_cpu_timer_accounting(vcpu);
3669 preempt_enable();
3670}
3671
3672static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3673{
3674 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3675 __disable_cpu_timer_accounting(vcpu);
3676 preempt_enable();
3677}
3678
4287f247
DH
3679/* set the cpu timer - may only be called from the VCPU thread itself */
3680void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
3681{
db0758b2 3682 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
9c23a131 3683 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
db0758b2
DH
3684 if (vcpu->arch.cputm_enabled)
3685 vcpu->arch.cputm_start = get_tod_clock_fast();
4287f247 3686 vcpu->arch.sie_block->cputm = cputm;
9c23a131 3687 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
db0758b2 3688 preempt_enable();
4287f247
DH
3689}
3690
db0758b2 3691/* update and get the cpu timer - can also be called from other VCPU threads */
4287f247
DH
3692__u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
3693{
9c23a131 3694 unsigned int seq;
db0758b2 3695 __u64 value;
db0758b2
DH
3696
3697 if (unlikely(!vcpu->arch.cputm_enabled))
3698 return vcpu->arch.sie_block->cputm;
3699
9c23a131
DH
3700 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3701 do {
3702 seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
3703 /*
3704 * If the writer would ever execute a read in the critical
3705 * section, e.g. in irq context, we have a deadlock.
3706 */
3707 WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
3708 value = vcpu->arch.sie_block->cputm;
3709 /* if cputm_start is 0, accounting is being started/stopped */
3710 if (likely(vcpu->arch.cputm_start))
3711 value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
3712 } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
3713 preempt_enable();
db0758b2 3714 return value;
4287f247
DH
3715}
3716
b0c632db
HC
3717void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3718{
9977e886 3719
37d9df98 3720 gmap_enable(vcpu->arch.enabled_gmap);
ef8f4f49 3721 kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
5ebda316 3722 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
db0758b2 3723 __start_cpu_timer_accounting(vcpu);
01a745ac 3724 vcpu->cpu = cpu;
b0c632db
HC
3725}
3726
3727void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3728{
01a745ac 3729 vcpu->cpu = -1;
5ebda316 3730 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
db0758b2 3731 __stop_cpu_timer_accounting(vcpu);
9daecfc6 3732 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
37d9df98
DH
3733 vcpu->arch.enabled_gmap = gmap_get_enabled();
3734 gmap_disable(vcpu->arch.enabled_gmap);
9977e886 3735
b0c632db
HC
3736}
3737
31928aa5 3738void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
42897d86 3739{
72f25020 3740 mutex_lock(&vcpu->kvm->lock);
fdf03650 3741 preempt_disable();
72f25020 3742 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
d16b52cb 3743 vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
fdf03650 3744 preempt_enable();
72f25020 3745 mutex_unlock(&vcpu->kvm->lock);
25508824 3746 if (!kvm_is_ucontrol(vcpu->kvm)) {
dafd032a 3747 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
eaa78f34 3748 sca_add_vcpu(vcpu);
25508824 3749 }
6502a34c
DH
3750 if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
3751 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
37d9df98
DH
3752 /* make vcpu_load load the right gmap on the first trigger */
3753 vcpu->arch.enabled_gmap = vcpu->arch.gmap;
42897d86
MT
3754}
3755
8ec2fa52
CB
3756static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
3757{
3758 if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
3759 test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
3760 return true;
3761 return false;
3762}
3763
3764static bool kvm_has_pckmo_ecc(struct kvm *kvm)
3765{
3766 /* At least one ECC subfunction must be present */
3767 return kvm_has_pckmo_subfunc(kvm, 32) ||
3768 kvm_has_pckmo_subfunc(kvm, 33) ||
3769 kvm_has_pckmo_subfunc(kvm, 34) ||
3770 kvm_has_pckmo_subfunc(kvm, 40) ||
3771 kvm_has_pckmo_subfunc(kvm, 41);
3772
3773}
3774
5102ee87
TK
3775static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
3776{
e585b24a
TK
3777 /*
3778 * If the AP instructions are not being interpreted and the MSAX3
3779 * facility is not configured for the guest, there is nothing to set up.
3780 */
3781 if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
5102ee87
TK
3782 return;
3783
e585b24a 3784 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
a374e892 3785 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
37940fb0 3786 vcpu->arch.sie_block->eca &= ~ECA_APIE;
8ec2fa52 3787 vcpu->arch.sie_block->ecd &= ~ECD_ECC;
a374e892 3788
e585b24a
TK
3789 if (vcpu->kvm->arch.crypto.apie)
3790 vcpu->arch.sie_block->eca |= ECA_APIE;
a374e892 3791
e585b24a 3792 /* Set up protected key support */
8ec2fa52 3793 if (vcpu->kvm->arch.crypto.aes_kw) {
a374e892 3794 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
8ec2fa52
CB
3795 /* ecc is also wrapped with AES key */
3796 if (kvm_has_pckmo_ecc(vcpu->kvm))
3797 vcpu->arch.sie_block->ecd |= ECD_ECC;
3798 }
3799
a374e892
TK
3800 if (vcpu->kvm->arch.crypto.dea_kw)
3801 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
5102ee87
TK
3802}
3803
b31605c1
DD
3804void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
3805{
fe0ef003 3806 free_page((unsigned long)phys_to_virt(vcpu->arch.sie_block->cbrlo));
b31605c1
DD
3807 vcpu->arch.sie_block->cbrlo = 0;
3808}
3809
3810int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
3811{
fe0ef003
NB
3812 void *cbrlo_page = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3813
3814 if (!cbrlo_page)
b31605c1 3815 return -ENOMEM;
fe0ef003
NB
3816
3817 vcpu->arch.sie_block->cbrlo = virt_to_phys(cbrlo_page);
b31605c1
DD
3818 return 0;
3819}
3820
91520f1a
MM
3821static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
3822{
3823 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
3824
91520f1a 3825 vcpu->arch.sie_block->ibc = model->ibc;
80bc79dc 3826 if (test_kvm_facility(vcpu->kvm, 7))
fe0ef003 3827 vcpu->arch.sie_block->fac = virt_to_phys(model->fac_list);
91520f1a
MM
3828}
3829
ff72bb55 3830static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
b0c632db 3831{
b31605c1 3832 int rc = 0;
29b40f10 3833 u16 uvrc, uvrrc;
b31288fa 3834
9e6dabef
CH
3835 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
3836 CPUSTAT_SM |
a4a4f191
GH
3837 CPUSTAT_STOPPED);
3838
53df84f8 3839 if (test_kvm_facility(vcpu->kvm, 78))
ef8f4f49 3840 kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
53df84f8 3841 else if (test_kvm_facility(vcpu->kvm, 8))
ef8f4f49 3842 kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
a4a4f191 3843
91520f1a
MM
3844 kvm_s390_vcpu_setup_model(vcpu);
3845
bdab09f3
DH
3846 /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
3847 if (MACHINE_HAS_ESOP)
0c9d8683 3848 vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
bd50e8ec 3849 if (test_kvm_facility(vcpu->kvm, 9))
0c9d8683 3850 vcpu->arch.sie_block->ecb |= ECB_SRSI;
24fe0195
PM
3851 if (test_kvm_facility(vcpu->kvm, 11))
3852 vcpu->arch.sie_block->ecb |= ECB_PTF;
f597d24e 3853 if (test_kvm_facility(vcpu->kvm, 73))
0c9d8683 3854 vcpu->arch.sie_block->ecb |= ECB_TE;
7119decf
JSG
3855 if (!kvm_is_ucontrol(vcpu->kvm))
3856 vcpu->arch.sie_block->ecb |= ECB_SPECI;
7feb6bb8 3857
c9f0a2b8 3858 if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
0c9d8683 3859 vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
cd1836f5 3860 if (test_kvm_facility(vcpu->kvm, 130))
0c9d8683
DH
3861 vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
3862 vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
48ee7d3a 3863 if (sclp.has_cei)
0c9d8683 3864 vcpu->arch.sie_block->eca |= ECA_CEI;
11ad65b7 3865 if (sclp.has_ib)
0c9d8683 3866 vcpu->arch.sie_block->eca |= ECA_IB;
37c5f6c8 3867 if (sclp.has_siif)
0c9d8683 3868 vcpu->arch.sie_block->eca |= ECA_SII;
37c5f6c8 3869 if (sclp.has_sigpif)
0c9d8683 3870 vcpu->arch.sie_block->eca |= ECA_SIGPI;
18280d8b 3871 if (test_kvm_facility(vcpu->kvm, 129)) {
0c9d8683
DH
3872 vcpu->arch.sie_block->eca |= ECA_VX;
3873 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
13211ea7 3874 }
8fa1696e
CW
3875 if (test_kvm_facility(vcpu->kvm, 139))
3876 vcpu->arch.sie_block->ecd |= ECD_MEF;
a3da7b4a
CB
3877 if (test_kvm_facility(vcpu->kvm, 156))
3878 vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
d7c5cb01
MM
3879 if (vcpu->arch.sie_block->gd) {
3880 vcpu->arch.sie_block->eca |= ECA_AIV;
3881 VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3882 vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3883 }
fe0ef003
NB
3884 vcpu->arch.sie_block->sdnxo = virt_to_phys(&vcpu->run->s.regs.sdnx) | SDNXC;
3885 vcpu->arch.sie_block->riccbd = virt_to_phys(&vcpu->run->s.regs.riccb);
730cd632
FA
3886
3887 if (sclp.has_kss)
ef8f4f49 3888 kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
730cd632
FA
3889 else
3890 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
5a5e6536 3891
e6db1d61 3892 if (vcpu->kvm->arch.use_cmma) {
b31605c1
DD
3893 rc = kvm_s390_vcpu_setup_cmma(vcpu);
3894 if (rc)
3895 return rc;
b31288fa 3896 }
0ac96caf 3897 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
ca872302 3898 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
9d8d5786 3899
67d49d52
CW
3900 vcpu->arch.sie_block->hpid = HPID_KVM;
3901
5102ee87
TK
3902 kvm_s390_vcpu_crypto_setup(vcpu);
3903
3f4bbb43
MR
3904 kvm_s390_vcpu_pci_setup(vcpu);
3905
29b40f10
JF
3906 mutex_lock(&vcpu->kvm->lock);
3907 if (kvm_s390_pv_is_protected(vcpu->kvm)) {
3908 rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
3909 if (rc)
3910 kvm_s390_vcpu_unsetup_cmma(vcpu);
3911 }
3912 mutex_unlock(&vcpu->kvm->lock);
3913
b31605c1 3914 return rc;
b0c632db
HC
3915}
3916
897cc38e
SC
3917int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3918{
3919 if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
3920 return -EINVAL;
3921 return 0;
3922}
3923
e529ef66 3924int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
b0c632db 3925{
7feb6bb8 3926 struct sie_page *sie_page;
897cc38e 3927 int rc;
4d47555a 3928
da72ca4d 3929 BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
c4196218 3930 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT);
7feb6bb8 3931 if (!sie_page)
e529ef66 3932 return -ENOMEM;
b0c632db 3933
7feb6bb8 3934 vcpu->arch.sie_block = &sie_page->sie_block;
fe0ef003 3935 vcpu->arch.sie_block->itdba = virt_to_phys(&sie_page->itdb);
7feb6bb8 3936
efed1104
DH
3937 /* the real guest size will always be smaller than msl */
3938 vcpu->arch.sie_block->mso = 0;
3939 vcpu->arch.sie_block->msl = sclp.hamax;
3940
e529ef66 3941 vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
ba5c1e9b 3942 spin_lock_init(&vcpu->arch.local_int.lock);
ee6a569d 3943 vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm);
9c23a131 3944 seqcount_init(&vcpu->arch.cputm_seqcount);
ba5c1e9b 3945
321f8ee5
SC
3946 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3947 kvm_clear_async_pf_completion_queue(vcpu);
3948 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
3949 KVM_SYNC_GPRS |
3950 KVM_SYNC_ACRS |
3951 KVM_SYNC_CRS |
3952 KVM_SYNC_ARCH0 |
23a60f83
CW
3953 KVM_SYNC_PFAULT |
3954 KVM_SYNC_DIAG318;
321f8ee5
SC
3955 kvm_s390_set_prefix(vcpu, 0);
3956 if (test_kvm_facility(vcpu->kvm, 64))
3957 vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
3958 if (test_kvm_facility(vcpu->kvm, 82))
3959 vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
3960 if (test_kvm_facility(vcpu->kvm, 133))
3961 vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
3962 if (test_kvm_facility(vcpu->kvm, 156))
3963 vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
3964 /* fprs can be synchronized via vrs, even if the guest has no vx. With
3965 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
3966 */
3967 if (MACHINE_HAS_VX)
3968 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
3969 else
3970 vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
3971
3972 if (kvm_is_ucontrol(vcpu->kvm)) {
3973 rc = __kvm_ucontrol_vcpu_init(vcpu);
3974 if (rc)
a2017f17 3975 goto out_free_sie_block;
321f8ee5
SC
3976 }
3977
e529ef66
SC
3978 VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
3979 vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3980 trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3981
ff72bb55
SC
3982 rc = kvm_s390_vcpu_setup(vcpu);
3983 if (rc)
3984 goto out_ucontrol_uninit;
24fe0195
PM
3985
3986 kvm_s390_update_topology_change_report(vcpu->kvm, 1);
e529ef66 3987 return 0;
b0c632db 3988
ff72bb55
SC
3989out_ucontrol_uninit:
3990 if (kvm_is_ucontrol(vcpu->kvm))
3991 gmap_remove(vcpu->arch.gmap);
7b06bf2f
WY
3992out_free_sie_block:
3993 free_page((unsigned long)(vcpu->arch.sie_block));
e529ef66 3994 return rc;
b0c632db
HC
3995}
3996
b0c632db
HC
3997int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3998{
9b57e9d5 3999 clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
9a022067 4000 return kvm_s390_vcpu_has_irq(vcpu, 0);
b0c632db
HC
4001}
4002
199b5763
LM
4003bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
4004{
0546c63d 4005 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
199b5763
LM
4006}
4007
27406cd5 4008void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
49b99e1e 4009{
805de8f4 4010 atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
61a6df54 4011 exit_sie(vcpu);
49b99e1e
CB
4012}
4013
27406cd5 4014void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
49b99e1e 4015{
805de8f4 4016 atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
49b99e1e
CB
4017}
4018
8e236546
CB
4019static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
4020{
805de8f4 4021 atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
61a6df54 4022 exit_sie(vcpu);
8e236546
CB
4023}
4024
9ea59728
DH
4025bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
4026{
4027 return atomic_read(&vcpu->arch.sie_block->prog20) &
4028 (PROG_BLOCK_SIE | PROG_REQUEST);
4029}
4030
8e236546
CB
4031static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
4032{
9bf9fde2 4033 atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
8e236546
CB
4034}
4035
49b99e1e 4036/*
9ea59728 4037 * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
49b99e1e
CB
4038 * If the CPU is not running (e.g. waiting as idle) the function will
4039 * return immediately. */
4040void exit_sie(struct kvm_vcpu *vcpu)
4041{
ef8f4f49 4042 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
9ea59728 4043 kvm_s390_vsie_kick(vcpu);
49b99e1e
CB
4044 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
4045 cpu_relax();
4046}
4047
8e236546
CB
4048/* Kick a guest cpu out of SIE to process a request synchronously */
4049void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
49b99e1e 4050{
df06dae3 4051 __kvm_make_request(req, vcpu);
8e236546 4052 kvm_s390_vcpu_request(vcpu);
49b99e1e
CB
4053}
4054
414d3b07
MS
4055static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
4056 unsigned long end)
2c70fe44 4057{
2c70fe44
CB
4058 struct kvm *kvm = gmap->private;
4059 struct kvm_vcpu *vcpu;
414d3b07 4060 unsigned long prefix;
46808a4c 4061 unsigned long i;
2c70fe44 4062
70fea301
NB
4063 trace_kvm_s390_gmap_notifier(start, end, gmap_is_shadow(gmap));
4064
65d0b0d4
DH
4065 if (gmap_is_shadow(gmap))
4066 return;
414d3b07
MS
4067 if (start >= 1UL << 31)
4068 /* We are only interested in prefix pages */
4069 return;
2c70fe44
CB
4070 kvm_for_each_vcpu(i, vcpu, kvm) {
4071 /* match against both prefix pages */
414d3b07
MS
4072 prefix = kvm_s390_get_prefix(vcpu);
4073 if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
4074 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
4075 start, end);
cc65c3a1 4076 kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
2c70fe44
CB
4077 }
4078 }
4079}
4080
8b905d28
CB
4081bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
4082{
4083 /* do not poll with more than halt_poll_max_steal percent of steal time */
4084 if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >=
6f390916 4085 READ_ONCE(halt_poll_max_steal)) {
8b905d28
CB
4086 vcpu->stat.halt_no_poll_steal++;
4087 return true;
4088 }
4089 return false;
4090}
4091
b6d33834
CD
4092int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
4093{
4094 /* kvm common code refers to this, but never calls it */
4095 BUG();
4096 return 0;
4097}
4098
14eebd91
CO
4099static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
4100 struct kvm_one_reg *reg)
4101{
4102 int r = -EINVAL;
4103
4104 switch (reg->id) {
29b7c71b
CO
4105 case KVM_REG_S390_TODPR:
4106 r = put_user(vcpu->arch.sie_block->todpr,
4107 (u32 __user *)reg->addr);
4108 break;
4109 case KVM_REG_S390_EPOCHDIFF:
4110 r = put_user(vcpu->arch.sie_block->epoch,
4111 (u64 __user *)reg->addr);
4112 break;
46a6dd1c 4113 case KVM_REG_S390_CPU_TIMER:
4287f247 4114 r = put_user(kvm_s390_get_cpu_timer(vcpu),
46a6dd1c
J
4115 (u64 __user *)reg->addr);
4116 break;
4117 case KVM_REG_S390_CLOCK_COMP:
4118 r = put_user(vcpu->arch.sie_block->ckc,
4119 (u64 __user *)reg->addr);
4120 break;
536336c2
DD
4121 case KVM_REG_S390_PFTOKEN:
4122 r = put_user(vcpu->arch.pfault_token,
4123 (u64 __user *)reg->addr);
4124 break;
4125 case KVM_REG_S390_PFCOMPARE:
4126 r = put_user(vcpu->arch.pfault_compare,
4127 (u64 __user *)reg->addr);
4128 break;
4129 case KVM_REG_S390_PFSELECT:
4130 r = put_user(vcpu->arch.pfault_select,
4131 (u64 __user *)reg->addr);
4132 break;
672550fb
CB
4133 case KVM_REG_S390_PP:
4134 r = put_user(vcpu->arch.sie_block->pp,
4135 (u64 __user *)reg->addr);
4136 break;
afa45ff5
CB
4137 case KVM_REG_S390_GBEA:
4138 r = put_user(vcpu->arch.sie_block->gbea,
4139 (u64 __user *)reg->addr);
4140 break;
14eebd91
CO
4141 default:
4142 break;
4143 }
4144
4145 return r;
4146}
4147
4148static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
4149 struct kvm_one_reg *reg)
4150{
4151 int r = -EINVAL;
4287f247 4152 __u64 val;
14eebd91
CO
4153
4154 switch (reg->id) {
29b7c71b
CO
4155 case KVM_REG_S390_TODPR:
4156 r = get_user(vcpu->arch.sie_block->todpr,
4157 (u32 __user *)reg->addr);
4158 break;
4159 case KVM_REG_S390_EPOCHDIFF:
4160 r = get_user(vcpu->arch.sie_block->epoch,
4161 (u64 __user *)reg->addr);
4162 break;
46a6dd1c 4163 case KVM_REG_S390_CPU_TIMER:
4287f247
DH
4164 r = get_user(val, (u64 __user *)reg->addr);
4165 if (!r)
4166 kvm_s390_set_cpu_timer(vcpu, val);
46a6dd1c
J
4167 break;
4168 case KVM_REG_S390_CLOCK_COMP:
4169 r = get_user(vcpu->arch.sie_block->ckc,
4170 (u64 __user *)reg->addr);
4171 break;
536336c2
DD
4172 case KVM_REG_S390_PFTOKEN:
4173 r = get_user(vcpu->arch.pfault_token,
4174 (u64 __user *)reg->addr);
9fbd8082
DH
4175 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4176 kvm_clear_async_pf_completion_queue(vcpu);
536336c2
DD
4177 break;
4178 case KVM_REG_S390_PFCOMPARE:
4179 r = get_user(vcpu->arch.pfault_compare,
4180 (u64 __user *)reg->addr);
4181 break;
4182 case KVM_REG_S390_PFSELECT:
4183 r = get_user(vcpu->arch.pfault_select,
4184 (u64 __user *)reg->addr);
4185 break;
672550fb
CB
4186 case KVM_REG_S390_PP:
4187 r = get_user(vcpu->arch.sie_block->pp,
4188 (u64 __user *)reg->addr);
4189 break;
afa45ff5
CB
4190 case KVM_REG_S390_GBEA:
4191 r = get_user(vcpu->arch.sie_block->gbea,
4192 (u64 __user *)reg->addr);
4193 break;
14eebd91
CO
4194 default:
4195 break;
4196 }
4197
4198 return r;
4199}
b6d33834 4200
7de3f142 4201static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
b0c632db 4202{
7de3f142
JF
4203 vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
4204 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
4205 memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
4206
4207 kvm_clear_async_pf_completion_queue(vcpu);
4208 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
4209 kvm_s390_vcpu_stop(vcpu);
4210 kvm_s390_clear_local_irqs(vcpu);
4211}
4212
4213static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
4214{
4215 /* Initial reset is a superset of the normal reset */
4216 kvm_arch_vcpu_ioctl_normal_reset(vcpu);
4217
e93fc7b4
CB
4218 /*
4219 * This equals initial cpu reset in pop, but we don't switch to ESA.
4220 * We do not only reset the internal data, but also ...
4221 */
7de3f142
JF
4222 vcpu->arch.sie_block->gpsw.mask = 0;
4223 vcpu->arch.sie_block->gpsw.addr = 0;
4224 kvm_s390_set_prefix(vcpu, 0);
4225 kvm_s390_set_cpu_timer(vcpu, 0);
4226 vcpu->arch.sie_block->ckc = 0;
7de3f142
JF
4227 memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
4228 vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
4229 vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
e93fc7b4
CB
4230
4231 /* ... the data in sync regs */
4232 memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs));
4233 vcpu->run->s.regs.ckc = 0;
4234 vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK;
4235 vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK;
4236 vcpu->run->psw_addr = 0;
4237 vcpu->run->psw_mask = 0;
4238 vcpu->run->s.regs.todpr = 0;
4239 vcpu->run->s.regs.cputm = 0;
4240 vcpu->run->s.regs.ckc = 0;
4241 vcpu->run->s.regs.pp = 0;
4242 vcpu->run->s.regs.gbea = 1;
7de3f142 4243 vcpu->run->s.regs.fpc = 0;
0f303504
JF
4244 /*
4245 * Do not reset these registers in the protected case, as some of
cada938a 4246 * them are overlaid and they are not accessible in this case
0f303504
JF
4247 * anyway.
4248 */
4249 if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
4250 vcpu->arch.sie_block->gbea = 1;
4251 vcpu->arch.sie_block->pp = 0;
4252 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
4253 vcpu->arch.sie_block->todpr = 0;
4254 }
7de3f142
JF
4255}
4256
4257static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
4258{
4259 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
4260
4261 /* Clear reset is a superset of the initial reset */
4262 kvm_arch_vcpu_ioctl_initial_reset(vcpu);
4263
4264 memset(&regs->gprs, 0, sizeof(regs->gprs));
4265 memset(&regs->vrs, 0, sizeof(regs->vrs));
4266 memset(&regs->acrs, 0, sizeof(regs->acrs));
4267 memset(&regs->gscb, 0, sizeof(regs->gscb));
4268
4269 regs->etoken = 0;
4270 regs->etoken_extension = 0;
b0c632db
HC
4271}
4272
4273int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4274{
875656fe 4275 vcpu_load(vcpu);
5a32c1af 4276 memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
875656fe 4277 vcpu_put(vcpu);
b0c632db
HC
4278 return 0;
4279}
4280
4281int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4282{
1fc9b76b 4283 vcpu_load(vcpu);
5a32c1af 4284 memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
1fc9b76b 4285 vcpu_put(vcpu);
b0c632db
HC
4286 return 0;
4287}
4288
4289int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4290 struct kvm_sregs *sregs)
4291{
b4ef9d4e
CD
4292 vcpu_load(vcpu);
4293
59674c1a 4294 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
b0c632db 4295 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
b4ef9d4e
CD
4296
4297 vcpu_put(vcpu);
b0c632db
HC
4298 return 0;
4299}
4300
4301int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4302 struct kvm_sregs *sregs)
4303{
bcdec41c
CD
4304 vcpu_load(vcpu);
4305
59674c1a 4306 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
b0c632db 4307 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
bcdec41c
CD
4308
4309 vcpu_put(vcpu);
b0c632db
HC
4310 return 0;
4311}
4312
4313int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4314{
6a96bc7f
CD
4315 int ret = 0;
4316
4317 vcpu_load(vcpu);
4318
4319 if (test_fp_ctl(fpu->fpc)) {
4320 ret = -EINVAL;
4321 goto out;
4322 }
e1788bb9 4323 vcpu->run->s.regs.fpc = fpu->fpc;
9abc2a08 4324 if (MACHINE_HAS_VX)
a7d4b8f2
DH
4325 convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
4326 (freg_t *) fpu->fprs);
9abc2a08 4327 else
a7d4b8f2 4328 memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
6a96bc7f
CD
4329
4330out:
4331 vcpu_put(vcpu);
4332 return ret;
b0c632db
HC
4333}
4334
4335int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4336{
1393123e
CD
4337 vcpu_load(vcpu);
4338
9abc2a08
DH
4339 /* make sure we have the latest values */
4340 save_fpu_regs();
4341 if (MACHINE_HAS_VX)
a7d4b8f2
DH
4342 convert_vx_to_fp((freg_t *) fpu->fprs,
4343 (__vector128 *) vcpu->run->s.regs.vrs);
9abc2a08 4344 else
a7d4b8f2 4345 memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
e1788bb9 4346 fpu->fpc = vcpu->run->s.regs.fpc;
1393123e
CD
4347
4348 vcpu_put(vcpu);
b0c632db
HC
4349 return 0;
4350}
4351
4352static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
4353{
4354 int rc = 0;
4355
7a42fdc2 4356 if (!is_vcpu_stopped(vcpu))
b0c632db 4357 rc = -EBUSY;
d7b0b5eb
CO
4358 else {
4359 vcpu->run->psw_mask = psw.mask;
4360 vcpu->run->psw_addr = psw.addr;
4361 }
b0c632db
HC
4362 return rc;
4363}
4364
4365int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4366 struct kvm_translation *tr)
4367{
4368 return -EINVAL; /* not implemented yet */
4369}
4370
27291e21
DH
4371#define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
4372 KVM_GUESTDBG_USE_HW_BP | \
4373 KVM_GUESTDBG_ENABLE)
4374
d0bfb940
JK
4375int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4376 struct kvm_guest_debug *dbg)
b0c632db 4377{
27291e21
DH
4378 int rc = 0;
4379
66b56562
CD
4380 vcpu_load(vcpu);
4381
27291e21
DH
4382 vcpu->guest_debug = 0;
4383 kvm_s390_clear_bp_data(vcpu);
4384
66b56562
CD
4385 if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
4386 rc = -EINVAL;
4387 goto out;
4388 }
4389 if (!sclp.has_gpere) {
4390 rc = -EINVAL;
4391 goto out;
4392 }
27291e21
DH
4393
4394 if (dbg->control & KVM_GUESTDBG_ENABLE) {
4395 vcpu->guest_debug = dbg->control;
4396 /* enforce guest PER */
ef8f4f49 4397 kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
27291e21
DH
4398
4399 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
4400 rc = kvm_s390_import_bp_data(vcpu, dbg);
4401 } else {
9daecfc6 4402 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
27291e21
DH
4403 vcpu->arch.guestdbg.last_bp = 0;
4404 }
4405
4406 if (rc) {
4407 vcpu->guest_debug = 0;
4408 kvm_s390_clear_bp_data(vcpu);
9daecfc6 4409 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
27291e21
DH
4410 }
4411
66b56562
CD
4412out:
4413 vcpu_put(vcpu);
27291e21 4414 return rc;
b0c632db
HC
4415}
4416
62d9f0db
MT
4417int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4418 struct kvm_mp_state *mp_state)
4419{
fd232561
CD
4420 int ret;
4421
4422 vcpu_load(vcpu);
4423
6352e4d2 4424 /* CHECK_STOP and LOAD are not supported yet */
fd232561
CD
4425 ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
4426 KVM_MP_STATE_OPERATING;
4427
4428 vcpu_put(vcpu);
4429 return ret;
62d9f0db
MT
4430}
4431
4432int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4433 struct kvm_mp_state *mp_state)
4434{
6352e4d2
DH
4435 int rc = 0;
4436
e83dff5e
CD
4437 vcpu_load(vcpu);
4438
6352e4d2 4439 /* user space knows about this interface - let it control the state */
67cf68b6 4440 kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm);
6352e4d2
DH
4441
4442 switch (mp_state->mp_state) {
4443 case KVM_MP_STATE_STOPPED:
fe28c786 4444 rc = kvm_s390_vcpu_stop(vcpu);
6352e4d2
DH
4445 break;
4446 case KVM_MP_STATE_OPERATING:
fe28c786 4447 rc = kvm_s390_vcpu_start(vcpu);
6352e4d2
DH
4448 break;
4449 case KVM_MP_STATE_LOAD:
7c36a3fc
JF
4450 if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
4451 rc = -ENXIO;
4452 break;
4453 }
4454 rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD);
4455 break;
6352e4d2 4456 case KVM_MP_STATE_CHECK_STOP:
3b684a42 4457 fallthrough; /* CHECK_STOP and LOAD are not supported yet */
6352e4d2
DH
4458 default:
4459 rc = -ENXIO;
4460 }
4461
e83dff5e 4462 vcpu_put(vcpu);
6352e4d2 4463 return rc;
62d9f0db
MT
4464}
4465
8ad35755
DH
4466static bool ibs_enabled(struct kvm_vcpu *vcpu)
4467{
8d5fb0dc 4468 return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
8ad35755
DH
4469}
4470
2c70fe44
CB
4471static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
4472{
8ad35755 4473retry:
8e236546 4474 kvm_s390_vcpu_request_handled(vcpu);
2fa6e1e1 4475 if (!kvm_request_pending(vcpu))
586b7ccd 4476 return 0;
2c70fe44 4477 /*
cc65c3a1 4478 * If the guest prefix changed, re-arm the ipte notifier for the
b2d73b2a 4479 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
2c70fe44
CB
4480 * This ensures that the ipte instruction for this request has
4481 * already finished. We might race against a second unmapper that
4482 * wants to set the blocking bit. Lets just retry the request loop.
4483 */
cc65c3a1 4484 if (kvm_check_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu)) {
2c70fe44 4485 int rc;
b2d73b2a
MS
4486 rc = gmap_mprotect_notify(vcpu->arch.gmap,
4487 kvm_s390_get_prefix(vcpu),
4488 PAGE_SIZE * 2, PROT_WRITE);
aca411a4 4489 if (rc) {
cc65c3a1 4490 kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
2c70fe44 4491 return rc;
aca411a4 4492 }
8ad35755 4493 goto retry;
2c70fe44 4494 }
8ad35755 4495
d3d692c8
DH
4496 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
4497 vcpu->arch.sie_block->ihcpu = 0xffff;
4498 goto retry;
4499 }
4500
8ad35755
DH
4501 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
4502 if (!ibs_enabled(vcpu)) {
4503 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
ef8f4f49 4504 kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
8ad35755
DH
4505 }
4506 goto retry;
2c70fe44 4507 }
8ad35755
DH
4508
4509 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
4510 if (ibs_enabled(vcpu)) {
4511 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
9daecfc6 4512 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
8ad35755
DH
4513 }
4514 goto retry;
4515 }
4516
6502a34c
DH
4517 if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
4518 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
4519 goto retry;
4520 }
4521
190df4a2
CI
4522 if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
4523 /*
c9f0a2b8 4524 * Disable CMM virtualization; we will emulate the ESSA
190df4a2
CI
4525 * instruction manually, in order to provide additional
4526 * functionalities needed for live migration.
4527 */
4528 vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
4529 goto retry;
4530 }
4531
4532 if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
4533 /*
c9f0a2b8
JF
4534 * Re-enable CMM virtualization if CMMA is available and
4535 * CMM has been used.
190df4a2
CI
4536 */
4537 if ((vcpu->kvm->arch.use_cmma) &&
c9f0a2b8 4538 (vcpu->kvm->mm->context.uses_cmm))
190df4a2
CI
4539 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
4540 goto retry;
4541 }
4542
3194cdb7
DH
4543 /* we left the vsie handler, nothing to do, just clear the request */
4544 kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
0759d068 4545
2c70fe44
CB
4546 return 0;
4547}
4548
c0573ba5 4549static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
8fa1696e
CW
4550{
4551 struct kvm_vcpu *vcpu;
2cfd7b73 4552 union tod_clock clk;
46808a4c 4553 unsigned long i;
8fa1696e 4554
8fa1696e
CW
4555 preempt_disable();
4556
2cfd7b73 4557 store_tod_clock_ext(&clk);
8fa1696e 4558
2cfd7b73 4559 kvm->arch.epoch = gtod->tod - clk.tod;
0e7def5f
DH
4560 kvm->arch.epdx = 0;
4561 if (test_kvm_facility(kvm, 139)) {
2cfd7b73 4562 kvm->arch.epdx = gtod->epoch_idx - clk.ei;
0e7def5f
DH
4563 if (kvm->arch.epoch > gtod->tod)
4564 kvm->arch.epdx -= 1;
4565 }
8fa1696e
CW
4566
4567 kvm_s390_vcpu_block_all(kvm);
4568 kvm_for_each_vcpu(i, vcpu, kvm) {
4569 vcpu->arch.sie_block->epoch = kvm->arch.epoch;
4570 vcpu->arch.sie_block->epdx = kvm->arch.epdx;
4571 }
4572
4573 kvm_s390_vcpu_unblock_all(kvm);
4574 preempt_enable();
c0573ba5
CI
4575}
4576
c0573ba5
CI
4577int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
4578{
4579 if (!mutex_trylock(&kvm->lock))
4580 return 0;
4581 __kvm_s390_set_tod_clock(kvm, gtod);
8fa1696e 4582 mutex_unlock(&kvm->lock);
c0573ba5 4583 return 1;
8fa1696e
CW
4584}
4585
fa576c58
TH
4586/**
4587 * kvm_arch_fault_in_page - fault-in guest page if necessary
4588 * @vcpu: The corresponding virtual cpu
4589 * @gpa: Guest physical address
4590 * @writable: Whether the page should be writable or not
4591 *
4592 * Make sure that a guest page has been faulted-in on the host.
4593 *
4594 * Return: Zero on success, negative error code otherwise.
4595 */
4596long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
24eb3a82 4597{
527e30b4
MS
4598 return gmap_fault(vcpu->arch.gmap, gpa,
4599 writable ? FAULT_FLAG_WRITE : 0);
24eb3a82
DD
4600}
4601
3c038e6b
DD
4602static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
4603 unsigned long token)
4604{
4605 struct kvm_s390_interrupt inti;
383d0b05 4606 struct kvm_s390_irq irq;
3c038e6b
DD
4607
4608 if (start_token) {
383d0b05
JF
4609 irq.u.ext.ext_params2 = token;
4610 irq.type = KVM_S390_INT_PFAULT_INIT;
4611 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
3c038e6b
DD
4612 } else {
4613 inti.type = KVM_S390_INT_PFAULT_DONE;
383d0b05 4614 inti.parm64 = token;
3c038e6b
DD
4615 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
4616 }
4617}
4618
2a18b7e7 4619bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
3c038e6b
DD
4620 struct kvm_async_pf *work)
4621{
4622 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
4623 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
2a18b7e7
VK
4624
4625 return true;
3c038e6b
DD
4626}
4627
4628void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
4629 struct kvm_async_pf *work)
4630{
4631 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
4632 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
4633}
4634
4635void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
4636 struct kvm_async_pf *work)
4637{
4638 /* s390 will always inject the page directly */
4639}
4640
7c0ade6c 4641bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
3c038e6b
DD
4642{
4643 /*
4644 * s390 will always inject the page directly,
4645 * but we still want check_async_completion to cleanup
4646 */
4647 return true;
4648}
4649
e8c22266 4650static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
3c038e6b
DD
4651{
4652 hva_t hva;
4653 struct kvm_arch_async_pf arch;
3c038e6b
DD
4654
4655 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
e8c22266 4656 return false;
3c038e6b
DD
4657 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
4658 vcpu->arch.pfault_compare)
e8c22266 4659 return false;
3c038e6b 4660 if (psw_extint_disabled(vcpu))
e8c22266 4661 return false;
9a022067 4662 if (kvm_s390_vcpu_has_irq(vcpu, 0))
e8c22266 4663 return false;
b9224cd7 4664 if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
e8c22266 4665 return false;
3c038e6b 4666 if (!vcpu->arch.gmap->pfault_enabled)
e8c22266 4667 return false;
3c038e6b 4668
81480cc1
HC
4669 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
4670 hva += current->thread.gmap_addr & ~PAGE_MASK;
4671 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
e8c22266 4672 return false;
3c038e6b 4673
e8c22266 4674 return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
3c038e6b
DD
4675}
4676
3fb4c40f 4677static int vcpu_pre_run(struct kvm_vcpu *vcpu)
b0c632db 4678{
3fb4c40f 4679 int rc, cpuflags;
e168bf8d 4680
3c038e6b
DD
4681 /*
4682 * On s390 notifications for arriving pages will be delivered directly
4683 * to the guest but the house keeping for completed pfaults is
4684 * handled outside the worker.
4685 */
4686 kvm_check_async_pf_completion(vcpu);
4687
7ec7c8c7
CB
4688 vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
4689 vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
b0c632db
HC
4690
4691 if (need_resched())
4692 schedule();
4693
79395031
JF
4694 if (!kvm_is_ucontrol(vcpu->kvm)) {
4695 rc = kvm_s390_deliver_pending_interrupts(vcpu);
16631c42 4696 if (rc || guestdbg_exit_pending(vcpu))
79395031
JF
4697 return rc;
4698 }
0ff31867 4699
2c70fe44
CB
4700 rc = kvm_s390_handle_requests(vcpu);
4701 if (rc)
4702 return rc;
4703
27291e21
DH
4704 if (guestdbg_enabled(vcpu)) {
4705 kvm_s390_backup_guest_per_regs(vcpu);
4706 kvm_s390_patch_guest_per_regs(vcpu);
4707 }
4708
4eeef242 4709 clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
9f30f621 4710
b0c632db 4711 vcpu->arch.sie_block->icptcode = 0;
3fb4c40f
TH
4712 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
4713 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
4714 trace_kvm_s390_sie_enter(vcpu, cpuflags);
2b29a9fd 4715
3fb4c40f
TH
4716 return 0;
4717}
4718
492d8642
TH
4719static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
4720{
56317920
DH
4721 struct kvm_s390_pgm_info pgm_info = {
4722 .code = PGM_ADDRESSING,
4723 };
4724 u8 opcode, ilen;
492d8642
TH
4725 int rc;
4726
4727 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4728 trace_kvm_s390_sie_fault(vcpu);
4729
4730 /*
4731 * We want to inject an addressing exception, which is defined as a
4732 * suppressing or terminating exception. However, since we came here
4733 * by a DAT access exception, the PSW still points to the faulting
4734 * instruction since DAT exceptions are nullifying. So we've got
4735 * to look up the current opcode to get the length of the instruction
4736 * to be able to forward the PSW.
4737 */
3fa8cad7 4738 rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
56317920 4739 ilen = insn_length(opcode);
9b0d721a
DH
4740 if (rc < 0) {
4741 return rc;
4742 } else if (rc) {
4743 /* Instruction-Fetching Exceptions - we can't detect the ilen.
4744 * Forward by arbitrary ilc, injection will take care of
4745 * nullification if necessary.
4746 */
4747 pgm_info = vcpu->arch.pgm;
4748 ilen = 4;
4749 }
56317920
DH
4750 pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
4751 kvm_s390_forward_psw(vcpu, ilen);
4752 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
492d8642
TH
4753}
4754
3fb4c40f
TH
4755static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
4756{
4d62fcc0
QH
4757 struct mcck_volatile_info *mcck_info;
4758 struct sie_page *sie_page;
4759
2b29a9fd
DD
4760 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
4761 vcpu->arch.sie_block->icptcode);
4762 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
4763
27291e21
DH
4764 if (guestdbg_enabled(vcpu))
4765 kvm_s390_restore_guest_per_regs(vcpu);
4766
7ec7c8c7
CB
4767 vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
4768 vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
71f116bf 4769
4d62fcc0
QH
4770 if (exit_reason == -EINTR) {
4771 VCPU_EVENT(vcpu, 3, "%s", "machine check");
4772 sie_page = container_of(vcpu->arch.sie_block,
4773 struct sie_page, sie_block);
4774 mcck_info = &sie_page->mcck_info;
4775 kvm_s390_reinject_machine_check(vcpu, mcck_info);
4776 return 0;
4777 }
4778
71f116bf
DH
4779 if (vcpu->arch.sie_block->icptcode > 0) {
4780 int rc = kvm_handle_sie_intercept(vcpu);
4781
4782 if (rc != -EOPNOTSUPP)
4783 return rc;
4784 vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
4785 vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
4786 vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
4787 vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
4788 return -EREMOTE;
4789 } else if (exit_reason != -EFAULT) {
4790 vcpu->stat.exit_null++;
4791 return 0;
210b1607
TH
4792 } else if (kvm_is_ucontrol(vcpu->kvm)) {
4793 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
4794 vcpu->run->s390_ucontrol.trans_exc_code =
4795 current->thread.gmap_addr;
4796 vcpu->run->s390_ucontrol.pgm_code = 0x10;
71f116bf 4797 return -EREMOTE;
24eb3a82 4798 } else if (current->thread.gmap_pfault) {
3c038e6b 4799 trace_kvm_s390_major_guest_pfault(vcpu);
24eb3a82 4800 current->thread.gmap_pfault = 0;
71f116bf
DH
4801 if (kvm_arch_setup_async_pf(vcpu))
4802 return 0;
50a05be4 4803 vcpu->stat.pfault_sync++;
71f116bf 4804 return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
a76ccff6 4805 }
71f116bf 4806 return vcpu_post_run_fault_in_sie(vcpu);
3fb4c40f
TH
4807}
4808
3adae0b4 4809#define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK)
3fb4c40f
TH
4810static int __vcpu_run(struct kvm_vcpu *vcpu)
4811{
4812 int rc, exit_reason;
c8aac234 4813 struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block;
3fb4c40f 4814
800c1065
TH
4815 /*
4816 * We try to hold kvm->srcu during most of vcpu_run (except when run-
4817 * ning the guest), so that memslots (and other stuff) are protected
4818 */
2031f287 4819 kvm_vcpu_srcu_read_lock(vcpu);
800c1065 4820
a76ccff6
TH
4821 do {
4822 rc = vcpu_pre_run(vcpu);
16631c42 4823 if (rc || guestdbg_exit_pending(vcpu))
a76ccff6 4824 break;
3fb4c40f 4825
2031f287 4826 kvm_vcpu_srcu_read_unlock(vcpu);
a76ccff6
TH
4827 /*
4828 * As PF_VCPU will be used in fault handler, between
4829 * guest_enter and guest_exit should be no uaccess.
4830 */
0097d12e 4831 local_irq_disable();
6edaa530 4832 guest_enter_irqoff();
db0758b2 4833 __disable_cpu_timer_accounting(vcpu);
0097d12e 4834 local_irq_enable();
c8aac234
JF
4835 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4836 memcpy(sie_page->pv_grregs,
4837 vcpu->run->s.regs.gprs,
4838 sizeof(sie_page->pv_grregs));
4839 }
56e62a73
SS
4840 if (test_cpu_flag(CIF_FPU))
4841 load_fpu_regs();
a76ccff6
TH
4842 exit_reason = sie64a(vcpu->arch.sie_block,
4843 vcpu->run->s.regs.gprs);
c8aac234
JF
4844 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4845 memcpy(vcpu->run->s.regs.gprs,
4846 sie_page->pv_grregs,
4847 sizeof(sie_page->pv_grregs));
3adae0b4
JF
4848 /*
4849 * We're not allowed to inject interrupts on intercepts
4850 * that leave the guest state in an "in-between" state
4851 * where the next SIE entry will do a continuation.
4852 * Fence interrupts in our "internal" PSW.
4853 */
4854 if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR ||
4855 vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) {
4856 vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
4857 }
c8aac234 4858 }
0097d12e 4859 local_irq_disable();
db0758b2 4860 __enable_cpu_timer_accounting(vcpu);
6edaa530 4861 guest_exit_irqoff();
0097d12e 4862 local_irq_enable();
2031f287 4863 kvm_vcpu_srcu_read_lock(vcpu);
a76ccff6
TH
4864
4865 rc = vcpu_post_run(vcpu, exit_reason);
27291e21 4866 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
3fb4c40f 4867
2031f287 4868 kvm_vcpu_srcu_read_unlock(vcpu);
e168bf8d 4869 return rc;
b0c632db
HC
4870}
4871
2f0a83be 4872static void sync_regs_fmt2(struct kvm_vcpu *vcpu)
b028ee3e 4873{
2f0a83be 4874 struct kvm_run *kvm_run = vcpu->run;
4d5f2c04 4875 struct runtime_instr_cb *riccb;
4e0b1ab7 4876 struct gs_cb *gscb;
4d5f2c04
CB
4877
4878 riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
4e0b1ab7 4879 gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
b028ee3e
DH
4880 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
4881 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
b028ee3e 4882 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
b028ee3e
DH
4883 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
4884 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
4885 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
4886 }
4887 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
4888 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
4889 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
4890 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
9fbd8082
DH
4891 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4892 kvm_clear_async_pf_completion_queue(vcpu);
b028ee3e 4893 }
23a60f83
CW
4894 if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) {
4895 vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318;
4896 vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc;
3fd8417f 4897 VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc);
23a60f83 4898 }
80cd8763
FZ
4899 /*
4900 * If userspace sets the riccb (e.g. after migration) to a valid state,
4901 * we should enable RI here instead of doing the lazy enablement.
4902 */
4903 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
4d5f2c04 4904 test_kvm_facility(vcpu->kvm, 64) &&
bb59c2da 4905 riccb->v &&
0c9d8683 4906 !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
4d5f2c04 4907 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
0c9d8683 4908 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
80cd8763 4909 }
4e0b1ab7
FZ
4910 /*
4911 * If userspace sets the gscb (e.g. after migration) to non-zero,
4912 * we should enable GS here instead of doing the lazy enablement.
4913 */
4914 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
4915 test_kvm_facility(vcpu->kvm, 133) &&
4916 gscb->gssm &&
4917 !vcpu->arch.gs_enabled) {
4918 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
4919 vcpu->arch.sie_block->ecb |= ECB_GS;
4920 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
4921 vcpu->arch.gs_enabled = 1;
80cd8763 4922 }
35b3fde6
CB
4923 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
4924 test_kvm_facility(vcpu->kvm, 82)) {
4925 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
4926 vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
4927 }
811ea797
JF
4928 if (MACHINE_HAS_GS) {
4929 preempt_disable();
99441a38 4930 local_ctl_set_bit(2, CR2_GUARDED_STORAGE_BIT);
811ea797
JF
4931 if (current->thread.gs_cb) {
4932 vcpu->arch.host_gscb = current->thread.gs_cb;
4933 save_gs_cb(vcpu->arch.host_gscb);
4934 }
4935 if (vcpu->arch.gs_enabled) {
4936 current->thread.gs_cb = (struct gs_cb *)
4937 &vcpu->run->s.regs.gscb;
4938 restore_gs_cb(current->thread.gs_cb);
4939 }
4940 preempt_enable();
4941 }
4942 /* SIE will load etoken directly from SDNX and therefore kvm_run */
4943}
4944
2f0a83be 4945static void sync_regs(struct kvm_vcpu *vcpu)
811ea797 4946{
2f0a83be
TZ
4947 struct kvm_run *kvm_run = vcpu->run;
4948
811ea797
JF
4949 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
4950 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
4951 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
4952 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
4953 /* some control register changes require a tlb flush */
4954 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4955 }
4956 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4957 kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
4958 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
4959 }
31d8b8d4
CB
4960 save_access_regs(vcpu->arch.host_acrs);
4961 restore_access_regs(vcpu->run->s.regs.acrs);
e1788bb9
CB
4962 /* save host (userspace) fprs/vrs */
4963 save_fpu_regs();
4964 vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
4965 vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
4966 if (MACHINE_HAS_VX)
4967 current->thread.fpu.regs = vcpu->run->s.regs.vrs;
4968 else
4969 current->thread.fpu.regs = vcpu->run->s.regs.fprs;
4970 current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
4971 if (test_fp_ctl(current->thread.fpu.fpc))
4972 /* User space provided an invalid FPC, let's clear it */
4973 current->thread.fpu.fpc = 0;
811ea797
JF
4974
4975 /* Sync fmt2 only data */
4976 if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) {
2f0a83be 4977 sync_regs_fmt2(vcpu);
811ea797
JF
4978 } else {
4979 /*
4980 * In several places we have to modify our internal view to
4981 * not do things that are disallowed by the ultravisor. For
4982 * example we must not inject interrupts after specific exits
4983 * (e.g. 112 prefix page not secure). We do this by turning
4984 * off the machine check, external and I/O interrupt bits
4985 * of our PSW copy. To avoid getting validity intercepts, we
4986 * do only accept the condition code from userspace.
4987 */
4988 vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC;
4989 vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask &
4990 PSW_MASK_CC;
4991 }
4992
4993 kvm_run->kvm_dirty_regs = 0;
4994}
4995
2f0a83be 4996static void store_regs_fmt2(struct kvm_vcpu *vcpu)
811ea797 4997{
2f0a83be
TZ
4998 struct kvm_run *kvm_run = vcpu->run;
4999
811ea797
JF
5000 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
5001 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
5002 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
5003 kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
23a60f83 5004 kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val;
4e0b1ab7 5005 if (MACHINE_HAS_GS) {
44bada28 5006 preempt_disable();
99441a38 5007 local_ctl_set_bit(2, CR2_GUARDED_STORAGE_BIT);
811ea797
JF
5008 if (vcpu->arch.gs_enabled)
5009 save_gs_cb(current->thread.gs_cb);
811ea797
JF
5010 current->thread.gs_cb = vcpu->arch.host_gscb;
5011 restore_gs_cb(vcpu->arch.host_gscb);
811ea797 5012 if (!vcpu->arch.host_gscb)
99441a38 5013 local_ctl_clear_bit(2, CR2_GUARDED_STORAGE_BIT);
811ea797 5014 vcpu->arch.host_gscb = NULL;
44bada28 5015 preempt_enable();
4e0b1ab7 5016 }
811ea797 5017 /* SIE will save etoken directly into SDNX and therefore kvm_run */
b028ee3e
DH
5018}
5019
2f0a83be 5020static void store_regs(struct kvm_vcpu *vcpu)
b028ee3e 5021{
2f0a83be
TZ
5022 struct kvm_run *kvm_run = vcpu->run;
5023
b028ee3e
DH
5024 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
5025 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
5026 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
5027 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
4287f247 5028 kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
b028ee3e 5029 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
b028ee3e
DH
5030 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
5031 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
5032 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
31d8b8d4
CB
5033 save_access_regs(vcpu->run->s.regs.acrs);
5034 restore_access_regs(vcpu->arch.host_acrs);
e1788bb9
CB
5035 /* Save guest register state */
5036 save_fpu_regs();
5037 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
5038 /* Restore will be done lazily at return */
5039 current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
5040 current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
811ea797 5041 if (likely(!kvm_s390_pv_cpu_is_protected(vcpu)))
2f0a83be 5042 store_regs_fmt2(vcpu);
b028ee3e
DH
5043}
5044
1b94f6f8 5045int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
b0c632db 5046{
1b94f6f8 5047 struct kvm_run *kvm_run = vcpu->run;
8f2abe6a 5048 int rc;
b0c632db 5049
0460eb35
JF
5050 /*
5051 * Running a VM while dumping always has the potential to
5052 * produce inconsistent dump data. But for PV vcpus a SIE
5053 * entry while dumping could also lead to a fatal validity
5054 * intercept which we absolutely want to avoid.
5055 */
5056 if (vcpu->kvm->arch.pv.dumping)
5057 return -EINVAL;
5058
460df4c1
PB
5059 if (kvm_run->immediate_exit)
5060 return -EINTR;
5061
200824f5
TH
5062 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
5063 kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
5064 return -EINVAL;
5065
accb757d
CD
5066 vcpu_load(vcpu);
5067
27291e21
DH
5068 if (guestdbg_exit_pending(vcpu)) {
5069 kvm_s390_prepare_debug_exit(vcpu);
accb757d
CD
5070 rc = 0;
5071 goto out;
27291e21
DH
5072 }
5073
20b7035c 5074 kvm_sigset_activate(vcpu);
b0c632db 5075
fe28c786
JF
5076 /*
5077 * no need to check the return value of vcpu_start as it can only have
5078 * an error for protvirt, but protvirt means user cpu state
5079 */
6352e4d2
DH
5080 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
5081 kvm_s390_vcpu_start(vcpu);
5082 } else if (is_vcpu_stopped(vcpu)) {
ea2cdd27 5083 pr_err_ratelimited("can't run stopped vcpu %d\n",
6352e4d2 5084 vcpu->vcpu_id);
accb757d
CD
5085 rc = -EINVAL;
5086 goto out;
6352e4d2 5087 }
b0c632db 5088
2f0a83be 5089 sync_regs(vcpu);
db0758b2 5090 enable_cpu_timer_accounting(vcpu);
d7b0b5eb 5091
dab4079d 5092 might_fault();
a76ccff6 5093 rc = __vcpu_run(vcpu);
9ace903d 5094
b1d16c49
CE
5095 if (signal_pending(current) && !rc) {
5096 kvm_run->exit_reason = KVM_EXIT_INTR;
8f2abe6a 5097 rc = -EINTR;
b1d16c49 5098 }
8f2abe6a 5099
27291e21
DH
5100 if (guestdbg_exit_pending(vcpu) && !rc) {
5101 kvm_s390_prepare_debug_exit(vcpu);
5102 rc = 0;
5103 }
5104
8f2abe6a 5105 if (rc == -EREMOTE) {
71f116bf 5106 /* userspace support is needed, kvm_run has been prepared */
8f2abe6a
CB
5107 rc = 0;
5108 }
b0c632db 5109
db0758b2 5110 disable_cpu_timer_accounting(vcpu);
2f0a83be 5111 store_regs(vcpu);
d7b0b5eb 5112
20b7035c 5113 kvm_sigset_deactivate(vcpu);
b0c632db 5114
b0c632db 5115 vcpu->stat.exit_userspace++;
accb757d
CD
5116out:
5117 vcpu_put(vcpu);
7e8e6ab4 5118 return rc;
b0c632db
HC
5119}
5120
b0c632db
HC
5121/*
5122 * store status at address
5123 * we use have two special cases:
5124 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
5125 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
5126 */
d0bce605 5127int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
b0c632db 5128{
092670cd 5129 unsigned char archmode = 1;
9abc2a08 5130 freg_t fprs[NUM_FPRS];
fda902cb 5131 unsigned int px;
4287f247 5132 u64 clkcomp, cputm;
d0bce605 5133 int rc;
b0c632db 5134
d9a3a09a 5135 px = kvm_s390_get_prefix(vcpu);
d0bce605
HC
5136 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
5137 if (write_guest_abs(vcpu, 163, &archmode, 1))
b0c632db 5138 return -EFAULT;
d9a3a09a 5139 gpa = 0;
d0bce605
HC
5140 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
5141 if (write_guest_real(vcpu, 163, &archmode, 1))
b0c632db 5142 return -EFAULT;
d9a3a09a
MS
5143 gpa = px;
5144 } else
5145 gpa -= __LC_FPREGS_SAVE_AREA;
9abc2a08
DH
5146
5147 /* manually convert vector registers if necessary */
5148 if (MACHINE_HAS_VX) {
9522b37f 5149 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
9abc2a08
DH
5150 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
5151 fprs, 128);
5152 } else {
5153 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
6fd8e67d 5154 vcpu->run->s.regs.fprs, 128);
9abc2a08 5155 }
d9a3a09a 5156 rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
d0bce605 5157 vcpu->run->s.regs.gprs, 128);
d9a3a09a 5158 rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
d0bce605 5159 &vcpu->arch.sie_block->gpsw, 16);
d9a3a09a 5160 rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
fda902cb 5161 &px, 4);
d9a3a09a 5162 rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
9abc2a08 5163 &vcpu->run->s.regs.fpc, 4);
d9a3a09a 5164 rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
d0bce605 5165 &vcpu->arch.sie_block->todpr, 4);
4287f247 5166 cputm = kvm_s390_get_cpu_timer(vcpu);
d9a3a09a 5167 rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
4287f247 5168 &cputm, 8);
178bd789 5169 clkcomp = vcpu->arch.sie_block->ckc >> 8;
d9a3a09a 5170 rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
d0bce605 5171 &clkcomp, 8);
d9a3a09a 5172 rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
d0bce605 5173 &vcpu->run->s.regs.acrs, 64);
d9a3a09a 5174 rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
d0bce605
HC
5175 &vcpu->arch.sie_block->gcr, 128);
5176 return rc ? -EFAULT : 0;
b0c632db
HC
5177}
5178
e879892c
TH
5179int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
5180{
5181 /*
5182 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
31d8b8d4 5183 * switch in the run ioctl. Let's update our copies before we save
e879892c
TH
5184 * it into the save area
5185 */
d0164ee2 5186 save_fpu_regs();
9abc2a08 5187 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
e879892c
TH
5188 save_access_regs(vcpu->run->s.regs.acrs);
5189
5190 return kvm_s390_store_status_unloaded(vcpu, addr);
5191}
5192
8ad35755
DH
5193static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
5194{
5195 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
8e236546 5196 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
8ad35755
DH
5197}
5198
5199static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
5200{
46808a4c 5201 unsigned long i;
8ad35755
DH
5202 struct kvm_vcpu *vcpu;
5203
5204 kvm_for_each_vcpu(i, vcpu, kvm) {
5205 __disable_ibs_on_vcpu(vcpu);
5206 }
5207}
5208
5209static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
5210{
09a400e7
DH
5211 if (!sclp.has_ibs)
5212 return;
8ad35755 5213 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
8e236546 5214 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
8ad35755
DH
5215}
5216
fe28c786 5217int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
6852d7b6 5218{
fe28c786 5219 int i, online_vcpus, r = 0, started_vcpus = 0;
8ad35755
DH
5220
5221 if (!is_vcpu_stopped(vcpu))
fe28c786 5222 return 0;
8ad35755 5223
6852d7b6 5224 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
8ad35755 5225 /* Only one cpu at a time may enter/leave the STOPPED state. */
433b9ee4 5226 spin_lock(&vcpu->kvm->arch.start_stop_lock);
8ad35755
DH
5227 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
5228
fe28c786
JF
5229 /* Let's tell the UV that we want to change into the operating state */
5230 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5231 r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR);
5232 if (r) {
5233 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5234 return r;
5235 }
5236 }
5237
8ad35755 5238 for (i = 0; i < online_vcpus; i++) {
113d10bc 5239 if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i)))
8ad35755
DH
5240 started_vcpus++;
5241 }
5242
5243 if (started_vcpus == 0) {
5244 /* we're the only active VCPU -> speed it up */
5245 __enable_ibs_on_vcpu(vcpu);
5246 } else if (started_vcpus == 1) {
5247 /*
5248 * As we are starting a second VCPU, we have to disable
5249 * the IBS facility on all VCPUs to remove potentially
38860756 5250 * outstanding ENABLE requests.
8ad35755
DH
5251 */
5252 __disable_ibs_on_all_vcpus(vcpu->kvm);
5253 }
5254
9daecfc6 5255 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
72f21820
CB
5256 /*
5257 * The real PSW might have changed due to a RESTART interpreted by the
5258 * ultravisor. We block all interrupts and let the next sie exit
5259 * refresh our view.
5260 */
5261 if (kvm_s390_pv_cpu_is_protected(vcpu))
5262 vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
8ad35755
DH
5263 /*
5264 * Another VCPU might have used IBS while we were offline.
5265 * Let's play safe and flush the VCPU at startup.
5266 */
d3d692c8 5267 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
433b9ee4 5268 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
fe28c786 5269 return 0;
6852d7b6
DH
5270}
5271
fe28c786 5272int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
6852d7b6 5273{
fe28c786 5274 int i, online_vcpus, r = 0, started_vcpus = 0;
8ad35755
DH
5275 struct kvm_vcpu *started_vcpu = NULL;
5276
5277 if (is_vcpu_stopped(vcpu))
fe28c786 5278 return 0;
8ad35755 5279
6852d7b6 5280 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
8ad35755 5281 /* Only one cpu at a time may enter/leave the STOPPED state. */
433b9ee4 5282 spin_lock(&vcpu->kvm->arch.start_stop_lock);
8ad35755
DH
5283 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
5284
fe28c786
JF
5285 /* Let's tell the UV that we want to change into the stopped state */
5286 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5287 r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP);
5288 if (r) {
5289 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5290 return r;
5291 }
5292 }
5293
812de046
EF
5294 /*
5295 * Set the VCPU to STOPPED and THEN clear the interrupt flag,
5296 * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders
5297 * have been fully processed. This will ensure that the VCPU
5298 * is kept BUSY if another VCPU is inquiring with SIGP SENSE.
5299 */
5300 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
6cddd432 5301 kvm_s390_clear_stop_irq(vcpu);
32f5ff63 5302
8ad35755
DH
5303 __disable_ibs_on_vcpu(vcpu);
5304
5305 for (i = 0; i < online_vcpus; i++) {
113d10bc
MZ
5306 struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i);
5307
5308 if (!is_vcpu_stopped(tmp)) {
8ad35755 5309 started_vcpus++;
113d10bc 5310 started_vcpu = tmp;
8ad35755
DH
5311 }
5312 }
5313
5314 if (started_vcpus == 1) {
5315 /*
5316 * As we only have one VCPU left, we want to enable the
5317 * IBS facility for that VCPU to speed it up.
5318 */
5319 __enable_ibs_on_vcpu(started_vcpu);
5320 }
5321
433b9ee4 5322 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
fe28c786 5323 return 0;
6852d7b6
DH
5324}
5325
d6712df9
CH
5326static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
5327 struct kvm_enable_cap *cap)
5328{
5329 int r;
5330
5331 if (cap->flags)
5332 return -EINVAL;
5333
5334 switch (cap->cap) {
fa6b7fe9
CH
5335 case KVM_CAP_S390_CSS_SUPPORT:
5336 if (!vcpu->kvm->arch.css_support) {
5337 vcpu->kvm->arch.css_support = 1;
c92ea7b9 5338 VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
fa6b7fe9
CH
5339 trace_kvm_s390_enable_css(vcpu->kvm);
5340 }
5341 r = 0;
5342 break;
d6712df9
CH
5343 default:
5344 r = -EINVAL;
5345 break;
5346 }
5347 return r;
5348}
5349
0e1234c0
JSG
5350static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu,
5351 struct kvm_s390_mem_op *mop)
19e12277
JF
5352{
5353 void __user *uaddr = (void __user *)mop->buf;
b99f4512 5354 void *sida_addr;
19e12277
JF
5355 int r = 0;
5356
5357 if (mop->flags || !mop->size)
5358 return -EINVAL;
5359 if (mop->size + mop->sida_offset < mop->size)
5360 return -EINVAL;
5361 if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block))
5362 return -E2BIG;
2c212e1b
JSG
5363 if (!kvm_s390_pv_cpu_is_protected(vcpu))
5364 return -EINVAL;
19e12277 5365
b99f4512
NB
5366 sida_addr = (char *)sida_addr(vcpu->arch.sie_block) + mop->sida_offset;
5367
19e12277
JF
5368 switch (mop->op) {
5369 case KVM_S390_MEMOP_SIDA_READ:
b99f4512 5370 if (copy_to_user(uaddr, sida_addr, mop->size))
19e12277
JF
5371 r = -EFAULT;
5372
5373 break;
5374 case KVM_S390_MEMOP_SIDA_WRITE:
b99f4512 5375 if (copy_from_user(sida_addr, uaddr, mop->size))
19e12277
JF
5376 r = -EFAULT;
5377 break;
5378 }
5379 return r;
5380}
0e1234c0
JSG
5381
5382static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu,
5383 struct kvm_s390_mem_op *mop)
41408c28
TH
5384{
5385 void __user *uaddr = (void __user *)mop->buf;
701422b3 5386 enum gacc_mode acc_mode;
41408c28 5387 void *tmpbuf = NULL;
a41f505e 5388 int r;
41408c28 5389
a41f505e
JSG
5390 r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_INJECT_EXCEPTION |
5391 KVM_S390_MEMOP_F_CHECK_ONLY |
5392 KVM_S390_MEMOP_F_SKEY_PROTECTION);
5393 if (r)
5394 return r;
5395 if (mop->ar >= NUM_ACRS)
41408c28 5396 return -EINVAL;
19e12277
JF
5397 if (kvm_s390_pv_cpu_is_protected(vcpu))
5398 return -EINVAL;
41408c28
TH
5399 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
5400 tmpbuf = vmalloc(mop->size);
5401 if (!tmpbuf)
5402 return -ENOMEM;
5403 }
5404
701422b3
JSG
5405 acc_mode = mop->op == KVM_S390_MEMOP_LOGICAL_READ ? GACC_FETCH : GACC_STORE;
5406 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
5407 r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size,
5408 acc_mode, mop->key);
5409 goto out_inject;
5410 }
5411 if (acc_mode == GACC_FETCH) {
e9e9feeb
JSG
5412 r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
5413 mop->size, mop->key);
701422b3
JSG
5414 if (r)
5415 goto out_inject;
5416 if (copy_to_user(uaddr, tmpbuf, mop->size)) {
5417 r = -EFAULT;
5418 goto out_free;
41408c28 5419 }
701422b3 5420 } else {
41408c28
TH
5421 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
5422 r = -EFAULT;
701422b3 5423 goto out_free;
41408c28 5424 }
e9e9feeb
JSG
5425 r = write_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
5426 mop->size, mop->key);
41408c28
TH
5427 }
5428
701422b3 5429out_inject:
41408c28
TH
5430 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
5431 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
5432
701422b3 5433out_free:
41408c28
TH
5434 vfree(tmpbuf);
5435 return r;
5436}
5437
0e1234c0
JSG
5438static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu,
5439 struct kvm_s390_mem_op *mop)
19e12277
JF
5440{
5441 int r, srcu_idx;
5442
5443 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
5444
5445 switch (mop->op) {
5446 case KVM_S390_MEMOP_LOGICAL_READ:
5447 case KVM_S390_MEMOP_LOGICAL_WRITE:
0e1234c0 5448 r = kvm_s390_vcpu_mem_op(vcpu, mop);
19e12277
JF
5449 break;
5450 case KVM_S390_MEMOP_SIDA_READ:
5451 case KVM_S390_MEMOP_SIDA_WRITE:
5452 /* we are locked against sida going away by the vcpu->mutex */
0e1234c0 5453 r = kvm_s390_vcpu_sida_op(vcpu, mop);
19e12277
JF
5454 break;
5455 default:
5456 r = -EINVAL;
5457 }
5458
5459 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
5460 return r;
5461}
5462
5cb0944c
PB
5463long kvm_arch_vcpu_async_ioctl(struct file *filp,
5464 unsigned int ioctl, unsigned long arg)
b0c632db
HC
5465{
5466 struct kvm_vcpu *vcpu = filp->private_data;
5467 void __user *argp = (void __user *)arg;
1ad1fa82 5468 int rc;
b0c632db 5469
93736624 5470 switch (ioctl) {
47b43c52
JF
5471 case KVM_S390_IRQ: {
5472 struct kvm_s390_irq s390irq;
5473
47b43c52 5474 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
9b062471 5475 return -EFAULT;
1ad1fa82
IL
5476 rc = kvm_s390_inject_vcpu(vcpu, &s390irq);
5477 break;
47b43c52 5478 }
93736624 5479 case KVM_S390_INTERRUPT: {
ba5c1e9b 5480 struct kvm_s390_interrupt s390int;
53936b5b 5481 struct kvm_s390_irq s390irq = {};
ba5c1e9b
CO
5482
5483 if (copy_from_user(&s390int, argp, sizeof(s390int)))
9b062471 5484 return -EFAULT;
383d0b05
JF
5485 if (s390int_to_s390irq(&s390int, &s390irq))
5486 return -EINVAL;
1ad1fa82
IL
5487 rc = kvm_s390_inject_vcpu(vcpu, &s390irq);
5488 break;
ba5c1e9b 5489 }
1ad1fa82
IL
5490 default:
5491 rc = -ENOIOCTLCMD;
5492 break;
9b062471 5493 }
1ad1fa82
IL
5494
5495 /*
5496 * To simplify single stepping of userspace-emulated instructions,
5497 * KVM_EXIT_S390_SIEIC exit sets KVM_GUESTDBG_EXIT_PENDING (see
5498 * should_handle_per_ifetch()). However, if userspace emulation injects
5499 * an interrupt, it needs to be cleared, so that KVM_EXIT_DEBUG happens
5500 * after (and not before) the interrupt delivery.
5501 */
5502 if (!rc)
5503 vcpu->guest_debug &= ~KVM_GUESTDBG_EXIT_PENDING;
5504
5505 return rc;
5cb0944c
PB
5506}
5507
8aba0958
JF
5508static int kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu *vcpu,
5509 struct kvm_pv_cmd *cmd)
5510{
5511 struct kvm_s390_pv_dmp dmp;
5512 void *data;
5513 int ret;
5514
5515 /* Dump initialization is a prerequisite */
5516 if (!vcpu->kvm->arch.pv.dumping)
5517 return -EINVAL;
5518
5519 if (copy_from_user(&dmp, (__u8 __user *)cmd->data, sizeof(dmp)))
5520 return -EFAULT;
5521
5522 /* We only handle this subcmd right now */
5523 if (dmp.subcmd != KVM_PV_DUMP_CPU)
5524 return -EINVAL;
5525
5526 /* CPU dump length is the same as create cpu storage donation. */
5527 if (dmp.buff_len != uv_info.guest_cpu_stor_len)
5528 return -EINVAL;
5529
5530 data = kvzalloc(uv_info.guest_cpu_stor_len, GFP_KERNEL);
5531 if (!data)
5532 return -ENOMEM;
5533
5534 ret = kvm_s390_pv_dump_cpu(vcpu, data, &cmd->rc, &cmd->rrc);
5535
5536 VCPU_EVENT(vcpu, 3, "PROTVIRT DUMP CPU %d rc %x rrc %x",
5537 vcpu->vcpu_id, cmd->rc, cmd->rrc);
5538
5539 if (ret)
5540 ret = -EINVAL;
5541
5542 /* On success copy over the dump data */
5543 if (!ret && copy_to_user((__u8 __user *)dmp.buff_addr, data, uv_info.guest_cpu_stor_len))
5544 ret = -EFAULT;
5545
5546 kvfree(data);
5547 return ret;
5548}
5549
5cb0944c
PB
5550long kvm_arch_vcpu_ioctl(struct file *filp,
5551 unsigned int ioctl, unsigned long arg)
5552{
5553 struct kvm_vcpu *vcpu = filp->private_data;
5554 void __user *argp = (void __user *)arg;
5555 int idx;
5556 long r;
8a8378fa 5557 u16 rc, rrc;
9b062471
CD
5558
5559 vcpu_load(vcpu);
5560
5561 switch (ioctl) {
b0c632db 5562 case KVM_S390_STORE_STATUS:
800c1065 5563 idx = srcu_read_lock(&vcpu->kvm->srcu);
55680890 5564 r = kvm_s390_store_status_unloaded(vcpu, arg);
800c1065 5565 srcu_read_unlock(&vcpu->kvm->srcu, idx);
bc923cc9 5566 break;
b0c632db
HC
5567 case KVM_S390_SET_INITIAL_PSW: {
5568 psw_t psw;
5569
bc923cc9 5570 r = -EFAULT;
b0c632db 5571 if (copy_from_user(&psw, argp, sizeof(psw)))
bc923cc9
AK
5572 break;
5573 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
5574 break;
b0c632db 5575 }
7de3f142
JF
5576 case KVM_S390_CLEAR_RESET:
5577 r = 0;
5578 kvm_arch_vcpu_ioctl_clear_reset(vcpu);
8a8378fa
JF
5579 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5580 r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
5581 UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc);
5582 VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x",
5583 rc, rrc);
5584 }
7de3f142 5585 break;
b0c632db 5586 case KVM_S390_INITIAL_RESET:
7de3f142
JF
5587 r = 0;
5588 kvm_arch_vcpu_ioctl_initial_reset(vcpu);
8a8378fa
JF
5589 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5590 r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
5591 UVC_CMD_CPU_RESET_INITIAL,
5592 &rc, &rrc);
5593 VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x",
5594 rc, rrc);
5595 }
7de3f142
JF
5596 break;
5597 case KVM_S390_NORMAL_RESET:
5598 r = 0;
5599 kvm_arch_vcpu_ioctl_normal_reset(vcpu);
8a8378fa
JF
5600 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5601 r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
5602 UVC_CMD_CPU_RESET, &rc, &rrc);
5603 VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x",
5604 rc, rrc);
5605 }
bc923cc9 5606 break;
14eebd91
CO
5607 case KVM_SET_ONE_REG:
5608 case KVM_GET_ONE_REG: {
5609 struct kvm_one_reg reg;
68cf7b1f
JF
5610 r = -EINVAL;
5611 if (kvm_s390_pv_cpu_is_protected(vcpu))
5612 break;
14eebd91
CO
5613 r = -EFAULT;
5614 if (copy_from_user(&reg, argp, sizeof(reg)))
5615 break;
5616 if (ioctl == KVM_SET_ONE_REG)
5617 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
5618 else
5619 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
5620 break;
5621 }
27e0393f
CO
5622#ifdef CONFIG_KVM_S390_UCONTROL
5623 case KVM_S390_UCAS_MAP: {
5624 struct kvm_s390_ucas_mapping ucasmap;
5625
5626 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
5627 r = -EFAULT;
5628 break;
5629 }
5630
5631 if (!kvm_is_ucontrol(vcpu->kvm)) {
5632 r = -EINVAL;
5633 break;
5634 }
5635
5636 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
5637 ucasmap.vcpu_addr, ucasmap.length);
5638 break;
5639 }
5640 case KVM_S390_UCAS_UNMAP: {
5641 struct kvm_s390_ucas_mapping ucasmap;
5642
5643 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
5644 r = -EFAULT;
5645 break;
5646 }
5647
5648 if (!kvm_is_ucontrol(vcpu->kvm)) {
5649 r = -EINVAL;
5650 break;
5651 }
5652
5653 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
5654 ucasmap.length);
5655 break;
5656 }
5657#endif
ccc7910f 5658 case KVM_S390_VCPU_FAULT: {
527e30b4 5659 r = gmap_fault(vcpu->arch.gmap, arg, 0);
ccc7910f
CO
5660 break;
5661 }
d6712df9
CH
5662 case KVM_ENABLE_CAP:
5663 {
5664 struct kvm_enable_cap cap;
5665 r = -EFAULT;
5666 if (copy_from_user(&cap, argp, sizeof(cap)))
5667 break;
5668 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
5669 break;
5670 }
41408c28
TH
5671 case KVM_S390_MEM_OP: {
5672 struct kvm_s390_mem_op mem_op;
5673
5674 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
0e1234c0 5675 r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op);
41408c28
TH
5676 else
5677 r = -EFAULT;
5678 break;
5679 }
816c7667
JF
5680 case KVM_S390_SET_IRQ_STATE: {
5681 struct kvm_s390_irq_state irq_state;
5682
5683 r = -EFAULT;
5684 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
5685 break;
5686 if (irq_state.len > VCPU_IRQS_MAX_BUF ||
5687 irq_state.len == 0 ||
5688 irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
5689 r = -EINVAL;
5690 break;
5691 }
bb64da9a 5692 /* do not use irq_state.flags, it will break old QEMUs */
816c7667
JF
5693 r = kvm_s390_set_irq_state(vcpu,
5694 (void __user *) irq_state.buf,
5695 irq_state.len);
5696 break;
5697 }
5698 case KVM_S390_GET_IRQ_STATE: {
5699 struct kvm_s390_irq_state irq_state;
5700
5701 r = -EFAULT;
5702 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
5703 break;
5704 if (irq_state.len == 0) {
5705 r = -EINVAL;
5706 break;
5707 }
bb64da9a 5708 /* do not use irq_state.flags, it will break old QEMUs */
816c7667
JF
5709 r = kvm_s390_get_irq_state(vcpu,
5710 (__u8 __user *) irq_state.buf,
5711 irq_state.len);
8aba0958
JF
5712 break;
5713 }
5714 case KVM_S390_PV_CPU_COMMAND: {
5715 struct kvm_pv_cmd cmd;
5716
5717 r = -EINVAL;
5718 if (!is_prot_virt_host())
5719 break;
5720
5721 r = -EFAULT;
5722 if (copy_from_user(&cmd, argp, sizeof(cmd)))
5723 break;
5724
5725 r = -EINVAL;
5726 if (cmd.flags)
5727 break;
5728
5729 /* We only handle this cmd right now */
5730 if (cmd.cmd != KVM_PV_DUMP)
5731 break;
5732
5733 r = kvm_s390_handle_pv_vcpu_dump(vcpu, &cmd);
5734
5735 /* Always copy over UV rc / rrc data */
5736 if (copy_to_user((__u8 __user *)argp, &cmd.rc,
5737 sizeof(cmd.rc) + sizeof(cmd.rrc)))
5738 r = -EFAULT;
816c7667
JF
5739 break;
5740 }
b0c632db 5741 default:
3e6afcf1 5742 r = -ENOTTY;
b0c632db 5743 }
9b062471
CD
5744
5745 vcpu_put(vcpu);
bc923cc9 5746 return r;
b0c632db
HC
5747}
5748
1499fa80 5749vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
5b1c1493
CO
5750{
5751#ifdef CONFIG_KVM_S390_UCONTROL
5752 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
5753 && (kvm_is_ucontrol(vcpu->kvm))) {
5754 vmf->page = virt_to_page(vcpu->arch.sie_block);
5755 get_page(vmf->page);
5756 return 0;
5757 }
5758#endif
5759 return VM_FAULT_SIGBUS;
5760}
5761
d663b8a2
PB
5762bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
5763{
5764 return true;
5765}
5766
b0c632db 5767/* Section: memory related */
f7784b8e 5768int kvm_arch_prepare_memory_region(struct kvm *kvm,
537a17b3
SC
5769 const struct kvm_memory_slot *old,
5770 struct kvm_memory_slot *new,
7b6195a9 5771 enum kvm_mr_change change)
b0c632db 5772{
ec5c8697
SC
5773 gpa_t size;
5774
5775 /* When we are protected, we should not change the memory slots */
5776 if (kvm_s390_pv_get_handle(kvm))
5777 return -EINVAL;
5778
f2d3155e
NB
5779 if (change != KVM_MR_DELETE && change != KVM_MR_FLAGS_ONLY) {
5780 /*
5781 * A few sanity checks. We can have memory slots which have to be
5782 * located/ended at a segment boundary (1MB). The memory in userland is
5783 * ok to be fragmented into various different vmas. It is okay to mmap()
5784 * and munmap() stuff in this slot after doing this call at any time
5785 */
cf5b4869 5786
f2d3155e
NB
5787 if (new->userspace_addr & 0xffffful)
5788 return -EINVAL;
b0c632db 5789
f2d3155e
NB
5790 size = new->npages * PAGE_SIZE;
5791 if (size & 0xffffful)
5792 return -EINVAL;
b0c632db 5793
f2d3155e
NB
5794 if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit)
5795 return -EINVAL;
5796 }
b0c632db 5797
f2d3155e
NB
5798 if (!kvm->arch.migration_mode)
5799 return 0;
5800
5801 /*
5802 * Turn off migration mode when:
5803 * - userspace creates a new memslot with dirty logging off,
5804 * - userspace modifies an existing memslot (MOVE or FLAGS_ONLY) and
5805 * dirty logging is turned off.
5806 * Migration mode expects dirty page logging being enabled to store
5807 * its dirty bitmap.
5808 */
5809 if (change != KVM_MR_DELETE &&
5810 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
5811 WARN(kvm_s390_vm_stop_migration(kvm),
5812 "Failed to stop migration mode");
a3a92c31 5813
f7784b8e
MT
5814 return 0;
5815}
5816
5817void kvm_arch_commit_memory_region(struct kvm *kvm,
9d4c197c 5818 struct kvm_memory_slot *old,
f36f3f28 5819 const struct kvm_memory_slot *new,
8482644a 5820 enum kvm_mr_change change)
f7784b8e 5821{
19ec166c 5822 int rc = 0;
598841ca 5823
19ec166c
CB
5824 switch (change) {
5825 case KVM_MR_DELETE:
5826 rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
5827 old->npages * PAGE_SIZE);
5828 break;
5829 case KVM_MR_MOVE:
5830 rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
5831 old->npages * PAGE_SIZE);
5832 if (rc)
5833 break;
3b684a42 5834 fallthrough;
19ec166c 5835 case KVM_MR_CREATE:
cf5b4869
SC
5836 rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr,
5837 new->base_gfn * PAGE_SIZE,
5838 new->npages * PAGE_SIZE);
19ec166c
CB
5839 break;
5840 case KVM_MR_FLAGS_ONLY:
5841 break;
5842 default:
5843 WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
5844 }
598841ca 5845 if (rc)
ea2cdd27 5846 pr_warn("failed to commit memory region\n");
598841ca 5847 return;
b0c632db
HC
5848}
5849
60a37709
AY
5850static inline unsigned long nonhyp_mask(int i)
5851{
5852 unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
5853
5854 return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
5855}
5856
b0c632db
HC
5857static int __init kvm_s390_init(void)
5858{
b8449265 5859 int i, r;
60a37709 5860
07197fd0 5861 if (!sclp.has_sief2) {
8d43d570 5862 pr_info("SIE is not available\n");
07197fd0
DH
5863 return -ENODEV;
5864 }
5865
a4499382 5866 if (nested && hpage) {
8d43d570 5867 pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
a4499382
JF
5868 return -EINVAL;
5869 }
5870
60a37709 5871 for (i = 0; i < 16; i++)
c3b9e3e1 5872 kvm_s390_fac_base[i] |=
17e89e13 5873 stfle_fac_list[i] & nonhyp_mask(i);
60a37709 5874
b8449265
SC
5875 r = __kvm_s390_init();
5876 if (r)
5877 return r;
5878
81a1cf9f 5879 r = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
b8449265
SC
5880 if (r) {
5881 __kvm_s390_exit();
5882 return r;
5883 }
5884 return 0;
b0c632db
HC
5885}
5886
5887static void __exit kvm_s390_exit(void)
5888{
5889 kvm_exit();
b8449265
SC
5890
5891 __kvm_s390_exit();
b0c632db
HC
5892}
5893
5894module_init(kvm_s390_init);
5895module_exit(kvm_s390_exit);
566af940
CH
5896
5897/*
5898 * Enable autoloading of the kvm module.
5899 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
5900 * since x86 takes a different approach.
5901 */
5902#include <linux/miscdevice.h>
5903MODULE_ALIAS_MISCDEV(KVM_MINOR);
5904MODULE_ALIAS("devname:kvm");