1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2b0c632dbSHeiko Carstens /* 3bb64da9aSChristian Borntraeger * hosting IBM Z kernel virtual machines (s390x) 4b0c632dbSHeiko Carstens * 53e6c5568SJanosch Frank * Copyright IBM Corp. 2008, 2020 6b0c632dbSHeiko Carstens * 7b0c632dbSHeiko Carstens * Author(s): Carsten Otte <cotte@de.ibm.com> 8b0c632dbSHeiko Carstens * Christian Borntraeger <borntraeger@de.ibm.com> 9628eb9b8SChristian Ehrhardt * Christian Ehrhardt <ehrhardt@de.ibm.com> 1015f36ebdSJason J. Herne * Jason J. Herne <jjherne@us.ibm.com> 11b0c632dbSHeiko Carstens */ 12b0c632dbSHeiko Carstens 137aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390" 147aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 157aedd9d4SMichael Mueller 16b0c632dbSHeiko Carstens #include <linux/compiler.h> 17b0c632dbSHeiko Carstens #include <linux/err.h> 18b0c632dbSHeiko Carstens #include <linux/fs.h> 19ca872302SChristian Borntraeger #include <linux/hrtimer.h> 20b0c632dbSHeiko Carstens #include <linux/init.h> 21b0c632dbSHeiko Carstens #include <linux/kvm.h> 22b0c632dbSHeiko Carstens #include <linux/kvm_host.h> 23b2d73b2aSMartin Schwidefsky #include <linux/mman.h> 24b0c632dbSHeiko Carstens #include <linux/module.h> 25d3217967SPaul Gortmaker #include <linux/moduleparam.h> 26a374e892STony Krowiak #include <linux/random.h> 27b0c632dbSHeiko Carstens #include <linux/slab.h> 28ba5c1e9bSCarsten Otte #include <linux/timer.h> 2941408c28SThomas Huth #include <linux/vmalloc.h> 3015c9705fSDavid Hildenbrand #include <linux/bitmap.h> 31174cd4b1SIngo Molnar #include <linux/sched/signal.h> 32190df4a2SClaudio Imbrenda #include <linux/string.h> 3365fddcfcSMike Rapoport #include <linux/pgtable.h> 34ca2fd060SClaudio Imbrenda #include <linux/mmu_notifier.h> 35174cd4b1SIngo Molnar 36cbb870c8SHeiko Carstens #include <asm/asm-offsets.h> 37b0c632dbSHeiko Carstens #include <asm/lowcore.h> 38fd5ada04SMartin Schwidefsky #include <asm/stp.h> 391e133ab2SMartin Schwidefsky #include <asm/gmap.h> 40f5daba1dSHeiko Carstens #include <asm/nmi.h> 41a0616cdeSDavid Howells #include <asm/switch_to.h> 426d3da241SJens Freimann #include <asm/isc.h> 431526bf9cSChristian Borntraeger #include <asm/sclp.h> 440a763c78SDavid Hildenbrand #include <asm/cpacf.h> 45221bb8a4SLinus Torvalds #include <asm/timex.h> 46e585b24aSTony Krowiak #include <asm/ap.h> 4729b40f10SJanosch Frank #include <asm/uv.h> 4856e62a73SSven Schnelle #include <asm/fpu/api.h> 498f2abe6aSChristian Borntraeger #include "kvm-s390.h" 50b0c632dbSHeiko Carstens #include "gaccess.h" 5198b1d33dSMatthew Rosato #include "pci.h" 52b0c632dbSHeiko Carstens 535786fffaSCornelia Huck #define CREATE_TRACE_POINTS 545786fffaSCornelia Huck #include "trace.h" 55ade38c31SCornelia Huck #include "trace-s390.h" 565786fffaSCornelia Huck 5741408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */ 58816c7667SJens Freimann #define LOCAL_IRQS 32 59816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \ 60816c7667SJens Freimann (KVM_MAX_VCPUS + LOCAL_IRQS)) 6141408c28SThomas Huth 62fcfe1baeSJing Zhang const struct _kvm_stats_desc kvm_vm_stats_desc[] = { 63fcfe1baeSJing Zhang KVM_GENERIC_VM_STATS(), 64fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_io), 65fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_float_mchk), 66fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_pfault_done), 67fcfe1baeSJing Zhang STATS_DESC_COUNTER(VM, inject_service_signal), 6873f91b00SMatthew Rosato STATS_DESC_COUNTER(VM, inject_virtio), 6973f91b00SMatthew Rosato STATS_DESC_COUNTER(VM, aen_forward) 70fcfe1baeSJing Zhang }; 71fcfe1baeSJing Zhang 72fcfe1baeSJing Zhang const struct kvm_stats_header kvm_vm_stats_header = { 73fcfe1baeSJing Zhang .name_size = KVM_STATS_NAME_SIZE, 74fcfe1baeSJing Zhang .num_desc = ARRAY_SIZE(kvm_vm_stats_desc), 75fcfe1baeSJing Zhang .id_offset = sizeof(struct kvm_stats_header), 76fcfe1baeSJing Zhang .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, 77fcfe1baeSJing Zhang .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + 78fcfe1baeSJing Zhang sizeof(kvm_vm_stats_desc), 79fcfe1baeSJing Zhang }; 80fcfe1baeSJing Zhang 81ce55c049SJing Zhang const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { 82ce55c049SJing Zhang KVM_GENERIC_VCPU_STATS(), 83ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_userspace), 84ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_null), 85ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_external_request), 86ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_io_request), 87ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_external_interrupt), 88ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_stop_request), 89ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_validity), 90ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_instruction), 91ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_pei), 92ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, halt_no_poll_steal), 93ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lctl), 94ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lctlg), 95ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stctl), 96ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stctg), 97ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_program_interruption), 98ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_instr_and_program), 99ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_operation_exception), 100ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_ckc), 101ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_cputm), 102ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_external_call), 103ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_emergency_signal), 104ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_service_signal), 105ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_virtio), 106ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_stop_signal), 107ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_prefix_signal), 108ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_restart_signal), 109ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_program), 110ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_io), 111ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, deliver_machine_check), 112ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, exit_wait_state), 113ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_ckc), 114ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_cputm), 115ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_external_call), 116ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_emergency_signal), 117ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_mchk), 118ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_pfault_init), 119ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_program), 120ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_restart), 121ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_set_prefix), 122ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, inject_stop_signal), 123ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_epsw), 124ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_gs), 125ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_io_other), 126ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lpsw), 127ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_lpswe), 128ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_pfmf), 129ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ptff), 130ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sck), 131ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sckpf), 132ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stidp), 133ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_spx), 134ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stpx), 135ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stap), 136ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_iske), 137ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ri), 138ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_rrbe), 139ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sske), 140ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock), 141ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stsi), 142ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_stfl), 143ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tb), 144ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tpi), 145ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tprot), 146ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_tsch), 147ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sie), 148ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_essa), 149ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sthyi), 150ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_sense), 151ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running), 152ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call), 153ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency), 154ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency), 155ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_start), 156ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_stop), 157ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status), 158ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status), 159ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status), 160ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_arch), 161ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix), 162ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_restart), 163ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset), 164ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset), 165ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown), 166bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_10), 167bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_44), 168bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c), 169bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, diag_9c_ignored), 170bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, diag_9c_forward), 171bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_258), 172bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_308), 173bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_500), 174bb000f64SChristian Borntraeger STATS_DESC_COUNTER(VCPU, instruction_diagnose_other), 175ce55c049SJing Zhang STATS_DESC_COUNTER(VCPU, pfault_sync) 176ce55c049SJing Zhang }; 177ce55c049SJing Zhang 178ce55c049SJing Zhang const struct kvm_stats_header kvm_vcpu_stats_header = { 179ce55c049SJing Zhang .name_size = KVM_STATS_NAME_SIZE, 180ce55c049SJing Zhang .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc), 181ce55c049SJing Zhang .id_offset = sizeof(struct kvm_stats_header), 182ce55c049SJing Zhang .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE, 183ce55c049SJing Zhang .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE + 184ce55c049SJing Zhang sizeof(kvm_vcpu_stats_desc), 185ce55c049SJing Zhang }; 186ce55c049SJing Zhang 187a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */ 188a411edf1SDavid Hildenbrand static int nested; 189a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO); 190a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support"); 191a411edf1SDavid Hildenbrand 192a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */ 193a4499382SJanosch Frank static int hpage; 194a4499382SJanosch Frank module_param(hpage, int, 0444); 195a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support"); 196b0c632dbSHeiko Carstens 1978b905d28SChristian Borntraeger /* maximum percentage of steal time for polling. >100 is treated like 100 */ 1988b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10; 1998b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644); 200b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling"); 2018b905d28SChristian Borntraeger 202cc674ef2SMichael Mueller /* if set to true, the GISA will be initialized and used if available */ 203cc674ef2SMichael Mueller static bool use_gisa = true; 204cc674ef2SMichael Mueller module_param(use_gisa, bool, 0644); 205cc674ef2SMichael Mueller MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it."); 206cc674ef2SMichael Mueller 20787e28a15SPierre Morel /* maximum diag9c forwarding per second */ 20887e28a15SPierre Morel unsigned int diag9c_forwarding_hz; 20987e28a15SPierre Morel module_param(diag9c_forwarding_hz, uint, 0644); 21087e28a15SPierre Morel MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off"); 21187e28a15SPierre Morel 212*fb491d55SClaudio Imbrenda static int async_destroy; 213*fb491d55SClaudio Imbrenda 214c3b9e3e1SChristian Borntraeger /* 215c3b9e3e1SChristian Borntraeger * For now we handle at most 16 double words as this is what the s390 base 216c3b9e3e1SChristian Borntraeger * kernel handles and stores in the prefix page. If we ever need to go beyond 217c3b9e3e1SChristian Borntraeger * this, this requires changes to code, but the external uapi can stay. 218c3b9e3e1SChristian Borntraeger */ 219c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16 220c3b9e3e1SChristian Borntraeger 221c3b9e3e1SChristian Borntraeger /* 222c3b9e3e1SChristian Borntraeger * Base feature mask that defines default mask for facilities. Consists of the 223c3b9e3e1SChristian Borntraeger * defines in FACILITIES_KVM and the non-hypervisor managed bits. 224c3b9e3e1SChristian Borntraeger */ 225c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM }; 226c3b9e3e1SChristian Borntraeger /* 227c3b9e3e1SChristian Borntraeger * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL 228c3b9e3e1SChristian Borntraeger * and defines the facilities that can be enabled via a cpu model. 229c3b9e3e1SChristian Borntraeger */ 230c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL }; 231c3b9e3e1SChristian Borntraeger 232c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void) 23378c4b59fSMichael Mueller { 234c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64); 235c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64); 236c3b9e3e1SChristian Borntraeger BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) > 23717e89e13SSven Schnelle sizeof(stfle_fac_list)); 238c3b9e3e1SChristian Borntraeger 239c3b9e3e1SChristian Borntraeger return SIZE_INTERNAL; 24078c4b59fSMichael Mueller } 24178c4b59fSMichael Mueller 24215c9705fSDavid Hildenbrand /* available cpu features supported by kvm */ 24315c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 2440a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */ 2450a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc; 24615c9705fSDavid Hildenbrand 2479d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier; 248a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier; 24978f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf; 2503e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv; 2519d8d5786SMichael Mueller 252b0c632dbSHeiko Carstens /* Section: not file related */ 25313a34e06SRadim Krčmář int kvm_arch_hardware_enable(void) 254b0c632dbSHeiko Carstens { 255b0c632dbSHeiko Carstens /* every s390 is virtualization enabled ;-) */ 25610474ae8SAlexander Graf return 0; 257b0c632dbSHeiko Carstens } 258b0c632dbSHeiko Carstens 259b9904085SSean Christopherson int kvm_arch_check_processor_compat(void *opaque) 260f257d6dcSSean Christopherson { 261f257d6dcSSean Christopherson return 0; 262f257d6dcSSean Christopherson } 263f257d6dcSSean Christopherson 26429b40f10SJanosch Frank /* forward declarations */ 265414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 266414d3b07SMartin Schwidefsky unsigned long end); 26729b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm); 2682c70fe44SChristian Borntraeger 2691575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta) 2701575767eSDavid Hildenbrand { 2711575767eSDavid Hildenbrand u8 delta_idx = 0; 2721575767eSDavid Hildenbrand 2731575767eSDavid Hildenbrand /* 2741575767eSDavid Hildenbrand * The TOD jumps by delta, we have to compensate this by adding 2751575767eSDavid Hildenbrand * -delta to the epoch. 2761575767eSDavid Hildenbrand */ 2771575767eSDavid Hildenbrand delta = -delta; 2781575767eSDavid Hildenbrand 2791575767eSDavid Hildenbrand /* sign-extension - we're adding to signed values below */ 2801575767eSDavid Hildenbrand if ((s64)delta < 0) 2811575767eSDavid Hildenbrand delta_idx = -1; 2821575767eSDavid Hildenbrand 2831575767eSDavid Hildenbrand scb->epoch += delta; 2841575767eSDavid Hildenbrand if (scb->ecd & ECD_MEF) { 2851575767eSDavid Hildenbrand scb->epdx += delta_idx; 2861575767eSDavid Hildenbrand if (scb->epoch < delta) 2871575767eSDavid Hildenbrand scb->epdx += 1; 2881575767eSDavid Hildenbrand } 2891575767eSDavid Hildenbrand } 2901575767eSDavid Hildenbrand 291fdf03650SFan Zhang /* 292fdf03650SFan Zhang * This callback is executed during stop_machine(). All CPUs are therefore 293fdf03650SFan Zhang * temporarily stopped. In order not to change guest behavior, we have to 294fdf03650SFan Zhang * disable preemption whenever we touch the epoch of kvm and the VCPUs, 295fdf03650SFan Zhang * so a CPU won't be stopped while calculating with the epoch. 296fdf03650SFan Zhang */ 297fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val, 298fdf03650SFan Zhang void *v) 299fdf03650SFan Zhang { 300fdf03650SFan Zhang struct kvm *kvm; 301fdf03650SFan Zhang struct kvm_vcpu *vcpu; 30246808a4cSMarc Zyngier unsigned long i; 303fdf03650SFan Zhang unsigned long long *delta = v; 304fdf03650SFan Zhang 305fdf03650SFan Zhang list_for_each_entry(kvm, &vm_list, vm_list) { 306fdf03650SFan Zhang kvm_for_each_vcpu(i, vcpu, kvm) { 3071575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.sie_block, *delta); 3081575767eSDavid Hildenbrand if (i == 0) { 3091575767eSDavid Hildenbrand kvm->arch.epoch = vcpu->arch.sie_block->epoch; 3101575767eSDavid Hildenbrand kvm->arch.epdx = vcpu->arch.sie_block->epdx; 3111575767eSDavid Hildenbrand } 312db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 313db0758b2SDavid Hildenbrand vcpu->arch.cputm_start += *delta; 31491473b48SDavid Hildenbrand if (vcpu->arch.vsie_block) 3151575767eSDavid Hildenbrand kvm_clock_sync_scb(vcpu->arch.vsie_block, 3161575767eSDavid Hildenbrand *delta); 317fdf03650SFan Zhang } 318fdf03650SFan Zhang } 319fdf03650SFan Zhang return NOTIFY_OK; 320fdf03650SFan Zhang } 321fdf03650SFan Zhang 322fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = { 323fdf03650SFan Zhang .notifier_call = kvm_clock_sync, 324fdf03650SFan Zhang }; 325fdf03650SFan Zhang 326b9904085SSean Christopherson int kvm_arch_hardware_setup(void *opaque) 327b0c632dbSHeiko Carstens { 3282c70fe44SChristian Borntraeger gmap_notifier.notifier_call = kvm_gmap_notifier; 329b2d73b2aSMartin Schwidefsky gmap_register_pte_notifier(&gmap_notifier); 330a3508fbeSDavid Hildenbrand vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier; 331a3508fbeSDavid Hildenbrand gmap_register_pte_notifier(&vsie_gmap_notifier); 332fdf03650SFan Zhang atomic_notifier_chain_register(&s390_epoch_delta_notifier, 333fdf03650SFan Zhang &kvm_clock_notifier); 334b0c632dbSHeiko Carstens return 0; 335b0c632dbSHeiko Carstens } 336b0c632dbSHeiko Carstens 337b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void) 338b0c632dbSHeiko Carstens { 339b2d73b2aSMartin Schwidefsky gmap_unregister_pte_notifier(&gmap_notifier); 340a3508fbeSDavid Hildenbrand gmap_unregister_pte_notifier(&vsie_gmap_notifier); 341fdf03650SFan Zhang atomic_notifier_chain_unregister(&s390_epoch_delta_notifier, 342fdf03650SFan Zhang &kvm_clock_notifier); 343b0c632dbSHeiko Carstens } 344b0c632dbSHeiko Carstens 34522be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr) 34622be5a13SDavid Hildenbrand { 34722be5a13SDavid Hildenbrand set_bit_inv(nr, kvm_s390_available_cpu_feat); 34822be5a13SDavid Hildenbrand } 34922be5a13SDavid Hildenbrand 3500a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr) 3510a763c78SDavid Hildenbrand { 3524fa3b91bSHeiko Carstens unsigned long function = (unsigned long)nr | 0x100; 353d051ae53SHeiko Carstens int cc; 3540a763c78SDavid Hildenbrand 3550a763c78SDavid Hildenbrand asm volatile( 3564fa3b91bSHeiko Carstens " lgr 0,%[function]\n" 3570a763c78SDavid Hildenbrand /* Parameter registers are ignored for "test bit" */ 3580a763c78SDavid Hildenbrand " plo 0,0,0,0(0)\n" 3590a763c78SDavid Hildenbrand " ipm %0\n" 3600a763c78SDavid Hildenbrand " srl %0,28\n" 3610a763c78SDavid Hildenbrand : "=d" (cc) 3624fa3b91bSHeiko Carstens : [function] "d" (function) 3634fa3b91bSHeiko Carstens : "cc", "0"); 3640a763c78SDavid Hildenbrand return cc == 0; 3650a763c78SDavid Hildenbrand } 3660a763c78SDavid Hildenbrand 367d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query) 368d6681397SChristian Borntraeger { 369d6681397SChristian Borntraeger asm volatile( 3704fa3b91bSHeiko Carstens " lghi 0,0\n" 3714fa3b91bSHeiko Carstens " lgr 1,%[query]\n" 3724fa3b91bSHeiko Carstens /* Parameter registers are ignored */ 373d6681397SChristian Borntraeger " .insn rrf,%[opc] << 16,2,4,6,0\n" 374b1c41ac3SHeiko Carstens : 3754fa3b91bSHeiko Carstens : [query] "d" ((unsigned long)query), [opc] "i" (opcode) 3764fa3b91bSHeiko Carstens : "cc", "memory", "0", "1"); 377d6681397SChristian Borntraeger } 378d6681397SChristian Borntraeger 379173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938 3804f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939 381173aec2dSChristian Borntraeger 38222be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void) 38322be5a13SDavid Hildenbrand { 3840a763c78SDavid Hildenbrand int i; 3850a763c78SDavid Hildenbrand 3860a763c78SDavid Hildenbrand for (i = 0; i < 256; ++i) { 3870a763c78SDavid Hildenbrand if (plo_test_bit(i)) 3880a763c78SDavid Hildenbrand kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7); 3890a763c78SDavid Hildenbrand } 3900a763c78SDavid Hildenbrand 3910a763c78SDavid Hildenbrand if (test_facility(28)) /* TOD-clock steering */ 392221bb8a4SLinus Torvalds ptff(kvm_s390_available_subfunc.ptff, 393221bb8a4SLinus Torvalds sizeof(kvm_s390_available_subfunc.ptff), 394221bb8a4SLinus Torvalds PTFF_QAF); 3950a763c78SDavid Hildenbrand 3960a763c78SDavid Hildenbrand if (test_facility(17)) { /* MSA */ 39769c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMAC, (cpacf_mask_t *) 39869c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmac); 39969c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMC, (cpacf_mask_t *) 40069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmc); 40169c0e360SMartin Schwidefsky __cpacf_query(CPACF_KM, (cpacf_mask_t *) 40269c0e360SMartin Schwidefsky kvm_s390_available_subfunc.km); 40369c0e360SMartin Schwidefsky __cpacf_query(CPACF_KIMD, (cpacf_mask_t *) 40469c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kimd); 40569c0e360SMartin Schwidefsky __cpacf_query(CPACF_KLMD, (cpacf_mask_t *) 40669c0e360SMartin Schwidefsky kvm_s390_available_subfunc.klmd); 4070a763c78SDavid Hildenbrand } 4080a763c78SDavid Hildenbrand if (test_facility(76)) /* MSA3 */ 40969c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *) 41069c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pckmo); 4110a763c78SDavid Hildenbrand if (test_facility(77)) { /* MSA4 */ 41269c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *) 41369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmctr); 41469c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMF, (cpacf_mask_t *) 41569c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmf); 41669c0e360SMartin Schwidefsky __cpacf_query(CPACF_KMO, (cpacf_mask_t *) 41769c0e360SMartin Schwidefsky kvm_s390_available_subfunc.kmo); 41869c0e360SMartin Schwidefsky __cpacf_query(CPACF_PCC, (cpacf_mask_t *) 41969c0e360SMartin Schwidefsky kvm_s390_available_subfunc.pcc); 4200a763c78SDavid Hildenbrand } 4210a763c78SDavid Hildenbrand if (test_facility(57)) /* MSA5 */ 422985a9d20SHarald Freudenberger __cpacf_query(CPACF_PRNO, (cpacf_mask_t *) 42369c0e360SMartin Schwidefsky kvm_s390_available_subfunc.ppno); 4240a763c78SDavid Hildenbrand 425e000b8e0SJason J. Herne if (test_facility(146)) /* MSA8 */ 426e000b8e0SJason J. Herne __cpacf_query(CPACF_KMA, (cpacf_mask_t *) 427e000b8e0SJason J. Herne kvm_s390_available_subfunc.kma); 428e000b8e0SJason J. Herne 42913209ad0SChristian Borntraeger if (test_facility(155)) /* MSA9 */ 43013209ad0SChristian Borntraeger __cpacf_query(CPACF_KDSA, (cpacf_mask_t *) 43113209ad0SChristian Borntraeger kvm_s390_available_subfunc.kdsa); 43213209ad0SChristian Borntraeger 433173aec2dSChristian Borntraeger if (test_facility(150)) /* SORTL */ 434173aec2dSChristian Borntraeger __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl); 435173aec2dSChristian Borntraeger 4364f45b90eSChristian Borntraeger if (test_facility(151)) /* DFLTCC */ 4374f45b90eSChristian Borntraeger __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc); 4384f45b90eSChristian Borntraeger 43922be5a13SDavid Hildenbrand if (MACHINE_HAS_ESOP) 44022be5a13SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP); 441a3508fbeSDavid Hildenbrand /* 442a3508fbeSDavid Hildenbrand * We need SIE support, ESOP (PROT_READ protection for gmap_shadow), 443a3508fbeSDavid Hildenbrand * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing). 444a3508fbeSDavid Hildenbrand */ 445a3508fbeSDavid Hildenbrand if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao || 446a411edf1SDavid Hildenbrand !test_facility(3) || !nested) 447a3508fbeSDavid Hildenbrand return; 448a3508fbeSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2); 44919c439b5SDavid Hildenbrand if (sclp.has_64bscao) 45019c439b5SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO); 4510615a326SDavid Hildenbrand if (sclp.has_siif) 4520615a326SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF); 45377d18f6dSDavid Hildenbrand if (sclp.has_gpere) 45477d18f6dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE); 455a1b7b9b2SDavid Hildenbrand if (sclp.has_gsls) 456a1b7b9b2SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS); 4575630a8e8SDavid Hildenbrand if (sclp.has_ib) 4585630a8e8SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB); 45913ee3f67SDavid Hildenbrand if (sclp.has_cei) 46013ee3f67SDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI); 4617fd7f39dSDavid Hildenbrand if (sclp.has_ibs) 4627fd7f39dSDavid Hildenbrand allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS); 463730cd632SFarhan Ali if (sclp.has_kss) 464730cd632SFarhan Ali allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS); 4655d3876a8SDavid Hildenbrand /* 4665d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make 4675d3876a8SDavid Hildenbrand * all skey handling functions read/set the skey from the PGSTE 4685d3876a8SDavid Hildenbrand * instead of the real storage key. 4695d3876a8SDavid Hildenbrand * 4705d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make 4715d3876a8SDavid Hildenbrand * pages being detected as preserved although they are resident. 4725d3876a8SDavid Hildenbrand * 4735d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will 4745d3876a8SDavid Hildenbrand * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY. 4755d3876a8SDavid Hildenbrand * 4765d3876a8SDavid Hildenbrand * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and 4775d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be 4785d3876a8SDavid Hildenbrand * correctly shadowed. We can do that for the PGSTE but not for PTE.I. 4795d3876a8SDavid Hildenbrand * 4805d3876a8SDavid Hildenbrand * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We 4815d3876a8SDavid Hildenbrand * cannot easily shadow the SCA because of the ipte lock. 4825d3876a8SDavid Hildenbrand */ 48322be5a13SDavid Hildenbrand } 48422be5a13SDavid Hildenbrand 485b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque) 486b0c632dbSHeiko Carstens { 487f76f6371SJanosch Frank int rc = -ENOMEM; 488308c3e66SMichael Mueller 48978f26131SChristian Borntraeger kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long)); 49078f26131SChristian Borntraeger if (!kvm_s390_dbf) 49178f26131SChristian Borntraeger return -ENOMEM; 49278f26131SChristian Borntraeger 4933e6c5568SJanosch Frank kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long)); 4943e6c5568SJanosch Frank if (!kvm_s390_dbf_uv) 4953e6c5568SJanosch Frank goto out; 4963e6c5568SJanosch Frank 4973e6c5568SJanosch Frank if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) || 4983e6c5568SJanosch Frank debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view)) 499f76f6371SJanosch Frank goto out; 50078f26131SChristian Borntraeger 50122be5a13SDavid Hildenbrand kvm_s390_cpu_feat_init(); 50222be5a13SDavid Hildenbrand 50384877d93SCornelia Huck /* Register floating interrupt controller interface. */ 504308c3e66SMichael Mueller rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC); 505308c3e66SMichael Mueller if (rc) { 5068d43d570SMichael Mueller pr_err("A FLIC registration call failed with rc=%d\n", rc); 507f76f6371SJanosch Frank goto out; 508308c3e66SMichael Mueller } 509b1d1e76eSMichael Mueller 510189e7d87SMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) { 51198b1d33dSMatthew Rosato rc = kvm_s390_pci_init(); 51298b1d33dSMatthew Rosato if (rc) { 51398b1d33dSMatthew Rosato pr_err("Unable to allocate AIFT for PCI\n"); 51498b1d33dSMatthew Rosato goto out; 51598b1d33dSMatthew Rosato } 51698b1d33dSMatthew Rosato } 51798b1d33dSMatthew Rosato 518b1d1e76eSMichael Mueller rc = kvm_s390_gib_init(GAL_ISC); 519b1d1e76eSMichael Mueller if (rc) 520f76f6371SJanosch Frank goto out; 521b1d1e76eSMichael Mueller 522308c3e66SMichael Mueller return 0; 523308c3e66SMichael Mueller 524f76f6371SJanosch Frank out: 525f76f6371SJanosch Frank kvm_arch_exit(); 526308c3e66SMichael Mueller return rc; 527b0c632dbSHeiko Carstens } 528b0c632dbSHeiko Carstens 52978f26131SChristian Borntraeger void kvm_arch_exit(void) 53078f26131SChristian Borntraeger { 5311282c21eSMichael Mueller kvm_s390_gib_destroy(); 532189e7d87SMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 53398b1d33dSMatthew Rosato kvm_s390_pci_exit(); 53478f26131SChristian Borntraeger debug_unregister(kvm_s390_dbf); 5353e6c5568SJanosch Frank debug_unregister(kvm_s390_dbf_uv); 53678f26131SChristian Borntraeger } 53778f26131SChristian Borntraeger 538b0c632dbSHeiko Carstens /* Section: device related */ 539b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp, 540b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 541b0c632dbSHeiko Carstens { 542b0c632dbSHeiko Carstens if (ioctl == KVM_S390_ENABLE_SIE) 543b0c632dbSHeiko Carstens return s390_enable_sie(); 544b0c632dbSHeiko Carstens return -EINVAL; 545b0c632dbSHeiko Carstens } 546b0c632dbSHeiko Carstens 547784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 548b0c632dbSHeiko Carstens { 549d7b0b5ebSCarsten Otte int r; 550d7b0b5ebSCarsten Otte 5512bd0ac4eSCarsten Otte switch (ext) { 552d7b0b5ebSCarsten Otte case KVM_CAP_S390_PSW: 553b6cf8788SChristian Borntraeger case KVM_CAP_S390_GMAP: 55452e16b18SChristian Borntraeger case KVM_CAP_SYNC_MMU: 5551efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 5561efd0f59SCarsten Otte case KVM_CAP_S390_UCONTROL: 5571efd0f59SCarsten Otte #endif 5583c038e6bSDominik Dingel case KVM_CAP_ASYNC_PF: 55960b413c9SChristian Borntraeger case KVM_CAP_SYNC_REGS: 56014eebd91SCarsten Otte case KVM_CAP_ONE_REG: 561d6712df9SCornelia Huck case KVM_CAP_ENABLE_CAP: 562fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 56310ccaa1eSCornelia Huck case KVM_CAP_IOEVENTFD: 564c05c4186SJens Freimann case KVM_CAP_DEVICE_CTRL: 56578599d90SCornelia Huck case KVM_CAP_S390_IRQCHIP: 566f2061656SDominik Dingel case KVM_CAP_VM_ATTRIBUTES: 5676352e4d2SDavid Hildenbrand case KVM_CAP_MP_STATE: 568460df4c1SPaolo Bonzini case KVM_CAP_IMMEDIATE_EXIT: 56947b43c52SJens Freimann case KVM_CAP_S390_INJECT_IRQ: 5702444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 571e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 57230ee2a98SJason J. Herne case KVM_CAP_S390_SKEYS: 573816c7667SJens Freimann case KVM_CAP_S390_IRQ_STATE: 5746502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 5754036e387SClaudio Imbrenda case KVM_CAP_S390_CMMA_MIGRATION: 57647a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 577da9a1446SChristian Borntraeger case KVM_CAP_S390_AIS_MIGRATION: 5787de3f142SJanosch Frank case KVM_CAP_S390_VCPU_RESETS: 579b9b2782cSPeter Xu case KVM_CAP_SET_GUEST_DEBUG: 58023a60f83SCollin Walling case KVM_CAP_S390_DIAG318: 581d004079eSJanis Schoetterl-Glausch case KVM_CAP_S390_MEM_OP_EXTENSION: 582d7b0b5ebSCarsten Otte r = 1; 583d7b0b5ebSCarsten Otte break; 584a43b80b7SMaxim Levitsky case KVM_CAP_SET_GUEST_DEBUG2: 585a43b80b7SMaxim Levitsky r = KVM_GUESTDBG_VALID_MASK; 586a43b80b7SMaxim Levitsky break; 587a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 588a4499382SJanosch Frank r = 0; 58940ebdb8eSJanosch Frank if (hpage && !kvm_is_ucontrol(kvm)) 590a4499382SJanosch Frank r = 1; 591a4499382SJanosch Frank break; 59241408c28SThomas Huth case KVM_CAP_S390_MEM_OP: 59341408c28SThomas Huth r = MEM_OP_MAX_SIZE; 59441408c28SThomas Huth break; 595e726b1bdSChristian Borntraeger case KVM_CAP_NR_VCPUS: 596e726b1bdSChristian Borntraeger case KVM_CAP_MAX_VCPUS: 597a86cb413SThomas Huth case KVM_CAP_MAX_VCPU_ID: 59876a6dd72SDavid Hildenbrand r = KVM_S390_BSCA_CPU_SLOTS; 599a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 600a6940674SDavid Hildenbrand r = KVM_MAX_VCPUS; 601a6940674SDavid Hildenbrand else if (sclp.has_esca && sclp.has_64bscao) 60276a6dd72SDavid Hildenbrand r = KVM_S390_ESCA_CPU_SLOTS; 60382cc27efSVitaly Kuznetsov if (ext == KVM_CAP_NR_VCPUS) 60482cc27efSVitaly Kuznetsov r = min_t(unsigned int, num_online_cpus(), r); 605e726b1bdSChristian Borntraeger break; 6061526bf9cSChristian Borntraeger case KVM_CAP_S390_COW: 607abf09bedSMartin Schwidefsky r = MACHINE_HAS_ESOP; 6081526bf9cSChristian Borntraeger break; 60968c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 61068c55750SEric Farman r = MACHINE_HAS_VX; 61168c55750SEric Farman break; 612c6e5f166SFan Zhang case KVM_CAP_S390_RI: 613c6e5f166SFan Zhang r = test_facility(64); 614c6e5f166SFan Zhang break; 6154e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 6164e0b1ab7SFan Zhang r = test_facility(133); 6174e0b1ab7SFan Zhang break; 61835b3fde6SChristian Borntraeger case KVM_CAP_S390_BPB: 61935b3fde6SChristian Borntraeger r = test_facility(82); 62035b3fde6SChristian Borntraeger break; 62113da9ae1SChristian Borntraeger case KVM_CAP_S390_PROTECTED: 62213da9ae1SChristian Borntraeger r = is_prot_virt_host(); 62313da9ae1SChristian Borntraeger break; 624e9bf3acbSJanosch Frank case KVM_CAP_S390_PROTECTED_DUMP: { 625e9bf3acbSJanosch Frank u64 pv_cmds_dump[] = { 626e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_INIT, 627e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE, 628e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_CPU, 629e9bf3acbSJanosch Frank BIT_UVC_CMD_DUMP_COMPLETE, 630e9bf3acbSJanosch Frank }; 631e9bf3acbSJanosch Frank int i; 632e9bf3acbSJanosch Frank 633e9bf3acbSJanosch Frank r = is_prot_virt_host(); 634e9bf3acbSJanosch Frank 635e9bf3acbSJanosch Frank for (i = 0; i < ARRAY_SIZE(pv_cmds_dump); i++) { 636e9bf3acbSJanosch Frank if (!test_bit_inv(pv_cmds_dump[i], 637e9bf3acbSJanosch Frank (unsigned long *)&uv_info.inst_calls_list)) { 638e9bf3acbSJanosch Frank r = 0; 639e9bf3acbSJanosch Frank break; 640e9bf3acbSJanosch Frank } 641e9bf3acbSJanosch Frank } 642e9bf3acbSJanosch Frank break; 643e9bf3acbSJanosch Frank } 644db1c875eSMatthew Rosato case KVM_CAP_S390_ZPCI_OP: 645db1c875eSMatthew Rosato r = kvm_s390_pci_interp_allowed(); 646db1c875eSMatthew Rosato break; 647f5ecfee9SPierre Morel case KVM_CAP_S390_CPU_TOPOLOGY: 648f5ecfee9SPierre Morel r = test_facility(11); 649f5ecfee9SPierre Morel break; 6502bd0ac4eSCarsten Otte default: 651d7b0b5ebSCarsten Otte r = 0; 652b0c632dbSHeiko Carstens } 653d7b0b5ebSCarsten Otte return r; 6542bd0ac4eSCarsten Otte } 655b0c632dbSHeiko Carstens 6560dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 65715f36ebdSJason J. Herne { 6580959e168SJanosch Frank int i; 65915f36ebdSJason J. Herne gfn_t cur_gfn, last_gfn; 6600959e168SJanosch Frank unsigned long gaddr, vmaddr; 66115f36ebdSJason J. Herne struct gmap *gmap = kvm->arch.gmap; 6620959e168SJanosch Frank DECLARE_BITMAP(bitmap, _PAGE_ENTRIES); 66315f36ebdSJason J. Herne 6640959e168SJanosch Frank /* Loop over all guest segments */ 6650959e168SJanosch Frank cur_gfn = memslot->base_gfn; 66615f36ebdSJason J. Herne last_gfn = memslot->base_gfn + memslot->npages; 6670959e168SJanosch Frank for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) { 6680959e168SJanosch Frank gaddr = gfn_to_gpa(cur_gfn); 6690959e168SJanosch Frank vmaddr = gfn_to_hva_memslot(memslot, cur_gfn); 6700959e168SJanosch Frank if (kvm_is_error_hva(vmaddr)) 6710959e168SJanosch Frank continue; 67215f36ebdSJason J. Herne 6730959e168SJanosch Frank bitmap_zero(bitmap, _PAGE_ENTRIES); 6740959e168SJanosch Frank gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr); 6750959e168SJanosch Frank for (i = 0; i < _PAGE_ENTRIES; i++) { 6760959e168SJanosch Frank if (test_bit(i, bitmap)) 6770959e168SJanosch Frank mark_page_dirty(kvm, cur_gfn + i); 6780959e168SJanosch Frank } 6790959e168SJanosch Frank 6801763f8d0SChristian Borntraeger if (fatal_signal_pending(current)) 6811763f8d0SChristian Borntraeger return; 68270c88a00SChristian Borntraeger cond_resched(); 68315f36ebdSJason J. Herne } 68415f36ebdSJason J. Herne } 68515f36ebdSJason J. Herne 686b0c632dbSHeiko Carstens /* Section: vm related */ 687a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu); 688a6e2f683SEugene (jno) Dvurechenski 689b0c632dbSHeiko Carstens /* 690b0c632dbSHeiko Carstens * Get (and clear) the dirty memory log for a memory slot. 691b0c632dbSHeiko Carstens */ 692b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, 693b0c632dbSHeiko Carstens struct kvm_dirty_log *log) 694b0c632dbSHeiko Carstens { 69515f36ebdSJason J. Herne int r; 69615f36ebdSJason J. Herne unsigned long n; 69715f36ebdSJason J. Herne struct kvm_memory_slot *memslot; 6982a49f61dSSean Christopherson int is_dirty; 69915f36ebdSJason J. Herne 700e1e8a962SJanosch Frank if (kvm_is_ucontrol(kvm)) 701e1e8a962SJanosch Frank return -EINVAL; 702e1e8a962SJanosch Frank 70315f36ebdSJason J. Herne mutex_lock(&kvm->slots_lock); 70415f36ebdSJason J. Herne 70515f36ebdSJason J. Herne r = -EINVAL; 70615f36ebdSJason J. Herne if (log->slot >= KVM_USER_MEM_SLOTS) 70715f36ebdSJason J. Herne goto out; 70815f36ebdSJason J. Herne 7092a49f61dSSean Christopherson r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot); 71015f36ebdSJason J. Herne if (r) 71115f36ebdSJason J. Herne goto out; 71215f36ebdSJason J. Herne 71315f36ebdSJason J. Herne /* Clear the dirty log */ 71415f36ebdSJason J. Herne if (is_dirty) { 71515f36ebdSJason J. Herne n = kvm_dirty_bitmap_bytes(memslot); 71615f36ebdSJason J. Herne memset(memslot->dirty_bitmap, 0, n); 71715f36ebdSJason J. Herne } 71815f36ebdSJason J. Herne r = 0; 71915f36ebdSJason J. Herne out: 72015f36ebdSJason J. Herne mutex_unlock(&kvm->slots_lock); 72115f36ebdSJason J. Herne return r; 722b0c632dbSHeiko Carstens } 723b0c632dbSHeiko Carstens 7246502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm) 7256502a34cSDavid Hildenbrand { 72646808a4cSMarc Zyngier unsigned long i; 7276502a34cSDavid Hildenbrand struct kvm_vcpu *vcpu; 7286502a34cSDavid Hildenbrand 7296502a34cSDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 7306502a34cSDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu); 7316502a34cSDavid Hildenbrand } 7326502a34cSDavid Hildenbrand } 7336502a34cSDavid Hildenbrand 734e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) 735d938dc55SCornelia Huck { 736d938dc55SCornelia Huck int r; 737d938dc55SCornelia Huck 738d938dc55SCornelia Huck if (cap->flags) 739d938dc55SCornelia Huck return -EINVAL; 740d938dc55SCornelia Huck 741d938dc55SCornelia Huck switch (cap->cap) { 74284223598SCornelia Huck case KVM_CAP_S390_IRQCHIP: 743c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP"); 74484223598SCornelia Huck kvm->arch.use_irqchip = 1; 74584223598SCornelia Huck r = 0; 74684223598SCornelia Huck break; 7472444b352SDavid Hildenbrand case KVM_CAP_S390_USER_SIGP: 748c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP"); 7492444b352SDavid Hildenbrand kvm->arch.user_sigp = 1; 7502444b352SDavid Hildenbrand r = 0; 7512444b352SDavid Hildenbrand break; 75268c55750SEric Farman case KVM_CAP_S390_VECTOR_REGISTERS: 7535967c17bSDavid Hildenbrand mutex_lock(&kvm->lock); 754a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 7555967c17bSDavid Hildenbrand r = -EBUSY; 7565967c17bSDavid Hildenbrand } else if (MACHINE_HAS_VX) { 757c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 129); 758c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 129); 7592f87d942SGuenther Hutzl if (test_facility(134)) { 7602f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_mask, 134); 7612f87d942SGuenther Hutzl set_kvm_facility(kvm->arch.model.fac_list, 134); 7622f87d942SGuenther Hutzl } 76353743aa7SMaxim Samoylov if (test_facility(135)) { 76453743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_mask, 135); 76553743aa7SMaxim Samoylov set_kvm_facility(kvm->arch.model.fac_list, 135); 76653743aa7SMaxim Samoylov } 7677832e91cSChristian Borntraeger if (test_facility(148)) { 7687832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 148); 7697832e91cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 148); 7707832e91cSChristian Borntraeger } 771d5cb6ab1SChristian Borntraeger if (test_facility(152)) { 772d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 152); 773d5cb6ab1SChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 152); 774d5cb6ab1SChristian Borntraeger } 7751f703d2cSChristian Borntraeger if (test_facility(192)) { 7761f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_mask, 192); 7771f703d2cSChristian Borntraeger set_kvm_facility(kvm->arch.model.fac_list, 192); 7781f703d2cSChristian Borntraeger } 77918280d8bSMichael Mueller r = 0; 78018280d8bSMichael Mueller } else 78118280d8bSMichael Mueller r = -EINVAL; 7825967c17bSDavid Hildenbrand mutex_unlock(&kvm->lock); 783c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s", 784c92ea7b9SChristian Borntraeger r ? "(not available)" : "(success)"); 78568c55750SEric Farman break; 786c6e5f166SFan Zhang case KVM_CAP_S390_RI: 787c6e5f166SFan Zhang r = -EINVAL; 788c6e5f166SFan Zhang mutex_lock(&kvm->lock); 789a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 790c6e5f166SFan Zhang r = -EBUSY; 791c6e5f166SFan Zhang } else if (test_facility(64)) { 792c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 64); 793c54f0d6aSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 64); 794c6e5f166SFan Zhang r = 0; 795c6e5f166SFan Zhang } 796c6e5f166SFan Zhang mutex_unlock(&kvm->lock); 797c6e5f166SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s", 798c6e5f166SFan Zhang r ? "(not available)" : "(success)"); 799c6e5f166SFan Zhang break; 80047a4693eSYi Min Zhao case KVM_CAP_S390_AIS: 80147a4693eSYi Min Zhao mutex_lock(&kvm->lock); 80247a4693eSYi Min Zhao if (kvm->created_vcpus) { 80347a4693eSYi Min Zhao r = -EBUSY; 80447a4693eSYi Min Zhao } else { 80547a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_mask, 72); 80647a4693eSYi Min Zhao set_kvm_facility(kvm->arch.model.fac_list, 72); 80747a4693eSYi Min Zhao r = 0; 80847a4693eSYi Min Zhao } 80947a4693eSYi Min Zhao mutex_unlock(&kvm->lock); 81047a4693eSYi Min Zhao VM_EVENT(kvm, 3, "ENABLE: AIS %s", 81147a4693eSYi Min Zhao r ? "(not available)" : "(success)"); 81247a4693eSYi Min Zhao break; 8134e0b1ab7SFan Zhang case KVM_CAP_S390_GS: 8144e0b1ab7SFan Zhang r = -EINVAL; 8154e0b1ab7SFan Zhang mutex_lock(&kvm->lock); 816241e3ec0SChristian Borntraeger if (kvm->created_vcpus) { 8174e0b1ab7SFan Zhang r = -EBUSY; 8184e0b1ab7SFan Zhang } else if (test_facility(133)) { 8194e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_mask, 133); 8204e0b1ab7SFan Zhang set_kvm_facility(kvm->arch.model.fac_list, 133); 8214e0b1ab7SFan Zhang r = 0; 8224e0b1ab7SFan Zhang } 8234e0b1ab7SFan Zhang mutex_unlock(&kvm->lock); 8244e0b1ab7SFan Zhang VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s", 8254e0b1ab7SFan Zhang r ? "(not available)" : "(success)"); 8264e0b1ab7SFan Zhang break; 827a4499382SJanosch Frank case KVM_CAP_S390_HPAGE_1M: 828a4499382SJanosch Frank mutex_lock(&kvm->lock); 829a4499382SJanosch Frank if (kvm->created_vcpus) 830a4499382SJanosch Frank r = -EBUSY; 83140ebdb8eSJanosch Frank else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) 832a4499382SJanosch Frank r = -EINVAL; 833a4499382SJanosch Frank else { 834a4499382SJanosch Frank r = 0; 835d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 836a4499382SJanosch Frank kvm->mm->context.allow_gmap_hpage_1m = 1; 837d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 838a4499382SJanosch Frank /* 839a4499382SJanosch Frank * We might have to create fake 4k page 840a4499382SJanosch Frank * tables. To avoid that the hardware works on 841a4499382SJanosch Frank * stale PGSTEs, we emulate these instructions. 842a4499382SJanosch Frank */ 843a4499382SJanosch Frank kvm->arch.use_skf = 0; 844a4499382SJanosch Frank kvm->arch.use_pfmfi = 0; 845a4499382SJanosch Frank } 846a4499382SJanosch Frank mutex_unlock(&kvm->lock); 847a4499382SJanosch Frank VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s", 848a4499382SJanosch Frank r ? "(not available)" : "(success)"); 849a4499382SJanosch Frank break; 850e44fc8c9SEkaterina Tumanova case KVM_CAP_S390_USER_STSI: 851c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI"); 852e44fc8c9SEkaterina Tumanova kvm->arch.user_stsi = 1; 853e44fc8c9SEkaterina Tumanova r = 0; 854e44fc8c9SEkaterina Tumanova break; 8556502a34cSDavid Hildenbrand case KVM_CAP_S390_USER_INSTR0: 8566502a34cSDavid Hildenbrand VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0"); 8576502a34cSDavid Hildenbrand kvm->arch.user_instr0 = 1; 8586502a34cSDavid Hildenbrand icpt_operexc_on_all_vcpus(kvm); 8596502a34cSDavid Hildenbrand r = 0; 8606502a34cSDavid Hildenbrand break; 861f5ecfee9SPierre Morel case KVM_CAP_S390_CPU_TOPOLOGY: 862f5ecfee9SPierre Morel r = -EINVAL; 863f5ecfee9SPierre Morel mutex_lock(&kvm->lock); 864f5ecfee9SPierre Morel if (kvm->created_vcpus) { 865f5ecfee9SPierre Morel r = -EBUSY; 866f5ecfee9SPierre Morel } else if (test_facility(11)) { 867f5ecfee9SPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 11); 868f5ecfee9SPierre Morel set_kvm_facility(kvm->arch.model.fac_list, 11); 869f5ecfee9SPierre Morel r = 0; 870f5ecfee9SPierre Morel } 871f5ecfee9SPierre Morel mutex_unlock(&kvm->lock); 872f5ecfee9SPierre Morel VM_EVENT(kvm, 3, "ENABLE: CAP_S390_CPU_TOPOLOGY %s", 873f5ecfee9SPierre Morel r ? "(not available)" : "(success)"); 874f5ecfee9SPierre Morel break; 875d938dc55SCornelia Huck default: 876d938dc55SCornelia Huck r = -EINVAL; 877d938dc55SCornelia Huck break; 878d938dc55SCornelia Huck } 879d938dc55SCornelia Huck return r; 880d938dc55SCornelia Huck } 881d938dc55SCornelia Huck 8828c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 8838c0a7ce6SDominik Dingel { 8848c0a7ce6SDominik Dingel int ret; 8858c0a7ce6SDominik Dingel 8868c0a7ce6SDominik Dingel switch (attr->attr) { 8878c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 8888c0a7ce6SDominik Dingel ret = 0; 889c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes", 890a3a92c31SDominik Dingel kvm->arch.mem_limit); 891a3a92c31SDominik Dingel if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr)) 8928c0a7ce6SDominik Dingel ret = -EFAULT; 8938c0a7ce6SDominik Dingel break; 8948c0a7ce6SDominik Dingel default: 8958c0a7ce6SDominik Dingel ret = -ENXIO; 8968c0a7ce6SDominik Dingel break; 8978c0a7ce6SDominik Dingel } 8988c0a7ce6SDominik Dingel return ret; 8998c0a7ce6SDominik Dingel } 9008c0a7ce6SDominik Dingel 9018c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr) 9024f718eabSDominik Dingel { 9034f718eabSDominik Dingel int ret; 9044f718eabSDominik Dingel unsigned int idx; 9054f718eabSDominik Dingel switch (attr->attr) { 9064f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 907f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 908c24cc9c8SDavid Hildenbrand if (!sclp.has_cmma) 909e6db1d61SDominik Dingel break; 910e6db1d61SDominik Dingel 911c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support"); 9124f718eabSDominik Dingel mutex_lock(&kvm->lock); 913a4499382SJanosch Frank if (kvm->created_vcpus) 914a4499382SJanosch Frank ret = -EBUSY; 915a4499382SJanosch Frank else if (kvm->mm->context.allow_gmap_hpage_1m) 916a4499382SJanosch Frank ret = -EINVAL; 917a4499382SJanosch Frank else { 9184f718eabSDominik Dingel kvm->arch.use_cmma = 1; 919c9f0a2b8SJanosch Frank /* Not compatible with cmma. */ 920c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = 0; 9214f718eabSDominik Dingel ret = 0; 9224f718eabSDominik Dingel } 9234f718eabSDominik Dingel mutex_unlock(&kvm->lock); 9244f718eabSDominik Dingel break; 9254f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 926f9cbd9b0SDavid Hildenbrand ret = -ENXIO; 927f9cbd9b0SDavid Hildenbrand if (!sclp.has_cmma) 928f9cbd9b0SDavid Hildenbrand break; 929c3489155SDominik Dingel ret = -EINVAL; 930c3489155SDominik Dingel if (!kvm->arch.use_cmma) 931c3489155SDominik Dingel break; 932c3489155SDominik Dingel 933c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "RESET: CMMA states"); 9344f718eabSDominik Dingel mutex_lock(&kvm->lock); 9354f718eabSDominik Dingel idx = srcu_read_lock(&kvm->srcu); 936a13cff31SDominik Dingel s390_reset_cmma(kvm->arch.gmap->mm); 9374f718eabSDominik Dingel srcu_read_unlock(&kvm->srcu, idx); 9384f718eabSDominik Dingel mutex_unlock(&kvm->lock); 9394f718eabSDominik Dingel ret = 0; 9404f718eabSDominik Dingel break; 9418c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: { 9428c0a7ce6SDominik Dingel unsigned long new_limit; 9438c0a7ce6SDominik Dingel 9448c0a7ce6SDominik Dingel if (kvm_is_ucontrol(kvm)) 9458c0a7ce6SDominik Dingel return -EINVAL; 9468c0a7ce6SDominik Dingel 9478c0a7ce6SDominik Dingel if (get_user(new_limit, (u64 __user *)attr->addr)) 9488c0a7ce6SDominik Dingel return -EFAULT; 9498c0a7ce6SDominik Dingel 950a3a92c31SDominik Dingel if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT && 951a3a92c31SDominik Dingel new_limit > kvm->arch.mem_limit) 9528c0a7ce6SDominik Dingel return -E2BIG; 9538c0a7ce6SDominik Dingel 954a3a92c31SDominik Dingel if (!new_limit) 955a3a92c31SDominik Dingel return -EINVAL; 956a3a92c31SDominik Dingel 9576ea427bbSMartin Schwidefsky /* gmap_create takes last usable address */ 958a3a92c31SDominik Dingel if (new_limit != KVM_S390_NO_MEM_LIMIT) 959a3a92c31SDominik Dingel new_limit -= 1; 960a3a92c31SDominik Dingel 9618c0a7ce6SDominik Dingel ret = -EBUSY; 9628c0a7ce6SDominik Dingel mutex_lock(&kvm->lock); 963a03825bbSPaolo Bonzini if (!kvm->created_vcpus) { 9646ea427bbSMartin Schwidefsky /* gmap_create will round the limit up */ 9656ea427bbSMartin Schwidefsky struct gmap *new = gmap_create(current->mm, new_limit); 9668c0a7ce6SDominik Dingel 9678c0a7ce6SDominik Dingel if (!new) { 9688c0a7ce6SDominik Dingel ret = -ENOMEM; 9698c0a7ce6SDominik Dingel } else { 9706ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 9718c0a7ce6SDominik Dingel new->private = kvm; 9728c0a7ce6SDominik Dingel kvm->arch.gmap = new; 9738c0a7ce6SDominik Dingel ret = 0; 9748c0a7ce6SDominik Dingel } 9758c0a7ce6SDominik Dingel } 9768c0a7ce6SDominik Dingel mutex_unlock(&kvm->lock); 977a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit); 978a3a92c31SDominik Dingel VM_EVENT(kvm, 3, "New guest asce: 0x%pK", 979a3a92c31SDominik Dingel (void *) kvm->arch.gmap->asce); 9808c0a7ce6SDominik Dingel break; 9818c0a7ce6SDominik Dingel } 9824f718eabSDominik Dingel default: 9834f718eabSDominik Dingel ret = -ENXIO; 9844f718eabSDominik Dingel break; 9854f718eabSDominik Dingel } 9864f718eabSDominik Dingel return ret; 9874f718eabSDominik Dingel } 9884f718eabSDominik Dingel 989a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu); 990a374e892STony Krowiak 99120c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm) 992a374e892STony Krowiak { 993a374e892STony Krowiak struct kvm_vcpu *vcpu; 99446808a4cSMarc Zyngier unsigned long i; 995a374e892STony Krowiak 99620c922f0STony Krowiak kvm_s390_vcpu_block_all(kvm); 99720c922f0STony Krowiak 9983194cdb7SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 99920c922f0STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 10003194cdb7SDavid Hildenbrand /* recreate the shadow crycb by leaving the VSIE handler */ 10013194cdb7SDavid Hildenbrand kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 10023194cdb7SDavid Hildenbrand } 100320c922f0STony Krowiak 100420c922f0STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 100520c922f0STony Krowiak } 100620c922f0STony Krowiak 100720c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr) 100820c922f0STony Krowiak { 1009a374e892STony Krowiak mutex_lock(&kvm->lock); 1010a374e892STony Krowiak switch (attr->attr) { 1011a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 10128e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10138e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 101437940fb0STony Krowiak return -EINVAL; 10158e41bd54SChristian Borntraeger } 1016a374e892STony Krowiak get_random_bytes( 1017a374e892STony Krowiak kvm->arch.crypto.crycb->aes_wrapping_key_mask, 1018a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 1019a374e892STony Krowiak kvm->arch.crypto.aes_kw = 1; 1020c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support"); 1021a374e892STony Krowiak break; 1022a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 10238e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10248e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 102537940fb0STony Krowiak return -EINVAL; 10268e41bd54SChristian Borntraeger } 1027a374e892STony Krowiak get_random_bytes( 1028a374e892STony Krowiak kvm->arch.crypto.crycb->dea_wrapping_key_mask, 1029a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 1030a374e892STony Krowiak kvm->arch.crypto.dea_kw = 1; 1031c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support"); 1032a374e892STony Krowiak break; 1033a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 10348e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10358e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 103637940fb0STony Krowiak return -EINVAL; 10378e41bd54SChristian Borntraeger } 1038a374e892STony Krowiak kvm->arch.crypto.aes_kw = 0; 1039a374e892STony Krowiak memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0, 1040a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 1041c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support"); 1042a374e892STony Krowiak break; 1043a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 10448e41bd54SChristian Borntraeger if (!test_kvm_facility(kvm, 76)) { 10458e41bd54SChristian Borntraeger mutex_unlock(&kvm->lock); 104637940fb0STony Krowiak return -EINVAL; 10478e41bd54SChristian Borntraeger } 1048a374e892STony Krowiak kvm->arch.crypto.dea_kw = 0; 1049a374e892STony Krowiak memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0, 1050a374e892STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 1051c92ea7b9SChristian Borntraeger VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support"); 1052a374e892STony Krowiak break; 105337940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 105437940fb0STony Krowiak if (!ap_instructions_available()) { 105537940fb0STony Krowiak mutex_unlock(&kvm->lock); 105637940fb0STony Krowiak return -EOPNOTSUPP; 105737940fb0STony Krowiak } 105837940fb0STony Krowiak kvm->arch.crypto.apie = 1; 105937940fb0STony Krowiak break; 106037940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 106137940fb0STony Krowiak if (!ap_instructions_available()) { 106237940fb0STony Krowiak mutex_unlock(&kvm->lock); 106337940fb0STony Krowiak return -EOPNOTSUPP; 106437940fb0STony Krowiak } 106537940fb0STony Krowiak kvm->arch.crypto.apie = 0; 106637940fb0STony Krowiak break; 1067a374e892STony Krowiak default: 1068a374e892STony Krowiak mutex_unlock(&kvm->lock); 1069a374e892STony Krowiak return -ENXIO; 1070a374e892STony Krowiak } 1071a374e892STony Krowiak 107220c922f0STony Krowiak kvm_s390_vcpu_crypto_reset_all(kvm); 1073a374e892STony Krowiak mutex_unlock(&kvm->lock); 1074a374e892STony Krowiak return 0; 1075a374e892STony Krowiak } 1076a374e892STony Krowiak 10773f4bbb43SMatthew Rosato static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu) 10783f4bbb43SMatthew Rosato { 10793f4bbb43SMatthew Rosato /* Only set the ECB bits after guest requests zPCI interpretation */ 10803f4bbb43SMatthew Rosato if (!vcpu->kvm->arch.use_zpci_interp) 10813f4bbb43SMatthew Rosato return; 10823f4bbb43SMatthew Rosato 10833f4bbb43SMatthew Rosato vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI; 10843f4bbb43SMatthew Rosato vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI; 10853f4bbb43SMatthew Rosato } 10863f4bbb43SMatthew Rosato 10873f4bbb43SMatthew Rosato void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm) 10883f4bbb43SMatthew Rosato { 10893f4bbb43SMatthew Rosato struct kvm_vcpu *vcpu; 10903f4bbb43SMatthew Rosato unsigned long i; 10913f4bbb43SMatthew Rosato 10923f4bbb43SMatthew Rosato lockdep_assert_held(&kvm->lock); 10933f4bbb43SMatthew Rosato 10943f4bbb43SMatthew Rosato if (!kvm_s390_pci_interp_allowed()) 10953f4bbb43SMatthew Rosato return; 10963f4bbb43SMatthew Rosato 10973f4bbb43SMatthew Rosato /* 10983f4bbb43SMatthew Rosato * If host is configured for PCI and the necessary facilities are 10993f4bbb43SMatthew Rosato * available, turn on interpretation for the life of this guest 11003f4bbb43SMatthew Rosato */ 11013f4bbb43SMatthew Rosato kvm->arch.use_zpci_interp = 1; 11023f4bbb43SMatthew Rosato 11033f4bbb43SMatthew Rosato kvm_s390_vcpu_block_all(kvm); 11043f4bbb43SMatthew Rosato 11053f4bbb43SMatthew Rosato kvm_for_each_vcpu(i, vcpu, kvm) { 11063f4bbb43SMatthew Rosato kvm_s390_vcpu_pci_setup(vcpu); 11073f4bbb43SMatthew Rosato kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu); 11083f4bbb43SMatthew Rosato } 11093f4bbb43SMatthew Rosato 11103f4bbb43SMatthew Rosato kvm_s390_vcpu_unblock_all(kvm); 11113f4bbb43SMatthew Rosato } 11123f4bbb43SMatthew Rosato 1113190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req) 1114190df4a2SClaudio Imbrenda { 111546808a4cSMarc Zyngier unsigned long cx; 1116190df4a2SClaudio Imbrenda struct kvm_vcpu *vcpu; 1117190df4a2SClaudio Imbrenda 1118190df4a2SClaudio Imbrenda kvm_for_each_vcpu(cx, vcpu, kvm) 1119190df4a2SClaudio Imbrenda kvm_s390_sync_request(req, vcpu); 1120190df4a2SClaudio Imbrenda } 1121190df4a2SClaudio Imbrenda 1122190df4a2SClaudio Imbrenda /* 1123190df4a2SClaudio Imbrenda * Must be called with kvm->srcu held to avoid races on memslots, and with 11241de1ea7eSChristian Borntraeger * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration. 1125190df4a2SClaudio Imbrenda */ 1126190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm) 1127190df4a2SClaudio Imbrenda { 1128190df4a2SClaudio Imbrenda struct kvm_memory_slot *ms; 1129190df4a2SClaudio Imbrenda struct kvm_memslots *slots; 1130afdad616SClaudio Imbrenda unsigned long ram_pages = 0; 1131a54d8066SMaciej S. Szmigiero int bkt; 1132190df4a2SClaudio Imbrenda 1133190df4a2SClaudio Imbrenda /* migration mode already enabled */ 1134afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 1135190df4a2SClaudio Imbrenda return 0; 1136190df4a2SClaudio Imbrenda slots = kvm_memslots(kvm); 1137a54d8066SMaciej S. Szmigiero if (!slots || kvm_memslots_empty(slots)) 1138190df4a2SClaudio Imbrenda return -EINVAL; 1139190df4a2SClaudio Imbrenda 1140afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) { 1141afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1142afdad616SClaudio Imbrenda return 0; 1143190df4a2SClaudio Imbrenda } 1144190df4a2SClaudio Imbrenda /* mark all the pages in active slots as dirty */ 1145a54d8066SMaciej S. Szmigiero kvm_for_each_memslot(ms, bkt, slots) { 114613a17cc0SIgor Mammedov if (!ms->dirty_bitmap) 114713a17cc0SIgor Mammedov return -EINVAL; 1148afdad616SClaudio Imbrenda /* 1149afdad616SClaudio Imbrenda * The second half of the bitmap is only used on x86, 1150afdad616SClaudio Imbrenda * and would be wasted otherwise, so we put it to good 1151afdad616SClaudio Imbrenda * use here to keep track of the state of the storage 1152afdad616SClaudio Imbrenda * attributes. 1153afdad616SClaudio Imbrenda */ 1154afdad616SClaudio Imbrenda memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms)); 1155afdad616SClaudio Imbrenda ram_pages += ms->npages; 1156190df4a2SClaudio Imbrenda } 1157afdad616SClaudio Imbrenda atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages); 1158afdad616SClaudio Imbrenda kvm->arch.migration_mode = 1; 1159190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION); 1160190df4a2SClaudio Imbrenda return 0; 1161190df4a2SClaudio Imbrenda } 1162190df4a2SClaudio Imbrenda 1163190df4a2SClaudio Imbrenda /* 11641de1ea7eSChristian Borntraeger * Must be called with kvm->slots_lock to avoid races with ourselves and 1165190df4a2SClaudio Imbrenda * kvm_s390_vm_start_migration. 1166190df4a2SClaudio Imbrenda */ 1167190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm) 1168190df4a2SClaudio Imbrenda { 1169190df4a2SClaudio Imbrenda /* migration mode already disabled */ 1170afdad616SClaudio Imbrenda if (!kvm->arch.migration_mode) 1171190df4a2SClaudio Imbrenda return 0; 1172afdad616SClaudio Imbrenda kvm->arch.migration_mode = 0; 1173afdad616SClaudio Imbrenda if (kvm->arch.use_cmma) 1174190df4a2SClaudio Imbrenda kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION); 1175190df4a2SClaudio Imbrenda return 0; 1176190df4a2SClaudio Imbrenda } 1177190df4a2SClaudio Imbrenda 1178190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm, 1179190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1180190df4a2SClaudio Imbrenda { 11811de1ea7eSChristian Borntraeger int res = -ENXIO; 1182190df4a2SClaudio Imbrenda 11831de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 1184190df4a2SClaudio Imbrenda switch (attr->attr) { 1185190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_START: 1186190df4a2SClaudio Imbrenda res = kvm_s390_vm_start_migration(kvm); 1187190df4a2SClaudio Imbrenda break; 1188190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION_STOP: 1189190df4a2SClaudio Imbrenda res = kvm_s390_vm_stop_migration(kvm); 1190190df4a2SClaudio Imbrenda break; 1191190df4a2SClaudio Imbrenda default: 1192190df4a2SClaudio Imbrenda break; 1193190df4a2SClaudio Imbrenda } 11941de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 1195190df4a2SClaudio Imbrenda 1196190df4a2SClaudio Imbrenda return res; 1197190df4a2SClaudio Imbrenda } 1198190df4a2SClaudio Imbrenda 1199190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm, 1200190df4a2SClaudio Imbrenda struct kvm_device_attr *attr) 1201190df4a2SClaudio Imbrenda { 1202afdad616SClaudio Imbrenda u64 mig = kvm->arch.migration_mode; 1203190df4a2SClaudio Imbrenda 1204190df4a2SClaudio Imbrenda if (attr->attr != KVM_S390_VM_MIGRATION_STATUS) 1205190df4a2SClaudio Imbrenda return -ENXIO; 1206190df4a2SClaudio Imbrenda 1207190df4a2SClaudio Imbrenda if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig))) 1208190df4a2SClaudio Imbrenda return -EFAULT; 1209190df4a2SClaudio Imbrenda return 0; 1210190df4a2SClaudio Imbrenda } 1211190df4a2SClaudio Imbrenda 12128fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 12138fa1696eSCollin L. Walling { 12148fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 12158fa1696eSCollin L. Walling 12168fa1696eSCollin L. Walling if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod))) 12178fa1696eSCollin L. Walling return -EFAULT; 12188fa1696eSCollin L. Walling 12190e7def5fSDavid Hildenbrand if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx) 12208fa1696eSCollin L. Walling return -EINVAL; 12210e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 12228fa1696eSCollin L. Walling 12238fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx", 12248fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 12258fa1696eSCollin L. Walling 12268fa1696eSCollin L. Walling return 0; 12278fa1696eSCollin L. Walling } 12288fa1696eSCollin L. Walling 122972f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 123072f25020SJason J. Herne { 123172f25020SJason J. Herne u8 gtod_high; 123272f25020SJason J. Herne 123372f25020SJason J. Herne if (copy_from_user(>od_high, (void __user *)attr->addr, 123472f25020SJason J. Herne sizeof(gtod_high))) 123572f25020SJason J. Herne return -EFAULT; 123672f25020SJason J. Herne 123772f25020SJason J. Herne if (gtod_high != 0) 123872f25020SJason J. Herne return -EINVAL; 123958c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high); 124072f25020SJason J. Herne 124172f25020SJason J. Herne return 0; 124272f25020SJason J. Herne } 124372f25020SJason J. Herne 124472f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 124572f25020SJason J. Herne { 12460e7def5fSDavid Hildenbrand struct kvm_s390_vm_tod_clock gtod = { 0 }; 124772f25020SJason J. Herne 12480e7def5fSDavid Hildenbrand if (copy_from_user(>od.tod, (void __user *)attr->addr, 12490e7def5fSDavid Hildenbrand sizeof(gtod.tod))) 125072f25020SJason J. Herne return -EFAULT; 125172f25020SJason J. Herne 12520e7def5fSDavid Hildenbrand kvm_s390_set_tod_clock(kvm, >od); 12530e7def5fSDavid Hildenbrand VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod); 125472f25020SJason J. Herne return 0; 125572f25020SJason J. Herne } 125672f25020SJason J. Herne 125772f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr) 125872f25020SJason J. Herne { 125972f25020SJason J. Herne int ret; 126072f25020SJason J. Herne 126172f25020SJason J. Herne if (attr->flags) 126272f25020SJason J. Herne return -EINVAL; 126372f25020SJason J. Herne 126472f25020SJason J. Herne switch (attr->attr) { 12658fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 12668fa1696eSCollin L. Walling ret = kvm_s390_set_tod_ext(kvm, attr); 12678fa1696eSCollin L. Walling break; 126872f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 126972f25020SJason J. Herne ret = kvm_s390_set_tod_high(kvm, attr); 127072f25020SJason J. Herne break; 127172f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 127272f25020SJason J. Herne ret = kvm_s390_set_tod_low(kvm, attr); 127372f25020SJason J. Herne break; 127472f25020SJason J. Herne default: 127572f25020SJason J. Herne ret = -ENXIO; 127672f25020SJason J. Herne break; 127772f25020SJason J. Herne } 127872f25020SJason J. Herne return ret; 127972f25020SJason J. Herne } 128072f25020SJason J. Herne 128133d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm, 12828fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock *gtod) 12838fa1696eSCollin L. Walling { 12842cfd7b73SHeiko Carstens union tod_clock clk; 12858fa1696eSCollin L. Walling 12868fa1696eSCollin L. Walling preempt_disable(); 12878fa1696eSCollin L. Walling 12882cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 12898fa1696eSCollin L. Walling 12902cfd7b73SHeiko Carstens gtod->tod = clk.tod + kvm->arch.epoch; 129133d1b272SDavid Hildenbrand gtod->epoch_idx = 0; 129233d1b272SDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 12932cfd7b73SHeiko Carstens gtod->epoch_idx = clk.ei + kvm->arch.epdx; 12942cfd7b73SHeiko Carstens if (gtod->tod < clk.tod) 12958fa1696eSCollin L. Walling gtod->epoch_idx += 1; 129633d1b272SDavid Hildenbrand } 12978fa1696eSCollin L. Walling 12988fa1696eSCollin L. Walling preempt_enable(); 12998fa1696eSCollin L. Walling } 13008fa1696eSCollin L. Walling 13018fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr) 13028fa1696eSCollin L. Walling { 13038fa1696eSCollin L. Walling struct kvm_s390_vm_tod_clock gtod; 13048fa1696eSCollin L. Walling 13058fa1696eSCollin L. Walling memset(>od, 0, sizeof(gtod)); 130633d1b272SDavid Hildenbrand kvm_s390_get_tod_clock(kvm, >od); 13078fa1696eSCollin L. Walling if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 13088fa1696eSCollin L. Walling return -EFAULT; 13098fa1696eSCollin L. Walling 13108fa1696eSCollin L. Walling VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx", 13118fa1696eSCollin L. Walling gtod.epoch_idx, gtod.tod); 13128fa1696eSCollin L. Walling return 0; 13138fa1696eSCollin L. Walling } 13148fa1696eSCollin L. Walling 131572f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr) 131672f25020SJason J. Herne { 131772f25020SJason J. Herne u8 gtod_high = 0; 131872f25020SJason J. Herne 131972f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od_high, 132072f25020SJason J. Herne sizeof(gtod_high))) 132172f25020SJason J. Herne return -EFAULT; 132258c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high); 132372f25020SJason J. Herne 132472f25020SJason J. Herne return 0; 132572f25020SJason J. Herne } 132672f25020SJason J. Herne 132772f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr) 132872f25020SJason J. Herne { 13295a3d883aSDavid Hildenbrand u64 gtod; 133072f25020SJason J. Herne 133160417fccSDavid Hildenbrand gtod = kvm_s390_get_tod_clock_fast(kvm); 133272f25020SJason J. Herne if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod))) 133372f25020SJason J. Herne return -EFAULT; 133458c383c6SChristian Borntraeger VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod); 133572f25020SJason J. Herne 133672f25020SJason J. Herne return 0; 133772f25020SJason J. Herne } 133872f25020SJason J. Herne 133972f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr) 134072f25020SJason J. Herne { 134172f25020SJason J. Herne int ret; 134272f25020SJason J. Herne 134372f25020SJason J. Herne if (attr->flags) 134472f25020SJason J. Herne return -EINVAL; 134572f25020SJason J. Herne 134672f25020SJason J. Herne switch (attr->attr) { 13478fa1696eSCollin L. Walling case KVM_S390_VM_TOD_EXT: 13488fa1696eSCollin L. Walling ret = kvm_s390_get_tod_ext(kvm, attr); 13498fa1696eSCollin L. Walling break; 135072f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 135172f25020SJason J. Herne ret = kvm_s390_get_tod_high(kvm, attr); 135272f25020SJason J. Herne break; 135372f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 135472f25020SJason J. Herne ret = kvm_s390_get_tod_low(kvm, attr); 135572f25020SJason J. Herne break; 135672f25020SJason J. Herne default: 135772f25020SJason J. Herne ret = -ENXIO; 135872f25020SJason J. Herne break; 135972f25020SJason J. Herne } 136072f25020SJason J. Herne return ret; 136172f25020SJason J. Herne } 136272f25020SJason J. Herne 1363658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1364658b6edaSMichael Mueller { 1365658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1366053dd230SDavid Hildenbrand u16 lowest_ibc, unblocked_ibc; 1367658b6edaSMichael Mueller int ret = 0; 1368658b6edaSMichael Mueller 1369658b6edaSMichael Mueller mutex_lock(&kvm->lock); 1370a03825bbSPaolo Bonzini if (kvm->created_vcpus) { 1371658b6edaSMichael Mueller ret = -EBUSY; 1372658b6edaSMichael Mueller goto out; 1373658b6edaSMichael Mueller } 1374c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); 1375658b6edaSMichael Mueller if (!proc) { 1376658b6edaSMichael Mueller ret = -ENOMEM; 1377658b6edaSMichael Mueller goto out; 1378658b6edaSMichael Mueller } 1379658b6edaSMichael Mueller if (!copy_from_user(proc, (void __user *)attr->addr, 1380658b6edaSMichael Mueller sizeof(*proc))) { 13819bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = proc->cpuid; 1382053dd230SDavid Hildenbrand lowest_ibc = sclp.ibc >> 16 & 0xfff; 1383053dd230SDavid Hildenbrand unblocked_ibc = sclp.ibc & 0xfff; 13840487c44dSDavid Hildenbrand if (lowest_ibc && proc->ibc) { 1385053dd230SDavid Hildenbrand if (proc->ibc > unblocked_ibc) 1386053dd230SDavid Hildenbrand kvm->arch.model.ibc = unblocked_ibc; 1387053dd230SDavid Hildenbrand else if (proc->ibc < lowest_ibc) 1388053dd230SDavid Hildenbrand kvm->arch.model.ibc = lowest_ibc; 1389053dd230SDavid Hildenbrand else 1390658b6edaSMichael Mueller kvm->arch.model.ibc = proc->ibc; 1391053dd230SDavid Hildenbrand } 1392c54f0d6aSDavid Hildenbrand memcpy(kvm->arch.model.fac_list, proc->fac_list, 1393658b6edaSMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 1394a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1395a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1396a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1397a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1398a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1399a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1400a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1401658b6edaSMichael Mueller } else 1402658b6edaSMichael Mueller ret = -EFAULT; 1403658b6edaSMichael Mueller kfree(proc); 1404658b6edaSMichael Mueller out: 1405658b6edaSMichael Mueller mutex_unlock(&kvm->lock); 1406658b6edaSMichael Mueller return ret; 1407658b6edaSMichael Mueller } 1408658b6edaSMichael Mueller 140915c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm, 141015c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 141115c9705fSDavid Hildenbrand { 141215c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 141315c9705fSDavid Hildenbrand 141415c9705fSDavid Hildenbrand if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data))) 141515c9705fSDavid Hildenbrand return -EFAULT; 141615c9705fSDavid Hildenbrand if (!bitmap_subset((unsigned long *) data.feat, 141715c9705fSDavid Hildenbrand kvm_s390_available_cpu_feat, 141815c9705fSDavid Hildenbrand KVM_S390_VM_CPU_FEAT_NR_BITS)) 141915c9705fSDavid Hildenbrand return -EINVAL; 142015c9705fSDavid Hildenbrand 142115c9705fSDavid Hildenbrand mutex_lock(&kvm->lock); 14222f8311c9SChristian Borntraeger if (kvm->created_vcpus) { 14232f8311c9SChristian Borntraeger mutex_unlock(&kvm->lock); 14242f8311c9SChristian Borntraeger return -EBUSY; 14252f8311c9SChristian Borntraeger } 1426da0f8e95SYury Norov bitmap_from_arr64(kvm->arch.cpu_feat, data.feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 142715c9705fSDavid Hildenbrand mutex_unlock(&kvm->lock); 14282f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 14292f8311c9SChristian Borntraeger data.feat[0], 14302f8311c9SChristian Borntraeger data.feat[1], 14312f8311c9SChristian Borntraeger data.feat[2]); 14322f8311c9SChristian Borntraeger return 0; 143315c9705fSDavid Hildenbrand } 143415c9705fSDavid Hildenbrand 14350a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm, 14360a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 14370a763c78SDavid Hildenbrand { 1438346fa2f8SChristian Borntraeger mutex_lock(&kvm->lock); 1439346fa2f8SChristian Borntraeger if (kvm->created_vcpus) { 1440346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1441346fa2f8SChristian Borntraeger return -EBUSY; 1442346fa2f8SChristian Borntraeger } 1443346fa2f8SChristian Borntraeger 1444346fa2f8SChristian Borntraeger if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr, 1445346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) { 1446346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1447346fa2f8SChristian Borntraeger return -EFAULT; 1448346fa2f8SChristian Borntraeger } 1449346fa2f8SChristian Borntraeger mutex_unlock(&kvm->lock); 1450346fa2f8SChristian Borntraeger 145111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 145211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 145311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 145411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 145511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 145611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 145711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 145811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 145911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 146011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 146111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 146211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx", 146311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 146411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 146511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx", 146611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 146711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 146811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 146911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 147011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 147111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 147211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 147311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 147411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 147511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 147611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 147711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 147811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 147911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 148011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx", 148111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 148211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 148311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx", 148411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 148511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 148611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx", 148711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 148811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 148911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 149011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 149111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 149211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx", 149311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 149411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 149513209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 149613209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 149713209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1498173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1499173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1500173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1501173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1502173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 15034f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 15044f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 15054f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 15064f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 15074f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 150811ba5961SChristian Borntraeger 1509346fa2f8SChristian Borntraeger return 0; 15100a763c78SDavid Hildenbrand } 15110a763c78SDavid Hildenbrand 1512658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1513658b6edaSMichael Mueller { 1514658b6edaSMichael Mueller int ret = -ENXIO; 1515658b6edaSMichael Mueller 1516658b6edaSMichael Mueller switch (attr->attr) { 1517658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1518658b6edaSMichael Mueller ret = kvm_s390_set_processor(kvm, attr); 1519658b6edaSMichael Mueller break; 152015c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 152115c9705fSDavid Hildenbrand ret = kvm_s390_set_processor_feat(kvm, attr); 152215c9705fSDavid Hildenbrand break; 15230a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 15240a763c78SDavid Hildenbrand ret = kvm_s390_set_processor_subfunc(kvm, attr); 15250a763c78SDavid Hildenbrand break; 1526658b6edaSMichael Mueller } 1527658b6edaSMichael Mueller return ret; 1528658b6edaSMichael Mueller } 1529658b6edaSMichael Mueller 1530658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr) 1531658b6edaSMichael Mueller { 1532658b6edaSMichael Mueller struct kvm_s390_vm_cpu_processor *proc; 1533658b6edaSMichael Mueller int ret = 0; 1534658b6edaSMichael Mueller 1535c4196218SChristian Borntraeger proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT); 1536658b6edaSMichael Mueller if (!proc) { 1537658b6edaSMichael Mueller ret = -ENOMEM; 1538658b6edaSMichael Mueller goto out; 1539658b6edaSMichael Mueller } 15409bb0ec09SDavid Hildenbrand proc->cpuid = kvm->arch.model.cpuid; 1541658b6edaSMichael Mueller proc->ibc = kvm->arch.model.ibc; 1542c54f0d6aSDavid Hildenbrand memcpy(&proc->fac_list, kvm->arch.model.fac_list, 1543c54f0d6aSDavid Hildenbrand S390_ARCH_FAC_LIST_SIZE_BYTE); 1544a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx", 1545a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1546a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1547a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1548a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[0], 1549a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[1], 1550a8c39dd7SChristian Borntraeger kvm->arch.model.fac_list[2]); 1551658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc))) 1552658b6edaSMichael Mueller ret = -EFAULT; 1553658b6edaSMichael Mueller kfree(proc); 1554658b6edaSMichael Mueller out: 1555658b6edaSMichael Mueller return ret; 1556658b6edaSMichael Mueller } 1557658b6edaSMichael Mueller 1558658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr) 1559658b6edaSMichael Mueller { 1560658b6edaSMichael Mueller struct kvm_s390_vm_cpu_machine *mach; 1561658b6edaSMichael Mueller int ret = 0; 1562658b6edaSMichael Mueller 1563c4196218SChristian Borntraeger mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT); 1564658b6edaSMichael Mueller if (!mach) { 1565658b6edaSMichael Mueller ret = -ENOMEM; 1566658b6edaSMichael Mueller goto out; 1567658b6edaSMichael Mueller } 1568658b6edaSMichael Mueller get_cpu_id((struct cpuid *) &mach->cpuid); 156937c5f6c8SDavid Hildenbrand mach->ibc = sclp.ibc; 1570c54f0d6aSDavid Hildenbrand memcpy(&mach->fac_mask, kvm->arch.model.fac_mask, 1571981467c9SMichael Mueller S390_ARCH_FAC_LIST_SIZE_BYTE); 157217e89e13SSven Schnelle memcpy((unsigned long *)&mach->fac_list, stfle_fac_list, 157317e89e13SSven Schnelle sizeof(stfle_fac_list)); 1574a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx", 1575a8c39dd7SChristian Borntraeger kvm->arch.model.ibc, 1576a8c39dd7SChristian Borntraeger kvm->arch.model.cpuid); 1577a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx", 1578a8c39dd7SChristian Borntraeger mach->fac_mask[0], 1579a8c39dd7SChristian Borntraeger mach->fac_mask[1], 1580a8c39dd7SChristian Borntraeger mach->fac_mask[2]); 1581a8c39dd7SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx", 1582a8c39dd7SChristian Borntraeger mach->fac_list[0], 1583a8c39dd7SChristian Borntraeger mach->fac_list[1], 1584a8c39dd7SChristian Borntraeger mach->fac_list[2]); 1585658b6edaSMichael Mueller if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach))) 1586658b6edaSMichael Mueller ret = -EFAULT; 1587658b6edaSMichael Mueller kfree(mach); 1588658b6edaSMichael Mueller out: 1589658b6edaSMichael Mueller return ret; 1590658b6edaSMichael Mueller } 1591658b6edaSMichael Mueller 159215c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm, 159315c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 159415c9705fSDavid Hildenbrand { 159515c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 159615c9705fSDavid Hildenbrand 1597da0f8e95SYury Norov bitmap_to_arr64(data.feat, kvm->arch.cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 159815c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 159915c9705fSDavid Hildenbrand return -EFAULT; 16002f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 16012f8311c9SChristian Borntraeger data.feat[0], 16022f8311c9SChristian Borntraeger data.feat[1], 16032f8311c9SChristian Borntraeger data.feat[2]); 160415c9705fSDavid Hildenbrand return 0; 160515c9705fSDavid Hildenbrand } 160615c9705fSDavid Hildenbrand 160715c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm, 160815c9705fSDavid Hildenbrand struct kvm_device_attr *attr) 160915c9705fSDavid Hildenbrand { 161015c9705fSDavid Hildenbrand struct kvm_s390_vm_cpu_feat data; 161115c9705fSDavid Hildenbrand 1612da0f8e95SYury Norov bitmap_to_arr64(data.feat, kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS); 161315c9705fSDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &data, sizeof(data))) 161415c9705fSDavid Hildenbrand return -EFAULT; 16152f8311c9SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx", 16162f8311c9SChristian Borntraeger data.feat[0], 16172f8311c9SChristian Borntraeger data.feat[1], 16182f8311c9SChristian Borntraeger data.feat[2]); 161915c9705fSDavid Hildenbrand return 0; 162015c9705fSDavid Hildenbrand } 162115c9705fSDavid Hildenbrand 16220a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm, 16230a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 16240a763c78SDavid Hildenbrand { 1625346fa2f8SChristian Borntraeger if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs, 1626346fa2f8SChristian Borntraeger sizeof(struct kvm_s390_vm_cpu_subfunc))) 1627346fa2f8SChristian Borntraeger return -EFAULT; 1628346fa2f8SChristian Borntraeger 162911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 163011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0], 163111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1], 163211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2], 163311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]); 163411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx", 163511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0], 163611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]); 163711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx", 163811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0], 163911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]); 164011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx", 164111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0], 164211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]); 164311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx", 164411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[0], 164511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]); 164611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx", 164711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0], 164811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]); 164911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx", 165011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0], 165111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]); 165211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx", 165311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0], 165411ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]); 165511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx", 165611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0], 165711ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]); 165811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx", 165911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0], 166011ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]); 166111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx", 166211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0], 166311ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]); 166411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx", 166511ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0], 166611ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]); 166711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx", 166811ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0], 166911ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]); 167011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx", 167111ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0], 167211ba5961SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]); 167313209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx", 167413209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0], 167513209ad0SChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]); 1676173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1677173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0], 1678173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1], 1679173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2], 1680173aec2dSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]); 16814f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 16824f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0], 16834f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1], 16844f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2], 16854f45b90eSChristian Borntraeger ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]); 168611ba5961SChristian Borntraeger 1687346fa2f8SChristian Borntraeger return 0; 16880a763c78SDavid Hildenbrand } 16890a763c78SDavid Hildenbrand 16900a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm, 16910a763c78SDavid Hildenbrand struct kvm_device_attr *attr) 16920a763c78SDavid Hildenbrand { 16930a763c78SDavid Hildenbrand if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc, 16940a763c78SDavid Hildenbrand sizeof(struct kvm_s390_vm_cpu_subfunc))) 16950a763c78SDavid Hildenbrand return -EFAULT; 169611ba5961SChristian Borntraeger 169711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 169811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[0], 169911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[1], 170011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[2], 170111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]); 170211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx", 170311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0], 170411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]); 170511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx", 170611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0], 170711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]); 170811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx", 170911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0], 171011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]); 171111ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx", 171211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[0], 171311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.km)[1]); 171411ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx", 171511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0], 171611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]); 171711ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx", 171811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0], 171911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]); 172011ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx", 172111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0], 172211ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]); 172311ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx", 172411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0], 172511ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]); 172611ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx", 172711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0], 172811ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]); 172911ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx", 173011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0], 173111ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]); 173211ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx", 173311ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0], 173411ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]); 173511ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx", 173611ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0], 173711ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]); 173811ba5961SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx", 173911ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[0], 174011ba5961SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]); 174113209ad0SChristian Borntraeger VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx", 174213209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0], 174313209ad0SChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]); 1744173aec2dSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 1745173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0], 1746173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1], 1747173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2], 1748173aec2dSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]); 17494f45b90eSChristian Borntraeger VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx", 17504f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0], 17514f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1], 17524f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2], 17534f45b90eSChristian Borntraeger ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]); 175411ba5961SChristian Borntraeger 17550a763c78SDavid Hildenbrand return 0; 17560a763c78SDavid Hildenbrand } 1757346fa2f8SChristian Borntraeger 1758658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr) 1759658b6edaSMichael Mueller { 1760658b6edaSMichael Mueller int ret = -ENXIO; 1761658b6edaSMichael Mueller 1762658b6edaSMichael Mueller switch (attr->attr) { 1763658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1764658b6edaSMichael Mueller ret = kvm_s390_get_processor(kvm, attr); 1765658b6edaSMichael Mueller break; 1766658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 1767658b6edaSMichael Mueller ret = kvm_s390_get_machine(kvm, attr); 1768658b6edaSMichael Mueller break; 176915c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 177015c9705fSDavid Hildenbrand ret = kvm_s390_get_processor_feat(kvm, attr); 177115c9705fSDavid Hildenbrand break; 177215c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 177315c9705fSDavid Hildenbrand ret = kvm_s390_get_machine_feat(kvm, attr); 177415c9705fSDavid Hildenbrand break; 17750a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 17760a763c78SDavid Hildenbrand ret = kvm_s390_get_processor_subfunc(kvm, attr); 17770a763c78SDavid Hildenbrand break; 17780a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 17790a763c78SDavid Hildenbrand ret = kvm_s390_get_machine_subfunc(kvm, attr); 17800a763c78SDavid Hildenbrand break; 1781658b6edaSMichael Mueller } 1782658b6edaSMichael Mueller return ret; 1783658b6edaSMichael Mueller } 1784658b6edaSMichael Mueller 178524fe0195SPierre Morel /** 178624fe0195SPierre Morel * kvm_s390_update_topology_change_report - update CPU topology change report 178724fe0195SPierre Morel * @kvm: guest KVM description 178824fe0195SPierre Morel * @val: set or clear the MTCR bit 178924fe0195SPierre Morel * 179024fe0195SPierre Morel * Updates the Multiprocessor Topology-Change-Report bit to signal 179124fe0195SPierre Morel * the guest with a topology change. 179224fe0195SPierre Morel * This is only relevant if the topology facility is present. 179324fe0195SPierre Morel * 179424fe0195SPierre Morel * The SCA version, bsca or esca, doesn't matter as offset is the same. 179524fe0195SPierre Morel */ 179624fe0195SPierre Morel static void kvm_s390_update_topology_change_report(struct kvm *kvm, bool val) 179724fe0195SPierre Morel { 179824fe0195SPierre Morel union sca_utility new, old; 179924fe0195SPierre Morel struct bsca_block *sca; 180024fe0195SPierre Morel 180124fe0195SPierre Morel read_lock(&kvm->arch.sca_lock); 180224fe0195SPierre Morel sca = kvm->arch.sca; 180324fe0195SPierre Morel do { 180424fe0195SPierre Morel old = READ_ONCE(sca->utility); 180524fe0195SPierre Morel new = old; 180624fe0195SPierre Morel new.mtcr = val; 180724fe0195SPierre Morel } while (cmpxchg(&sca->utility.val, old.val, new.val) != old.val); 180824fe0195SPierre Morel read_unlock(&kvm->arch.sca_lock); 180924fe0195SPierre Morel } 181024fe0195SPierre Morel 1811f5ecfee9SPierre Morel static int kvm_s390_set_topo_change_indication(struct kvm *kvm, 1812f5ecfee9SPierre Morel struct kvm_device_attr *attr) 1813f5ecfee9SPierre Morel { 1814f5ecfee9SPierre Morel if (!test_kvm_facility(kvm, 11)) 1815f5ecfee9SPierre Morel return -ENXIO; 1816f5ecfee9SPierre Morel 1817f5ecfee9SPierre Morel kvm_s390_update_topology_change_report(kvm, !!attr->attr); 1818f5ecfee9SPierre Morel return 0; 1819f5ecfee9SPierre Morel } 1820f5ecfee9SPierre Morel 1821f5ecfee9SPierre Morel static int kvm_s390_get_topo_change_indication(struct kvm *kvm, 1822f5ecfee9SPierre Morel struct kvm_device_attr *attr) 1823f5ecfee9SPierre Morel { 1824f5ecfee9SPierre Morel u8 topo; 1825f5ecfee9SPierre Morel 1826f5ecfee9SPierre Morel if (!test_kvm_facility(kvm, 11)) 1827f5ecfee9SPierre Morel return -ENXIO; 1828f5ecfee9SPierre Morel 1829f5ecfee9SPierre Morel read_lock(&kvm->arch.sca_lock); 1830f5ecfee9SPierre Morel topo = ((struct bsca_block *)kvm->arch.sca)->utility.mtcr; 1831f5ecfee9SPierre Morel read_unlock(&kvm->arch.sca_lock); 1832f5ecfee9SPierre Morel 1833f5ecfee9SPierre Morel return put_user(topo, (u8 __user *)attr->addr); 1834f5ecfee9SPierre Morel } 1835f5ecfee9SPierre Morel 1836f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1837f2061656SDominik Dingel { 1838f2061656SDominik Dingel int ret; 1839f2061656SDominik Dingel 1840f2061656SDominik Dingel switch (attr->group) { 18414f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 18428c0a7ce6SDominik Dingel ret = kvm_s390_set_mem_control(kvm, attr); 18434f718eabSDominik Dingel break; 184472f25020SJason J. Herne case KVM_S390_VM_TOD: 184572f25020SJason J. Herne ret = kvm_s390_set_tod(kvm, attr); 184672f25020SJason J. Herne break; 1847658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1848658b6edaSMichael Mueller ret = kvm_s390_set_cpu_model(kvm, attr); 1849658b6edaSMichael Mueller break; 1850a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1851a374e892STony Krowiak ret = kvm_s390_vm_set_crypto(kvm, attr); 1852a374e892STony Krowiak break; 1853190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1854190df4a2SClaudio Imbrenda ret = kvm_s390_vm_set_migration(kvm, attr); 1855190df4a2SClaudio Imbrenda break; 1856f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY: 1857f5ecfee9SPierre Morel ret = kvm_s390_set_topo_change_indication(kvm, attr); 1858f5ecfee9SPierre Morel break; 1859f2061656SDominik Dingel default: 1860f2061656SDominik Dingel ret = -ENXIO; 1861f2061656SDominik Dingel break; 1862f2061656SDominik Dingel } 1863f2061656SDominik Dingel 1864f2061656SDominik Dingel return ret; 1865f2061656SDominik Dingel } 1866f2061656SDominik Dingel 1867f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1868f2061656SDominik Dingel { 18698c0a7ce6SDominik Dingel int ret; 18708c0a7ce6SDominik Dingel 18718c0a7ce6SDominik Dingel switch (attr->group) { 18728c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_CTRL: 18738c0a7ce6SDominik Dingel ret = kvm_s390_get_mem_control(kvm, attr); 18748c0a7ce6SDominik Dingel break; 187572f25020SJason J. Herne case KVM_S390_VM_TOD: 187672f25020SJason J. Herne ret = kvm_s390_get_tod(kvm, attr); 187772f25020SJason J. Herne break; 1878658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1879658b6edaSMichael Mueller ret = kvm_s390_get_cpu_model(kvm, attr); 1880658b6edaSMichael Mueller break; 1881190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1882190df4a2SClaudio Imbrenda ret = kvm_s390_vm_get_migration(kvm, attr); 1883190df4a2SClaudio Imbrenda break; 1884f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY: 1885f5ecfee9SPierre Morel ret = kvm_s390_get_topo_change_indication(kvm, attr); 1886f5ecfee9SPierre Morel break; 18878c0a7ce6SDominik Dingel default: 18888c0a7ce6SDominik Dingel ret = -ENXIO; 18898c0a7ce6SDominik Dingel break; 18908c0a7ce6SDominik Dingel } 18918c0a7ce6SDominik Dingel 18928c0a7ce6SDominik Dingel return ret; 1893f2061656SDominik Dingel } 1894f2061656SDominik Dingel 1895f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1896f2061656SDominik Dingel { 1897f2061656SDominik Dingel int ret; 1898f2061656SDominik Dingel 1899f2061656SDominik Dingel switch (attr->group) { 19004f718eabSDominik Dingel case KVM_S390_VM_MEM_CTRL: 19014f718eabSDominik Dingel switch (attr->attr) { 19024f718eabSDominik Dingel case KVM_S390_VM_MEM_ENABLE_CMMA: 19034f718eabSDominik Dingel case KVM_S390_VM_MEM_CLR_CMMA: 1904f9cbd9b0SDavid Hildenbrand ret = sclp.has_cmma ? 0 : -ENXIO; 1905f9cbd9b0SDavid Hildenbrand break; 19068c0a7ce6SDominik Dingel case KVM_S390_VM_MEM_LIMIT_SIZE: 19074f718eabSDominik Dingel ret = 0; 19084f718eabSDominik Dingel break; 19094f718eabSDominik Dingel default: 19104f718eabSDominik Dingel ret = -ENXIO; 19114f718eabSDominik Dingel break; 19124f718eabSDominik Dingel } 19134f718eabSDominik Dingel break; 191472f25020SJason J. Herne case KVM_S390_VM_TOD: 191572f25020SJason J. Herne switch (attr->attr) { 191672f25020SJason J. Herne case KVM_S390_VM_TOD_LOW: 191772f25020SJason J. Herne case KVM_S390_VM_TOD_HIGH: 191872f25020SJason J. Herne ret = 0; 191972f25020SJason J. Herne break; 192072f25020SJason J. Herne default: 192172f25020SJason J. Herne ret = -ENXIO; 192272f25020SJason J. Herne break; 192372f25020SJason J. Herne } 192472f25020SJason J. Herne break; 1925658b6edaSMichael Mueller case KVM_S390_VM_CPU_MODEL: 1926658b6edaSMichael Mueller switch (attr->attr) { 1927658b6edaSMichael Mueller case KVM_S390_VM_CPU_PROCESSOR: 1928658b6edaSMichael Mueller case KVM_S390_VM_CPU_MACHINE: 192915c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_PROCESSOR_FEAT: 193015c9705fSDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_FEAT: 19310a763c78SDavid Hildenbrand case KVM_S390_VM_CPU_MACHINE_SUBFUNC: 1932346fa2f8SChristian Borntraeger case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC: 1933658b6edaSMichael Mueller ret = 0; 1934658b6edaSMichael Mueller break; 1935658b6edaSMichael Mueller default: 1936658b6edaSMichael Mueller ret = -ENXIO; 1937658b6edaSMichael Mueller break; 1938658b6edaSMichael Mueller } 1939658b6edaSMichael Mueller break; 1940a374e892STony Krowiak case KVM_S390_VM_CRYPTO: 1941a374e892STony Krowiak switch (attr->attr) { 1942a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_AES_KW: 1943a374e892STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW: 1944a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_AES_KW: 1945a374e892STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW: 1946a374e892STony Krowiak ret = 0; 1947a374e892STony Krowiak break; 194837940fb0STony Krowiak case KVM_S390_VM_CRYPTO_ENABLE_APIE: 194937940fb0STony Krowiak case KVM_S390_VM_CRYPTO_DISABLE_APIE: 195037940fb0STony Krowiak ret = ap_instructions_available() ? 0 : -ENXIO; 195137940fb0STony Krowiak break; 1952a374e892STony Krowiak default: 1953a374e892STony Krowiak ret = -ENXIO; 1954a374e892STony Krowiak break; 1955a374e892STony Krowiak } 1956a374e892STony Krowiak break; 1957190df4a2SClaudio Imbrenda case KVM_S390_VM_MIGRATION: 1958190df4a2SClaudio Imbrenda ret = 0; 1959190df4a2SClaudio Imbrenda break; 1960f5ecfee9SPierre Morel case KVM_S390_VM_CPU_TOPOLOGY: 1961f5ecfee9SPierre Morel ret = test_kvm_facility(kvm, 11) ? 0 : -ENXIO; 1962f5ecfee9SPierre Morel break; 1963f2061656SDominik Dingel default: 1964f2061656SDominik Dingel ret = -ENXIO; 1965f2061656SDominik Dingel break; 1966f2061656SDominik Dingel } 1967f2061656SDominik Dingel 1968f2061656SDominik Dingel return ret; 1969f2061656SDominik Dingel } 1970f2061656SDominik Dingel 197130ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 197230ee2a98SJason J. Herne { 197330ee2a98SJason J. Herne uint8_t *keys; 197430ee2a98SJason J. Herne uint64_t hva; 19754f899147SChristian Borntraeger int srcu_idx, i, r = 0; 197630ee2a98SJason J. Herne 197730ee2a98SJason J. Herne if (args->flags != 0) 197830ee2a98SJason J. Herne return -EINVAL; 197930ee2a98SJason J. Herne 198030ee2a98SJason J. Herne /* Is this guest using storage keys? */ 198155531b74SJanosch Frank if (!mm_uses_skeys(current->mm)) 198230ee2a98SJason J. Herne return KVM_S390_GET_SKEYS_NONE; 198330ee2a98SJason J. Herne 198430ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 198530ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 198630ee2a98SJason J. Herne return -EINVAL; 198730ee2a98SJason J. Herne 1988c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); 198930ee2a98SJason J. Herne if (!keys) 199030ee2a98SJason J. Herne return -ENOMEM; 199130ee2a98SJason J. Herne 1992d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm); 19934f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 199430ee2a98SJason J. Herne for (i = 0; i < args->count; i++) { 199530ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 199630ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 199730ee2a98SJason J. Herne r = -EFAULT; 1998d3ed1ceeSMartin Schwidefsky break; 199930ee2a98SJason J. Herne } 200030ee2a98SJason J. Herne 2001154c8c19SDavid Hildenbrand r = get_guest_storage_key(current->mm, hva, &keys[i]); 2002154c8c19SDavid Hildenbrand if (r) 2003d3ed1ceeSMartin Schwidefsky break; 200430ee2a98SJason J. Herne } 20054f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 2006d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm); 200730ee2a98SJason J. Herne 2008d3ed1ceeSMartin Schwidefsky if (!r) { 200930ee2a98SJason J. Herne r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys, 201030ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 201130ee2a98SJason J. Herne if (r) 201230ee2a98SJason J. Herne r = -EFAULT; 2013d3ed1ceeSMartin Schwidefsky } 2014d3ed1ceeSMartin Schwidefsky 201530ee2a98SJason J. Herne kvfree(keys); 201630ee2a98SJason J. Herne return r; 201730ee2a98SJason J. Herne } 201830ee2a98SJason J. Herne 201930ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args) 202030ee2a98SJason J. Herne { 202130ee2a98SJason J. Herne uint8_t *keys; 202230ee2a98SJason J. Herne uint64_t hva; 20234f899147SChristian Borntraeger int srcu_idx, i, r = 0; 2024bd096f64SJanosch Frank bool unlocked; 202530ee2a98SJason J. Herne 202630ee2a98SJason J. Herne if (args->flags != 0) 202730ee2a98SJason J. Herne return -EINVAL; 202830ee2a98SJason J. Herne 202930ee2a98SJason J. Herne /* Enforce sane limit on memory allocation */ 203030ee2a98SJason J. Herne if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX) 203130ee2a98SJason J. Herne return -EINVAL; 203230ee2a98SJason J. Herne 2033c4196218SChristian Borntraeger keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT); 203430ee2a98SJason J. Herne if (!keys) 203530ee2a98SJason J. Herne return -ENOMEM; 203630ee2a98SJason J. Herne 203730ee2a98SJason J. Herne r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr, 203830ee2a98SJason J. Herne sizeof(uint8_t) * args->count); 203930ee2a98SJason J. Herne if (r) { 204030ee2a98SJason J. Herne r = -EFAULT; 204130ee2a98SJason J. Herne goto out; 204230ee2a98SJason J. Herne } 204330ee2a98SJason J. Herne 204430ee2a98SJason J. Herne /* Enable storage key handling for the guest */ 204514d4a425SDominik Dingel r = s390_enable_skey(); 204614d4a425SDominik Dingel if (r) 204714d4a425SDominik Dingel goto out; 204830ee2a98SJason J. Herne 2049bd096f64SJanosch Frank i = 0; 2050d8ed45c5SMichel Lespinasse mmap_read_lock(current->mm); 20514f899147SChristian Borntraeger srcu_idx = srcu_read_lock(&kvm->srcu); 2052bd096f64SJanosch Frank while (i < args->count) { 2053bd096f64SJanosch Frank unlocked = false; 205430ee2a98SJason J. Herne hva = gfn_to_hva(kvm, args->start_gfn + i); 205530ee2a98SJason J. Herne if (kvm_is_error_hva(hva)) { 205630ee2a98SJason J. Herne r = -EFAULT; 2057d3ed1ceeSMartin Schwidefsky break; 205830ee2a98SJason J. Herne } 205930ee2a98SJason J. Herne 206030ee2a98SJason J. Herne /* Lowest order bit is reserved */ 206130ee2a98SJason J. Herne if (keys[i] & 0x01) { 206230ee2a98SJason J. Herne r = -EINVAL; 2063d3ed1ceeSMartin Schwidefsky break; 206430ee2a98SJason J. Herne } 206530ee2a98SJason J. Herne 2066fe69eabfSDavid Hildenbrand r = set_guest_storage_key(current->mm, hva, keys[i], 0); 2067bd096f64SJanosch Frank if (r) { 206864019a2eSPeter Xu r = fixup_user_fault(current->mm, hva, 2069bd096f64SJanosch Frank FAULT_FLAG_WRITE, &unlocked); 207030ee2a98SJason J. Herne if (r) 2071d3ed1ceeSMartin Schwidefsky break; 207230ee2a98SJason J. Herne } 2073bd096f64SJanosch Frank if (!r) 2074bd096f64SJanosch Frank i++; 2075bd096f64SJanosch Frank } 20764f899147SChristian Borntraeger srcu_read_unlock(&kvm->srcu, srcu_idx); 2077d8ed45c5SMichel Lespinasse mmap_read_unlock(current->mm); 207830ee2a98SJason J. Herne out: 207930ee2a98SJason J. Herne kvfree(keys); 208030ee2a98SJason J. Herne return r; 208130ee2a98SJason J. Herne } 208230ee2a98SJason J. Herne 20834036e387SClaudio Imbrenda /* 20844036e387SClaudio Imbrenda * Base address and length must be sent at the start of each block, therefore 20854036e387SClaudio Imbrenda * it's cheaper to send some clean data, as long as it's less than the size of 20864036e387SClaudio Imbrenda * two longs. 20874036e387SClaudio Imbrenda */ 20884036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) 20894036e387SClaudio Imbrenda /* for consistency */ 20904036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) 20914036e387SClaudio Imbrenda 2092afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 2093afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 2094afdad616SClaudio Imbrenda { 2095afdad616SClaudio Imbrenda unsigned long pgstev, hva, cur_gfn = args->start_gfn; 2096afdad616SClaudio Imbrenda 2097afdad616SClaudio Imbrenda args->count = 0; 2098afdad616SClaudio Imbrenda while (args->count < bufsize) { 2099afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2100afdad616SClaudio Imbrenda /* 2101afdad616SClaudio Imbrenda * We return an error if the first value was invalid, but we 2102afdad616SClaudio Imbrenda * return successfully if at least one value was copied. 2103afdad616SClaudio Imbrenda */ 2104afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2105afdad616SClaudio Imbrenda return args->count ? 0 : -EFAULT; 2106afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2107afdad616SClaudio Imbrenda pgstev = 0; 2108afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2109afdad616SClaudio Imbrenda cur_gfn++; 2110afdad616SClaudio Imbrenda } 2111afdad616SClaudio Imbrenda 2112afdad616SClaudio Imbrenda return 0; 2113afdad616SClaudio Imbrenda } 2114afdad616SClaudio Imbrenda 2115c928bfc2SMaciej S. Szmigiero static struct kvm_memory_slot *gfn_to_memslot_approx(struct kvm_memslots *slots, 2116c928bfc2SMaciej S. Szmigiero gfn_t gfn) 2117c928bfc2SMaciej S. Szmigiero { 2118c928bfc2SMaciej S. Szmigiero return ____gfn_to_memslot(slots, gfn, true); 2119c928bfc2SMaciej S. Szmigiero } 2120c928bfc2SMaciej S. Szmigiero 2121afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots, 2122afdad616SClaudio Imbrenda unsigned long cur_gfn) 2123afdad616SClaudio Imbrenda { 2124c928bfc2SMaciej S. Szmigiero struct kvm_memory_slot *ms = gfn_to_memslot_approx(slots, cur_gfn); 2125afdad616SClaudio Imbrenda unsigned long ofs = cur_gfn - ms->base_gfn; 2126a54d8066SMaciej S. Szmigiero struct rb_node *mnode = &ms->gfn_node[slots->node_idx]; 2127afdad616SClaudio Imbrenda 2128afdad616SClaudio Imbrenda if (ms->base_gfn + ms->npages <= cur_gfn) { 2129a54d8066SMaciej S. Szmigiero mnode = rb_next(mnode); 2130afdad616SClaudio Imbrenda /* If we are above the highest slot, wrap around */ 2131a54d8066SMaciej S. Szmigiero if (!mnode) 2132a54d8066SMaciej S. Szmigiero mnode = rb_first(&slots->gfn_tree); 2133afdad616SClaudio Imbrenda 2134a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]); 2135afdad616SClaudio Imbrenda ofs = 0; 2136afdad616SClaudio Imbrenda } 2137afdad616SClaudio Imbrenda ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs); 2138a54d8066SMaciej S. Szmigiero while (ofs >= ms->npages && (mnode = rb_next(mnode))) { 2139a54d8066SMaciej S. Szmigiero ms = container_of(mnode, struct kvm_memory_slot, gfn_node[slots->node_idx]); 2140b5c7e7ecSYury Norov ofs = find_first_bit(kvm_second_dirty_bitmap(ms), ms->npages); 2141afdad616SClaudio Imbrenda } 2142afdad616SClaudio Imbrenda return ms->base_gfn + ofs; 2143afdad616SClaudio Imbrenda } 2144afdad616SClaudio Imbrenda 2145afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args, 2146afdad616SClaudio Imbrenda u8 *res, unsigned long bufsize) 2147afdad616SClaudio Imbrenda { 2148afdad616SClaudio Imbrenda unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev; 2149afdad616SClaudio Imbrenda struct kvm_memslots *slots = kvm_memslots(kvm); 2150afdad616SClaudio Imbrenda struct kvm_memory_slot *ms; 2151afdad616SClaudio Imbrenda 2152a54d8066SMaciej S. Szmigiero if (unlikely(kvm_memslots_empty(slots))) 21530774a964SSean Christopherson return 0; 21540774a964SSean Christopherson 2155afdad616SClaudio Imbrenda cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn); 2156afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2157afdad616SClaudio Imbrenda args->count = 0; 2158afdad616SClaudio Imbrenda args->start_gfn = cur_gfn; 2159afdad616SClaudio Imbrenda if (!ms) 2160afdad616SClaudio Imbrenda return 0; 2161afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 21626a656832SMaciej S. Szmigiero mem_end = kvm_s390_get_gfn_end(slots); 2163afdad616SClaudio Imbrenda 2164afdad616SClaudio Imbrenda while (args->count < bufsize) { 2165afdad616SClaudio Imbrenda hva = gfn_to_hva(kvm, cur_gfn); 2166afdad616SClaudio Imbrenda if (kvm_is_error_hva(hva)) 2167afdad616SClaudio Imbrenda return 0; 2168afdad616SClaudio Imbrenda /* Decrement only if we actually flipped the bit to 0 */ 2169afdad616SClaudio Imbrenda if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms))) 2170afdad616SClaudio Imbrenda atomic64_dec(&kvm->arch.cmma_dirty_pages); 2171afdad616SClaudio Imbrenda if (get_pgste(kvm->mm, hva, &pgstev) < 0) 2172afdad616SClaudio Imbrenda pgstev = 0; 2173afdad616SClaudio Imbrenda /* Save the value */ 2174afdad616SClaudio Imbrenda res[args->count++] = (pgstev >> 24) & 0x43; 2175afdad616SClaudio Imbrenda /* If the next bit is too far away, stop. */ 2176afdad616SClaudio Imbrenda if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE) 2177afdad616SClaudio Imbrenda return 0; 2178afdad616SClaudio Imbrenda /* If we reached the previous "next", find the next one */ 2179afdad616SClaudio Imbrenda if (cur_gfn == next_gfn) 2180afdad616SClaudio Imbrenda next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1); 2181afdad616SClaudio Imbrenda /* Reached the end of memory or of the buffer, stop */ 2182afdad616SClaudio Imbrenda if ((next_gfn >= mem_end) || 2183afdad616SClaudio Imbrenda (next_gfn - args->start_gfn >= bufsize)) 2184afdad616SClaudio Imbrenda return 0; 2185afdad616SClaudio Imbrenda cur_gfn++; 2186afdad616SClaudio Imbrenda /* Reached the end of the current memslot, take the next one. */ 2187afdad616SClaudio Imbrenda if (cur_gfn - ms->base_gfn >= ms->npages) { 2188afdad616SClaudio Imbrenda ms = gfn_to_memslot(kvm, cur_gfn); 2189afdad616SClaudio Imbrenda if (!ms) 2190afdad616SClaudio Imbrenda return 0; 2191afdad616SClaudio Imbrenda } 2192afdad616SClaudio Imbrenda } 2193afdad616SClaudio Imbrenda return 0; 2194afdad616SClaudio Imbrenda } 2195afdad616SClaudio Imbrenda 2196afdad616SClaudio Imbrenda /* 21974036e387SClaudio Imbrenda * This function searches for the next page with dirty CMMA attributes, and 21984036e387SClaudio Imbrenda * saves the attributes in the buffer up to either the end of the buffer or 21994036e387SClaudio Imbrenda * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found; 22004036e387SClaudio Imbrenda * no trailing clean bytes are saved. 22014036e387SClaudio Imbrenda * In case no dirty bits were found, or if CMMA was not enabled or used, the 22024036e387SClaudio Imbrenda * output buffer will indicate 0 as length. 22034036e387SClaudio Imbrenda */ 22044036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm, 22054036e387SClaudio Imbrenda struct kvm_s390_cmma_log *args) 22064036e387SClaudio Imbrenda { 2207afdad616SClaudio Imbrenda unsigned long bufsize; 2208afdad616SClaudio Imbrenda int srcu_idx, peek, ret; 2209afdad616SClaudio Imbrenda u8 *values; 22104036e387SClaudio Imbrenda 2211afdad616SClaudio Imbrenda if (!kvm->arch.use_cmma) 22124036e387SClaudio Imbrenda return -ENXIO; 22134036e387SClaudio Imbrenda /* Invalid/unsupported flags were specified */ 22144036e387SClaudio Imbrenda if (args->flags & ~KVM_S390_CMMA_PEEK) 22154036e387SClaudio Imbrenda return -EINVAL; 22164036e387SClaudio Imbrenda /* Migration mode query, and we are not doing a migration */ 22174036e387SClaudio Imbrenda peek = !!(args->flags & KVM_S390_CMMA_PEEK); 2218afdad616SClaudio Imbrenda if (!peek && !kvm->arch.migration_mode) 22194036e387SClaudio Imbrenda return -EINVAL; 22204036e387SClaudio Imbrenda /* CMMA is disabled or was not used, or the buffer has length zero */ 22214036e387SClaudio Imbrenda bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX); 2222c9f0a2b8SJanosch Frank if (!bufsize || !kvm->mm->context.uses_cmm) { 22234036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 22244036e387SClaudio Imbrenda return 0; 22254036e387SClaudio Imbrenda } 22264036e387SClaudio Imbrenda /* We are not peeking, and there are no dirty pages */ 2227afdad616SClaudio Imbrenda if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) { 22284036e387SClaudio Imbrenda memset(args, 0, sizeof(*args)); 22294036e387SClaudio Imbrenda return 0; 22304036e387SClaudio Imbrenda } 22314036e387SClaudio Imbrenda 2232afdad616SClaudio Imbrenda values = vmalloc(bufsize); 2233afdad616SClaudio Imbrenda if (!values) 22344036e387SClaudio Imbrenda return -ENOMEM; 22354036e387SClaudio Imbrenda 2236d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 22374036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 2238afdad616SClaudio Imbrenda if (peek) 2239afdad616SClaudio Imbrenda ret = kvm_s390_peek_cmma(kvm, args, values, bufsize); 2240afdad616SClaudio Imbrenda else 2241afdad616SClaudio Imbrenda ret = kvm_s390_get_cmma(kvm, args, values, bufsize); 22424036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2243d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 22444036e387SClaudio Imbrenda 2245afdad616SClaudio Imbrenda if (kvm->arch.migration_mode) 2246afdad616SClaudio Imbrenda args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages); 2247afdad616SClaudio Imbrenda else 2248afdad616SClaudio Imbrenda args->remaining = 0; 22494036e387SClaudio Imbrenda 2250afdad616SClaudio Imbrenda if (copy_to_user((void __user *)args->values, values, args->count)) 2251afdad616SClaudio Imbrenda ret = -EFAULT; 2252afdad616SClaudio Imbrenda 2253afdad616SClaudio Imbrenda vfree(values); 2254afdad616SClaudio Imbrenda return ret; 22554036e387SClaudio Imbrenda } 22564036e387SClaudio Imbrenda 22574036e387SClaudio Imbrenda /* 22584036e387SClaudio Imbrenda * This function sets the CMMA attributes for the given pages. If the input 22594036e387SClaudio Imbrenda * buffer has zero length, no action is taken, otherwise the attributes are 2260c9f0a2b8SJanosch Frank * set and the mm->context.uses_cmm flag is set. 22614036e387SClaudio Imbrenda */ 22624036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm, 22634036e387SClaudio Imbrenda const struct kvm_s390_cmma_log *args) 22644036e387SClaudio Imbrenda { 22654036e387SClaudio Imbrenda unsigned long hva, mask, pgstev, i; 22664036e387SClaudio Imbrenda uint8_t *bits; 22674036e387SClaudio Imbrenda int srcu_idx, r = 0; 22684036e387SClaudio Imbrenda 22694036e387SClaudio Imbrenda mask = args->mask; 22704036e387SClaudio Imbrenda 22714036e387SClaudio Imbrenda if (!kvm->arch.use_cmma) 22724036e387SClaudio Imbrenda return -ENXIO; 22734036e387SClaudio Imbrenda /* invalid/unsupported flags */ 22744036e387SClaudio Imbrenda if (args->flags != 0) 22754036e387SClaudio Imbrenda return -EINVAL; 22764036e387SClaudio Imbrenda /* Enforce sane limit on memory allocation */ 22774036e387SClaudio Imbrenda if (args->count > KVM_S390_CMMA_SIZE_MAX) 22784036e387SClaudio Imbrenda return -EINVAL; 22794036e387SClaudio Imbrenda /* Nothing to do */ 22804036e387SClaudio Imbrenda if (args->count == 0) 22814036e387SClaudio Imbrenda return 0; 22824036e387SClaudio Imbrenda 228342bc47b3SKees Cook bits = vmalloc(array_size(sizeof(*bits), args->count)); 22844036e387SClaudio Imbrenda if (!bits) 22854036e387SClaudio Imbrenda return -ENOMEM; 22864036e387SClaudio Imbrenda 22874036e387SClaudio Imbrenda r = copy_from_user(bits, (void __user *)args->values, args->count); 22884036e387SClaudio Imbrenda if (r) { 22894036e387SClaudio Imbrenda r = -EFAULT; 22904036e387SClaudio Imbrenda goto out; 22914036e387SClaudio Imbrenda } 22924036e387SClaudio Imbrenda 2293d8ed45c5SMichel Lespinasse mmap_read_lock(kvm->mm); 22944036e387SClaudio Imbrenda srcu_idx = srcu_read_lock(&kvm->srcu); 22954036e387SClaudio Imbrenda for (i = 0; i < args->count; i++) { 22964036e387SClaudio Imbrenda hva = gfn_to_hva(kvm, args->start_gfn + i); 22974036e387SClaudio Imbrenda if (kvm_is_error_hva(hva)) { 22984036e387SClaudio Imbrenda r = -EFAULT; 22994036e387SClaudio Imbrenda break; 23004036e387SClaudio Imbrenda } 23014036e387SClaudio Imbrenda 23024036e387SClaudio Imbrenda pgstev = bits[i]; 23034036e387SClaudio Imbrenda pgstev = pgstev << 24; 23041bab1c02SClaudio Imbrenda mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT; 23054036e387SClaudio Imbrenda set_pgste_bits(kvm->mm, hva, mask, pgstev); 23064036e387SClaudio Imbrenda } 23074036e387SClaudio Imbrenda srcu_read_unlock(&kvm->srcu, srcu_idx); 2308d8ed45c5SMichel Lespinasse mmap_read_unlock(kvm->mm); 23094036e387SClaudio Imbrenda 2310c9f0a2b8SJanosch Frank if (!kvm->mm->context.uses_cmm) { 2311d8ed45c5SMichel Lespinasse mmap_write_lock(kvm->mm); 2312c9f0a2b8SJanosch Frank kvm->mm->context.uses_cmm = 1; 2313d8ed45c5SMichel Lespinasse mmap_write_unlock(kvm->mm); 23144036e387SClaudio Imbrenda } 23154036e387SClaudio Imbrenda out: 23164036e387SClaudio Imbrenda vfree(bits); 23174036e387SClaudio Imbrenda return r; 23184036e387SClaudio Imbrenda } 23194036e387SClaudio Imbrenda 2320be48d86fSClaudio Imbrenda /** 2321be48d86fSClaudio Imbrenda * kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to 2322be48d86fSClaudio Imbrenda * non protected. 2323be48d86fSClaudio Imbrenda * @kvm: the VM whose protected vCPUs are to be converted 2324be48d86fSClaudio Imbrenda * @rc: return value for the RC field of the UVC (in case of error) 2325be48d86fSClaudio Imbrenda * @rrc: return value for the RRC field of the UVC (in case of error) 2326be48d86fSClaudio Imbrenda * 2327be48d86fSClaudio Imbrenda * Does not stop in case of error, tries to convert as many 2328be48d86fSClaudio Imbrenda * CPUs as possible. In case of error, the RC and RRC of the last error are 2329be48d86fSClaudio Imbrenda * returned. 2330be48d86fSClaudio Imbrenda * 2331be48d86fSClaudio Imbrenda * Return: 0 in case of success, otherwise -EIO 2332be48d86fSClaudio Imbrenda */ 2333be48d86fSClaudio Imbrenda int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc) 233429b40f10SJanosch Frank { 233529b40f10SJanosch Frank struct kvm_vcpu *vcpu; 233646808a4cSMarc Zyngier unsigned long i; 2337be48d86fSClaudio Imbrenda u16 _rc, _rrc; 2338be48d86fSClaudio Imbrenda int ret = 0; 233929b40f10SJanosch Frank 234029b40f10SJanosch Frank /* 234129b40f10SJanosch Frank * We ignore failures and try to destroy as many CPUs as possible. 234229b40f10SJanosch Frank * At the same time we must not free the assigned resources when 234329b40f10SJanosch Frank * this fails, as the ultravisor has still access to that memory. 234429b40f10SJanosch Frank * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak 234529b40f10SJanosch Frank * behind. 234629b40f10SJanosch Frank * We want to return the first failure rc and rrc, though. 234729b40f10SJanosch Frank */ 234829b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 234929b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 2350be48d86fSClaudio Imbrenda if (kvm_s390_pv_destroy_cpu(vcpu, &_rc, &_rrc) && !ret) { 2351be48d86fSClaudio Imbrenda *rc = _rc; 2352be48d86fSClaudio Imbrenda *rrc = _rrc; 235329b40f10SJanosch Frank ret = -EIO; 235429b40f10SJanosch Frank } 235529b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 235629b40f10SJanosch Frank } 2357ee6a569dSMichael Mueller /* Ensure that we re-enable gisa if the non-PV guest used it but the PV guest did not. */ 2358ee6a569dSMichael Mueller if (use_gisa) 2359ee6a569dSMichael Mueller kvm_s390_gisa_enable(kvm); 236029b40f10SJanosch Frank return ret; 236129b40f10SJanosch Frank } 236229b40f10SJanosch Frank 2363be48d86fSClaudio Imbrenda /** 2364be48d86fSClaudio Imbrenda * kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM 2365be48d86fSClaudio Imbrenda * to protected. 2366be48d86fSClaudio Imbrenda * @kvm: the VM whose protected vCPUs are to be converted 2367be48d86fSClaudio Imbrenda * @rc: return value for the RC field of the UVC (in case of error) 2368be48d86fSClaudio Imbrenda * @rrc: return value for the RRC field of the UVC (in case of error) 2369be48d86fSClaudio Imbrenda * 2370be48d86fSClaudio Imbrenda * Tries to undo the conversion in case of error. 2371be48d86fSClaudio Imbrenda * 2372be48d86fSClaudio Imbrenda * Return: 0 in case of success, otherwise -EIO 2373be48d86fSClaudio Imbrenda */ 237429b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc) 237529b40f10SJanosch Frank { 237646808a4cSMarc Zyngier unsigned long i; 237746808a4cSMarc Zyngier int r = 0; 237829b40f10SJanosch Frank u16 dummy; 237929b40f10SJanosch Frank 238029b40f10SJanosch Frank struct kvm_vcpu *vcpu; 238129b40f10SJanosch Frank 2382ee6a569dSMichael Mueller /* Disable the GISA if the ultravisor does not support AIV. */ 2383ee6a569dSMichael Mueller if (!test_bit_inv(BIT_UV_FEAT_AIV, &uv_info.uv_feature_indications)) 2384ee6a569dSMichael Mueller kvm_s390_gisa_disable(kvm); 2385ee6a569dSMichael Mueller 238629b40f10SJanosch Frank kvm_for_each_vcpu(i, vcpu, kvm) { 238729b40f10SJanosch Frank mutex_lock(&vcpu->mutex); 238829b40f10SJanosch Frank r = kvm_s390_pv_create_cpu(vcpu, rc, rrc); 238929b40f10SJanosch Frank mutex_unlock(&vcpu->mutex); 239029b40f10SJanosch Frank if (r) 239129b40f10SJanosch Frank break; 239229b40f10SJanosch Frank } 239329b40f10SJanosch Frank if (r) 239429b40f10SJanosch Frank kvm_s390_cpus_from_pv(kvm, &dummy, &dummy); 239529b40f10SJanosch Frank return r; 239629b40f10SJanosch Frank } 239729b40f10SJanosch Frank 239835d02493SJanosch Frank /* 239935d02493SJanosch Frank * Here we provide user space with a direct interface to query UV 240035d02493SJanosch Frank * related data like UV maxima and available features as well as 240135d02493SJanosch Frank * feature specific data. 240235d02493SJanosch Frank * 240335d02493SJanosch Frank * To facilitate future extension of the data structures we'll try to 240435d02493SJanosch Frank * write data up to the maximum requested length. 240535d02493SJanosch Frank */ 240635d02493SJanosch Frank static ssize_t kvm_s390_handle_pv_info(struct kvm_s390_pv_info *info) 240735d02493SJanosch Frank { 240835d02493SJanosch Frank ssize_t len_min; 240935d02493SJanosch Frank 241035d02493SJanosch Frank switch (info->header.id) { 241135d02493SJanosch Frank case KVM_PV_INFO_VM: { 241235d02493SJanosch Frank len_min = sizeof(info->header) + sizeof(info->vm); 241335d02493SJanosch Frank 241435d02493SJanosch Frank if (info->header.len_max < len_min) 241535d02493SJanosch Frank return -EINVAL; 241635d02493SJanosch Frank 241735d02493SJanosch Frank memcpy(info->vm.inst_calls_list, 241835d02493SJanosch Frank uv_info.inst_calls_list, 241935d02493SJanosch Frank sizeof(uv_info.inst_calls_list)); 242035d02493SJanosch Frank 242135d02493SJanosch Frank /* It's max cpuid not max cpus, so it's off by one */ 242235d02493SJanosch Frank info->vm.max_cpus = uv_info.max_guest_cpu_id + 1; 242335d02493SJanosch Frank info->vm.max_guests = uv_info.max_num_sec_conf; 242435d02493SJanosch Frank info->vm.max_guest_addr = uv_info.max_sec_stor_addr; 242535d02493SJanosch Frank info->vm.feature_indication = uv_info.uv_feature_indications; 242635d02493SJanosch Frank 242735d02493SJanosch Frank return len_min; 242835d02493SJanosch Frank } 2429fe9a93e0SJanosch Frank case KVM_PV_INFO_DUMP: { 2430fe9a93e0SJanosch Frank len_min = sizeof(info->header) + sizeof(info->dump); 2431fe9a93e0SJanosch Frank 2432fe9a93e0SJanosch Frank if (info->header.len_max < len_min) 2433fe9a93e0SJanosch Frank return -EINVAL; 2434fe9a93e0SJanosch Frank 2435fe9a93e0SJanosch Frank info->dump.dump_cpu_buffer_len = uv_info.guest_cpu_stor_len; 2436fe9a93e0SJanosch Frank info->dump.dump_config_mem_buffer_per_1m = uv_info.conf_dump_storage_state_len; 2437fe9a93e0SJanosch Frank info->dump.dump_config_finalize_len = uv_info.conf_dump_finalize_len; 2438fe9a93e0SJanosch Frank return len_min; 2439fe9a93e0SJanosch Frank } 244035d02493SJanosch Frank default: 244135d02493SJanosch Frank return -EINVAL; 244235d02493SJanosch Frank } 244335d02493SJanosch Frank } 244435d02493SJanosch Frank 24450460eb35SJanosch Frank static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd, 24460460eb35SJanosch Frank struct kvm_s390_pv_dmp dmp) 24470460eb35SJanosch Frank { 24480460eb35SJanosch Frank int r = -EINVAL; 24490460eb35SJanosch Frank void __user *result_buff = (void __user *)dmp.buff_addr; 24500460eb35SJanosch Frank 24510460eb35SJanosch Frank switch (dmp.subcmd) { 24520460eb35SJanosch Frank case KVM_PV_DUMP_INIT: { 24530460eb35SJanosch Frank if (kvm->arch.pv.dumping) 24540460eb35SJanosch Frank break; 24550460eb35SJanosch Frank 24560460eb35SJanosch Frank /* 24570460eb35SJanosch Frank * Block SIE entry as concurrent dump UVCs could lead 24580460eb35SJanosch Frank * to validities. 24590460eb35SJanosch Frank */ 24600460eb35SJanosch Frank kvm_s390_vcpu_block_all(kvm); 24610460eb35SJanosch Frank 24620460eb35SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 24630460eb35SJanosch Frank UVC_CMD_DUMP_INIT, &cmd->rc, &cmd->rrc); 24640460eb35SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP INIT: rc %x rrc %x", 24650460eb35SJanosch Frank cmd->rc, cmd->rrc); 24660460eb35SJanosch Frank if (!r) { 24670460eb35SJanosch Frank kvm->arch.pv.dumping = true; 24680460eb35SJanosch Frank } else { 24690460eb35SJanosch Frank kvm_s390_vcpu_unblock_all(kvm); 24700460eb35SJanosch Frank r = -EINVAL; 24710460eb35SJanosch Frank } 24720460eb35SJanosch Frank break; 24730460eb35SJanosch Frank } 24740460eb35SJanosch Frank case KVM_PV_DUMP_CONFIG_STOR_STATE: { 24750460eb35SJanosch Frank if (!kvm->arch.pv.dumping) 24760460eb35SJanosch Frank break; 24770460eb35SJanosch Frank 24780460eb35SJanosch Frank /* 24790460eb35SJanosch Frank * gaddr is an output parameter since we might stop 24800460eb35SJanosch Frank * early. As dmp will be copied back in our caller, we 24810460eb35SJanosch Frank * don't need to do it ourselves. 24820460eb35SJanosch Frank */ 24830460eb35SJanosch Frank r = kvm_s390_pv_dump_stor_state(kvm, result_buff, &dmp.gaddr, dmp.buff_len, 24840460eb35SJanosch Frank &cmd->rc, &cmd->rrc); 24850460eb35SJanosch Frank break; 24860460eb35SJanosch Frank } 24870460eb35SJanosch Frank case KVM_PV_DUMP_COMPLETE: { 24880460eb35SJanosch Frank if (!kvm->arch.pv.dumping) 24890460eb35SJanosch Frank break; 24900460eb35SJanosch Frank 24910460eb35SJanosch Frank r = -EINVAL; 24920460eb35SJanosch Frank if (dmp.buff_len < uv_info.conf_dump_finalize_len) 24930460eb35SJanosch Frank break; 24940460eb35SJanosch Frank 24950460eb35SJanosch Frank r = kvm_s390_pv_dump_complete(kvm, result_buff, 24960460eb35SJanosch Frank &cmd->rc, &cmd->rrc); 24970460eb35SJanosch Frank break; 24980460eb35SJanosch Frank } 24990460eb35SJanosch Frank default: 25000460eb35SJanosch Frank r = -ENOTTY; 25010460eb35SJanosch Frank break; 25020460eb35SJanosch Frank } 25030460eb35SJanosch Frank 25040460eb35SJanosch Frank return r; 25050460eb35SJanosch Frank } 25060460eb35SJanosch Frank 250729b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) 250829b40f10SJanosch Frank { 2509*fb491d55SClaudio Imbrenda const bool need_lock = (cmd->cmd != KVM_PV_ASYNC_CLEANUP_PERFORM); 2510*fb491d55SClaudio Imbrenda void __user *argp = (void __user *)cmd->data; 251129b40f10SJanosch Frank int r = 0; 251229b40f10SJanosch Frank u16 dummy; 2513*fb491d55SClaudio Imbrenda 2514*fb491d55SClaudio Imbrenda if (need_lock) 2515*fb491d55SClaudio Imbrenda mutex_lock(&kvm->lock); 251629b40f10SJanosch Frank 251729b40f10SJanosch Frank switch (cmd->cmd) { 251829b40f10SJanosch Frank case KVM_PV_ENABLE: { 251929b40f10SJanosch Frank r = -EINVAL; 252029b40f10SJanosch Frank if (kvm_s390_pv_is_protected(kvm)) 252129b40f10SJanosch Frank break; 252229b40f10SJanosch Frank 252329b40f10SJanosch Frank /* 252429b40f10SJanosch Frank * FMT 4 SIE needs esca. As we never switch back to bsca from 252529b40f10SJanosch Frank * esca, we need no cleanup in the error cases below 252629b40f10SJanosch Frank */ 252729b40f10SJanosch Frank r = sca_switch_to_extended(kvm); 252829b40f10SJanosch Frank if (r) 252929b40f10SJanosch Frank break; 253029b40f10SJanosch Frank 2531d8ed45c5SMichel Lespinasse mmap_write_lock(current->mm); 2532fa0c5eabSJanosch Frank r = gmap_mark_unmergeable(); 2533d8ed45c5SMichel Lespinasse mmap_write_unlock(current->mm); 2534fa0c5eabSJanosch Frank if (r) 2535fa0c5eabSJanosch Frank break; 2536fa0c5eabSJanosch Frank 253729b40f10SJanosch Frank r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc); 253829b40f10SJanosch Frank if (r) 253929b40f10SJanosch Frank break; 254029b40f10SJanosch Frank 254129b40f10SJanosch Frank r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc); 254229b40f10SJanosch Frank if (r) 254329b40f10SJanosch Frank kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy); 25440890ddeaSChristian Borntraeger 25450890ddeaSChristian Borntraeger /* we need to block service interrupts from now on */ 25460890ddeaSChristian Borntraeger set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 254729b40f10SJanosch Frank break; 254829b40f10SJanosch Frank } 2549*fb491d55SClaudio Imbrenda case KVM_PV_ASYNC_CLEANUP_PREPARE: 2550*fb491d55SClaudio Imbrenda r = -EINVAL; 2551*fb491d55SClaudio Imbrenda if (!kvm_s390_pv_is_protected(kvm) || !async_destroy) 2552*fb491d55SClaudio Imbrenda break; 2553*fb491d55SClaudio Imbrenda 2554*fb491d55SClaudio Imbrenda r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); 2555*fb491d55SClaudio Imbrenda /* 2556*fb491d55SClaudio Imbrenda * If a CPU could not be destroyed, destroy VM will also fail. 2557*fb491d55SClaudio Imbrenda * There is no point in trying to destroy it. Instead return 2558*fb491d55SClaudio Imbrenda * the rc and rrc from the first CPU that failed destroying. 2559*fb491d55SClaudio Imbrenda */ 2560*fb491d55SClaudio Imbrenda if (r) 2561*fb491d55SClaudio Imbrenda break; 2562*fb491d55SClaudio Imbrenda r = kvm_s390_pv_set_aside(kvm, &cmd->rc, &cmd->rrc); 2563*fb491d55SClaudio Imbrenda 2564*fb491d55SClaudio Imbrenda /* no need to block service interrupts any more */ 2565*fb491d55SClaudio Imbrenda clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 2566*fb491d55SClaudio Imbrenda break; 2567*fb491d55SClaudio Imbrenda case KVM_PV_ASYNC_CLEANUP_PERFORM: 2568*fb491d55SClaudio Imbrenda r = -EINVAL; 2569*fb491d55SClaudio Imbrenda if (!async_destroy) 2570*fb491d55SClaudio Imbrenda break; 2571*fb491d55SClaudio Imbrenda /* kvm->lock must not be held; this is asserted inside the function. */ 2572*fb491d55SClaudio Imbrenda r = kvm_s390_pv_deinit_aside_vm(kvm, &cmd->rc, &cmd->rrc); 2573*fb491d55SClaudio Imbrenda break; 257429b40f10SJanosch Frank case KVM_PV_DISABLE: { 257529b40f10SJanosch Frank r = -EINVAL; 257629b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 257729b40f10SJanosch Frank break; 257829b40f10SJanosch Frank 257929b40f10SJanosch Frank r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc); 258029b40f10SJanosch Frank /* 258129b40f10SJanosch Frank * If a CPU could not be destroyed, destroy VM will also fail. 258229b40f10SJanosch Frank * There is no point in trying to destroy it. Instead return 258329b40f10SJanosch Frank * the rc and rrc from the first CPU that failed destroying. 258429b40f10SJanosch Frank */ 258529b40f10SJanosch Frank if (r) 258629b40f10SJanosch Frank break; 2587*fb491d55SClaudio Imbrenda r = kvm_s390_pv_deinit_cleanup_all(kvm, &cmd->rc, &cmd->rrc); 25880890ddeaSChristian Borntraeger 25890890ddeaSChristian Borntraeger /* no need to block service interrupts any more */ 25900890ddeaSChristian Borntraeger clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs); 259129b40f10SJanosch Frank break; 259229b40f10SJanosch Frank } 259329b40f10SJanosch Frank case KVM_PV_SET_SEC_PARMS: { 259429b40f10SJanosch Frank struct kvm_s390_pv_sec_parm parms = {}; 259529b40f10SJanosch Frank void *hdr; 259629b40f10SJanosch Frank 259729b40f10SJanosch Frank r = -EINVAL; 259829b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 259929b40f10SJanosch Frank break; 260029b40f10SJanosch Frank 260129b40f10SJanosch Frank r = -EFAULT; 260229b40f10SJanosch Frank if (copy_from_user(&parms, argp, sizeof(parms))) 260329b40f10SJanosch Frank break; 260429b40f10SJanosch Frank 260529b40f10SJanosch Frank /* Currently restricted to 8KB */ 260629b40f10SJanosch Frank r = -EINVAL; 260729b40f10SJanosch Frank if (parms.length > PAGE_SIZE * 2) 260829b40f10SJanosch Frank break; 260929b40f10SJanosch Frank 261029b40f10SJanosch Frank r = -ENOMEM; 261129b40f10SJanosch Frank hdr = vmalloc(parms.length); 261229b40f10SJanosch Frank if (!hdr) 261329b40f10SJanosch Frank break; 261429b40f10SJanosch Frank 261529b40f10SJanosch Frank r = -EFAULT; 261629b40f10SJanosch Frank if (!copy_from_user(hdr, (void __user *)parms.origin, 261729b40f10SJanosch Frank parms.length)) 261829b40f10SJanosch Frank r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length, 261929b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 262029b40f10SJanosch Frank 262129b40f10SJanosch Frank vfree(hdr); 262229b40f10SJanosch Frank break; 262329b40f10SJanosch Frank } 262429b40f10SJanosch Frank case KVM_PV_UNPACK: { 262529b40f10SJanosch Frank struct kvm_s390_pv_unp unp = {}; 262629b40f10SJanosch Frank 262729b40f10SJanosch Frank r = -EINVAL; 26281ed576a2SJanosch Frank if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm)) 262929b40f10SJanosch Frank break; 263029b40f10SJanosch Frank 263129b40f10SJanosch Frank r = -EFAULT; 263229b40f10SJanosch Frank if (copy_from_user(&unp, argp, sizeof(unp))) 263329b40f10SJanosch Frank break; 263429b40f10SJanosch Frank 263529b40f10SJanosch Frank r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak, 263629b40f10SJanosch Frank &cmd->rc, &cmd->rrc); 263729b40f10SJanosch Frank break; 263829b40f10SJanosch Frank } 263929b40f10SJanosch Frank case KVM_PV_VERIFY: { 264029b40f10SJanosch Frank r = -EINVAL; 264129b40f10SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 264229b40f10SJanosch Frank break; 264329b40f10SJanosch Frank 264429b40f10SJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 264529b40f10SJanosch Frank UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc); 264629b40f10SJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc, 264729b40f10SJanosch Frank cmd->rrc); 264829b40f10SJanosch Frank break; 264929b40f10SJanosch Frank } 2650e0d2773dSJanosch Frank case KVM_PV_PREP_RESET: { 2651e0d2773dSJanosch Frank r = -EINVAL; 2652e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2653e0d2773dSJanosch Frank break; 2654e0d2773dSJanosch Frank 2655e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2656e0d2773dSJanosch Frank UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc); 2657e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x", 2658e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2659e0d2773dSJanosch Frank break; 2660e0d2773dSJanosch Frank } 2661e0d2773dSJanosch Frank case KVM_PV_UNSHARE_ALL: { 2662e0d2773dSJanosch Frank r = -EINVAL; 2663e0d2773dSJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 2664e0d2773dSJanosch Frank break; 2665e0d2773dSJanosch Frank 2666e0d2773dSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm), 2667e0d2773dSJanosch Frank UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc); 2668e0d2773dSJanosch Frank KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x", 2669e0d2773dSJanosch Frank cmd->rc, cmd->rrc); 2670e0d2773dSJanosch Frank break; 2671e0d2773dSJanosch Frank } 267235d02493SJanosch Frank case KVM_PV_INFO: { 267335d02493SJanosch Frank struct kvm_s390_pv_info info = {}; 267435d02493SJanosch Frank ssize_t data_len; 267535d02493SJanosch Frank 267635d02493SJanosch Frank /* 267735d02493SJanosch Frank * No need to check the VM protection here. 267835d02493SJanosch Frank * 267935d02493SJanosch Frank * Maybe user space wants to query some of the data 268035d02493SJanosch Frank * when the VM is still unprotected. If we see the 268135d02493SJanosch Frank * need to fence a new data command we can still 268235d02493SJanosch Frank * return an error in the info handler. 268335d02493SJanosch Frank */ 268435d02493SJanosch Frank 268535d02493SJanosch Frank r = -EFAULT; 268635d02493SJanosch Frank if (copy_from_user(&info, argp, sizeof(info.header))) 268735d02493SJanosch Frank break; 268835d02493SJanosch Frank 268935d02493SJanosch Frank r = -EINVAL; 269035d02493SJanosch Frank if (info.header.len_max < sizeof(info.header)) 269135d02493SJanosch Frank break; 269235d02493SJanosch Frank 269335d02493SJanosch Frank data_len = kvm_s390_handle_pv_info(&info); 269435d02493SJanosch Frank if (data_len < 0) { 269535d02493SJanosch Frank r = data_len; 269635d02493SJanosch Frank break; 269735d02493SJanosch Frank } 269835d02493SJanosch Frank /* 269935d02493SJanosch Frank * If a data command struct is extended (multiple 270035d02493SJanosch Frank * times) this can be used to determine how much of it 270135d02493SJanosch Frank * is valid. 270235d02493SJanosch Frank */ 270335d02493SJanosch Frank info.header.len_written = data_len; 270435d02493SJanosch Frank 270535d02493SJanosch Frank r = -EFAULT; 270635d02493SJanosch Frank if (copy_to_user(argp, &info, data_len)) 270735d02493SJanosch Frank break; 270835d02493SJanosch Frank 270935d02493SJanosch Frank r = 0; 271035d02493SJanosch Frank break; 271135d02493SJanosch Frank } 27120460eb35SJanosch Frank case KVM_PV_DUMP: { 27130460eb35SJanosch Frank struct kvm_s390_pv_dmp dmp; 27140460eb35SJanosch Frank 27150460eb35SJanosch Frank r = -EINVAL; 27160460eb35SJanosch Frank if (!kvm_s390_pv_is_protected(kvm)) 27170460eb35SJanosch Frank break; 27180460eb35SJanosch Frank 27190460eb35SJanosch Frank r = -EFAULT; 27200460eb35SJanosch Frank if (copy_from_user(&dmp, argp, sizeof(dmp))) 27210460eb35SJanosch Frank break; 27220460eb35SJanosch Frank 27230460eb35SJanosch Frank r = kvm_s390_pv_dmp(kvm, cmd, dmp); 27240460eb35SJanosch Frank if (r) 27250460eb35SJanosch Frank break; 27260460eb35SJanosch Frank 27270460eb35SJanosch Frank if (copy_to_user(argp, &dmp, sizeof(dmp))) { 27280460eb35SJanosch Frank r = -EFAULT; 27290460eb35SJanosch Frank break; 27300460eb35SJanosch Frank } 27310460eb35SJanosch Frank 27320460eb35SJanosch Frank break; 27330460eb35SJanosch Frank } 273429b40f10SJanosch Frank default: 273529b40f10SJanosch Frank r = -ENOTTY; 273629b40f10SJanosch Frank } 2737*fb491d55SClaudio Imbrenda if (need_lock) 2738*fb491d55SClaudio Imbrenda mutex_unlock(&kvm->lock); 2739*fb491d55SClaudio Imbrenda 274029b40f10SJanosch Frank return r; 274129b40f10SJanosch Frank } 274229b40f10SJanosch Frank 2743e9e9feebSJanis Schoetterl-Glausch static bool access_key_invalid(u8 access_key) 2744e9e9feebSJanis Schoetterl-Glausch { 2745e9e9feebSJanis Schoetterl-Glausch return access_key > 0xf; 2746e9e9feebSJanis Schoetterl-Glausch } 2747e9e9feebSJanis Schoetterl-Glausch 2748ef11c946SJanis Schoetterl-Glausch static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) 2749ef11c946SJanis Schoetterl-Glausch { 2750ef11c946SJanis Schoetterl-Glausch void __user *uaddr = (void __user *)mop->buf; 2751ef11c946SJanis Schoetterl-Glausch u64 supported_flags; 2752ef11c946SJanis Schoetterl-Glausch void *tmpbuf = NULL; 2753ef11c946SJanis Schoetterl-Glausch int r, srcu_idx; 2754ef11c946SJanis Schoetterl-Glausch 2755ef11c946SJanis Schoetterl-Glausch supported_flags = KVM_S390_MEMOP_F_SKEY_PROTECTION 2756ef11c946SJanis Schoetterl-Glausch | KVM_S390_MEMOP_F_CHECK_ONLY; 27573d9042f8SJanis Schoetterl-Glausch if (mop->flags & ~supported_flags || !mop->size) 2758ef11c946SJanis Schoetterl-Glausch return -EINVAL; 2759ef11c946SJanis Schoetterl-Glausch if (mop->size > MEM_OP_MAX_SIZE) 2760ef11c946SJanis Schoetterl-Glausch return -E2BIG; 2761b5d12744SJanis Schoetterl-Glausch /* 2762b5d12744SJanis Schoetterl-Glausch * This is technically a heuristic only, if the kvm->lock is not 2763b5d12744SJanis Schoetterl-Glausch * taken, it is not guaranteed that the vm is/remains non-protected. 2764b5d12744SJanis Schoetterl-Glausch * This is ok from a kernel perspective, wrongdoing is detected 2765b5d12744SJanis Schoetterl-Glausch * on the access, -EFAULT is returned and the vm may crash the 2766b5d12744SJanis Schoetterl-Glausch * next time it accesses the memory in question. 2767b5d12744SJanis Schoetterl-Glausch * There is no sane usecase to do switching and a memop on two 2768b5d12744SJanis Schoetterl-Glausch * different CPUs at the same time. 2769b5d12744SJanis Schoetterl-Glausch */ 2770b5d12744SJanis Schoetterl-Glausch if (kvm_s390_pv_get_handle(kvm)) 2771ef11c946SJanis Schoetterl-Glausch return -EINVAL; 2772ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { 2773ef11c946SJanis Schoetterl-Glausch if (access_key_invalid(mop->key)) 2774ef11c946SJanis Schoetterl-Glausch return -EINVAL; 2775ef11c946SJanis Schoetterl-Glausch } else { 2776ef11c946SJanis Schoetterl-Glausch mop->key = 0; 2777ef11c946SJanis Schoetterl-Glausch } 2778ef11c946SJanis Schoetterl-Glausch if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 2779ef11c946SJanis Schoetterl-Glausch tmpbuf = vmalloc(mop->size); 2780ef11c946SJanis Schoetterl-Glausch if (!tmpbuf) 2781ef11c946SJanis Schoetterl-Glausch return -ENOMEM; 2782ef11c946SJanis Schoetterl-Glausch } 2783ef11c946SJanis Schoetterl-Glausch 2784ef11c946SJanis Schoetterl-Glausch srcu_idx = srcu_read_lock(&kvm->srcu); 2785ef11c946SJanis Schoetterl-Glausch 2786ef11c946SJanis Schoetterl-Glausch if (kvm_is_error_gpa(kvm, mop->gaddr)) { 2787ef11c946SJanis Schoetterl-Glausch r = PGM_ADDRESSING; 2788ef11c946SJanis Schoetterl-Glausch goto out_unlock; 2789ef11c946SJanis Schoetterl-Glausch } 2790ef11c946SJanis Schoetterl-Glausch 2791ef11c946SJanis Schoetterl-Glausch switch (mop->op) { 2792ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_READ: { 2793ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 2794ef11c946SJanis Schoetterl-Glausch r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_FETCH, mop->key); 2795ef11c946SJanis Schoetterl-Glausch } else { 2796ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2797ef11c946SJanis Schoetterl-Glausch mop->size, GACC_FETCH, mop->key); 2798ef11c946SJanis Schoetterl-Glausch if (r == 0) { 2799ef11c946SJanis Schoetterl-Glausch if (copy_to_user(uaddr, tmpbuf, mop->size)) 2800ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2801ef11c946SJanis Schoetterl-Glausch } 2802ef11c946SJanis Schoetterl-Glausch } 2803ef11c946SJanis Schoetterl-Glausch break; 2804ef11c946SJanis Schoetterl-Glausch } 2805ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEMOP_ABSOLUTE_WRITE: { 2806ef11c946SJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 2807ef11c946SJanis Schoetterl-Glausch r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_STORE, mop->key); 2808ef11c946SJanis Schoetterl-Glausch } else { 2809ef11c946SJanis Schoetterl-Glausch if (copy_from_user(tmpbuf, uaddr, mop->size)) { 2810ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2811ef11c946SJanis Schoetterl-Glausch break; 2812ef11c946SJanis Schoetterl-Glausch } 2813ef11c946SJanis Schoetterl-Glausch r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf, 2814ef11c946SJanis Schoetterl-Glausch mop->size, GACC_STORE, mop->key); 2815ef11c946SJanis Schoetterl-Glausch } 2816ef11c946SJanis Schoetterl-Glausch break; 2817ef11c946SJanis Schoetterl-Glausch } 2818ef11c946SJanis Schoetterl-Glausch default: 2819ef11c946SJanis Schoetterl-Glausch r = -EINVAL; 2820ef11c946SJanis Schoetterl-Glausch } 2821ef11c946SJanis Schoetterl-Glausch 2822ef11c946SJanis Schoetterl-Glausch out_unlock: 2823ef11c946SJanis Schoetterl-Glausch srcu_read_unlock(&kvm->srcu, srcu_idx); 2824ef11c946SJanis Schoetterl-Glausch 2825ef11c946SJanis Schoetterl-Glausch vfree(tmpbuf); 2826ef11c946SJanis Schoetterl-Glausch return r; 2827ef11c946SJanis Schoetterl-Glausch } 2828ef11c946SJanis Schoetterl-Glausch 2829b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp, 2830b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 2831b0c632dbSHeiko Carstens { 2832b0c632dbSHeiko Carstens struct kvm *kvm = filp->private_data; 2833b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 2834f2061656SDominik Dingel struct kvm_device_attr attr; 2835b0c632dbSHeiko Carstens int r; 2836b0c632dbSHeiko Carstens 2837b0c632dbSHeiko Carstens switch (ioctl) { 2838ba5c1e9bSCarsten Otte case KVM_S390_INTERRUPT: { 2839ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 2840ba5c1e9bSCarsten Otte 2841ba5c1e9bSCarsten Otte r = -EFAULT; 2842ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 2843ba5c1e9bSCarsten Otte break; 2844ba5c1e9bSCarsten Otte r = kvm_s390_inject_vm(kvm, &s390int); 2845ba5c1e9bSCarsten Otte break; 2846ba5c1e9bSCarsten Otte } 284784223598SCornelia Huck case KVM_CREATE_IRQCHIP: { 284884223598SCornelia Huck struct kvm_irq_routing_entry routing; 284984223598SCornelia Huck 285084223598SCornelia Huck r = -EINVAL; 285184223598SCornelia Huck if (kvm->arch.use_irqchip) { 285284223598SCornelia Huck /* Set up dummy routing. */ 285384223598SCornelia Huck memset(&routing, 0, sizeof(routing)); 2854152b2839SNicholas Krause r = kvm_set_irq_routing(kvm, &routing, 0, 0); 285584223598SCornelia Huck } 285684223598SCornelia Huck break; 285784223598SCornelia Huck } 2858f2061656SDominik Dingel case KVM_SET_DEVICE_ATTR: { 2859f2061656SDominik Dingel r = -EFAULT; 2860f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2861f2061656SDominik Dingel break; 2862f2061656SDominik Dingel r = kvm_s390_vm_set_attr(kvm, &attr); 2863f2061656SDominik Dingel break; 2864f2061656SDominik Dingel } 2865f2061656SDominik Dingel case KVM_GET_DEVICE_ATTR: { 2866f2061656SDominik Dingel r = -EFAULT; 2867f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2868f2061656SDominik Dingel break; 2869f2061656SDominik Dingel r = kvm_s390_vm_get_attr(kvm, &attr); 2870f2061656SDominik Dingel break; 2871f2061656SDominik Dingel } 2872f2061656SDominik Dingel case KVM_HAS_DEVICE_ATTR: { 2873f2061656SDominik Dingel r = -EFAULT; 2874f2061656SDominik Dingel if (copy_from_user(&attr, (void __user *)arg, sizeof(attr))) 2875f2061656SDominik Dingel break; 2876f2061656SDominik Dingel r = kvm_s390_vm_has_attr(kvm, &attr); 2877f2061656SDominik Dingel break; 2878f2061656SDominik Dingel } 287930ee2a98SJason J. Herne case KVM_S390_GET_SKEYS: { 288030ee2a98SJason J. Herne struct kvm_s390_skeys args; 288130ee2a98SJason J. Herne 288230ee2a98SJason J. Herne r = -EFAULT; 288330ee2a98SJason J. Herne if (copy_from_user(&args, argp, 288430ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 288530ee2a98SJason J. Herne break; 288630ee2a98SJason J. Herne r = kvm_s390_get_skeys(kvm, &args); 288730ee2a98SJason J. Herne break; 288830ee2a98SJason J. Herne } 288930ee2a98SJason J. Herne case KVM_S390_SET_SKEYS: { 289030ee2a98SJason J. Herne struct kvm_s390_skeys args; 289130ee2a98SJason J. Herne 289230ee2a98SJason J. Herne r = -EFAULT; 289330ee2a98SJason J. Herne if (copy_from_user(&args, argp, 289430ee2a98SJason J. Herne sizeof(struct kvm_s390_skeys))) 289530ee2a98SJason J. Herne break; 289630ee2a98SJason J. Herne r = kvm_s390_set_skeys(kvm, &args); 289730ee2a98SJason J. Herne break; 289830ee2a98SJason J. Herne } 28994036e387SClaudio Imbrenda case KVM_S390_GET_CMMA_BITS: { 29004036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 29014036e387SClaudio Imbrenda 29024036e387SClaudio Imbrenda r = -EFAULT; 29034036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 29044036e387SClaudio Imbrenda break; 29051de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 29064036e387SClaudio Imbrenda r = kvm_s390_get_cmma_bits(kvm, &args); 29071de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 29084036e387SClaudio Imbrenda if (!r) { 29094036e387SClaudio Imbrenda r = copy_to_user(argp, &args, sizeof(args)); 29104036e387SClaudio Imbrenda if (r) 29114036e387SClaudio Imbrenda r = -EFAULT; 29124036e387SClaudio Imbrenda } 29134036e387SClaudio Imbrenda break; 29144036e387SClaudio Imbrenda } 29154036e387SClaudio Imbrenda case KVM_S390_SET_CMMA_BITS: { 29164036e387SClaudio Imbrenda struct kvm_s390_cmma_log args; 29174036e387SClaudio Imbrenda 29184036e387SClaudio Imbrenda r = -EFAULT; 29194036e387SClaudio Imbrenda if (copy_from_user(&args, argp, sizeof(args))) 29204036e387SClaudio Imbrenda break; 29211de1ea7eSChristian Borntraeger mutex_lock(&kvm->slots_lock); 29224036e387SClaudio Imbrenda r = kvm_s390_set_cmma_bits(kvm, &args); 29231de1ea7eSChristian Borntraeger mutex_unlock(&kvm->slots_lock); 29244036e387SClaudio Imbrenda break; 29254036e387SClaudio Imbrenda } 292629b40f10SJanosch Frank case KVM_S390_PV_COMMAND: { 292729b40f10SJanosch Frank struct kvm_pv_cmd args; 292829b40f10SJanosch Frank 292967cf68b6SEric Farman /* protvirt means user cpu state */ 293067cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(kvm); 293129b40f10SJanosch Frank r = 0; 293229b40f10SJanosch Frank if (!is_prot_virt_host()) { 293329b40f10SJanosch Frank r = -EINVAL; 293429b40f10SJanosch Frank break; 293529b40f10SJanosch Frank } 293629b40f10SJanosch Frank if (copy_from_user(&args, argp, sizeof(args))) { 293729b40f10SJanosch Frank r = -EFAULT; 293829b40f10SJanosch Frank break; 293929b40f10SJanosch Frank } 294029b40f10SJanosch Frank if (args.flags) { 294129b40f10SJanosch Frank r = -EINVAL; 294229b40f10SJanosch Frank break; 294329b40f10SJanosch Frank } 2944*fb491d55SClaudio Imbrenda /* must be called without kvm->lock */ 294529b40f10SJanosch Frank r = kvm_s390_handle_pv(kvm, &args); 294629b40f10SJanosch Frank if (copy_to_user(argp, &args, sizeof(args))) { 294729b40f10SJanosch Frank r = -EFAULT; 294829b40f10SJanosch Frank break; 294929b40f10SJanosch Frank } 295029b40f10SJanosch Frank break; 295129b40f10SJanosch Frank } 2952ef11c946SJanis Schoetterl-Glausch case KVM_S390_MEM_OP: { 2953ef11c946SJanis Schoetterl-Glausch struct kvm_s390_mem_op mem_op; 2954ef11c946SJanis Schoetterl-Glausch 2955ef11c946SJanis Schoetterl-Glausch if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 2956ef11c946SJanis Schoetterl-Glausch r = kvm_s390_vm_mem_op(kvm, &mem_op); 2957ef11c946SJanis Schoetterl-Glausch else 2958ef11c946SJanis Schoetterl-Glausch r = -EFAULT; 2959ef11c946SJanis Schoetterl-Glausch break; 2960ef11c946SJanis Schoetterl-Glausch } 2961db1c875eSMatthew Rosato case KVM_S390_ZPCI_OP: { 2962db1c875eSMatthew Rosato struct kvm_s390_zpci_op args; 2963db1c875eSMatthew Rosato 2964db1c875eSMatthew Rosato r = -EINVAL; 2965db1c875eSMatthew Rosato if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 2966db1c875eSMatthew Rosato break; 2967db1c875eSMatthew Rosato if (copy_from_user(&args, argp, sizeof(args))) { 2968db1c875eSMatthew Rosato r = -EFAULT; 2969db1c875eSMatthew Rosato break; 2970db1c875eSMatthew Rosato } 2971db1c875eSMatthew Rosato r = kvm_s390_pci_zpci_op(kvm, &args); 2972db1c875eSMatthew Rosato break; 2973db1c875eSMatthew Rosato } 2974b0c632dbSHeiko Carstens default: 2975367e1319SAvi Kivity r = -ENOTTY; 2976b0c632dbSHeiko Carstens } 2977b0c632dbSHeiko Carstens 2978b0c632dbSHeiko Carstens return r; 2979b0c632dbSHeiko Carstens } 2980b0c632dbSHeiko Carstens 298145c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void) 298245c9b47cSTony Krowiak { 2983e585b24aSTony Krowiak struct ap_config_info info; 298445c9b47cSTony Krowiak 2985e585b24aSTony Krowiak if (ap_instructions_available()) { 2986e585b24aSTony Krowiak if (ap_qci(&info) == 0) 2987e585b24aSTony Krowiak return info.apxa; 298845c9b47cSTony Krowiak } 298945c9b47cSTony Krowiak 299045c9b47cSTony Krowiak return 0; 299145c9b47cSTony Krowiak } 299245c9b47cSTony Krowiak 2993e585b24aSTony Krowiak /* 2994e585b24aSTony Krowiak * The format of the crypto control block (CRYCB) is specified in the 3 low 2995e585b24aSTony Krowiak * order bits of the CRYCB designation (CRYCBD) field as follows: 2996e585b24aSTony Krowiak * Format 0: Neither the message security assist extension 3 (MSAX3) nor the 2997e585b24aSTony Krowiak * AP extended addressing (APXA) facility are installed. 2998e585b24aSTony Krowiak * Format 1: The APXA facility is not installed but the MSAX3 facility is. 2999e585b24aSTony Krowiak * Format 2: Both the APXA and MSAX3 facilities are installed 3000e585b24aSTony Krowiak */ 300145c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm) 300245c9b47cSTony Krowiak { 300345c9b47cSTony Krowiak kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb; 300445c9b47cSTony Krowiak 3005e585b24aSTony Krowiak /* Clear the CRYCB format bits - i.e., set format 0 by default */ 3006e585b24aSTony Krowiak kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK); 3007e585b24aSTony Krowiak 3008e585b24aSTony Krowiak /* Check whether MSAX3 is installed */ 3009e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 3010e585b24aSTony Krowiak return; 3011e585b24aSTony Krowiak 301245c9b47cSTony Krowiak if (kvm_s390_apxa_installed()) 301345c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT2; 301445c9b47cSTony Krowiak else 301545c9b47cSTony Krowiak kvm->arch.crypto.crycbd |= CRYCB_FORMAT1; 301645c9b47cSTony Krowiak } 301745c9b47cSTony Krowiak 301886956e70STony Krowiak /* 301986956e70STony Krowiak * kvm_arch_crypto_set_masks 302086956e70STony Krowiak * 302186956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 302286956e70STony Krowiak * to be set. 302386956e70STony Krowiak * @apm: the mask identifying the accessible AP adapters 302486956e70STony Krowiak * @aqm: the mask identifying the accessible AP domains 302586956e70STony Krowiak * @adm: the mask identifying the accessible AP control domains 302686956e70STony Krowiak * 302786956e70STony Krowiak * Set the masks that identify the adapters, domains and control domains to 302886956e70STony Krowiak * which the KVM guest is granted access. 302986956e70STony Krowiak * 303086956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 303186956e70STony Krowiak * function. 303286956e70STony Krowiak */ 30330e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm, 30340e237e44SPierre Morel unsigned long *aqm, unsigned long *adm) 30350e237e44SPierre Morel { 30360e237e44SPierre Morel struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb; 30370e237e44SPierre Morel 30380e237e44SPierre Morel kvm_s390_vcpu_block_all(kvm); 30390e237e44SPierre Morel 30400e237e44SPierre Morel switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) { 30410e237e44SPierre Morel case CRYCB_FORMAT2: /* APCB1 use 256 bits */ 30420e237e44SPierre Morel memcpy(crycb->apcb1.apm, apm, 32); 30430e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx", 30440e237e44SPierre Morel apm[0], apm[1], apm[2], apm[3]); 30450e237e44SPierre Morel memcpy(crycb->apcb1.aqm, aqm, 32); 30460e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx", 30470e237e44SPierre Morel aqm[0], aqm[1], aqm[2], aqm[3]); 30480e237e44SPierre Morel memcpy(crycb->apcb1.adm, adm, 32); 30490e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx", 30500e237e44SPierre Morel adm[0], adm[1], adm[2], adm[3]); 30510e237e44SPierre Morel break; 30520e237e44SPierre Morel case CRYCB_FORMAT1: 30530e237e44SPierre Morel case CRYCB_FORMAT0: /* Fall through both use APCB0 */ 30540e237e44SPierre Morel memcpy(crycb->apcb0.apm, apm, 8); 30550e237e44SPierre Morel memcpy(crycb->apcb0.aqm, aqm, 2); 30560e237e44SPierre Morel memcpy(crycb->apcb0.adm, adm, 2); 30570e237e44SPierre Morel VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x", 30580e237e44SPierre Morel apm[0], *((unsigned short *)aqm), 30590e237e44SPierre Morel *((unsigned short *)adm)); 30600e237e44SPierre Morel break; 30610e237e44SPierre Morel default: /* Can not happen */ 30620e237e44SPierre Morel break; 30630e237e44SPierre Morel } 30640e237e44SPierre Morel 30650e237e44SPierre Morel /* recreate the shadow crycb for each vcpu */ 30660e237e44SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 30670e237e44SPierre Morel kvm_s390_vcpu_unblock_all(kvm); 30680e237e44SPierre Morel } 30690e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks); 30700e237e44SPierre Morel 307186956e70STony Krowiak /* 307286956e70STony Krowiak * kvm_arch_crypto_clear_masks 307386956e70STony Krowiak * 307486956e70STony Krowiak * @kvm: pointer to the target guest's KVM struct containing the crypto masks 307586956e70STony Krowiak * to be cleared. 307686956e70STony Krowiak * 307786956e70STony Krowiak * Clear the masks that identify the adapters, domains and control domains to 307886956e70STony Krowiak * which the KVM guest is granted access. 307986956e70STony Krowiak * 308086956e70STony Krowiak * Note: The kvm->lock mutex must be locked by the caller before invoking this 308186956e70STony Krowiak * function. 308286956e70STony Krowiak */ 308342104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm) 308442104598STony Krowiak { 308542104598STony Krowiak kvm_s390_vcpu_block_all(kvm); 308642104598STony Krowiak 308742104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb0, 0, 308842104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb0)); 308942104598STony Krowiak memset(&kvm->arch.crypto.crycb->apcb1, 0, 309042104598STony Krowiak sizeof(kvm->arch.crypto.crycb->apcb1)); 309142104598STony Krowiak 30920e237e44SPierre Morel VM_EVENT(kvm, 3, "%s", "CLR CRYCB:"); 30936cc571b1SPierre Morel /* recreate the shadow crycb for each vcpu */ 30946cc571b1SPierre Morel kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART); 309542104598STony Krowiak kvm_s390_vcpu_unblock_all(kvm); 309642104598STony Krowiak } 309742104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks); 309842104598STony Krowiak 30999bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void) 31009d8d5786SMichael Mueller { 31019bb0ec09SDavid Hildenbrand struct cpuid cpuid; 31029bb0ec09SDavid Hildenbrand 31039bb0ec09SDavid Hildenbrand get_cpu_id(&cpuid); 31049bb0ec09SDavid Hildenbrand cpuid.version = 0xff; 31059bb0ec09SDavid Hildenbrand return *((u64 *) &cpuid); 31069d8d5786SMichael Mueller } 31079d8d5786SMichael Mueller 3108c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm) 31095102ee87STony Krowiak { 3110c54f0d6aSDavid Hildenbrand kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb; 311145c9b47cSTony Krowiak kvm_s390_set_crycb_format(kvm); 31121e753732STony Krowiak init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem); 31135102ee87STony Krowiak 3114e585b24aSTony Krowiak if (!test_kvm_facility(kvm, 76)) 3115e585b24aSTony Krowiak return; 3116e585b24aSTony Krowiak 3117ed6f76b4STony Krowiak /* Enable AES/DEA protected key functions by default */ 3118ed6f76b4STony Krowiak kvm->arch.crypto.aes_kw = 1; 3119ed6f76b4STony Krowiak kvm->arch.crypto.dea_kw = 1; 3120ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 3121ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask)); 3122ed6f76b4STony Krowiak get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 3123ed6f76b4STony Krowiak sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask)); 31245102ee87STony Krowiak } 31255102ee87STony Krowiak 31267d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm) 31277d43bafcSEugene (jno) Dvurechenski { 31287d43bafcSEugene (jno) Dvurechenski if (kvm->arch.use_esca) 31295e044315SEugene (jno) Dvurechenski free_pages_exact(kvm->arch.sca, sizeof(struct esca_block)); 31307d43bafcSEugene (jno) Dvurechenski else 31317d43bafcSEugene (jno) Dvurechenski free_page((unsigned long)(kvm->arch.sca)); 31327d43bafcSEugene (jno) Dvurechenski kvm->arch.sca = NULL; 31337d43bafcSEugene (jno) Dvurechenski } 31347d43bafcSEugene (jno) Dvurechenski 313509340b2fSMatthew Rosato void kvm_arch_free_vm(struct kvm *kvm) 313609340b2fSMatthew Rosato { 313709340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) 313809340b2fSMatthew Rosato kvm_s390_pci_clear_list(kvm); 313909340b2fSMatthew Rosato 314009340b2fSMatthew Rosato __kvm_arch_free_vm(kvm); 314109340b2fSMatthew Rosato } 314209340b2fSMatthew Rosato 3143e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 3144b0c632dbSHeiko Carstens { 3145c4196218SChristian Borntraeger gfp_t alloc_flags = GFP_KERNEL_ACCOUNT; 31469d8d5786SMichael Mueller int i, rc; 3147b0c632dbSHeiko Carstens char debug_name[16]; 3148f6c137ffSChristian Borntraeger static unsigned long sca_offset; 3149b0c632dbSHeiko Carstens 3150e08b9637SCarsten Otte rc = -EINVAL; 3151e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 3152e08b9637SCarsten Otte if (type & ~KVM_VM_S390_UCONTROL) 3153e08b9637SCarsten Otte goto out_err; 3154e08b9637SCarsten Otte if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN))) 3155e08b9637SCarsten Otte goto out_err; 3156e08b9637SCarsten Otte #else 3157e08b9637SCarsten Otte if (type) 3158e08b9637SCarsten Otte goto out_err; 3159e08b9637SCarsten Otte #endif 3160e08b9637SCarsten Otte 3161b0c632dbSHeiko Carstens rc = s390_enable_sie(); 3162b0c632dbSHeiko Carstens if (rc) 3163d89f5effSJan Kiszka goto out_err; 3164b0c632dbSHeiko Carstens 3165b290411aSCarsten Otte rc = -ENOMEM; 3166b290411aSCarsten Otte 316776a6dd72SDavid Hildenbrand if (!sclp.has_64bscao) 316876a6dd72SDavid Hildenbrand alloc_flags |= GFP_DMA; 31695e044315SEugene (jno) Dvurechenski rwlock_init(&kvm->arch.sca_lock); 31709ac96d75SDavid Hildenbrand /* start with basic SCA */ 317176a6dd72SDavid Hildenbrand kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags); 3172b0c632dbSHeiko Carstens if (!kvm->arch.sca) 3173d89f5effSJan Kiszka goto out_err; 31740d9ce162SJunaid Shahid mutex_lock(&kvm_lock); 3175c5c2c393SDavid Hildenbrand sca_offset += 16; 3176bc784cceSEugene (jno) Dvurechenski if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE) 3177c5c2c393SDavid Hildenbrand sca_offset = 0; 3178bc784cceSEugene (jno) Dvurechenski kvm->arch.sca = (struct bsca_block *) 3179bc784cceSEugene (jno) Dvurechenski ((char *) kvm->arch.sca + sca_offset); 31800d9ce162SJunaid Shahid mutex_unlock(&kvm_lock); 3181b0c632dbSHeiko Carstens 3182b0c632dbSHeiko Carstens sprintf(debug_name, "kvm-%u", current->pid); 3183b0c632dbSHeiko Carstens 31841cb9cf72SChristian Borntraeger kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long)); 3185b0c632dbSHeiko Carstens if (!kvm->arch.dbf) 318640f5b735SDominik Dingel goto out_err; 3187b0c632dbSHeiko Carstens 318819114bebSMichael Mueller BUILD_BUG_ON(sizeof(struct sie_page2) != 4096); 3189c54f0d6aSDavid Hildenbrand kvm->arch.sie_page2 = 3190c4196218SChristian Borntraeger (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA); 3191c54f0d6aSDavid Hildenbrand if (!kvm->arch.sie_page2) 319240f5b735SDominik Dingel goto out_err; 31939d8d5786SMichael Mueller 319425c84dbaSMichael Mueller kvm->arch.sie_page2->kvm = kvm; 3195c54f0d6aSDavid Hildenbrand kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list; 3196c3b9e3e1SChristian Borntraeger 3197c3b9e3e1SChristian Borntraeger for (i = 0; i < kvm_s390_fac_size(); i++) { 319817e89e13SSven Schnelle kvm->arch.model.fac_mask[i] = stfle_fac_list[i] & 3199c3b9e3e1SChristian Borntraeger (kvm_s390_fac_base[i] | 3200c3b9e3e1SChristian Borntraeger kvm_s390_fac_ext[i]); 320117e89e13SSven Schnelle kvm->arch.model.fac_list[i] = stfle_fac_list[i] & 3202c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i]; 3203c3b9e3e1SChristian Borntraeger } 3204346fa2f8SChristian Borntraeger kvm->arch.model.subfuncs = kvm_s390_available_subfunc; 3205981467c9SMichael Mueller 32061935222dSDavid Hildenbrand /* we are always in czam mode - even on pre z14 machines */ 32071935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_mask, 138); 32081935222dSDavid Hildenbrand set_kvm_facility(kvm->arch.model.fac_list, 138); 32091935222dSDavid Hildenbrand /* we emulate STHYI in kvm */ 321095ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_mask, 74); 321195ca2cb5SJanosch Frank set_kvm_facility(kvm->arch.model.fac_list, 74); 32121bab1c02SClaudio Imbrenda if (MACHINE_HAS_TLB_GUEST) { 32131bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_mask, 147); 32141bab1c02SClaudio Imbrenda set_kvm_facility(kvm->arch.model.fac_list, 147); 32151bab1c02SClaudio Imbrenda } 321695ca2cb5SJanosch Frank 321705f31e3bSPierre Morel if (css_general_characteristics.aiv && test_facility(65)) 321805f31e3bSPierre Morel set_kvm_facility(kvm->arch.model.fac_mask, 65); 321905f31e3bSPierre Morel 32209bb0ec09SDavid Hildenbrand kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid(); 322137c5f6c8SDavid Hildenbrand kvm->arch.model.ibc = sclp.ibc & 0x0fff; 32229d8d5786SMichael Mueller 3223c54f0d6aSDavid Hildenbrand kvm_s390_crypto_init(kvm); 32245102ee87STony Krowiak 322509340b2fSMatthew Rosato if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) { 322609340b2fSMatthew Rosato mutex_lock(&kvm->lock); 322709340b2fSMatthew Rosato kvm_s390_pci_init_list(kvm); 322809340b2fSMatthew Rosato kvm_s390_vcpu_pci_enable_interp(kvm); 322909340b2fSMatthew Rosato mutex_unlock(&kvm->lock); 323009340b2fSMatthew Rosato } 323109340b2fSMatthew Rosato 323251978393SFei Li mutex_init(&kvm->arch.float_int.ais_lock); 3233ba5c1e9bSCarsten Otte spin_lock_init(&kvm->arch.float_int.lock); 32346d3da241SJens Freimann for (i = 0; i < FIRQ_LIST_COUNT; i++) 32356d3da241SJens Freimann INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]); 32368a242234SHeiko Carstens init_waitqueue_head(&kvm->arch.ipte_wq); 3237a6b7e459SThomas Huth mutex_init(&kvm->arch.ipte_mutex); 3238ba5c1e9bSCarsten Otte 3239b0c632dbSHeiko Carstens debug_register_view(kvm->arch.dbf, &debug_sprintf_view); 324078f26131SChristian Borntraeger VM_EVENT(kvm, 3, "vm created with type %lu", type); 3241b0c632dbSHeiko Carstens 3242e08b9637SCarsten Otte if (type & KVM_VM_S390_UCONTROL) { 3243e08b9637SCarsten Otte kvm->arch.gmap = NULL; 3244a3a92c31SDominik Dingel kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT; 3245e08b9637SCarsten Otte } else { 324632e6b236SGuenther Hutzl if (sclp.hamax == U64_MAX) 3247ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = TASK_SIZE_MAX; 324832e6b236SGuenther Hutzl else 3249ee71d16dSMartin Schwidefsky kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX, 325032e6b236SGuenther Hutzl sclp.hamax + 1); 32516ea427bbSMartin Schwidefsky kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1); 3252598841caSCarsten Otte if (!kvm->arch.gmap) 325340f5b735SDominik Dingel goto out_err; 32542c70fe44SChristian Borntraeger kvm->arch.gmap->private = kvm; 325524eb3a82SDominik Dingel kvm->arch.gmap->pfault_enabled = 0; 3256e08b9637SCarsten Otte } 3257fa6b7fe9SCornelia Huck 3258c9f0a2b8SJanosch Frank kvm->arch.use_pfmfi = sclp.has_pfmfi; 325955531b74SJanosch Frank kvm->arch.use_skf = sclp.has_skey; 32608ad35755SDavid Hildenbrand spin_lock_init(&kvm->arch.start_stop_lock); 3261a3508fbeSDavid Hildenbrand kvm_s390_vsie_init(kvm); 3262cc674ef2SMichael Mueller if (use_gisa) 3263d7c5cb01SMichael Mueller kvm_s390_gisa_init(kvm); 3264*fb491d55SClaudio Imbrenda INIT_LIST_HEAD(&kvm->arch.pv.need_cleanup); 3265*fb491d55SClaudio Imbrenda kvm->arch.pv.set_aside = NULL; 32668335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid); 32678ad35755SDavid Hildenbrand 3268d89f5effSJan Kiszka return 0; 3269d89f5effSJan Kiszka out_err: 3270c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 327140f5b735SDominik Dingel debug_unregister(kvm->arch.dbf); 32727d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 327378f26131SChristian Borntraeger KVM_EVENT(3, "creation of vm failed: %d", rc); 3274d89f5effSJan Kiszka return rc; 3275b0c632dbSHeiko Carstens } 3276b0c632dbSHeiko Carstens 3277d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 3278d329c035SChristian Borntraeger { 327929b40f10SJanosch Frank u16 rc, rrc; 328029b40f10SJanosch Frank 3281d329c035SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "free cpu"); 3282ade38c31SCornelia Huck trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id); 328367335e63SChristian Borntraeger kvm_s390_clear_local_irqs(vcpu); 32843c038e6bSDominik Dingel kvm_clear_async_pf_completion_queue(vcpu); 3285bc784cceSEugene (jno) Dvurechenski if (!kvm_is_ucontrol(vcpu->kvm)) 3286a6e2f683SEugene (jno) Dvurechenski sca_del_vcpu(vcpu); 328724fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1); 328827e0393fSCarsten Otte 328927e0393fSCarsten Otte if (kvm_is_ucontrol(vcpu->kvm)) 32906ea427bbSMartin Schwidefsky gmap_remove(vcpu->arch.gmap); 329127e0393fSCarsten Otte 3292e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) 3293b31605c1SDominik Dingel kvm_s390_vcpu_unsetup_cmma(vcpu); 329429b40f10SJanosch Frank /* We can not hold the vcpu mutex here, we are already dying */ 329529b40f10SJanosch Frank if (kvm_s390_pv_cpu_get_handle(vcpu)) 329629b40f10SJanosch Frank kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc); 3297d329c035SChristian Borntraeger free_page((unsigned long)(vcpu->arch.sie_block)); 3298d329c035SChristian Borntraeger } 3299d329c035SChristian Borntraeger 3300b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm) 3301b0c632dbSHeiko Carstens { 330229b40f10SJanosch Frank u16 rc, rrc; 330329b40f10SJanosch Frank 330427592ae8SMarc Zyngier kvm_destroy_vcpus(kvm); 33057d43bafcSEugene (jno) Dvurechenski sca_dispose(kvm); 3306d7c5cb01SMichael Mueller kvm_s390_gisa_destroy(kvm); 330729b40f10SJanosch Frank /* 330829b40f10SJanosch Frank * We are already at the end of life and kvm->lock is not taken. 330929b40f10SJanosch Frank * This is ok as the file descriptor is closed by now and nobody 3310*fb491d55SClaudio Imbrenda * can mess with the pv state. 331129b40f10SJanosch Frank */ 3312*fb491d55SClaudio Imbrenda kvm_s390_pv_deinit_cleanup_all(kvm, &rc, &rrc); 3313ca2fd060SClaudio Imbrenda /* 3314ca2fd060SClaudio Imbrenda * Remove the mmu notifier only when the whole KVM VM is torn down, 3315ca2fd060SClaudio Imbrenda * and only if one was registered to begin with. If the VM is 3316ca2fd060SClaudio Imbrenda * currently not protected, but has been previously been protected, 3317ca2fd060SClaudio Imbrenda * then it's possible that the notifier is still registered. 3318ca2fd060SClaudio Imbrenda */ 3319ca2fd060SClaudio Imbrenda if (kvm->arch.pv.mmu_notifier.ops) 3320ca2fd060SClaudio Imbrenda mmu_notifier_unregister(&kvm->arch.pv.mmu_notifier, kvm->mm); 3321ca2fd060SClaudio Imbrenda 332229b40f10SJanosch Frank debug_unregister(kvm->arch.dbf); 3323c54f0d6aSDavid Hildenbrand free_page((unsigned long)kvm->arch.sie_page2); 332427e0393fSCarsten Otte if (!kvm_is_ucontrol(kvm)) 33256ea427bbSMartin Schwidefsky gmap_remove(kvm->arch.gmap); 3326841b91c5SCornelia Huck kvm_s390_destroy_adapters(kvm); 332767335e63SChristian Borntraeger kvm_s390_clear_float_irqs(kvm); 3328a3508fbeSDavid Hildenbrand kvm_s390_vsie_destroy(kvm); 33298335713aSChristian Borntraeger KVM_EVENT(3, "vm 0x%pK destroyed", kvm); 3330b0c632dbSHeiko Carstens } 3331b0c632dbSHeiko Carstens 3332b0c632dbSHeiko Carstens /* Section: vcpu related */ 3333dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu) 3334b0c632dbSHeiko Carstens { 33356ea427bbSMartin Schwidefsky vcpu->arch.gmap = gmap_create(current->mm, -1UL); 333627e0393fSCarsten Otte if (!vcpu->arch.gmap) 333727e0393fSCarsten Otte return -ENOMEM; 33382c70fe44SChristian Borntraeger vcpu->arch.gmap->private = vcpu->kvm; 3339dafd032aSDominik Dingel 334027e0393fSCarsten Otte return 0; 334127e0393fSCarsten Otte } 334227e0393fSCarsten Otte 3343a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu) 3344a6e2f683SEugene (jno) Dvurechenski { 3345a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) 3346a6940674SDavid Hildenbrand return; 33475e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 33487d43bafcSEugene (jno) Dvurechenski if (vcpu->kvm->arch.use_esca) { 33497d43bafcSEugene (jno) Dvurechenski struct esca_block *sca = vcpu->kvm->arch.sca; 33507d43bafcSEugene (jno) Dvurechenski 33517d43bafcSEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 33527d43bafcSEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 33537d43bafcSEugene (jno) Dvurechenski } else { 3354bc784cceSEugene (jno) Dvurechenski struct bsca_block *sca = vcpu->kvm->arch.sca; 3355a6e2f683SEugene (jno) Dvurechenski 3356a6e2f683SEugene (jno) Dvurechenski clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 3357a6e2f683SEugene (jno) Dvurechenski sca->cpu[vcpu->vcpu_id].sda = 0; 3358a6e2f683SEugene (jno) Dvurechenski } 33595e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 33607d43bafcSEugene (jno) Dvurechenski } 3361a6e2f683SEugene (jno) Dvurechenski 3362eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu) 3363a6e2f683SEugene (jno) Dvurechenski { 3364a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 3365fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(vcpu->kvm->arch.sca); 3366a6940674SDavid Hildenbrand 3367a6940674SDavid Hildenbrand /* we still need the basic sca for the ipte control */ 3368fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3369fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys; 3370f07afa04SDavid Hildenbrand return; 3371a6940674SDavid Hildenbrand } 3372eaa78f34SDavid Hildenbrand read_lock(&vcpu->kvm->arch.sca_lock); 3373eaa78f34SDavid Hildenbrand if (vcpu->kvm->arch.use_esca) { 3374eaa78f34SDavid Hildenbrand struct esca_block *sca = vcpu->kvm->arch.sca; 3375fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca); 33767d43bafcSEugene (jno) Dvurechenski 3377fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block); 3378fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3379fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys & ESCA_SCAOL_MASK; 33800c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 3381eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn); 33827d43bafcSEugene (jno) Dvurechenski } else { 3383eaa78f34SDavid Hildenbrand struct bsca_block *sca = vcpu->kvm->arch.sca; 3384fe0ef003SNico Boehr phys_addr_t sca_phys = virt_to_phys(sca); 3385a6e2f683SEugene (jno) Dvurechenski 3386fe0ef003SNico Boehr sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block); 3387fe0ef003SNico Boehr vcpu->arch.sie_block->scaoh = sca_phys >> 32; 3388fe0ef003SNico Boehr vcpu->arch.sie_block->scaol = sca_phys; 3389eaa78f34SDavid Hildenbrand set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn); 3390a6e2f683SEugene (jno) Dvurechenski } 3391eaa78f34SDavid Hildenbrand read_unlock(&vcpu->kvm->arch.sca_lock); 33925e044315SEugene (jno) Dvurechenski } 33935e044315SEugene (jno) Dvurechenski 33945e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */ 33955e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s) 33965e044315SEugene (jno) Dvurechenski { 33975e044315SEugene (jno) Dvurechenski d->sda = s->sda; 33985e044315SEugene (jno) Dvurechenski d->sigp_ctrl.c = s->sigp_ctrl.c; 33995e044315SEugene (jno) Dvurechenski d->sigp_ctrl.scn = s->sigp_ctrl.scn; 34005e044315SEugene (jno) Dvurechenski } 34015e044315SEugene (jno) Dvurechenski 34025e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s) 34035e044315SEugene (jno) Dvurechenski { 34045e044315SEugene (jno) Dvurechenski int i; 34055e044315SEugene (jno) Dvurechenski 34065e044315SEugene (jno) Dvurechenski d->ipte_control = s->ipte_control; 34075e044315SEugene (jno) Dvurechenski d->mcn[0] = s->mcn; 34085e044315SEugene (jno) Dvurechenski for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++) 34095e044315SEugene (jno) Dvurechenski sca_copy_entry(&d->cpu[i], &s->cpu[i]); 34105e044315SEugene (jno) Dvurechenski } 34115e044315SEugene (jno) Dvurechenski 34125e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm) 34135e044315SEugene (jno) Dvurechenski { 34145e044315SEugene (jno) Dvurechenski struct bsca_block *old_sca = kvm->arch.sca; 34155e044315SEugene (jno) Dvurechenski struct esca_block *new_sca; 34165e044315SEugene (jno) Dvurechenski struct kvm_vcpu *vcpu; 341746808a4cSMarc Zyngier unsigned long vcpu_idx; 34185e044315SEugene (jno) Dvurechenski u32 scaol, scaoh; 3419fe0ef003SNico Boehr phys_addr_t new_sca_phys; 34205e044315SEugene (jno) Dvurechenski 342129b40f10SJanosch Frank if (kvm->arch.use_esca) 342229b40f10SJanosch Frank return 0; 342329b40f10SJanosch Frank 3424c4196218SChristian Borntraeger new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO); 34255e044315SEugene (jno) Dvurechenski if (!new_sca) 34265e044315SEugene (jno) Dvurechenski return -ENOMEM; 34275e044315SEugene (jno) Dvurechenski 3428fe0ef003SNico Boehr new_sca_phys = virt_to_phys(new_sca); 3429fe0ef003SNico Boehr scaoh = new_sca_phys >> 32; 3430fe0ef003SNico Boehr scaol = new_sca_phys & ESCA_SCAOL_MASK; 34315e044315SEugene (jno) Dvurechenski 34325e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_block_all(kvm); 34335e044315SEugene (jno) Dvurechenski write_lock(&kvm->arch.sca_lock); 34345e044315SEugene (jno) Dvurechenski 34355e044315SEugene (jno) Dvurechenski sca_copy_b_to_e(new_sca, old_sca); 34365e044315SEugene (jno) Dvurechenski 34375e044315SEugene (jno) Dvurechenski kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) { 34385e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaoh = scaoh; 34395e044315SEugene (jno) Dvurechenski vcpu->arch.sie_block->scaol = scaol; 34400c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_ESCA; 34415e044315SEugene (jno) Dvurechenski } 34425e044315SEugene (jno) Dvurechenski kvm->arch.sca = new_sca; 34435e044315SEugene (jno) Dvurechenski kvm->arch.use_esca = 1; 34445e044315SEugene (jno) Dvurechenski 34455e044315SEugene (jno) Dvurechenski write_unlock(&kvm->arch.sca_lock); 34465e044315SEugene (jno) Dvurechenski kvm_s390_vcpu_unblock_all(kvm); 34475e044315SEugene (jno) Dvurechenski 34485e044315SEugene (jno) Dvurechenski free_page((unsigned long)old_sca); 34495e044315SEugene (jno) Dvurechenski 34508335713aSChristian Borntraeger VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)", 34518335713aSChristian Borntraeger old_sca, kvm->arch.sca); 34525e044315SEugene (jno) Dvurechenski return 0; 34537d43bafcSEugene (jno) Dvurechenski } 3454a6e2f683SEugene (jno) Dvurechenski 3455a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id) 3456a6e2f683SEugene (jno) Dvurechenski { 34575e044315SEugene (jno) Dvurechenski int rc; 34585e044315SEugene (jno) Dvurechenski 3459a6940674SDavid Hildenbrand if (!kvm_s390_use_sca_entries()) { 3460a6940674SDavid Hildenbrand if (id < KVM_MAX_VCPUS) 3461a6940674SDavid Hildenbrand return true; 3462a6940674SDavid Hildenbrand return false; 3463a6940674SDavid Hildenbrand } 34645e044315SEugene (jno) Dvurechenski if (id < KVM_S390_BSCA_CPU_SLOTS) 34655e044315SEugene (jno) Dvurechenski return true; 346676a6dd72SDavid Hildenbrand if (!sclp.has_esca || !sclp.has_64bscao) 34675e044315SEugene (jno) Dvurechenski return false; 34685e044315SEugene (jno) Dvurechenski 34695e044315SEugene (jno) Dvurechenski rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm); 34705e044315SEugene (jno) Dvurechenski 34715e044315SEugene (jno) Dvurechenski return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS; 3472a6e2f683SEugene (jno) Dvurechenski } 3473a6e2f683SEugene (jno) Dvurechenski 3474db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3475db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3476db0758b2SDavid Hildenbrand { 3477db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start != 0); 34789c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3479db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 34809c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3481db0758b2SDavid Hildenbrand } 3482db0758b2SDavid Hildenbrand 3483db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3484db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3485db0758b2SDavid Hildenbrand { 3486db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_start == 0); 34879c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3488db0758b2SDavid Hildenbrand vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start; 3489db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = 0; 34909c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3491db0758b2SDavid Hildenbrand } 3492db0758b2SDavid Hildenbrand 3493db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3494db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3495db0758b2SDavid Hildenbrand { 3496db0758b2SDavid Hildenbrand WARN_ON_ONCE(vcpu->arch.cputm_enabled); 3497db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = true; 3498db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 3499db0758b2SDavid Hildenbrand } 3500db0758b2SDavid Hildenbrand 3501db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */ 3502db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3503db0758b2SDavid Hildenbrand { 3504db0758b2SDavid Hildenbrand WARN_ON_ONCE(!vcpu->arch.cputm_enabled); 3505db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 3506db0758b2SDavid Hildenbrand vcpu->arch.cputm_enabled = false; 3507db0758b2SDavid Hildenbrand } 3508db0758b2SDavid Hildenbrand 3509db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3510db0758b2SDavid Hildenbrand { 3511db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3512db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 3513db0758b2SDavid Hildenbrand preempt_enable(); 3514db0758b2SDavid Hildenbrand } 3515db0758b2SDavid Hildenbrand 3516db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu) 3517db0758b2SDavid Hildenbrand { 3518db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 3519db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 3520db0758b2SDavid Hildenbrand preempt_enable(); 3521db0758b2SDavid Hildenbrand } 3522db0758b2SDavid Hildenbrand 35234287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */ 35244287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm) 35254287f247SDavid Hildenbrand { 3526db0758b2SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 35279c23a131SDavid Hildenbrand raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount); 3528db0758b2SDavid Hildenbrand if (vcpu->arch.cputm_enabled) 3529db0758b2SDavid Hildenbrand vcpu->arch.cputm_start = get_tod_clock_fast(); 35304287f247SDavid Hildenbrand vcpu->arch.sie_block->cputm = cputm; 35319c23a131SDavid Hildenbrand raw_write_seqcount_end(&vcpu->arch.cputm_seqcount); 3532db0758b2SDavid Hildenbrand preempt_enable(); 35334287f247SDavid Hildenbrand } 35344287f247SDavid Hildenbrand 3535db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */ 35364287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu) 35374287f247SDavid Hildenbrand { 35389c23a131SDavid Hildenbrand unsigned int seq; 3539db0758b2SDavid Hildenbrand __u64 value; 3540db0758b2SDavid Hildenbrand 3541db0758b2SDavid Hildenbrand if (unlikely(!vcpu->arch.cputm_enabled)) 35424287f247SDavid Hildenbrand return vcpu->arch.sie_block->cputm; 3543db0758b2SDavid Hildenbrand 35449c23a131SDavid Hildenbrand preempt_disable(); /* protect from TOD sync and vcpu_load/put */ 35459c23a131SDavid Hildenbrand do { 35469c23a131SDavid Hildenbrand seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount); 35479c23a131SDavid Hildenbrand /* 35489c23a131SDavid Hildenbrand * If the writer would ever execute a read in the critical 35499c23a131SDavid Hildenbrand * section, e.g. in irq context, we have a deadlock. 35509c23a131SDavid Hildenbrand */ 35519c23a131SDavid Hildenbrand WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu); 3552db0758b2SDavid Hildenbrand value = vcpu->arch.sie_block->cputm; 35539c23a131SDavid Hildenbrand /* if cputm_start is 0, accounting is being started/stopped */ 35549c23a131SDavid Hildenbrand if (likely(vcpu->arch.cputm_start)) 3555db0758b2SDavid Hildenbrand value -= get_tod_clock_fast() - vcpu->arch.cputm_start; 35569c23a131SDavid Hildenbrand } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1)); 35579c23a131SDavid Hildenbrand preempt_enable(); 3558db0758b2SDavid Hildenbrand return value; 35594287f247SDavid Hildenbrand } 35604287f247SDavid Hildenbrand 3561b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 3562b0c632dbSHeiko Carstens { 35639977e886SHendrik Brueckner 356437d9df98SDavid Hildenbrand gmap_enable(vcpu->arch.enabled_gmap); 3565ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING); 35665ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3567db0758b2SDavid Hildenbrand __start_cpu_timer_accounting(vcpu); 356801a745acSDavid Hildenbrand vcpu->cpu = cpu; 3569b0c632dbSHeiko Carstens } 3570b0c632dbSHeiko Carstens 3571b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 3572b0c632dbSHeiko Carstens { 357301a745acSDavid Hildenbrand vcpu->cpu = -1; 35745ebda316SDavid Hildenbrand if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu)) 3575db0758b2SDavid Hildenbrand __stop_cpu_timer_accounting(vcpu); 35769daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING); 357737d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = gmap_get_enabled(); 357837d9df98SDavid Hildenbrand gmap_disable(vcpu->arch.enabled_gmap); 35799977e886SHendrik Brueckner 3580b0c632dbSHeiko Carstens } 3581b0c632dbSHeiko Carstens 358231928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 358342897d86SMarcelo Tosatti { 358472f25020SJason J. Herne mutex_lock(&vcpu->kvm->lock); 3585fdf03650SFan Zhang preempt_disable(); 358672f25020SJason J. Herne vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch; 3587d16b52cbSDavid Hildenbrand vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx; 3588fdf03650SFan Zhang preempt_enable(); 358972f25020SJason J. Herne mutex_unlock(&vcpu->kvm->lock); 359025508824SDavid Hildenbrand if (!kvm_is_ucontrol(vcpu->kvm)) { 3591dafd032aSDominik Dingel vcpu->arch.gmap = vcpu->kvm->arch.gmap; 3592eaa78f34SDavid Hildenbrand sca_add_vcpu(vcpu); 359325508824SDavid Hildenbrand } 35946502a34cSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0) 35956502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 359637d9df98SDavid Hildenbrand /* make vcpu_load load the right gmap on the first trigger */ 359737d9df98SDavid Hildenbrand vcpu->arch.enabled_gmap = vcpu->arch.gmap; 359842897d86SMarcelo Tosatti } 359942897d86SMarcelo Tosatti 36008ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr) 36018ec2fa52SChristian Borntraeger { 36028ec2fa52SChristian Borntraeger if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) && 36038ec2fa52SChristian Borntraeger test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo)) 36048ec2fa52SChristian Borntraeger return true; 36058ec2fa52SChristian Borntraeger return false; 36068ec2fa52SChristian Borntraeger } 36078ec2fa52SChristian Borntraeger 36088ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm) 36098ec2fa52SChristian Borntraeger { 36108ec2fa52SChristian Borntraeger /* At least one ECC subfunction must be present */ 36118ec2fa52SChristian Borntraeger return kvm_has_pckmo_subfunc(kvm, 32) || 36128ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 33) || 36138ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 34) || 36148ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 40) || 36158ec2fa52SChristian Borntraeger kvm_has_pckmo_subfunc(kvm, 41); 36168ec2fa52SChristian Borntraeger 36178ec2fa52SChristian Borntraeger } 36188ec2fa52SChristian Borntraeger 36195102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu) 36205102ee87STony Krowiak { 3621e585b24aSTony Krowiak /* 3622e585b24aSTony Krowiak * If the AP instructions are not being interpreted and the MSAX3 3623e585b24aSTony Krowiak * facility is not configured for the guest, there is nothing to set up. 3624e585b24aSTony Krowiak */ 3625e585b24aSTony Krowiak if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76)) 36265102ee87STony Krowiak return; 36275102ee87STony Krowiak 3628e585b24aSTony Krowiak vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd; 3629a374e892STony Krowiak vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA); 363037940fb0STony Krowiak vcpu->arch.sie_block->eca &= ~ECA_APIE; 36318ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd &= ~ECD_ECC; 3632a374e892STony Krowiak 3633e585b24aSTony Krowiak if (vcpu->kvm->arch.crypto.apie) 3634e585b24aSTony Krowiak vcpu->arch.sie_block->eca |= ECA_APIE; 3635e585b24aSTony Krowiak 3636e585b24aSTony Krowiak /* Set up protected key support */ 36378ec2fa52SChristian Borntraeger if (vcpu->kvm->arch.crypto.aes_kw) { 3638a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_AES; 36398ec2fa52SChristian Borntraeger /* ecc is also wrapped with AES key */ 36408ec2fa52SChristian Borntraeger if (kvm_has_pckmo_ecc(vcpu->kvm)) 36418ec2fa52SChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ECC; 36428ec2fa52SChristian Borntraeger } 36438ec2fa52SChristian Borntraeger 3644a374e892STony Krowiak if (vcpu->kvm->arch.crypto.dea_kw) 3645a374e892STony Krowiak vcpu->arch.sie_block->ecb3 |= ECB3_DEA; 36465102ee87STony Krowiak } 36475102ee87STony Krowiak 3648b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu) 3649b31605c1SDominik Dingel { 3650fe0ef003SNico Boehr free_page((unsigned long)phys_to_virt(vcpu->arch.sie_block->cbrlo)); 3651b31605c1SDominik Dingel vcpu->arch.sie_block->cbrlo = 0; 3652b31605c1SDominik Dingel } 3653b31605c1SDominik Dingel 3654b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu) 3655b31605c1SDominik Dingel { 3656fe0ef003SNico Boehr void *cbrlo_page = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); 3657fe0ef003SNico Boehr 3658fe0ef003SNico Boehr if (!cbrlo_page) 3659b31605c1SDominik Dingel return -ENOMEM; 3660fe0ef003SNico Boehr 3661fe0ef003SNico Boehr vcpu->arch.sie_block->cbrlo = virt_to_phys(cbrlo_page); 3662b31605c1SDominik Dingel return 0; 3663b31605c1SDominik Dingel } 3664b31605c1SDominik Dingel 366591520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu) 366691520f1aSMichael Mueller { 366791520f1aSMichael Mueller struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model; 366891520f1aSMichael Mueller 366991520f1aSMichael Mueller vcpu->arch.sie_block->ibc = model->ibc; 367080bc79dcSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 7)) 3671fe0ef003SNico Boehr vcpu->arch.sie_block->fac = virt_to_phys(model->fac_list); 367291520f1aSMichael Mueller } 367391520f1aSMichael Mueller 3674ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu) 3675ff72bb55SSean Christopherson { 3676b31605c1SDominik Dingel int rc = 0; 367729b40f10SJanosch Frank u16 uvrc, uvrrc; 3678b31288faSKonstantin Weitz 36799e6dabefSCornelia Huck atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | 36809e6dabefSCornelia Huck CPUSTAT_SM | 3681a4a4f191SGuenther Hutzl CPUSTAT_STOPPED); 3682a4a4f191SGuenther Hutzl 368353df84f8SGuenther Hutzl if (test_kvm_facility(vcpu->kvm, 78)) 3684ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2); 368553df84f8SGuenther Hutzl else if (test_kvm_facility(vcpu->kvm, 8)) 3686ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED); 3687a4a4f191SGuenther Hutzl 368891520f1aSMichael Mueller kvm_s390_vcpu_setup_model(vcpu); 368991520f1aSMichael Mueller 3690bdab09f3SDavid Hildenbrand /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */ 3691bdab09f3SDavid Hildenbrand if (MACHINE_HAS_ESOP) 36920c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT; 3693bd50e8ecSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 9)) 36940c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_SRSI; 369524fe0195SPierre Morel if (test_kvm_facility(vcpu->kvm, 11)) 369624fe0195SPierre Morel vcpu->arch.sie_block->ecb |= ECB_PTF; 3697f597d24eSDavid Hildenbrand if (test_kvm_facility(vcpu->kvm, 73)) 36980c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb |= ECB_TE; 36997119decfSJanis Schoetterl-Glausch if (!kvm_is_ucontrol(vcpu->kvm)) 37007119decfSJanis Schoetterl-Glausch vcpu->arch.sie_block->ecb |= ECB_SPECI; 37017feb6bb8SMichael Mueller 3702c9f0a2b8SJanosch Frank if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi) 37030c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI; 3704cd1836f5SJanosch Frank if (test_kvm_facility(vcpu->kvm, 130)) 37050c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb2 |= ECB2_IEP; 37060c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI; 370748ee7d3aSDavid Hildenbrand if (sclp.has_cei) 37080c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_CEI; 370911ad65b7SDavid Hildenbrand if (sclp.has_ib) 37100c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_IB; 371137c5f6c8SDavid Hildenbrand if (sclp.has_siif) 37120c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SII; 371337c5f6c8SDavid Hildenbrand if (sclp.has_sigpif) 37140c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_SIGPI; 371518280d8bSMichael Mueller if (test_kvm_facility(vcpu->kvm, 129)) { 37160c9d8683SDavid Hildenbrand vcpu->arch.sie_block->eca |= ECA_VX; 37170c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 371813211ea7SEric Farman } 37198fa1696eSCollin L. Walling if (test_kvm_facility(vcpu->kvm, 139)) 37208fa1696eSCollin L. Walling vcpu->arch.sie_block->ecd |= ECD_MEF; 3721a3da7b4aSChristian Borntraeger if (test_kvm_facility(vcpu->kvm, 156)) 3722a3da7b4aSChristian Borntraeger vcpu->arch.sie_block->ecd |= ECD_ETOKENF; 3723d7c5cb01SMichael Mueller if (vcpu->arch.sie_block->gd) { 3724d7c5cb01SMichael Mueller vcpu->arch.sie_block->eca |= ECA_AIV; 3725d7c5cb01SMichael Mueller VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u", 3726d7c5cb01SMichael Mueller vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id); 3727d7c5cb01SMichael Mueller } 3728fe0ef003SNico Boehr vcpu->arch.sie_block->sdnxo = virt_to_phys(&vcpu->run->s.regs.sdnx) | SDNXC; 3729fe0ef003SNico Boehr vcpu->arch.sie_block->riccbd = virt_to_phys(&vcpu->run->s.regs.riccb); 3730730cd632SFarhan Ali 3731730cd632SFarhan Ali if (sclp.has_kss) 3732ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS); 3733730cd632SFarhan Ali else 3734492d8642SThomas Huth vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE; 37355a5e6536SMatthew Rosato 3736e6db1d61SDominik Dingel if (vcpu->kvm->arch.use_cmma) { 3737b31605c1SDominik Dingel rc = kvm_s390_vcpu_setup_cmma(vcpu); 3738b31605c1SDominik Dingel if (rc) 3739b31605c1SDominik Dingel return rc; 3740b31288faSKonstantin Weitz } 37410ac96cafSDavid Hildenbrand hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3742ca872302SChristian Borntraeger vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup; 37439d8d5786SMichael Mueller 374467d49d52SCollin Walling vcpu->arch.sie_block->hpid = HPID_KVM; 374567d49d52SCollin Walling 37465102ee87STony Krowiak kvm_s390_vcpu_crypto_setup(vcpu); 37475102ee87STony Krowiak 37483f4bbb43SMatthew Rosato kvm_s390_vcpu_pci_setup(vcpu); 37493f4bbb43SMatthew Rosato 375029b40f10SJanosch Frank mutex_lock(&vcpu->kvm->lock); 375129b40f10SJanosch Frank if (kvm_s390_pv_is_protected(vcpu->kvm)) { 375229b40f10SJanosch Frank rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc); 375329b40f10SJanosch Frank if (rc) 375429b40f10SJanosch Frank kvm_s390_vcpu_unsetup_cmma(vcpu); 375529b40f10SJanosch Frank } 375629b40f10SJanosch Frank mutex_unlock(&vcpu->kvm->lock); 375729b40f10SJanosch Frank 3758b31605c1SDominik Dingel return rc; 3759b0c632dbSHeiko Carstens } 3760b0c632dbSHeiko Carstens 3761897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 3762897cc38eSSean Christopherson { 3763897cc38eSSean Christopherson if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id)) 3764897cc38eSSean Christopherson return -EINVAL; 3765897cc38eSSean Christopherson return 0; 3766897cc38eSSean Christopherson } 3767897cc38eSSean Christopherson 3768e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 3769b0c632dbSHeiko Carstens { 37707feb6bb8SMichael Mueller struct sie_page *sie_page; 3771897cc38eSSean Christopherson int rc; 37724d47555aSCarsten Otte 3773da72ca4dSQingFeng Hao BUILD_BUG_ON(sizeof(struct sie_page) != 4096); 3774c4196218SChristian Borntraeger sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT); 37757feb6bb8SMichael Mueller if (!sie_page) 3776e529ef66SSean Christopherson return -ENOMEM; 3777b0c632dbSHeiko Carstens 37787feb6bb8SMichael Mueller vcpu->arch.sie_block = &sie_page->sie_block; 3779fe0ef003SNico Boehr vcpu->arch.sie_block->itdba = virt_to_phys(&sie_page->itdb); 37807feb6bb8SMichael Mueller 3781efed1104SDavid Hildenbrand /* the real guest size will always be smaller than msl */ 3782efed1104SDavid Hildenbrand vcpu->arch.sie_block->mso = 0; 3783efed1104SDavid Hildenbrand vcpu->arch.sie_block->msl = sclp.hamax; 3784efed1104SDavid Hildenbrand 3785e529ef66SSean Christopherson vcpu->arch.sie_block->icpua = vcpu->vcpu_id; 3786ba5c1e9bSCarsten Otte spin_lock_init(&vcpu->arch.local_int.lock); 3787ee6a569dSMichael Mueller vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm); 37889c23a131SDavid Hildenbrand seqcount_init(&vcpu->arch.cputm_seqcount); 3789ba5c1e9bSCarsten Otte 3790321f8ee5SSean Christopherson vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 3791321f8ee5SSean Christopherson kvm_clear_async_pf_completion_queue(vcpu); 3792321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX | 3793321f8ee5SSean Christopherson KVM_SYNC_GPRS | 3794321f8ee5SSean Christopherson KVM_SYNC_ACRS | 3795321f8ee5SSean Christopherson KVM_SYNC_CRS | 3796321f8ee5SSean Christopherson KVM_SYNC_ARCH0 | 379723a60f83SCollin Walling KVM_SYNC_PFAULT | 379823a60f83SCollin Walling KVM_SYNC_DIAG318; 3799321f8ee5SSean Christopherson kvm_s390_set_prefix(vcpu, 0); 3800321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 64)) 3801321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB; 3802321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 82)) 3803321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC; 3804321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 133)) 3805321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB; 3806321f8ee5SSean Christopherson if (test_kvm_facility(vcpu->kvm, 156)) 3807321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN; 3808321f8ee5SSean Christopherson /* fprs can be synchronized via vrs, even if the guest has no vx. With 3809321f8ee5SSean Christopherson * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format. 3810321f8ee5SSean Christopherson */ 3811321f8ee5SSean Christopherson if (MACHINE_HAS_VX) 3812321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS; 3813321f8ee5SSean Christopherson else 3814321f8ee5SSean Christopherson vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS; 3815321f8ee5SSean Christopherson 3816321f8ee5SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) { 3817321f8ee5SSean Christopherson rc = __kvm_ucontrol_vcpu_init(vcpu); 3818321f8ee5SSean Christopherson if (rc) 3819a2017f17SSean Christopherson goto out_free_sie_block; 3820321f8ee5SSean Christopherson } 3821321f8ee5SSean Christopherson 3822e529ef66SSean Christopherson VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", 3823e529ef66SSean Christopherson vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3824e529ef66SSean Christopherson trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block); 3825b0c632dbSHeiko Carstens 3826ff72bb55SSean Christopherson rc = kvm_s390_vcpu_setup(vcpu); 3827ff72bb55SSean Christopherson if (rc) 3828ff72bb55SSean Christopherson goto out_ucontrol_uninit; 382924fe0195SPierre Morel 383024fe0195SPierre Morel kvm_s390_update_topology_change_report(vcpu->kvm, 1); 3831e529ef66SSean Christopherson return 0; 3832e529ef66SSean Christopherson 3833ff72bb55SSean Christopherson out_ucontrol_uninit: 3834ff72bb55SSean Christopherson if (kvm_is_ucontrol(vcpu->kvm)) 3835ff72bb55SSean Christopherson gmap_remove(vcpu->arch.gmap); 38367b06bf2fSWei Yongjun out_free_sie_block: 38377b06bf2fSWei Yongjun free_page((unsigned long)(vcpu->arch.sie_block)); 3838e529ef66SSean Christopherson return rc; 3839b0c632dbSHeiko Carstens } 3840b0c632dbSHeiko Carstens 3841b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) 3842b0c632dbSHeiko Carstens { 38439b57e9d5SHalil Pasic clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 38449a022067SDavid Hildenbrand return kvm_s390_vcpu_has_irq(vcpu, 0); 3845b0c632dbSHeiko Carstens } 3846b0c632dbSHeiko Carstens 3847199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 3848199b5763SLongpeng(Mike) { 38490546c63dSLongpeng(Mike) return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE); 3850199b5763SLongpeng(Mike) } 3851199b5763SLongpeng(Mike) 385227406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu) 385349b99e1eSChristian Borntraeger { 3854805de8f4SPeter Zijlstra atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 385561a6df54SDavid Hildenbrand exit_sie(vcpu); 385649b99e1eSChristian Borntraeger } 385749b99e1eSChristian Borntraeger 385827406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu) 385949b99e1eSChristian Borntraeger { 3860805de8f4SPeter Zijlstra atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20); 386149b99e1eSChristian Borntraeger } 386249b99e1eSChristian Borntraeger 38638e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu) 38648e236546SChristian Borntraeger { 3865805de8f4SPeter Zijlstra atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 386661a6df54SDavid Hildenbrand exit_sie(vcpu); 38678e236546SChristian Borntraeger } 38688e236546SChristian Borntraeger 38699ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu) 38709ea59728SDavid Hildenbrand { 38719ea59728SDavid Hildenbrand return atomic_read(&vcpu->arch.sie_block->prog20) & 38729ea59728SDavid Hildenbrand (PROG_BLOCK_SIE | PROG_REQUEST); 38739ea59728SDavid Hildenbrand } 38749ea59728SDavid Hildenbrand 38758e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu) 38768e236546SChristian Borntraeger { 38779bf9fde2SJason J. Herne atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20); 38788e236546SChristian Borntraeger } 38798e236546SChristian Borntraeger 388049b99e1eSChristian Borntraeger /* 38819ea59728SDavid Hildenbrand * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running. 388249b99e1eSChristian Borntraeger * If the CPU is not running (e.g. waiting as idle) the function will 388349b99e1eSChristian Borntraeger * return immediately. */ 388449b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu) 388549b99e1eSChristian Borntraeger { 3886ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT); 38879ea59728SDavid Hildenbrand kvm_s390_vsie_kick(vcpu); 388849b99e1eSChristian Borntraeger while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE) 388949b99e1eSChristian Borntraeger cpu_relax(); 389049b99e1eSChristian Borntraeger } 389149b99e1eSChristian Borntraeger 38928e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */ 38938e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu) 389449b99e1eSChristian Borntraeger { 3895df06dae3SSean Christopherson __kvm_make_request(req, vcpu); 38968e236546SChristian Borntraeger kvm_s390_vcpu_request(vcpu); 389749b99e1eSChristian Borntraeger } 389849b99e1eSChristian Borntraeger 3899414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start, 3900414d3b07SMartin Schwidefsky unsigned long end) 39012c70fe44SChristian Borntraeger { 39022c70fe44SChristian Borntraeger struct kvm *kvm = gmap->private; 39032c70fe44SChristian Borntraeger struct kvm_vcpu *vcpu; 3904414d3b07SMartin Schwidefsky unsigned long prefix; 390546808a4cSMarc Zyngier unsigned long i; 39062c70fe44SChristian Borntraeger 390765d0b0d4SDavid Hildenbrand if (gmap_is_shadow(gmap)) 390865d0b0d4SDavid Hildenbrand return; 3909414d3b07SMartin Schwidefsky if (start >= 1UL << 31) 3910414d3b07SMartin Schwidefsky /* We are only interested in prefix pages */ 3911414d3b07SMartin Schwidefsky return; 39122c70fe44SChristian Borntraeger kvm_for_each_vcpu(i, vcpu, kvm) { 39132c70fe44SChristian Borntraeger /* match against both prefix pages */ 3914414d3b07SMartin Schwidefsky prefix = kvm_s390_get_prefix(vcpu); 3915414d3b07SMartin Schwidefsky if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) { 3916414d3b07SMartin Schwidefsky VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx", 3917414d3b07SMartin Schwidefsky start, end); 3918cc65c3a1SSean Christopherson kvm_s390_sync_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu); 39192c70fe44SChristian Borntraeger } 39202c70fe44SChristian Borntraeger } 39212c70fe44SChristian Borntraeger } 39222c70fe44SChristian Borntraeger 39238b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) 39248b905d28SChristian Borntraeger { 39258b905d28SChristian Borntraeger /* do not poll with more than halt_poll_max_steal percent of steal time */ 39268b905d28SChristian Borntraeger if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >= 39276f390916SSean Christopherson READ_ONCE(halt_poll_max_steal)) { 39288b905d28SChristian Borntraeger vcpu->stat.halt_no_poll_steal++; 39298b905d28SChristian Borntraeger return true; 39308b905d28SChristian Borntraeger } 39318b905d28SChristian Borntraeger return false; 39328b905d28SChristian Borntraeger } 39338b905d28SChristian Borntraeger 3934b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 3935b6d33834SChristoffer Dall { 3936b6d33834SChristoffer Dall /* kvm common code refers to this, but never calls it */ 3937b6d33834SChristoffer Dall BUG(); 3938b6d33834SChristoffer Dall return 0; 3939b6d33834SChristoffer Dall } 3940b6d33834SChristoffer Dall 394114eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, 394214eebd91SCarsten Otte struct kvm_one_reg *reg) 394314eebd91SCarsten Otte { 394414eebd91SCarsten Otte int r = -EINVAL; 394514eebd91SCarsten Otte 394614eebd91SCarsten Otte switch (reg->id) { 394729b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 394829b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->todpr, 394929b7c71bSCarsten Otte (u32 __user *)reg->addr); 395029b7c71bSCarsten Otte break; 395129b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 395229b7c71bSCarsten Otte r = put_user(vcpu->arch.sie_block->epoch, 395329b7c71bSCarsten Otte (u64 __user *)reg->addr); 395429b7c71bSCarsten Otte break; 395546a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 39564287f247SDavid Hildenbrand r = put_user(kvm_s390_get_cpu_timer(vcpu), 395746a6dd1cSJason J. herne (u64 __user *)reg->addr); 395846a6dd1cSJason J. herne break; 395946a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 396046a6dd1cSJason J. herne r = put_user(vcpu->arch.sie_block->ckc, 396146a6dd1cSJason J. herne (u64 __user *)reg->addr); 396246a6dd1cSJason J. herne break; 3963536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 3964536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_token, 3965536336c2SDominik Dingel (u64 __user *)reg->addr); 3966536336c2SDominik Dingel break; 3967536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 3968536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_compare, 3969536336c2SDominik Dingel (u64 __user *)reg->addr); 3970536336c2SDominik Dingel break; 3971536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 3972536336c2SDominik Dingel r = put_user(vcpu->arch.pfault_select, 3973536336c2SDominik Dingel (u64 __user *)reg->addr); 3974536336c2SDominik Dingel break; 3975672550fbSChristian Borntraeger case KVM_REG_S390_PP: 3976672550fbSChristian Borntraeger r = put_user(vcpu->arch.sie_block->pp, 3977672550fbSChristian Borntraeger (u64 __user *)reg->addr); 3978672550fbSChristian Borntraeger break; 3979afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 3980afa45ff5SChristian Borntraeger r = put_user(vcpu->arch.sie_block->gbea, 3981afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 3982afa45ff5SChristian Borntraeger break; 398314eebd91SCarsten Otte default: 398414eebd91SCarsten Otte break; 398514eebd91SCarsten Otte } 398614eebd91SCarsten Otte 398714eebd91SCarsten Otte return r; 398814eebd91SCarsten Otte } 398914eebd91SCarsten Otte 399014eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, 399114eebd91SCarsten Otte struct kvm_one_reg *reg) 399214eebd91SCarsten Otte { 399314eebd91SCarsten Otte int r = -EINVAL; 39944287f247SDavid Hildenbrand __u64 val; 399514eebd91SCarsten Otte 399614eebd91SCarsten Otte switch (reg->id) { 399729b7c71bSCarsten Otte case KVM_REG_S390_TODPR: 399829b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->todpr, 399929b7c71bSCarsten Otte (u32 __user *)reg->addr); 400029b7c71bSCarsten Otte break; 400129b7c71bSCarsten Otte case KVM_REG_S390_EPOCHDIFF: 400229b7c71bSCarsten Otte r = get_user(vcpu->arch.sie_block->epoch, 400329b7c71bSCarsten Otte (u64 __user *)reg->addr); 400429b7c71bSCarsten Otte break; 400546a6dd1cSJason J. herne case KVM_REG_S390_CPU_TIMER: 40064287f247SDavid Hildenbrand r = get_user(val, (u64 __user *)reg->addr); 40074287f247SDavid Hildenbrand if (!r) 40084287f247SDavid Hildenbrand kvm_s390_set_cpu_timer(vcpu, val); 400946a6dd1cSJason J. herne break; 401046a6dd1cSJason J. herne case KVM_REG_S390_CLOCK_COMP: 401146a6dd1cSJason J. herne r = get_user(vcpu->arch.sie_block->ckc, 401246a6dd1cSJason J. herne (u64 __user *)reg->addr); 401346a6dd1cSJason J. herne break; 4014536336c2SDominik Dingel case KVM_REG_S390_PFTOKEN: 4015536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_token, 4016536336c2SDominik Dingel (u64 __user *)reg->addr); 40179fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 40189fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4019536336c2SDominik Dingel break; 4020536336c2SDominik Dingel case KVM_REG_S390_PFCOMPARE: 4021536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_compare, 4022536336c2SDominik Dingel (u64 __user *)reg->addr); 4023536336c2SDominik Dingel break; 4024536336c2SDominik Dingel case KVM_REG_S390_PFSELECT: 4025536336c2SDominik Dingel r = get_user(vcpu->arch.pfault_select, 4026536336c2SDominik Dingel (u64 __user *)reg->addr); 4027536336c2SDominik Dingel break; 4028672550fbSChristian Borntraeger case KVM_REG_S390_PP: 4029672550fbSChristian Borntraeger r = get_user(vcpu->arch.sie_block->pp, 4030672550fbSChristian Borntraeger (u64 __user *)reg->addr); 4031672550fbSChristian Borntraeger break; 4032afa45ff5SChristian Borntraeger case KVM_REG_S390_GBEA: 4033afa45ff5SChristian Borntraeger r = get_user(vcpu->arch.sie_block->gbea, 4034afa45ff5SChristian Borntraeger (u64 __user *)reg->addr); 4035afa45ff5SChristian Borntraeger break; 403614eebd91SCarsten Otte default: 403714eebd91SCarsten Otte break; 403814eebd91SCarsten Otte } 403914eebd91SCarsten Otte 404014eebd91SCarsten Otte return r; 404114eebd91SCarsten Otte } 4042b6d33834SChristoffer Dall 40437de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu) 4044b0c632dbSHeiko Carstens { 40457de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI; 40467de3f142SJanosch Frank vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID; 40477de3f142SJanosch Frank memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb)); 40487de3f142SJanosch Frank 40497de3f142SJanosch Frank kvm_clear_async_pf_completion_queue(vcpu); 40507de3f142SJanosch Frank if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) 40517de3f142SJanosch Frank kvm_s390_vcpu_stop(vcpu); 40527de3f142SJanosch Frank kvm_s390_clear_local_irqs(vcpu); 40537de3f142SJanosch Frank } 40547de3f142SJanosch Frank 40557de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu) 40567de3f142SJanosch Frank { 40577de3f142SJanosch Frank /* Initial reset is a superset of the normal reset */ 40587de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 40597de3f142SJanosch Frank 4060e93fc7b4SChristian Borntraeger /* 4061e93fc7b4SChristian Borntraeger * This equals initial cpu reset in pop, but we don't switch to ESA. 4062e93fc7b4SChristian Borntraeger * We do not only reset the internal data, but also ... 4063e93fc7b4SChristian Borntraeger */ 40647de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.mask = 0; 40657de3f142SJanosch Frank vcpu->arch.sie_block->gpsw.addr = 0; 40667de3f142SJanosch Frank kvm_s390_set_prefix(vcpu, 0); 40677de3f142SJanosch Frank kvm_s390_set_cpu_timer(vcpu, 0); 40687de3f142SJanosch Frank vcpu->arch.sie_block->ckc = 0; 40697de3f142SJanosch Frank memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr)); 40707de3f142SJanosch Frank vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK; 40717de3f142SJanosch Frank vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK; 4072e93fc7b4SChristian Borntraeger 4073e93fc7b4SChristian Borntraeger /* ... the data in sync regs */ 4074e93fc7b4SChristian Borntraeger memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs)); 4075e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 4076e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK; 4077e93fc7b4SChristian Borntraeger vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK; 4078e93fc7b4SChristian Borntraeger vcpu->run->psw_addr = 0; 4079e93fc7b4SChristian Borntraeger vcpu->run->psw_mask = 0; 4080e93fc7b4SChristian Borntraeger vcpu->run->s.regs.todpr = 0; 4081e93fc7b4SChristian Borntraeger vcpu->run->s.regs.cputm = 0; 4082e93fc7b4SChristian Borntraeger vcpu->run->s.regs.ckc = 0; 4083e93fc7b4SChristian Borntraeger vcpu->run->s.regs.pp = 0; 4084e93fc7b4SChristian Borntraeger vcpu->run->s.regs.gbea = 1; 40857de3f142SJanosch Frank vcpu->run->s.regs.fpc = 0; 40860f303504SJanosch Frank /* 40870f303504SJanosch Frank * Do not reset these registers in the protected case, as some of 40880f303504SJanosch Frank * them are overlayed and they are not accessible in this case 40890f303504SJanosch Frank * anyway. 40900f303504SJanosch Frank */ 40910f303504SJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 40927de3f142SJanosch Frank vcpu->arch.sie_block->gbea = 1; 40937de3f142SJanosch Frank vcpu->arch.sie_block->pp = 0; 40947de3f142SJanosch Frank vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 40950f303504SJanosch Frank vcpu->arch.sie_block->todpr = 0; 40960f303504SJanosch Frank } 40977de3f142SJanosch Frank } 40987de3f142SJanosch Frank 40997de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu) 41007de3f142SJanosch Frank { 41017de3f142SJanosch Frank struct kvm_sync_regs *regs = &vcpu->run->s.regs; 41027de3f142SJanosch Frank 41037de3f142SJanosch Frank /* Clear reset is a superset of the initial reset */ 41047de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 41057de3f142SJanosch Frank 41067de3f142SJanosch Frank memset(®s->gprs, 0, sizeof(regs->gprs)); 41077de3f142SJanosch Frank memset(®s->vrs, 0, sizeof(regs->vrs)); 41087de3f142SJanosch Frank memset(®s->acrs, 0, sizeof(regs->acrs)); 41097de3f142SJanosch Frank memset(®s->gscb, 0, sizeof(regs->gscb)); 41107de3f142SJanosch Frank 41117de3f142SJanosch Frank regs->etoken = 0; 41127de3f142SJanosch Frank regs->etoken_extension = 0; 4113b0c632dbSHeiko Carstens } 4114b0c632dbSHeiko Carstens 4115b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 4116b0c632dbSHeiko Carstens { 4117875656feSChristoffer Dall vcpu_load(vcpu); 41185a32c1afSChristian Borntraeger memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs)); 4119875656feSChristoffer Dall vcpu_put(vcpu); 4120b0c632dbSHeiko Carstens return 0; 4121b0c632dbSHeiko Carstens } 4122b0c632dbSHeiko Carstens 4123b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 4124b0c632dbSHeiko Carstens { 41251fc9b76bSChristoffer Dall vcpu_load(vcpu); 41265a32c1afSChristian Borntraeger memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs)); 41271fc9b76bSChristoffer Dall vcpu_put(vcpu); 4128b0c632dbSHeiko Carstens return 0; 4129b0c632dbSHeiko Carstens } 4130b0c632dbSHeiko Carstens 4131b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 4132b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 4133b0c632dbSHeiko Carstens { 4134b4ef9d4eSChristoffer Dall vcpu_load(vcpu); 4135b4ef9d4eSChristoffer Dall 413659674c1aSChristian Borntraeger memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs)); 4137b0c632dbSHeiko Carstens memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs)); 4138b4ef9d4eSChristoffer Dall 4139b4ef9d4eSChristoffer Dall vcpu_put(vcpu); 4140b0c632dbSHeiko Carstens return 0; 4141b0c632dbSHeiko Carstens } 4142b0c632dbSHeiko Carstens 4143b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 4144b0c632dbSHeiko Carstens struct kvm_sregs *sregs) 4145b0c632dbSHeiko Carstens { 4146bcdec41cSChristoffer Dall vcpu_load(vcpu); 4147bcdec41cSChristoffer Dall 414859674c1aSChristian Borntraeger memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs)); 4149b0c632dbSHeiko Carstens memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs)); 4150bcdec41cSChristoffer Dall 4151bcdec41cSChristoffer Dall vcpu_put(vcpu); 4152b0c632dbSHeiko Carstens return 0; 4153b0c632dbSHeiko Carstens } 4154b0c632dbSHeiko Carstens 4155b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 4156b0c632dbSHeiko Carstens { 41576a96bc7fSChristoffer Dall int ret = 0; 41586a96bc7fSChristoffer Dall 41596a96bc7fSChristoffer Dall vcpu_load(vcpu); 41606a96bc7fSChristoffer Dall 41616a96bc7fSChristoffer Dall if (test_fp_ctl(fpu->fpc)) { 41626a96bc7fSChristoffer Dall ret = -EINVAL; 41636a96bc7fSChristoffer Dall goto out; 41646a96bc7fSChristoffer Dall } 4165e1788bb9SChristian Borntraeger vcpu->run->s.regs.fpc = fpu->fpc; 41669abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 4167a7d4b8f2SDavid Hildenbrand convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs, 4168a7d4b8f2SDavid Hildenbrand (freg_t *) fpu->fprs); 41699abc2a08SDavid Hildenbrand else 4170a7d4b8f2SDavid Hildenbrand memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs)); 41716a96bc7fSChristoffer Dall 41726a96bc7fSChristoffer Dall out: 41736a96bc7fSChristoffer Dall vcpu_put(vcpu); 41746a96bc7fSChristoffer Dall return ret; 4175b0c632dbSHeiko Carstens } 4176b0c632dbSHeiko Carstens 4177b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 4178b0c632dbSHeiko Carstens { 41791393123eSChristoffer Dall vcpu_load(vcpu); 41801393123eSChristoffer Dall 41819abc2a08SDavid Hildenbrand /* make sure we have the latest values */ 41829abc2a08SDavid Hildenbrand save_fpu_regs(); 41839abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) 4184a7d4b8f2SDavid Hildenbrand convert_vx_to_fp((freg_t *) fpu->fprs, 4185a7d4b8f2SDavid Hildenbrand (__vector128 *) vcpu->run->s.regs.vrs); 41869abc2a08SDavid Hildenbrand else 4187a7d4b8f2SDavid Hildenbrand memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs)); 4188e1788bb9SChristian Borntraeger fpu->fpc = vcpu->run->s.regs.fpc; 41891393123eSChristoffer Dall 41901393123eSChristoffer Dall vcpu_put(vcpu); 4191b0c632dbSHeiko Carstens return 0; 4192b0c632dbSHeiko Carstens } 4193b0c632dbSHeiko Carstens 4194b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw) 4195b0c632dbSHeiko Carstens { 4196b0c632dbSHeiko Carstens int rc = 0; 4197b0c632dbSHeiko Carstens 41987a42fdc2SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 4199b0c632dbSHeiko Carstens rc = -EBUSY; 4200d7b0b5ebSCarsten Otte else { 4201d7b0b5ebSCarsten Otte vcpu->run->psw_mask = psw.mask; 4202d7b0b5ebSCarsten Otte vcpu->run->psw_addr = psw.addr; 4203d7b0b5ebSCarsten Otte } 4204b0c632dbSHeiko Carstens return rc; 4205b0c632dbSHeiko Carstens } 4206b0c632dbSHeiko Carstens 4207b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 4208b0c632dbSHeiko Carstens struct kvm_translation *tr) 4209b0c632dbSHeiko Carstens { 4210b0c632dbSHeiko Carstens return -EINVAL; /* not implemented yet */ 4211b0c632dbSHeiko Carstens } 4212b0c632dbSHeiko Carstens 421327291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \ 421427291e21SDavid Hildenbrand KVM_GUESTDBG_USE_HW_BP | \ 421527291e21SDavid Hildenbrand KVM_GUESTDBG_ENABLE) 421627291e21SDavid Hildenbrand 4217d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 4218d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 4219b0c632dbSHeiko Carstens { 422027291e21SDavid Hildenbrand int rc = 0; 422127291e21SDavid Hildenbrand 422266b56562SChristoffer Dall vcpu_load(vcpu); 422366b56562SChristoffer Dall 422427291e21SDavid Hildenbrand vcpu->guest_debug = 0; 422527291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 422627291e21SDavid Hildenbrand 422766b56562SChristoffer Dall if (dbg->control & ~VALID_GUESTDBG_FLAGS) { 422866b56562SChristoffer Dall rc = -EINVAL; 422966b56562SChristoffer Dall goto out; 423066b56562SChristoffer Dall } 423166b56562SChristoffer Dall if (!sclp.has_gpere) { 423266b56562SChristoffer Dall rc = -EINVAL; 423366b56562SChristoffer Dall goto out; 423466b56562SChristoffer Dall } 423527291e21SDavid Hildenbrand 423627291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_ENABLE) { 423727291e21SDavid Hildenbrand vcpu->guest_debug = dbg->control; 423827291e21SDavid Hildenbrand /* enforce guest PER */ 4239ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_P); 424027291e21SDavid Hildenbrand 424127291e21SDavid Hildenbrand if (dbg->control & KVM_GUESTDBG_USE_HW_BP) 424227291e21SDavid Hildenbrand rc = kvm_s390_import_bp_data(vcpu, dbg); 424327291e21SDavid Hildenbrand } else { 42449daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 424527291e21SDavid Hildenbrand vcpu->arch.guestdbg.last_bp = 0; 424627291e21SDavid Hildenbrand } 424727291e21SDavid Hildenbrand 424827291e21SDavid Hildenbrand if (rc) { 424927291e21SDavid Hildenbrand vcpu->guest_debug = 0; 425027291e21SDavid Hildenbrand kvm_s390_clear_bp_data(vcpu); 42519daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P); 425227291e21SDavid Hildenbrand } 425327291e21SDavid Hildenbrand 425466b56562SChristoffer Dall out: 425566b56562SChristoffer Dall vcpu_put(vcpu); 425627291e21SDavid Hildenbrand return rc; 4257b0c632dbSHeiko Carstens } 4258b0c632dbSHeiko Carstens 425962d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 426062d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 426162d9f0dbSMarcelo Tosatti { 4262fd232561SChristoffer Dall int ret; 4263fd232561SChristoffer Dall 4264fd232561SChristoffer Dall vcpu_load(vcpu); 4265fd232561SChristoffer Dall 42666352e4d2SDavid Hildenbrand /* CHECK_STOP and LOAD are not supported yet */ 4267fd232561SChristoffer Dall ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED : 42686352e4d2SDavid Hildenbrand KVM_MP_STATE_OPERATING; 4269fd232561SChristoffer Dall 4270fd232561SChristoffer Dall vcpu_put(vcpu); 4271fd232561SChristoffer Dall return ret; 427262d9f0dbSMarcelo Tosatti } 427362d9f0dbSMarcelo Tosatti 427462d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 427562d9f0dbSMarcelo Tosatti struct kvm_mp_state *mp_state) 427662d9f0dbSMarcelo Tosatti { 42776352e4d2SDavid Hildenbrand int rc = 0; 42786352e4d2SDavid Hildenbrand 4279e83dff5eSChristoffer Dall vcpu_load(vcpu); 4280e83dff5eSChristoffer Dall 42816352e4d2SDavid Hildenbrand /* user space knows about this interface - let it control the state */ 428267cf68b6SEric Farman kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm); 42836352e4d2SDavid Hildenbrand 42846352e4d2SDavid Hildenbrand switch (mp_state->mp_state) { 42856352e4d2SDavid Hildenbrand case KVM_MP_STATE_STOPPED: 4286fe28c786SJanosch Frank rc = kvm_s390_vcpu_stop(vcpu); 42876352e4d2SDavid Hildenbrand break; 42886352e4d2SDavid Hildenbrand case KVM_MP_STATE_OPERATING: 4289fe28c786SJanosch Frank rc = kvm_s390_vcpu_start(vcpu); 42906352e4d2SDavid Hildenbrand break; 42916352e4d2SDavid Hildenbrand case KVM_MP_STATE_LOAD: 42927c36a3fcSJanosch Frank if (!kvm_s390_pv_cpu_is_protected(vcpu)) { 42937c36a3fcSJanosch Frank rc = -ENXIO; 42947c36a3fcSJanosch Frank break; 42957c36a3fcSJanosch Frank } 42967c36a3fcSJanosch Frank rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD); 42977c36a3fcSJanosch Frank break; 42986352e4d2SDavid Hildenbrand case KVM_MP_STATE_CHECK_STOP: 42993b684a42SJoe Perches fallthrough; /* CHECK_STOP and LOAD are not supported yet */ 43006352e4d2SDavid Hildenbrand default: 43016352e4d2SDavid Hildenbrand rc = -ENXIO; 43026352e4d2SDavid Hildenbrand } 43036352e4d2SDavid Hildenbrand 4304e83dff5eSChristoffer Dall vcpu_put(vcpu); 43056352e4d2SDavid Hildenbrand return rc; 430662d9f0dbSMarcelo Tosatti } 430762d9f0dbSMarcelo Tosatti 43088ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu) 43098ad35755SDavid Hildenbrand { 43108d5fb0dcSDavid Hildenbrand return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS); 43118ad35755SDavid Hildenbrand } 43128ad35755SDavid Hildenbrand 43132c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu) 43142c70fe44SChristian Borntraeger { 43158ad35755SDavid Hildenbrand retry: 43168e236546SChristian Borntraeger kvm_s390_vcpu_request_handled(vcpu); 43172fa6e1e1SRadim Krčmář if (!kvm_request_pending(vcpu)) 4318586b7ccdSChristian Borntraeger return 0; 43192c70fe44SChristian Borntraeger /* 4320cc65c3a1SSean Christopherson * If the guest prefix changed, re-arm the ipte notifier for the 4321b2d73b2aSMartin Schwidefsky * guest prefix page. gmap_mprotect_notify will wait on the ptl lock. 43222c70fe44SChristian Borntraeger * This ensures that the ipte instruction for this request has 43232c70fe44SChristian Borntraeger * already finished. We might race against a second unmapper that 43242c70fe44SChristian Borntraeger * wants to set the blocking bit. Lets just retry the request loop. 43252c70fe44SChristian Borntraeger */ 4326cc65c3a1SSean Christopherson if (kvm_check_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu)) { 43272c70fe44SChristian Borntraeger int rc; 4328b2d73b2aSMartin Schwidefsky rc = gmap_mprotect_notify(vcpu->arch.gmap, 4329fda902cbSMichael Mueller kvm_s390_get_prefix(vcpu), 4330b2d73b2aSMartin Schwidefsky PAGE_SIZE * 2, PROT_WRITE); 4331aca411a4SJulius Niedworok if (rc) { 4332cc65c3a1SSean Christopherson kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu); 43332c70fe44SChristian Borntraeger return rc; 4334aca411a4SJulius Niedworok } 43358ad35755SDavid Hildenbrand goto retry; 43362c70fe44SChristian Borntraeger } 43378ad35755SDavid Hildenbrand 4338d3d692c8SDavid Hildenbrand if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { 4339d3d692c8SDavid Hildenbrand vcpu->arch.sie_block->ihcpu = 0xffff; 4340d3d692c8SDavid Hildenbrand goto retry; 4341d3d692c8SDavid Hildenbrand } 4342d3d692c8SDavid Hildenbrand 43438ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) { 43448ad35755SDavid Hildenbrand if (!ibs_enabled(vcpu)) { 43458ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1); 4346ef8f4f49SDavid Hildenbrand kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS); 43478ad35755SDavid Hildenbrand } 43488ad35755SDavid Hildenbrand goto retry; 43498ad35755SDavid Hildenbrand } 43508ad35755SDavid Hildenbrand 43518ad35755SDavid Hildenbrand if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) { 43528ad35755SDavid Hildenbrand if (ibs_enabled(vcpu)) { 43538ad35755SDavid Hildenbrand trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0); 43549daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS); 43558ad35755SDavid Hildenbrand } 43568ad35755SDavid Hildenbrand goto retry; 43578ad35755SDavid Hildenbrand } 43588ad35755SDavid Hildenbrand 43596502a34cSDavid Hildenbrand if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) { 43606502a34cSDavid Hildenbrand vcpu->arch.sie_block->ictl |= ICTL_OPEREXC; 43616502a34cSDavid Hildenbrand goto retry; 43626502a34cSDavid Hildenbrand } 43636502a34cSDavid Hildenbrand 4364190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) { 4365190df4a2SClaudio Imbrenda /* 4366c9f0a2b8SJanosch Frank * Disable CMM virtualization; we will emulate the ESSA 4367190df4a2SClaudio Imbrenda * instruction manually, in order to provide additional 4368190df4a2SClaudio Imbrenda * functionalities needed for live migration. 4369190df4a2SClaudio Imbrenda */ 4370190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA; 4371190df4a2SClaudio Imbrenda goto retry; 4372190df4a2SClaudio Imbrenda } 4373190df4a2SClaudio Imbrenda 4374190df4a2SClaudio Imbrenda if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) { 4375190df4a2SClaudio Imbrenda /* 4376c9f0a2b8SJanosch Frank * Re-enable CMM virtualization if CMMA is available and 4377c9f0a2b8SJanosch Frank * CMM has been used. 4378190df4a2SClaudio Imbrenda */ 4379190df4a2SClaudio Imbrenda if ((vcpu->kvm->arch.use_cmma) && 4380c9f0a2b8SJanosch Frank (vcpu->kvm->mm->context.uses_cmm)) 4381190df4a2SClaudio Imbrenda vcpu->arch.sie_block->ecb2 |= ECB2_CMMA; 4382190df4a2SClaudio Imbrenda goto retry; 4383190df4a2SClaudio Imbrenda } 4384190df4a2SClaudio Imbrenda 43853194cdb7SDavid Hildenbrand /* we left the vsie handler, nothing to do, just clear the request */ 43863194cdb7SDavid Hildenbrand kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu); 43870759d068SDavid Hildenbrand 43882c70fe44SChristian Borntraeger return 0; 43892c70fe44SChristian Borntraeger } 43902c70fe44SChristian Borntraeger 4391c0573ba5SClaudio Imbrenda static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 43928fa1696eSCollin L. Walling { 43938fa1696eSCollin L. Walling struct kvm_vcpu *vcpu; 43942cfd7b73SHeiko Carstens union tod_clock clk; 439546808a4cSMarc Zyngier unsigned long i; 43968fa1696eSCollin L. Walling 43978fa1696eSCollin L. Walling preempt_disable(); 43988fa1696eSCollin L. Walling 43992cfd7b73SHeiko Carstens store_tod_clock_ext(&clk); 44008fa1696eSCollin L. Walling 44012cfd7b73SHeiko Carstens kvm->arch.epoch = gtod->tod - clk.tod; 44020e7def5fSDavid Hildenbrand kvm->arch.epdx = 0; 44030e7def5fSDavid Hildenbrand if (test_kvm_facility(kvm, 139)) { 44042cfd7b73SHeiko Carstens kvm->arch.epdx = gtod->epoch_idx - clk.ei; 44058fa1696eSCollin L. Walling if (kvm->arch.epoch > gtod->tod) 44068fa1696eSCollin L. Walling kvm->arch.epdx -= 1; 44070e7def5fSDavid Hildenbrand } 44088fa1696eSCollin L. Walling 44098fa1696eSCollin L. Walling kvm_s390_vcpu_block_all(kvm); 44108fa1696eSCollin L. Walling kvm_for_each_vcpu(i, vcpu, kvm) { 44118fa1696eSCollin L. Walling vcpu->arch.sie_block->epoch = kvm->arch.epoch; 44128fa1696eSCollin L. Walling vcpu->arch.sie_block->epdx = kvm->arch.epdx; 44138fa1696eSCollin L. Walling } 44148fa1696eSCollin L. Walling 44158fa1696eSCollin L. Walling kvm_s390_vcpu_unblock_all(kvm); 44168fa1696eSCollin L. Walling preempt_enable(); 4417c0573ba5SClaudio Imbrenda } 4418c0573ba5SClaudio Imbrenda 4419c0573ba5SClaudio Imbrenda void kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 4420c0573ba5SClaudio Imbrenda { 4421c0573ba5SClaudio Imbrenda mutex_lock(&kvm->lock); 4422c0573ba5SClaudio Imbrenda __kvm_s390_set_tod_clock(kvm, gtod); 44238fa1696eSCollin L. Walling mutex_unlock(&kvm->lock); 44248fa1696eSCollin L. Walling } 44258fa1696eSCollin L. Walling 4426c0573ba5SClaudio Imbrenda int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod) 4427c0573ba5SClaudio Imbrenda { 4428c0573ba5SClaudio Imbrenda if (!mutex_trylock(&kvm->lock)) 4429c0573ba5SClaudio Imbrenda return 0; 4430c0573ba5SClaudio Imbrenda __kvm_s390_set_tod_clock(kvm, gtod); 4431c0573ba5SClaudio Imbrenda mutex_unlock(&kvm->lock); 4432c0573ba5SClaudio Imbrenda return 1; 4433c0573ba5SClaudio Imbrenda } 4434c0573ba5SClaudio Imbrenda 4435fa576c58SThomas Huth /** 4436fa576c58SThomas Huth * kvm_arch_fault_in_page - fault-in guest page if necessary 4437fa576c58SThomas Huth * @vcpu: The corresponding virtual cpu 4438fa576c58SThomas Huth * @gpa: Guest physical address 4439fa576c58SThomas Huth * @writable: Whether the page should be writable or not 4440fa576c58SThomas Huth * 4441fa576c58SThomas Huth * Make sure that a guest page has been faulted-in on the host. 4442fa576c58SThomas Huth * 4443fa576c58SThomas Huth * Return: Zero on success, negative error code otherwise. 4444fa576c58SThomas Huth */ 4445fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable) 444624eb3a82SDominik Dingel { 4447527e30b4SMartin Schwidefsky return gmap_fault(vcpu->arch.gmap, gpa, 4448527e30b4SMartin Schwidefsky writable ? FAULT_FLAG_WRITE : 0); 444924eb3a82SDominik Dingel } 445024eb3a82SDominik Dingel 44513c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token, 44523c038e6bSDominik Dingel unsigned long token) 44533c038e6bSDominik Dingel { 44543c038e6bSDominik Dingel struct kvm_s390_interrupt inti; 4455383d0b05SJens Freimann struct kvm_s390_irq irq; 44563c038e6bSDominik Dingel 44573c038e6bSDominik Dingel if (start_token) { 4458383d0b05SJens Freimann irq.u.ext.ext_params2 = token; 4459383d0b05SJens Freimann irq.type = KVM_S390_INT_PFAULT_INIT; 4460383d0b05SJens Freimann WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq)); 44613c038e6bSDominik Dingel } else { 44623c038e6bSDominik Dingel inti.type = KVM_S390_INT_PFAULT_DONE; 4463383d0b05SJens Freimann inti.parm64 = token; 44643c038e6bSDominik Dingel WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti)); 44653c038e6bSDominik Dingel } 44663c038e6bSDominik Dingel } 44673c038e6bSDominik Dingel 44682a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 44693c038e6bSDominik Dingel struct kvm_async_pf *work) 44703c038e6bSDominik Dingel { 44713c038e6bSDominik Dingel trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token); 44723c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token); 44732a18b7e7SVitaly Kuznetsov 44742a18b7e7SVitaly Kuznetsov return true; 44753c038e6bSDominik Dingel } 44763c038e6bSDominik Dingel 44773c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 44783c038e6bSDominik Dingel struct kvm_async_pf *work) 44793c038e6bSDominik Dingel { 44803c038e6bSDominik Dingel trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token); 44813c038e6bSDominik Dingel __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token); 44823c038e6bSDominik Dingel } 44833c038e6bSDominik Dingel 44843c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 44853c038e6bSDominik Dingel struct kvm_async_pf *work) 44863c038e6bSDominik Dingel { 44873c038e6bSDominik Dingel /* s390 will always inject the page directly */ 44883c038e6bSDominik Dingel } 44893c038e6bSDominik Dingel 44907c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) 44913c038e6bSDominik Dingel { 44923c038e6bSDominik Dingel /* 44933c038e6bSDominik Dingel * s390 will always inject the page directly, 44943c038e6bSDominik Dingel * but we still want check_async_completion to cleanup 44953c038e6bSDominik Dingel */ 44963c038e6bSDominik Dingel return true; 44973c038e6bSDominik Dingel } 44983c038e6bSDominik Dingel 4499e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu) 45003c038e6bSDominik Dingel { 45013c038e6bSDominik Dingel hva_t hva; 45023c038e6bSDominik Dingel struct kvm_arch_async_pf arch; 45033c038e6bSDominik Dingel 45043c038e6bSDominik Dingel if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 4505e8c22266SVitaly Kuznetsov return false; 45063c038e6bSDominik Dingel if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) != 45073c038e6bSDominik Dingel vcpu->arch.pfault_compare) 4508e8c22266SVitaly Kuznetsov return false; 45093c038e6bSDominik Dingel if (psw_extint_disabled(vcpu)) 4510e8c22266SVitaly Kuznetsov return false; 45119a022067SDavid Hildenbrand if (kvm_s390_vcpu_has_irq(vcpu, 0)) 4512e8c22266SVitaly Kuznetsov return false; 4513b9224cd7SDavid Hildenbrand if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) 4514e8c22266SVitaly Kuznetsov return false; 45153c038e6bSDominik Dingel if (!vcpu->arch.gmap->pfault_enabled) 4516e8c22266SVitaly Kuznetsov return false; 45173c038e6bSDominik Dingel 451881480cc1SHeiko Carstens hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr)); 451981480cc1SHeiko Carstens hva += current->thread.gmap_addr & ~PAGE_MASK; 452081480cc1SHeiko Carstens if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8)) 4521e8c22266SVitaly Kuznetsov return false; 45223c038e6bSDominik Dingel 4523e8c22266SVitaly Kuznetsov return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch); 45243c038e6bSDominik Dingel } 45253c038e6bSDominik Dingel 45263fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu) 4527b0c632dbSHeiko Carstens { 45283fb4c40fSThomas Huth int rc, cpuflags; 4529e168bf8dSCarsten Otte 45303c038e6bSDominik Dingel /* 45313c038e6bSDominik Dingel * On s390 notifications for arriving pages will be delivered directly 45323c038e6bSDominik Dingel * to the guest but the house keeping for completed pfaults is 45333c038e6bSDominik Dingel * handled outside the worker. 45343c038e6bSDominik Dingel */ 45353c038e6bSDominik Dingel kvm_check_async_pf_completion(vcpu); 45363c038e6bSDominik Dingel 45377ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14]; 45387ec7c8c7SChristian Borntraeger vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15]; 4539b0c632dbSHeiko Carstens 4540b0c632dbSHeiko Carstens if (need_resched()) 4541b0c632dbSHeiko Carstens schedule(); 4542b0c632dbSHeiko Carstens 454379395031SJens Freimann if (!kvm_is_ucontrol(vcpu->kvm)) { 454479395031SJens Freimann rc = kvm_s390_deliver_pending_interrupts(vcpu); 454579395031SJens Freimann if (rc) 454679395031SJens Freimann return rc; 454779395031SJens Freimann } 45480ff31867SCarsten Otte 45492c70fe44SChristian Borntraeger rc = kvm_s390_handle_requests(vcpu); 45502c70fe44SChristian Borntraeger if (rc) 45512c70fe44SChristian Borntraeger return rc; 45522c70fe44SChristian Borntraeger 455327291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) { 455427291e21SDavid Hildenbrand kvm_s390_backup_guest_per_regs(vcpu); 455527291e21SDavid Hildenbrand kvm_s390_patch_guest_per_regs(vcpu); 455627291e21SDavid Hildenbrand } 455727291e21SDavid Hildenbrand 45584eeef242SSean Christopherson clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask); 45599f30f621SMichael Mueller 4560b0c632dbSHeiko Carstens vcpu->arch.sie_block->icptcode = 0; 45613fb4c40fSThomas Huth cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags); 45623fb4c40fSThomas Huth VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags); 45633fb4c40fSThomas Huth trace_kvm_s390_sie_enter(vcpu, cpuflags); 45642b29a9fdSDominik Dingel 45653fb4c40fSThomas Huth return 0; 45663fb4c40fSThomas Huth } 45673fb4c40fSThomas Huth 4568492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu) 4569492d8642SThomas Huth { 457056317920SDavid Hildenbrand struct kvm_s390_pgm_info pgm_info = { 457156317920SDavid Hildenbrand .code = PGM_ADDRESSING, 457256317920SDavid Hildenbrand }; 457356317920SDavid Hildenbrand u8 opcode, ilen; 4574492d8642SThomas Huth int rc; 4575492d8642SThomas Huth 4576492d8642SThomas Huth VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction"); 4577492d8642SThomas Huth trace_kvm_s390_sie_fault(vcpu); 4578492d8642SThomas Huth 4579492d8642SThomas Huth /* 4580492d8642SThomas Huth * We want to inject an addressing exception, which is defined as a 4581492d8642SThomas Huth * suppressing or terminating exception. However, since we came here 4582492d8642SThomas Huth * by a DAT access exception, the PSW still points to the faulting 4583492d8642SThomas Huth * instruction since DAT exceptions are nullifying. So we've got 4584492d8642SThomas Huth * to look up the current opcode to get the length of the instruction 4585492d8642SThomas Huth * to be able to forward the PSW. 4586492d8642SThomas Huth */ 45873fa8cad7SDavid Hildenbrand rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1); 458856317920SDavid Hildenbrand ilen = insn_length(opcode); 45899b0d721aSDavid Hildenbrand if (rc < 0) { 45909b0d721aSDavid Hildenbrand return rc; 45919b0d721aSDavid Hildenbrand } else if (rc) { 45929b0d721aSDavid Hildenbrand /* Instruction-Fetching Exceptions - we can't detect the ilen. 45939b0d721aSDavid Hildenbrand * Forward by arbitrary ilc, injection will take care of 45949b0d721aSDavid Hildenbrand * nullification if necessary. 45959b0d721aSDavid Hildenbrand */ 45969b0d721aSDavid Hildenbrand pgm_info = vcpu->arch.pgm; 45979b0d721aSDavid Hildenbrand ilen = 4; 45989b0d721aSDavid Hildenbrand } 459956317920SDavid Hildenbrand pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID; 460056317920SDavid Hildenbrand kvm_s390_forward_psw(vcpu, ilen); 460156317920SDavid Hildenbrand return kvm_s390_inject_prog_irq(vcpu, &pgm_info); 4602492d8642SThomas Huth } 4603492d8642SThomas Huth 46043fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason) 46053fb4c40fSThomas Huth { 46064d62fcc0SQingFeng Hao struct mcck_volatile_info *mcck_info; 46074d62fcc0SQingFeng Hao struct sie_page *sie_page; 46084d62fcc0SQingFeng Hao 46092b29a9fdSDominik Dingel VCPU_EVENT(vcpu, 6, "exit sie icptcode %d", 46102b29a9fdSDominik Dingel vcpu->arch.sie_block->icptcode); 46112b29a9fdSDominik Dingel trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode); 46122b29a9fdSDominik Dingel 461327291e21SDavid Hildenbrand if (guestdbg_enabled(vcpu)) 461427291e21SDavid Hildenbrand kvm_s390_restore_guest_per_regs(vcpu); 461527291e21SDavid Hildenbrand 46167ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14; 46177ec7c8c7SChristian Borntraeger vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15; 461871f116bfSDavid Hildenbrand 46194d62fcc0SQingFeng Hao if (exit_reason == -EINTR) { 46204d62fcc0SQingFeng Hao VCPU_EVENT(vcpu, 3, "%s", "machine check"); 46214d62fcc0SQingFeng Hao sie_page = container_of(vcpu->arch.sie_block, 46224d62fcc0SQingFeng Hao struct sie_page, sie_block); 46234d62fcc0SQingFeng Hao mcck_info = &sie_page->mcck_info; 46244d62fcc0SQingFeng Hao kvm_s390_reinject_machine_check(vcpu, mcck_info); 46254d62fcc0SQingFeng Hao return 0; 46264d62fcc0SQingFeng Hao } 46274d62fcc0SQingFeng Hao 462871f116bfSDavid Hildenbrand if (vcpu->arch.sie_block->icptcode > 0) { 462971f116bfSDavid Hildenbrand int rc = kvm_handle_sie_intercept(vcpu); 463071f116bfSDavid Hildenbrand 463171f116bfSDavid Hildenbrand if (rc != -EOPNOTSUPP) 463271f116bfSDavid Hildenbrand return rc; 463371f116bfSDavid Hildenbrand vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC; 463471f116bfSDavid Hildenbrand vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode; 463571f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa; 463671f116bfSDavid Hildenbrand vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb; 463771f116bfSDavid Hildenbrand return -EREMOTE; 463871f116bfSDavid Hildenbrand } else if (exit_reason != -EFAULT) { 463971f116bfSDavid Hildenbrand vcpu->stat.exit_null++; 464071f116bfSDavid Hildenbrand return 0; 4641210b1607SThomas Huth } else if (kvm_is_ucontrol(vcpu->kvm)) { 4642210b1607SThomas Huth vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL; 4643210b1607SThomas Huth vcpu->run->s390_ucontrol.trans_exc_code = 4644210b1607SThomas Huth current->thread.gmap_addr; 4645210b1607SThomas Huth vcpu->run->s390_ucontrol.pgm_code = 0x10; 464671f116bfSDavid Hildenbrand return -EREMOTE; 464724eb3a82SDominik Dingel } else if (current->thread.gmap_pfault) { 46483c038e6bSDominik Dingel trace_kvm_s390_major_guest_pfault(vcpu); 464924eb3a82SDominik Dingel current->thread.gmap_pfault = 0; 465071f116bfSDavid Hildenbrand if (kvm_arch_setup_async_pf(vcpu)) 465171f116bfSDavid Hildenbrand return 0; 465250a05be4SChristian Borntraeger vcpu->stat.pfault_sync++; 465371f116bfSDavid Hildenbrand return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1); 4654fa576c58SThomas Huth } 465571f116bfSDavid Hildenbrand return vcpu_post_run_fault_in_sie(vcpu); 46563fb4c40fSThomas Huth } 46573fb4c40fSThomas Huth 46583adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK) 46593fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu) 46603fb4c40fSThomas Huth { 46613fb4c40fSThomas Huth int rc, exit_reason; 4662c8aac234SJanosch Frank struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block; 46633fb4c40fSThomas Huth 4664800c1065SThomas Huth /* 4665800c1065SThomas Huth * We try to hold kvm->srcu during most of vcpu_run (except when run- 4666800c1065SThomas Huth * ning the guest), so that memslots (and other stuff) are protected 4667800c1065SThomas Huth */ 46682031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu); 4669800c1065SThomas Huth 4670a76ccff6SThomas Huth do { 46713fb4c40fSThomas Huth rc = vcpu_pre_run(vcpu); 46723fb4c40fSThomas Huth if (rc) 4673a76ccff6SThomas Huth break; 46743fb4c40fSThomas Huth 46752031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu); 46763fb4c40fSThomas Huth /* 4677a76ccff6SThomas Huth * As PF_VCPU will be used in fault handler, between 4678a76ccff6SThomas Huth * guest_enter and guest_exit should be no uaccess. 46793fb4c40fSThomas Huth */ 46800097d12eSChristian Borntraeger local_irq_disable(); 46816edaa530SPaolo Bonzini guest_enter_irqoff(); 4682db0758b2SDavid Hildenbrand __disable_cpu_timer_accounting(vcpu); 46830097d12eSChristian Borntraeger local_irq_enable(); 4684c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4685c8aac234SJanosch Frank memcpy(sie_page->pv_grregs, 4686c8aac234SJanosch Frank vcpu->run->s.regs.gprs, 4687c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 4688c8aac234SJanosch Frank } 468956e62a73SSven Schnelle if (test_cpu_flag(CIF_FPU)) 469056e62a73SSven Schnelle load_fpu_regs(); 4691a76ccff6SThomas Huth exit_reason = sie64a(vcpu->arch.sie_block, 4692a76ccff6SThomas Huth vcpu->run->s.regs.gprs); 4693c8aac234SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 4694c8aac234SJanosch Frank memcpy(vcpu->run->s.regs.gprs, 4695c8aac234SJanosch Frank sie_page->pv_grregs, 4696c8aac234SJanosch Frank sizeof(sie_page->pv_grregs)); 46973adae0b4SJanosch Frank /* 46983adae0b4SJanosch Frank * We're not allowed to inject interrupts on intercepts 46993adae0b4SJanosch Frank * that leave the guest state in an "in-between" state 47003adae0b4SJanosch Frank * where the next SIE entry will do a continuation. 47013adae0b4SJanosch Frank * Fence interrupts in our "internal" PSW. 47023adae0b4SJanosch Frank */ 47033adae0b4SJanosch Frank if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR || 47043adae0b4SJanosch Frank vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) { 47053adae0b4SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 47063adae0b4SJanosch Frank } 4707c8aac234SJanosch Frank } 47080097d12eSChristian Borntraeger local_irq_disable(); 4709db0758b2SDavid Hildenbrand __enable_cpu_timer_accounting(vcpu); 47106edaa530SPaolo Bonzini guest_exit_irqoff(); 47110097d12eSChristian Borntraeger local_irq_enable(); 47122031f287SSean Christopherson kvm_vcpu_srcu_read_lock(vcpu); 47133fb4c40fSThomas Huth 47143fb4c40fSThomas Huth rc = vcpu_post_run(vcpu, exit_reason); 471527291e21SDavid Hildenbrand } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc); 47163fb4c40fSThomas Huth 47172031f287SSean Christopherson kvm_vcpu_srcu_read_unlock(vcpu); 4718e168bf8dSCarsten Otte return rc; 4719b0c632dbSHeiko Carstens } 4720b0c632dbSHeiko Carstens 47212f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu) 4722b028ee3eSDavid Hildenbrand { 47232f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 47244d5f2c04SChristian Borntraeger struct runtime_instr_cb *riccb; 47254e0b1ab7SFan Zhang struct gs_cb *gscb; 47264d5f2c04SChristian Borntraeger 47274d5f2c04SChristian Borntraeger riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb; 47284e0b1ab7SFan Zhang gscb = (struct gs_cb *) &kvm_run->s.regs.gscb; 4729b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask; 4730b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr; 4731b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4732b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr; 4733b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->pp = kvm_run->s.regs.pp; 4734b028ee3eSDavid Hildenbrand vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea; 4735b028ee3eSDavid Hildenbrand } 4736b028ee3eSDavid Hildenbrand if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) { 4737b028ee3eSDavid Hildenbrand vcpu->arch.pfault_token = kvm_run->s.regs.pft; 4738b028ee3eSDavid Hildenbrand vcpu->arch.pfault_select = kvm_run->s.regs.pfs; 4739b028ee3eSDavid Hildenbrand vcpu->arch.pfault_compare = kvm_run->s.regs.pfc; 47409fbd8082SDavid Hildenbrand if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID) 47419fbd8082SDavid Hildenbrand kvm_clear_async_pf_completion_queue(vcpu); 4742b028ee3eSDavid Hildenbrand } 474323a60f83SCollin Walling if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) { 474423a60f83SCollin Walling vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318; 474523a60f83SCollin Walling vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc; 47463fd8417fSCollin Walling VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc); 474723a60f83SCollin Walling } 474880cd8763SFan Zhang /* 474980cd8763SFan Zhang * If userspace sets the riccb (e.g. after migration) to a valid state, 475080cd8763SFan Zhang * we should enable RI here instead of doing the lazy enablement. 475180cd8763SFan Zhang */ 475280cd8763SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) && 47534d5f2c04SChristian Borntraeger test_kvm_facility(vcpu->kvm, 64) && 4754bb59c2daSAlice Frosi riccb->v && 47550c9d8683SDavid Hildenbrand !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) { 47564d5f2c04SChristian Borntraeger VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)"); 47570c9d8683SDavid Hildenbrand vcpu->arch.sie_block->ecb3 |= ECB3_RI; 475880cd8763SFan Zhang } 47594e0b1ab7SFan Zhang /* 47604e0b1ab7SFan Zhang * If userspace sets the gscb (e.g. after migration) to non-zero, 47614e0b1ab7SFan Zhang * we should enable GS here instead of doing the lazy enablement. 47624e0b1ab7SFan Zhang */ 47634e0b1ab7SFan Zhang if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) && 47644e0b1ab7SFan Zhang test_kvm_facility(vcpu->kvm, 133) && 47654e0b1ab7SFan Zhang gscb->gssm && 47664e0b1ab7SFan Zhang !vcpu->arch.gs_enabled) { 47674e0b1ab7SFan Zhang VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)"); 47684e0b1ab7SFan Zhang vcpu->arch.sie_block->ecb |= ECB_GS; 47694e0b1ab7SFan Zhang vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT; 47704e0b1ab7SFan Zhang vcpu->arch.gs_enabled = 1; 477180cd8763SFan Zhang } 477235b3fde6SChristian Borntraeger if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) && 477335b3fde6SChristian Borntraeger test_kvm_facility(vcpu->kvm, 82)) { 477435b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf &= ~FPF_BPBC; 477535b3fde6SChristian Borntraeger vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0; 477635b3fde6SChristian Borntraeger } 47774e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 47784e0b1ab7SFan Zhang preempt_disable(); 47794e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 47804e0b1ab7SFan Zhang if (current->thread.gs_cb) { 47814e0b1ab7SFan Zhang vcpu->arch.host_gscb = current->thread.gs_cb; 47824e0b1ab7SFan Zhang save_gs_cb(vcpu->arch.host_gscb); 47834e0b1ab7SFan Zhang } 47844e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) { 47854e0b1ab7SFan Zhang current->thread.gs_cb = (struct gs_cb *) 47864e0b1ab7SFan Zhang &vcpu->run->s.regs.gscb; 47874e0b1ab7SFan Zhang restore_gs_cb(current->thread.gs_cb); 47884e0b1ab7SFan Zhang } 47894e0b1ab7SFan Zhang preempt_enable(); 47904e0b1ab7SFan Zhang } 4791a3da7b4aSChristian Borntraeger /* SIE will load etoken directly from SDNX and therefore kvm_run */ 4792811ea797SJanosch Frank } 4793811ea797SJanosch Frank 47942f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu) 4795811ea797SJanosch Frank { 47962f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 47972f0a83beSTianjia Zhang 4798811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) 4799811ea797SJanosch Frank kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix); 4800811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) { 4801811ea797SJanosch Frank memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128); 4802811ea797SJanosch Frank /* some control register changes require a tlb flush */ 4803811ea797SJanosch Frank kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 4804811ea797SJanosch Frank } 4805811ea797SJanosch Frank if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) { 4806811ea797SJanosch Frank kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm); 4807811ea797SJanosch Frank vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc; 4808811ea797SJanosch Frank } 4809811ea797SJanosch Frank save_access_regs(vcpu->arch.host_acrs); 4810811ea797SJanosch Frank restore_access_regs(vcpu->run->s.regs.acrs); 4811811ea797SJanosch Frank /* save host (userspace) fprs/vrs */ 4812811ea797SJanosch Frank save_fpu_regs(); 4813811ea797SJanosch Frank vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc; 4814811ea797SJanosch Frank vcpu->arch.host_fpregs.regs = current->thread.fpu.regs; 4815811ea797SJanosch Frank if (MACHINE_HAS_VX) 4816811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.vrs; 4817811ea797SJanosch Frank else 4818811ea797SJanosch Frank current->thread.fpu.regs = vcpu->run->s.regs.fprs; 4819811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->run->s.regs.fpc; 4820811ea797SJanosch Frank if (test_fp_ctl(current->thread.fpu.fpc)) 4821811ea797SJanosch Frank /* User space provided an invalid FPC, let's clear it */ 4822811ea797SJanosch Frank current->thread.fpu.fpc = 0; 4823811ea797SJanosch Frank 4824811ea797SJanosch Frank /* Sync fmt2 only data */ 4825811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) { 48262f0a83beSTianjia Zhang sync_regs_fmt2(vcpu); 4827811ea797SJanosch Frank } else { 4828811ea797SJanosch Frank /* 4829811ea797SJanosch Frank * In several places we have to modify our internal view to 4830811ea797SJanosch Frank * not do things that are disallowed by the ultravisor. For 4831811ea797SJanosch Frank * example we must not inject interrupts after specific exits 4832811ea797SJanosch Frank * (e.g. 112 prefix page not secure). We do this by turning 4833811ea797SJanosch Frank * off the machine check, external and I/O interrupt bits 4834811ea797SJanosch Frank * of our PSW copy. To avoid getting validity intercepts, we 4835811ea797SJanosch Frank * do only accept the condition code from userspace. 4836811ea797SJanosch Frank */ 4837811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC; 4838811ea797SJanosch Frank vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask & 4839811ea797SJanosch Frank PSW_MASK_CC; 4840811ea797SJanosch Frank } 484180cd8763SFan Zhang 4842b028ee3eSDavid Hildenbrand kvm_run->kvm_dirty_regs = 0; 4843b028ee3eSDavid Hildenbrand } 4844b028ee3eSDavid Hildenbrand 48452f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu) 4846b028ee3eSDavid Hildenbrand { 48472f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48482f0a83beSTianjia Zhang 4849b028ee3eSDavid Hildenbrand kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr; 4850b028ee3eSDavid Hildenbrand kvm_run->s.regs.pp = vcpu->arch.sie_block->pp; 4851b028ee3eSDavid Hildenbrand kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea; 485235b3fde6SChristian Borntraeger kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC; 485323a60f83SCollin Walling kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val; 48544e0b1ab7SFan Zhang if (MACHINE_HAS_GS) { 485544bada28SHeiko Carstens preempt_disable(); 48564e0b1ab7SFan Zhang __ctl_set_bit(2, 4); 48574e0b1ab7SFan Zhang if (vcpu->arch.gs_enabled) 48584e0b1ab7SFan Zhang save_gs_cb(current->thread.gs_cb); 48594e0b1ab7SFan Zhang current->thread.gs_cb = vcpu->arch.host_gscb; 48604e0b1ab7SFan Zhang restore_gs_cb(vcpu->arch.host_gscb); 48614e0b1ab7SFan Zhang if (!vcpu->arch.host_gscb) 48624e0b1ab7SFan Zhang __ctl_clear_bit(2, 4); 48634e0b1ab7SFan Zhang vcpu->arch.host_gscb = NULL; 486444bada28SHeiko Carstens preempt_enable(); 48654e0b1ab7SFan Zhang } 4866a3da7b4aSChristian Borntraeger /* SIE will save etoken directly into SDNX and therefore kvm_run */ 4867b028ee3eSDavid Hildenbrand } 4868b028ee3eSDavid Hildenbrand 48692f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu) 4870811ea797SJanosch Frank { 48712f0a83beSTianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48722f0a83beSTianjia Zhang 4873811ea797SJanosch Frank kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask; 4874811ea797SJanosch Frank kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr; 4875811ea797SJanosch Frank kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu); 4876811ea797SJanosch Frank memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128); 4877811ea797SJanosch Frank kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu); 4878811ea797SJanosch Frank kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc; 4879811ea797SJanosch Frank kvm_run->s.regs.pft = vcpu->arch.pfault_token; 4880811ea797SJanosch Frank kvm_run->s.regs.pfs = vcpu->arch.pfault_select; 4881811ea797SJanosch Frank kvm_run->s.regs.pfc = vcpu->arch.pfault_compare; 4882811ea797SJanosch Frank save_access_regs(vcpu->run->s.regs.acrs); 4883811ea797SJanosch Frank restore_access_regs(vcpu->arch.host_acrs); 4884811ea797SJanosch Frank /* Save guest register state */ 4885811ea797SJanosch Frank save_fpu_regs(); 4886811ea797SJanosch Frank vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 4887811ea797SJanosch Frank /* Restore will be done lazily at return */ 4888811ea797SJanosch Frank current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc; 4889811ea797SJanosch Frank current->thread.fpu.regs = vcpu->arch.host_fpregs.regs; 4890811ea797SJanosch Frank if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) 48912f0a83beSTianjia Zhang store_regs_fmt2(vcpu); 4892811ea797SJanosch Frank } 4893811ea797SJanosch Frank 48941b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 4895b0c632dbSHeiko Carstens { 48961b94f6f8STianjia Zhang struct kvm_run *kvm_run = vcpu->run; 48978f2abe6aSChristian Borntraeger int rc; 4898b0c632dbSHeiko Carstens 48990460eb35SJanosch Frank /* 49000460eb35SJanosch Frank * Running a VM while dumping always has the potential to 49010460eb35SJanosch Frank * produce inconsistent dump data. But for PV vcpus a SIE 49020460eb35SJanosch Frank * entry while dumping could also lead to a fatal validity 49030460eb35SJanosch Frank * intercept which we absolutely want to avoid. 49040460eb35SJanosch Frank */ 49050460eb35SJanosch Frank if (vcpu->kvm->arch.pv.dumping) 49060460eb35SJanosch Frank return -EINVAL; 49070460eb35SJanosch Frank 4908460df4c1SPaolo Bonzini if (kvm_run->immediate_exit) 4909460df4c1SPaolo Bonzini return -EINTR; 4910460df4c1SPaolo Bonzini 4911200824f5SThomas Huth if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS || 4912200824f5SThomas Huth kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS) 4913200824f5SThomas Huth return -EINVAL; 4914200824f5SThomas Huth 4915accb757dSChristoffer Dall vcpu_load(vcpu); 4916accb757dSChristoffer Dall 491727291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu)) { 491827291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 4919accb757dSChristoffer Dall rc = 0; 4920accb757dSChristoffer Dall goto out; 492127291e21SDavid Hildenbrand } 492227291e21SDavid Hildenbrand 492320b7035cSJan H. Schönherr kvm_sigset_activate(vcpu); 4924b0c632dbSHeiko Carstens 4925fe28c786SJanosch Frank /* 4926fe28c786SJanosch Frank * no need to check the return value of vcpu_start as it can only have 4927fe28c786SJanosch Frank * an error for protvirt, but protvirt means user cpu state 4928fe28c786SJanosch Frank */ 49296352e4d2SDavid Hildenbrand if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) { 49306852d7b6SDavid Hildenbrand kvm_s390_vcpu_start(vcpu); 49316352e4d2SDavid Hildenbrand } else if (is_vcpu_stopped(vcpu)) { 4932ea2cdd27SDavid Hildenbrand pr_err_ratelimited("can't run stopped vcpu %d\n", 49336352e4d2SDavid Hildenbrand vcpu->vcpu_id); 4934accb757dSChristoffer Dall rc = -EINVAL; 4935accb757dSChristoffer Dall goto out; 49366352e4d2SDavid Hildenbrand } 4937b0c632dbSHeiko Carstens 49382f0a83beSTianjia Zhang sync_regs(vcpu); 4939db0758b2SDavid Hildenbrand enable_cpu_timer_accounting(vcpu); 4940d7b0b5ebSCarsten Otte 4941dab4079dSHeiko Carstens might_fault(); 4942e168bf8dSCarsten Otte rc = __vcpu_run(vcpu); 49439ace903dSChristian Ehrhardt 4944b1d16c49SChristian Ehrhardt if (signal_pending(current) && !rc) { 4945b1d16c49SChristian Ehrhardt kvm_run->exit_reason = KVM_EXIT_INTR; 49468f2abe6aSChristian Borntraeger rc = -EINTR; 4947b1d16c49SChristian Ehrhardt } 49488f2abe6aSChristian Borntraeger 494927291e21SDavid Hildenbrand if (guestdbg_exit_pending(vcpu) && !rc) { 495027291e21SDavid Hildenbrand kvm_s390_prepare_debug_exit(vcpu); 495127291e21SDavid Hildenbrand rc = 0; 495227291e21SDavid Hildenbrand } 495327291e21SDavid Hildenbrand 49548f2abe6aSChristian Borntraeger if (rc == -EREMOTE) { 495571f116bfSDavid Hildenbrand /* userspace support is needed, kvm_run has been prepared */ 49568f2abe6aSChristian Borntraeger rc = 0; 49578f2abe6aSChristian Borntraeger } 49588f2abe6aSChristian Borntraeger 4959db0758b2SDavid Hildenbrand disable_cpu_timer_accounting(vcpu); 49602f0a83beSTianjia Zhang store_regs(vcpu); 4961d7b0b5ebSCarsten Otte 496220b7035cSJan H. Schönherr kvm_sigset_deactivate(vcpu); 4963b0c632dbSHeiko Carstens 4964b0c632dbSHeiko Carstens vcpu->stat.exit_userspace++; 4965accb757dSChristoffer Dall out: 4966accb757dSChristoffer Dall vcpu_put(vcpu); 49677e8e6ab4SHeiko Carstens return rc; 4968b0c632dbSHeiko Carstens } 4969b0c632dbSHeiko Carstens 4970b0c632dbSHeiko Carstens /* 4971b0c632dbSHeiko Carstens * store status at address 4972b0c632dbSHeiko Carstens * we use have two special cases: 4973b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit 4974b0c632dbSHeiko Carstens * KVM_S390_STORE_STATUS_PREFIXED: -> prefix 4975b0c632dbSHeiko Carstens */ 4976d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa) 4977b0c632dbSHeiko Carstens { 4978092670cdSCarsten Otte unsigned char archmode = 1; 49799abc2a08SDavid Hildenbrand freg_t fprs[NUM_FPRS]; 4980fda902cbSMichael Mueller unsigned int px; 49814287f247SDavid Hildenbrand u64 clkcomp, cputm; 4982d0bce605SHeiko Carstens int rc; 4983b0c632dbSHeiko Carstens 4984d9a3a09aSMartin Schwidefsky px = kvm_s390_get_prefix(vcpu); 4985d0bce605SHeiko Carstens if (gpa == KVM_S390_STORE_STATUS_NOADDR) { 4986d0bce605SHeiko Carstens if (write_guest_abs(vcpu, 163, &archmode, 1)) 4987b0c632dbSHeiko Carstens return -EFAULT; 4988d9a3a09aSMartin Schwidefsky gpa = 0; 4989d0bce605SHeiko Carstens } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) { 4990d0bce605SHeiko Carstens if (write_guest_real(vcpu, 163, &archmode, 1)) 4991b0c632dbSHeiko Carstens return -EFAULT; 4992d9a3a09aSMartin Schwidefsky gpa = px; 4993d9a3a09aSMartin Schwidefsky } else 4994d9a3a09aSMartin Schwidefsky gpa -= __LC_FPREGS_SAVE_AREA; 49959abc2a08SDavid Hildenbrand 49969abc2a08SDavid Hildenbrand /* manually convert vector registers if necessary */ 49979abc2a08SDavid Hildenbrand if (MACHINE_HAS_VX) { 49989522b37fSDavid Hildenbrand convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs); 4999d9a3a09aSMartin Schwidefsky rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 50009abc2a08SDavid Hildenbrand fprs, 128); 50019abc2a08SDavid Hildenbrand } else { 50029abc2a08SDavid Hildenbrand rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA, 50036fd8e67dSDavid Hildenbrand vcpu->run->s.regs.fprs, 128); 50049abc2a08SDavid Hildenbrand } 5005d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA, 5006d0bce605SHeiko Carstens vcpu->run->s.regs.gprs, 128); 5007d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA, 5008d0bce605SHeiko Carstens &vcpu->arch.sie_block->gpsw, 16); 5009d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA, 5010fda902cbSMichael Mueller &px, 4); 5011d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA, 50129abc2a08SDavid Hildenbrand &vcpu->run->s.regs.fpc, 4); 5013d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA, 5014d0bce605SHeiko Carstens &vcpu->arch.sie_block->todpr, 4); 50154287f247SDavid Hildenbrand cputm = kvm_s390_get_cpu_timer(vcpu); 5016d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA, 50174287f247SDavid Hildenbrand &cputm, 8); 5018178bd789SThomas Huth clkcomp = vcpu->arch.sie_block->ckc >> 8; 5019d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA, 5020d0bce605SHeiko Carstens &clkcomp, 8); 5021d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA, 5022d0bce605SHeiko Carstens &vcpu->run->s.regs.acrs, 64); 5023d9a3a09aSMartin Schwidefsky rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA, 5024d0bce605SHeiko Carstens &vcpu->arch.sie_block->gcr, 128); 5025d0bce605SHeiko Carstens return rc ? -EFAULT : 0; 5026b0c632dbSHeiko Carstens } 5027b0c632dbSHeiko Carstens 5028e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr) 5029e879892cSThomas Huth { 5030e879892cSThomas Huth /* 5031e879892cSThomas Huth * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy 503231d8b8d4SChristian Borntraeger * switch in the run ioctl. Let's update our copies before we save 5033e879892cSThomas Huth * it into the save area 5034e879892cSThomas Huth */ 5035d0164ee2SHendrik Brueckner save_fpu_regs(); 50369abc2a08SDavid Hildenbrand vcpu->run->s.regs.fpc = current->thread.fpu.fpc; 5037e879892cSThomas Huth save_access_regs(vcpu->run->s.regs.acrs); 5038e879892cSThomas Huth 5039e879892cSThomas Huth return kvm_s390_store_status_unloaded(vcpu, addr); 5040e879892cSThomas Huth } 5041e879892cSThomas Huth 50428ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 50438ad35755SDavid Hildenbrand { 50448ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu); 50458e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu); 50468ad35755SDavid Hildenbrand } 50478ad35755SDavid Hildenbrand 50488ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm) 50498ad35755SDavid Hildenbrand { 505046808a4cSMarc Zyngier unsigned long i; 50518ad35755SDavid Hildenbrand struct kvm_vcpu *vcpu; 50528ad35755SDavid Hildenbrand 50538ad35755SDavid Hildenbrand kvm_for_each_vcpu(i, vcpu, kvm) { 50548ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 50558ad35755SDavid Hildenbrand } 50568ad35755SDavid Hildenbrand } 50578ad35755SDavid Hildenbrand 50588ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu) 50598ad35755SDavid Hildenbrand { 506009a400e7SDavid Hildenbrand if (!sclp.has_ibs) 506109a400e7SDavid Hildenbrand return; 50628ad35755SDavid Hildenbrand kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu); 50638e236546SChristian Borntraeger kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu); 50648ad35755SDavid Hildenbrand } 50658ad35755SDavid Hildenbrand 5066fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu) 50676852d7b6SDavid Hildenbrand { 5068fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 50698ad35755SDavid Hildenbrand 50708ad35755SDavid Hildenbrand if (!is_vcpu_stopped(vcpu)) 5071fe28c786SJanosch Frank return 0; 50728ad35755SDavid Hildenbrand 50736852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1); 50748ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 5075433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 50768ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 50778ad35755SDavid Hildenbrand 5078fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the operating state */ 5079fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 5080fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR); 5081fe28c786SJanosch Frank if (r) { 5082fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5083fe28c786SJanosch Frank return r; 5084fe28c786SJanosch Frank } 5085fe28c786SJanosch Frank } 5086fe28c786SJanosch Frank 50878ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 5088113d10bcSMarc Zyngier if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i))) 50898ad35755SDavid Hildenbrand started_vcpus++; 50908ad35755SDavid Hildenbrand } 50918ad35755SDavid Hildenbrand 50928ad35755SDavid Hildenbrand if (started_vcpus == 0) { 50938ad35755SDavid Hildenbrand /* we're the only active VCPU -> speed it up */ 50948ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(vcpu); 50958ad35755SDavid Hildenbrand } else if (started_vcpus == 1) { 50968ad35755SDavid Hildenbrand /* 50978ad35755SDavid Hildenbrand * As we are starting a second VCPU, we have to disable 50988ad35755SDavid Hildenbrand * the IBS facility on all VCPUs to remove potentially 509938860756SBhaskar Chowdhury * outstanding ENABLE requests. 51008ad35755SDavid Hildenbrand */ 51018ad35755SDavid Hildenbrand __disable_ibs_on_all_vcpus(vcpu->kvm); 51028ad35755SDavid Hildenbrand } 51038ad35755SDavid Hildenbrand 51049daecfc6SDavid Hildenbrand kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED); 51058ad35755SDavid Hildenbrand /* 510672f21820SChristian Borntraeger * The real PSW might have changed due to a RESTART interpreted by the 510772f21820SChristian Borntraeger * ultravisor. We block all interrupts and let the next sie exit 510872f21820SChristian Borntraeger * refresh our view. 510972f21820SChristian Borntraeger */ 511072f21820SChristian Borntraeger if (kvm_s390_pv_cpu_is_protected(vcpu)) 511172f21820SChristian Borntraeger vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK; 511272f21820SChristian Borntraeger /* 51138ad35755SDavid Hildenbrand * Another VCPU might have used IBS while we were offline. 51148ad35755SDavid Hildenbrand * Let's play safe and flush the VCPU at startup. 51158ad35755SDavid Hildenbrand */ 5116d3d692c8SDavid Hildenbrand kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); 5117433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5118fe28c786SJanosch Frank return 0; 51196852d7b6SDavid Hildenbrand } 51206852d7b6SDavid Hildenbrand 5121fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu) 51226852d7b6SDavid Hildenbrand { 5123fe28c786SJanosch Frank int i, online_vcpus, r = 0, started_vcpus = 0; 51248ad35755SDavid Hildenbrand struct kvm_vcpu *started_vcpu = NULL; 51258ad35755SDavid Hildenbrand 51268ad35755SDavid Hildenbrand if (is_vcpu_stopped(vcpu)) 5127fe28c786SJanosch Frank return 0; 51288ad35755SDavid Hildenbrand 51296852d7b6SDavid Hildenbrand trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0); 51308ad35755SDavid Hildenbrand /* Only one cpu at a time may enter/leave the STOPPED state. */ 5131433b9ee4SDavid Hildenbrand spin_lock(&vcpu->kvm->arch.start_stop_lock); 51328ad35755SDavid Hildenbrand online_vcpus = atomic_read(&vcpu->kvm->online_vcpus); 51338ad35755SDavid Hildenbrand 5134fe28c786SJanosch Frank /* Let's tell the UV that we want to change into the stopped state */ 5135fe28c786SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 5136fe28c786SJanosch Frank r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP); 5137fe28c786SJanosch Frank if (r) { 5138fe28c786SJanosch Frank spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5139fe28c786SJanosch Frank return r; 5140fe28c786SJanosch Frank } 5141fe28c786SJanosch Frank } 5142fe28c786SJanosch Frank 5143812de046SEric Farman /* 5144812de046SEric Farman * Set the VCPU to STOPPED and THEN clear the interrupt flag, 5145812de046SEric Farman * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders 5146812de046SEric Farman * have been fully processed. This will ensure that the VCPU 5147812de046SEric Farman * is kept BUSY if another VCPU is inquiring with SIGP SENSE. 5148812de046SEric Farman */ 5149812de046SEric Farman kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED); 51506cddd432SDavid Hildenbrand kvm_s390_clear_stop_irq(vcpu); 515132f5ff63SDavid Hildenbrand 51528ad35755SDavid Hildenbrand __disable_ibs_on_vcpu(vcpu); 51538ad35755SDavid Hildenbrand 51548ad35755SDavid Hildenbrand for (i = 0; i < online_vcpus; i++) { 5155113d10bcSMarc Zyngier struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i); 5156113d10bcSMarc Zyngier 5157113d10bcSMarc Zyngier if (!is_vcpu_stopped(tmp)) { 51588ad35755SDavid Hildenbrand started_vcpus++; 5159113d10bcSMarc Zyngier started_vcpu = tmp; 51608ad35755SDavid Hildenbrand } 51618ad35755SDavid Hildenbrand } 51628ad35755SDavid Hildenbrand 51638ad35755SDavid Hildenbrand if (started_vcpus == 1) { 51648ad35755SDavid Hildenbrand /* 51658ad35755SDavid Hildenbrand * As we only have one VCPU left, we want to enable the 51668ad35755SDavid Hildenbrand * IBS facility for that VCPU to speed it up. 51678ad35755SDavid Hildenbrand */ 51688ad35755SDavid Hildenbrand __enable_ibs_on_vcpu(started_vcpu); 51698ad35755SDavid Hildenbrand } 51708ad35755SDavid Hildenbrand 5171433b9ee4SDavid Hildenbrand spin_unlock(&vcpu->kvm->arch.start_stop_lock); 5172fe28c786SJanosch Frank return 0; 51736852d7b6SDavid Hildenbrand } 51746852d7b6SDavid Hildenbrand 5175d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 5176d6712df9SCornelia Huck struct kvm_enable_cap *cap) 5177d6712df9SCornelia Huck { 5178d6712df9SCornelia Huck int r; 5179d6712df9SCornelia Huck 5180d6712df9SCornelia Huck if (cap->flags) 5181d6712df9SCornelia Huck return -EINVAL; 5182d6712df9SCornelia Huck 5183d6712df9SCornelia Huck switch (cap->cap) { 5184fa6b7fe9SCornelia Huck case KVM_CAP_S390_CSS_SUPPORT: 5185fa6b7fe9SCornelia Huck if (!vcpu->kvm->arch.css_support) { 5186fa6b7fe9SCornelia Huck vcpu->kvm->arch.css_support = 1; 5187c92ea7b9SChristian Borntraeger VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support"); 5188fa6b7fe9SCornelia Huck trace_kvm_s390_enable_css(vcpu->kvm); 5189fa6b7fe9SCornelia Huck } 5190fa6b7fe9SCornelia Huck r = 0; 5191fa6b7fe9SCornelia Huck break; 5192d6712df9SCornelia Huck default: 5193d6712df9SCornelia Huck r = -EINVAL; 5194d6712df9SCornelia Huck break; 5195d6712df9SCornelia Huck } 5196d6712df9SCornelia Huck return r; 5197d6712df9SCornelia Huck } 5198d6712df9SCornelia Huck 51990e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu, 520019e12277SJanosch Frank struct kvm_s390_mem_op *mop) 520119e12277SJanosch Frank { 520219e12277SJanosch Frank void __user *uaddr = (void __user *)mop->buf; 5203b99f4512SNico Boehr void *sida_addr; 520419e12277SJanosch Frank int r = 0; 520519e12277SJanosch Frank 520619e12277SJanosch Frank if (mop->flags || !mop->size) 520719e12277SJanosch Frank return -EINVAL; 520819e12277SJanosch Frank if (mop->size + mop->sida_offset < mop->size) 520919e12277SJanosch Frank return -EINVAL; 521019e12277SJanosch Frank if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block)) 521119e12277SJanosch Frank return -E2BIG; 52122c212e1bSJanis Schoetterl-Glausch if (!kvm_s390_pv_cpu_is_protected(vcpu)) 52132c212e1bSJanis Schoetterl-Glausch return -EINVAL; 521419e12277SJanosch Frank 5215b99f4512SNico Boehr sida_addr = (char *)sida_addr(vcpu->arch.sie_block) + mop->sida_offset; 5216b99f4512SNico Boehr 521719e12277SJanosch Frank switch (mop->op) { 521819e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 5219b99f4512SNico Boehr if (copy_to_user(uaddr, sida_addr, mop->size)) 522019e12277SJanosch Frank r = -EFAULT; 522119e12277SJanosch Frank 522219e12277SJanosch Frank break; 522319e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 5224b99f4512SNico Boehr if (copy_from_user(sida_addr, uaddr, mop->size)) 522519e12277SJanosch Frank r = -EFAULT; 522619e12277SJanosch Frank break; 522719e12277SJanosch Frank } 522819e12277SJanosch Frank return r; 522919e12277SJanosch Frank } 52300e1234c0SJanis Schoetterl-Glausch 52310e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu, 523241408c28SThomas Huth struct kvm_s390_mem_op *mop) 523341408c28SThomas Huth { 523441408c28SThomas Huth void __user *uaddr = (void __user *)mop->buf; 523541408c28SThomas Huth void *tmpbuf = NULL; 523619e12277SJanosch Frank int r = 0; 523741408c28SThomas Huth const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION 5238e9e9feebSJanis Schoetterl-Glausch | KVM_S390_MEMOP_F_CHECK_ONLY 5239e9e9feebSJanis Schoetterl-Glausch | KVM_S390_MEMOP_F_SKEY_PROTECTION; 524041408c28SThomas Huth 5241a13b03bbSThomas Huth if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size) 524241408c28SThomas Huth return -EINVAL; 524341408c28SThomas Huth if (mop->size > MEM_OP_MAX_SIZE) 524441408c28SThomas Huth return -E2BIG; 524519e12277SJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 524619e12277SJanosch Frank return -EINVAL; 5247e9e9feebSJanis Schoetterl-Glausch if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { 5248e9e9feebSJanis Schoetterl-Glausch if (access_key_invalid(mop->key)) 5249e9e9feebSJanis Schoetterl-Glausch return -EINVAL; 5250e9e9feebSJanis Schoetterl-Glausch } else { 5251e9e9feebSJanis Schoetterl-Glausch mop->key = 0; 5252e9e9feebSJanis Schoetterl-Glausch } 525341408c28SThomas Huth if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { 525441408c28SThomas Huth tmpbuf = vmalloc(mop->size); 525541408c28SThomas Huth if (!tmpbuf) 525641408c28SThomas Huth return -ENOMEM; 525741408c28SThomas Huth } 525841408c28SThomas Huth 525941408c28SThomas Huth switch (mop->op) { 526041408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_READ: 526141408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 5262e9e9feebSJanis Schoetterl-Glausch r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, 5263e9e9feebSJanis Schoetterl-Glausch GACC_FETCH, mop->key); 526441408c28SThomas Huth break; 526541408c28SThomas Huth } 5266e9e9feebSJanis Schoetterl-Glausch r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf, 5267e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key); 526841408c28SThomas Huth if (r == 0) { 526941408c28SThomas Huth if (copy_to_user(uaddr, tmpbuf, mop->size)) 527041408c28SThomas Huth r = -EFAULT; 527141408c28SThomas Huth } 527241408c28SThomas Huth break; 527341408c28SThomas Huth case KVM_S390_MEMOP_LOGICAL_WRITE: 527441408c28SThomas Huth if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) { 5275e9e9feebSJanis Schoetterl-Glausch r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size, 5276e9e9feebSJanis Schoetterl-Glausch GACC_STORE, mop->key); 527741408c28SThomas Huth break; 527841408c28SThomas Huth } 527941408c28SThomas Huth if (copy_from_user(tmpbuf, uaddr, mop->size)) { 528041408c28SThomas Huth r = -EFAULT; 528141408c28SThomas Huth break; 528241408c28SThomas Huth } 5283e9e9feebSJanis Schoetterl-Glausch r = write_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf, 5284e9e9feebSJanis Schoetterl-Glausch mop->size, mop->key); 528541408c28SThomas Huth break; 528641408c28SThomas Huth } 528741408c28SThomas Huth 528841408c28SThomas Huth if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0) 528941408c28SThomas Huth kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm); 529041408c28SThomas Huth 529141408c28SThomas Huth vfree(tmpbuf); 529241408c28SThomas Huth return r; 529341408c28SThomas Huth } 529441408c28SThomas Huth 52950e1234c0SJanis Schoetterl-Glausch static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu, 529619e12277SJanosch Frank struct kvm_s390_mem_op *mop) 529719e12277SJanosch Frank { 529819e12277SJanosch Frank int r, srcu_idx; 529919e12277SJanosch Frank 530019e12277SJanosch Frank srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 530119e12277SJanosch Frank 530219e12277SJanosch Frank switch (mop->op) { 530319e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_READ: 530419e12277SJanosch Frank case KVM_S390_MEMOP_LOGICAL_WRITE: 53050e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_mem_op(vcpu, mop); 530619e12277SJanosch Frank break; 530719e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_READ: 530819e12277SJanosch Frank case KVM_S390_MEMOP_SIDA_WRITE: 530919e12277SJanosch Frank /* we are locked against sida going away by the vcpu->mutex */ 53100e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_sida_op(vcpu, mop); 531119e12277SJanosch Frank break; 531219e12277SJanosch Frank default: 531319e12277SJanosch Frank r = -EINVAL; 531419e12277SJanosch Frank } 531519e12277SJanosch Frank 531619e12277SJanosch Frank srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 531719e12277SJanosch Frank return r; 531819e12277SJanosch Frank } 531919e12277SJanosch Frank 53205cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp, 5321b0c632dbSHeiko Carstens unsigned int ioctl, unsigned long arg) 5322b0c632dbSHeiko Carstens { 5323b0c632dbSHeiko Carstens struct kvm_vcpu *vcpu = filp->private_data; 5324b0c632dbSHeiko Carstens void __user *argp = (void __user *)arg; 5325b0c632dbSHeiko Carstens 532693736624SAvi Kivity switch (ioctl) { 532747b43c52SJens Freimann case KVM_S390_IRQ: { 532847b43c52SJens Freimann struct kvm_s390_irq s390irq; 532947b43c52SJens Freimann 533047b43c52SJens Freimann if (copy_from_user(&s390irq, argp, sizeof(s390irq))) 53319b062471SChristoffer Dall return -EFAULT; 53329b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 533347b43c52SJens Freimann } 533493736624SAvi Kivity case KVM_S390_INTERRUPT: { 5335ba5c1e9bSCarsten Otte struct kvm_s390_interrupt s390int; 533653936b5bSThomas Huth struct kvm_s390_irq s390irq = {}; 5337ba5c1e9bSCarsten Otte 5338ba5c1e9bSCarsten Otte if (copy_from_user(&s390int, argp, sizeof(s390int))) 53399b062471SChristoffer Dall return -EFAULT; 5340383d0b05SJens Freimann if (s390int_to_s390irq(&s390int, &s390irq)) 5341383d0b05SJens Freimann return -EINVAL; 53429b062471SChristoffer Dall return kvm_s390_inject_vcpu(vcpu, &s390irq); 5343ba5c1e9bSCarsten Otte } 53449b062471SChristoffer Dall } 53455cb0944cSPaolo Bonzini return -ENOIOCTLCMD; 53465cb0944cSPaolo Bonzini } 53475cb0944cSPaolo Bonzini 53488aba0958SJanosch Frank static int kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu *vcpu, 53498aba0958SJanosch Frank struct kvm_pv_cmd *cmd) 53508aba0958SJanosch Frank { 53518aba0958SJanosch Frank struct kvm_s390_pv_dmp dmp; 53528aba0958SJanosch Frank void *data; 53538aba0958SJanosch Frank int ret; 53548aba0958SJanosch Frank 53558aba0958SJanosch Frank /* Dump initialization is a prerequisite */ 53568aba0958SJanosch Frank if (!vcpu->kvm->arch.pv.dumping) 53578aba0958SJanosch Frank return -EINVAL; 53588aba0958SJanosch Frank 53598aba0958SJanosch Frank if (copy_from_user(&dmp, (__u8 __user *)cmd->data, sizeof(dmp))) 53608aba0958SJanosch Frank return -EFAULT; 53618aba0958SJanosch Frank 53628aba0958SJanosch Frank /* We only handle this subcmd right now */ 53638aba0958SJanosch Frank if (dmp.subcmd != KVM_PV_DUMP_CPU) 53648aba0958SJanosch Frank return -EINVAL; 53658aba0958SJanosch Frank 53668aba0958SJanosch Frank /* CPU dump length is the same as create cpu storage donation. */ 53678aba0958SJanosch Frank if (dmp.buff_len != uv_info.guest_cpu_stor_len) 53688aba0958SJanosch Frank return -EINVAL; 53698aba0958SJanosch Frank 53708aba0958SJanosch Frank data = kvzalloc(uv_info.guest_cpu_stor_len, GFP_KERNEL); 53718aba0958SJanosch Frank if (!data) 53728aba0958SJanosch Frank return -ENOMEM; 53738aba0958SJanosch Frank 53748aba0958SJanosch Frank ret = kvm_s390_pv_dump_cpu(vcpu, data, &cmd->rc, &cmd->rrc); 53758aba0958SJanosch Frank 53768aba0958SJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT DUMP CPU %d rc %x rrc %x", 53778aba0958SJanosch Frank vcpu->vcpu_id, cmd->rc, cmd->rrc); 53788aba0958SJanosch Frank 53798aba0958SJanosch Frank if (ret) 53808aba0958SJanosch Frank ret = -EINVAL; 53818aba0958SJanosch Frank 53828aba0958SJanosch Frank /* On success copy over the dump data */ 53838aba0958SJanosch Frank if (!ret && copy_to_user((__u8 __user *)dmp.buff_addr, data, uv_info.guest_cpu_stor_len)) 53848aba0958SJanosch Frank ret = -EFAULT; 53858aba0958SJanosch Frank 53868aba0958SJanosch Frank kvfree(data); 53878aba0958SJanosch Frank return ret; 53888aba0958SJanosch Frank } 53898aba0958SJanosch Frank 53905cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp, 53915cb0944cSPaolo Bonzini unsigned int ioctl, unsigned long arg) 53925cb0944cSPaolo Bonzini { 53935cb0944cSPaolo Bonzini struct kvm_vcpu *vcpu = filp->private_data; 53945cb0944cSPaolo Bonzini void __user *argp = (void __user *)arg; 53955cb0944cSPaolo Bonzini int idx; 53965cb0944cSPaolo Bonzini long r; 53978a8378faSJanosch Frank u16 rc, rrc; 53989b062471SChristoffer Dall 53999b062471SChristoffer Dall vcpu_load(vcpu); 54009b062471SChristoffer Dall 54019b062471SChristoffer Dall switch (ioctl) { 5402b0c632dbSHeiko Carstens case KVM_S390_STORE_STATUS: 5403800c1065SThomas Huth idx = srcu_read_lock(&vcpu->kvm->srcu); 540455680890SChristian Borntraeger r = kvm_s390_store_status_unloaded(vcpu, arg); 5405800c1065SThomas Huth srcu_read_unlock(&vcpu->kvm->srcu, idx); 5406bc923cc9SAvi Kivity break; 5407b0c632dbSHeiko Carstens case KVM_S390_SET_INITIAL_PSW: { 5408b0c632dbSHeiko Carstens psw_t psw; 5409b0c632dbSHeiko Carstens 5410bc923cc9SAvi Kivity r = -EFAULT; 5411b0c632dbSHeiko Carstens if (copy_from_user(&psw, argp, sizeof(psw))) 5412bc923cc9SAvi Kivity break; 5413bc923cc9SAvi Kivity r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw); 5414bc923cc9SAvi Kivity break; 5415b0c632dbSHeiko Carstens } 54167de3f142SJanosch Frank case KVM_S390_CLEAR_RESET: 54177de3f142SJanosch Frank r = 0; 54187de3f142SJanosch Frank kvm_arch_vcpu_ioctl_clear_reset(vcpu); 54198a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54208a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54218a8378faSJanosch Frank UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc); 54228a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x", 54238a8378faSJanosch Frank rc, rrc); 54248a8378faSJanosch Frank } 54257de3f142SJanosch Frank break; 5426b0c632dbSHeiko Carstens case KVM_S390_INITIAL_RESET: 54277de3f142SJanosch Frank r = 0; 54287de3f142SJanosch Frank kvm_arch_vcpu_ioctl_initial_reset(vcpu); 54298a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54308a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54318a8378faSJanosch Frank UVC_CMD_CPU_RESET_INITIAL, 54328a8378faSJanosch Frank &rc, &rrc); 54338a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x", 54348a8378faSJanosch Frank rc, rrc); 54358a8378faSJanosch Frank } 54367de3f142SJanosch Frank break; 54377de3f142SJanosch Frank case KVM_S390_NORMAL_RESET: 54387de3f142SJanosch Frank r = 0; 54397de3f142SJanosch Frank kvm_arch_vcpu_ioctl_normal_reset(vcpu); 54408a8378faSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) { 54418a8378faSJanosch Frank r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu), 54428a8378faSJanosch Frank UVC_CMD_CPU_RESET, &rc, &rrc); 54438a8378faSJanosch Frank VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x", 54448a8378faSJanosch Frank rc, rrc); 54458a8378faSJanosch Frank } 5446bc923cc9SAvi Kivity break; 544714eebd91SCarsten Otte case KVM_SET_ONE_REG: 544814eebd91SCarsten Otte case KVM_GET_ONE_REG: { 544914eebd91SCarsten Otte struct kvm_one_reg reg; 545068cf7b1fSJanosch Frank r = -EINVAL; 545168cf7b1fSJanosch Frank if (kvm_s390_pv_cpu_is_protected(vcpu)) 545268cf7b1fSJanosch Frank break; 545314eebd91SCarsten Otte r = -EFAULT; 545414eebd91SCarsten Otte if (copy_from_user(®, argp, sizeof(reg))) 545514eebd91SCarsten Otte break; 545614eebd91SCarsten Otte if (ioctl == KVM_SET_ONE_REG) 545714eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®); 545814eebd91SCarsten Otte else 545914eebd91SCarsten Otte r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®); 546014eebd91SCarsten Otte break; 546114eebd91SCarsten Otte } 546227e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 546327e0393fSCarsten Otte case KVM_S390_UCAS_MAP: { 546427e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 546527e0393fSCarsten Otte 546627e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 546727e0393fSCarsten Otte r = -EFAULT; 546827e0393fSCarsten Otte break; 546927e0393fSCarsten Otte } 547027e0393fSCarsten Otte 547127e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 547227e0393fSCarsten Otte r = -EINVAL; 547327e0393fSCarsten Otte break; 547427e0393fSCarsten Otte } 547527e0393fSCarsten Otte 547627e0393fSCarsten Otte r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr, 547727e0393fSCarsten Otte ucasmap.vcpu_addr, ucasmap.length); 547827e0393fSCarsten Otte break; 547927e0393fSCarsten Otte } 548027e0393fSCarsten Otte case KVM_S390_UCAS_UNMAP: { 548127e0393fSCarsten Otte struct kvm_s390_ucas_mapping ucasmap; 548227e0393fSCarsten Otte 548327e0393fSCarsten Otte if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) { 548427e0393fSCarsten Otte r = -EFAULT; 548527e0393fSCarsten Otte break; 548627e0393fSCarsten Otte } 548727e0393fSCarsten Otte 548827e0393fSCarsten Otte if (!kvm_is_ucontrol(vcpu->kvm)) { 548927e0393fSCarsten Otte r = -EINVAL; 549027e0393fSCarsten Otte break; 549127e0393fSCarsten Otte } 549227e0393fSCarsten Otte 549327e0393fSCarsten Otte r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr, 549427e0393fSCarsten Otte ucasmap.length); 549527e0393fSCarsten Otte break; 549627e0393fSCarsten Otte } 549727e0393fSCarsten Otte #endif 5498ccc7910fSCarsten Otte case KVM_S390_VCPU_FAULT: { 5499527e30b4SMartin Schwidefsky r = gmap_fault(vcpu->arch.gmap, arg, 0); 5500ccc7910fSCarsten Otte break; 5501ccc7910fSCarsten Otte } 5502d6712df9SCornelia Huck case KVM_ENABLE_CAP: 5503d6712df9SCornelia Huck { 5504d6712df9SCornelia Huck struct kvm_enable_cap cap; 5505d6712df9SCornelia Huck r = -EFAULT; 5506d6712df9SCornelia Huck if (copy_from_user(&cap, argp, sizeof(cap))) 5507d6712df9SCornelia Huck break; 5508d6712df9SCornelia Huck r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 5509d6712df9SCornelia Huck break; 5510d6712df9SCornelia Huck } 551141408c28SThomas Huth case KVM_S390_MEM_OP: { 551241408c28SThomas Huth struct kvm_s390_mem_op mem_op; 551341408c28SThomas Huth 551441408c28SThomas Huth if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0) 55150e1234c0SJanis Schoetterl-Glausch r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op); 551641408c28SThomas Huth else 551741408c28SThomas Huth r = -EFAULT; 551841408c28SThomas Huth break; 551941408c28SThomas Huth } 5520816c7667SJens Freimann case KVM_S390_SET_IRQ_STATE: { 5521816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5522816c7667SJens Freimann 5523816c7667SJens Freimann r = -EFAULT; 5524816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5525816c7667SJens Freimann break; 5526816c7667SJens Freimann if (irq_state.len > VCPU_IRQS_MAX_BUF || 5527816c7667SJens Freimann irq_state.len == 0 || 5528816c7667SJens Freimann irq_state.len % sizeof(struct kvm_s390_irq) > 0) { 5529816c7667SJens Freimann r = -EINVAL; 5530816c7667SJens Freimann break; 5531816c7667SJens Freimann } 5532bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5533816c7667SJens Freimann r = kvm_s390_set_irq_state(vcpu, 5534816c7667SJens Freimann (void __user *) irq_state.buf, 5535816c7667SJens Freimann irq_state.len); 5536816c7667SJens Freimann break; 5537816c7667SJens Freimann } 5538816c7667SJens Freimann case KVM_S390_GET_IRQ_STATE: { 5539816c7667SJens Freimann struct kvm_s390_irq_state irq_state; 5540816c7667SJens Freimann 5541816c7667SJens Freimann r = -EFAULT; 5542816c7667SJens Freimann if (copy_from_user(&irq_state, argp, sizeof(irq_state))) 5543816c7667SJens Freimann break; 5544816c7667SJens Freimann if (irq_state.len == 0) { 5545816c7667SJens Freimann r = -EINVAL; 5546816c7667SJens Freimann break; 5547816c7667SJens Freimann } 5548bb64da9aSChristian Borntraeger /* do not use irq_state.flags, it will break old QEMUs */ 5549816c7667SJens Freimann r = kvm_s390_get_irq_state(vcpu, 5550816c7667SJens Freimann (__u8 __user *) irq_state.buf, 5551816c7667SJens Freimann irq_state.len); 5552816c7667SJens Freimann break; 5553816c7667SJens Freimann } 55548aba0958SJanosch Frank case KVM_S390_PV_CPU_COMMAND: { 55558aba0958SJanosch Frank struct kvm_pv_cmd cmd; 55568aba0958SJanosch Frank 55578aba0958SJanosch Frank r = -EINVAL; 55588aba0958SJanosch Frank if (!is_prot_virt_host()) 55598aba0958SJanosch Frank break; 55608aba0958SJanosch Frank 55618aba0958SJanosch Frank r = -EFAULT; 55628aba0958SJanosch Frank if (copy_from_user(&cmd, argp, sizeof(cmd))) 55638aba0958SJanosch Frank break; 55648aba0958SJanosch Frank 55658aba0958SJanosch Frank r = -EINVAL; 55668aba0958SJanosch Frank if (cmd.flags) 55678aba0958SJanosch Frank break; 55688aba0958SJanosch Frank 55698aba0958SJanosch Frank /* We only handle this cmd right now */ 55708aba0958SJanosch Frank if (cmd.cmd != KVM_PV_DUMP) 55718aba0958SJanosch Frank break; 55728aba0958SJanosch Frank 55738aba0958SJanosch Frank r = kvm_s390_handle_pv_vcpu_dump(vcpu, &cmd); 55748aba0958SJanosch Frank 55758aba0958SJanosch Frank /* Always copy over UV rc / rrc data */ 55768aba0958SJanosch Frank if (copy_to_user((__u8 __user *)argp, &cmd.rc, 55778aba0958SJanosch Frank sizeof(cmd.rc) + sizeof(cmd.rrc))) 55788aba0958SJanosch Frank r = -EFAULT; 55798aba0958SJanosch Frank break; 55808aba0958SJanosch Frank } 5581b0c632dbSHeiko Carstens default: 55823e6afcf1SCarsten Otte r = -ENOTTY; 5583b0c632dbSHeiko Carstens } 55849b062471SChristoffer Dall 55859b062471SChristoffer Dall vcpu_put(vcpu); 5586bc923cc9SAvi Kivity return r; 5587b0c632dbSHeiko Carstens } 5588b0c632dbSHeiko Carstens 55891499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 55905b1c1493SCarsten Otte { 55915b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL 55925b1c1493SCarsten Otte if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET) 55935b1c1493SCarsten Otte && (kvm_is_ucontrol(vcpu->kvm))) { 55945b1c1493SCarsten Otte vmf->page = virt_to_page(vcpu->arch.sie_block); 55955b1c1493SCarsten Otte get_page(vmf->page); 55965b1c1493SCarsten Otte return 0; 55975b1c1493SCarsten Otte } 55985b1c1493SCarsten Otte #endif 55995b1c1493SCarsten Otte return VM_FAULT_SIGBUS; 56005b1c1493SCarsten Otte } 56015b1c1493SCarsten Otte 5602b0c632dbSHeiko Carstens /* Section: memory related */ 5603f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 5604537a17b3SSean Christopherson const struct kvm_memory_slot *old, 5605537a17b3SSean Christopherson struct kvm_memory_slot *new, 56067b6195a9STakuya Yoshikawa enum kvm_mr_change change) 5607b0c632dbSHeiko Carstens { 5608ec5c8697SSean Christopherson gpa_t size; 5609ec5c8697SSean Christopherson 5610ec5c8697SSean Christopherson /* When we are protected, we should not change the memory slots */ 5611ec5c8697SSean Christopherson if (kvm_s390_pv_get_handle(kvm)) 5612ec5c8697SSean Christopherson return -EINVAL; 5613ec5c8697SSean Christopherson 5614ec5c8697SSean Christopherson if (change == KVM_MR_DELETE || change == KVM_MR_FLAGS_ONLY) 5615ec5c8697SSean Christopherson return 0; 5616cf5b4869SSean Christopherson 5617dd2887e7SNick Wang /* A few sanity checks. We can have memory slots which have to be 5618dd2887e7SNick Wang located/ended at a segment boundary (1MB). The memory in userland is 5619dd2887e7SNick Wang ok to be fragmented into various different vmas. It is okay to mmap() 5620dd2887e7SNick Wang and munmap() stuff in this slot after doing this call at any time */ 5621b0c632dbSHeiko Carstens 5622cf5b4869SSean Christopherson if (new->userspace_addr & 0xffffful) 5623b0c632dbSHeiko Carstens return -EINVAL; 5624b0c632dbSHeiko Carstens 5625ec5c8697SSean Christopherson size = new->npages * PAGE_SIZE; 5626cf5b4869SSean Christopherson if (size & 0xffffful) 5627b0c632dbSHeiko Carstens return -EINVAL; 5628b0c632dbSHeiko Carstens 5629cf5b4869SSean Christopherson if ((new->base_gfn * PAGE_SIZE) + size > kvm->arch.mem_limit) 5630a3a92c31SDominik Dingel return -EINVAL; 5631a3a92c31SDominik Dingel 5632f7784b8eSMarcelo Tosatti return 0; 5633f7784b8eSMarcelo Tosatti } 5634f7784b8eSMarcelo Tosatti 5635f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 56369d4c197cSSean Christopherson struct kvm_memory_slot *old, 5637f36f3f28SPaolo Bonzini const struct kvm_memory_slot *new, 56388482644aSTakuya Yoshikawa enum kvm_mr_change change) 5639f7784b8eSMarcelo Tosatti { 564019ec166cSChristian Borntraeger int rc = 0; 5641f7784b8eSMarcelo Tosatti 564219ec166cSChristian Borntraeger switch (change) { 564319ec166cSChristian Borntraeger case KVM_MR_DELETE: 564419ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 564519ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 564619ec166cSChristian Borntraeger break; 564719ec166cSChristian Borntraeger case KVM_MR_MOVE: 564819ec166cSChristian Borntraeger rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE, 564919ec166cSChristian Borntraeger old->npages * PAGE_SIZE); 565019ec166cSChristian Borntraeger if (rc) 565119ec166cSChristian Borntraeger break; 56523b684a42SJoe Perches fallthrough; 565319ec166cSChristian Borntraeger case KVM_MR_CREATE: 5654cf5b4869SSean Christopherson rc = gmap_map_segment(kvm->arch.gmap, new->userspace_addr, 5655cf5b4869SSean Christopherson new->base_gfn * PAGE_SIZE, 5656cf5b4869SSean Christopherson new->npages * PAGE_SIZE); 565719ec166cSChristian Borntraeger break; 565819ec166cSChristian Borntraeger case KVM_MR_FLAGS_ONLY: 565919ec166cSChristian Borntraeger break; 566019ec166cSChristian Borntraeger default: 566119ec166cSChristian Borntraeger WARN(1, "Unknown KVM MR CHANGE: %d\n", change); 566219ec166cSChristian Borntraeger } 5663598841caSCarsten Otte if (rc) 5664ea2cdd27SDavid Hildenbrand pr_warn("failed to commit memory region\n"); 5665598841caSCarsten Otte return; 5666b0c632dbSHeiko Carstens } 5667b0c632dbSHeiko Carstens 566860a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i) 566960a37709SAlexander Yarygin { 567060a37709SAlexander Yarygin unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30; 567160a37709SAlexander Yarygin 567260a37709SAlexander Yarygin return 0x0000ffffffffffffUL >> (nonhyp_fai << 4); 567360a37709SAlexander Yarygin } 567460a37709SAlexander Yarygin 5675b0c632dbSHeiko Carstens static int __init kvm_s390_init(void) 5676b0c632dbSHeiko Carstens { 567760a37709SAlexander Yarygin int i; 567860a37709SAlexander Yarygin 567907197fd0SDavid Hildenbrand if (!sclp.has_sief2) { 56808d43d570SMichael Mueller pr_info("SIE is not available\n"); 568107197fd0SDavid Hildenbrand return -ENODEV; 568207197fd0SDavid Hildenbrand } 568307197fd0SDavid Hildenbrand 5684a4499382SJanosch Frank if (nested && hpage) { 56858d43d570SMichael Mueller pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n"); 5686a4499382SJanosch Frank return -EINVAL; 5687a4499382SJanosch Frank } 5688a4499382SJanosch Frank 568960a37709SAlexander Yarygin for (i = 0; i < 16; i++) 5690c3b9e3e1SChristian Borntraeger kvm_s390_fac_base[i] |= 569117e89e13SSven Schnelle stfle_fac_list[i] & nonhyp_mask(i); 569260a37709SAlexander Yarygin 56939d8d5786SMichael Mueller return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 5694b0c632dbSHeiko Carstens } 5695b0c632dbSHeiko Carstens 5696b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void) 5697b0c632dbSHeiko Carstens { 5698b0c632dbSHeiko Carstens kvm_exit(); 5699b0c632dbSHeiko Carstens } 5700b0c632dbSHeiko Carstens 5701b0c632dbSHeiko Carstens module_init(kvm_s390_init); 5702b0c632dbSHeiko Carstens module_exit(kvm_s390_exit); 5703566af940SCornelia Huck 5704566af940SCornelia Huck /* 5705566af940SCornelia Huck * Enable autoloading of the kvm module. 5706566af940SCornelia Huck * Note that we add the module alias here instead of virt/kvm/kvm_main.c 5707566af940SCornelia Huck * since x86 takes a different approach. 5708566af940SCornelia Huck */ 5709566af940SCornelia Huck #include <linux/miscdevice.h> 5710566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR); 5711566af940SCornelia Huck MODULE_ALIAS("devname:kvm"); 5712