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